Fix incorrect equivalence between SUBREGs with different promotion states
Corrects a long-standing bug in expression equivalence checking that affected SUBREGs.
This commit fixes a critical bug in GCC’s expression equivalence checking (exp_equiv_p), which incorrectly treated SUBREG expressions with different promotion states as equivalent. This issue, latent for decades and exposed by recent RISC-V backend changes, could lead to incorrect code generation. Specifically, it caused Common Subexpression Elimination (CSE) and Global CSE (GCSE) to remove valid code, resulting in combine passes incorrectly removing zero-extension instructions, ultimately producing wrong code.
In Details
The exp_equiv_p function, used by CSE and GCSE, incorrectly returned true for SUBREG expressions exhibiting different promotion states. This bug was exacerbated by a recent RISC-V backend change that ceased unconditionally pretending zero-extension instructions were always available during expansion, now emitting shift pairs when extensions are absent. When these shift pairs exposed an underlying SUBREG with differing promotion states in different contexts, GCSE incorrectly fused them. The resulting representative instance, carrying promotion state markers, was inserted into a context w…
- SUBREG
- In GCC's intermediate representation (RTL), a SUBREG represents a sub-register or a value that is a portion of a larger register, often created by zero-extension or sign-extension operations.
- promotion state
- Refers to whether a value has been zero-extended or sign-extended to match the width of a larger register or data type. This state is important for correct arithmetic and logical operations.
- CSE (Common Subexpression Elimination)
- An optimization pass that identifies and replaces multiple occurrences of the same expression with a single temporary variable holding the result.
- GCSE (Global Common Subexpression Elimination)
- An extension of CSE that performs common subexpression elimination across basic blocks, not just within a single block.
- RTL (Register Transfer Language)
- GCC's intermediate representation, which describes operations at a lower level than the compiler's tree representation, closer to machine instructions.