https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90836
Bug ID: 90836 Summary: Missing popcount pattern matching Product: gcc Version: 9.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: ktkachov at gcc dot gnu.org Target Milestone: --- The following source: int foo (unsigned long long a) { unsigned long long b = a; b -= ((b>>1) & 0x5555555555555555ULL); b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL; b *= 0x0101010101010101ULL; return (int)(b >> 56); } implements a popcount operation. Some compilers can emit for x86: xorq %rax, %rax popcnt %rdi, %rax ret but GCC doesn't detect this and emits the sequence of masks and shifts. This code is important for some workloads, 531.deepsjeng_r from SPEC, for example. Can this be matched in a match.pd pattern?