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

match.pd: Narrow popcount when operand fits in narrower type

Optimizes `popcount` by narrowing the argument type if its value range allows.

This optimization narrows the argument type for the popcount intrinsic when the value’s range indicates it fits within a smaller unsigned type. The compiler selects a more efficient popcount function (e.g., a 32-bit one for a 64-bit value known to be less than 2^32), potentially allowing targets to use cheaper hardware instructions. This applies to types wider than a machine word and uses built-ins like __builtin_popcount only when the target lacks a native popcount for narrower types.

In Details

The match.pd pattern for POPCOUNT has been enhanced to narrow its argument if its value range is known to fit within a narrower unsigned type. This involves converting the argument to the narrower type and then calling the appropriate popcount function. The function is chosen based on the narrowed type: IFN_POPCOUNT if supported, else __builtin_popcount (32-bit) or __builtin_popcountll (64-bit). The built-in fallbacks are restricted to types wider than a machine word that lack their own popcount operation, avoiding redundant narrowing when libgcc provides a suitable routine. This…

For Context
popcount
An operation that counts the number of set bits (bits with a value of 1) in a binary representation of an integer. Also known as Hamming weight.
intrinsic
A special function provided by the compiler that maps directly to a specific machine instruction or a sequence of instructions, often offering performance benefits over standard C/C++ code.
value range
The set of all possible values an expression or variable can take during program execution, determined through compiler analysis.
machine word
The natural unit of data used by a particular processor design. Typically corresponds to the size of registers, e.g., 32 or 64 bits.
__builtin_popcount
A GCC intrinsic function that counts the number of set bits in an unsigned integer.
Filed Under: optimizationpopcountintrinsictype system