[gcc r16-9269] tree-optimization/125668 - missed alignment updating from forwprop

Richard Biener via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:9c4272c58571ddcae364d1fc7d01a30f213dce10

commit r16-9269-g9c4272c58571ddcae364d1fc7d01a30f213dce10
Author: Richard Biener <[email protected]>
Date:   Mon Jun 8 14:02:38 2026 +0200

    tree-optimization/125668 - missed alignment updating from forwprop
    
    With the PR125296 fix I forgot to handle the LHS propagation case.
    
            PR tree-optimization/125668
            * tree-ssa-forwprop.cc (forward_propagate_addr_expr_1):
            Preserve alignment of the original access also for propagating
            into the LHS.
    
            * gcc.dg/pr125668.c: New testcase.
    
    (cherry picked from commit 878b5b0030d4b42b94b7df38375d6bf5f187dc33)

Diff:
---
 gcc/testsuite/gcc.dg/pr125668.c | 20 ++++++++++++++++++++
 gcc/tree-ssa-forwprop.cc        |  6 ++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/pr125668.c b/gcc/testsuite/gcc.dg/pr125668.c
new file mode 100644
index 000000000000..4cacc4bd2350
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr125668.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-strict-aliasing -fdump-tree-forwprop1-gimple" } */
+
+struct S {
+  long x;
+  int y;
+};
+
+struct S2 {
+  struct S bufs[];
+};
+
+void foo(struct S2 *p, int i)
+{
+  struct S *buf = &p->bufs[i];
+  if (*((volatile int *)&buf->y) < 0)
+    *((volatile int *)&buf->y) = 0;
+}
+
+/* { dg-final { scan-tree-dump-times ", 32>" 2 "forwprop1" { target lp64 } } } */
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index 47f897a9e1ad..6b745382fa0e 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -770,8 +770,10 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs,
 	      new_base = build_fold_addr_expr (*def_rhs_basep);
 	      new_offset = TREE_OPERAND (lhs, 1);
 	    }
-	  *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
-				   new_base, new_offset);
+	  tree atype = TREE_TYPE (*def_rhs_basep);
+	  if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
+	    atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
+	  *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
 	  TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
 	  TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
 	  TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.