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