We miss comparing base/clique so restrict vs. non-restrict refs
appear the same.
Bootstrapped and tested on x86_64-unknown-linux-gnu.
v2: sashiko review + testcase (LLM generated)
* tree-ssa-sccvn.cc (vn_reference_eq): When lexically comparing
also compare base and clique.
* tree-ssa-pre.cc (prefer): Compare restrict info and pick
unrestricted variant.
* gcc.dg/torture/ssa-pre-restrict.c: New testcase.
---
.../gcc.dg/torture/ssa-pre-restrict.c | 38 +++++++++++++++++++
gcc/tree-ssa-pre.cc | 17 +++++++++
gcc/tree-ssa-sccvn.cc | 2 +
3 files changed, 57 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/torture/ssa-pre-restrict.c
diff --git a/gcc/testsuite/gcc.dg/torture/ssa-pre-restrict.c
b/gcc/testsuite/gcc.dg/torture/ssa-pre-restrict.c
new file mode 100644
index 00000000000..d40191c2a85
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/ssa-pre-restrict.c
@@ -0,0 +1,38 @@
+/* { dg-do run } */
+
+extern void abort (void);
+
+static inline int __attribute__((always_inline))
+helper (int *__restrict__ a, int *b, int cond, int idx, int new_val)
+{
+ int v, v2;
+ if (cond)
+ {
+ v = a[0];
+ v2 = a[0];
+ }
+ else
+ {
+ v = b[0];
+ b[idx] = new_val;
+ v2 = b[0];
+ }
+ return v + v2;
+}
+
+int __attribute__((noipa))
+foo (int *p, int cond, int idx, int new_val)
+{
+ int *q = p;
+ return helper (p, q, cond, idx, new_val);
+}
+
+int
+main (void)
+{
+ int val[2] = { 42, 0 };
+ int res = foo (val, 0, 0, 100);
+ if (res != 142)
+ abort ();
+ return 0;
+}
diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc
index cd3f07b2cb7..ec6c4a8527d 100644
--- a/gcc/tree-ssa-pre.cc
+++ b/gcc/tree-ssa-pre.cc
@@ -910,6 +910,23 @@ prefer (pre_expr a, pre_expr b)
p = psize;
}
}
+ /* When restrict info differs we have to pick the non-restricted
+ variant if available. */
+ if (vroa->clique != vrob->clique
+ || vroa->base != vrob->base)
+ {
+ pre_expr prestrict = NULL;
+ if (vroa->clique == 0)
+ prestrict = a;
+ else if (vrob->clique == 0)
+ prestrict = b;
+ else
+ /* There's no conservative variant to pick. */
+ return NULL;
+ if (p && p != prestrict)
+ return NULL;
+ p = prestrict;
+ }
}
}
/* Note we cannot leave it undecided because when having
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 153951218df..831ec66d57c 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -936,6 +936,8 @@ vn_reference_eq (const_vn_reference_t const vr1,
const_vn_reference_t const vr2,
|| ! TYPE_SIZE (vro2->type)
|| ! operand_equal_p (TYPE_SIZE (vro1->type),
TYPE_SIZE (vro2->type))))
+ || vro1->clique != vro2->clique
+ || vro1->base != vro2->base
|| (get_deref_alias_set (vro1->opcode == MEM_REF
? TREE_TYPE (vro1->op0)
: TREE_TYPE (vro1->op2))
--
2.51.0