]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/107407 - wrong code with DSE
authorRichard Biener <rguenther@suse.de>
Fri, 28 Oct 2022 13:03:49 +0000 (15:03 +0200)
committerRichard Biener <rguenther@suse.de>
Mon, 12 Dec 2022 10:25:54 +0000 (11:25 +0100)
So what happens is that we elide processing of this check with

          /* In addition to kills we can remove defs whose only use
             is another def in defs.  That can only ever be PHIs of which
             we track two for simplicity reasons, the first and last in
             {first,last}_phi_def (we fail for multiple PHIs anyways).
             We can also ignore defs that feed only into
             already visited PHIs.  */
          else if (single_imm_use (vdef, &use_p, &use_stmt)
                   && (use_stmt == first_phi_def
                       || use_stmt == last_phi_def
                       || (gimple_code (use_stmt) == GIMPLE_PHI
                           && bitmap_bit_p (visited,
                                            SSA_NAME_VERSION
                                              (PHI_RESULT (use_stmt))))))

where we have the last PHI being the outer loop virtual PHI and the first
PHI being the loop exit PHI of the outer loop and we've already processed
the single immediate use of the outer loop PHI, the inner loop PHI.  But
we still have to perform the above check!

It's easiest to perform the check when we visit the PHI node instead of
delaying it to the actual processing loop.

PR tree-optimization/107407
* tree-ssa-dse.cc (dse_classify_store): Perform backedge
varying index check when collecting PHI uses rather than
after optimizing processing of the candidate defs.

* gcc.dg/torture/pr107407.c: New testcase.

(cherry picked from commit 031a400e49d8db156c43f9ec0b21ab0c2aee8c6d)

gcc/testsuite/gcc.dg/torture/pr107407.c [new file with mode: 0644]
gcc/tree-ssa-dse.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr107407.c b/gcc/testsuite/gcc.dg/torture/pr107407.c
new file mode 100644 (file)
index 0000000..228fce1
--- /dev/null
@@ -0,0 +1,26 @@
+/* { dg-do run } */
+
+int *a;
+int c[4];
+int d;
+
+static int
+f(char k, int j)
+{
+  for (; k <= 3; k++)
+    {
+      a = &c[k];
+      for (; d <= 1; d++)
+        *a = 3;
+    }
+  *a = 0;
+}
+
+int main()
+{
+  int i;
+  f(0, 0);
+  if (c[0] != 3)
+    __builtin_abort ();
+  return 0;
+}
index 881a2d0f98dba81ab4c13af357ca7ec6a3916e40..e647f0e7368c3a2191f0fe295dccc3d7c6022a47 100644 (file)
@@ -930,14 +930,6 @@ dse_classify_store (ao_ref *ref, gimple *stmt,
 
       if (gimple_code (temp) == GIMPLE_PHI)
        {
-         /* If we visit this PHI by following a backedge then we have to
-            make sure ref->ref only refers to SSA names that are invariant
-            with respect to the loop represented by this PHI node.  */
-         if (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt),
-                             gimple_bb (temp))
-             && !for_each_index (ref->ref ? &ref->ref : &ref->base,
-                                 check_name, gimple_bb (temp)))
-           return DSE_STORE_LIVE;
          defvar = PHI_RESULT (temp);
          bitmap_set_bit (visited, SSA_NAME_VERSION (defvar));
        }
@@ -971,6 +963,15 @@ dse_classify_store (ao_ref *ref, gimple *stmt,
              if (!bitmap_bit_p (visited,
                                 SSA_NAME_VERSION (PHI_RESULT (use_stmt))))
                {
+                 /* If we visit this PHI by following a backedge then we have
+                    to make sure ref->ref only refers to SSA names that are
+                    invariant with respect to the loop represented by this
+                    PHI node.  */
+                 if (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt),
+                                     gimple_bb (use_stmt))
+                     && !for_each_index (ref->ref ? &ref->ref : &ref->base,
+                                         check_name, gimple_bb (use_stmt)))
+                   return DSE_STORE_LIVE;
                  defs.safe_push (use_stmt);
                  if (!first_phi_def)
                    first_phi_def = use_stmt;