https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78103
--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Created attachment 51204 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51204&action=edit gcc12-pr78103.patch Untested partial fix. The patch does 2 things, one is (shown on the first testcase) optimize sign or zero extension of __builtin_clz{,l,ll} from 32-bits to 64-bit - no need to emit sign extension instruction. The other change is to match 32-bit subtraction of a constant and __builtin_clz, can be optimized into bsr and addition. All this only with -mno-lzcnt, with -mlzcnt a very different insn is emitted that handles it fine.