Open niaow opened 3 years ago
Yes, it would be nice if the compiler knew the output range of the bit ops so it could optimize its uses.
The optimizer in me notes that you can do v &= v - 1
instead of v &^= 1 << i
.
cc @zdjones
The compiler knows a bunch of tricks along these lines, so it mightn't be too hard to add this one.
Removing the bounds check falls under #25086
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Tried to remove the trailing bit in a loop. A simple example of this pattern which counts each bit: https://play.golang.org/p/iQc490A5DRV Same thing with instruction dump: https://godbolt.org/z/E54rY4
What did you expect to see?
The compiler produces a tight loop, optimizing away the negative shift check. It would be nice if the bounds check could be eliminated too, but that does not seem to be quite as straightforward to prove.
What did you see instead?
There is a runtime check to see if
bits.TrailingZeros64
is negative.