match-sat-alu.pd: Recognize unsigned constant-clamp truncation
GCC can now optimize unsigned saturating truncation resulting from clamping a signed value to the bounds of a narrower unsigned type.
GCC’s match-sat-alu.pd pattern matcher has been extended to recognize unsigned saturating truncation when a signed value is clamped to the range of a narrower unsigned type. This optimization handles nested MIN and MAX operations in either order and can replace complex sequences with a single uqxtn instruction on architectures like AArch64, as shown in an example.
In Details
The match-sat-alu.pd pattern matcher now identifies unsigned saturating truncation that arises from clamping a signed value to the range of a narrower unsigned type. This new pattern, integer_constant_clamp, handles nested MIN and MAX operations irrespective of their order. It specifically targets cases where the clamping results in a value within the unsigned type's range, allowing replacement with efficient instructions like UQXTN on AArch64. The match requires an exact zero lower bound and the maximum upper bound for the unsigned type, with a single use of the outer clamp.
- unsigned saturating truncation
- A conversion to a narrower unsigned integer type where the result is clamped to the maximum value of the target type if the original value exceeds it. This prevents overflow by capping the result at the highest representable value for the unsigned type.
- clamping
- The process of restricting a value to a specified range. In this context, it means ensuring a value does not exceed the minimum or maximum representable values for a given data type.
- MIN/MAX operations
- Functions that return the smaller (MIN) or larger (MAX) of two input values. They are often used in sequence to implement clamping.
- pattern matcher
- A component within a compiler that identifies specific sequences of intermediate representation (IR) operations that can be replaced by more efficient instructions or simpler IR forms. This is crucial for optimization.