Cselim prevents store sinking with loads in middle basic block
Cselim optimization now avoids sinking stores across basic blocks if there are loads accessing the same memory in an intermediate block, preventing incorrect c…
A recent change to the Common Subexpression Elimination (CSElim) optimization allowed store sinking even when a middle basic block was non-empty and contained active loads. This could incorrectly mark a symbol as non-trapping, leading to wrong code if an intermediate load accessed the stored value before the store was sunk. The cond_store_replacement function in tree-ssa-phiopt.cc now explicitly checks for loads within the middle basic block, disabling store sinking in such cases to ensure correctness.
In Details
Common Subexpression Elimination (CSElim) is a GCC optimization pass that identifies and removes redundant computations. The tree-ssa-phiopt.cc file contains implementation details for various SSA-based optimizations, including components affecting cselim. Specifically, cond_store_replacement attempts to optimize conditional stores by 'sinking' them, consolidating multiple conditional stores into a single store. The previous change (r17-1273-g391ee229b737eb) broadened this with non-empty middle basic blocks. However, this introduced a bug where if the left-hand side (LHS) of a store was…
For Context
Compilers try to make your code run faster through a process called 'optimization'. One such optimization, Common Subexpression Elimination (CSElim), looks for duplicate calculations and removes them. Sometimes, compilers can also move (or 'sink') operations like 'stores' (writing a value to memory) to a later point if it doesn't change the program's behavior. A recent update to this optimization introduced a problem: it would sometimes move a 'store' past a 'load' (reading a value from memory) that was supposed to read the value *before* it was stored. This could lead to the program reading incorrect data, causing bugs or crashes. The fix ensures that the compiler is more careful when moving 'stores' and only does so if there are no 'loads' in between that would be affected, guaranteeing the program's correctness.