Simplify shift operations by dropping no-longer-needed bitwise operands
Compiler optimization removes redundant bitwise operations before shifts.
A new simplification rule in match.pd allows the compiler to drop operands from bitwise XOR or OR operations when all their set bits are below the shift count. This optimization, inspired by real-world code where flag bits are packed into a word, improves generated code by eliminating unnecessary operations. The rule applies to both GENERIC and GIMPLE expressions and has been verified on aarch64.
In Details
This commit adds a new simplification rule to match.pd for GENERIC and GIMPLE. The rule identifies expressions of the form (X ^ Y) >> C or (X | Y) >> C where Y (or X in the case of XOR) has all its set bits below the shift count C. In such cases, Y contributes no information to the shifted result, and the bitwise operation can be removed. This is particularly useful when Y represents packed flag bits.
- match.pd
- A file containing pattern-matching rules used by GCC's tree-ssa optimizer to perform expression simplifications and transformations.
- GENERIC
- GCC's first intermediate representation (IR) after parsing, representing code in a generic, tree-based structure.
- GIMPLE
- GCC's second intermediate representation (IR), a three-address code form derived from GENERIC, used for most optimizations.
- tree-ssa
- GCC's optimization framework that utilizes the Static Single Assignment (SSA) form on the GENERIC/GIMPLE intermediate representations.