]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/120341 - stores into STRING_CSTs can trap
authorRichard Biener <rguenther@suse.de>
Fri, 30 May 2025 11:47:55 +0000 (13:47 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 6 Jun 2025 07:53:11 +0000 (09:53 +0200)
The following fixes conditional store elimination and store motion
so they consider stores to STRING_CSTs as trapping.

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.

(cherry picked from commit 02c58bc4b0885f5b6f50033da35768ebe6c4a030)

gcc/testsuite/gcc.dg/torture/pr120341-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/pr120341-2.c [new file with mode: 0644]
gcc/tree-ssa-loop-im.cc
gcc/tree-ssa-phiopt.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr120341-1.c b/gcc/testsuite/gcc.dg/torture/pr120341-1.c
new file mode 100644 (file)
index 0000000..e23185b
--- /dev/null
@@ -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 (file)
index 0000000..7bcc96f
--- /dev/null
@@ -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;
+}
index 225964c621565e46282a7279dd43359f6e811d82..71a46f7e03f9579d9c3874a045981b5538fa0109 100644 (file)
@@ -3293,7 +3293,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
index 7f3390be31e56bb11fe6ebfc102654ec836f5800..aaebae6b38c0389f6caa04f9fa765952aeb67c66 100644 (file)
@@ -3565,8 +3565,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;
     }