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

Folds (type)(minmax((wide)a, (wide)b)) to minmax(a, b)

This commit adds a new pattern to GCC's `match.pd` to fold widened min/max integer expressions back to their original types, improving optimization before vect…

GCC’s match.pd now includes an optimization that folds minmax expressions where operands are initially widened to a larger integer type and then the result is cast back to the original type. For example, (uint32_t) MAX_EXPR <(uint64_t) a, (uint64_t) b> can now be simplified to MAX_EXPR <a, b>. This folding occurs when the widening preserves signedness, effectively optimizing these expressions before they reach the vectorizer, thereby potentially altering vectorization outcomes.

In Details

This change introduces a new pattern into match.pd that specifically targets minmax expressions of the form (type)(minmax((wide)a, (wide)b)). The optimization folds this to minmax(a,b) when wide is truly wider than type and their signedness matches. During this folding, the wider minmax result is guaranteed to be one of the original operands. This transformation takes place early in the tree-SSA optimization passes, prior to vectorization, which can eliminate the need for the vectorizer to perform narrowing, as observed in pr113281-5.c.

For Context

Inside GCC, there's a phase of optimization where the compiler works with an abstract representation of your code called Tree-SSA. Sometimes, your code might perform an operation like finding the minimum or maximum of two numbers, but it first converts those numbers to a larger size (widens them) and then converts the result back to the original size. For example, finding the maximum of two 32-bit unsigned integers by first converting them to 64-bit, then finding the maximum, then converting back to 32-bit. This commit adds a new rule to the compiler's optimization patterns (match.pd) that recognizes this specific scenario. If the widening doesn't change the behavior (e.g., unsigned to unsigned), the compiler can now simplify this directly to finding the min/max of the original, smaller numbers. This makes the code more efficient and helps prepare it better for subsequent advanced optimizations, like automatically using vector instructions on modern CPUs.

Filed Under: optimizationtree-ssavectorization