https://gcc.gnu.org/g:210ff013a2e14b94d0b3e5e645ab6b69847e06ef

commit r17-6495-g210ff013a2e14b94d0b3e5e645ab6b69847e06ef
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Nov 19 04:44:22 2026 -0800

    tree-optimization/128641 - predcom initializers must be TBAA-equivalent
    
    This follows up the previous PR128641 change addressing the Sashiko concern.
    The same problem exists for looparound initializers.
    pcom_worker::valid_initializer_p accepts the preheader load
    that feeds a looparound PHI as the initial value of a chain without checking
    that it behaves like the chain root for TBAA.  The check in
    find_looparound_phi that nothing clobbers the initializer between its
    definition and the loop entry queries the alias set of the initializer, not
    that of the chain root.  The chain then replaces the in-loop load of the
    root alias set at iteration 0 with the stale value:
    
      prev = *(long long *) b;     // initializer
      f (b);                       // writes only long, TBAA-disjoint from above
      loop:
        cur = *(long *) (b + 8 + k * 8);
        s += *(long *) (b + k * 8);   // iteration 0 must see the value from f
        t += prev;
        prev = cur;
    
    Require the initializer and the chain root to behave the same for TBAA in
    both directions in valid_initializer_p.  Factor the check shared with
    determine_offset into refs_tbaa_equivalent_p.
    
    The check is symmetric, so it also covers a later reordering of the
    initializer load.
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            PR tree-optimization/128641
            * tree-predcom.cc (refs_tbaa_equivalent_p): New.
            (pcom_worker::determine_offset): Use it.
            (pcom_worker::valid_initializer_p): Also require the initializer and
            ROOT to be TBAA-equivalent.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/128641
            * gcc.dg/torture/pr128641-3.c: New test.
            * gcc.dg/torture/pr128641-4.c: Likewise.
            * gcc.dg/torture/pr128641-5.c: Likewise.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr128641-3.c | 42 +++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128641-4.c | 42 +++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128641-5.c | 44 +++++++++++++++++++++++++++++++
 gcc/tree-predcom.cc                       | 24 ++++++++++++-----
 4 files changed, 145 insertions(+), 7 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr128641-3.c 
b/gcc/testsuite/gcc.dg/torture/pr128641-3.c
new file mode 100644
index 000000000000..694d8a73d894
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128641-3.c
@@ -0,0 +1,42 @@
+/* The accesses to long and long long deliberately alias.  Check that
+   predictive commoning does not use an initializer with a different alias
+   set from the chain root.  */
+/* { dg-do run } */
+/* { dg-require-effective-target lp64 } */
+
+extern void *calloc (__SIZE_TYPE__, __SIZE_TYPE__);
+
+int n, sink;
+
+__attribute__((noinline)) void
+g (char *p)
+{
+  *(long long *) p = 3;
+}
+
+__attribute__((noinline)) void
+f (char *p)
+{
+  *(long *) p = 7;
+}
+
+int
+main (void)
+{
+  char *b = calloc (80, 1);
+  g (b);
+  long long prev = *(long long *) b;
+  f (b);
+  long long s = 0, t = 0;
+  for (int k = 0; k < (n & 7) + 1; k++)
+    {
+      long cur = *(long *) (b + 8 + k * 8);
+      s = s * 31 + *(long *) (b + k * 8);
+      t += prev;
+      prev = cur;
+      sink = k;
+    }
+  if (s != 7 || t != 3)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128641-4.c 
b/gcc/testsuite/gcc.dg/torture/pr128641-4.c
new file mode 100644
index 000000000000..dcdbd2f383c0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128641-4.c
@@ -0,0 +1,42 @@
+/* The accesses to long and long long deliberately alias.  Check that
+   predictive commoning does not use an initializer with a different alias
+   set from the chain root.  */
+/* { dg-do run } */
+/* { dg-require-effective-target lp64 } */
+
+extern void *calloc (__SIZE_TYPE__, __SIZE_TYPE__);
+
+int n, sink;
+
+__attribute__((noinline)) void
+g (char *p)
+{
+  *(long *) p = 3;
+}
+
+__attribute__((noinline)) void
+f (char *p)
+{
+  *(long long *) p = 7;
+}
+
+int
+main (void)
+{
+  char *b = calloc (80, 1);
+  g (b);
+  long prev = *(long *) b;
+  f (b);
+  long s = 0, t = 0;
+  for (int k = 0; k < (n & 7) + 1; k++)
+    {
+      long long cur = *(long long *) (b + 8 + k * 8);
+      s = s * 31 + *(long long *) (b + k * 8);
+      t += prev;
+      prev = cur;
+      sink = k;
+    }
+  if (s != 7 || t != 3)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128641-5.c 
b/gcc/testsuite/gcc.dg/torture/pr128641-5.c
new file mode 100644
index 000000000000..fb973d1c102d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128641-5.c
@@ -0,0 +1,44 @@
+/* The accesses to struct A and struct B deliberately alias.  Check that
+   predictive commoning does not use an initializer with a different alias
+   set from the chain root.  */
+/* { dg-do run } */
+
+extern void *calloc (__SIZE_TYPE__, __SIZE_TYPE__);
+
+struct A { long x; };
+struct B { long x; };
+
+int n, sink;
+
+__attribute__((noinline)) void
+g (struct A *p)
+{
+  p->x = 3;
+}
+
+__attribute__((noinline)) void
+f (struct B *p)
+{
+  p->x = 7;
+}
+
+int
+main (void)
+{
+  struct B *b = calloc (10, sizeof (struct B));
+  g ((struct A *) b);
+  long prev = ((struct A *) b)->x;
+  f (b);
+  long s = 0, t = 0;
+  for (int k = 0; k < (n & 7) + 1; k++)
+    {
+      long cur = b[k + 1].x;
+      s = s * 31 + b[k].x;
+      t += prev;
+      prev = cur;
+      sink = k;
+    }
+  if (s != 7 || t != 3)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc
index 503e74fb4af4..61e2b6ce0e3a 100644
--- a/gcc/tree-predcom.cc
+++ b/gcc/tree-predcom.cc
@@ -820,6 +820,16 @@ pcom_worker::aff_combination_dr_offset (struct 
data_reference *dr,
   aff_combination_add (offset, &delta);
 }
 
+/* Returns true if the references A and B behave the same for TBAA.  The
+   references created for a chain reuse the alias info of the chain root,
+   which can be either of A and B, so check both directions.  */
+
+static bool
+refs_tbaa_equivalent_p (tree a, tree b)
+{
+  return refs_same_for_tbaa_p (a, b) && refs_same_for_tbaa_p (b, a);
+}
+
 /* Determines number of iterations of the innermost enclosing loop before B
    refers to exactly the same location as A and stores it to OFF.  If A and
    B do not have the same step, they never meet, or anything else fails,
@@ -834,14 +844,11 @@ pcom_worker::determine_offset (struct data_reference *a,
   tree typea, typeb;
 
   /* Check that both the references access the location in the same type
-     and behave the same for TBAA.  The references created for a chain
-     reuse the alias info of the chain root, which is not known here and
-     can be either of A and B, so check both directions.  */
+     and behave the same for TBAA.  */
   typea = TREE_TYPE (DR_REF (a));
   typeb = TREE_TYPE (DR_REF (b));
   if (!useless_type_conversion_p (typeb, typea)
-      || !refs_same_for_tbaa_p (DR_REF (a), DR_REF (b))
-      || !refs_same_for_tbaa_p (DR_REF (b), DR_REF (a)))
+      || !refs_tbaa_equivalent_p (DR_REF (a), DR_REF (b)))
     return false;
 
   /* Check whether the base address and the step of both references is the
@@ -1373,8 +1380,11 @@ pcom_worker::valid_initializer_p (struct data_reference 
*ref, unsigned distance,
   aff_tree diff, base, step;
   poly_widest_int off;
 
-  /* Both REF and ROOT must be accessing the same object.  */
-  if (!operand_equal_p (DR_BASE_ADDRESS (ref), DR_BASE_ADDRESS (root), 0))
+  /* Both REF and ROOT must be accessing the same object and behave the
+     same for TBAA, since the initializer stands in for a reference with
+     the alias info of ROOT.  */
+  if (!operand_equal_p (DR_BASE_ADDRESS (ref), DR_BASE_ADDRESS (root), 0)
+      || !refs_tbaa_equivalent_p (DR_REF (ref), DR_REF (root)))
     return false;
 
   /* The initializer is defined outside of loop, hence its address must be

Reply via email to