The following removes the optimization eliding the maybe_zero condition
from number_of_iterations_lt_to_ne when the IV can overflow since the
IV delta input is not accurately reflecting this.

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

        PR tree-optimization/122537
        * tree-ssa-loop-niter.cc (number_of_iterations_lt_to_ne): Register
        may_be_zero condition when the IV may overflow.

        * gcc.dg/torture/pr122537.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr122537.c | 18 ++++++++++++++++++
 gcc/tree-ssa-loop-niter.cc              |  2 +-
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr122537.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr122537.c 
b/gcc/testsuite/gcc.dg/torture/pr122537.c
new file mode 100644
index 00000000000..8fc64abcffd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr122537.c
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+/* { dg-additional-options "-fwrapv" } */
+
+void abort ();
+int a, b;
+int main()
+{
+  int c = 61;
+  int d = 61;
+  c += 8;
+  while (c + a - 80 >= d + a - 80) {
+    c -= d;
+    b++;
+  }
+  if (b != 1)
+    abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index 23e97c4f394..207bf8ccf60 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -1218,7 +1218,7 @@ number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, 
affine_iv *iv1,
     }
 
   /* IV0 < IV1 does not loop if IV0->base >= IV1->base.  */
-  if (mpz_cmp (mmod, bnds->below) < 0)
+  if (fv_comp_no_overflow && mpz_cmp (mmod, bnds->below) < 0)
     noloop = boolean_false_node;
   else
     noloop = fold_build2 (GE_EXPR, boolean_type_node,
-- 
2.51.0

Reply via email to