GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
match.pd Performance Win

match.pd: Canonicalize right shift comparisons.

Canonicalizes right shift comparisons with constants into left shifts.

The match.pd pattern matching system now canonicalizes right shift non-equality comparisons involving constants. Expressions like (A >> CST1) CMP CST2 are transformed into equivalent comparisons involving left-shifted constants (A CMP (CST2 << CST1)), potentially simplifying the comparison to a single operation. This optimization aims to improve pattern matching and subsequent code generation by reducing complex shift-and-compare sequences.

In Details

Introduces new patterns in match.pd to canonicalize right-shift and compare operations. Specifically, (A >> CST1) CMP CST2 is transformed into A CMP (CST2 << CST1) for inequality, and a masked left shift for inequalities. This aims to normalize these expressions for the backend, simplifying later instruction selection.

For Context
match.pd
A file within GCC that defines patterns for matching and transforming intermediate representation (IR) expressions, primarily used for optimization and code generation.
canonicalize
To transform an expression or data structure into a standard or preferred form, often to simplify comparisons or facilitate further processing.
right shift
A bitwise operation that shifts the bits of a number to the right. In C/C++, it can be arithmetic (preserving sign bit) or logical (shifting in zeros).
left shift
A bitwise operation that shifts the bits of a number to the left, effectively multiplying by powers of two.
intermediate representation (IR)
A representation of code used internally by a compiler. GCC uses several IRs, such as GENERIC, GIMPLE, and RTL, each suited for different stages of compilation.
Filed Under: optimizationpattern matchingRISC-Vx86aarch64