With the PR125296 fix I forgot to handle the LHS propagation case.
Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
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.
---
gcc/testsuite/gcc.dg/pr125668.c | 20 ++++++++++++++++++++
gcc/tree-ssa-forwprop.cc | 6 ++++--
2 files changed, 24 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/pr125668.c
diff --git a/gcc/testsuite/gcc.dg/pr125668.c b/gcc/testsuite/gcc.dg/pr125668.c
new file mode 100644
index 00000000000..4cacc4bd235
--- /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 922e36590fd..9bb001d2f63 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -760,8 +760,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);
--
2.51.0