This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR40389


Thix fixes PR40389 - we have to assume that the return value has
its address taken when NRV is in effect (this may not be a 100%
complete patch, as we probably also have to assume that it escaped).

At least it fixes the miscompile.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.

Richard.

2009-06-13  Richard Guenther  <rguenther@suse.de>

	PR middle-end/40389
	* gimple.c (walk_stmt_load_store_addr_ops): The LHS of a call
	has its address taken if NRV was applied.
	* tree-ssa-operands.c (parse_ssa_operands): Adjust likewise.

	* g++.dg/torture/pr40389.C: New testcase.

Index: gcc/gimple.c
===================================================================
*** gcc/gimple.c	(revision 148453)
--- gcc/gimple.c	(working copy)
*************** walk_stmt_load_store_addr_ops (gimple st
*** 3264,3269 ****
--- 3264,3272 ----
  	  && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
  	ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
  			   data);
+       if (visit_addr
+ 	  && gimple_call_return_slot_opt_p (stmt))
+ 	ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
      }
    else if (gimple_code (stmt) == GIMPLE_ASM)
      {
Index: gcc/tree-ssa-operands.c
===================================================================
*** gcc/tree-ssa-operands.c	(revision 148453)
--- gcc/tree-ssa-operands.c	(working copy)
*************** parse_ssa_operands (gimple stmt)
*** 1036,1041 ****
--- 1036,1045 ----
  	  start = 1;
  	}
  
+       if (code == GIMPLE_CALL
+ 	  && gimple_call_return_slot_opt_p (stmt))
+ 	mark_address_taken (gimple_call_lhs (stmt));
+ 
        for (i = start; i < gimple_num_ops (stmt); i++)
  	get_expr_operands (stmt, gimple_op_ptr (stmt, i), opf_use);
  
Index: gcc/testsuite/g++.dg/torture/pr40389.C
===================================================================
*** gcc/testsuite/g++.dg/torture/pr40389.C	(revision 0)
--- gcc/testsuite/g++.dg/torture/pr40389.C	(revision 0)
***************
*** 0 ****
--- 1,82 ----
+ template <typename V> struct S
+ {
+   V *f, *l;
+   __attribute__ ((noinline)) S (void) { f = 0, l = 0; }
+   void foo (V *x)
+   {
+     if (x->p != 0)
+       x->p->n = x->n;
+     else
+       f = x->n;
+     if (x->n != 0)
+       x->n->p = x->p;
+     else
+       l = x->p;
+   }
+   __attribute__ ((noinline)) void bar (V *x)
+   {
+     x->n = 0;
+     x->p = l;
+     if (l != 0)
+       l->n = x;
+     else
+       f = x;
+     l = x;
+   }
+ };
+ 
+ struct H;
+ 
+ struct A
+ {
+   S <H> k;
+ };
+ 
+ struct H
+ {
+   A *a;
+   H *p, *n;
+   __attribute__ ((noinline)) H (void) { p = 0, n = 0, a = 0; }
+   __attribute__ ((noinline)) H (A *b) : a (b)
+   {
+     p = 0;
+     n = 0;
+     if (a != 0)
+       a->k.bar (this);
+   }
+   __attribute__ ((noinline)) H (const H &h) : a (h.a)
+   {
+     p = 0;
+     n = 0;
+     if (a != 0)
+       a->k.bar (this);
+   }
+   ~H (void) { if (a != 0) a->k.foo (this); }
+   H &operator= (const H &o)
+   {
+     if (a != 0 || &o == this)
+       __builtin_abort ();
+     a = o.a;
+     if (a != 0)
+       a->k.bar (this);
+     return *this;
+   }
+ };
+ 
+ __attribute__ ((noinline))
+ H baz (void)
+ {
+   return H (new A);
+ }
+ 
+ H g;
+ 
+ int
+ main (void)
+ {
+   g = baz ();
+   if (g.a->k.f != &g)
+     __builtin_abort ();
+   return 0;
+ }
+ 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]