43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.java;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.bytecode.Bytecodes.AALOAD;
|
|
28 |
import static org.graalvm.compiler.bytecode.Bytecodes.AASTORE;
|
58533
|
29 |
import static org.graalvm.compiler.bytecode.Bytecodes.ACONST_NULL;
|
|
30 |
import static org.graalvm.compiler.bytecode.Bytecodes.ALOAD;
|
|
31 |
import static org.graalvm.compiler.bytecode.Bytecodes.ALOAD_0;
|
|
32 |
import static org.graalvm.compiler.bytecode.Bytecodes.ALOAD_1;
|
|
33 |
import static org.graalvm.compiler.bytecode.Bytecodes.ALOAD_2;
|
|
34 |
import static org.graalvm.compiler.bytecode.Bytecodes.ALOAD_3;
|
51736
|
35 |
import static org.graalvm.compiler.bytecode.Bytecodes.ANEWARRAY;
|
43972
|
36 |
import static org.graalvm.compiler.bytecode.Bytecodes.ARETURN;
|
|
37 |
import static org.graalvm.compiler.bytecode.Bytecodes.ARRAYLENGTH;
|
58533
|
38 |
import static org.graalvm.compiler.bytecode.Bytecodes.ASTORE;
|
|
39 |
import static org.graalvm.compiler.bytecode.Bytecodes.ASTORE_0;
|
|
40 |
import static org.graalvm.compiler.bytecode.Bytecodes.ASTORE_1;
|
|
41 |
import static org.graalvm.compiler.bytecode.Bytecodes.ASTORE_2;
|
|
42 |
import static org.graalvm.compiler.bytecode.Bytecodes.ASTORE_3;
|
43972
|
43 |
import static org.graalvm.compiler.bytecode.Bytecodes.ATHROW;
|
|
44 |
import static org.graalvm.compiler.bytecode.Bytecodes.BALOAD;
|
|
45 |
import static org.graalvm.compiler.bytecode.Bytecodes.BASTORE;
|
58533
|
46 |
import static org.graalvm.compiler.bytecode.Bytecodes.BIPUSH;
|
|
47 |
import static org.graalvm.compiler.bytecode.Bytecodes.BREAKPOINT;
|
43972
|
48 |
import static org.graalvm.compiler.bytecode.Bytecodes.CALOAD;
|
|
49 |
import static org.graalvm.compiler.bytecode.Bytecodes.CASTORE;
|
50330
|
50 |
import static org.graalvm.compiler.bytecode.Bytecodes.CHECKCAST;
|
58533
|
51 |
import static org.graalvm.compiler.bytecode.Bytecodes.D2F;
|
|
52 |
import static org.graalvm.compiler.bytecode.Bytecodes.D2I;
|
|
53 |
import static org.graalvm.compiler.bytecode.Bytecodes.D2L;
|
|
54 |
import static org.graalvm.compiler.bytecode.Bytecodes.DADD;
|
43972
|
55 |
import static org.graalvm.compiler.bytecode.Bytecodes.DALOAD;
|
|
56 |
import static org.graalvm.compiler.bytecode.Bytecodes.DASTORE;
|
58533
|
57 |
import static org.graalvm.compiler.bytecode.Bytecodes.DCMPG;
|
|
58 |
import static org.graalvm.compiler.bytecode.Bytecodes.DCMPL;
|
|
59 |
import static org.graalvm.compiler.bytecode.Bytecodes.DCONST_0;
|
|
60 |
import static org.graalvm.compiler.bytecode.Bytecodes.DCONST_1;
|
|
61 |
import static org.graalvm.compiler.bytecode.Bytecodes.DDIV;
|
|
62 |
import static org.graalvm.compiler.bytecode.Bytecodes.DLOAD;
|
|
63 |
import static org.graalvm.compiler.bytecode.Bytecodes.DLOAD_0;
|
|
64 |
import static org.graalvm.compiler.bytecode.Bytecodes.DLOAD_1;
|
|
65 |
import static org.graalvm.compiler.bytecode.Bytecodes.DLOAD_2;
|
|
66 |
import static org.graalvm.compiler.bytecode.Bytecodes.DLOAD_3;
|
|
67 |
import static org.graalvm.compiler.bytecode.Bytecodes.DMUL;
|
|
68 |
import static org.graalvm.compiler.bytecode.Bytecodes.DNEG;
|
|
69 |
import static org.graalvm.compiler.bytecode.Bytecodes.DREM;
|
43972
|
70 |
import static org.graalvm.compiler.bytecode.Bytecodes.DRETURN;
|
58533
|
71 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSTORE;
|
|
72 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSTORE_0;
|
|
73 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSTORE_1;
|
|
74 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSTORE_2;
|
|
75 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSTORE_3;
|
|
76 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSUB;
|
|
77 |
import static org.graalvm.compiler.bytecode.Bytecodes.DUP;
|
|
78 |
import static org.graalvm.compiler.bytecode.Bytecodes.DUP2;
|
|
79 |
import static org.graalvm.compiler.bytecode.Bytecodes.DUP2_X1;
|
|
80 |
import static org.graalvm.compiler.bytecode.Bytecodes.DUP2_X2;
|
|
81 |
import static org.graalvm.compiler.bytecode.Bytecodes.DUP_X1;
|
|
82 |
import static org.graalvm.compiler.bytecode.Bytecodes.DUP_X2;
|
|
83 |
import static org.graalvm.compiler.bytecode.Bytecodes.F2D;
|
|
84 |
import static org.graalvm.compiler.bytecode.Bytecodes.F2I;
|
|
85 |
import static org.graalvm.compiler.bytecode.Bytecodes.F2L;
|
|
86 |
import static org.graalvm.compiler.bytecode.Bytecodes.FADD;
|
43972
|
87 |
import static org.graalvm.compiler.bytecode.Bytecodes.FALOAD;
|
|
88 |
import static org.graalvm.compiler.bytecode.Bytecodes.FASTORE;
|
58533
|
89 |
import static org.graalvm.compiler.bytecode.Bytecodes.FCMPG;
|
|
90 |
import static org.graalvm.compiler.bytecode.Bytecodes.FCMPL;
|
|
91 |
import static org.graalvm.compiler.bytecode.Bytecodes.FCONST_0;
|
|
92 |
import static org.graalvm.compiler.bytecode.Bytecodes.FCONST_1;
|
|
93 |
import static org.graalvm.compiler.bytecode.Bytecodes.FCONST_2;
|
|
94 |
import static org.graalvm.compiler.bytecode.Bytecodes.FDIV;
|
|
95 |
import static org.graalvm.compiler.bytecode.Bytecodes.FLOAD;
|
|
96 |
import static org.graalvm.compiler.bytecode.Bytecodes.FLOAD_0;
|
|
97 |
import static org.graalvm.compiler.bytecode.Bytecodes.FLOAD_1;
|
|
98 |
import static org.graalvm.compiler.bytecode.Bytecodes.FLOAD_2;
|
|
99 |
import static org.graalvm.compiler.bytecode.Bytecodes.FLOAD_3;
|
|
100 |
import static org.graalvm.compiler.bytecode.Bytecodes.FMUL;
|
|
101 |
import static org.graalvm.compiler.bytecode.Bytecodes.FNEG;
|
|
102 |
import static org.graalvm.compiler.bytecode.Bytecodes.FREM;
|
43972
|
103 |
import static org.graalvm.compiler.bytecode.Bytecodes.FRETURN;
|
58533
|
104 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSTORE;
|
|
105 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSTORE_0;
|
|
106 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSTORE_1;
|
|
107 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSTORE_2;
|
|
108 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSTORE_3;
|
|
109 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSUB;
|
43972
|
110 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETFIELD;
|
49873
|
111 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETSTATIC;
|
43972
|
112 |
import static org.graalvm.compiler.bytecode.Bytecodes.GOTO;
|
|
113 |
import static org.graalvm.compiler.bytecode.Bytecodes.GOTO_W;
|
58533
|
114 |
import static org.graalvm.compiler.bytecode.Bytecodes.I2B;
|
|
115 |
import static org.graalvm.compiler.bytecode.Bytecodes.I2C;
|
|
116 |
import static org.graalvm.compiler.bytecode.Bytecodes.I2D;
|
|
117 |
import static org.graalvm.compiler.bytecode.Bytecodes.I2F;
|
|
118 |
import static org.graalvm.compiler.bytecode.Bytecodes.I2L;
|
|
119 |
import static org.graalvm.compiler.bytecode.Bytecodes.I2S;
|
|
120 |
import static org.graalvm.compiler.bytecode.Bytecodes.IADD;
|
43972
|
121 |
import static org.graalvm.compiler.bytecode.Bytecodes.IALOAD;
|
58533
|
122 |
import static org.graalvm.compiler.bytecode.Bytecodes.IAND;
|
43972
|
123 |
import static org.graalvm.compiler.bytecode.Bytecodes.IASTORE;
|
58533
|
124 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_0;
|
|
125 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_1;
|
|
126 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_2;
|
|
127 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_3;
|
|
128 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_4;
|
|
129 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_5;
|
|
130 |
import static org.graalvm.compiler.bytecode.Bytecodes.ICONST_M1;
|
50330
|
131 |
import static org.graalvm.compiler.bytecode.Bytecodes.IDIV;
|
43972
|
132 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFEQ;
|
|
133 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFGE;
|
|
134 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFGT;
|
|
135 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFLE;
|
|
136 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFLT;
|
|
137 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNE;
|
|
138 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNONNULL;
|
|
139 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNULL;
|
|
140 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ACMPEQ;
|
|
141 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ACMPNE;
|
|
142 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPEQ;
|
|
143 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPGE;
|
|
144 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPGT;
|
|
145 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPLE;
|
|
146 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPLT;
|
|
147 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPNE;
|
58533
|
148 |
import static org.graalvm.compiler.bytecode.Bytecodes.IINC;
|
|
149 |
import static org.graalvm.compiler.bytecode.Bytecodes.ILOAD;
|
|
150 |
import static org.graalvm.compiler.bytecode.Bytecodes.ILOAD_0;
|
|
151 |
import static org.graalvm.compiler.bytecode.Bytecodes.ILOAD_1;
|
|
152 |
import static org.graalvm.compiler.bytecode.Bytecodes.ILOAD_2;
|
|
153 |
import static org.graalvm.compiler.bytecode.Bytecodes.ILOAD_3;
|
|
154 |
import static org.graalvm.compiler.bytecode.Bytecodes.IMUL;
|
|
155 |
import static org.graalvm.compiler.bytecode.Bytecodes.INEG;
|
|
156 |
import static org.graalvm.compiler.bytecode.Bytecodes.INSTANCEOF;
|
43972
|
157 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEDYNAMIC;
|
|
158 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEINTERFACE;
|
|
159 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESPECIAL;
|
|
160 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESTATIC;
|
|
161 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEVIRTUAL;
|
58533
|
162 |
import static org.graalvm.compiler.bytecode.Bytecodes.IOR;
|
50330
|
163 |
import static org.graalvm.compiler.bytecode.Bytecodes.IREM;
|
43972
|
164 |
import static org.graalvm.compiler.bytecode.Bytecodes.IRETURN;
|
58533
|
165 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISHL;
|
|
166 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISHR;
|
|
167 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISTORE;
|
|
168 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISTORE_0;
|
|
169 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISTORE_1;
|
|
170 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISTORE_2;
|
|
171 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISTORE_3;
|
|
172 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISUB;
|
|
173 |
import static org.graalvm.compiler.bytecode.Bytecodes.IUSHR;
|
|
174 |
import static org.graalvm.compiler.bytecode.Bytecodes.IXOR;
|
43972
|
175 |
import static org.graalvm.compiler.bytecode.Bytecodes.JSR;
|
|
176 |
import static org.graalvm.compiler.bytecode.Bytecodes.JSR_W;
|
58533
|
177 |
import static org.graalvm.compiler.bytecode.Bytecodes.L2D;
|
|
178 |
import static org.graalvm.compiler.bytecode.Bytecodes.L2F;
|
|
179 |
import static org.graalvm.compiler.bytecode.Bytecodes.L2I;
|
|
180 |
import static org.graalvm.compiler.bytecode.Bytecodes.LADD;
|
43972
|
181 |
import static org.graalvm.compiler.bytecode.Bytecodes.LALOAD;
|
58533
|
182 |
import static org.graalvm.compiler.bytecode.Bytecodes.LAND;
|
43972
|
183 |
import static org.graalvm.compiler.bytecode.Bytecodes.LASTORE;
|
58533
|
184 |
import static org.graalvm.compiler.bytecode.Bytecodes.LCMP;
|
|
185 |
import static org.graalvm.compiler.bytecode.Bytecodes.LCONST_0;
|
|
186 |
import static org.graalvm.compiler.bytecode.Bytecodes.LCONST_1;
|
52910
|
187 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDC;
|
|
188 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDC2_W;
|
|
189 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDC_W;
|
50330
|
190 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDIV;
|
58533
|
191 |
import static org.graalvm.compiler.bytecode.Bytecodes.LLOAD;
|
|
192 |
import static org.graalvm.compiler.bytecode.Bytecodes.LLOAD_0;
|
|
193 |
import static org.graalvm.compiler.bytecode.Bytecodes.LLOAD_1;
|
|
194 |
import static org.graalvm.compiler.bytecode.Bytecodes.LLOAD_2;
|
|
195 |
import static org.graalvm.compiler.bytecode.Bytecodes.LLOAD_3;
|
|
196 |
import static org.graalvm.compiler.bytecode.Bytecodes.LMUL;
|
|
197 |
import static org.graalvm.compiler.bytecode.Bytecodes.LNEG;
|
43972
|
198 |
import static org.graalvm.compiler.bytecode.Bytecodes.LOOKUPSWITCH;
|
58533
|
199 |
import static org.graalvm.compiler.bytecode.Bytecodes.LOR;
|
50330
|
200 |
import static org.graalvm.compiler.bytecode.Bytecodes.LREM;
|
43972
|
201 |
import static org.graalvm.compiler.bytecode.Bytecodes.LRETURN;
|
58533
|
202 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSHL;
|
|
203 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSHR;
|
|
204 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSTORE;
|
|
205 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSTORE_0;
|
|
206 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSTORE_1;
|
|
207 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSTORE_2;
|
|
208 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSTORE_3;
|
|
209 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSUB;
|
|
210 |
import static org.graalvm.compiler.bytecode.Bytecodes.LUSHR;
|
|
211 |
import static org.graalvm.compiler.bytecode.Bytecodes.LXOR;
|
|
212 |
import static org.graalvm.compiler.bytecode.Bytecodes.MONITORENTER;
|
|
213 |
import static org.graalvm.compiler.bytecode.Bytecodes.MONITOREXIT;
|
51736
|
214 |
import static org.graalvm.compiler.bytecode.Bytecodes.MULTIANEWARRAY;
|
|
215 |
import static org.graalvm.compiler.bytecode.Bytecodes.NEW;
|
58533
|
216 |
import static org.graalvm.compiler.bytecode.Bytecodes.NEWARRAY;
|
|
217 |
import static org.graalvm.compiler.bytecode.Bytecodes.NOP;
|
|
218 |
import static org.graalvm.compiler.bytecode.Bytecodes.POP;
|
|
219 |
import static org.graalvm.compiler.bytecode.Bytecodes.POP2;
|
43972
|
220 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTFIELD;
|
49873
|
221 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTSTATIC;
|
43972
|
222 |
import static org.graalvm.compiler.bytecode.Bytecodes.RET;
|
|
223 |
import static org.graalvm.compiler.bytecode.Bytecodes.RETURN;
|
|
224 |
import static org.graalvm.compiler.bytecode.Bytecodes.SALOAD;
|
|
225 |
import static org.graalvm.compiler.bytecode.Bytecodes.SASTORE;
|
58533
|
226 |
import static org.graalvm.compiler.bytecode.Bytecodes.SIPUSH;
|
|
227 |
import static org.graalvm.compiler.bytecode.Bytecodes.SWAP;
|
43972
|
228 |
import static org.graalvm.compiler.bytecode.Bytecodes.TABLESWITCH;
|
58533
|
229 |
import static org.graalvm.compiler.bytecode.Bytecodes.WIDE;
|
43972
|
230 |
import static org.graalvm.compiler.core.common.GraalOptions.SupportJsrBytecodes;
|
|
231 |
|
54724
|
232 |
import java.util.ArrayDeque;
|
43972
|
233 |
import java.util.ArrayList;
|
|
234 |
import java.util.Arrays;
|
|
235 |
import java.util.Collection;
|
|
236 |
import java.util.Iterator;
|
|
237 |
import java.util.List;
|
|
238 |
import java.util.TreeSet;
|
|
239 |
|
49873
|
240 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
|
241 |
import jdk.internal.vm.compiler.collections.Equivalence;
|
43972
|
242 |
import org.graalvm.compiler.bytecode.Bytecode;
|
|
243 |
import org.graalvm.compiler.bytecode.BytecodeLookupSwitch;
|
|
244 |
import org.graalvm.compiler.bytecode.BytecodeStream;
|
|
245 |
import org.graalvm.compiler.bytecode.BytecodeSwitch;
|
|
246 |
import org.graalvm.compiler.bytecode.BytecodeTableSwitch;
|
|
247 |
import org.graalvm.compiler.bytecode.Bytecodes;
|
46344
|
248 |
import org.graalvm.compiler.core.common.PermanentBailoutException;
|
46640
|
249 |
import org.graalvm.compiler.debug.DebugContext;
|
58533
|
250 |
import org.graalvm.compiler.debug.GraalError;
|
46344
|
251 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
252 |
|
|
253 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
254 |
import jdk.vm.ci.meta.ExceptionHandler;
|
|
255 |
|
|
256 |
/**
|
|
257 |
* Builds a mapping between bytecodes and basic blocks and builds a conservative control flow graph
|
54724
|
258 |
* (CFG). It makes one linear pass over the bytecodes to build the CFG where it detects block
|
43972
|
259 |
* headers and connects them.
|
|
260 |
* <p>
|
|
261 |
* It also creates exception dispatch blocks for exception handling. These blocks are between a
|
|
262 |
* bytecode that might throw an exception, and the actual exception handler entries, and are later
|
|
263 |
* used to create the type checks with the exception handler catch types. If a bytecode is covered
|
|
264 |
* by an exception handler, this bytecode ends the basic block. This guarantees that a) control flow
|
|
265 |
* cannot be transferred to an exception dispatch block in the middle of a block, and b) that every
|
|
266 |
* block has at most one exception dispatch block (which is always the last entry in the successor
|
|
267 |
* list).
|
|
268 |
* <p>
|
|
269 |
* If a bytecode is covered by multiple exception handlers, a chain of exception dispatch blocks is
|
|
270 |
* created so that multiple exception handler types can be checked. The chains are re-used if
|
|
271 |
* multiple bytecodes are covered by the same exception handlers.
|
|
272 |
* <p>
|
|
273 |
* Note that exception unwinds, i.e., bytecodes that can throw an exception but the exception is not
|
|
274 |
* handled in this method, do not end a basic block. Not modeling the exception unwind block reduces
|
|
275 |
* the complexity of the CFG, and there is no algorithm yet where the exception unwind block would
|
|
276 |
* matter.
|
|
277 |
* <p>
|
|
278 |
* The class also handles subroutines (jsr and ret bytecodes): subroutines are inlined by
|
|
279 |
* duplicating the subroutine blocks. This is limited to simple, structured subroutines with a
|
|
280 |
* maximum subroutine nesting of 4. Otherwise, a bailout is thrown.
|
|
281 |
* <p>
|
|
282 |
* Loops in the methods are detected. If a method contains an irreducible loop (a loop with more
|
|
283 |
* than one entry), a bailout is thrown. This simplifies the compiler later on since only structured
|
|
284 |
* loops need to be supported.
|
|
285 |
* <p>
|
|
286 |
* A data flow analysis computes the live local variables from the point of view of the interpreter.
|
|
287 |
* The result is used later to prune frame states, i.e., remove local variable entries that are
|
|
288 |
* guaranteed to be never used again (even in the case of deoptimization).
|
|
289 |
* <p>
|
|
290 |
* The algorithms and analysis in this class are conservative and do not use any assumptions or
|
|
291 |
* profiling information.
|
|
292 |
*/
|
|
293 |
public final class BciBlockMapping {
|
|
294 |
|
|
295 |
public static class BciBlock implements Cloneable {
|
|
296 |
|
49873
|
297 |
int id;
|
|
298 |
final int startBci;
|
58533
|
299 |
int endBci; // The bci of the last bytecode in the block
|
49873
|
300 |
private boolean isExceptionEntry;
|
|
301 |
private boolean isLoopHeader;
|
|
302 |
int loopId;
|
|
303 |
int loopEnd;
|
|
304 |
List<BciBlock> successors;
|
43972
|
305 |
private int predecessorCount;
|
|
306 |
|
|
307 |
private boolean visited;
|
|
308 |
private boolean active;
|
49873
|
309 |
long loops;
|
|
310 |
JSRData jsrData;
|
43972
|
311 |
|
|
312 |
public static class JSRData implements Cloneable {
|
46344
|
313 |
public EconomicMap<JsrScope, BciBlock> jsrAlternatives;
|
43972
|
314 |
public JsrScope jsrScope = JsrScope.EMPTY_SCOPE;
|
|
315 |
public BciBlock jsrSuccessor;
|
|
316 |
public int jsrReturnBci;
|
|
317 |
public BciBlock retSuccessor;
|
|
318 |
public boolean endsWithRet = false;
|
|
319 |
|
|
320 |
public JSRData copy() {
|
|
321 |
try {
|
|
322 |
return (JSRData) this.clone();
|
|
323 |
} catch (CloneNotSupportedException e) {
|
|
324 |
return null;
|
|
325 |
}
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
49873
|
329 |
BciBlock(int startBci) {
|
|
330 |
this.startBci = startBci;
|
|
331 |
this.successors = new ArrayList<>();
|
|
332 |
}
|
|
333 |
|
|
334 |
public int getStartBci() {
|
|
335 |
return startBci;
|
|
336 |
}
|
|
337 |
|
|
338 |
public int getEndBci() {
|
|
339 |
return endBci;
|
|
340 |
}
|
|
341 |
|
|
342 |
public long getLoops() {
|
|
343 |
return loops;
|
43972
|
344 |
}
|
|
345 |
|
|
346 |
public BciBlock exceptionDispatchBlock() {
|
|
347 |
if (successors.size() > 0 && successors.get(successors.size() - 1) instanceof ExceptionDispatchBlock) {
|
|
348 |
return successors.get(successors.size() - 1);
|
|
349 |
}
|
|
350 |
return null;
|
|
351 |
}
|
|
352 |
|
|
353 |
public int getId() {
|
|
354 |
return id;
|
|
355 |
}
|
|
356 |
|
|
357 |
public int getPredecessorCount() {
|
|
358 |
return this.predecessorCount;
|
|
359 |
}
|
|
360 |
|
|
361 |
public int numNormalSuccessors() {
|
|
362 |
if (exceptionDispatchBlock() != null) {
|
|
363 |
return successors.size() - 1;
|
|
364 |
}
|
|
365 |
return successors.size();
|
|
366 |
}
|
|
367 |
|
|
368 |
public BciBlock copy() {
|
|
369 |
try {
|
|
370 |
BciBlock block = (BciBlock) super.clone();
|
|
371 |
if (block.jsrData != null) {
|
|
372 |
block.jsrData = block.jsrData.copy();
|
|
373 |
}
|
|
374 |
block.successors = new ArrayList<>(successors);
|
|
375 |
return block;
|
|
376 |
} catch (CloneNotSupportedException e) {
|
|
377 |
throw new RuntimeException(e);
|
|
378 |
}
|
|
379 |
}
|
|
380 |
|
|
381 |
@Override
|
|
382 |
public String toString() {
|
|
383 |
StringBuilder sb = new StringBuilder("B").append(getId());
|
49873
|
384 |
sb.append('[').append(startBci).append("..").append(endBci);
|
|
385 |
if (isLoopHeader || isExceptionEntry || this instanceof ExceptionDispatchBlock) {
|
43972
|
386 |
sb.append(' ');
|
|
387 |
if (isLoopHeader) {
|
|
388 |
sb.append('L');
|
|
389 |
}
|
|
390 |
if (isExceptionEntry) {
|
|
391 |
sb.append('!');
|
49873
|
392 |
} else if (this instanceof ExceptionDispatchBlock) {
|
|
393 |
sb.append("<!>");
|
43972
|
394 |
}
|
|
395 |
}
|
|
396 |
sb.append(']');
|
|
397 |
return sb.toString();
|
|
398 |
}
|
|
399 |
|
|
400 |
public int getLoopDepth() {
|
|
401 |
return Long.bitCount(loops);
|
|
402 |
}
|
|
403 |
|
|
404 |
public boolean isLoopHeader() {
|
|
405 |
return isLoopHeader;
|
|
406 |
}
|
|
407 |
|
|
408 |
public boolean isExceptionEntry() {
|
|
409 |
return isExceptionEntry;
|
|
410 |
}
|
|
411 |
|
|
412 |
public BciBlock getSuccessor(int index) {
|
|
413 |
return successors.get(index);
|
|
414 |
}
|
|
415 |
|
|
416 |
/**
|
|
417 |
* Get the loop id of the inner most loop.
|
|
418 |
*
|
|
419 |
* @return the loop id of the most inner loop or -1 if not part of any loop
|
|
420 |
*/
|
|
421 |
public int getLoopId() {
|
|
422 |
long l = loops;
|
|
423 |
if (l == 0) {
|
|
424 |
return -1;
|
|
425 |
}
|
|
426 |
int pos = 0;
|
|
427 |
for (int lMask = 1; (l & lMask) == 0; lMask = lMask << 1) {
|
|
428 |
pos++;
|
|
429 |
}
|
|
430 |
return pos;
|
|
431 |
}
|
|
432 |
|
|
433 |
/**
|
|
434 |
* Iterate over loop ids.
|
|
435 |
*/
|
|
436 |
public Iterable<Integer> loopIdIterable() {
|
|
437 |
return new Iterable<Integer>() {
|
|
438 |
@Override
|
|
439 |
public Iterator<Integer> iterator() {
|
|
440 |
return idIterator(loops);
|
|
441 |
}
|
|
442 |
};
|
|
443 |
}
|
|
444 |
|
|
445 |
private static Iterator<Integer> idIterator(long field) {
|
|
446 |
return new Iterator<Integer>() {
|
|
447 |
|
|
448 |
long l = field;
|
|
449 |
int pos = 0;
|
|
450 |
int lMask = 1;
|
|
451 |
|
|
452 |
@Override
|
|
453 |
public Integer next() {
|
|
454 |
for (; (l & lMask) == 0; lMask = lMask << 1) {
|
|
455 |
pos++;
|
|
456 |
}
|
|
457 |
l &= ~lMask;
|
|
458 |
return pos;
|
|
459 |
}
|
|
460 |
|
|
461 |
@Override
|
|
462 |
public boolean hasNext() {
|
|
463 |
return l != 0;
|
|
464 |
}
|
|
465 |
};
|
|
466 |
|
|
467 |
}
|
|
468 |
|
|
469 |
public double probability() {
|
|
470 |
return 1D;
|
|
471 |
}
|
|
472 |
|
|
473 |
public BciBlock getPostdominator() {
|
|
474 |
return null;
|
|
475 |
}
|
|
476 |
|
|
477 |
private JSRData getOrCreateJSRData() {
|
|
478 |
if (jsrData == null) {
|
|
479 |
jsrData = new JSRData();
|
|
480 |
}
|
|
481 |
return jsrData;
|
|
482 |
}
|
|
483 |
|
|
484 |
void setEndsWithRet() {
|
|
485 |
getOrCreateJSRData().endsWithRet = true;
|
|
486 |
}
|
|
487 |
|
|
488 |
public JsrScope getJsrScope() {
|
|
489 |
if (this.jsrData == null) {
|
|
490 |
return JsrScope.EMPTY_SCOPE;
|
|
491 |
} else {
|
|
492 |
return jsrData.jsrScope;
|
|
493 |
}
|
|
494 |
}
|
|
495 |
|
|
496 |
public boolean endsWithRet() {
|
|
497 |
if (this.jsrData == null) {
|
|
498 |
return false;
|
|
499 |
} else {
|
|
500 |
return jsrData.endsWithRet;
|
|
501 |
}
|
|
502 |
}
|
|
503 |
|
|
504 |
void setRetSuccessor(BciBlock bciBlock) {
|
|
505 |
this.getOrCreateJSRData().retSuccessor = bciBlock;
|
|
506 |
}
|
|
507 |
|
|
508 |
public BciBlock getRetSuccessor() {
|
|
509 |
if (this.jsrData == null) {
|
|
510 |
return null;
|
|
511 |
} else {
|
|
512 |
return jsrData.retSuccessor;
|
|
513 |
}
|
|
514 |
}
|
|
515 |
|
|
516 |
public BciBlock getJsrSuccessor() {
|
|
517 |
if (this.jsrData == null) {
|
|
518 |
return null;
|
|
519 |
} else {
|
|
520 |
return jsrData.jsrSuccessor;
|
|
521 |
}
|
|
522 |
}
|
|
523 |
|
|
524 |
public int getJsrReturnBci() {
|
|
525 |
if (this.jsrData == null) {
|
|
526 |
return -1;
|
|
527 |
} else {
|
|
528 |
return jsrData.jsrReturnBci;
|
|
529 |
}
|
|
530 |
}
|
|
531 |
|
46344
|
532 |
public EconomicMap<JsrScope, BciBlock> getJsrAlternatives() {
|
43972
|
533 |
if (this.jsrData == null) {
|
|
534 |
return null;
|
|
535 |
} else {
|
|
536 |
return jsrData.jsrAlternatives;
|
|
537 |
}
|
|
538 |
}
|
|
539 |
|
|
540 |
public void initJsrAlternatives() {
|
|
541 |
JSRData data = this.getOrCreateJSRData();
|
|
542 |
if (data.jsrAlternatives == null) {
|
46344
|
543 |
data.jsrAlternatives = EconomicMap.create(Equivalence.DEFAULT);
|
43972
|
544 |
}
|
|
545 |
}
|
|
546 |
|
|
547 |
void setJsrScope(JsrScope nextScope) {
|
|
548 |
this.getOrCreateJSRData().jsrScope = nextScope;
|
|
549 |
}
|
|
550 |
|
|
551 |
void setJsrSuccessor(BciBlock clone) {
|
|
552 |
this.getOrCreateJSRData().jsrSuccessor = clone;
|
|
553 |
}
|
|
554 |
|
|
555 |
void setJsrReturnBci(int bci) {
|
|
556 |
this.getOrCreateJSRData().jsrReturnBci = bci;
|
|
557 |
}
|
|
558 |
|
|
559 |
public int getSuccessorCount() {
|
|
560 |
return successors.size();
|
|
561 |
}
|
|
562 |
|
|
563 |
public List<BciBlock> getSuccessors() {
|
|
564 |
return successors;
|
|
565 |
}
|
|
566 |
|
|
567 |
void setId(int i) {
|
|
568 |
this.id = i;
|
|
569 |
}
|
|
570 |
|
|
571 |
public void addSuccessor(BciBlock sux) {
|
|
572 |
successors.add(sux);
|
|
573 |
sux.predecessorCount++;
|
|
574 |
}
|
|
575 |
|
|
576 |
public void clearSucccessors() {
|
|
577 |
for (BciBlock sux : successors) {
|
|
578 |
sux.predecessorCount--;
|
|
579 |
}
|
|
580 |
successors.clear();
|
|
581 |
}
|
49873
|
582 |
|
|
583 |
public boolean isExceptionDispatch() {
|
|
584 |
return false;
|
|
585 |
}
|
43972
|
586 |
}
|
|
587 |
|
|
588 |
public static class ExceptionDispatchBlock extends BciBlock {
|
49873
|
589 |
public final ExceptionHandler handler;
|
|
590 |
public final int deoptBci;
|
|
591 |
|
|
592 |
/**
|
|
593 |
* Constructor for a normal dispatcher.
|
|
594 |
*/
|
|
595 |
ExceptionDispatchBlock(ExceptionHandler handler, int deoptBci) {
|
|
596 |
super(handler.getHandlerBCI());
|
|
597 |
this.endBci = startBci;
|
|
598 |
this.deoptBci = deoptBci;
|
|
599 |
this.handler = handler;
|
|
600 |
}
|
|
601 |
|
|
602 |
/**
|
|
603 |
* Constructor for the method unwind dispatcher.
|
|
604 |
*/
|
|
605 |
ExceptionDispatchBlock(int deoptBci) {
|
|
606 |
super(deoptBci);
|
|
607 |
this.endBci = deoptBci;
|
|
608 |
this.deoptBci = deoptBci;
|
|
609 |
this.handler = null;
|
|
610 |
}
|
|
611 |
|
|
612 |
@Override
|
|
613 |
public boolean isExceptionDispatch() {
|
|
614 |
return true;
|
|
615 |
}
|
43972
|
616 |
}
|
|
617 |
|
54724
|
618 |
private static final class TraversalStep {
|
|
619 |
private BciBlock block;
|
|
620 |
private int currentSuccessorIndex;
|
|
621 |
private long loops;
|
|
622 |
|
|
623 |
private TraversalStep(BciBlock block) {
|
|
624 |
this.block = block;
|
|
625 |
this.currentSuccessorIndex = 0;
|
|
626 |
this.loops = 0;
|
|
627 |
}
|
|
628 |
}
|
|
629 |
|
43972
|
630 |
/**
|
|
631 |
* The blocks found in this method, in reverse postorder.
|
|
632 |
*/
|
|
633 |
private BciBlock[] blocks;
|
|
634 |
public final Bytecode code;
|
|
635 |
public boolean hasJsrBytecodes;
|
|
636 |
|
|
637 |
private final ExceptionHandler[] exceptionHandlers;
|
|
638 |
private BciBlock startBlock;
|
|
639 |
private BciBlock[] loopHeaders;
|
|
640 |
|
|
641 |
private static final int LOOP_HEADER_MAX_CAPACITY = Long.SIZE;
|
|
642 |
private static final int LOOP_HEADER_INITIAL_CAPACITY = 4;
|
|
643 |
|
|
644 |
private int blocksNotYetAssignedId;
|
46640
|
645 |
private final DebugContext debug;
|
43972
|
646 |
|
|
647 |
/**
|
|
648 |
* Creates a new BlockMap instance from {@code code}.
|
|
649 |
*/
|
46640
|
650 |
private BciBlockMapping(Bytecode code, DebugContext debug) {
|
43972
|
651 |
this.code = code;
|
46640
|
652 |
this.debug = debug;
|
43972
|
653 |
this.exceptionHandlers = code.getExceptionHandlers();
|
|
654 |
}
|
|
655 |
|
|
656 |
public BciBlock[] getBlocks() {
|
|
657 |
return this.blocks;
|
|
658 |
}
|
|
659 |
|
|
660 |
/**
|
|
661 |
* Builds the block map and conservative CFG and numbers blocks.
|
|
662 |
*/
|
46344
|
663 |
public void build(BytecodeStream stream, OptionValues options) {
|
43972
|
664 |
int codeSize = code.getCodeSize();
|
|
665 |
BciBlock[] blockMap = new BciBlock[codeSize];
|
|
666 |
makeExceptionEntries(blockMap);
|
|
667 |
iterateOverBytecodes(blockMap, stream);
|
|
668 |
if (hasJsrBytecodes) {
|
46344
|
669 |
if (!SupportJsrBytecodes.getValue(options)) {
|
43972
|
670 |
throw new JsrNotSupportedBailout("jsr/ret parsing disabled");
|
|
671 |
}
|
|
672 |
createJsrAlternatives(blockMap, blockMap[0]);
|
|
673 |
}
|
46640
|
674 |
if (debug.isLogEnabled()) {
|
43972
|
675 |
this.log(blockMap, "Before BlockOrder");
|
|
676 |
}
|
|
677 |
computeBlockOrder(blockMap);
|
|
678 |
fixLoopBits(blockMap);
|
|
679 |
|
|
680 |
assert verify();
|
|
681 |
|
|
682 |
startBlock = blockMap[0];
|
46640
|
683 |
if (debug.isLogEnabled()) {
|
43972
|
684 |
this.log(blockMap, "Before LivenessAnalysis");
|
|
685 |
}
|
|
686 |
}
|
|
687 |
|
|
688 |
private boolean verify() {
|
|
689 |
for (BciBlock block : blocks) {
|
|
690 |
assert blocks[block.getId()] == block;
|
|
691 |
for (int i = 0; i < block.getSuccessorCount(); i++) {
|
|
692 |
BciBlock sux = block.getSuccessor(i);
|
|
693 |
if (sux instanceof ExceptionDispatchBlock) {
|
|
694 |
assert i == block.getSuccessorCount() - 1 : "Only one exception handler allowed, and it must be last in successors list";
|
|
695 |
}
|
|
696 |
}
|
|
697 |
}
|
|
698 |
|
|
699 |
return true;
|
|
700 |
}
|
|
701 |
|
|
702 |
private void makeExceptionEntries(BciBlock[] blockMap) {
|
|
703 |
// start basic blocks at all exception handler blocks and mark them as exception entries
|
|
704 |
for (ExceptionHandler h : this.exceptionHandlers) {
|
|
705 |
BciBlock xhandler = makeBlock(blockMap, h.getHandlerBCI());
|
|
706 |
xhandler.isExceptionEntry = true;
|
|
707 |
}
|
|
708 |
}
|
|
709 |
|
|
710 |
private void iterateOverBytecodes(BciBlock[] blockMap, BytecodeStream stream) {
|
|
711 |
// iterate over the bytecodes top to bottom.
|
|
712 |
// mark the entrypoints of basic blocks and build lists of successors for
|
|
713 |
// all bytecodes that end basic blocks (i.e. goto, ifs, switches, throw, jsr, returns, ret)
|
|
714 |
BciBlock current = null;
|
|
715 |
stream.setBCI(0);
|
|
716 |
while (stream.currentBC() != Bytecodes.END) {
|
|
717 |
int bci = stream.currentBCI();
|
|
718 |
|
|
719 |
if (current == null || blockMap[bci] != null) {
|
|
720 |
BciBlock b = makeBlock(blockMap, bci);
|
|
721 |
if (current != null) {
|
|
722 |
addSuccessor(blockMap, current.endBci, b);
|
|
723 |
}
|
|
724 |
current = b;
|
|
725 |
}
|
|
726 |
blockMap[bci] = current;
|
|
727 |
current.endBci = bci;
|
|
728 |
|
|
729 |
switch (stream.currentBC()) {
|
|
730 |
case IRETURN: // fall through
|
|
731 |
case LRETURN: // fall through
|
|
732 |
case FRETURN: // fall through
|
|
733 |
case DRETURN: // fall through
|
|
734 |
case ARETURN: // fall through
|
|
735 |
case RETURN: {
|
|
736 |
current = null;
|
|
737 |
break;
|
|
738 |
}
|
|
739 |
case ATHROW: {
|
|
740 |
current = null;
|
|
741 |
ExceptionDispatchBlock handler = handleExceptions(blockMap, bci);
|
|
742 |
if (handler != null) {
|
|
743 |
addSuccessor(blockMap, bci, handler);
|
|
744 |
}
|
|
745 |
break;
|
|
746 |
}
|
|
747 |
case IFEQ: // fall through
|
|
748 |
case IFNE: // fall through
|
|
749 |
case IFLT: // fall through
|
|
750 |
case IFGE: // fall through
|
|
751 |
case IFGT: // fall through
|
|
752 |
case IFLE: // fall through
|
|
753 |
case IF_ICMPEQ: // fall through
|
|
754 |
case IF_ICMPNE: // fall through
|
|
755 |
case IF_ICMPLT: // fall through
|
|
756 |
case IF_ICMPGE: // fall through
|
|
757 |
case IF_ICMPGT: // fall through
|
|
758 |
case IF_ICMPLE: // fall through
|
|
759 |
case IF_ACMPEQ: // fall through
|
|
760 |
case IF_ACMPNE: // fall through
|
|
761 |
case IFNULL: // fall through
|
|
762 |
case IFNONNULL: {
|
|
763 |
current = null;
|
|
764 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.readBranchDest()));
|
|
765 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.nextBCI()));
|
|
766 |
break;
|
|
767 |
}
|
|
768 |
case GOTO:
|
|
769 |
case GOTO_W: {
|
|
770 |
current = null;
|
|
771 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.readBranchDest()));
|
|
772 |
break;
|
|
773 |
}
|
|
774 |
case TABLESWITCH: {
|
|
775 |
current = null;
|
|
776 |
addSwitchSuccessors(blockMap, bci, new BytecodeTableSwitch(stream, bci));
|
|
777 |
break;
|
|
778 |
}
|
|
779 |
case LOOKUPSWITCH: {
|
|
780 |
current = null;
|
|
781 |
addSwitchSuccessors(blockMap, bci, new BytecodeLookupSwitch(stream, bci));
|
|
782 |
break;
|
|
783 |
}
|
|
784 |
case JSR:
|
|
785 |
case JSR_W: {
|
|
786 |
hasJsrBytecodes = true;
|
|
787 |
int target = stream.readBranchDest();
|
|
788 |
if (target == 0) {
|
|
789 |
throw new JsrNotSupportedBailout("jsr target bci 0 not allowed");
|
|
790 |
}
|
|
791 |
BciBlock b1 = makeBlock(blockMap, target);
|
|
792 |
current.setJsrSuccessor(b1);
|
|
793 |
current.setJsrReturnBci(stream.nextBCI());
|
|
794 |
current = null;
|
|
795 |
addSuccessor(blockMap, bci, b1);
|
|
796 |
break;
|
|
797 |
}
|
|
798 |
case RET: {
|
|
799 |
current.setEndsWithRet();
|
|
800 |
current = null;
|
|
801 |
break;
|
|
802 |
}
|
|
803 |
case INVOKEINTERFACE:
|
|
804 |
case INVOKESPECIAL:
|
|
805 |
case INVOKESTATIC:
|
|
806 |
case INVOKEVIRTUAL:
|
|
807 |
case INVOKEDYNAMIC: {
|
|
808 |
current = null;
|
|
809 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.nextBCI()));
|
|
810 |
ExceptionDispatchBlock handler = handleExceptions(blockMap, bci);
|
|
811 |
if (handler != null) {
|
|
812 |
addSuccessor(blockMap, bci, handler);
|
|
813 |
}
|
|
814 |
break;
|
|
815 |
}
|
50330
|
816 |
case IDIV:
|
|
817 |
case IREM:
|
|
818 |
case LDIV:
|
|
819 |
case LREM:
|
43972
|
820 |
case IASTORE:
|
|
821 |
case LASTORE:
|
|
822 |
case FASTORE:
|
|
823 |
case DASTORE:
|
|
824 |
case AASTORE:
|
|
825 |
case BASTORE:
|
|
826 |
case CASTORE:
|
|
827 |
case SASTORE:
|
|
828 |
case IALOAD:
|
|
829 |
case LALOAD:
|
|
830 |
case FALOAD:
|
|
831 |
case DALOAD:
|
|
832 |
case AALOAD:
|
|
833 |
case BALOAD:
|
|
834 |
case CALOAD:
|
|
835 |
case SALOAD:
|
|
836 |
case ARRAYLENGTH:
|
50330
|
837 |
case CHECKCAST:
|
58533
|
838 |
case INSTANCEOF:
|
51736
|
839 |
case NEW:
|
58533
|
840 |
case NEWARRAY:
|
51736
|
841 |
case ANEWARRAY:
|
|
842 |
case MULTIANEWARRAY:
|
49873
|
843 |
case PUTSTATIC:
|
|
844 |
case GETSTATIC:
|
43972
|
845 |
case PUTFIELD:
|
52910
|
846 |
case GETFIELD:
|
|
847 |
case LDC:
|
|
848 |
case LDC_W:
|
58533
|
849 |
case LDC2_W:
|
|
850 |
case MONITORENTER: {
|
51736
|
851 |
/*
|
|
852 |
* All bytecodes that can trigger lazy class initialization via a
|
|
853 |
* ClassInitializationPlugin (allocations, static field access) must be listed
|
|
854 |
* because the class initializer is allowed to throw an exception, which
|
|
855 |
* requires proper exception handling.
|
|
856 |
*/
|
43972
|
857 |
ExceptionDispatchBlock handler = handleExceptions(blockMap, bci);
|
|
858 |
if (handler != null) {
|
|
859 |
current = null;
|
|
860 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.nextBCI()));
|
|
861 |
addSuccessor(blockMap, bci, handler);
|
|
862 |
}
|
58533
|
863 |
break;
|
43972
|
864 |
}
|
58533
|
865 |
|
|
866 |
case NOP:
|
|
867 |
case ACONST_NULL:
|
|
868 |
case ICONST_M1:
|
|
869 |
case ICONST_0:
|
|
870 |
case ICONST_1:
|
|
871 |
case ICONST_2:
|
|
872 |
case ICONST_3:
|
|
873 |
case ICONST_4:
|
|
874 |
case ICONST_5:
|
|
875 |
case LCONST_0:
|
|
876 |
case LCONST_1:
|
|
877 |
case FCONST_0:
|
|
878 |
case FCONST_1:
|
|
879 |
case FCONST_2:
|
|
880 |
case DCONST_0:
|
|
881 |
case DCONST_1:
|
|
882 |
case BIPUSH:
|
|
883 |
case SIPUSH:
|
|
884 |
case ILOAD:
|
|
885 |
case LLOAD:
|
|
886 |
case FLOAD:
|
|
887 |
case DLOAD:
|
|
888 |
case ALOAD:
|
|
889 |
case ILOAD_0:
|
|
890 |
case ILOAD_1:
|
|
891 |
case ILOAD_2:
|
|
892 |
case ILOAD_3:
|
|
893 |
case LLOAD_0:
|
|
894 |
case LLOAD_1:
|
|
895 |
case LLOAD_2:
|
|
896 |
case LLOAD_3:
|
|
897 |
case FLOAD_0:
|
|
898 |
case FLOAD_1:
|
|
899 |
case FLOAD_2:
|
|
900 |
case FLOAD_3:
|
|
901 |
case DLOAD_0:
|
|
902 |
case DLOAD_1:
|
|
903 |
case DLOAD_2:
|
|
904 |
case DLOAD_3:
|
|
905 |
case ALOAD_0:
|
|
906 |
case ALOAD_1:
|
|
907 |
case ALOAD_2:
|
|
908 |
case ALOAD_3:
|
|
909 |
case ISTORE:
|
|
910 |
case LSTORE:
|
|
911 |
case FSTORE:
|
|
912 |
case DSTORE:
|
|
913 |
case ASTORE:
|
|
914 |
case ISTORE_0:
|
|
915 |
case ISTORE_1:
|
|
916 |
case ISTORE_2:
|
|
917 |
case ISTORE_3:
|
|
918 |
case LSTORE_0:
|
|
919 |
case LSTORE_1:
|
|
920 |
case LSTORE_2:
|
|
921 |
case LSTORE_3:
|
|
922 |
case FSTORE_0:
|
|
923 |
case FSTORE_1:
|
|
924 |
case FSTORE_2:
|
|
925 |
case FSTORE_3:
|
|
926 |
case DSTORE_0:
|
|
927 |
case DSTORE_1:
|
|
928 |
case DSTORE_2:
|
|
929 |
case DSTORE_3:
|
|
930 |
case ASTORE_0:
|
|
931 |
case ASTORE_1:
|
|
932 |
case ASTORE_2:
|
|
933 |
case ASTORE_3:
|
|
934 |
case POP:
|
|
935 |
case POP2:
|
|
936 |
case DUP:
|
|
937 |
case DUP_X1:
|
|
938 |
case DUP_X2:
|
|
939 |
case DUP2:
|
|
940 |
case DUP2_X1:
|
|
941 |
case DUP2_X2:
|
|
942 |
case SWAP:
|
|
943 |
case IADD:
|
|
944 |
case LADD:
|
|
945 |
case FADD:
|
|
946 |
case DADD:
|
|
947 |
case ISUB:
|
|
948 |
case LSUB:
|
|
949 |
case FSUB:
|
|
950 |
case DSUB:
|
|
951 |
case IMUL:
|
|
952 |
case LMUL:
|
|
953 |
case FMUL:
|
|
954 |
case DMUL:
|
|
955 |
case FDIV:
|
|
956 |
case DDIV:
|
|
957 |
case FREM:
|
|
958 |
case DREM:
|
|
959 |
case INEG:
|
|
960 |
case LNEG:
|
|
961 |
case FNEG:
|
|
962 |
case DNEG:
|
|
963 |
case ISHL:
|
|
964 |
case LSHL:
|
|
965 |
case ISHR:
|
|
966 |
case LSHR:
|
|
967 |
case IUSHR:
|
|
968 |
case LUSHR:
|
|
969 |
case IAND:
|
|
970 |
case LAND:
|
|
971 |
case IOR:
|
|
972 |
case LOR:
|
|
973 |
case IXOR:
|
|
974 |
case LXOR:
|
|
975 |
case IINC:
|
|
976 |
case I2L:
|
|
977 |
case I2F:
|
|
978 |
case I2D:
|
|
979 |
case L2I:
|
|
980 |
case L2F:
|
|
981 |
case L2D:
|
|
982 |
case F2I:
|
|
983 |
case F2L:
|
|
984 |
case F2D:
|
|
985 |
case D2I:
|
|
986 |
case D2L:
|
|
987 |
case D2F:
|
|
988 |
case I2B:
|
|
989 |
case I2C:
|
|
990 |
case I2S:
|
|
991 |
case LCMP:
|
|
992 |
case FCMPL:
|
|
993 |
case FCMPG:
|
|
994 |
case DCMPL:
|
|
995 |
case DCMPG:
|
|
996 |
case MONITOREXIT:
|
|
997 |
// All stack manipulation, comparison, conversion and arithmetic operators
|
|
998 |
// except for idiv and irem can't throw exceptions so the don't need to connect
|
|
999 |
// exception edges. MONITOREXIT can't throw exceptions in the context of
|
|
1000 |
// compiled code because of the structured locking requirement in the parser.
|
|
1001 |
break;
|
|
1002 |
|
|
1003 |
case WIDE:
|
|
1004 |
case BREAKPOINT:
|
|
1005 |
default:
|
|
1006 |
throw new GraalError("Unhandled bytecode");
|
43972
|
1007 |
}
|
|
1008 |
stream.next();
|
|
1009 |
}
|
|
1010 |
}
|
|
1011 |
|
|
1012 |
private BciBlock makeBlock(BciBlock[] blockMap, int startBci) {
|
|
1013 |
BciBlock oldBlock = blockMap[startBci];
|
|
1014 |
if (oldBlock == null) {
|
49873
|
1015 |
BciBlock newBlock = new BciBlock(startBci);
|
43972
|
1016 |
blocksNotYetAssignedId++;
|
|
1017 |
blockMap[startBci] = newBlock;
|
|
1018 |
return newBlock;
|
|
1019 |
|
|
1020 |
} else if (oldBlock.startBci != startBci) {
|
|
1021 |
// Backward branch into the middle of an already processed block.
|
|
1022 |
// Add the correct fall-through successor.
|
49873
|
1023 |
BciBlock newBlock = new BciBlock(startBci);
|
43972
|
1024 |
blocksNotYetAssignedId++;
|
|
1025 |
newBlock.endBci = oldBlock.endBci;
|
|
1026 |
for (BciBlock oldSuccessor : oldBlock.getSuccessors()) {
|
|
1027 |
newBlock.addSuccessor(oldSuccessor);
|
|
1028 |
}
|
|
1029 |
|
|
1030 |
oldBlock.endBci = startBci - 1;
|
|
1031 |
oldBlock.clearSucccessors();
|
|
1032 |
oldBlock.addSuccessor(newBlock);
|
|
1033 |
|
|
1034 |
for (int i = startBci; i <= newBlock.endBci; i++) {
|
|
1035 |
blockMap[i] = newBlock;
|
|
1036 |
}
|
|
1037 |
return newBlock;
|
|
1038 |
|
|
1039 |
} else {
|
|
1040 |
return oldBlock;
|
|
1041 |
}
|
|
1042 |
}
|
|
1043 |
|
|
1044 |
private void addSwitchSuccessors(BciBlock[] blockMap, int predBci, BytecodeSwitch bswitch) {
|
|
1045 |
// adds distinct targets to the successor list
|
|
1046 |
Collection<Integer> targets = new TreeSet<>();
|
|
1047 |
for (int i = 0; i < bswitch.numberOfCases(); i++) {
|
|
1048 |
targets.add(bswitch.targetAt(i));
|
|
1049 |
}
|
|
1050 |
targets.add(bswitch.defaultTarget());
|
|
1051 |
for (int targetBci : targets) {
|
|
1052 |
addSuccessor(blockMap, predBci, makeBlock(blockMap, targetBci));
|
|
1053 |
}
|
|
1054 |
}
|
|
1055 |
|
|
1056 |
private static void addSuccessor(BciBlock[] blockMap, int predBci, BciBlock sux) {
|
|
1057 |
BciBlock predecessor = blockMap[predBci];
|
|
1058 |
if (sux.isExceptionEntry) {
|
|
1059 |
throw new PermanentBailoutException("Exception handler can be reached by both normal and exceptional control flow");
|
|
1060 |
}
|
|
1061 |
predecessor.addSuccessor(sux);
|
|
1062 |
}
|
|
1063 |
|
|
1064 |
private final ArrayList<BciBlock> jsrVisited = new ArrayList<>();
|
|
1065 |
|
|
1066 |
private void createJsrAlternatives(BciBlock[] blockMap, BciBlock block) {
|
|
1067 |
jsrVisited.add(block);
|
|
1068 |
JsrScope scope = block.getJsrScope();
|
|
1069 |
|
|
1070 |
if (block.endsWithRet()) {
|
|
1071 |
block.setRetSuccessor(blockMap[scope.nextReturnAddress()]);
|
|
1072 |
block.addSuccessor(block.getRetSuccessor());
|
|
1073 |
assert block.getRetSuccessor() != block.getJsrSuccessor();
|
|
1074 |
}
|
46640
|
1075 |
debug.log("JSR alternatives block %s sux %s jsrSux %s retSux %s jsrScope %s", block, block.getSuccessors(), block.getJsrSuccessor(), block.getRetSuccessor(), block.getJsrScope());
|
43972
|
1076 |
|
|
1077 |
if (block.getJsrSuccessor() != null || !scope.isEmpty()) {
|
|
1078 |
for (int i = 0; i < block.getSuccessorCount(); i++) {
|
|
1079 |
BciBlock successor = block.getSuccessor(i);
|
|
1080 |
JsrScope nextScope = scope;
|
|
1081 |
if (successor == block.getJsrSuccessor()) {
|
|
1082 |
nextScope = scope.push(block.getJsrReturnBci());
|
|
1083 |
}
|
|
1084 |
if (successor == block.getRetSuccessor()) {
|
|
1085 |
nextScope = scope.pop();
|
|
1086 |
}
|
|
1087 |
if (!successor.getJsrScope().isPrefixOf(nextScope)) {
|
|
1088 |
throw new JsrNotSupportedBailout("unstructured control flow (" + successor.getJsrScope() + " " + nextScope + ")");
|
|
1089 |
}
|
|
1090 |
if (!nextScope.isEmpty()) {
|
|
1091 |
BciBlock clone;
|
|
1092 |
if (successor.getJsrAlternatives() != null && successor.getJsrAlternatives().containsKey(nextScope)) {
|
|
1093 |
clone = successor.getJsrAlternatives().get(nextScope);
|
|
1094 |
} else {
|
|
1095 |
successor.initJsrAlternatives();
|
|
1096 |
clone = successor.copy();
|
|
1097 |
blocksNotYetAssignedId++;
|
|
1098 |
clone.setJsrScope(nextScope);
|
|
1099 |
successor.getJsrAlternatives().put(nextScope, clone);
|
|
1100 |
}
|
|
1101 |
block.getSuccessors().set(i, clone);
|
|
1102 |
if (successor == block.getJsrSuccessor()) {
|
|
1103 |
block.setJsrSuccessor(clone);
|
|
1104 |
}
|
|
1105 |
if (successor == block.getRetSuccessor()) {
|
|
1106 |
block.setRetSuccessor(clone);
|
|
1107 |
}
|
|
1108 |
}
|
|
1109 |
}
|
|
1110 |
}
|
|
1111 |
for (BciBlock successor : block.getSuccessors()) {
|
|
1112 |
if (!jsrVisited.contains(successor)) {
|
|
1113 |
createJsrAlternatives(blockMap, successor);
|
|
1114 |
}
|
|
1115 |
}
|
|
1116 |
}
|
|
1117 |
|
|
1118 |
private ExceptionDispatchBlock handleExceptions(BciBlock[] blockMap, int bci) {
|
|
1119 |
ExceptionDispatchBlock lastHandler = null;
|
49873
|
1120 |
int dispatchBlocks = 0;
|
43972
|
1121 |
|
|
1122 |
for (int i = exceptionHandlers.length - 1; i >= 0; i--) {
|
|
1123 |
ExceptionHandler h = exceptionHandlers[i];
|
|
1124 |
if (h.getStartBCI() <= bci && bci < h.getEndBCI()) {
|
|
1125 |
if (h.isCatchAll()) {
|
|
1126 |
// Discard all information about succeeding exception handlers, since they can
|
|
1127 |
// never be reached.
|
49873
|
1128 |
dispatchBlocks = 0;
|
43972
|
1129 |
lastHandler = null;
|
|
1130 |
}
|
|
1131 |
|
48861
|
1132 |
// We do not reuse exception dispatch blocks, because nested exception handlers
|
|
1133 |
// might have problems reasoning about the correct frame state.
|
49873
|
1134 |
ExceptionDispatchBlock curHandler = new ExceptionDispatchBlock(h, bci);
|
|
1135 |
dispatchBlocks++;
|
48861
|
1136 |
curHandler.addSuccessor(blockMap[h.getHandlerBCI()]);
|
|
1137 |
if (lastHandler != null) {
|
|
1138 |
curHandler.addSuccessor(lastHandler);
|
43972
|
1139 |
}
|
|
1140 |
lastHandler = curHandler;
|
|
1141 |
}
|
|
1142 |
}
|
49873
|
1143 |
blocksNotYetAssignedId += dispatchBlocks;
|
43972
|
1144 |
return lastHandler;
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
private boolean loopChanges;
|
|
1148 |
|
|
1149 |
private void fixLoopBits(BciBlock[] blockMap) {
|
|
1150 |
do {
|
|
1151 |
loopChanges = false;
|
|
1152 |
for (BciBlock b : blocks) {
|
|
1153 |
b.visited = false;
|
|
1154 |
}
|
|
1155 |
|
54724
|
1156 |
long loop = fixLoopBits(blockMap[0]);
|
43972
|
1157 |
|
|
1158 |
if (loop != 0) {
|
|
1159 |
// There is a path from a loop end to the method entry that does not pass the loop
|
|
1160 |
// header.
|
|
1161 |
// Therefore, the loop is non reducible (has more than one entry).
|
|
1162 |
// We don't want to compile such methods because the IR only supports structured
|
|
1163 |
// loops.
|
|
1164 |
throw new PermanentBailoutException("Non-reducible loop: %016x", loop);
|
|
1165 |
}
|
|
1166 |
} while (loopChanges);
|
|
1167 |
}
|
|
1168 |
|
|
1169 |
private void computeBlockOrder(BciBlock[] blockMap) {
|
|
1170 |
int maxBlocks = blocksNotYetAssignedId;
|
|
1171 |
this.blocks = new BciBlock[blocksNotYetAssignedId];
|
|
1172 |
long loop = computeBlockOrder(blockMap[0]);
|
|
1173 |
|
|
1174 |
if (loop != 0) {
|
|
1175 |
// There is a path from a loop end to the method entry that does not pass the loop
|
|
1176 |
// header. Therefore, the loop is non reducible (has more than one entry).
|
|
1177 |
// We don't want to compile such methods because the IR only supports structured loops.
|
|
1178 |
throw new PermanentBailoutException("Non-reducible loop");
|
|
1179 |
}
|
|
1180 |
|
|
1181 |
// Purge null entries for unreached blocks and sort blocks such that loop bodies are always
|
|
1182 |
// consecutively in the array.
|
46393
|
1183 |
int blockCount = maxBlocks - blocksNotYetAssignedId + 1;
|
43972
|
1184 |
BciBlock[] newBlocks = new BciBlock[blockCount];
|
|
1185 |
int next = 0;
|
|
1186 |
for (int i = 0; i < blocks.length; ++i) {
|
|
1187 |
BciBlock b = blocks[i];
|
|
1188 |
if (b != null) {
|
|
1189 |
b.setId(next);
|
|
1190 |
newBlocks[next++] = b;
|
|
1191 |
if (b.isLoopHeader) {
|
|
1192 |
next = handleLoopHeader(newBlocks, next, i, b);
|
|
1193 |
}
|
|
1194 |
}
|
|
1195 |
}
|
46393
|
1196 |
assert next == newBlocks.length - 1;
|
43972
|
1197 |
|
|
1198 |
// Add unwind block.
|
54601
|
1199 |
ExceptionDispatchBlock unwindBlock = new ExceptionDispatchBlock(BytecodeFrame.AFTER_EXCEPTION_BCI);
|
43972
|
1200 |
unwindBlock.setId(newBlocks.length - 1);
|
|
1201 |
newBlocks[newBlocks.length - 1] = unwindBlock;
|
|
1202 |
|
|
1203 |
blocks = newBlocks;
|
|
1204 |
}
|
|
1205 |
|
|
1206 |
private int handleLoopHeader(BciBlock[] newBlocks, int nextStart, int i, BciBlock loopHeader) {
|
|
1207 |
int next = nextStart;
|
|
1208 |
int endOfLoop = nextStart - 1;
|
|
1209 |
for (int j = i + 1; j < blocks.length; ++j) {
|
|
1210 |
BciBlock other = blocks[j];
|
|
1211 |
if (other != null && (other.loops & (1L << loopHeader.loopId)) != 0) {
|
|
1212 |
other.setId(next);
|
|
1213 |
endOfLoop = next;
|
|
1214 |
newBlocks[next++] = other;
|
|
1215 |
blocks[j] = null;
|
|
1216 |
if (other.isLoopHeader) {
|
|
1217 |
next = handleLoopHeader(newBlocks, next, j, other);
|
|
1218 |
}
|
|
1219 |
}
|
|
1220 |
}
|
|
1221 |
loopHeader.loopEnd = endOfLoop;
|
|
1222 |
return next;
|
|
1223 |
}
|
|
1224 |
|
|
1225 |
public void log(BciBlock[] blockMap, String name) {
|
46640
|
1226 |
if (debug.isLogEnabled()) {
|
49873
|
1227 |
debug.log("%sBlockMap %s: %n%s", debug.getCurrentScopeName(), name, toString(blockMap, loopHeaders));
|
|
1228 |
}
|
|
1229 |
}
|
|
1230 |
|
|
1231 |
public static String toString(BciBlock[] blockMap, BciBlock[] loopHeadersMap) {
|
|
1232 |
StringBuilder sb = new StringBuilder();
|
|
1233 |
for (BciBlock b : blockMap) {
|
|
1234 |
if (b == null) {
|
|
1235 |
continue;
|
|
1236 |
}
|
|
1237 |
sb.append("B").append(b.getId()).append("[").append(b.startBci).append("..").append(b.endBci).append("]");
|
|
1238 |
if (b.isLoopHeader) {
|
|
1239 |
sb.append(" LoopHeader");
|
43972
|
1240 |
}
|
49873
|
1241 |
if (b.isExceptionEntry) {
|
|
1242 |
sb.append(" ExceptionEntry");
|
|
1243 |
}
|
|
1244 |
if (b instanceof ExceptionDispatchBlock) {
|
|
1245 |
sb.append(" ExceptionDispatch");
|
|
1246 |
}
|
|
1247 |
if (!b.successors.isEmpty()) {
|
|
1248 |
sb.append(" Successors=[");
|
|
1249 |
for (BciBlock s : b.getSuccessors()) {
|
|
1250 |
if (sb.charAt(sb.length() - 1) != '[') {
|
|
1251 |
sb.append(", ");
|
|
1252 |
}
|
|
1253 |
sb.append("B").append(s.getId());
|
43972
|
1254 |
}
|
49873
|
1255 |
sb.append("]");
|
|
1256 |
}
|
|
1257 |
if (b.loops != 0L) {
|
|
1258 |
sb.append(" Loops=[");
|
|
1259 |
for (int pos : b.loopIdIterable()) {
|
|
1260 |
if (sb.charAt(sb.length() - 1) == '[') {
|
|
1261 |
sb.append(", ");
|
43972
|
1262 |
}
|
49873
|
1263 |
sb.append("B").append(loopHeadersMap[pos].getId());
|
43972
|
1264 |
}
|
49873
|
1265 |
sb.append("]");
|
43972
|
1266 |
}
|
49873
|
1267 |
sb.append(System.lineSeparator());
|
43972
|
1268 |
}
|
49873
|
1269 |
return sb.toString();
|
|
1270 |
}
|
|
1271 |
|
|
1272 |
@Override
|
|
1273 |
public String toString() {
|
|
1274 |
return toString(blocks, loopHeaders);
|
43972
|
1275 |
}
|
|
1276 |
|
|
1277 |
/**
|
|
1278 |
* Get the header block for a loop index.
|
|
1279 |
*/
|
|
1280 |
public BciBlock getLoopHeader(int index) {
|
|
1281 |
return loopHeaders[index];
|
|
1282 |
}
|
|
1283 |
|
|
1284 |
/**
|
|
1285 |
* The next available loop number.
|
|
1286 |
*/
|
|
1287 |
private int nextLoop;
|
|
1288 |
|
|
1289 |
/**
|
|
1290 |
* Mark the block as a loop header, using the next available loop number. Also checks for corner
|
|
1291 |
* cases that we don't want to compile.
|
|
1292 |
*/
|
|
1293 |
private void makeLoopHeader(BciBlock block) {
|
|
1294 |
if (!block.isLoopHeader) {
|
|
1295 |
block.isLoopHeader = true;
|
|
1296 |
|
|
1297 |
if (block.isExceptionEntry) {
|
|
1298 |
// Loops that are implicitly formed by an exception handler lead to all sorts of
|
|
1299 |
// corner cases.
|
|
1300 |
// Don't compile such methods for now, until we see a concrete case that allows
|
|
1301 |
// checking for correctness.
|
|
1302 |
throw new PermanentBailoutException("Loop formed by an exception handler");
|
|
1303 |
}
|
|
1304 |
if (nextLoop >= LOOP_HEADER_MAX_CAPACITY) {
|
|
1305 |
// This restriction can be removed by using a fall-back to a BitSet in case we have
|
|
1306 |
// more than 64 loops
|
|
1307 |
// Don't compile such methods for now, until we see a concrete case that allows
|
|
1308 |
// checking for correctness.
|
|
1309 |
throw new PermanentBailoutException("Too many loops in method");
|
|
1310 |
}
|
|
1311 |
|
|
1312 |
assert block.loops == 0;
|
|
1313 |
block.loops = 1L << nextLoop;
|
46640
|
1314 |
debug.log("makeLoopHeader(%s) -> %x", block, block.loops);
|
43972
|
1315 |
if (loopHeaders == null) {
|
|
1316 |
loopHeaders = new BciBlock[LOOP_HEADER_INITIAL_CAPACITY];
|
|
1317 |
} else if (nextLoop >= loopHeaders.length) {
|
|
1318 |
loopHeaders = Arrays.copyOf(loopHeaders, LOOP_HEADER_MAX_CAPACITY);
|
|
1319 |
}
|
|
1320 |
loopHeaders[nextLoop] = block;
|
|
1321 |
block.loopId = nextLoop;
|
|
1322 |
nextLoop++;
|
|
1323 |
}
|
|
1324 |
assert Long.bitCount(block.loops) == 1;
|
|
1325 |
}
|
|
1326 |
|
|
1327 |
/**
|
54724
|
1328 |
* Non-recursive depth-first traversal of the control flow graph. The flag
|
|
1329 |
* {@linkplain BciBlock#visited} is used to visit every block only once. The flag
|
|
1330 |
* {@linkplain BciBlock#active} is used to detect cycles (backward edges)
|
43972
|
1331 |
*/
|
54724
|
1332 |
private long computeBlockOrder(BciBlock initialBlock) {
|
|
1333 |
ArrayDeque<TraversalStep> workStack = new ArrayDeque<>();
|
|
1334 |
workStack.push(new TraversalStep(initialBlock));
|
|
1335 |
while (true) {
|
|
1336 |
TraversalStep step = workStack.peek();
|
|
1337 |
BciBlock block = step.block;
|
|
1338 |
if (step.currentSuccessorIndex == 0) {
|
|
1339 |
block.visited = true;
|
|
1340 |
block.active = true;
|
43972
|
1341 |
} else {
|
54724
|
1342 |
BciBlock successor = block.getSuccessor(step.currentSuccessorIndex - 1);
|
|
1343 |
if (successor.active) {
|
|
1344 |
// Reached block via backward branch.
|
|
1345 |
step.loops |= (1L << successor.loopId);
|
|
1346 |
}
|
43972
|
1347 |
}
|
54724
|
1348 |
if (step.currentSuccessorIndex < block.successors.size()) {
|
|
1349 |
BciBlock successor = block.getSuccessors().get(step.currentSuccessorIndex);
|
|
1350 |
if (successor.visited) {
|
|
1351 |
if (successor.active) {
|
|
1352 |
// Reached block via backward branch.
|
|
1353 |
makeLoopHeader(successor);
|
|
1354 |
step.loops |= successor.loops;
|
|
1355 |
} else if (successor.isLoopHeader) {
|
|
1356 |
step.loops |= successor.loops & ~(1L << successor.loopId);
|
|
1357 |
} else {
|
|
1358 |
step.loops |= successor.loops;
|
|
1359 |
}
|
|
1360 |
} else {
|
|
1361 |
workStack.push(new TraversalStep(successor));
|
|
1362 |
}
|
|
1363 |
step.currentSuccessorIndex++;
|
|
1364 |
} else {
|
|
1365 |
// We processed all the successors of this block.
|
|
1366 |
block.loops = step.loops;
|
|
1367 |
debug.log("computeBlockOrder(%s) -> %x", block, block.loops);
|
43972
|
1368 |
|
54724
|
1369 |
if (block.isLoopHeader) {
|
|
1370 |
step.loops &= ~(1L << block.loopId);
|
|
1371 |
}
|
|
1372 |
|
|
1373 |
block.active = false;
|
|
1374 |
blocksNotYetAssignedId--;
|
|
1375 |
blocks[blocksNotYetAssignedId] = block;
|
|
1376 |
|
|
1377 |
workStack.pop();
|
|
1378 |
if (!workStack.isEmpty()) {
|
|
1379 |
workStack.peek().loops |= step.loops;
|
|
1380 |
} else {
|
|
1381 |
return step.loops;
|
|
1382 |
}
|
43972
|
1383 |
}
|
|
1384 |
}
|
|
1385 |
}
|
|
1386 |
|
54724
|
1387 |
private long fixLoopBits(BciBlock initialBlock) {
|
|
1388 |
ArrayDeque<TraversalStep> workStack = new ArrayDeque<>();
|
|
1389 |
workStack.push(new TraversalStep(initialBlock));
|
|
1390 |
while (true) {
|
|
1391 |
TraversalStep step = workStack.peek();
|
|
1392 |
BciBlock block = step.block;
|
|
1393 |
if (step.currentSuccessorIndex == 0) {
|
|
1394 |
block.visited = true;
|
|
1395 |
step.loops = block.loops;
|
|
1396 |
}
|
|
1397 |
if (step.currentSuccessorIndex < block.getSuccessors().size()) {
|
|
1398 |
BciBlock successor = block.getSuccessors().get(step.currentSuccessorIndex);
|
|
1399 |
if (successor.visited) {
|
|
1400 |
// Return cached loop information for this block.
|
|
1401 |
if (successor.isLoopHeader) {
|
|
1402 |
step.loops |= successor.loops & ~(1L << successor.loopId);
|
|
1403 |
} else {
|
|
1404 |
step.loops |= successor.loops;
|
|
1405 |
}
|
|
1406 |
} else {
|
|
1407 |
workStack.push(new TraversalStep(successor));
|
|
1408 |
}
|
|
1409 |
step.currentSuccessorIndex++;
|
43972
|
1410 |
} else {
|
54724
|
1411 |
if (block.loops != step.loops) {
|
|
1412 |
loopChanges = true;
|
|
1413 |
block.loops = step.loops;
|
|
1414 |
debug.log("fixLoopBits0(%s) -> %x", block, block.loops);
|
|
1415 |
}
|
|
1416 |
|
|
1417 |
if (block.isLoopHeader) {
|
|
1418 |
step.loops &= ~(1L << block.loopId);
|
|
1419 |
}
|
|
1420 |
|
|
1421 |
workStack.pop();
|
|
1422 |
if (!workStack.isEmpty()) {
|
|
1423 |
workStack.peek().loops |= step.loops;
|
|
1424 |
} else {
|
|
1425 |
return step.loops;
|
|
1426 |
}
|
43972
|
1427 |
}
|
|
1428 |
}
|
|
1429 |
}
|
|
1430 |
|
46640
|
1431 |
public static BciBlockMapping create(BytecodeStream stream, Bytecode code, OptionValues options, DebugContext debug) {
|
|
1432 |
BciBlockMapping map = new BciBlockMapping(code, debug);
|
46344
|
1433 |
map.build(stream, options);
|
46640
|
1434 |
if (debug.isDumpEnabled(DebugContext.INFO_LEVEL)) {
|
|
1435 |
debug.dump(DebugContext.INFO_LEVEL, map, code.getMethod().format("After block building %f %R %H.%n(%P)"));
|
43972
|
1436 |
}
|
|
1437 |
|
|
1438 |
return map;
|
|
1439 |
}
|
|
1440 |
|
|
1441 |
public BciBlock[] getLoopHeaders() {
|
|
1442 |
return loopHeaders;
|
|
1443 |
}
|
|
1444 |
|
|
1445 |
public BciBlock getStartBlock() {
|
|
1446 |
return startBlock;
|
|
1447 |
}
|
|
1448 |
|
|
1449 |
public ExceptionDispatchBlock getUnwindBlock() {
|
|
1450 |
return (ExceptionDispatchBlock) blocks[blocks.length - 1];
|
|
1451 |
}
|
|
1452 |
|
|
1453 |
public int getLoopCount() {
|
|
1454 |
return nextLoop;
|
|
1455 |
}
|
|
1456 |
|
|
1457 |
public int getBlockCount() {
|
|
1458 |
return blocks.length;
|
|
1459 |
}
|
|
1460 |
}
|