The following preserves the SSO when performing loop store motion.
Bootstrapped and tested on x86_64-unknown-linux-gnu.
PR tree-optimization/128461
* tree-ssa-loop-im.cc (gather_mem_refs_stmt): Preserve SSO
when building a canonicalized ref.
* gcc.dg/torture/pr128461.c: New testcase.
---
gcc/testsuite/gcc.dg/torture/pr128461.c | 24 ++++++++++++++++++++++++
gcc/tree-ssa-loop-im.cc | 2 ++
2 files changed, 26 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr128461.c
diff --git a/gcc/testsuite/gcc.dg/torture/pr128461.c
b/gcc/testsuite/gcc.dg/torture/pr128461.c
new file mode 100644
index 00000000000..cac07eef249
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128461.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+
+#pragma scalar_storage_order big-endian
+struct S {
+ short a;
+};
+#pragma scalar_storage_order default
+
+struct S g[2];
+int n = 1;
+
+int main ()
+{
+ int x = 0;
+ g[1].a = 2;
+ for (int i = 0; i < n; i++)
+ {
+ x = g[1].a;
+ (&g[0])[1].a = 0;
+ }
+ if (x != 2)
+ __builtin_abort ();
+ return 0;
+}
diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc
index 04f6e9282ee..5bbeb5048b2 100644
--- a/gcc/tree-ssa-loop-im.cc
+++ b/gcc/tree-ssa-loop-im.cc
@@ -1757,6 +1757,8 @@ gather_mem_refs_stmt (class loop *loop, gimple *stmt)
build_int_cst (ref_alias_type, mem_off));
if ((*slot)->mem.volatile_p)
TREE_THIS_VOLATILE (new_ref) = 1;
+ if (reverse_storage_order_for_component_p (*mem))
+ REF_REVERSE_STORAGE_ORDER (new_ref) = 1;
(*slot)->mem.ref = new_ref;
/* Make sure the recorded base and offset are consistent
with the newly built ref. */
base-commit: 06c476d192702a07dc6a8431b7f1a54d7526d3a5
--
2.51.0