public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-9259] tree-optimization/108793 - niter compute type mismatch
@ 2023-03-15  9:48 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2023-03-15  9:48 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0a3642dde799ee2be9f2b60ec361191176390005

commit r12-9259-g0a3642dde799ee2be9f2b60ec361191176390005
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Feb 20 12:58:50 2023 +0100

    tree-optimization/108793 - niter compute type mismatch
    
    When computing the number of iterations until wrap types are mixed up,
    eventually leading to checking ICEs with a pointer bitwise inversion.
    The following uses niter_type for the calculation.
    
            PR tree-optimization/108793
            * tree-ssa-loop-niter.cc (number_of_iterations_until_wrap):
            Use convert operands to niter_type when computing num.
    
            * gcc.dg/torture/pr108793.c: New testcase.
    
    (cherry picked from commit a7e706df2280de4a42f68b6c44401e4348d3593c)

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr108793.c | 10 ++++++++++
 gcc/tree-ssa-loop-niter.cc              | 11 ++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr108793.c b/gcc/testsuite/gcc.dg/torture/pr108793.c
new file mode 100644
index 00000000000..83973eb05d9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr108793.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+
+typedef int *p;
+extern p a[], b[];
+int f () {
+  int n = 0;
+  for (p* i = &a[0]; i > &b[0]; i++)
+    n++;
+  return n;
+}
diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index afa51064953..0353ffd3022 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -1498,8 +1498,9 @@ number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0,
       if (integer_zerop (assumptions))
 	return false;
 
-      num = fold_build2 (MINUS_EXPR, niter_type, wide_int_to_tree (type, max),
-			 iv1->base);
+      num = fold_build2 (MINUS_EXPR, niter_type,
+			 wide_int_to_tree (niter_type, max),
+			 fold_convert (niter_type, iv1->base));
 
       /* When base has the form iv + 1, if we know iv >= n, then iv + 1 < n
 	 only when iv + 1 overflows, i.e. when iv == TYPE_VALUE_MAX.  */
@@ -1535,8 +1536,9 @@ number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0,
       if (integer_zerop (assumptions))
 	return false;
 
-      num = fold_build2 (MINUS_EXPR, niter_type, iv0->base,
-			 wide_int_to_tree (type, min));
+      num = fold_build2 (MINUS_EXPR, niter_type,
+			 fold_convert (niter_type, iv0->base),
+			 wide_int_to_tree (niter_type, min));
       low = min;
       if (TREE_CODE (iv0->base) == INTEGER_CST)
 	high = wi::to_wide (iv0->base) + 1;
@@ -1550,7 +1552,6 @@ number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0,
 
   /* (delta + step - 1) / step */
   step = fold_convert (niter_type, step);
-  num = fold_convert (niter_type, num);
   num = fold_build2 (PLUS_EXPR, niter_type, num, step);
   niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, num, step);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-15  9:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-15  9:48 [gcc r12-9259] tree-optimization/108793 - niter compute type mismatch Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).