]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Avoid using POINTER_DIFF_EXPR for overlap checks [PR119399]
authorRichard Sandiford <richard.sandiford@arm.com>
Thu, 10 Apr 2025 10:03:04 +0000 (11:03 +0100)
committerRichard Sandiford <richard.sandiford@arm.com>
Thu, 10 Apr 2025 10:03:04 +0000 (11:03 +0100)
In r10-4803-g8489e1f45b50600c I'd used POINTER_DIFF_EXPR to subtract
the two pointers involved in an overlap test.  I'm not sure whether
I'd specifically chosen that over MINUS_EXPR or not; if so, the only
reason I can think of is that it is probably faster on targets with
PSImode pointers.  Regardless, as the PR points out, subtracting
unrelated pointers using POINTER_DIFF_EXPR is undefined behaviour.

gcc/
PR tree-optimization/119399
* tree-data-ref.cc (create_waw_or_war_checks): Use a MINUS_EXPR
on two converted pointers, rather than converting a POINTER_DIFF_EXPR
on the pointers.

gcc/testsuite/
PR tree-optimization/119399
* gcc.dg/vect/pr119399.c: New test.

gcc/testsuite/gcc.dg/vect/pr119399.c [new file with mode: 0644]
gcc/tree-data-ref.cc

diff --git a/gcc/testsuite/gcc.dg/vect/pr119399.c b/gcc/testsuite/gcc.dg/vect/pr119399.c
new file mode 100644 (file)
index 0000000..8d868f4
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fdump-tree-vect-raw" } */
+
+void foo(int *p, int *q, int n)
+{
+  for (int i = 0; i < n; i++)
+    p[i] = q[i] + 1;
+}
+
+/* { dg-final { scan-tree-dump-not {<pointer_diff_expr,} "vect" } } */
index 18d9f0fa7660fd2d3f71811f297229442d86371d..8a46e6daabb2df80d30d1ada453a81736c6da676 100644 (file)
@@ -2498,9 +2498,10 @@ create_waw_or_war_checks (tree *cond_expr,
   limit = fold_build2 (PLUS_EXPR, sizetype, limit,
                       size_int (last_chunk_a + last_chunk_b));
 
-  tree subject = fold_build2 (POINTER_DIFF_EXPR, ssizetype, addr_b, addr_a);
-  subject = fold_build2 (PLUS_EXPR, sizetype,
-                        fold_convert (sizetype, subject), bias);
+  tree subject = fold_build2 (MINUS_EXPR, sizetype,
+                             fold_convert (sizetype, addr_b),
+                             fold_convert (sizetype, addr_a));
+  subject = fold_build2 (PLUS_EXPR, sizetype, subject, bias);
 
   *cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject, limit);
   if (dump_enabled_p ())