43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
*/
|
|
23 |
package org.graalvm.compiler.java;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.bytecode.Bytecodes.AALOAD;
|
|
26 |
import static org.graalvm.compiler.bytecode.Bytecodes.AASTORE;
|
|
27 |
import static org.graalvm.compiler.bytecode.Bytecodes.ARETURN;
|
|
28 |
import static org.graalvm.compiler.bytecode.Bytecodes.ARRAYLENGTH;
|
|
29 |
import static org.graalvm.compiler.bytecode.Bytecodes.ATHROW;
|
|
30 |
import static org.graalvm.compiler.bytecode.Bytecodes.BALOAD;
|
|
31 |
import static org.graalvm.compiler.bytecode.Bytecodes.BASTORE;
|
|
32 |
import static org.graalvm.compiler.bytecode.Bytecodes.CALOAD;
|
|
33 |
import static org.graalvm.compiler.bytecode.Bytecodes.CASTORE;
|
|
34 |
import static org.graalvm.compiler.bytecode.Bytecodes.DALOAD;
|
|
35 |
import static org.graalvm.compiler.bytecode.Bytecodes.DASTORE;
|
|
36 |
import static org.graalvm.compiler.bytecode.Bytecodes.DRETURN;
|
|
37 |
import static org.graalvm.compiler.bytecode.Bytecodes.FALOAD;
|
|
38 |
import static org.graalvm.compiler.bytecode.Bytecodes.FASTORE;
|
|
39 |
import static org.graalvm.compiler.bytecode.Bytecodes.FRETURN;
|
|
40 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETFIELD;
|
|
41 |
import static org.graalvm.compiler.bytecode.Bytecodes.GOTO;
|
|
42 |
import static org.graalvm.compiler.bytecode.Bytecodes.GOTO_W;
|
|
43 |
import static org.graalvm.compiler.bytecode.Bytecodes.IALOAD;
|
|
44 |
import static org.graalvm.compiler.bytecode.Bytecodes.IASTORE;
|
|
45 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFEQ;
|
|
46 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFGE;
|
|
47 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFGT;
|
|
48 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFLE;
|
|
49 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFLT;
|
|
50 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNE;
|
|
51 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNONNULL;
|
|
52 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNULL;
|
|
53 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ACMPEQ;
|
|
54 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ACMPNE;
|
|
55 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPEQ;
|
|
56 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPGE;
|
|
57 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPGT;
|
|
58 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPLE;
|
|
59 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPLT;
|
|
60 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPNE;
|
|
61 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEDYNAMIC;
|
|
62 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEINTERFACE;
|
|
63 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESPECIAL;
|
|
64 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESTATIC;
|
|
65 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEVIRTUAL;
|
|
66 |
import static org.graalvm.compiler.bytecode.Bytecodes.IRETURN;
|
|
67 |
import static org.graalvm.compiler.bytecode.Bytecodes.JSR;
|
|
68 |
import static org.graalvm.compiler.bytecode.Bytecodes.JSR_W;
|
|
69 |
import static org.graalvm.compiler.bytecode.Bytecodes.LALOAD;
|
|
70 |
import static org.graalvm.compiler.bytecode.Bytecodes.LASTORE;
|
|
71 |
import static org.graalvm.compiler.bytecode.Bytecodes.LOOKUPSWITCH;
|
|
72 |
import static org.graalvm.compiler.bytecode.Bytecodes.LRETURN;
|
|
73 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTFIELD;
|
|
74 |
import static org.graalvm.compiler.bytecode.Bytecodes.RET;
|
|
75 |
import static org.graalvm.compiler.bytecode.Bytecodes.RETURN;
|
|
76 |
import static org.graalvm.compiler.bytecode.Bytecodes.SALOAD;
|
|
77 |
import static org.graalvm.compiler.bytecode.Bytecodes.SASTORE;
|
|
78 |
import static org.graalvm.compiler.bytecode.Bytecodes.TABLESWITCH;
|
|
79 |
import static org.graalvm.compiler.core.common.GraalOptions.SupportJsrBytecodes;
|
|
80 |
|
|
81 |
import java.util.ArrayList;
|
|
82 |
import java.util.Arrays;
|
|
83 |
import java.util.Collection;
|
|
84 |
import java.util.HashSet;
|
|
85 |
import java.util.Iterator;
|
|
86 |
import java.util.List;
|
|
87 |
import java.util.TreeSet;
|
|
88 |
|
48861
|
89 |
import org.graalvm.collections.EconomicMap;
|
|
90 |
import org.graalvm.collections.Equivalence;
|
43972
|
91 |
import org.graalvm.compiler.bytecode.Bytecode;
|
|
92 |
import org.graalvm.compiler.bytecode.BytecodeLookupSwitch;
|
|
93 |
import org.graalvm.compiler.bytecode.BytecodeStream;
|
|
94 |
import org.graalvm.compiler.bytecode.BytecodeSwitch;
|
|
95 |
import org.graalvm.compiler.bytecode.BytecodeTableSwitch;
|
|
96 |
import org.graalvm.compiler.bytecode.Bytecodes;
|
46344
|
97 |
import org.graalvm.compiler.core.common.PermanentBailoutException;
|
46640
|
98 |
import org.graalvm.compiler.debug.DebugContext;
|
46344
|
99 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
100 |
|
|
101 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
102 |
import jdk.vm.ci.meta.ExceptionHandler;
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Builds a mapping between bytecodes and basic blocks and builds a conservative control flow graph
|
|
106 |
* (CFG). It makes one linear passes over the bytecodes to build the CFG where it detects block
|
|
107 |
* headers and connects them.
|
|
108 |
* <p>
|
|
109 |
* It also creates exception dispatch blocks for exception handling. These blocks are between a
|
|
110 |
* bytecode that might throw an exception, and the actual exception handler entries, and are later
|
|
111 |
* used to create the type checks with the exception handler catch types. If a bytecode is covered
|
|
112 |
* by an exception handler, this bytecode ends the basic block. This guarantees that a) control flow
|
|
113 |
* cannot be transferred to an exception dispatch block in the middle of a block, and b) that every
|
|
114 |
* block has at most one exception dispatch block (which is always the last entry in the successor
|
|
115 |
* list).
|
|
116 |
* <p>
|
|
117 |
* If a bytecode is covered by multiple exception handlers, a chain of exception dispatch blocks is
|
|
118 |
* created so that multiple exception handler types can be checked. The chains are re-used if
|
|
119 |
* multiple bytecodes are covered by the same exception handlers.
|
|
120 |
* <p>
|
|
121 |
* Note that exception unwinds, i.e., bytecodes that can throw an exception but the exception is not
|
|
122 |
* handled in this method, do not end a basic block. Not modeling the exception unwind block reduces
|
|
123 |
* the complexity of the CFG, and there is no algorithm yet where the exception unwind block would
|
|
124 |
* matter.
|
|
125 |
* <p>
|
|
126 |
* The class also handles subroutines (jsr and ret bytecodes): subroutines are inlined by
|
|
127 |
* duplicating the subroutine blocks. This is limited to simple, structured subroutines with a
|
|
128 |
* maximum subroutine nesting of 4. Otherwise, a bailout is thrown.
|
|
129 |
* <p>
|
|
130 |
* Loops in the methods are detected. If a method contains an irreducible loop (a loop with more
|
|
131 |
* than one entry), a bailout is thrown. This simplifies the compiler later on since only structured
|
|
132 |
* loops need to be supported.
|
|
133 |
* <p>
|
|
134 |
* A data flow analysis computes the live local variables from the point of view of the interpreter.
|
|
135 |
* The result is used later to prune frame states, i.e., remove local variable entries that are
|
|
136 |
* guaranteed to be never used again (even in the case of deoptimization).
|
|
137 |
* <p>
|
|
138 |
* The algorithms and analysis in this class are conservative and do not use any assumptions or
|
|
139 |
* profiling information.
|
|
140 |
*/
|
|
141 |
public final class BciBlockMapping {
|
|
142 |
|
|
143 |
public static class BciBlock implements Cloneable {
|
|
144 |
|
|
145 |
protected int id;
|
|
146 |
public int startBci;
|
|
147 |
public int endBci;
|
|
148 |
public boolean isExceptionEntry;
|
|
149 |
public boolean isLoopHeader;
|
|
150 |
public int loopId;
|
|
151 |
public int loopEnd;
|
|
152 |
protected List<BciBlock> successors;
|
|
153 |
private int predecessorCount;
|
|
154 |
|
|
155 |
private boolean visited;
|
|
156 |
private boolean active;
|
|
157 |
public long loops;
|
|
158 |
public JSRData jsrData;
|
|
159 |
|
|
160 |
public static class JSRData implements Cloneable {
|
46344
|
161 |
public EconomicMap<JsrScope, BciBlock> jsrAlternatives;
|
43972
|
162 |
public JsrScope jsrScope = JsrScope.EMPTY_SCOPE;
|
|
163 |
public BciBlock jsrSuccessor;
|
|
164 |
public int jsrReturnBci;
|
|
165 |
public BciBlock retSuccessor;
|
|
166 |
public boolean endsWithRet = false;
|
|
167 |
|
|
168 |
public JSRData copy() {
|
|
169 |
try {
|
|
170 |
return (JSRData) this.clone();
|
|
171 |
} catch (CloneNotSupportedException e) {
|
|
172 |
return null;
|
|
173 |
}
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
public BciBlock() {
|
|
178 |
this.successors = new ArrayList<>(4);
|
|
179 |
}
|
|
180 |
|
|
181 |
public BciBlock exceptionDispatchBlock() {
|
|
182 |
if (successors.size() > 0 && successors.get(successors.size() - 1) instanceof ExceptionDispatchBlock) {
|
|
183 |
return successors.get(successors.size() - 1);
|
|
184 |
}
|
|
185 |
return null;
|
|
186 |
}
|
|
187 |
|
|
188 |
public int getId() {
|
|
189 |
return id;
|
|
190 |
}
|
|
191 |
|
|
192 |
public int getPredecessorCount() {
|
|
193 |
return this.predecessorCount;
|
|
194 |
}
|
|
195 |
|
|
196 |
public int numNormalSuccessors() {
|
|
197 |
if (exceptionDispatchBlock() != null) {
|
|
198 |
return successors.size() - 1;
|
|
199 |
}
|
|
200 |
return successors.size();
|
|
201 |
}
|
|
202 |
|
|
203 |
public BciBlock copy() {
|
|
204 |
try {
|
|
205 |
BciBlock block = (BciBlock) super.clone();
|
|
206 |
if (block.jsrData != null) {
|
|
207 |
block.jsrData = block.jsrData.copy();
|
|
208 |
}
|
|
209 |
block.successors = new ArrayList<>(successors);
|
|
210 |
return block;
|
|
211 |
} catch (CloneNotSupportedException e) {
|
|
212 |
throw new RuntimeException(e);
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
@Override
|
|
217 |
public String toString() {
|
|
218 |
StringBuilder sb = new StringBuilder("B").append(getId());
|
|
219 |
sb.append('[').append(startBci).append("->").append(endBci);
|
|
220 |
if (isLoopHeader || isExceptionEntry) {
|
|
221 |
sb.append(' ');
|
|
222 |
if (isLoopHeader) {
|
|
223 |
sb.append('L');
|
|
224 |
}
|
|
225 |
if (isExceptionEntry) {
|
|
226 |
sb.append('!');
|
|
227 |
}
|
|
228 |
}
|
|
229 |
sb.append(']');
|
|
230 |
return sb.toString();
|
|
231 |
}
|
|
232 |
|
|
233 |
public int getLoopDepth() {
|
|
234 |
return Long.bitCount(loops);
|
|
235 |
}
|
|
236 |
|
|
237 |
public boolean isLoopHeader() {
|
|
238 |
return isLoopHeader;
|
|
239 |
}
|
|
240 |
|
|
241 |
public boolean isExceptionEntry() {
|
|
242 |
return isExceptionEntry;
|
|
243 |
}
|
|
244 |
|
|
245 |
public BciBlock getSuccessor(int index) {
|
|
246 |
return successors.get(index);
|
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Get the loop id of the inner most loop.
|
|
251 |
*
|
|
252 |
* @return the loop id of the most inner loop or -1 if not part of any loop
|
|
253 |
*/
|
|
254 |
public int getLoopId() {
|
|
255 |
long l = loops;
|
|
256 |
if (l == 0) {
|
|
257 |
return -1;
|
|
258 |
}
|
|
259 |
int pos = 0;
|
|
260 |
for (int lMask = 1; (l & lMask) == 0; lMask = lMask << 1) {
|
|
261 |
pos++;
|
|
262 |
}
|
|
263 |
return pos;
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Iterate over loop ids.
|
|
268 |
*/
|
|
269 |
public Iterable<Integer> loopIdIterable() {
|
|
270 |
return new Iterable<Integer>() {
|
|
271 |
@Override
|
|
272 |
public Iterator<Integer> iterator() {
|
|
273 |
return idIterator(loops);
|
|
274 |
}
|
|
275 |
};
|
|
276 |
}
|
|
277 |
|
|
278 |
private static Iterator<Integer> idIterator(long field) {
|
|
279 |
return new Iterator<Integer>() {
|
|
280 |
|
|
281 |
long l = field;
|
|
282 |
int pos = 0;
|
|
283 |
int lMask = 1;
|
|
284 |
|
|
285 |
@Override
|
|
286 |
public Integer next() {
|
|
287 |
for (; (l & lMask) == 0; lMask = lMask << 1) {
|
|
288 |
pos++;
|
|
289 |
}
|
|
290 |
l &= ~lMask;
|
|
291 |
return pos;
|
|
292 |
}
|
|
293 |
|
|
294 |
@Override
|
|
295 |
public boolean hasNext() {
|
|
296 |
return l != 0;
|
|
297 |
}
|
|
298 |
};
|
|
299 |
|
|
300 |
}
|
|
301 |
|
|
302 |
public double probability() {
|
|
303 |
return 1D;
|
|
304 |
}
|
|
305 |
|
|
306 |
public BciBlock getPostdominator() {
|
|
307 |
return null;
|
|
308 |
}
|
|
309 |
|
|
310 |
private JSRData getOrCreateJSRData() {
|
|
311 |
if (jsrData == null) {
|
|
312 |
jsrData = new JSRData();
|
|
313 |
}
|
|
314 |
return jsrData;
|
|
315 |
}
|
|
316 |
|
|
317 |
void setEndsWithRet() {
|
|
318 |
getOrCreateJSRData().endsWithRet = true;
|
|
319 |
}
|
|
320 |
|
|
321 |
public JsrScope getJsrScope() {
|
|
322 |
if (this.jsrData == null) {
|
|
323 |
return JsrScope.EMPTY_SCOPE;
|
|
324 |
} else {
|
|
325 |
return jsrData.jsrScope;
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
|
329 |
public boolean endsWithRet() {
|
|
330 |
if (this.jsrData == null) {
|
|
331 |
return false;
|
|
332 |
} else {
|
|
333 |
return jsrData.endsWithRet;
|
|
334 |
}
|
|
335 |
}
|
|
336 |
|
|
337 |
void setRetSuccessor(BciBlock bciBlock) {
|
|
338 |
this.getOrCreateJSRData().retSuccessor = bciBlock;
|
|
339 |
}
|
|
340 |
|
|
341 |
public BciBlock getRetSuccessor() {
|
|
342 |
if (this.jsrData == null) {
|
|
343 |
return null;
|
|
344 |
} else {
|
|
345 |
return jsrData.retSuccessor;
|
|
346 |
}
|
|
347 |
}
|
|
348 |
|
|
349 |
public BciBlock getJsrSuccessor() {
|
|
350 |
if (this.jsrData == null) {
|
|
351 |
return null;
|
|
352 |
} else {
|
|
353 |
return jsrData.jsrSuccessor;
|
|
354 |
}
|
|
355 |
}
|
|
356 |
|
|
357 |
public int getJsrReturnBci() {
|
|
358 |
if (this.jsrData == null) {
|
|
359 |
return -1;
|
|
360 |
} else {
|
|
361 |
return jsrData.jsrReturnBci;
|
|
362 |
}
|
|
363 |
}
|
|
364 |
|
46344
|
365 |
public EconomicMap<JsrScope, BciBlock> getJsrAlternatives() {
|
43972
|
366 |
if (this.jsrData == null) {
|
|
367 |
return null;
|
|
368 |
} else {
|
|
369 |
return jsrData.jsrAlternatives;
|
|
370 |
}
|
|
371 |
}
|
|
372 |
|
|
373 |
public void initJsrAlternatives() {
|
|
374 |
JSRData data = this.getOrCreateJSRData();
|
|
375 |
if (data.jsrAlternatives == null) {
|
46344
|
376 |
data.jsrAlternatives = EconomicMap.create(Equivalence.DEFAULT);
|
43972
|
377 |
}
|
|
378 |
}
|
|
379 |
|
|
380 |
void setJsrScope(JsrScope nextScope) {
|
|
381 |
this.getOrCreateJSRData().jsrScope = nextScope;
|
|
382 |
}
|
|
383 |
|
|
384 |
void setJsrSuccessor(BciBlock clone) {
|
|
385 |
this.getOrCreateJSRData().jsrSuccessor = clone;
|
|
386 |
}
|
|
387 |
|
|
388 |
void setJsrReturnBci(int bci) {
|
|
389 |
this.getOrCreateJSRData().jsrReturnBci = bci;
|
|
390 |
}
|
|
391 |
|
|
392 |
public int getSuccessorCount() {
|
|
393 |
return successors.size();
|
|
394 |
}
|
|
395 |
|
|
396 |
public List<BciBlock> getSuccessors() {
|
|
397 |
return successors;
|
|
398 |
}
|
|
399 |
|
|
400 |
void setId(int i) {
|
|
401 |
this.id = i;
|
|
402 |
}
|
|
403 |
|
|
404 |
public void addSuccessor(BciBlock sux) {
|
|
405 |
successors.add(sux);
|
|
406 |
sux.predecessorCount++;
|
|
407 |
}
|
|
408 |
|
|
409 |
public void clearSucccessors() {
|
|
410 |
for (BciBlock sux : successors) {
|
|
411 |
sux.predecessorCount--;
|
|
412 |
}
|
|
413 |
successors.clear();
|
|
414 |
}
|
|
415 |
}
|
|
416 |
|
|
417 |
public static class ExceptionDispatchBlock extends BciBlock {
|
|
418 |
public ExceptionHandler handler;
|
|
419 |
public int deoptBci;
|
|
420 |
}
|
|
421 |
|
|
422 |
/**
|
|
423 |
* The blocks found in this method, in reverse postorder.
|
|
424 |
*/
|
|
425 |
private BciBlock[] blocks;
|
|
426 |
public final Bytecode code;
|
|
427 |
public boolean hasJsrBytecodes;
|
|
428 |
|
|
429 |
private final ExceptionHandler[] exceptionHandlers;
|
|
430 |
private BciBlock startBlock;
|
|
431 |
private BciBlock[] loopHeaders;
|
|
432 |
|
|
433 |
private static final int LOOP_HEADER_MAX_CAPACITY = Long.SIZE;
|
|
434 |
private static final int LOOP_HEADER_INITIAL_CAPACITY = 4;
|
|
435 |
|
|
436 |
private int blocksNotYetAssignedId;
|
46640
|
437 |
private final DebugContext debug;
|
43972
|
438 |
|
|
439 |
/**
|
|
440 |
* Creates a new BlockMap instance from {@code code}.
|
|
441 |
*/
|
46640
|
442 |
private BciBlockMapping(Bytecode code, DebugContext debug) {
|
43972
|
443 |
this.code = code;
|
46640
|
444 |
this.debug = debug;
|
43972
|
445 |
this.exceptionHandlers = code.getExceptionHandlers();
|
|
446 |
}
|
|
447 |
|
|
448 |
public BciBlock[] getBlocks() {
|
|
449 |
return this.blocks;
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
|
453 |
* Builds the block map and conservative CFG and numbers blocks.
|
|
454 |
*/
|
46344
|
455 |
public void build(BytecodeStream stream, OptionValues options) {
|
43972
|
456 |
int codeSize = code.getCodeSize();
|
|
457 |
BciBlock[] blockMap = new BciBlock[codeSize];
|
|
458 |
makeExceptionEntries(blockMap);
|
|
459 |
iterateOverBytecodes(blockMap, stream);
|
|
460 |
if (hasJsrBytecodes) {
|
46344
|
461 |
if (!SupportJsrBytecodes.getValue(options)) {
|
43972
|
462 |
throw new JsrNotSupportedBailout("jsr/ret parsing disabled");
|
|
463 |
}
|
|
464 |
createJsrAlternatives(blockMap, blockMap[0]);
|
|
465 |
}
|
46640
|
466 |
if (debug.isLogEnabled()) {
|
43972
|
467 |
this.log(blockMap, "Before BlockOrder");
|
|
468 |
}
|
|
469 |
computeBlockOrder(blockMap);
|
|
470 |
fixLoopBits(blockMap);
|
|
471 |
|
|
472 |
assert verify();
|
|
473 |
|
|
474 |
startBlock = blockMap[0];
|
46640
|
475 |
if (debug.isLogEnabled()) {
|
43972
|
476 |
this.log(blockMap, "Before LivenessAnalysis");
|
|
477 |
}
|
|
478 |
}
|
|
479 |
|
|
480 |
private boolean verify() {
|
|
481 |
for (BciBlock block : blocks) {
|
|
482 |
assert blocks[block.getId()] == block;
|
|
483 |
|
|
484 |
for (int i = 0; i < block.getSuccessorCount(); i++) {
|
|
485 |
BciBlock sux = block.getSuccessor(i);
|
|
486 |
if (sux instanceof ExceptionDispatchBlock) {
|
|
487 |
assert i == block.getSuccessorCount() - 1 : "Only one exception handler allowed, and it must be last in successors list";
|
|
488 |
}
|
|
489 |
}
|
|
490 |
}
|
|
491 |
|
|
492 |
return true;
|
|
493 |
}
|
|
494 |
|
|
495 |
private void makeExceptionEntries(BciBlock[] blockMap) {
|
|
496 |
// start basic blocks at all exception handler blocks and mark them as exception entries
|
|
497 |
for (ExceptionHandler h : this.exceptionHandlers) {
|
|
498 |
BciBlock xhandler = makeBlock(blockMap, h.getHandlerBCI());
|
|
499 |
xhandler.isExceptionEntry = true;
|
|
500 |
}
|
|
501 |
}
|
|
502 |
|
|
503 |
private void iterateOverBytecodes(BciBlock[] blockMap, BytecodeStream stream) {
|
|
504 |
// iterate over the bytecodes top to bottom.
|
|
505 |
// mark the entrypoints of basic blocks and build lists of successors for
|
|
506 |
// all bytecodes that end basic blocks (i.e. goto, ifs, switches, throw, jsr, returns, ret)
|
|
507 |
BciBlock current = null;
|
|
508 |
stream.setBCI(0);
|
|
509 |
while (stream.currentBC() != Bytecodes.END) {
|
|
510 |
int bci = stream.currentBCI();
|
|
511 |
|
|
512 |
if (current == null || blockMap[bci] != null) {
|
|
513 |
BciBlock b = makeBlock(blockMap, bci);
|
|
514 |
if (current != null) {
|
|
515 |
addSuccessor(blockMap, current.endBci, b);
|
|
516 |
}
|
|
517 |
current = b;
|
|
518 |
}
|
|
519 |
blockMap[bci] = current;
|
|
520 |
current.endBci = bci;
|
|
521 |
|
|
522 |
switch (stream.currentBC()) {
|
|
523 |
case IRETURN: // fall through
|
|
524 |
case LRETURN: // fall through
|
|
525 |
case FRETURN: // fall through
|
|
526 |
case DRETURN: // fall through
|
|
527 |
case ARETURN: // fall through
|
|
528 |
case RETURN: {
|
|
529 |
current = null;
|
|
530 |
break;
|
|
531 |
}
|
|
532 |
case ATHROW: {
|
|
533 |
current = null;
|
|
534 |
ExceptionDispatchBlock handler = handleExceptions(blockMap, bci);
|
|
535 |
if (handler != null) {
|
|
536 |
addSuccessor(blockMap, bci, handler);
|
|
537 |
}
|
|
538 |
break;
|
|
539 |
}
|
|
540 |
case IFEQ: // fall through
|
|
541 |
case IFNE: // fall through
|
|
542 |
case IFLT: // fall through
|
|
543 |
case IFGE: // fall through
|
|
544 |
case IFGT: // fall through
|
|
545 |
case IFLE: // fall through
|
|
546 |
case IF_ICMPEQ: // fall through
|
|
547 |
case IF_ICMPNE: // fall through
|
|
548 |
case IF_ICMPLT: // fall through
|
|
549 |
case IF_ICMPGE: // fall through
|
|
550 |
case IF_ICMPGT: // fall through
|
|
551 |
case IF_ICMPLE: // fall through
|
|
552 |
case IF_ACMPEQ: // fall through
|
|
553 |
case IF_ACMPNE: // fall through
|
|
554 |
case IFNULL: // fall through
|
|
555 |
case IFNONNULL: {
|
|
556 |
current = null;
|
|
557 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.readBranchDest()));
|
|
558 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.nextBCI()));
|
|
559 |
break;
|
|
560 |
}
|
|
561 |
case GOTO:
|
|
562 |
case GOTO_W: {
|
|
563 |
current = null;
|
|
564 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.readBranchDest()));
|
|
565 |
break;
|
|
566 |
}
|
|
567 |
case TABLESWITCH: {
|
|
568 |
current = null;
|
|
569 |
addSwitchSuccessors(blockMap, bci, new BytecodeTableSwitch(stream, bci));
|
|
570 |
break;
|
|
571 |
}
|
|
572 |
case LOOKUPSWITCH: {
|
|
573 |
current = null;
|
|
574 |
addSwitchSuccessors(blockMap, bci, new BytecodeLookupSwitch(stream, bci));
|
|
575 |
break;
|
|
576 |
}
|
|
577 |
case JSR:
|
|
578 |
case JSR_W: {
|
|
579 |
hasJsrBytecodes = true;
|
|
580 |
int target = stream.readBranchDest();
|
|
581 |
if (target == 0) {
|
|
582 |
throw new JsrNotSupportedBailout("jsr target bci 0 not allowed");
|
|
583 |
}
|
|
584 |
BciBlock b1 = makeBlock(blockMap, target);
|
|
585 |
current.setJsrSuccessor(b1);
|
|
586 |
current.setJsrReturnBci(stream.nextBCI());
|
|
587 |
current = null;
|
|
588 |
addSuccessor(blockMap, bci, b1);
|
|
589 |
break;
|
|
590 |
}
|
|
591 |
case RET: {
|
|
592 |
current.setEndsWithRet();
|
|
593 |
current = null;
|
|
594 |
break;
|
|
595 |
}
|
|
596 |
case INVOKEINTERFACE:
|
|
597 |
case INVOKESPECIAL:
|
|
598 |
case INVOKESTATIC:
|
|
599 |
case INVOKEVIRTUAL:
|
|
600 |
case INVOKEDYNAMIC: {
|
|
601 |
current = null;
|
|
602 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.nextBCI()));
|
|
603 |
ExceptionDispatchBlock handler = handleExceptions(blockMap, bci);
|
|
604 |
if (handler != null) {
|
|
605 |
addSuccessor(blockMap, bci, handler);
|
|
606 |
}
|
|
607 |
break;
|
|
608 |
}
|
|
609 |
case IASTORE:
|
|
610 |
case LASTORE:
|
|
611 |
case FASTORE:
|
|
612 |
case DASTORE:
|
|
613 |
case AASTORE:
|
|
614 |
case BASTORE:
|
|
615 |
case CASTORE:
|
|
616 |
case SASTORE:
|
|
617 |
case IALOAD:
|
|
618 |
case LALOAD:
|
|
619 |
case FALOAD:
|
|
620 |
case DALOAD:
|
|
621 |
case AALOAD:
|
|
622 |
case BALOAD:
|
|
623 |
case CALOAD:
|
|
624 |
case SALOAD:
|
|
625 |
case ARRAYLENGTH:
|
|
626 |
case PUTFIELD:
|
|
627 |
case GETFIELD: {
|
|
628 |
ExceptionDispatchBlock handler = handleExceptions(blockMap, bci);
|
|
629 |
if (handler != null) {
|
|
630 |
current = null;
|
|
631 |
addSuccessor(blockMap, bci, makeBlock(blockMap, stream.nextBCI()));
|
|
632 |
addSuccessor(blockMap, bci, handler);
|
|
633 |
}
|
|
634 |
}
|
|
635 |
}
|
|
636 |
stream.next();
|
|
637 |
}
|
|
638 |
}
|
|
639 |
|
|
640 |
private BciBlock makeBlock(BciBlock[] blockMap, int startBci) {
|
|
641 |
BciBlock oldBlock = blockMap[startBci];
|
|
642 |
if (oldBlock == null) {
|
|
643 |
BciBlock newBlock = new BciBlock();
|
|
644 |
blocksNotYetAssignedId++;
|
|
645 |
newBlock.startBci = startBci;
|
|
646 |
blockMap[startBci] = newBlock;
|
|
647 |
return newBlock;
|
|
648 |
|
|
649 |
} else if (oldBlock.startBci != startBci) {
|
|
650 |
// Backward branch into the middle of an already processed block.
|
|
651 |
// Add the correct fall-through successor.
|
|
652 |
BciBlock newBlock = new BciBlock();
|
|
653 |
blocksNotYetAssignedId++;
|
|
654 |
newBlock.startBci = startBci;
|
|
655 |
newBlock.endBci = oldBlock.endBci;
|
|
656 |
for (BciBlock oldSuccessor : oldBlock.getSuccessors()) {
|
|
657 |
newBlock.addSuccessor(oldSuccessor);
|
|
658 |
}
|
|
659 |
|
|
660 |
oldBlock.endBci = startBci - 1;
|
|
661 |
oldBlock.clearSucccessors();
|
|
662 |
oldBlock.addSuccessor(newBlock);
|
|
663 |
|
|
664 |
for (int i = startBci; i <= newBlock.endBci; i++) {
|
|
665 |
blockMap[i] = newBlock;
|
|
666 |
}
|
|
667 |
return newBlock;
|
|
668 |
|
|
669 |
} else {
|
|
670 |
return oldBlock;
|
|
671 |
}
|
|
672 |
}
|
|
673 |
|
|
674 |
private void addSwitchSuccessors(BciBlock[] blockMap, int predBci, BytecodeSwitch bswitch) {
|
|
675 |
// adds distinct targets to the successor list
|
|
676 |
Collection<Integer> targets = new TreeSet<>();
|
|
677 |
for (int i = 0; i < bswitch.numberOfCases(); i++) {
|
|
678 |
targets.add(bswitch.targetAt(i));
|
|
679 |
}
|
|
680 |
targets.add(bswitch.defaultTarget());
|
|
681 |
for (int targetBci : targets) {
|
|
682 |
addSuccessor(blockMap, predBci, makeBlock(blockMap, targetBci));
|
|
683 |
}
|
|
684 |
}
|
|
685 |
|
|
686 |
private static void addSuccessor(BciBlock[] blockMap, int predBci, BciBlock sux) {
|
|
687 |
BciBlock predecessor = blockMap[predBci];
|
|
688 |
if (sux.isExceptionEntry) {
|
|
689 |
throw new PermanentBailoutException("Exception handler can be reached by both normal and exceptional control flow");
|
|
690 |
}
|
|
691 |
predecessor.addSuccessor(sux);
|
|
692 |
}
|
|
693 |
|
|
694 |
private final ArrayList<BciBlock> jsrVisited = new ArrayList<>();
|
|
695 |
|
|
696 |
private void createJsrAlternatives(BciBlock[] blockMap, BciBlock block) {
|
|
697 |
jsrVisited.add(block);
|
|
698 |
JsrScope scope = block.getJsrScope();
|
|
699 |
|
|
700 |
if (block.endsWithRet()) {
|
|
701 |
block.setRetSuccessor(blockMap[scope.nextReturnAddress()]);
|
|
702 |
block.addSuccessor(block.getRetSuccessor());
|
|
703 |
assert block.getRetSuccessor() != block.getJsrSuccessor();
|
|
704 |
}
|
46640
|
705 |
debug.log("JSR alternatives block %s sux %s jsrSux %s retSux %s jsrScope %s", block, block.getSuccessors(), block.getJsrSuccessor(), block.getRetSuccessor(), block.getJsrScope());
|
43972
|
706 |
|
|
707 |
if (block.getJsrSuccessor() != null || !scope.isEmpty()) {
|
|
708 |
for (int i = 0; i < block.getSuccessorCount(); i++) {
|
|
709 |
BciBlock successor = block.getSuccessor(i);
|
|
710 |
JsrScope nextScope = scope;
|
|
711 |
if (successor == block.getJsrSuccessor()) {
|
|
712 |
nextScope = scope.push(block.getJsrReturnBci());
|
|
713 |
}
|
|
714 |
if (successor == block.getRetSuccessor()) {
|
|
715 |
nextScope = scope.pop();
|
|
716 |
}
|
|
717 |
if (!successor.getJsrScope().isPrefixOf(nextScope)) {
|
|
718 |
throw new JsrNotSupportedBailout("unstructured control flow (" + successor.getJsrScope() + " " + nextScope + ")");
|
|
719 |
}
|
|
720 |
if (!nextScope.isEmpty()) {
|
|
721 |
BciBlock clone;
|
|
722 |
if (successor.getJsrAlternatives() != null && successor.getJsrAlternatives().containsKey(nextScope)) {
|
|
723 |
clone = successor.getJsrAlternatives().get(nextScope);
|
|
724 |
} else {
|
|
725 |
successor.initJsrAlternatives();
|
|
726 |
clone = successor.copy();
|
|
727 |
blocksNotYetAssignedId++;
|
|
728 |
clone.setJsrScope(nextScope);
|
|
729 |
successor.getJsrAlternatives().put(nextScope, clone);
|
|
730 |
}
|
|
731 |
block.getSuccessors().set(i, clone);
|
|
732 |
if (successor == block.getJsrSuccessor()) {
|
|
733 |
block.setJsrSuccessor(clone);
|
|
734 |
}
|
|
735 |
if (successor == block.getRetSuccessor()) {
|
|
736 |
block.setRetSuccessor(clone);
|
|
737 |
}
|
|
738 |
}
|
|
739 |
}
|
|
740 |
}
|
|
741 |
for (BciBlock successor : block.getSuccessors()) {
|
|
742 |
if (!jsrVisited.contains(successor)) {
|
|
743 |
createJsrAlternatives(blockMap, successor);
|
|
744 |
}
|
|
745 |
}
|
|
746 |
}
|
|
747 |
|
|
748 |
private ExceptionDispatchBlock handleExceptions(BciBlock[] blockMap, int bci) {
|
|
749 |
ExceptionDispatchBlock lastHandler = null;
|
|
750 |
|
|
751 |
for (int i = exceptionHandlers.length - 1; i >= 0; i--) {
|
|
752 |
ExceptionHandler h = exceptionHandlers[i];
|
|
753 |
if (h.getStartBCI() <= bci && bci < h.getEndBCI()) {
|
|
754 |
if (h.isCatchAll()) {
|
|
755 |
// Discard all information about succeeding exception handlers, since they can
|
|
756 |
// never be reached.
|
|
757 |
lastHandler = null;
|
|
758 |
}
|
|
759 |
|
48861
|
760 |
// We do not reuse exception dispatch blocks, because nested exception handlers
|
|
761 |
// might have problems reasoning about the correct frame state.
|
|
762 |
ExceptionDispatchBlock curHandler = new ExceptionDispatchBlock();
|
|
763 |
blocksNotYetAssignedId++;
|
|
764 |
curHandler.startBci = -1;
|
|
765 |
curHandler.endBci = -1;
|
|
766 |
curHandler.deoptBci = bci;
|
|
767 |
curHandler.handler = h;
|
|
768 |
curHandler.addSuccessor(blockMap[h.getHandlerBCI()]);
|
|
769 |
if (lastHandler != null) {
|
|
770 |
curHandler.addSuccessor(lastHandler);
|
43972
|
771 |
}
|
|
772 |
lastHandler = curHandler;
|
|
773 |
}
|
|
774 |
}
|
|
775 |
return lastHandler;
|
|
776 |
}
|
|
777 |
|
|
778 |
private boolean loopChanges;
|
|
779 |
|
|
780 |
private void fixLoopBits(BciBlock[] blockMap) {
|
|
781 |
do {
|
|
782 |
loopChanges = false;
|
|
783 |
for (BciBlock b : blocks) {
|
|
784 |
b.visited = false;
|
|
785 |
}
|
|
786 |
|
|
787 |
long loop = fixLoopBits(blockMap, blockMap[0]);
|
|
788 |
|
|
789 |
if (loop != 0) {
|
|
790 |
// There is a path from a loop end to the method entry that does not pass the loop
|
|
791 |
// header.
|
|
792 |
// Therefore, the loop is non reducible (has more than one entry).
|
|
793 |
// We don't want to compile such methods because the IR only supports structured
|
|
794 |
// loops.
|
|
795 |
throw new PermanentBailoutException("Non-reducible loop: %016x", loop);
|
|
796 |
}
|
|
797 |
} while (loopChanges);
|
|
798 |
}
|
|
799 |
|
|
800 |
private void computeBlockOrder(BciBlock[] blockMap) {
|
|
801 |
int maxBlocks = blocksNotYetAssignedId;
|
|
802 |
this.blocks = new BciBlock[blocksNotYetAssignedId];
|
|
803 |
long loop = computeBlockOrder(blockMap[0]);
|
|
804 |
|
|
805 |
if (loop != 0) {
|
|
806 |
// There is a path from a loop end to the method entry that does not pass the loop
|
|
807 |
// header. Therefore, the loop is non reducible (has more than one entry).
|
|
808 |
// We don't want to compile such methods because the IR only supports structured loops.
|
|
809 |
throw new PermanentBailoutException("Non-reducible loop");
|
|
810 |
}
|
|
811 |
|
|
812 |
// Purge null entries for unreached blocks and sort blocks such that loop bodies are always
|
|
813 |
// consecutively in the array.
|
46393
|
814 |
int blockCount = maxBlocks - blocksNotYetAssignedId + 1;
|
43972
|
815 |
BciBlock[] newBlocks = new BciBlock[blockCount];
|
|
816 |
int next = 0;
|
|
817 |
for (int i = 0; i < blocks.length; ++i) {
|
|
818 |
BciBlock b = blocks[i];
|
|
819 |
if (b != null) {
|
|
820 |
b.setId(next);
|
|
821 |
newBlocks[next++] = b;
|
|
822 |
if (b.isLoopHeader) {
|
|
823 |
next = handleLoopHeader(newBlocks, next, i, b);
|
|
824 |
}
|
|
825 |
}
|
|
826 |
}
|
46393
|
827 |
assert next == newBlocks.length - 1;
|
43972
|
828 |
|
|
829 |
// Add unwind block.
|
|
830 |
ExceptionDispatchBlock unwindBlock = new ExceptionDispatchBlock();
|
|
831 |
unwindBlock.startBci = -1;
|
|
832 |
unwindBlock.endBci = -1;
|
|
833 |
unwindBlock.deoptBci = code.getMethod().isSynchronized() ? BytecodeFrame.UNWIND_BCI : BytecodeFrame.AFTER_EXCEPTION_BCI;
|
|
834 |
unwindBlock.setId(newBlocks.length - 1);
|
|
835 |
newBlocks[newBlocks.length - 1] = unwindBlock;
|
|
836 |
|
|
837 |
blocks = newBlocks;
|
|
838 |
}
|
|
839 |
|
|
840 |
private int handleLoopHeader(BciBlock[] newBlocks, int nextStart, int i, BciBlock loopHeader) {
|
|
841 |
int next = nextStart;
|
|
842 |
int endOfLoop = nextStart - 1;
|
|
843 |
for (int j = i + 1; j < blocks.length; ++j) {
|
|
844 |
BciBlock other = blocks[j];
|
|
845 |
if (other != null && (other.loops & (1L << loopHeader.loopId)) != 0) {
|
|
846 |
other.setId(next);
|
|
847 |
endOfLoop = next;
|
|
848 |
newBlocks[next++] = other;
|
|
849 |
blocks[j] = null;
|
|
850 |
if (other.isLoopHeader) {
|
|
851 |
next = handleLoopHeader(newBlocks, next, j, other);
|
|
852 |
}
|
|
853 |
}
|
|
854 |
}
|
|
855 |
loopHeader.loopEnd = endOfLoop;
|
|
856 |
return next;
|
|
857 |
}
|
|
858 |
|
|
859 |
public void log(BciBlock[] blockMap, String name) {
|
46640
|
860 |
if (debug.isLogEnabled()) {
|
43972
|
861 |
String n = System.lineSeparator();
|
46640
|
862 |
StringBuilder sb = new StringBuilder(debug.getCurrentScopeName()).append("BlockMap ").append(name).append(" :");
|
43972
|
863 |
sb.append(n);
|
|
864 |
Iterable<BciBlock> it;
|
|
865 |
if (blocks == null) {
|
|
866 |
it = new HashSet<>(Arrays.asList(blockMap));
|
|
867 |
} else {
|
|
868 |
it = Arrays.asList(blocks);
|
|
869 |
}
|
|
870 |
for (BciBlock b : it) {
|
|
871 |
if (b == null) {
|
|
872 |
continue;
|
|
873 |
}
|
|
874 |
sb.append("B").append(b.getId()).append(" (").append(b.startBci).append(" -> ").append(b.endBci).append(")");
|
|
875 |
if (b.isLoopHeader) {
|
|
876 |
sb.append(" LoopHeader");
|
|
877 |
}
|
|
878 |
if (b.isExceptionEntry) {
|
|
879 |
sb.append(" ExceptionEntry");
|
|
880 |
}
|
|
881 |
sb.append(n).append(" Sux : ");
|
|
882 |
for (BciBlock s : b.getSuccessors()) {
|
|
883 |
sb.append("B").append(s.getId()).append(" (").append(s.startBci).append(" -> ").append(s.endBci).append(")");
|
|
884 |
if (s.isExceptionEntry) {
|
|
885 |
sb.append("!");
|
|
886 |
}
|
|
887 |
sb.append(" ");
|
|
888 |
}
|
|
889 |
sb.append(n).append(" Loop : ");
|
|
890 |
for (int pos : b.loopIdIterable()) {
|
|
891 |
sb.append("B").append(loopHeaders[pos].getId()).append(" ");
|
|
892 |
}
|
|
893 |
sb.append(n);
|
|
894 |
}
|
46640
|
895 |
debug.log("%s", sb);
|
43972
|
896 |
}
|
|
897 |
}
|
|
898 |
|
|
899 |
/**
|
|
900 |
* Get the header block for a loop index.
|
|
901 |
*/
|
|
902 |
public BciBlock getLoopHeader(int index) {
|
|
903 |
return loopHeaders[index];
|
|
904 |
}
|
|
905 |
|
|
906 |
/**
|
|
907 |
* The next available loop number.
|
|
908 |
*/
|
|
909 |
private int nextLoop;
|
|
910 |
|
|
911 |
/**
|
|
912 |
* Mark the block as a loop header, using the next available loop number. Also checks for corner
|
|
913 |
* cases that we don't want to compile.
|
|
914 |
*/
|
|
915 |
private void makeLoopHeader(BciBlock block) {
|
|
916 |
if (!block.isLoopHeader) {
|
|
917 |
block.isLoopHeader = true;
|
|
918 |
|
|
919 |
if (block.isExceptionEntry) {
|
|
920 |
// Loops that are implicitly formed by an exception handler lead to all sorts of
|
|
921 |
// corner cases.
|
|
922 |
// Don't compile such methods for now, until we see a concrete case that allows
|
|
923 |
// checking for correctness.
|
|
924 |
throw new PermanentBailoutException("Loop formed by an exception handler");
|
|
925 |
}
|
|
926 |
if (nextLoop >= LOOP_HEADER_MAX_CAPACITY) {
|
|
927 |
// This restriction can be removed by using a fall-back to a BitSet in case we have
|
|
928 |
// more than 64 loops
|
|
929 |
// Don't compile such methods for now, until we see a concrete case that allows
|
|
930 |
// checking for correctness.
|
|
931 |
throw new PermanentBailoutException("Too many loops in method");
|
|
932 |
}
|
|
933 |
|
|
934 |
assert block.loops == 0;
|
|
935 |
block.loops = 1L << nextLoop;
|
46640
|
936 |
debug.log("makeLoopHeader(%s) -> %x", block, block.loops);
|
43972
|
937 |
if (loopHeaders == null) {
|
|
938 |
loopHeaders = new BciBlock[LOOP_HEADER_INITIAL_CAPACITY];
|
|
939 |
} else if (nextLoop >= loopHeaders.length) {
|
|
940 |
loopHeaders = Arrays.copyOf(loopHeaders, LOOP_HEADER_MAX_CAPACITY);
|
|
941 |
}
|
|
942 |
loopHeaders[nextLoop] = block;
|
|
943 |
block.loopId = nextLoop;
|
|
944 |
nextLoop++;
|
|
945 |
}
|
|
946 |
assert Long.bitCount(block.loops) == 1;
|
|
947 |
}
|
|
948 |
|
|
949 |
/**
|
|
950 |
* Depth-first traversal of the control flow graph. The flag {@linkplain BciBlock#visited} is
|
|
951 |
* used to visit every block only once. The flag {@linkplain BciBlock#active} is used to detect
|
|
952 |
* cycles (backward edges).
|
|
953 |
*/
|
|
954 |
private long computeBlockOrder(BciBlock block) {
|
|
955 |
if (block.visited) {
|
|
956 |
if (block.active) {
|
|
957 |
// Reached block via backward branch.
|
|
958 |
makeLoopHeader(block);
|
|
959 |
// Return cached loop information for this block.
|
|
960 |
return block.loops;
|
|
961 |
} else if (block.isLoopHeader) {
|
|
962 |
return block.loops & ~(1L << block.loopId);
|
|
963 |
} else {
|
|
964 |
return block.loops;
|
|
965 |
}
|
|
966 |
}
|
|
967 |
|
|
968 |
block.visited = true;
|
|
969 |
block.active = true;
|
|
970 |
|
|
971 |
long loops = 0;
|
|
972 |
for (BciBlock successor : block.getSuccessors()) {
|
|
973 |
// Recursively process successors.
|
|
974 |
loops |= computeBlockOrder(successor);
|
|
975 |
if (successor.active) {
|
|
976 |
// Reached block via backward branch.
|
|
977 |
loops |= (1L << successor.loopId);
|
|
978 |
}
|
|
979 |
}
|
|
980 |
|
|
981 |
block.loops = loops;
|
46640
|
982 |
debug.log("computeBlockOrder(%s) -> %x", block, block.loops);
|
43972
|
983 |
|
|
984 |
if (block.isLoopHeader) {
|
|
985 |
loops &= ~(1L << block.loopId);
|
|
986 |
}
|
|
987 |
|
|
988 |
block.active = false;
|
|
989 |
blocksNotYetAssignedId--;
|
|
990 |
blocks[blocksNotYetAssignedId] = block;
|
|
991 |
|
|
992 |
return loops;
|
|
993 |
}
|
|
994 |
|
|
995 |
private long fixLoopBits(BciBlock[] blockMap, BciBlock block) {
|
|
996 |
if (block.visited) {
|
|
997 |
// Return cached loop information for this block.
|
|
998 |
if (block.isLoopHeader) {
|
|
999 |
return block.loops & ~(1L << block.loopId);
|
|
1000 |
} else {
|
|
1001 |
return block.loops;
|
|
1002 |
}
|
|
1003 |
}
|
|
1004 |
|
|
1005 |
block.visited = true;
|
|
1006 |
long loops = block.loops;
|
|
1007 |
for (BciBlock successor : block.getSuccessors()) {
|
|
1008 |
// Recursively process successors.
|
|
1009 |
loops |= fixLoopBits(blockMap, successor);
|
|
1010 |
}
|
|
1011 |
if (block.loops != loops) {
|
|
1012 |
loopChanges = true;
|
|
1013 |
block.loops = loops;
|
46640
|
1014 |
debug.log("fixLoopBits0(%s) -> %x", block, block.loops);
|
43972
|
1015 |
}
|
|
1016 |
|
|
1017 |
if (block.isLoopHeader) {
|
|
1018 |
loops &= ~(1L << block.loopId);
|
|
1019 |
}
|
|
1020 |
|
|
1021 |
return loops;
|
|
1022 |
}
|
|
1023 |
|
46640
|
1024 |
public static BciBlockMapping create(BytecodeStream stream, Bytecode code, OptionValues options, DebugContext debug) {
|
|
1025 |
BciBlockMapping map = new BciBlockMapping(code, debug);
|
46344
|
1026 |
map.build(stream, options);
|
46640
|
1027 |
if (debug.isDumpEnabled(DebugContext.INFO_LEVEL)) {
|
|
1028 |
debug.dump(DebugContext.INFO_LEVEL, map, code.getMethod().format("After block building %f %R %H.%n(%P)"));
|
43972
|
1029 |
}
|
|
1030 |
|
|
1031 |
return map;
|
|
1032 |
}
|
|
1033 |
|
|
1034 |
public BciBlock[] getLoopHeaders() {
|
|
1035 |
return loopHeaders;
|
|
1036 |
}
|
|
1037 |
|
|
1038 |
public BciBlock getStartBlock() {
|
|
1039 |
return startBlock;
|
|
1040 |
}
|
|
1041 |
|
|
1042 |
public ExceptionDispatchBlock getUnwindBlock() {
|
|
1043 |
return (ExceptionDispatchBlock) blocks[blocks.length - 1];
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
public int getLoopCount() {
|
|
1047 |
return nextLoop;
|
|
1048 |
}
|
|
1049 |
|
|
1050 |
public int getBlockCount() {
|
|
1051 |
return blocks.length;
|
|
1052 |
}
|
|
1053 |
}
|