[backport from gcc-4.7/trunk r183752 ] gcc/ 2012-01-31 Richard Guenther PR tree-optimization/51528 * tree-sra.c (sra_modify_assign): Avoid copy-in/out for aggregate assigns. gcc/testsuite/ 2012-01-31 Richard Guenther PR tree-optimization/51528 * gcc.dg/torture/pr51528.c: New testcase. --- gcc-4.6.2/gcc/testsuite/gcc.dg/torture/pr51528.c.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.6.2/gcc/testsuite/gcc.dg/torture/pr51528.c 2012-02-05 21:47:04.000000000 +0100 @@ -0,0 +1,46 @@ +/* { dg-do run } */ +/* { dg-options "-fno-early-inlining" } */ + +extern void abort (void); + +union U +{ + int i; + _Bool b; +}; + +_Bool gb; + +void __attribute__ ((noinline)) +use_bool (union U u) +{ + gb = u.b; +} + +union U +bar (void) +{ + union U u; + u.i = 0xFFFE; + return u; +} + +union U __attribute__ ((noinline)) +foo (void) +{ + union U u,v; + + u.b = 1; + use_bool (u); + u = bar (); + + return u; +} + +int main (int argc, char **argv) +{ + union U u = foo (); + if (u.i != 0xFFFE) + abort (); + return 0; +} --- gcc-4.6.2/gcc/tree-sra.c.~1~ 2012-02-05 21:45:41.000000000 +0100 +++ gcc-4.6.2/gcc/tree-sra.c 2012-02-05 21:47:04.000000000 +0100 @@ -2934,9 +2934,14 @@ sra_modify_assign (gimple *stmt, gimple_ sra_stats.deleted++; return SRA_AM_REMOVED; } + /* Restore the aggregate RHS from its components so the + prevailing aggregate copy does the right thing. */ if (access_has_children_p (racc)) - generate_subtree_copies (racc->first_child, lhs, racc->offset, - 0, 0, gsi, false, true, loc); + generate_subtree_copies (racc->first_child, racc->base, 0, 0, 0, + gsi, false, false, loc); + /* Re-load the components of the aggregate copy destination. + But use the RHS aggregate to load from to expose more + optimization opportunities. */ if (access_has_children_p (lacc)) generate_subtree_copies (lacc->first_child, rhs, lacc->offset, 0, 0, gsi, true, true, loc);