[gcc r17-3363] tree-optimization/126925 - missed folding causes IVOPTs crash

Richard Biener via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:8ff8114e4448307f27e38fbdb98028283ab64f1c

commit r17-3363-g8ff8114e4448307f27e38fbdb98028283ab64f1c
Author: Richard Biener <[email protected]>
Date:   Tue Aug 18 09:49:12 2026 +0200

    tree-optimization/126925 - missed folding causes IVOPTs crash
    
    When we fail to simplify an IV candidate step to zero SCEV might
    still have simplified the before/after candidate to an IV with
    zero step.  Avoid crashing in such situation.
    
            PR tree-optimization/126925
            * tree-ssa-loop-ivopts.cc (create_new_iv): Avoid crashing
            if the use IV is invariant.
    
            * gcc.dg/torture/pr126925.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr126925.c | 21 +++++++++++++++++++++
 gcc/tree-ssa-loop-ivopts.cc             | 11 ++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr126925.c b/gcc/testsuite/gcc.dg/torture/pr126925.c
new file mode 100644
index 000000000000..06e601d64985
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr126925.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+
+int a;
+long b;
+int c(long d) {
+  for (;;)
+    if (d)
+      return b;
+}
+void e(int d) {
+  unsigned long f = 6;
+  int g;
+  while (c(f)) {
+    g = 0;
+    for (; g < 2; g++) {
+      a = 0;
+      short h = d;
+      f = d + f - (h + f + (a + 4 + f));
+    }
+  }
+}
diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
index 3c0ce1794c2f..963e89e4b3c2 100644
--- a/gcc/tree-ssa-loop-ivopts.cc
+++ b/gcc/tree-ssa-loop-ivopts.cc
@@ -7257,10 +7257,15 @@ create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
       name_info (data, cand->var_before)->preserve_biv = true;
       name_info (data, cand->var_after)->preserve_biv = true;
 
-      /* Rewrite the increment so that it uses var_before directly.  */
+      /* Rewrite the increment so that it uses var_before directly.  Missed
+	 optimization can result in a use IV with zero step, avoid
+	 crashing in that case.  */
       use = find_interesting_uses_op (data, cand->var_after);
-      group = data->vgroups[use->group_id];
-      group->selected = cand;
+      if (use)
+	{
+	  group = data->vgroups[use->group_id];
+	  group->selected = cand;
+	}
       return;
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.