AArch64 recognizes unsigned saturating addition pattern.
Optimizes unsigned saturating addition on AArch64, reducing instruction count.
This commit enables GCC to recognize a specific pattern for unsigned saturating addition on AArch64: X + MIN(~X, Y). This pattern is mathematically equivalent to X + Y with saturation. The optimization transforms a sequence of four instructions into two, improving code density and performance. A test case ensures the pattern is correctly applied only when the MIN operation has a single use.
In Details
The match-sat-alu.pd file defines patterns for recognizing and optimizing saturating arithmetic. This change adds a rule for unsigned saturating addition that matches the expression X + MIN(~X, Y). For unsigned integers, ~X is equivalent to MAX_UINT - X. Therefore, MIN(~X, Y) effectively selects Y if Y is small enough not to cause overflow when added to X. The pattern requires the result of MIN to have a single use to ensure its elimination. This optimization is particularly effective on AArch64.
- saturating addition
- An arithmetic operation where the result is capped at the maximum representable value if the true sum exceeds it, or at the minimum representable value if the true sum falls below it.
- unsigned integer
- An integer data type that can only represent non-negative values.
- AArch64
- The 64-bit ARM architecture, commonly used in mobile devices and servers.
- match-sat-alu.pd
- A GCC machine description file that defines patterns for recognizing and optimizing saturating arithmetic operations on various ALUs.
- MIN
- The minimum function, which returns the smaller of two values.
- ~X
- Bitwise NOT operator. For unsigned integers, it is equivalent to MAX_VALUE - X.