GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc Performance Win

Enable POPCNT generation for 32-bit patterns from Hacker's Delight.

This commit enables the generation of the POPCNT instruction for several 32-bit bit counting patterns found in Hacker's Delight.

This commit enables the generation of the POPCNT (population count) instruction for specific 32-bit patterns used to count the number of set bits in a word, as described in Hacker’s Delight. By recognizing these patterns, the compiler can replace sequences of arithmetic and logical operations with a single, efficient POPCNT instruction, leading to significant performance improvements for bit counting operations. New test cases are added to verify the correct generation of the POPCNT instruction for these patterns.

In Details

This commit adds new patterns to match.pd to enable POPCNT instruction generation for bit counting idioms found in Hacker's Delight. These patterns recognize sequences of bitwise operations commonly used to calculate the number of set bits in a 32-bit word. The change impacts code generation for architectures that support the POPCNT instruction. The new tests in gcc.dg/tree-ssa/ verify the correct codegen.

For Context

The POPCNT instruction is a hardware instruction available on many modern processors that efficiently counts the number of bits set to 1 in a binary number. Compilers can often recognize specific sequences of code that perform the same operation and replace them with a single POPCNT instruction. This commit adds new patterns to the compiler that allow it to recognize more of these bit-counting sequences, resulting in faster code for applications that rely on counting bits.

Filed Under: optimizationpopcntbit manipulation