RFR 8227494 [lworld][c1] Optimize withfield bytecode into putfield

Ioi Lam ioi.lam at oracle.com
Wed Jul 24 05:30:20 UTC 2019


This optimization significantly reduces the amount of code generated by 
C1 for
withfield bytecodes.

In the <init> methods of value classes, it's quite common to have a 
sequence like
this, where we load a value object from a local slot, and overwrite the 
same local
slot with a modified copy of the value object.

      defaultvalue #1 // class compiler/valhalla/valuetypes/MyValue1
      astore 9
      aload 9
      withfield #7 // Field x:I
      astore 9

If this object was created by defaultvalue, and has not escaped, and is 
not stored
in any other local slots, we can effectively treat the withfield/astore
sequence as a single putfield bytecode.

I implemented a very rudimentary escape analysis in c1_GraphBuilder.cpp. 
It seems like
there's no need to follow back branches like this:

         static FooValue test4() {
             FooValue v = FooValue.default;
             for (int i=1; i<=2; i++) {
                 v = __WithField(v.x, i);
                 v = __WithField(v.y, i);

             return v;

The local slot for "v" in the loop body is a Phi node (not a 
NewValueTypeInstance node),
so it will fail the "obj->is_optimizable_for_withfield()" check (which 
will only return true
from NewValueTypeInstance) in GraphBuilder::withfield().

- Ioi

More information about the valhalla-dev mailing list