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

Complete branchless min/max folds in match.pd.

GCC's pattern matching now better optimizes branchless min/max operations, reducing instruction count.

This commit enhances GCC’s pattern matching capabilities by completing the folding of branchless minimum and maximum operations. New patterns handle cases like Y + MIN (X - Y, 0) and MIN (X + Y, Y) - Y, which can reduce instruction counts significantly, as seen in the x264 median-of-three predictor example.

In Details

This patch adds new patterns to match.pd for optimizing branchless min/max idioms in GIMPLE. It extends previous work by covering patterns like Y + MIN (X - Y, 0) and MIN (X + Y, Y) - Y. The gimple_match_no_overflow_p function is introduced to handle overflow checks, ensuring correctness for various integral types and the -fwrapv flag. These patterns are crucial for performance-critical code, especially in multimedia codecs.

For Context
match.pd
A file containing pattern definitions used by GCC's GIMPLE (Generic Intermediate Language) pattern matcher to simplify and optimize code.
GIMPLE
GCC's three-address intermediate representation, used internally for performing optimizations before code generation.
fold
An optimization where an expression is replaced by its computed value if the value is known at compile time.
branchless
Code that avoids conditional branches, often using arithmetic or bitwise operations to achieve the same result, potentially improving performance on modern processors.
x86_64
The 64-bit extension of the x86 instruction set architecture, commonly used in desktop and server processors.
median-of-three predictor
An algorithm often used in sorting or data prediction that selects the median value from three given values to improve efficiency.
Filed Under: optimizationgccx86_64