ext-dce: Narrow Sign-Extending Loads to Zero-Extending When Upper Bits Are Dead
The ext-dce pass now optimizes sign-extending loads to zero-extending loads when the upper bits are unused, improving code generation.
The ext-dce (extended dead code elimination) pass now optimizes sign-extending loads into zero-extending loads when it can prove that the upper bits of the loaded value are not used. This optimization applies to memory operands (loads). By converting, for example, a sign-extending lh to a zero-extending lhu on RISC-V, the compiler can generate more efficient code sequences.
In Details
The commit enhances ext_dce_try_optimize_extension in ext-dce.cc to handle SIGN_EXTEND of MEM operands. When the bit-level liveness analysis determines that the sign bits are dead, the code replaces the sign-extending load with a zero-extending load using validate_change. This optimization was previously limited to register operands and is now extended to memory operands.
For Context
Dead code elimination is a compiler optimization that removes code that doesn't affect the program's result. This commit extends the ext-dce pass to recognize when the upper bits of a loaded value are unused. In such cases, a sign-extending load (which preserves the sign of the value) can be safely replaced with a zero-extending load (which fills the upper bits with zeros), potentially leading to more efficient machine code on architectures like RISC-V and AArch64.