WEDNESDAY, AUGUST 5, 2026
gcc/tree-optimization Performance Win
Recognize add/sub absolute-value idiom
GCC now optimizes Y + abs(X) and Y - abs(X) expressions to improve code efficiency.
GCC’s tree optimizer now recognizes and simplifies expressions involving addition or subtraction of an absolute value. The transformation is applied to signed integral types by performing the operation in the unsigned domain, preserving wrapping semantics and avoiding signed overflow. This optimization is not applied if signed overflow traps or is sanitized.
In Details
Recognizes the idiom X >=/> 0 ? Y + X : Y - X and its variants in the SSA form. It transforms these into an unsigned addition/subtraction and back, which can simplify control flow and enable further optimizations, provided signed overflow is not a concern. This is handled by match.pd pattern matching.
For Context
- tree-optimization
- A phase in the GCC compiler that operates on an intermediate representation called GENERIC, which is structured as a tree. Optimizations here aim to improve code efficiency before it's lowered to RTL.
- absolute value
- The non-negative value of a number without regard to its sign. For example, the absolute value of -5 is 5, and the absolute value of 5 is 5.
- SSA form
- Static Single Assignment form. An intermediate representation where every variable is assigned a value exactly once, simplifying many optimization analyses.
- signed overflow
- An arithmetic condition that occurs when the result of an operation on signed integers exceeds the maximum representable value or goes below the minimum representable value for that type.