Fix truncation of AND of LSHIFTRT for wide modes
Corrects an optimization bug involving bitwise AND and right shifts on wide integer types.
This commit fixes a bug in the simplify_truncation pass that incorrectly handled the truncation of a bitwise AND operation applied to a right-shifted value when using wide integer modes. The issue occurred when the source operand mode (OM) was wider than a host word, leading to incorrect code generation for certain bitwise operations. The fix involves more direct checks to ensure that mask bits are within the defined range of the narrowed shift operation, preventing incorrect bit manipulation.
In Details
The simplify_context::simplify_truncation function was incorrectly simplifying (truncate:M (and:OM (lshiftrt:OM x C) C2)) to (and:M (lshiftrt:M (truncate:M x) C) C2) when OM was wider than HOST_BITS_PER_WIDE_INT. This occurred because GET_MODE_MASK was capped, causing the comparison of masks to always succeed. The fix adds checks to ensure no bit of C2 falls outside the bits defined by the narrowed shift and requires M to be HWI computable for accurate GET_MODE_MASK behavior, resolving incorrect bit manipulation for wide modes.
- simplify-rtx
- A GCC optimization pass that simplifies Register Transfer Language (RTL) expressions.
- RTL
- Register Transfer Language, GCC's internal intermediate representation used for lower-level optimizations and machine code generation.
- LSHIFTRT
- Logical Shift Right operation in RTL.
- AND
- Bitwise AND operation.
- Truncate
- An operation that reduces the number of bits in a value, typically by discarding the most significant bits.
- Wide Modes
- GCC internal data types (modes) that represent integers wider than a machine word, such as TImode (128 bits on a 64-bit host).
- HOST_BITS_PER_WIDE_INT
- A macro defining the number of bits in a host's 'wide' integer type, typically used for intermediate calculations in GCC.