match-sat-alu.pd: Recognize signed saturation clamps
GCC now optimizes signed saturating additions and subtractions that involve clamping a wider intermediate result.
GCC’s match-sat-alu.pd pattern matcher has been updated to recognize signed saturating addition and subtraction operations where a wider intermediate result is clamped to the bounds of the final result type. This optimization handles both orders of MIN and MAX operations and allows for direct replacement with instructions like SQADD on AArch64, significantly simplifying code generation.
In Details
The match-sat-alu.pd pattern matcher now recognizes signed saturating addition (.SAT_ADD) and subtraction (.SAT_SUB) operations that use a wider intermediate type, which is then clamped to the result type's bounds. The patterns handle both MIN/MAX nesting orders, requiring the intermediate to be strictly wider than the result to ensure arithmetic precision before clamping. The outer clamp must have a single use to facilitate replacement. This optimization is demonstrated with an AArch64 example showing the reduction of a complex sequence involving adds, cmp, and csel to a sing…
- signed saturating addition
- An addition operation where, if the sum exceeds the maximum or minimum representable value for the signed result type, the result is clamped to that maximum or minimum value. This prevents overflow by capping the result.
- signed saturating subtraction
- A subtraction operation where, if the difference exceeds the maximum or minimum representable value for the signed result type, the result is clamped to that maximum or minimum value. This prevents overflow by capping the result.
- widened
- An operation where the intermediate result is stored in a data type that has more bits (higher precision) than the final result type. This is often done to perform calculations without overflow before clamping or converting to the final narrower 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.
- 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.