Add operand type checks to signed saturation add patterns
Fixes incorrect saturation results by ensuring operand types match the expected result type.
This commit addresses issues in match-sat-alu.pd where signed saturation add operations (.SAT_ADD) did not properly check operand types, leading to incorrect results, particularly when used by the vectorizer. The patterns now enforce that the operands’ types are consistent with the result type of the saturation operation, preventing cases where intermediate conversions cause unexpected behavior, such as saturation to the wrong value range.
In Details
Several forms of signed_integer_sat_add in match-sat-alu.pd were found to be incorrect. The .ADD_OVERFLOW form lacked a types_match check, and other forms allowed operands that, after implicit or explicit conversion, did not match the result type. This commit adds checks to ensure that the primary operand X matches the result type and that the immediate operand IMM either matches or is a constant that fits the result type. This resolves incorrect saturation behavior, especially when operations are widened or narrowed.
- .SAT_ADD
- A pattern in GCC's intermediate representation (PD - Pattern Description) representing a signed saturating addition operation.
- .ADD_OVERFLOW
- A pattern in GCC's intermediate representation that computes an addition and an overflow flag simultaneously. It's often used as a building block for saturation arithmetic.
- vectorizer
- A compiler component that transforms code to use SIMD (Single Instruction, Multiple Data) instructions, allowing a single operation to be performed on multiple data elements simultaneously for performance gains.
- widening_mul
- A compiler pass or optimization stage that handles arithmetic operations involving operands of different sizes, potentially widening them to a larger type before performing the operation.