https://gcc.gnu.org/g:1c8a4ea630bfbf15a0b2f41a970c09fb38e68fdc

commit r17-6636-g1c8a4ea630bfbf15a0b2f41a970c09fb38e68fdc
Author: Richard Biener <[email protected]>
Date:   Tue Nov 24 16:03:09 2026 +0100

    tree-optimization/128714 - avoid wrong restrict info on vectorized refs
    
    The following makes sure to compare restrict info on a DR group
    before copying it to a vector ref.  We should probably compute
    such properties upfront, but this is consistent with how we handle
    TBAA info and also suitable for backporting.
    
            PR tree-optimization/128714
            * tree-vectorizer.h (vect_copy_ref_info): Take stmt_vec_info
            as source.
            * tree-vect-data-refs.cc (vect_copy_ref_info): Likewise.
            Before copying restrict info verify it is consistent across
            a group.
            (vect_setup_realignment): Adjust.
            * tree-vect-stmts.cc (vectorizable_scan_store): Likewise.
            (vectorizable_load): Likewise.
            (vectorizable_store): Likewise.
    
            * gcc.dg/vect/bb-slp-pr128714.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/vect/bb-slp-pr128714.c | 28 ++++++++++++++++++++++++++++
 gcc/tree-vect-data-refs.cc                  | 27 +++++++++++++++++++++++----
 gcc/tree-vect-stmts.cc                      | 22 +++++++++++-----------
 gcc/tree-vectorizer.h                       |  2 +-
 4 files changed, 63 insertions(+), 16 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr128714.c 
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr128714.c
new file mode 100644
index 000000000000..713b50828a69
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr128714.c
@@ -0,0 +1,28 @@
+/* { dg-additional-options "-fvect-cost-model=dynamic" } */
+
+#include "tree-vect.h"
+
+int m[2];
+int r[2];
+
+static inline void g (int *restrict a, int *p, int *q, int *o)
+{
+  *q = 5;                 /* q == p + 1: writes p[1] */
+  int x0 = a[0];          /* restrict load; m[0] is never modified */
+  int x1 = p[1];          /* must read 5 */
+  o[0] = x0 * 3;
+  o[1] = x1 * 3;
+  *q = 7;
+}
+
+__attribute__((noipa)) void h (int *p, int *q, int *o) { g (p, p, q, o); }
+
+int main ()
+{
+  check_vect ();
+  m[0] = 1; m[1] = 2;
+  h (m, m + 1, r);
+  if (r[1] != 15)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
index cf4c9a3ac230..37cdaae1dd39 100644
--- a/gcc/tree-vect-data-refs.cc
+++ b/gcc/tree-vect-data-refs.cc
@@ -6161,22 +6161,41 @@ bump_vector_ptr (vec_info *vinfo,
 }
 
 
-/* Copy memory reference info such as base/clique from the SRC reference
+/* Copy memory reference info such as base/clique from the SRC stmts reference
    to the DEST MEM_REF.  */
 
 void
-vect_copy_ref_info (tree dest, tree src)
+vect_copy_ref_info (tree dest, stmt_vec_info src)
 {
   if (TREE_CODE (dest) != MEM_REF)
     return;
 
-  tree src_base = src;
+  tree src_base = DR_REF (STMT_VINFO_DR_INFO (src)->dr);
   while (handled_component_p (src_base))
     src_base = TREE_OPERAND (src_base, 0);
   if (TREE_CODE (src_base) != MEM_REF
       && TREE_CODE (src_base) != TARGET_MEM_REF)
     return;
 
+  if (MR_DEPENDENCE_CLIQUE (src_base) == 0)
+    return;
+
+  /* Verify the restrict information is consistent across the group.  */
+  if (STMT_VINFO_GROUPED_ACCESS (src))
+    for (auto si = DR_GROUP_FIRST_ELEMENT (src); si;
+        si = DR_GROUP_NEXT_ELEMENT (si))
+      {
+       tree base2 = DR_REF (STMT_VINFO_DR_INFO (si)->dr);
+       while (handled_component_p (base2))
+         base2 = TREE_OPERAND (base2, 0);
+       if (TREE_CODE (base2) != MEM_REF
+           && TREE_CODE (base2) != TARGET_MEM_REF)
+         return;
+       if (MR_DEPENDENCE_CLIQUE (base2) != MR_DEPENDENCE_CLIQUE (src_base)
+           || MR_DEPENDENCE_BASE (base2) != MR_DEPENDENCE_BASE (src_base))
+         return;
+      }
+
   MR_DEPENDENCE_CLIQUE (dest) = MR_DEPENDENCE_CLIQUE (src_base);
   MR_DEPENDENCE_BASE (dest) = MR_DEPENDENCE_BASE (src_base);
 }
@@ -6554,7 +6573,7 @@ vect_setup_realignment (vec_info *vinfo, stmt_vec_info 
stmt_info, tree vectype,
       data_ref
        = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
                  build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
-      vect_copy_ref_info (data_ref, DR_REF (dr));
+      vect_copy_ref_info (data_ref, stmt_info);
       new_stmt = gimple_build_assign (vec_dest, data_ref);
       new_temp = make_ssa_name (vec_dest, new_stmt);
       gimple_assign_set_lhs (new_stmt, new_temp);
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index a884c05fa196..50aeca2780ac 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -8001,6 +8001,7 @@ vectorizable_scan_store (vec_info *vinfo, stmt_vec_info 
stmt_info,
       std::swap (rhs1, rhs2);
       std::swap (var1, var2);
       std::swap (load1_dr_info, load2_dr_info);
+      std::swap (load1_stmt_info, load2_stmt_info);
     }
 
   tree *init = loop_vinfo->scan_map->get (var1);
@@ -8097,7 +8098,7 @@ vectorizable_scan_store (vec_info *vinfo, stmt_vec_info 
stmt_info,
          tree data_ref = fold_build2 (MEM_REF, vectype,
                                       unshare_expr (ldataref_ptr),
                                       dataref_offset);
-         vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
+         vect_copy_ref_info (data_ref, load1_stmt_info);
          gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
          vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
        }
@@ -8175,7 +8176,7 @@ vectorizable_scan_store (vec_info *vinfo, stmt_vec_info 
stmt_info,
          tree data_ref = fold_build2 (MEM_REF, vectype,
                                       unshare_expr (dataref_ptr),
                                       dataref_offset);
-         vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
+         vect_copy_ref_info (data_ref, stmt_info);
          g = gimple_build_assign (data_ref, new_temp);
          vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
        }
@@ -8190,7 +8191,7 @@ vectorizable_scan_store (vec_info *vinfo, stmt_vec_info 
stmt_info,
        tree data_ref = fold_build2 (MEM_REF, vectype,
                                     unshare_expr (dataref_ptr),
                                     dataref_offset);
-       vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
+       vect_copy_ref_info (data_ref, stmt_info);
        gimple *g = gimple_build_assign (data_ref, orig);
        vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
       }
@@ -8719,7 +8720,7 @@ vectorizable_store (vec_info *vinfo,
              tree this_off = build_int_cst (TREE_TYPE (alias_off),
                                             group_el * elsz);
              newref = build2 (MEM_REF, ltype, running_off, this_off);
-             vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
+             vect_copy_ref_info (newref, first_stmt_info);
 
              /* And store it to *running_off.  */
              assign = gimple_build_assign (newref, elem);
@@ -9623,7 +9624,7 @@ vectorizable_store (vec_info *vinfo,
            TREE_TYPE (data_ref)
              = build_aligned_type (TREE_TYPE (data_ref),
                                    align * BITS_PER_UNIT);
-         vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
+         vect_copy_ref_info (data_ref, first_stmt_info);
          new_stmt = gimple_build_assign (data_ref, vec_oprnd);
          vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
        }
@@ -10525,7 +10526,7 @@ vectorizable_load (vec_info *vinfo,
              tree this_off = build_int_cst (TREE_TYPE (alias_off),
                                             load_el * elsz + cst_offset);
              tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
-             vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
+             vect_copy_ref_info (data_ref, first_stmt_info);
              new_temp = make_ssa_name (ltype);
              new_stmt = gimple_build_assign (new_temp, data_ref);
              vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
@@ -11424,7 +11425,7 @@ vectorizable_load (vec_info *vinfo,
          /* DATA_REF is null if we've already built the statement.  */
          if (data_ref)
            {
-             vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
+             vect_copy_ref_info (data_ref, first_stmt_info);
              new_stmt = gimple_build_assign (vec_dest, data_ref);
            }
          new_temp = (need_zeroing
@@ -11843,8 +11844,7 @@ vectorizable_load (vec_info *vinfo,
                  data_ref = build_constructor (vectype, NULL);
                else if (ltype != vectype)
                  {
-                   vect_copy_ref_info (data_ref,
-                                       DR_REF (first_dr_info->dr));
+                   vect_copy_ref_info (data_ref, first_stmt_info);
                    tree tem = make_ssa_name (ltype);
                    new_stmt = gimple_build_assign (tem, data_ref);
                    vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
@@ -11926,7 +11926,7 @@ vectorizable_load (vec_info *vinfo,
            vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
            data_ref = build2 (MEM_REF, vectype,
                               ptr, build_int_cst (ref_type, 0));
-           vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
+           vect_copy_ref_info (data_ref, first_stmt_info);
            vec_dest = vect_create_destination_var (scalar_dest, vectype);
            new_stmt = gimple_build_assign (vec_dest, data_ref);
            new_temp = make_ssa_name (vec_dest, new_stmt);
@@ -12004,7 +12004,7 @@ vectorizable_load (vec_info *vinfo,
          /* DATA_REF is null if we've already built the statement.  */
          if (data_ref)
            {
-             vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
+             vect_copy_ref_info (data_ref, first_stmt_info);
              new_stmt = gimple_build_assign (vec_dest, data_ref);
            }
 
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index bee7055c7ff5..780fd67371d6 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -2663,7 +2663,7 @@ extern tree vect_create_data_ref_ptr (vec_info *,
                                      tree = NULL_TREE);
 extern tree bump_vector_ptr (vec_info *, tree, gimple_stmt_iterator *,
                             stmt_vec_info, tree);
-extern void vect_copy_ref_info (tree, tree);
+extern void vect_copy_ref_info (tree, stmt_vec_info);
 extern tree vect_create_destination_var (tree, tree);
 extern bool vect_grouped_store_supported (tree, unsigned HOST_WIDE_INT);
 extern internal_fn vect_store_lanes_supported (tree, unsigned HOST_WIDE_INT, 
bool);

Reply via email to