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

commit r17-6406-gd7837b35f0195737e7afbb2a16f47b49ef743a91
Author: Richard Biener <[email protected]>
Date:   Wed Nov 18 11:42:30 2026 +0100

    Allow redundant store skipping itself on loop backedge
    
    When searching for an earlier redundant store in vn_reference_lookup,
    avoiding same-val store skipping as required for fixing PR128818
    would prevent recognizing a store in a loop as redundant with an
    earlier store before the loop.  Along the loop backedge, the walk
    encounters the store itself from the previous iteration.  Because the
    store being eliminated will no longer execute on any iteration if removed,
    it can be safely ignored when encountered along a backedge without altering
    effective types under TBAA.
    
    Pass the candidate statement being eliminated as redundant_store to
    vn_reference_lookup and vn_walk_cb_data, and ignore def_stmt in
    vn_reference_lookup_3 when def_stmt == data->redundant_store and we
    are disambiguating along a backedge.
    
            * tree-ssa-sccvn.h (vn_reference_lookup): Change parameter
            redundant_store_removal_p from bool to gimple *redundant_store.
            * tree-ssa-sccvn.cc (struct vn_walk_cb_data): Change field
            redundant_store_removal_p to gimple *redundant_store.
            (vn_reference_lookup_3): Ignore def_stmt if it is the redundant
            store itself on a backedge.
            (vn_reference_lookup_pieces): Pass NULL redundant_store.
            (vn_reference_lookup): Update parameter and pass down
            redundant_store.
            (eliminate_dom_walker::eliminate_stmt): Pass stmt as
            redundant_store to vn_reference_lookup.
    
            * gcc.dg/tree-ssa/pr87126.c: Remove XFAIL for the redundant
            store removal.

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/pr87126.c |  4 ++--
 gcc/tree-ssa-sccvn.cc                   | 25 +++++++++++++++++--------
 gcc/tree-ssa-sccvn.h                    |  2 +-
 3 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr87126.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr87126.c
index 37232ba3a0a8..ff6136dc1d49 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr87126.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr87126.c
@@ -20,6 +20,6 @@ L2:
 
 /* The load of d could be eliminated if we'd value-number the
    irreducible region in RPO of the reducible result.  Likewise
-   a redundant store could be removed.  */
-/* { dg-final { scan-tree-dump-times "d = 0;" 1 "fre1" { xfail *-*-* } } } */
+   a redundant store can be removed.  */
+/* { dg-final { scan-tree-dump-times "d = 0;" 1 "fre1" } } */
 /* { dg-final { scan-tree-dump-not " = d;" "fre1" { xfail *-*-* } } } */
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index cbed8a2302a8..828dc2d4703f 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -1941,11 +1941,11 @@ struct vn_walk_cb_data
 {
   vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
                   vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
-                  bool redundant_store_removal_p_)
+                  gimple *redundant_store_)
     : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
       mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
       vn_walk_kind (vn_walk_kind_),
-      tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
+      tbaa_p (tbaa_p_), redundant_store (redundant_store_),
       saved_operands (vNULL), first_range (), first_set (-2),
       first_base_set (-2), using_dummy_vr (false)
   {
@@ -2008,7 +2008,7 @@ struct vn_walk_cb_data
   tree same_val;
   vn_lookup_kind vn_walk_kind;
   bool tbaa_p;
-  bool redundant_store_removal_p;
+  gimple *redundant_store;
   vec<vn_reference_op_s> saved_operands;
 
   /* The VDEFs of partial defs we come along.  */
@@ -2876,6 +2876,15 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void 
*data_,
   bool lhs_ref_ok = false;
   poly_int64 copy_size;
 
+  /* If def_stmt is the redundant store itself, we can ignore it:
+     we can only reach it via a backedge, and if we eliminate it, it no longer
+     executes on any iteration.  */
+  if (def_stmt == data->redundant_store)
+    {
+      *disambiguate_only = TR_DISAMBIGUATE;
+      return NULL;
+    }
+
   /* First try to disambiguate after value-replacing in the definitions LHS.  
*/
   if (is_gimple_assign (def_stmt))
     {
@@ -2908,7 +2917,7 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void 
*data_,
         Avoid this for obvious must aliases to save compile-time though.
         We also may not do this when the query is used for redundant
         store removal.  */
-      if (!data->redundant_store_removal_p
+      if (!data->redundant_store
          && gimple_clobber_p (def_stmt)
          && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
        {
@@ -4263,7 +4272,7 @@ vn_reference_lookup_pieces (tree vuse, alias_set_type set,
       ao_ref r;
       unsigned limit = param_sccvn_max_alias_queries_per_access;
       vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
-                           false);
+                           NULL);
       vec<vn_reference_op_s> ops_for_ref;
       if (!valueized_p)
        ops_for_ref = vr1.operands;
@@ -4364,7 +4373,7 @@ tree
 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
                     vn_reference_t *vnresult, bool tbaa_p,
                     tree *last_vuse_ptr, tree mask,
-                    bool redundant_store_removal_p)
+                    gimple *redundant_store)
 {
   vec<vn_reference_op_s> operands;
   struct vn_reference_s vr1;
@@ -4432,7 +4441,7 @@ vn_reference_lookup (tree op, tree vuse, vn_lookup_kind 
kind,
        }
       vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
                            last_vuse_ptr, kind, tbaa_p, mask,
-                           redundant_store_removal_p);
+                           redundant_store);
 
       wvnresult
        = ((vn_reference_t)
@@ -7626,7 +7635,7 @@ eliminate_dom_walker::eliminate_stmt (basic_block b, 
gimple_stmt_iterator *gsi)
       if (lookup_lhs)
        val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
                                   VN_WALKREWRITE, &vnresult, false,
-                                  NULL, NULL_TREE, true);
+                                  NULL, NULL_TREE, stmt);
       if (TREE_CODE (rhs) == SSA_NAME)
        rhs = VN_INFO (rhs)->valnum;
       gassign *ass;
diff --git a/gcc/tree-ssa-sccvn.h b/gcc/tree-ssa-sccvn.h
index f8ff1cc1668a..361bec016299 100644
--- a/gcc/tree-ssa-sccvn.h
+++ b/gcc/tree-ssa-sccvn.h
@@ -268,7 +268,7 @@ tree vn_reference_lookup_pieces (tree, alias_set_type, 
alias_set_type, tree,
                                 vec<vn_reference_op_s> ,
                                 vn_reference_t *, vn_lookup_kind);
 tree vn_reference_lookup (tree, tree, vn_lookup_kind, vn_reference_t *, bool,
-                         tree * = NULL, tree = NULL_TREE, bool = false);
+                         tree * = NULL, tree = NULL_TREE, gimple * = NULL);
 void vn_reference_lookup_call (gcall *, vn_reference_t *, vn_reference_t);
 vn_reference_t vn_reference_insert_pieces (tree, alias_set_type, 
alias_set_type,
                                           poly_int64, poly_int64,

Reply via email to