loop invariant code motion and the jit

Stuart Monteith stuart.monteith at linaro.org
Thu Jan 19 14:21:56 UTC 2017


Andrew - Do you mean a java.lang.ref.Reference or something like an
java.util.concurrent.atomic.AtomicBoolean or AtomicReference?

Peter - it has sometimes been the case that "stop" will work in your
example with "-client" but not "-server" because of the better
optimisation in C2.


On 19 January 2017 at 09:08, Andrew Haley <aph at redhat.com> wrote:
> On 18/01/17 20:04, Peter Veentjer wrote:
>> private boolean stop;
>> private long value;
>>
>> @Override
>> public void run() {
>>     while (!stop) {
>>         value++;
>>     }
>> }
>>
>> In theory the read of stop can be pulled out of the loop since stop isn't
>> properly synchronised (volatile would do the trick).
>
> In practice, too.
>
>> Is there any prevention of this optimisation being applied to protect users
>> against improper synchronised code?
>
> No.  If you want this to work properly, use a Reference or (in JDK9) a
> VarHandle or a Fence.
>
> Andrew.
>


More information about the hotspot-compiler-dev mailing list