match-sat-alu.pd: Recognize signed constant-clamp truncation
GCC can now optimize signed saturating truncation that results from clamping a value to the bounds of a narrower signed type.
GCC’s match-sat-alu.pd pattern matcher now recognizes signed saturating truncation that occurs when a value is clamped to the bounds of a narrower signed integer type. This optimization handles both orders of MIN and MAX operations used for clamping, enabling more efficient code generation. An AArch64 example shows a significant reduction from multiple instructions to a single sqxtn instruction.
In Details
The match-sat-alu.pd pattern matcher has been enhanced to recognize a specific form of signed saturating truncation. This form arises when a value is effectively clamped to the minimum and maximum representable values of a narrower signed type. The new patterns cover both MIN(MAX(X, T_MIN), T_MAX) and MAX(MIN(X, T_MAX), T_MIN) structures, targeting cases where the inner and outer clamp operations result in saturation to the target type's bounds. This allows for direct mapping to specialized instructions like SQXTN on architectures like AArch64, reducing instruction counts.
- signed saturating truncation
- A conversion to a narrower signed integer type where the result is clamped to the minimum or maximum representable value of the target type if the original value falls outside that range. This prevents overflow by capping the result.
- 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.