Correct unsigned saturation add pattern for overflow type
Fixes incorrect saturation results for unsigned additions by checking the type of the overflow result.
This commit corrects a bug in the unsigned saturating add (SAT_U_ADD) pattern within match-sat-alu.pd. Previously, when the .ADD_OVERFLOW sub-pattern was used to determine saturation, the type of its overflow flag was not checked against the expected result type. This led to incorrect saturation values, particularly when using vector types or when the overflow result type differed from the addition’s type. The fix ensures that the .ADD_OVERFLOW result’s type matches the addition’s type, preventing incorrect saturation.
In Details
The unsigned_integer_sat_add forms in match-sat-alu.pd, particularly those selecting a separate X + Y and using .ADD_OVERFLOW, did not verify the type of the overflow result. The overflow flag can describe a different type than X + Y, causing incorrect saturation if the compiler selects the wrong type. This commit adds a check to ensure the .ADD_OVERFLOW result parts have the same type as the X + Y operation, resolving issues seen with the vectorizer and general incorrect saturations.
- unsigned saturating add
- An arithmetic operation that adds two unsigned numbers. If the result exceeds the maximum representable value for the type, it is clamped to the maximum; if it falls below the minimum, it is clamped to the minimum. This is also known as saturation arithmetic.
- .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.