https://gcc.gnu.org/g:ae937b5ba700b3d9883505d73c59983863696d3c

commit r17-6460-gae937b5ba700b3d9883505d73c59983863696d3c
Author: Kyrylo Tkachov <[email protected]>
Date:   Wed Nov 18 06:24:37 2026 -0800

    tree-optimization/128641 - predcom chains must be TBAA-equivalent
    
    pcom_worker::determine_offset places two references in the same chain
    when their types satisfy useless_type_conversion_p.  On LP64 long and
    long long satisfy it but have different alias sets.  References created
    for a chain (ref_at_iteration) copy the alias info of the chain root.
    In the PR a long store and a long long load form one chain, so the
    preheader load of the long long location is created as a long access.
    With the IPA modref summary of f, which only writes long long, TBAA then
    treats the long load as independent of the call f (b) and the value is
    read before the call, giving 0 instead of 7.
    
    Require refs_same_for_tbaa_p in determine_offset, in addition to the
    type check.  The chain root is not known there and can change when the
    chain is sorted by offset, so check both directions.  Every reference
    in a chain then has the same TBAA behaviour as its root.
    
    Bootstrapped and tested on aarch64-linux-gnu.
    
    gcc/ChangeLog:
    
            PR tree-optimization/128641
            * tree-predcom.cc (pcom_worker::determine_offset): Also require
            refs_same_for_tbaa_p in both directions for the two references.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/128641
            * gcc.dg/torture/pr128641.c: New test.
            * gcc.dg/torture/pr128641-2.c: Likewise.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr128641-2.c | 27 +++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128641.c   | 27 +++++++++++++++++++++++++++
 gcc/tree-predcom.cc                       |  9 +++++++--
 3 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr128641-2.c 
b/gcc/testsuite/gcc.dg/torture/pr128641-2.c
new file mode 100644
index 000000000000..792c01d8bfd8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128641-2.c
@@ -0,0 +1,27 @@
+/* { dg-do run } */
+
+extern void *malloc (__SIZE_TYPE__);
+
+int n;
+
+__attribute__((noinline)) void
+f (char *p)
+{
+  *(long *) p = 7;
+}
+
+int
+main (void)
+{
+  char *b = malloc (72);
+  f (b);
+  long s = 0;
+  for (int k = 0; k < (n & 7) + 1; k++)
+    {
+      s = s * 31 + *(long *) (b + k * 8);
+      *(long long *) (b + 8 + k * 8) = k;
+    }
+  if (s != 7)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128641.c 
b/gcc/testsuite/gcc.dg/torture/pr128641.c
new file mode 100644
index 000000000000..a9370fd7ef53
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128641.c
@@ -0,0 +1,27 @@
+/* { dg-do run } */
+
+extern void *malloc (__SIZE_TYPE__);
+
+int n;
+
+__attribute__((noinline)) void
+f (char *p)
+{
+  *(long long *) p = 7;
+}
+
+int
+main (void)
+{
+  char *b = malloc (72);
+  f (b);
+  long long s = 0;
+  for (int k = 0; k < (n & 7) + 1; k++)
+    {
+      s = s * 31 + *(long long *) (b + k * 8);
+      *(long *) (b + 8 + k * 8) = k;
+    }
+  if (s != 7)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc
index a2043ef3122a..503e74fb4af4 100644
--- a/gcc/tree-predcom.cc
+++ b/gcc/tree-predcom.cc
@@ -833,10 +833,15 @@ pcom_worker::determine_offset (struct data_reference *a,
   aff_tree diff, baseb, step;
   tree typea, typeb;
 
-  /* Check that both the references access the location in the same type.  */
+  /* 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.  */
   typea = TREE_TYPE (DR_REF (a));
   typeb = TREE_TYPE (DR_REF (b));
-  if (!useless_type_conversion_p (typeb, typea))
+  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)))
     return false;
 
   /* Check whether the base address and the step of both references is the

Reply via email to