https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95771
Bug ID: 95771
Summary: Failure to optimize popcount idiom when argument is
unsigned char
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: gabravier at gmail dot com
Target Milestone: ---
int f(unsigned char x)
{
int i = 0;
while (x)
{
x &= x - 1;
++i;
}
return i;
}
This can be optimized to __builtin_popcount(x). LLVM does this transformation,
but GCC does not.
PS : GCC does this optimization if x is int and a few other types. I've also
seen that GCC does not do this optimization for __int128 (which it could do
with adding a popcount of the low and high parts of x).