pair-fusion: Verify that a fused pair really is adjacent
Fixes pair-fusion optimization to prevent incorrect code generation by ensuring fused memory accesses are truly adjacent.
The pair-fusion optimization pass has been corrected to accurately verify that fused memory accesses are indeed adjacent. Previously, when using MEM_EXPR base offsets, the pass could incorrectly identify non-adjacent memory accesses as adjacent, leading to silent generation of wrong code. This fix adds a check to compare offsets relative to a common base register, preventing such misinterpretations and ensuring correctness.
In Details
The fuse_pair function in pair-fusion.cc was incorrectly merging MEM_EXPRs that shared a base register but had differing offsets, leading to invalid fusion. This was particularly problematic for stack slots. The fix ensures that the two memory accesses are exactly one access size apart when their base registers match, enforcing the adjacency invariant and preventing incorrect code generation by making violations an ICE.
- pair-fusion
- An optimization pass that attempts to fuse adjacent memory access operations (reads or writes) into a single, more efficient instruction.
- MEM_EXPR
- A GCC intermediate representation that denotes a memory reference, typically comprising a base address and an offset.
- out-of-SSA
- Refers to transformations that occur after the Static Single Assignment (SSA) form has been established, potentially breaking SSA invariants. The pass in question must maintain these invariants.