https://gcc.gnu.org/g:09e8d26982bf4280ec41b2e5215a57900517db7c

commit r17-5801-g09e8d26982bf4280ec41b2e5215a57900517db7c
Author: Steve Bezalel <[email protected]>
Date:   Fri Oct 30 02:47:27 2026 +0000

    tree-optimization/127211 - Reset SCEV after predcom without unrolling
    
    tree_predictive_commoning only resets SCEV when a loop was unrolled or
    needed loop-closed SSA fixup (changed > 1).  When a loop is transformed
    without either, the SCEV results cached by predcom's data-reference
    analysis survive the transform.  At -O2, where predcom only runs
    implicitly with -ftree-loop-vectorize and never unrolls, that is the
    common case.
    
    In the testcase, the evolution of s is cached as not analyzable while
    its step is still a load of a[4].  Predcom then hoists that load, after
    which s is the affine IV {a_I_lsm0.4_26, +, a_I_lsm0.4_26}_1, but ivopts
    still reads the cached result, finds no niter and does not
    strength-reduce the b[s] store.
    
    At plain -O2 this is hidden because passes between predcom and ivopts
    clear the SCEV cache as a side effect: fre4 in pass_pre_slp_scalar_cleanup,
    and slp1 through the SSA name freelist flush.  -fno-tree-slp-vectorize
    disables both.
    
    The fix resets SCEV whenever predcom changed something.
    
    Bootstrapped and regtested on x86_64-pc-linux-gnu.  OK for trunk?
    
            PR tree-optimization/127211
    
    gcc/ChangeLog:
    
            * tree-predcom.cc (tree_predictive_commoning): Reset SCEV
            whenever a loop was transformed, not only when unrolled.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr127211.c: New test.
    
    Signed-off-by: Steve Bezalel <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/pr127211.c | 24 ++++++++++++++++++++++++
 gcc/tree-predcom.cc                      |  7 +++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr127211.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr127211.c
new file mode 100644
index 000000000000..bf9ae22bf219
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127211.c
@@ -0,0 +1,24 @@
+/* PR tree-optimization/127211 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-slp-vectorize -fdump-tree-ivopts-details" } */
+
+int a[4096], b[4096];
+
+void
+fn (int m)
+{
+  int s = 0;
+  for (int i = 0; i < a[4]; i++)
+    {
+      a[3*i + 2] = m;
+      if (m & 1)
+       a[3*i + 5] = m - 1;
+      b[s] = m;
+      s += a[4];
+    }
+}
+
+/* Predictive commoning hoists the load of a[4] out of the loop, after
+   which s is an affine IV.  SCEV results cached by its data-reference
+   analysis before the transform must not reach ivopts.  */
+/* { dg-final { scan-tree-dump-not "niter scev_not_known" "ivopts" } } */
diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc
index 8e2894d46a09..a2043ef3122a 100644
--- a/gcc/tree-predcom.cc
+++ b/gcc/tree-predcom.cc
@@ -3512,11 +3512,14 @@ tree_predictive_commoning (bool allow_unroll_p)
     {
       ret = TODO_update_ssa_only_virtuals;
 
+      /* Cached SCEV information is stale after any transformation, not
+        only after unrolling: the data-reference analysis above queried
+        SCEV before references were rewritten.  */
+      scev_reset ();
+
       /* Some loop(s) got unrolled.  */
       if (changed > 1)
        {
-         scev_reset ();
-
          /* Need to fix up loop closed SSA.  */
          if (changed >= 4)
            rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);

Reply via email to