diff mbox series

[1/2] Check negative combined step

Message ID 20220113014821.168869-1-guojiufu@linux.ibm.com
State New
Headers show
Series [1/2] Check negative combined step | expand

Commit Message

Jiufu Guo Jan. 13, 2022, 1:48 a.m. UTC
Hi,

Previously, there is discussion in:
https://gcc.gnu.org/pipermail/gcc-patches/2021-December/586460.html
I seperate it as two patches.

This first patch is to avoid negative step when combining two ivs.
The second patch is adding more accurate assumptions.

This patch pass bootstrap and regtest on ppc64, ppc64le and x86_64.
Is this ok for trunk?

BR,
Jiufu

	PR tree-optimization/100740

gcc/ChangeLog:

	* tree-ssa-loop-niter.c (number_of_iterations_cond): Check
	sign of combined step.

gcc/testsuite/ChangeLog:

	* gcc.c-torture/execute/pr100740.c: New test.



---
 gcc/tree-ssa-loop-niter.c                      |  6 ++++--
 gcc/testsuite/gcc.c-torture/execute/pr100740.c | 13 +++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr100740.c
diff mbox series

Patch

diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c
index b767056aeb0..439d595a79f 100644
--- a/gcc/tree-ssa-loop-niter.c
+++ b/gcc/tree-ssa-loop-niter.c
@@ -1890,8 +1890,10 @@  number_of_iterations_cond (class loop *loop,
       tree step = fold_binary_to_constant (MINUS_EXPR, step_type,
 					   iv0->step, iv1->step);
 
-      /* No need to check sign of the new step since below code takes care
-	 of this well.  */
+      /* Like cases shown in PR100740/102131, negtive step is not safe.  */
+      if (tree_int_cst_sign_bit (step))
+	return false;
+
       if (code != NE_EXPR
 	  && (TREE_CODE (step) != INTEGER_CST
 	      || !iv0->no_overflow || !iv1->no_overflow))
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr100740.c b/gcc/testsuite/gcc.c-torture/execute/pr100740.c
new file mode 100644
index 00000000000..381cdeb947a
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr100740.c
@@ -0,0 +1,13 @@ 
+/* PR tree-optimization/100740 */
+
+unsigned a, b;
+int
+main ()
+{
+  unsigned c = 0;
+  for (a = 0; a < 2; a++)
+    for (b = 0; b < 2; b++)
+      if (++c < a)
+	__builtin_abort ();
+  return 0;
+}