]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/120043 - bogus conditional store elimination
authorRichard Biener <rguenther@suse.de>
Thu, 8 May 2025 08:05:42 +0000 (10:05 +0200)
committerRichard Biener <rguenth@gcc.gnu.org>
Thu, 8 May 2025 10:59:02 +0000 (12:59 +0200)
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.

gcc/testsuite/gcc.dg/torture/pr120043.c [new file with mode: 0644]
gcc/tree-ssa-phiopt.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr120043.c b/gcc/testsuite/gcc.dg/torture/pr120043.c
new file mode 100644 (file)
index 0000000..ae27468
--- /dev/null
@@ -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;
+}
index efd43d2d77e74dbe714a8739d72d9be1f19be585..9724040fc3d20ba68a7e0b4a9316a08ef30f3272 100644 (file)
@@ -3526,8 +3526,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;
     }