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

Optimize bitwise operations involving popcount.

New patterns fold bitwise OR/AND operations involving popcount for better optimization.

The match.pd file gains new optimization patterns that fold certain bitwise OR and AND operations involving popcount. Specifically, (a != 0) | (popcount(a) == n) is folded into (a != 0) | (n == 0), and (a == 0) & (popcount(a) != n) into (a == 0) & (n != 0). This simplifies expressions where the result is determined by the non-zero status of a and a popcount comparison.

In Details

The pattern-matching directives in match.pd are augmented with new folds for popcount. The patterns (a != 0) | (popcount(a) == n) and (a == 0) & (popcount(a) != n) are recognized and folded. This leverages the properties that if a == 0, then popcount(a) == 0, and if a != 0, the outer OR or AND can simplify the expression based on the non-zero status of a alone. This targets PR tree-optimization/112094.

For Context
popcount
A function that counts the number of set bits (1s) in the binary representation of an integer. It's often implemented as a highly optimized intrinsic or hardware instruction.
match.pd
A file containing pattern-matching directives for GCC's optimization passes. It defines rules for recognizing and transforming specific code patterns in the compiler's intermediate representation.
folding
A compiler optimization technique where constant expressions or known properties of operations are used to simplify code at compile time. This includes evaluating constant arithmetic and simplifying conditional logic based on known values.
Filed Under: optimizationpattern matchingpopcount