Use wide_int for shift-and-mask folding on wider types
Enables shift-and-mask folding optimization for __int128 and wider types.
The GCC optimization pattern for folding shift-and-mask operations like (X >> C1) & C2 was previously limited to types with precision up to HOST_BITS_PER_WIDE_INT. This prevented the optimization from being applied to wider types such as __int128. This commit updates the pattern to use wide_int for its internal calculations, removing the precision limit and allowing the folding to occur on larger integer types. New tests are included to verify the fix.
In Details
The match.pd pattern for folding shift-and-mask operations (e.g., (X >> C1) & C2, (X << C1) & C2) previously relied on unsigned HOST_WIDE_INT for mask and zero-bit calculations. This restricted its application to types with precision no greater than HOST_BITS_PER_WIDE_INT. This patch modifies the pattern to use wide_int throughout, removing the TYPE_PRECISION guard. It also replaces the mode-mask loop with wi::exact_log2 to efficiently check for power-of-two masks and handles the all-ones mask case explicitly to avoid overflow issues. This enables the optimization for types li…
- wide_int
- A GCC internal data type used to represent arbitrary-precision integers. It allows the compiler to perform arithmetic on integer types wider than the host machine's native word size, such as
__int128. - shift-and-mask folding
- An optimization technique where a sequence of a shift operation followed by a bitwise AND operation is replaced by a single, equivalent operation or constant. This can simplify the generated code and improve performance.
- GIMPLE
- GCC's internal three-address code representation. It's an intermediate language used after parsing and before final code generation, facilitating various optimizations.
- __int128
- A GCC extension that provides a 128-bit integer type, allowing for operations on larger integer values than standard C types.
- HOST_BITS_PER_WIDE_INT
- A preprocessor macro defining the number of bits in a
HOST_WIDE_INT, which is typically the machine word size of the host compiler system.