Skip to content

Commit b94c3de

Browse files
wenshaocl4es
authored andcommitted
8339401: Optimize ClassFile load and store instructions
Reviewed-by: liach, redestad
1 parent 8ea6adc commit b94c3de

File tree

2 files changed

+178
-72
lines changed

2 files changed

+178
-72
lines changed

src/java.base/share/classes/jdk/internal/classfile/impl/BytecodeHelpers.java

Lines changed: 115 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -56,83 +57,125 @@ public static IllegalArgumentException cannotConvertException(TypeKind from, Typ
5657

5758
public static Opcode loadOpcode(TypeKind tk, int slot) {
5859
return switch (tk) {
59-
case INT, SHORT, BYTE, CHAR, BOOLEAN -> switch (slot) {
60-
case 0 -> Opcode.ILOAD_0;
61-
case 1 -> Opcode.ILOAD_1;
62-
case 2 -> Opcode.ILOAD_2;
63-
case 3 -> Opcode.ILOAD_3;
64-
default -> (slot < 256) ? Opcode.ILOAD : Opcode.ILOAD_W;
65-
};
66-
case LONG -> switch (slot) {
67-
case 0 -> Opcode.LLOAD_0;
68-
case 1 -> Opcode.LLOAD_1;
69-
case 2 -> Opcode.LLOAD_2;
70-
case 3 -> Opcode.LLOAD_3;
71-
default -> (slot < 256) ? Opcode.LLOAD : Opcode.LLOAD_W;
72-
};
73-
case DOUBLE -> switch (slot) {
74-
case 0 -> Opcode.DLOAD_0;
75-
case 1 -> Opcode.DLOAD_1;
76-
case 2 -> Opcode.DLOAD_2;
77-
case 3 -> Opcode.DLOAD_3;
78-
default -> (slot < 256) ? Opcode.DLOAD : Opcode.DLOAD_W;
79-
};
80-
case FLOAT -> switch (slot) {
81-
case 0 -> Opcode.FLOAD_0;
82-
case 1 -> Opcode.FLOAD_1;
83-
case 2 -> Opcode.FLOAD_2;
84-
case 3 -> Opcode.FLOAD_3;
85-
default -> (slot < 256) ? Opcode.FLOAD : Opcode.FLOAD_W;
86-
};
87-
case REFERENCE -> switch (slot) {
88-
case 0 -> Opcode.ALOAD_0;
89-
case 1 -> Opcode.ALOAD_1;
90-
case 2 -> Opcode.ALOAD_2;
91-
case 3 -> Opcode.ALOAD_3;
92-
default -> (slot < 256) ? Opcode.ALOAD : Opcode.ALOAD_W;
93-
};
94-
case VOID -> throw new IllegalArgumentException("void");
60+
case INT, SHORT, BYTE, CHAR, BOOLEAN
61+
-> iload(slot);
62+
case LONG -> lload(slot);
63+
case DOUBLE -> dload(slot);
64+
case FLOAT -> fload(slot);
65+
case REFERENCE -> aload(slot);
66+
case VOID -> throw new IllegalArgumentException("void");
67+
};
68+
}
69+
70+
public static Opcode aload(int slot) {
71+
return switch (slot) {
72+
case 0 -> Opcode.ALOAD_0;
73+
case 1 -> Opcode.ALOAD_1;
74+
case 2 -> Opcode.ALOAD_2;
75+
case 3 -> Opcode.ALOAD_3;
76+
default -> (slot < 256) ? Opcode.ALOAD : Opcode.ALOAD_W;
77+
};
78+
}
79+
80+
public static Opcode fload(int slot) {
81+
return switch (slot) {
82+
case 0 -> Opcode.FLOAD_0;
83+
case 1 -> Opcode.FLOAD_1;
84+
case 2 -> Opcode.FLOAD_2;
85+
case 3 -> Opcode.FLOAD_3;
86+
default -> (slot < 256) ? Opcode.FLOAD : Opcode.FLOAD_W;
87+
};
88+
}
89+
90+
public static Opcode dload(int slot) {
91+
return switch (slot) {
92+
case 0 -> Opcode.DLOAD_0;
93+
case 1 -> Opcode.DLOAD_1;
94+
case 2 -> Opcode.DLOAD_2;
95+
case 3 -> Opcode.DLOAD_3;
96+
default -> (slot < 256) ? Opcode.DLOAD : Opcode.DLOAD_W;
97+
};
98+
}
99+
100+
public static Opcode lload(int slot) {
101+
return switch (slot) {
102+
case 0 -> Opcode.LLOAD_0;
103+
case 1 -> Opcode.LLOAD_1;
104+
case 2 -> Opcode.LLOAD_2;
105+
case 3 -> Opcode.LLOAD_3;
106+
default -> (slot < 256) ? Opcode.LLOAD : Opcode.LLOAD_W;
107+
};
108+
}
109+
110+
public static Opcode iload(int slot) {
111+
return switch (slot) {
112+
case 0 -> Opcode.ILOAD_0;
113+
case 1 -> Opcode.ILOAD_1;
114+
case 2 -> Opcode.ILOAD_2;
115+
case 3 -> Opcode.ILOAD_3;
116+
default -> (slot < 256) ? Opcode.ILOAD : Opcode.ILOAD_W;
95117
};
96118
}
97119

98120
public static Opcode storeOpcode(TypeKind tk, int slot) {
99121
return switch (tk) {
100-
case INT, SHORT, BYTE, CHAR, BOOLEAN -> switch (slot) {
101-
case 0 -> Opcode.ISTORE_0;
102-
case 1 -> Opcode.ISTORE_1;
103-
case 2 -> Opcode.ISTORE_2;
104-
case 3 -> Opcode.ISTORE_3;
105-
default -> (slot < 256) ? Opcode.ISTORE : Opcode.ISTORE_W;
106-
};
107-
case LONG -> switch (slot) {
108-
case 0 -> Opcode.LSTORE_0;
109-
case 1 -> Opcode.LSTORE_1;
110-
case 2 -> Opcode.LSTORE_2;
111-
case 3 -> Opcode.LSTORE_3;
112-
default -> (slot < 256) ? Opcode.LSTORE : Opcode.LSTORE_W;
113-
};
114-
case DOUBLE -> switch (slot) {
115-
case 0 -> Opcode.DSTORE_0;
116-
case 1 -> Opcode.DSTORE_1;
117-
case 2 -> Opcode.DSTORE_2;
118-
case 3 -> Opcode.DSTORE_3;
119-
default -> (slot < 256) ? Opcode.DSTORE : Opcode.DSTORE_W;
120-
};
121-
case FLOAT -> switch (slot) {
122-
case 0 -> Opcode.FSTORE_0;
123-
case 1 -> Opcode.FSTORE_1;
124-
case 2 -> Opcode.FSTORE_2;
125-
case 3 -> Opcode.FSTORE_3;
126-
default -> (slot < 256) ? Opcode.FSTORE : Opcode.FSTORE_W;
127-
};
128-
case REFERENCE -> switch (slot) {
129-
case 0 -> Opcode.ASTORE_0;
130-
case 1 -> Opcode.ASTORE_1;
131-
case 2 -> Opcode.ASTORE_2;
132-
case 3 -> Opcode.ASTORE_3;
133-
default -> (slot < 256) ? Opcode.ASTORE : Opcode.ASTORE_W;
134-
};
135-
case VOID -> throw new IllegalArgumentException("void");
122+
case INT, SHORT, BYTE, CHAR, BOOLEAN
123+
-> istore(slot);
124+
case LONG -> lstore(slot);
125+
case DOUBLE -> dstore(slot);
126+
case FLOAT -> fstore(slot);
127+
case REFERENCE -> astore(slot);
128+
case VOID -> throw new IllegalArgumentException("void");
129+
};
130+
}
131+
132+
public static Opcode astore(int slot) {
133+
return switch (slot) {
134+
case 0 -> Opcode.ASTORE_0;
135+
case 1 -> Opcode.ASTORE_1;
136+
case 2 -> Opcode.ASTORE_2;
137+
case 3 -> Opcode.ASTORE_3;
138+
default -> (slot < 256) ? Opcode.ASTORE : Opcode.ASTORE_W;
139+
};
140+
}
141+
142+
public static Opcode fstore(int slot) {
143+
return switch (slot) {
144+
case 0 -> Opcode.FSTORE_0;
145+
case 1 -> Opcode.FSTORE_1;
146+
case 2 -> Opcode.FSTORE_2;
147+
case 3 -> Opcode.FSTORE_3;
148+
default -> (slot < 256) ? Opcode.FSTORE : Opcode.FSTORE_W;
149+
};
150+
}
151+
152+
public static Opcode dstore(int slot) {
153+
return switch (slot) {
154+
case 0 -> Opcode.DSTORE_0;
155+
case 1 -> Opcode.DSTORE_1;
156+
case 2 -> Opcode.DSTORE_2;
157+
case 3 -> Opcode.DSTORE_3;
158+
default -> (slot < 256) ? Opcode.DSTORE : Opcode.DSTORE_W;
159+
};
160+
}
161+
162+
public static Opcode lstore(int slot) {
163+
return switch (slot) {
164+
case 0 -> Opcode.LSTORE_0;
165+
case 1 -> Opcode.LSTORE_1;
166+
case 2 -> Opcode.LSTORE_2;
167+
case 3 -> Opcode.LSTORE_3;
168+
default -> (slot < 256) ? Opcode.LSTORE : Opcode.LSTORE_W;
169+
};
170+
}
171+
172+
public static Opcode istore(int slot) {
173+
return switch (slot) {
174+
case 0 -> Opcode.ISTORE_0;
175+
case 1 -> Opcode.ISTORE_1;
176+
case 2 -> Opcode.ISTORE_2;
177+
case 3 -> Opcode.ISTORE_3;
178+
default -> (slot < 256) ? Opcode.ISTORE : Opcode.ISTORE_W;
136179
};
137180
}
138181

src/java.base/share/classes/jdk/internal/classfile/impl/DirectCodeBuilder.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -63,6 +64,8 @@
6364

6465
import static java.lang.classfile.Opcode.*;
6566

67+
import static jdk.internal.classfile.impl.BytecodeHelpers.*;
68+
6669
public final class DirectCodeBuilder
6770
extends AbstractDirectBuilder<CodeModel>
6871
implements TerminalCodeBuilder {
@@ -855,6 +858,12 @@ public CodeBuilder aconst_null() {
855858
return this;
856859
}
857860

861+
@Override
862+
public CodeBuilder aload(int slot) {
863+
writeLocalVar(BytecodeHelpers.aload(slot), slot);
864+
return this;
865+
}
866+
858867
@Override
859868
public CodeBuilder anewarray(ClassEntry entry) {
860869
writeNewReferenceArray(entry);
@@ -867,6 +876,12 @@ public CodeBuilder arraylength() {
867876
return this;
868877
}
869878

879+
@Override
880+
public CodeBuilder astore(int slot) {
881+
writeLocalVar(BytecodeHelpers.astore(slot), slot);
882+
return this;
883+
}
884+
870885
@Override
871886
public CodeBuilder athrow() {
872887
writeBytecode(ATHROW);
@@ -940,6 +955,12 @@ public CodeBuilder ddiv() {
940955
return this;
941956
}
942957

958+
@Override
959+
public CodeBuilder dload(int slot) {
960+
writeLocalVar(BytecodeHelpers.dload(slot), slot);
961+
return this;
962+
}
963+
943964
@Override
944965
public CodeBuilder dmul() {
945966
writeBytecode(DMUL);
@@ -958,6 +979,12 @@ public CodeBuilder drem() {
958979
return this;
959980
}
960981

982+
@Override
983+
public CodeBuilder dstore(int slot) {
984+
writeLocalVar(BytecodeHelpers.dstore(slot), slot);
985+
return this;
986+
}
987+
961988
@Override
962989
public CodeBuilder dsub() {
963990
writeBytecode(DSUB);
@@ -1060,6 +1087,12 @@ public CodeBuilder fdiv() {
10601087
return this;
10611088
}
10621089

1090+
@Override
1091+
public CodeBuilder fload(int slot) {
1092+
writeLocalVar(BytecodeHelpers.fload(slot), slot);
1093+
return this;
1094+
}
1095+
10631096
@Override
10641097
public CodeBuilder fmul() {
10651098
writeBytecode(FMUL);
@@ -1078,6 +1111,12 @@ public CodeBuilder frem() {
10781111
return this;
10791112
}
10801113

1114+
@Override
1115+
public CodeBuilder fstore(int slot) {
1116+
writeLocalVar(BytecodeHelpers.fstore(slot), slot);
1117+
return this;
1118+
}
1119+
10811120
@Override
10821121
public CodeBuilder fsub() {
10831122
writeBytecode(FSUB);
@@ -1186,6 +1225,12 @@ public CodeBuilder iinc(int slot, int val) {
11861225
return this;
11871226
}
11881227

1228+
@Override
1229+
public CodeBuilder iload(int slot) {
1230+
writeLocalVar(BytecodeHelpers.iload(slot), slot);
1231+
return this;
1232+
}
1233+
11891234
@Override
11901235
public CodeBuilder imul() {
11911236
writeBytecode(IMUL);
@@ -1270,6 +1315,12 @@ public CodeBuilder ishr() {
12701315
return this;
12711316
}
12721317

1318+
@Override
1319+
public CodeBuilder istore(int slot) {
1320+
writeLocalVar(BytecodeHelpers.istore(slot), slot);
1321+
return this;
1322+
}
1323+
12731324
@Override
12741325
public CodeBuilder isub() {
12751326
writeBytecode(ISUB);
@@ -1354,6 +1405,12 @@ public CodeBuilder ldiv() {
13541405
return this;
13551406
}
13561407

1408+
@Override
1409+
public CodeBuilder lload(int slot) {
1410+
writeLocalVar(BytecodeHelpers.lload(slot), slot);
1411+
return this;
1412+
}
1413+
13571414
@Override
13581415
public CodeBuilder lmul() {
13591416
writeBytecode(LMUL);
@@ -1390,6 +1447,12 @@ public CodeBuilder lshr() {
13901447
return this;
13911448
}
13921449

1450+
@Override
1451+
public CodeBuilder lstore(int slot) {
1452+
writeLocalVar(BytecodeHelpers.lstore(slot), slot);
1453+
return this;
1454+
}
1455+
13931456
@Override
13941457
public CodeBuilder lsub() {
13951458
writeBytecode(LSUB);

0 commit comments

Comments
 (0)