https://gcc.gnu.org/g:439b14e222571da76da2bfec04b9035fb9f1862d

commit r16-2062-g439b14e222571da76da2bfec04b9035fb9f1862d
Author: Richard Biener <rguent...@suse.de>
Date:   Mon Jul 7 09:56:50 2025 +0200

    tree-optimization/120817 - bogus DSE of .MASK_STORE
    
    DSE used ao_ref_init_from_ptr_and_size for .MASK_STORE but
    alias-analysis will use the specified size to disambiguate
    against smaller objects.  For .MASK_STORE we instead have to
    make the access size unspecified but we can still constrain
    the access extent based on the maximum size possible.
    
            PR tree-optimization/120817
            * tree-ssa-dse.cc (initialize_ao_ref_for_dse): Use
            ao_ref_init_from_ptr_and_range with unknown size for
            .MASK_STORE and .MASK_LEN_STORE.
    
            * gcc.dg/vect/pr120817.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/vect/pr120817.c | 40 ++++++++++++++++++++++++++++++++++++
 gcc/tree-ssa-dse.cc                  |  8 ++++----
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/vect/pr120817.c 
b/gcc/testsuite/gcc.dg/vect/pr120817.c
new file mode 100644
index 000000000000..d8f55c9b98d2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr120817.c
@@ -0,0 +1,40 @@
+/* { dg-additional-options "-O1" } */
+/* { dg-additional-options "-mcpu=neoverse-n2" { target aarch64*-*-* } } */
+
+#include "tree-vect.h"
+
+typedef struct {
+    int _M_current;
+} __normal_iterator;
+
+typedef struct {
+    char _M_elems[5];
+} array_5;
+
+__normal_iterator __trans_tmp_1 = {-5};
+
+__attribute__((noipa))
+array_5 copySourceIntoTarget() {
+    array_5 target;
+    char* target_it = target._M_elems;
+
+    while (__trans_tmp_1._M_current != 0) {
+        *target_it = 1;
+        __trans_tmp_1._M_current++;
+        target_it++;
+    }
+
+    return target;
+}
+
+int main ()
+{
+  check_vect ();
+
+  array_5 res = copySourceIntoTarget();
+
+#pragma GCC novector
+  for (int i = 0; i < 5; i++)
+    if (res._M_elems[i] != 1)
+      __builtin_abort ();
+}
diff --git a/gcc/tree-ssa-dse.cc b/gcc/tree-ssa-dse.cc
index 5ac4280ee361..51a572316cdc 100644
--- a/gcc/tree-ssa-dse.cc
+++ b/gcc/tree-ssa-dse.cc
@@ -181,10 +181,10 @@ initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write, 
bool may_def_ok = false)
               can provide a may-def variant.  */
            if (may_def_ok)
              {
-               ao_ref_init_from_ptr_and_size (
-                 write, gimple_call_arg (stmt, 0),
-                 TYPE_SIZE_UNIT (
-                   TREE_TYPE (gimple_call_arg (stmt, stored_value_index))));
+               ao_ref_init_from_ptr_and_range (
+                 write, gimple_call_arg (stmt, 0), true, 0, -1,
+                 tree_to_poly_int64 (TYPE_SIZE (
+                   TREE_TYPE (gimple_call_arg (stmt, stored_value_index)))));
                return true;
              }
            break;

Reply via email to