How HotSpot's JIT Learned to Reason About Bits and Delete Code
The mask that compiles to nothing: how HotSpots JIT learned to reason about bits

I explore how HotSpot's C2 compiler evolved beyond simple numeric ranges to track individual bit states. By introducing 'known bits' alongside value ranges, the JIT can now prove that certain bitwise operations, like masking after a shift, are redundant. This refinement allows the compiler to safely eliminate unnecessary instructions, turning complex expressions into efficient, minimal code.
A range can say the value is small, but it can't say the value is even, let alone a multiple of four.
- piinbinary
The part of this that's the most interesting to me is the fact that it is worthwhile for the compiler to expend the effort looking for this optimization opportunity. I would expect (x << 2) & -4 to be a fairly rare pattern, and even then removing the & -4 only saves one or two assembly instructions.
- CalChris
Why is this known bits optimization being done by the JIT rather than the Java compiler?