]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/122537 - do not elide maybe_zero condition for wrapping IV
authorRichard Biener <rguenther@suse.de>
Thu, 29 Jan 2026 12:56:11 +0000 (13:56 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Thu, 29 Jan 2026 14:17:06 +0000 (15:17 +0100)
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.

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 [new file with mode: 0644]
gcc/tree-ssa-loop-niter.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr122537.c b/gcc/testsuite/gcc.dg/torture/pr122537.c
new file mode 100644 (file)
index 0000000..8fc64ab
--- /dev/null
@@ -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;
+}
index 23e97c4f3943f2f6669c1fce6c2a2bf45c7d8874..207bf8ccf60faae2b54d91f4d0431d01f9c9ff5e 100644 (file)
@@ -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,