]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/124692 - update stmt before folding
authorRichard Biener <rguenther@suse.de>
Mon, 30 Mar 2026 12:30:45 +0000 (14:30 +0200)
committerRichard Biener <rguenth@gcc.gnu.org>
Mon, 30 Mar 2026 13:45:36 +0000 (15:45 +0200)
The following makes sure SSA operands are up-to-date before folding.
Esp. when replace_uses_by is invoked from SCEV cprop which now has
ranger enabled we can otherwise end up ICEing where purpoted SSA
names now are constants.  This follows what forwprop does.

PR tree-optimization/124692
* tree-cfg.cc (replace_uses_by): Call update_stmt after
substitution and before folding.

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

gcc/testsuite/gcc.dg/torture/pr124692.c [new file with mode: 0644]
gcc/tree-cfg.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr124692.c b/gcc/testsuite/gcc.dg/torture/pr124692.c
new file mode 100644 (file)
index 0000000..340f7ea
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+
+int a, b, d, e[3][1], f, g, h;
+unsigned c;
+int main()
+{
+  while (b)
+    {
+      h = g;
+      for (b = 0; b < 2; b++)
+       ;
+      for (; c; c++)
+       {
+         int j = e[b][h];
+         f = a < 0 ? j : j - a;
+         if (!f)
+           break;
+         g = c;
+       }
+    }
+  return 0;
+}
index 66ea54f8b858a05f419db7cd825b5f25f6925fc9..3b95e0a68f139f0b35d576d6717aaf9e6c9f708a 100644 (file)
@@ -1988,14 +1988,16 @@ replace_uses_by (tree name, tree val)
                if (op && TREE_CODE (op) == ADDR_EXPR)
                  recompute_tree_invariant_for_addr_expr (op);
              }
+         update_stmt (stmt);
 
          if (fold_stmt (&gsi))
-           stmt = gsi_stmt (gsi);
+           {
+             stmt = gsi_stmt (gsi);
+             update_stmt (stmt);
+           }
 
          if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
            gimple_purge_dead_eh_edges (gimple_bb (stmt));
-
-         update_stmt (stmt);
        }
     }