https://gcc.gnu.org/g:967bcb274bcb33c4484a6e1fbf4940ac745a469e

commit r17-6309-g967bcb274bcb33c4484a6e1fbf4940ac745a469e
Author: Richard Biener <[email protected]>
Date:   Fri Nov 13 15:15:54 2026 +0100

    Fix lexical compare of VN references
    
    We miss comparing base/clique so restrict vs. non-restrict refs
    appear the same.
    
            * 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.

Diff:
---
 gcc/testsuite/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(+)

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 000000000000..d40191c2a85e
--- /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 cd3f07b2cb77..ec6c4a8527dd 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 153951218df8..831ec66d57c0 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))

Reply via email to