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

Optimize `minmax (a - c, b) + c` to `minmax (a, b + c)`.

New optimization pattern simplifies `minmax (a - c, b) + c` expressions.

A new optimization pattern minmax (a - c, b) + c -> minmax (a, b + c) has been added to GCC’s pattern matcher. This transformation is applied when the expression b + c is known not to overflow. This specific optimization was observed to occur in std::vector::push_back and helps in simplifying code by trading a subtraction and addition for a single addition. The current implementation is limited to unsigned types due to overflow checking limitations.

In Details

This commit introduces a new match.pd pattern to simplify expressions of the form minmax (a - c, b) + c into minmax (a, b + c). The transformation relies on overflow_free_p to ensure that b + c does not overflow. This is particularly relevant for std::vector::push_back implementations where arithmetic on size types can be simplified. The pattern is currently restricted to unsigned types because overflow_free_p does not accurately handle potential undefined behavior from signed overflows.

For Context
pattern matcher
A component of the compiler that applies predefined transformation rules (patterns) to the intermediate representation of the code to optimize it or generate machine-specific instructions.
minmax
Represents the minimum and maximum of two values. In GCC, MIN_EXPR and MAX_EXPR are used in the intermediate representation.
overflow_free_p
A predicate function used in GCC to determine if an arithmetic operation is guaranteed not to overflow for a given type and operands.
std::vector::push_back
A C++ standard library function that adds an element to the end of a vector. Its implementation often involves capacity checks and resizing, which can lead to arithmetic operations that this optimization targets.
unsigned types
Integer data types that can only represent non-negative values. Operations on unsigned types have well-defined wrap-around behavior on overflow.
Filed Under: optimizationpattern matchingcpp