https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113764
Bug ID: 113764 Summary: [X86] Generates lzcnt when bsr is sufficient Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: chfast at gmail dot com Target Milestone: --- When lzcnt instructions is enabled (-mlzcnt) the compiler generates lzcnt for __builtin_clz() in the context where the bsr instruction is sufficient and better. unsigned bsr(unsigned x) { return __builtin_clz(x) ^ 31; } bsr: xor eax, eax lzcnt eax, edi xor eax, 31 ret Without -mlzcnt the generated code is optimal. bsr: bsr eax, edi ret https://godbolt.org/z/5qcTq18nr