]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/122653 - handle POINTER_DIFF_EXPR in SCEV
authorRichard Biener <rguenther@suse.de>
Wed, 12 Nov 2025 10:14:37 +0000 (11:14 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 12 Nov 2025 14:01:29 +0000 (15:01 +0100)
The following adds POINTER_DIFF_EXPR handling to SCEV stmt analysis,
mapping to conversions and MINUS_EXPR.

PR tree-optimization/122653
* tree-scalar-evolution.cc (interpret_rhs_expr): Handle
POINTER_DIFF_EXPR.

* gcc.dg/tree-ssa/scev-16.c: New testcase.

gcc/testsuite/gcc.dg/tree-ssa/scev-16.c [new file with mode: 0644]
gcc/tree-scalar-evolution.cc

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/scev-16.c b/gcc/testsuite/gcc.dg/tree-ssa/scev-16.c
new file mode 100644 (file)
index 0000000..0cc7f70
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-sccp" } */
+
+extern char a[];
+int foo ()
+{
+  int cnt = 0;
+  char *aend = a + 32;
+  char *a0 = a;
+  do
+    {
+      a0 = a0 + 16;
+      cnt++;
+    }
+  while (aend - a0 > 12);
+  return cnt;
+}
+
+/* { dg-final { scan-tree-dump "return 2" "sccp" } } */
index 180df75311f5c313b56c69b1723aaa6c6b7f689d..626c201df5e1b6e834cf947e77f0942fb8740ddd 100644 (file)
@@ -1753,6 +1753,20 @@ interpret_rhs_expr (class loop *loop, gimple *at_stmt,
       res = chrec_fold_plus (type, chrec1, chrec2);
       break;
 
+    case POINTER_DIFF_EXPR:
+      {
+       tree utype = unsigned_type_for (type);
+       chrec1 = analyze_scalar_evolution (loop, rhs1);
+       chrec2 = analyze_scalar_evolution (loop, rhs2);
+       chrec1 = chrec_convert (utype, chrec1, at_stmt);
+       chrec2 = chrec_convert (utype, chrec2, at_stmt);
+       chrec1 = instantiate_parameters (loop, chrec1);
+       chrec2 = instantiate_parameters (loop, chrec2);
+       res = chrec_fold_minus (utype, chrec1, chrec2);
+       res = chrec_convert (type, res, at_stmt);
+       break;
+      }
+
     case PLUS_EXPR:
       chrec1 = analyze_scalar_evolution (loop, rhs1);
       chrec2 = analyze_scalar_evolution (loop, rhs2);