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.
Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. The previous idea of changing tree_could_trap_p had too much fallout. Richard. 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 | 10 ++++++++++ gcc/tree-ssa-phiopt.cc | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr120043.c diff --git a/gcc/testsuite/gcc.dg/torture/pr120043.c b/gcc/testsuite/gcc.dg/torture/pr120043.c new file mode 100644 index 00000000000..ae27468d86d --- /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 54ecd93495a..4e96cf33a52 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -3546,8 +3546,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; } -- 2.43.0