The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
1–9 of 9 posts
Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#2Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#3The 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.
Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#4Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#5Why is this known bits optimization being done by the JIT rather than the Java compiler?
But further, optimizing in the compiler can preclude future optimizations by the JIT. Giving the JIT more information can make it make better decisions.
Also, the JIT needs to do that optimization anyways. If a class was compiled with Java 1.0, it might miss some optimizations introduced in the Java 27 compiler. To cover for that, the JVM 27 will need in it's jit the optimizations which were missing from the 1.0 javac if it wants to keep making the code go faster.
That's why the JVM developers have ultimately pushed to make javac pretty dumb and the JIT pretty smart. They can make much better optimizations in the JIT which are retroactive.
Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#6Why is this known bits optimization being done by the JIT rather than the Java compiler?
Because you might want to put a breakpoint on that line of code and debug the variables going in and coming out. But further, optimizing in the compiler can preclude future optimizations by the JIT. Giving the JIT more information can make it make better decisions. Also, the JIT needs to do that optimization anyways. If a class was compiled with Java 1.0, it might miss some optimizations introduced in the Java 27 com…
Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#7The 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.
The particular patterns were added to C2 just recently, being copied from the implementations in LLVM and GCC.
Also it's unlikely the compiler looks for this in particular, but this falls out of a set of optimizations that do matter and it collapses into the output assembly on relevant architectures.
Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#8Why is this known bits optimization being done by the JIT rather than the Java compiler?
So you really want to you should ask why the JIT rather than compiler does optimizations in Java?
Re: The mask that compiles to nothing: how HotSpots JIT learned to reason about bits
#9The 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.
It's because the underlying optimizer follows patterns in LLVM, which is used for a lot of languages, most applicable here is C/C++, and bit things like this are used a lot for performance. The particular patterns were added to C2 just recently, being copied from the implementations in LLVM and GCC. Also it's unlikely the compiler looks for this in particular, but this falls out of a set of optimizations that do matt…