https://gcc.gnu.org/g:856c493db1d5e2fa9377f4a0c438afbcaf6c7e01

commit r15-9651-g856c493db1d5e2fa9377f4a0c438afbcaf6c7e01
Author: Richard Biener <rguent...@suse.de>
Date:   Thu May 8 10:05:42 2025 +0200

    tree-optimization/120043 - bogus conditional store elimination
    
    The following fixes conditional store elimination to properly
    check for conditional stores to readonly memory which we can
    obviously not store to unconditionally.  The tree_could_trap_p
    predicate used is only considering rvalues and the chosen
    approach mimics that of loop store motion.
    
            PR tree-optimization/120043
            * tree-ssa-phiopt.cc (cond_store_replacement): Check
            whether the store is to readonly memory.
    
            * gcc.dg/torture/pr120043.c: New testcase.
    
    (cherry picked from commit 93586e5d51188bf71f4f8fae4ee94ff631740f24)

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr120043.c | 10 ++++++++++
 gcc/tree-ssa-phiopt.cc                  |  8 +++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr120043.c 
b/gcc/testsuite/gcc.dg/torture/pr120043.c
new file mode 100644
index 000000000000..ae27468d86dc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr120043.c
@@ -0,0 +1,10 @@
+/* { dg-do run } */
+/* { dg-additional-options "-fallow-store-data-races" } */
+
+const int a;
+int *b;
+int main()
+{
+  &a != b || (*b = 1);
+  return 0;
+}
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index a194bf675e4e..7f3390be31e5 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -3559,8 +3559,14 @@ cond_store_replacement (basic_block middle_bb, 
basic_block join_bb,
       /* If LHS is an access to a local variable without address-taken
         (or when we allow data races) and known not to trap, we could
         always safely move down the store.  */
+      tree base;
       if (ref_can_have_store_data_races (lhs)
-         || tree_could_trap_p (lhs))
+         || tree_could_trap_p (lhs)
+         /* 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)))
        return false;
     }

Reply via email to