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

Folds bitwise operations into unsigned range tests.

Optimizes bitwise operations `(X + C1) | (C2 - X) < 0` into a more efficient unsigned range test.

This commit introduces a new optimization in match.pd that transforms a specific bitwise pattern into an unsigned range test. The pattern ((X + C1) | (C2 - X)) < 0 is equivalent to checking if X < -C1 or X > C2. This is further simplified to an unsigned comparison (X + C1) > C1 + C2. The optimization is applied to various spellings of this pattern, including those with constants C1 or C2 equal to zero, and is restricted to GIMPLE intermediate representation to avoid issues with sanitizers or GENERIC code.

In Details

The match.pd file has been updated to recognize and fold the pattern ((X + C1) | (C2 - X)) < 0 into an unsigned range test. The logic relies on the sign bit of the disjunction being the disjunction of the sign bits, leading to X < -C1 || X > C2. This is transformed into (unsigned)(X + C1) > D, where D = C1 + C2. The fold is GIMPLE-only, avoiding signed overflow issues and sanitizer warnings. Matchers for special cases like C1=0, C2=0, and comparisons of converted types are added, along with single_use constraints to prevent redundant comparisons.

For Context
match.pd
A file within GCC that defines patterns for instruction matching and optimization, particularly used for folding simple expressions into more efficient forms.
GIMPLE
GCC's internal three-address intermediate representation. It's a simplified, structured representation of code used for many optimization passes.
unsigned range test
A comparison operation that checks if a value falls within a specified range using unsigned arithmetic. This is often more efficient or necessary for certain bitwise properties.
fold
In compiler optimization, 'folding' refers to replacing a computation with its constant result or a simpler equivalent expression at compile time.
Filed Under: optimizationcompilerrisc-v