GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
match Performance Win

Optimize ``A > B ? ABS(A) : B`` to ``MAX(A, B)`` When B >= 0

GCC now optimizes ``A > B ? ABS(A) : B`` to ``MAX(A, B)`` when it knows ``B`` is non-negative, enabling more efficient code generation.

GCC’s pattern matching infrastructure now optimizes expressions of the form A > B ? ABS(A) : B into MAX(A, B) when it knows B is non-negative. This transformation is valid because if B is non-negative and A > B, then A must also be positive, making ABS(A) equivalent to A. This optimization is performed at -O1, where previously it was only done at -O2.

In Details

This commit adds a new pattern to GCC's match.pd file. This pattern transforms A > B ? ABS(A) : B to MAX(A, B) when B >= 0. This optimization is caught at -O2 via VRP, but at -O1 phiopt1 produces ABS_EXPR and no later pass simplifies it. The motivating testcase is PR tree-optimization/116700.

For Context

Compilers use various optimization techniques to simplify code and improve performance. One such technique is pattern matching, where the compiler looks for specific code patterns and replaces them with more efficient equivalents. This commit adds a new pattern to GCC that optimizes a conditional expression involving absolute values and maximums.

Filed Under: optimization