The following fixes conditional store elimination and store motion
so they consider stores to STRING_CSTs as trapping.
Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
PR tree-optimization/120341
* tree-ssa-loop-im.cc (can_sm_ref_p): STRING_CSTs are readonly.
* tree-ssa-phiopt.cc (cond_store_replacement): Likewise.
* gcc.dg/torture/pr120341-1.c: New testcase.
* gcc.dg/torture/pr120341-2.c: Likewise.
---
gcc/testsuite/gcc.dg/torture/pr120341-1.c | 11 +++++++++++
gcc/testsuite/gcc.dg/torture/pr120341-2.c | 13 +++++++++++++
gcc/tree-ssa-loop-im.cc | 3 ++-
gcc/tree-ssa-phiopt.cc | 5 +++--
4 files changed, 29 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr120341-1.c
create mode 100644 gcc/testsuite/gcc.dg/torture/pr120341-2.c
diff --git a/gcc/testsuite/gcc.dg/torture/pr120341-1.c
b/gcc/testsuite/gcc.dg/torture/pr120341-1.c
new file mode 100644
index 00000000000..e23185b62b0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr120341-1.c
@@ -0,0 +1,11 @@
+/* { dg-do run } */
+/* { dg-additional-options "-fallow-store-data-races" } */
+
+char a, *b;
+int main()
+{
+ b = "0";
+ if (a)
+ b[0]++;
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr120341-2.c
b/gcc/testsuite/gcc.dg/torture/pr120341-2.c
new file mode 100644
index 00000000000..7bcc96f63dd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr120341-2.c
@@ -0,0 +1,13 @@
+/* { dg-do run } */
+/* { dg-additional-options "-fallow-store-data-races" } */
+
+char a, *b;
+int main()
+{
+ while (a)
+ {
+ b = "0";
+ b[0]++;
+ }
+ return 0;
+}
diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc
index 50d2ee9b99d..aee419aa5b1 100644
--- a/gcc/tree-ssa-loop-im.cc
+++ b/gcc/tree-ssa-loop-im.cc
@@ -3307,7 +3307,8 @@ can_sm_ref_p (class loop *loop, im_mem_ref *ref)
explicitly. */
base = get_base_address (ref->mem.ref);
if ((tree_could_trap_p (ref->mem.ref)
- || (DECL_P (base) && TREE_READONLY (base)))
+ || (DECL_P (base) && TREE_READONLY (base))
+ || TREE_CODE (base) == STRING_CST)
/* ??? We can at least use false here, allowing loads? We
are forcing conditional stores if the ref is not always
stored to later anyway. So this would only guard
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 8c5908e5bff..bf493e12987 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -3525,8 +3525,9 @@ cond_store_replacement (basic_block middle_bb,
basic_block join_bb,
/* tree_could_trap_p is a predicate for rvalues, so check
for readonly memory explicitly. */
|| ((base = get_base_address (lhs))
- && DECL_P (base)
- && TREE_READONLY (base)))
+ && ((DECL_P (base)
+ && TREE_READONLY (base))
+ || TREE_CODE (base) == STRING_CST)))
return false;
}
--
2.43.0