One of the most common runtime errors we have seen in gcc-4_7 is
caused by dangling references to temporaries whole life time have
ended

e.g,

 const A& a = foo();

or
foo (A());// where temp's address is saved and used after foo.

Of course this is user error according to the standard, triaging of
bugs like this is pretty time consuming to triage. This patch tries to
introduce an option to disable stack reuse for temporaries, which can
be used to debugging purpose.

Is this good for trunk?

thanks,

David

2012-06-20  Xinliang David Li  <davi...@google.com>

        * common.opt: -ftemp-reuse-stack option.
        * gimplify.c (gimplify_target_expr): Check new flag.



Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi     (revision 188362)
+++ doc/invoke.texi     (working copy)
@@ -1003,6 +1003,7 @@ See S/390 and zSeries Options.
 -fstack-limit-register=@var{reg}  -fstack-limit-symbol=@var{sym} @gol
 -fno-stack-limit -fsplit-stack @gol
 -fleading-underscore  -ftls-model=@var{model} @gol
+-ftemp-stack-reuse @gol
 -ftrapv  -fwrapv  -fbounds-check @gol
 -fvisibility -fstrict-volatile-bitfields}
 @end table
@@ -19500,6 +19501,10 @@ indices used to access arrays are within
 currently only supported by the Java and Fortran front ends, where
 this option defaults to true and false respectively.

+@item -ftemp-stack-reuse
+@opindex ftemp_stack_reuse
+This option enables stack space reuse for temporaries. The default is on.
+
 @item -ftrapv
 @opindex ftrapv
 This option generates traps for signed overflow on addition, subtraction,
Index: gimplify.c
===================================================================
--- gimplify.c  (revision 188362)
+++ gimplify.c  (working copy)
@@ -5487,7 +5487,8 @@ gimplify_target_expr (tree *expr_p, gimp
       /* Add a clobber for the temporary going out of scope, like
         gimplify_bind_expr.  */
       if (gimplify_ctxp->in_cleanup_point_expr
-         && needs_to_live_in_memory (temp))
+         && needs_to_live_in_memory (temp)
+         && flag_temp_stack_reuse)
        {
          tree clobber = build_constructor (TREE_TYPE (temp), NULL);
          TREE_THIS_VOLATILE (clobber) = true;
Index: common.opt
===================================================================
--- common.opt  (revision 188362)
+++ common.opt  (working copy)
@@ -1322,6 +1322,10 @@ fif-conversion2
 Common Report Var(flag_if_conversion2) Optimization
 Perform conversion of conditional jumps to conditional execution

+ftemp-stack-reuse
+Common Report Var(flag_temp_stack_reuse) Init(1)
+Enable stack reuse for compiler generated temps
+
 ftree-loop-if-convert
 Common Report Var(flag_tree_loop_if_convert) Init(-1) Optimization
 Convert conditional jumps in innermost loops to branchless equivalents

Reply via email to