When investigating PR111591 with respect to TBAA and stack slot sharing
I noticed we're eventually scrapping a [TARGET_]MEM_REF offset when
rewriting the VAR_DECL base of the MEM_EXPR to use a pointer to the
partition instead.  The following makes sure to preserve that.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

        * emit-rtl.cc (set_mem_attributes_minus_bitpos): Preserve
        the offset when rewriting an exising MEM_REF base for
        stack slot sharing.
---
 gcc/emit-rtl.cc | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/gcc/emit-rtl.cc b/gcc/emit-rtl.cc
index 84b6833225e..4a7e420e7c0 100644
--- a/gcc/emit-rtl.cc
+++ b/gcc/emit-rtl.cc
@@ -2128,9 +2128,15 @@ set_mem_attributes_minus_bitpos (rtx ref, tree t, int 
objectp,
              tree *orig_base = &attrs.expr;
              while (handled_component_p (*orig_base))
                orig_base = &TREE_OPERAND (*orig_base, 0);
-             tree aptrt = reference_alias_ptr_type (*orig_base);
-             *orig_base = build2 (MEM_REF, TREE_TYPE (*orig_base), *namep,
-                                  build_int_cst (aptrt, 0));
+             if (TREE_CODE (*orig_base) == MEM_REF
+                 || TREE_CODE (*orig_base) == TARGET_MEM_REF)
+               TREE_OPERAND (*orig_base, 0) = *namep;
+             else
+               {
+                 tree aptrt = reference_alias_ptr_type (*orig_base);
+                 *orig_base = build2 (MEM_REF, TREE_TYPE (*orig_base),
+                                      *namep, build_int_cst (aptrt, 0));
+               }
            }
        }
 
-- 
2.35.3

Reply via email to