[partial backport of gcc-4.8.1 r199179 ] gcc/ 2013-05-22 Richard Biener Backport from mainline 2013-05-21 Richard Biener PR tree-optimization/57303 * tree-ssa-sink.c (statement_sink_location): Properly handle self-assignments. gcc/testsuite/ 2013-05-22 Richard Biener Backport from mainline 2013-05-21 Richard Biener PR tree-optimization/57303 * gcc.dg/torture/pr57303.c: New testcase. --- gcc-4.7.3/gcc/testsuite/gcc.dg/torture/pr57303.c.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.7.3/gcc/testsuite/gcc.dg/torture/pr57303.c 2013-06-16 14:15:07.954607303 +0200 @@ -0,0 +1,33 @@ +/* { dg-do run } */ + +void abort (void); + +struct S0 +{ + int f0; +}; +struct S1 +{ + struct S0 f0; +}; + +struct S1 x = { {0} }; +struct S1 y = { {1} }; + +static void +foo (struct S0 p) +{ + struct S0 *l = &y.f0; + *l = x.f0; + if (p.f0) + *l = *l; +} + +int +main () +{ + foo(y.f0); + if (y.f0.f0 != 0) + abort (); + return 0; +} --- gcc-4.7.3/gcc/tree-ssa-sink.c.~1~ 2011-10-26 15:26:50.000000000 +0200 +++ gcc-4.7.3/gcc/tree-ssa-sink.c 2013-06-16 14:15:07.954607303 +0200 @@ -412,7 +412,15 @@ statement_sink_location (gimple stmt, ba && gimple_vdef (use_stmt) && operand_equal_p (gimple_assign_lhs (stmt), gimple_assign_lhs (use_stmt), 0)) - continue; + { + /* If use_stmt is or might be a nop assignment then USE_STMT + acts as a use as well as definition. */ + if (stmt != use_stmt + && ref_maybe_used_by_stmt_p (use_stmt, + gimple_assign_lhs (stmt))) + return false; + continue; + } if (gimple_code (use_stmt) != GIMPLE_PHI) return false;