[gcc r13-10148] tree-optimization/120341 - stores into STRING_CSTs can trap

Richard Biener rguenth@gcc.gnu.org
Fri Mar 20 09:59:42 GMT 2026


https://gcc.gnu.org/g:8661ecc796c896c19c69f41a8a3658fdc9161832

commit r13-10148-g8661ecc796c896c19c69f41a8a3658fdc9161832
Author: Richard Biener <rguenther@suse.de>
Date:   Fri May 30 13:47:55 2025 +0200

    tree-optimization/120341 - stores into STRING_CSTs can trap
    
    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.
    
            * gcc.dg/torture/pr120341-1.c: New testcase.
            * gcc.dg/torture/pr120341-2.c: Likewise.
    
    (cherry picked from commit d16cbe084c90a151a2fc326daeb7287485cb31d0)

Diff:
---
 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 ++-
 3 files changed, 26 insertions(+), 1 deletion(-)

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 000000000000..e23185b62b07
--- /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 000000000000..7bcc96f63ddc
--- /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 28813f11b99a..e62c8913ff14 100644
--- a/gcc/tree-ssa-loop-im.cc
+++ b/gcc/tree-ssa-loop-im.cc
@@ -3180,7 +3180,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


More information about the Gcc-cvs mailing list