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.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

        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.
---
 gcc/testsuite/gcc.dg/torture/pr126925.c | 21 +++++++++++++++++++++
 gcc/tree-ssa-loop-ivopts.cc             | 11 ++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr126925.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr126925.c 
b/gcc/testsuite/gcc.dg/torture/pr126925.c
new file mode 100644
index 00000000000..06e601d6498
--- /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 3c0ce1794c2..963e89e4b3c 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;
     }
 
-- 
2.51.0

Reply via email to