https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90839
Bug ID: 90839 Summary: Detect lsb ones counting loop (final value replacement?) 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 code: int foo (int a) { int b = 0; while (a >>= 1) b++; return b; } Can calculates 31 - clz (a). A compiler could generate for x86: xorl %eax, %eax bsr %edi, %edx cmovne %edx, %eax ret But GCC generates the loop. I believe we already have popcount-detecting code in final value replacement...