GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
tree-optimization

tree-optimization: Fold abs(x) comparisons with x

GCC can now fold comparisons involving abs(x) and x, simplifying expressions and improving optimization.

This optimization pass folds comparisons involving the absolute value of a variable x and x itself. It handles both undefined-overflow and wrapping-overflow semantics, simplifying such comparisons to constants or sign tests. This transformation is applied even when x is unsigned after conversion. The new patterns reduce code complexity and enable further optimizations by making the relationship between abs(x) and x explicit.

In Details

The match.pd patterns have been extended to recognize and fold comparisons between abs(x) and x, including cases where x is an unsigned conversion of itself or its absolute value. The patterns account for undefined-overflow semantics by simplifying to constants or sign tests (e.g., x < 0). For wrapping-overflow, the INT_MIN case is handled explicitly using unsigned comparisons. These transformations help simplify complex conditional logic derived from absolute value operations.

For Context
abs(x)
The absolute value of x, which is x if x is non-negative, and -x if x is negative.
folding
A compiler optimization technique where constant expressions or comparisons are evaluated at compile time, replacing them with their results.
undefined-overflow
An arithmetic operation where the result exceeds the maximum representable value for the data type, leading to unpredictable behavior in C/C++ for signed integers.
wrapping-overflow
An arithmetic operation where the result exceeds the maximum representable value, and the value wraps around to the minimum representable value, as is standard for unsigned integers.
sign test
A check to determine if a value is positive, negative, or zero.
Filed Under: tree-optimizationabscomparisonoptimization