https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120296
Roger Sayle <roger at nextmovesoftware dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |roger at nextmovesoftware dot com Last reconfirmed| |2025-05-19 Status|UNCONFIRMED |NEW Ever confirmed|0 |1 --- Comment #1 from Roger Sayle <roger at nextmovesoftware dot com> --- A step to implementing this would be to add folding optimizations to match.pd: __builtin_popcount(x<<__builtin_clz(x)) => __builtin_popcount(x) __builtin_popcount(x>>__builtin_ctz(x)) => __builtin_popcount(x) when the right shift is unsigned or x is known to be non-negative I don't think it's necessary to also check that x can't be zero (which introduces undefined behavior (UB) in the c[tl]z, and from there UB in the shift) as the result has strictly less UB than the input.