https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95230
Bug ID: 95230 Summary: Failure to optimize bit-scatter pattern to and 1 Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: gabravier at gmail dot com Target Milestone: --- uint32_t scatter(uint32_t val) { uint32_t res = 0; uint32_t off = 0; for (uint32_t I = 0; I < 32; ++I) if (1 & (1 << I)) res |= (val & (1 << off++)) << I; return res; } This can be optimized to `return val & 1;`. LLVM does this transformation, but GCC does not. There is a more generic optimization that looks like it can be done using something else than `1` as the left operand of the `&` in the `if`.