From: Richard Biener Date: Thu, 1 Jul 2021 09:16:01 +0000 (+0200) Subject: tree-optimization/101278 - handle self-use in DSE analysis X-Git-Tag: basepoints/gcc-13~6361 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a87605938428f6c4c62d5b92cfc183cd2b2554e;p=thirdparty%2Fgcc.git tree-optimization/101278 - handle self-use in DSE analysis DSE store classification short-cuts the to-be classified stmt itself from chaining but fails to first check whether the store uses itself which can be the case when it is a call with the LHS also passed by value as argument. 2021-07-01 Richard Biener PR tree-optimization/101278 * tree-ssa-dse.c (dse_classify_store): First check for uses, then ignore stmt for chaining purposes. * gcc.dg/torture/pr101278.c: New testcase. --- diff --git a/gcc/testsuite/gcc.dg/torture/pr101278.c b/gcc/testsuite/gcc.dg/torture/pr101278.c new file mode 100644 index 000000000000..1d25658f37db --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr101278.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ + +struct X { int counter; }; + +struct X __attribute__((noipa)) foo (struct X x) +{ + x.counter++; + if (x.counter == 5) + __builtin_exit (0); + return x; +} + +int +main () +{ + struct X x; + x.counter = 0; + for (int i = 0; i < 10; ++i) + x = foo (x); + __builtin_abort (); +} diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c index c3939a6417f4..98daa8ab24c9 100644 --- a/gcc/tree-ssa-dse.c +++ b/gcc/tree-ssa-dse.c @@ -813,15 +813,11 @@ dse_classify_store (ao_ref *ref, gimple *stmt, break; } - /* We have visited ourselves already so ignore STMT for the - purpose of chaining. */ - if (use_stmt == stmt) - ; /* In simple cases we can look through PHI nodes, but we have to be careful with loops and with memory references containing operands that are also operands of PHI nodes. See gcc.c-torture/execute/20051110-*.c. */ - else if (gimple_code (use_stmt) == GIMPLE_PHI) + if (gimple_code (use_stmt) == GIMPLE_PHI) { /* If we already visited this PHI ignore it for further processing. */ @@ -861,6 +857,10 @@ dse_classify_store (ao_ref *ref, gimple *stmt, fail = true; break; } + /* We have visited ourselves already so ignore STMT for the + purpose of chaining. */ + else if (use_stmt == stmt) + ; /* If this is a store, remember it as we possibly need to walk the defs uses. */ else if (gimple_vdef (use_stmt))