1 /* 2 * Copyright (c) 2012, 2018, 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 24 25 package org.graalvm.compiler.java; 26 27 import static org.graalvm.compiler.bytecode.Bytecodes.DUP; 28 import static org.graalvm.compiler.bytecode.Bytecodes.DUP2; 29 import static org.graalvm.compiler.bytecode.Bytecodes.DUP2_X1; 30 import static org.graalvm.compiler.bytecode.Bytecodes.DUP2_X2; 31 import static org.graalvm.compiler.bytecode.Bytecodes.DUP_X1; 32 import static org.graalvm.compiler.bytecode.Bytecodes.DUP_X2; 33 import static org.graalvm.compiler.bytecode.Bytecodes.POP; 34 import static org.graalvm.compiler.bytecode.Bytecodes.POP2; 35 import static org.graalvm.compiler.bytecode.Bytecodes.SWAP; 36 import static org.graalvm.compiler.debug.GraalError.shouldNotReachHere; 37 import static org.graalvm.compiler.nodes.FrameState.TWO_SLOT_MARKER; 38 39 import java.util.ArrayList; 40 import java.util.Arrays; 41 import java.util.List; 42 import java.util.function.Function; 43 44 import org.graalvm.compiler.bytecode.Bytecode; 45 import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode; 46 import org.graalvm.compiler.core.common.GraalOptions; 47 import org.graalvm.compiler.core.common.PermanentBailoutException; 48 import org.graalvm.compiler.core.common.type.StampFactory; 49 import org.graalvm.compiler.core.common.type.StampPair; 50 import org.graalvm.compiler.debug.DebugContext; 51 import org.graalvm.compiler.graph.NodeSourcePosition; 52 import org.graalvm.compiler.java.BciBlockMapping.BciBlock; 53 import org.graalvm.compiler.nodeinfo.Verbosity; 54 import org.graalvm.compiler.nodes.AbstractMergeNode; 55 import org.graalvm.compiler.nodes.ConstantNode; 56 import org.graalvm.compiler.nodes.FrameState; 57 import org.graalvm.compiler.nodes.LoopBeginNode; 58 import org.graalvm.compiler.nodes.LoopExitNode; 59 import org.graalvm.compiler.nodes.NodeView; 60 import org.graalvm.compiler.nodes.ParameterNode; 61 import org.graalvm.compiler.nodes.PhiNode; 62 import org.graalvm.compiler.nodes.ProxyNode; 63 import org.graalvm.compiler.nodes.StateSplit; 64 import org.graalvm.compiler.nodes.StructuredGraph; 65 import org.graalvm.compiler.nodes.ValueNode; 66 import org.graalvm.compiler.nodes.ValuePhiNode; 67 import org.graalvm.compiler.nodes.calc.FloatingNode; 68 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; 69 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool; 70 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.SideEffectsState; 71 import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin; 72 import org.graalvm.compiler.nodes.java.MonitorIdNode; 73 import org.graalvm.compiler.nodes.util.GraphUtil; 74 75 import jdk.vm.ci.code.BytecodeFrame; 76 import jdk.vm.ci.meta.Assumptions; 77 import jdk.vm.ci.meta.JavaKind; 78 import jdk.vm.ci.meta.JavaType; 79 import jdk.vm.ci.meta.ResolvedJavaMethod; 80 import jdk.vm.ci.meta.ResolvedJavaType; 81 import jdk.vm.ci.meta.Signature; 82 83 public final class FrameStateBuilder implements SideEffectsState { 84 85 private static final ValueNode[] EMPTY_ARRAY = new ValueNode[0]; 86 private static final MonitorIdNode[] EMPTY_MONITOR_ARRAY = new MonitorIdNode[0]; 87 88 private final BytecodeParser parser; 89 private final GraphBuilderTool tool; 90 private final Bytecode code; 91 private int stackSize; 92 protected final ValueNode[] locals; 93 protected final ValueNode[] stack; 94 private ValueNode[] lockedObjects; 95 private boolean canVerifyKind; 96 97 /** 98 * @see BytecodeFrame#rethrowException 99 */ 100 private boolean rethrowException; 101 102 private MonitorIdNode[] monitorIds; 103 private final StructuredGraph graph; 104 private final boolean clearNonLiveLocals; 105 private FrameState outerFrameState; 106 private NodeSourcePosition outerSourcePosition; 107 108 /** 109 * The closest {@link StateSplit#hasSideEffect() side-effect} predecessors. There will be more 110 * than one when the current block contains no side-effects but merging predecessor blocks do. 111 */ 112 private List<StateSplit> sideEffects; 113 114 /** 115 * Creates a new frame state builder for the given method and the given target graph. 116 * 117 * @param method the method whose frame is simulated 118 * @param graph the target graph of Graal nodes created by the builder 119 */ 120 public FrameStateBuilder(GraphBuilderTool tool, ResolvedJavaMethod method, StructuredGraph graph) { 121 this(tool, new ResolvedJavaMethodBytecode(method), graph, false); 122 } 123 124 /** 125 * Creates a new frame state builder for the given code attribute, method and the given target 126 * graph. Additionally specifies if nonLiveLocals should be retained. 127 * 128 * @param code the bytecode in which the frame exists 129 * @param graph the target graph of Graal nodes created by the builder 130 * @param shouldRetainLocalVariables specifies if nonLiveLocals should be retained in state. 131 */ 132 public FrameStateBuilder(GraphBuilderTool tool, Bytecode code, StructuredGraph graph, boolean shouldRetainLocalVariables) { 133 this.tool = tool; 134 if (tool instanceof BytecodeParser) { 135 this.parser = (BytecodeParser) tool; 136 } else { 137 this.parser = null; 138 } 139 this.code = code; 140 this.locals = allocateArray(code.getMaxLocals()); 141 this.stack = allocateArray(Math.max(1, code.getMaxStackSize())); 142 this.lockedObjects = allocateArray(0); 143 144 assert graph != null; 145 146 this.monitorIds = EMPTY_MONITOR_ARRAY; 147 this.graph = graph; 148 this.clearNonLiveLocals = GraalOptions.OptClearNonLiveLocals.getValue(graph.getOptions()) && !shouldRetainLocalVariables; 149 this.canVerifyKind = true; 150 } 151 152 public void disableKindVerification() { 153 canVerifyKind = false; 154 } 155 156 public void initializeFromArgumentsArray(ValueNode[] arguments) { 157 158 int javaIndex = 0; 159 int index = 0; 160 if (!getMethod().isStatic()) { 161 // set the receiver 162 locals[javaIndex] = arguments[index]; 163 javaIndex = 1; 164 index = 1; 165 } 166 Signature sig = getMethod().getSignature(); 167 int max = sig.getParameterCount(false); 168 for (int i = 0; i < max; i++) { 169 JavaKind kind = sig.getParameterKind(i); 170 locals[javaIndex] = arguments[index]; 171 javaIndex++; 172 if (kind.needsTwoSlots()) { 173 locals[javaIndex] = TWO_SLOT_MARKER; 174 javaIndex++; 175 } 176 index++; 177 } 178 } 179 180 public void initializeForMethodStart(Assumptions assumptions, boolean eagerResolve, Plugins plugins) { 181 182 int javaIndex = 0; 183 int index = 0; 184 ResolvedJavaMethod method = getMethod(); 185 ResolvedJavaType originalType = method.getDeclaringClass(); 186 if (!method.isStatic()) { 187 // add the receiver 188 FloatingNode receiver = null; 189 StampPair receiverStamp = null; 190 if (plugins != null) { 191 receiverStamp = plugins.getOverridingStamp(tool, originalType, true); 192 } 193 if (receiverStamp == null) { 194 receiverStamp = StampFactory.forDeclaredType(assumptions, originalType, true); 195 } 196 197 if (plugins != null) { 198 for (ParameterPlugin plugin : plugins.getParameterPlugins()) { 199 receiver = plugin.interceptParameter(tool, index, receiverStamp); 200 if (receiver != null) { 201 break; 202 } 203 } 204 } 205 if (receiver == null) { 206 receiver = new ParameterNode(javaIndex, receiverStamp); 207 } 208 209 locals[javaIndex] = graph.addOrUniqueWithInputs(receiver); 210 javaIndex = 1; 211 index = 1; 212 } 213 Signature sig = method.getSignature(); 214 int max = sig.getParameterCount(false); 215 ResolvedJavaType accessingClass = originalType; 216 for (int i = 0; i < max; i++) { 217 JavaType type = sig.getParameterType(i, accessingClass); 218 if (eagerResolve) { 219 type = type.resolve(accessingClass); 220 } 221 JavaKind kind = type.getJavaKind(); 222 StampPair stamp = null; 223 if (plugins != null) { 224 stamp = plugins.getOverridingStamp(tool, type, false); 225 } 226 if (stamp == null) { 227 // GR-714: subword inputs cannot be trusted 228 if (kind.getStackKind() != kind) { 229 stamp = StampPair.createSingle(StampFactory.forKind(JavaKind.Int)); 230 } else { 231 stamp = StampFactory.forDeclaredType(assumptions, type, false); 232 } 233 } 234 235 FloatingNode param = null; 236 if (plugins != null) { 237 for (ParameterPlugin plugin : plugins.getParameterPlugins()) { 238 param = plugin.interceptParameter(tool, index, stamp); 239 if (param != null) { 240 break; 241 } 242 } 243 } 244 if (param == null) { 245 param = new ParameterNode(index, stamp); 246 } 247 248 locals[javaIndex] = graph.addOrUniqueWithInputs(param); 249 javaIndex++; 250 if (kind.needsTwoSlots()) { 251 locals[javaIndex] = TWO_SLOT_MARKER; 252 javaIndex++; 253 } 254 index++; 255 } 256 } 257 258 private FrameStateBuilder(FrameStateBuilder other) { 259 this.parser = other.parser; 260 this.tool = other.tool; 261 this.code = other.code; 262 this.stackSize = other.stackSize; 263 this.locals = other.locals.clone(); 264 this.stack = other.stack.clone(); 265 this.lockedObjects = other.lockedObjects.length == 0 ? other.lockedObjects : other.lockedObjects.clone(); 266 this.rethrowException = other.rethrowException; 267 this.canVerifyKind = other.canVerifyKind; 268 269 assert locals.length == code.getMaxLocals(); 270 assert stack.length == Math.max(1, code.getMaxStackSize()); 271 272 assert other.graph != null; 273 graph = other.graph; 274 clearNonLiveLocals = other.clearNonLiveLocals; 275 monitorIds = other.monitorIds.length == 0 ? other.monitorIds : other.monitorIds.clone(); 276 277 assert locals.length == code.getMaxLocals(); 278 assert stack.length == Math.max(1, code.getMaxStackSize()); 279 assert lockedObjects.length == monitorIds.length; 280 } 281 282 private static ValueNode[] allocateArray(int length) { 283 return length == 0 ? EMPTY_ARRAY : new ValueNode[length]; 284 } 285 286 public ResolvedJavaMethod getMethod() { 287 return code.getMethod(); 288 } 289 290 @Override 291 public String toString() { 292 StringBuilder sb = new StringBuilder(); 293 sb.append("[locals: ["); 294 for (int i = 0; i < locals.length; i++) { 295 sb.append(i == 0 ? "" : ",").append(locals[i] == null ? "_" : locals[i] == TWO_SLOT_MARKER ? "#" : locals[i].toString(Verbosity.Id)); 296 } 297 sb.append("] stack: ["); 298 for (int i = 0; i < stackSize; i++) { 299 sb.append(i == 0 ? "" : ",").append(stack[i] == null ? "_" : stack[i] == TWO_SLOT_MARKER ? "#" : stack[i].toString(Verbosity.Id)); 300 } 301 sb.append("] locks: ["); 302 for (int i = 0; i < lockedObjects.length; i++) { 303 sb.append(i == 0 ? "" : ",").append(lockedObjects[i].toString(Verbosity.Id)).append(" / ").append(monitorIds[i].toString(Verbosity.Id)); 304 } 305 sb.append("]"); 306 if (rethrowException) { 307 sb.append(" rethrowException"); 308 } 309 sb.append("]"); 310 return sb.toString(); 311 } 312 313 public FrameState create(int bci, StateSplit forStateSplit) { 314 if (parser != null && parser.parsingIntrinsic()) { 315 NodeSourcePosition sourcePosition = parser.getGraph().trackNodeSourcePosition() ? createBytecodePosition(bci) : null; 316 return parser.intrinsicContext.createFrameState(parser.getGraph(), this, forStateSplit, sourcePosition); 317 } 318 319 // Skip intrinsic frames 320 return create(bci, parser != null ? parser.getNonIntrinsicAncestor() : null, false, null, null); 321 } 322 323 /** 324 * @param pushedValues if non-null, values to {@link #push(JavaKind, ValueNode)} to the stack 325 * before creating the {@link FrameState} 326 */ 327 public FrameState create(int bci, BytecodeParser parent, boolean duringCall, JavaKind[] pushedSlotKinds, ValueNode[] pushedValues) { 328 if (outerFrameState == null && parent != null) { 329 assert !parent.parsingIntrinsic() : "must already have the next non-intrinsic ancestor"; 330 outerFrameState = parent.getFrameStateBuilder().create(parent.bci(), parent.getNonIntrinsicAncestor(), true, null, null); 331 } 332 if (bci == BytecodeFrame.AFTER_EXCEPTION_BCI && parent != null) { 333 FrameState newFrameState = outerFrameState.duplicateModified(outerFrameState.bci, true, false, JavaKind.Void, new JavaKind[]{JavaKind.Object}, new ValueNode[]{stack[0]}); 334 return newFrameState; 335 } 336 if (bci == BytecodeFrame.INVALID_FRAMESTATE_BCI) { 337 throw shouldNotReachHere(); 338 } 339 340 if (pushedValues != null) { 341 assert pushedSlotKinds.length == pushedValues.length; 342 int stackSizeToRestore = stackSize; 343 for (int i = 0; i < pushedValues.length; i++) { 344 push(pushedSlotKinds[i], pushedValues[i]); 345 } 346 FrameState res = graph.add(new FrameState(outerFrameState, code, bci, locals, stack, stackSize, lockedObjects, Arrays.asList(monitorIds), rethrowException, duringCall)); 347 stackSize = stackSizeToRestore; 348 return res; 349 } else { 350 if (bci == BytecodeFrame.AFTER_EXCEPTION_BCI) { 351 assert outerFrameState == null; 352 clearLocals(); 353 } 354 return graph.add(new FrameState(outerFrameState, code, bci, locals, stack, stackSize, lockedObjects, Arrays.asList(monitorIds), rethrowException, duringCall)); 355 } 356 } 357 358 public NodeSourcePosition createBytecodePosition(int bci) { 359 BytecodeParser parent = parser.getParent(); 360 NodeSourcePosition position = create(bci, parent); 361 return position; 362 } 363 364 private NodeSourcePosition create(int bci, BytecodeParser parent) { 365 if (outerSourcePosition == null && parent != null) { 366 outerSourcePosition = parent.getFrameStateBuilder().createBytecodePosition(parent.bci()); 367 } 368 if (bci == BytecodeFrame.AFTER_EXCEPTION_BCI && parent != null) { 369 return FrameState.toSourcePosition(outerFrameState); 370 } 371 if (bci == BytecodeFrame.INVALID_FRAMESTATE_BCI) { 372 throw shouldNotReachHere(); 373 } 374 if (parser.intrinsicContext != null && (parent == null || parent.intrinsicContext != parser.intrinsicContext)) { 375 // When parsing an intrinsic put in a substitution marker showing the original method as 376 // the caller. This keeps the relationship between the method and the method 377 // substitution clear in resulting NodeSourcePosition. 378 NodeSourcePosition original = new NodeSourcePosition(outerSourcePosition, parser.intrinsicContext.getOriginalMethod(), -1); 379 return NodeSourcePosition.substitution(original, code.getMethod(), bci); 380 } else { 381 return new NodeSourcePosition(outerSourcePosition, code.getMethod(), bci); 382 } 383 } 384 385 public FrameStateBuilder copy() { 386 return new FrameStateBuilder(this); 387 } 388 389 public boolean isCompatibleWith(FrameStateBuilder other) { 390 assert code.equals(other.code) && graph == other.graph && localsSize() == other.localsSize() : "Can only compare frame states of the same method"; 391 assert lockedObjects.length == monitorIds.length && other.lockedObjects.length == other.monitorIds.length : "mismatch between lockedObjects and monitorIds"; 392 393 if (stackSize() != other.stackSize()) { 394 return false; 395 } 396 for (int i = 0; i < stackSize(); i++) { 397 ValueNode x = stack[i]; 398 ValueNode y = other.stack[i]; 399 assert x != null && y != null; 400 if (x != y && (x == TWO_SLOT_MARKER || x.isDeleted() || y == TWO_SLOT_MARKER || y.isDeleted() || x.getStackKind() != y.getStackKind())) { 401 return false; 402 } 403 } 404 if (lockedObjects.length != other.lockedObjects.length) { 405 return false; 406 } 407 for (int i = 0; i < lockedObjects.length; i++) { 408 if (GraphUtil.originalValue(lockedObjects[i]) != GraphUtil.originalValue(other.lockedObjects[i]) || monitorIds[i] != other.monitorIds[i]) { 409 throw new PermanentBailoutException("unbalanced monitors"); 410 } 411 } 412 return true; 413 } 414 415 public void merge(AbstractMergeNode block, FrameStateBuilder other) { 416 assert isCompatibleWith(other); 417 418 for (int i = 0; i < localsSize(); i++) { 419 locals[i] = merge(locals[i], other.locals[i], block); 420 } 421 for (int i = 0; i < stackSize(); i++) { 422 stack[i] = merge(stack[i], other.stack[i], block); 423 } 424 for (int i = 0; i < lockedObjects.length; i++) { 425 lockedObjects[i] = merge(lockedObjects[i], other.lockedObjects[i], block); 426 assert monitorIds[i] == other.monitorIds[i]; 427 } 428 429 if (sideEffects == null) { 430 sideEffects = other.sideEffects; 431 } else { 432 if (other.sideEffects != null) { 433 sideEffects.addAll(other.sideEffects); 434 } 435 } 436 } 437 438 private ValueNode merge(ValueNode currentValue, ValueNode otherValue, AbstractMergeNode block) { 439 if (currentValue == null || currentValue.isDeleted()) { 440 return null; 441 } else if (block.isPhiAtMerge(currentValue)) { 442 if (otherValue == null || otherValue == TWO_SLOT_MARKER || otherValue.isDeleted() || currentValue.getStackKind() != otherValue.getStackKind()) { 443 // This phi must be dead anyway, add input of correct stack kind to keep the graph 444 // invariants. 445 ((PhiNode) currentValue).addInput(ConstantNode.defaultForKind(currentValue.getStackKind(), graph)); 446 } else { 447 ((PhiNode) currentValue).addInput(otherValue); 448 } 449 return currentValue; 450 } else if (currentValue != otherValue) { 451 if (currentValue == TWO_SLOT_MARKER || otherValue == TWO_SLOT_MARKER) { 452 return null; 453 } else if (otherValue == null || otherValue.isDeleted() || currentValue.getStackKind() != otherValue.getStackKind()) { 454 return null; 455 } 456 assert !(block instanceof LoopBeginNode) : String.format("Phi functions for loop headers are create eagerly for changed locals and all stack slots: %s != %s", currentValue, otherValue); 457 return createValuePhi(currentValue, otherValue, block); 458 } else { 459 return currentValue; 460 } 461 } 462 463 private ValuePhiNode createValuePhi(ValueNode currentValue, ValueNode otherValue, AbstractMergeNode block) { 464 ValuePhiNode phi = graph.addWithoutUnique(new ValuePhiNode(currentValue.stamp(NodeView.DEFAULT).unrestricted(), block)); 465 for (int i = 0; i < block.phiPredecessorCount(); i++) { 466 phi.addInput(currentValue); 467 } 468 phi.addInput(otherValue); 469 assert phi.valueCount() == block.phiPredecessorCount() + 1; 470 return phi; 471 } 472 473 public void inferPhiStamps(AbstractMergeNode block) { 474 for (int i = 0; i < localsSize(); i++) { 475 inferPhiStamp(block, locals[i]); 476 } 477 for (int i = 0; i < stackSize(); i++) { 478 inferPhiStamp(block, stack[i]); 479 } 480 for (int i = 0; i < lockedObjects.length; i++) { 481 inferPhiStamp(block, lockedObjects[i]); 482 } 483 } 484 485 private static void inferPhiStamp(AbstractMergeNode block, ValueNode node) { 486 if (block.isPhiAtMerge(node)) { 487 node.inferStamp(); 488 } 489 } 490 491 public void insertLoopPhis(LocalLiveness liveness, int loopId, LoopBeginNode loopBegin, boolean forcePhis, boolean stampFromValueForForcedPhis) { 492 for (int i = 0; i < localsSize(); i++) { 493 boolean changedInLoop = liveness.localIsChangedInLoop(loopId, i); 494 if (forcePhis || changedInLoop) { 495 locals[i] = createLoopPhi(loopBegin, locals[i], stampFromValueForForcedPhis && !changedInLoop); 496 } 497 } 498 for (int i = 0; i < stackSize(); i++) { 499 stack[i] = createLoopPhi(loopBegin, stack[i], false); 500 } 501 for (int i = 0; i < lockedObjects.length; i++) { 502 lockedObjects[i] = createLoopPhi(loopBegin, lockedObjects[i], false); 503 } 504 } 505 506 public void insertLoopProxies(LoopExitNode loopExit, FrameStateBuilder loopEntryState) { 507 DebugContext debug = graph.getDebug(); 508 for (int i = 0; i < localsSize(); i++) { 509 ValueNode value = locals[i]; 510 if (value != null && value != TWO_SLOT_MARKER && (!loopEntryState.contains(value) || loopExit.loopBegin().isPhiAtMerge(value))) { 511 debug.log(" inserting proxy for %s", value); 512 locals[i] = ProxyNode.forValue(value, loopExit, graph); 513 } 514 } 515 for (int i = 0; i < stackSize(); i++) { 516 ValueNode value = stack[i]; 517 if (value != null && value != TWO_SLOT_MARKER && (!loopEntryState.contains(value) || loopExit.loopBegin().isPhiAtMerge(value))) { 518 debug.log(" inserting proxy for %s", value); 519 stack[i] = ProxyNode.forValue(value, loopExit, graph); 520 } 521 } 522 for (int i = 0; i < lockedObjects.length; i++) { 523 ValueNode value = lockedObjects[i]; 524 if (value != null && (!loopEntryState.contains(value) || loopExit.loopBegin().isPhiAtMerge(value))) { 525 debug.log(" inserting proxy for %s", value); 526 lockedObjects[i] = ProxyNode.forValue(value, loopExit, graph); 527 } 528 } 529 } 530 531 public void insertProxies(Function<ValueNode, ValueNode> proxyFunction) { 532 DebugContext debug = graph.getDebug(); 533 for (int i = 0; i < localsSize(); i++) { 534 ValueNode value = locals[i]; 535 if (value != null && value != TWO_SLOT_MARKER) { 536 debug.log(" inserting proxy for %s", value); 537 locals[i] = proxyFunction.apply(value); 538 } 539 } 540 for (int i = 0; i < stackSize(); i++) { 541 ValueNode value = stack[i]; 542 if (value != null && value != TWO_SLOT_MARKER) { 543 debug.log(" inserting proxy for %s", value); 544 stack[i] = proxyFunction.apply(value); 545 } 546 } 547 for (int i = 0; i < lockedObjects.length; i++) { 548 ValueNode value = lockedObjects[i]; 549 if (value != null) { 550 debug.log(" inserting proxy for %s", value); 551 lockedObjects[i] = proxyFunction.apply(value); 552 } 553 } 554 } 555 556 private ValueNode createLoopPhi(AbstractMergeNode block, ValueNode value, boolean stampFromValue) { 557 if (value == null || value == TWO_SLOT_MARKER) { 558 return value; 559 } 560 assert !block.isPhiAtMerge(value) : "phi function for this block already created"; 561 562 ValuePhiNode phi = graph.addWithoutUnique(new ValuePhiNode(stampFromValue ? value.stamp(NodeView.DEFAULT) : value.stamp(NodeView.DEFAULT).unrestricted(), block)); 563 phi.addInput(value); 564 return phi; 565 } 566 567 /** 568 * Adds a locked monitor to this frame state. 569 * 570 * @param object the object whose monitor will be locked. 571 */ 572 public void pushLock(ValueNode object, MonitorIdNode monitorId) { 573 assert object.isAlive() && object.getStackKind() == JavaKind.Object : "unexpected value: " + object; 574 lockedObjects = Arrays.copyOf(lockedObjects, lockedObjects.length + 1); 575 monitorIds = Arrays.copyOf(monitorIds, monitorIds.length + 1); 576 lockedObjects[lockedObjects.length - 1] = object; 577 monitorIds[monitorIds.length - 1] = monitorId; 578 assert lockedObjects.length == monitorIds.length; 579 } 580 581 /** 582 * Removes a locked monitor from this frame state. 583 * 584 * @return the object whose monitor was removed from the locks list. 585 */ 586 public ValueNode popLock() { 587 try { 588 return lockedObjects[lockedObjects.length - 1]; 589 } finally { 590 lockedObjects = lockedObjects.length == 1 ? EMPTY_ARRAY : Arrays.copyOf(lockedObjects, lockedObjects.length - 1); 591 monitorIds = monitorIds.length == 1 ? EMPTY_MONITOR_ARRAY : Arrays.copyOf(monitorIds, monitorIds.length - 1); 592 assert lockedObjects.length == monitorIds.length; 593 } 594 } 595 596 public MonitorIdNode peekMonitorId() { 597 return monitorIds[monitorIds.length - 1]; 598 } 599 600 /** 601 * @return the current lock depth 602 */ 603 public int lockDepth(boolean includeParents) { 604 int depth = lockedObjects.length; 605 assert depth == monitorIds.length; 606 if (includeParents && parser.getParent() != null) { 607 depth += parser.getParent().frameState.lockDepth(true); 608 } 609 return depth; 610 } 611 612 public boolean contains(ValueNode value) { 613 for (int i = 0; i < localsSize(); i++) { 614 if (locals[i] == value) { 615 return true; 616 } 617 } 618 for (int i = 0; i < stackSize(); i++) { 619 if (stack[i] == value) { 620 return true; 621 } 622 } 623 assert lockedObjects.length == monitorIds.length; 624 for (int i = 0; i < lockedObjects.length; i++) { 625 if (lockedObjects[i] == value || monitorIds[i] == value) { 626 return true; 627 } 628 } 629 return false; 630 } 631 632 public void clearNonLiveLocals(BciBlock block, LocalLiveness liveness, boolean liveIn) { 633 /* 634 * Non-live local clearing is mandatory for the entry block of an OSR compilation so that 635 * dead object slots at the OSR entry are cleared. It's not sufficient to rely on PiNodes 636 * with Kind.Illegal, because the conflicting branch might not have been parsed. 637 */ 638 boolean isOSREntryBlock = graph.isOSR() && getMethod().equals(graph.method()) && graph.getEntryBCI() == block.startBci; 639 if (!clearNonLiveLocals && !isOSREntryBlock) { 640 return; 641 } 642 if (liveIn) { 643 for (int i = 0; i < locals.length; i++) { 644 if (!liveness.localIsLiveIn(block, i)) { 645 assert locals[i] != TWO_SLOT_MARKER || locals[i - 1] == null : "Clearing of second slot must have cleared the first slot too"; 646 locals[i] = null; 647 } 648 } 649 } else { 650 for (int i = 0; i < locals.length; i++) { 651 if (!liveness.localIsLiveOut(block, i)) { 652 assert locals[i] != TWO_SLOT_MARKER || locals[i - 1] == null : "Clearing of second slot must have cleared the first slot too"; 653 locals[i] = null; 654 } 655 } 656 } 657 } 658 659 /** 660 * Clears all local variables. 661 */ 662 public void clearLocals() { 663 for (int i = 0; i < locals.length; i++) { 664 locals[i] = null; 665 } 666 } 667 668 /** 669 * @see BytecodeFrame#rethrowException 670 */ 671 public boolean rethrowException() { 672 return rethrowException; 673 } 674 675 /** 676 * @see BytecodeFrame#rethrowException 677 */ 678 public void setRethrowException(boolean b) { 679 rethrowException = b; 680 } 681 682 /** 683 * Returns the size of the local variables. 684 * 685 * @return the size of the local variables 686 */ 687 public int localsSize() { 688 return locals.length; 689 } 690 691 /** 692 * Gets the current size (height) of the stack. 693 */ 694 public int stackSize() { 695 return stackSize; 696 } 697 698 private boolean verifyKind(JavaKind slotKind, ValueNode x) { 699 assert x != null; 700 assert x != TWO_SLOT_MARKER; 701 assert slotKind.getSlotCount() > 0; 702 703 if (canVerifyKind) { 704 assert x.getStackKind() == slotKind.getStackKind(); 705 } 706 return true; 707 } 708 709 /** 710 * Loads the local variable at the specified index, checking that the returned value is non-null 711 * and that two-stack values are properly handled. 712 * 713 * @param i the index of the local variable to load 714 * @param slotKind the kind of the local variable from the point of view of the bytecodes 715 * @return the instruction that produced the specified local 716 */ 717 public ValueNode loadLocal(int i, JavaKind slotKind) { 718 ValueNode x = locals[i]; 719 assert verifyKind(slotKind, x); 720 assert slotKind.needsTwoSlots() ? locals[i + 1] == TWO_SLOT_MARKER : (i == locals.length - 1 || locals[i + 1] != TWO_SLOT_MARKER); 721 return x; 722 } 723 724 /** 725 * Stores a given local variable at the specified index. If the value occupies two slots, then 726 * the next local variable index is also overwritten. 727 * 728 * @param i the index at which to store 729 * @param slotKind the kind of the local variable from the point of view of the bytecodes 730 * @param x the instruction which produces the value for the local 731 */ 732 public void storeLocal(int i, JavaKind slotKind, ValueNode x) { 733 assert verifyKind(slotKind, x); 734 735 if (locals[i] == TWO_SLOT_MARKER) { 736 /* Writing the second slot of a two-slot value invalidates the first slot. */ 737 locals[i - 1] = null; 738 } 739 locals[i] = x; 740 if (slotKind.needsTwoSlots()) { 741 /* Writing a two-slot value: mark the second slot. */ 742 locals[i + 1] = TWO_SLOT_MARKER; 743 } else if (i < locals.length - 1 && locals[i + 1] == TWO_SLOT_MARKER) { 744 /* 745 * Writing a one-slot value to an index previously occupied by a two-slot value: clear 746 * the old marker of the second slot. 747 */ 748 locals[i + 1] = null; 749 } 750 } 751 752 /** 753 * Pushes an instruction onto the stack with the expected type. 754 * 755 * @param slotKind the kind of the stack element from the point of view of the bytecodes 756 * @param x the instruction to push onto the stack 757 */ 758 public void push(JavaKind slotKind, ValueNode x) { 759 assert verifyKind(slotKind, x); 760 761 xpush(x); 762 if (slotKind.needsTwoSlots()) { 763 xpush(TWO_SLOT_MARKER); 764 } 765 } 766 767 public void pushReturn(JavaKind slotKind, ValueNode x) { 768 if (slotKind != JavaKind.Void) { 769 push(slotKind, x); 770 } 771 } 772 773 /** 774 * Pops an instruction off the stack with the expected type. 775 * 776 * @param slotKind the kind of the stack element from the point of view of the bytecodes 777 * @return the instruction on the top of the stack 778 */ 779 public ValueNode pop(JavaKind slotKind) { 780 if (slotKind.needsTwoSlots()) { 781 ValueNode s = xpop(); 782 assert s == TWO_SLOT_MARKER; 783 } 784 ValueNode x = xpop(); 785 assert verifyKind(slotKind, x); 786 return x; 787 } 788 789 private void xpush(ValueNode x) { 790 assert x != null; 791 stack[stackSize++] = x; 792 } 793 794 private ValueNode xpop() { 795 ValueNode result = stack[--stackSize]; 796 assert result != null; 797 return result; 798 } 799 800 private ValueNode xpeek() { 801 ValueNode result = stack[stackSize - 1]; 802 assert result != null; 803 return result; 804 } 805 806 /** 807 * Pop the specified number of slots off of this stack and return them as an array of 808 * instructions. 809 * 810 * @return an array containing the arguments off of the stack 811 */ 812 public ValueNode[] popArguments(int argSize) { 813 ValueNode[] result = allocateArray(argSize); 814 for (int i = argSize - 1; i >= 0; i--) { 815 ValueNode x = xpop(); 816 if (x == TWO_SLOT_MARKER) { 817 /* Ignore second slot of two-slot value. */ 818 x = xpop(); 819 } 820 assert x != null && x != TWO_SLOT_MARKER; 821 result[i] = x; 822 } 823 return result; 824 } 825 826 /** 827 * Clears all values on this stack. 828 */ 829 public void clearStack() { 830 stackSize = 0; 831 } 832 833 /** 834 * Performs a raw stack operation as defined in the Java bytecode specification. 835 * 836 * @param opcode The Java bytecode. 837 */ 838 public void stackOp(int opcode) { 839 switch (opcode) { 840 case POP: { 841 ValueNode w1 = xpop(); 842 assert w1 != TWO_SLOT_MARKER; 843 break; 844 } 845 case POP2: { 846 xpop(); 847 ValueNode w2 = xpop(); 848 assert w2 != TWO_SLOT_MARKER; 849 break; 850 } 851 case DUP: { 852 ValueNode w1 = xpeek(); 853 assert w1 != TWO_SLOT_MARKER; 854 xpush(w1); 855 break; 856 } 857 case DUP_X1: { 858 ValueNode w1 = xpop(); 859 ValueNode w2 = xpop(); 860 assert w1 != TWO_SLOT_MARKER; 861 xpush(w1); 862 xpush(w2); 863 xpush(w1); 864 break; 865 } 866 case DUP_X2: { 867 ValueNode w1 = xpop(); 868 ValueNode w2 = xpop(); 869 ValueNode w3 = xpop(); 870 assert w1 != TWO_SLOT_MARKER; 871 xpush(w1); 872 xpush(w3); 873 xpush(w2); 874 xpush(w1); 875 break; 876 } 877 case DUP2: { 878 ValueNode w1 = xpop(); 879 ValueNode w2 = xpop(); 880 xpush(w2); 881 xpush(w1); 882 xpush(w2); 883 xpush(w1); 884 break; 885 } 886 case DUP2_X1: { 887 ValueNode w1 = xpop(); 888 ValueNode w2 = xpop(); 889 ValueNode w3 = xpop(); 890 xpush(w2); 891 xpush(w1); 892 xpush(w3); 893 xpush(w2); 894 xpush(w1); 895 break; 896 } 897 case DUP2_X2: { 898 ValueNode w1 = xpop(); 899 ValueNode w2 = xpop(); 900 ValueNode w3 = xpop(); 901 ValueNode w4 = xpop(); 902 xpush(w2); 903 xpush(w1); 904 xpush(w4); 905 xpush(w3); 906 xpush(w2); 907 xpush(w1); 908 break; 909 } 910 case SWAP: { 911 ValueNode w1 = xpop(); 912 ValueNode w2 = xpop(); 913 assert w1 != TWO_SLOT_MARKER; 914 assert w2 != TWO_SLOT_MARKER; 915 xpush(w1); 916 xpush(w2); 917 break; 918 } 919 default: 920 throw shouldNotReachHere(); 921 } 922 } 923 924 @Override 925 public int hashCode() { 926 int result = hashCode(locals, locals.length); 927 result *= 13; 928 result += hashCode(stack, this.stackSize); 929 return result; 930 } 931 932 private static int hashCode(Object[] a, int length) { 933 int result = 1; 934 for (int i = 0; i < length; ++i) { 935 Object element = a[i]; 936 result = 31 * result + (element == null ? 0 : System.identityHashCode(element)); 937 } 938 return result; 939 } 940 941 private static boolean equals(ValueNode[] a, ValueNode[] b, int length) { 942 for (int i = 0; i < length; ++i) { 943 if (a[i] != b[i]) { 944 return false; 945 } 946 } 947 return true; 948 } 949 950 @Override 951 public boolean equals(Object otherObject) { 952 if (otherObject instanceof FrameStateBuilder) { 953 FrameStateBuilder other = (FrameStateBuilder) otherObject; 954 if (!other.code.equals(code)) { 955 return false; 956 } 957 if (other.stackSize != stackSize) { 958 return false; 959 } 960 if (other.parser != parser) { 961 return false; 962 } 963 if (other.tool != tool) { 964 return false; 965 } 966 if (other.rethrowException != rethrowException) { 967 return false; 968 } 969 if (other.graph != graph) { 970 return false; 971 } 972 if (other.locals.length != locals.length) { 973 return false; 974 } 975 return equals(other.locals, locals, locals.length) && equals(other.stack, stack, stackSize) && equals(other.lockedObjects, lockedObjects, lockedObjects.length) && 976 equals(other.monitorIds, monitorIds, monitorIds.length); 977 } 978 return false; 979 } 980 981 @Override 982 public boolean isAfterSideEffect() { 983 return sideEffects != null; 984 } 985 986 @Override 987 public Iterable<StateSplit> sideEffects() { 988 return sideEffects; 989 } 990 991 @Override 992 public void addSideEffect(StateSplit sideEffect) { 993 assert sideEffect != null; 994 assert sideEffect.hasSideEffect(); 995 if (sideEffects == null) { 996 sideEffects = new ArrayList<>(4); 997 } 998 sideEffects.add(sideEffect); 999 } 1000 }