GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
tree-optimization Performance Win

Simplify popcount of bitwise NOT by rewriting it.

Rewrites popcount(~x) to operand's precision minus popcount(x), enabling further optimizations.

This change simplifies the popcount of a bitwise NOT operation by rewriting it to the operand’s precision minus the popcount of the operand. This algebraic transformation removes the bitwise complement and allows for further simplifications, such as folding popcount(~x) + popcount(x) into a constant. New test cases are included to verify this optimization.

In Details

The popcount intrinsic in GCC's tree-SSA optimization pass is now enhanced to recognize and simplify patterns involving bitwise NOT (~). The transformation popcount(~x) is rewritten as precision(x) - popcount(x). This is valuable because it eliminates the bitwise NOT operation, allowing subsequent passes to perform further constant folding or other optimizations that may not directly apply to ~x but would apply to the rewritten form.

Filed Under: optimizationbitwise