https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104378
Bug ID: 104378 Summary: (N - x) ^ N should be optimized to x if x <= N (unsigned) and N is a pow2 - 1 Product: gcc Version: 12.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: pinskia at gcc dot gnu.org Target Milestone: --- Take: #define n 8 #define N ((1u<<n)-1) unsigned countLeadingZeros32(unsigned x) { if (x > N) __builtin_unreachable(); return (N - x) ^ N; } This should be optimized to just return x; Like it is done by LLVM.