package org.graalvm.compiler.nodes;
import static org.graalvm.compiler.debug.GraalError.shouldNotReachHere;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import jdk.internal.vm.compiler.collections.EconomicMap;
import jdk.internal.vm.compiler.collections.EconomicSet;
import jdk.internal.vm.compiler.collections.Equivalence;
import org.graalvm.compiler.core.common.Fields;
import org.graalvm.compiler.core.common.PermanentBailoutException;
import org.graalvm.compiler.core.common.util.TypeReader;
import org.graalvm.compiler.core.common.util.UnsafeArrayTypeReader;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Edges;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeBitMap;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.NodeInputList;
import org.graalvm.compiler.graph.NodeList;
import org.graalvm.compiler.graph.NodeSourcePosition;
import org.graalvm.compiler.graph.NodeSuccessorList;
import org.graalvm.compiler.graph.spi.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.InputType;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.GraphDecoder.MethodScope;
import org.graalvm.compiler.nodes.GraphDecoder.ProxyPlaceholder;
import org.graalvm.compiler.nodes.calc.FloatingNode;
import org.graalvm.compiler.nodes.extended.IntegerSwitchNode;
import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin.LoopExplosionKind;
import org.graalvm.compiler.options.OptionValues;
import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.meta.DeoptimizationAction;
import jdk.vm.ci.meta.DeoptimizationReason;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.PrimitiveConstant;
import jdk.vm.ci.meta.ResolvedJavaType;
public class GraphDecoder {
protected class MethodScope {
public final LoopScope callerLoopScope;
public final Graph.Mark methodStartMark;
public final EncodedGraph encodedGraph;
public final int maxFixedNodeOrderId;
public final TypeReader reader;
public final LoopExplosionKind loopExplosion;
public final List<ControlSinkNode> returnAndUnwindNodes;
public final EconomicSet<Node> loopExplosionMerges;
public MergeNode loopExplosionHead;
protected MethodScope(LoopScope callerLoopScope, StructuredGraph graph, EncodedGraph encodedGraph, LoopExplosionKind loopExplosion) {
this.callerLoopScope = callerLoopScope;
this.methodStartMark = graph.getMark();
this.encodedGraph = encodedGraph;
this.loopExplosion = loopExplosion;
this.returnAndUnwindNodes = new ArrayList<>(2);
if (encodedGraph != null) {
reader = UnsafeArrayTypeReader.create(encodedGraph.getEncoding(), encodedGraph.getStartOffset(), architecture.supportsUnalignedMemoryAccess());
maxFixedNodeOrderId = reader.getUVInt();
if (encodedGraph.nodeStartOffsets == null) {
int nodeCount = reader.getUVInt();
int[] nodeStartOffsets = new int[nodeCount];
for (int i = 0; i < nodeCount; i++) {
nodeStartOffsets[i] = encodedGraph.getStartOffset() - reader.getUVInt();
}
encodedGraph.nodeStartOffsets = nodeStartOffsets;
}
} else {
reader = null;
maxFixedNodeOrderId = 0;
}
if (loopExplosion != LoopExplosionKind.NONE) {
loopExplosionMerges = EconomicSet.create(Equivalence.IDENTITY);
} else {
loopExplosionMerges = null;
}
}
public boolean isInlinedMethod() {
return false;
}
public NodeSourcePosition getCallerBytecodePosition() {
return getCallerBytecodePosition(null);
}
public NodeSourcePosition getCallerBytecodePosition(NodeSourcePosition position) {
return position;
}
}
protected static class LoopScope {
public final MethodScope methodScope;
public final LoopScope outer;
public final int loopDepth;
public final int loopIteration;
public Deque<LoopScope> nextIterations;
public final EconomicMap<LoopExplosionState, LoopExplosionState> iterationStates;
public final int loopBeginOrderId;
public final BitSet nodesToProcess;
public final Node[] createdNodes;
public final Node[] initialCreatedNodes;
protected LoopScope(MethodScope methodScope) {
this.methodScope = methodScope;
this.outer = null;
this.nextIterations = methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN ? new ArrayDeque<>(2) : null;
this.loopDepth = 0;
this.loopIteration = 0;
this.iterationStates = null;
this.loopBeginOrderId = -1;
int nodeCount = methodScope.encodedGraph.nodeStartOffsets.length;
this.nodesToProcess = new BitSet(methodScope.maxFixedNodeOrderId);
this.createdNodes = new Node[nodeCount];
this.initialCreatedNodes = null;
}
protected LoopScope(MethodScope methodScope, LoopScope outer, int loopDepth, int loopIteration, int loopBeginOrderId, Node[] initialCreatedNodes, Node[] createdNodes,
Deque<LoopScope> nextIterations, EconomicMap<LoopExplosionState, LoopExplosionState> iterationStates) {
this.methodScope = methodScope;
this.outer = outer;
this.loopDepth = loopDepth;
this.loopIteration = loopIteration;
this.nextIterations = nextIterations;
this.iterationStates = iterationStates;
this.loopBeginOrderId = loopBeginOrderId;
this.nodesToProcess = new BitSet(methodScope.maxFixedNodeOrderId);
this.initialCreatedNodes = initialCreatedNodes;
this.createdNodes = createdNodes;
}
@Override
public String toString() {
return loopDepth + "," + loopIteration + (loopBeginOrderId == -1 ? "" : "#" + loopBeginOrderId);
}
}
protected static class LoopExplosionState {
public final FrameState state;
public final MergeNode merge;
public final int hashCode;
protected LoopExplosionState(FrameState state, MergeNode merge) {
this.state = state;
this.merge = merge;
int h = 0;
for (ValueNode value : state.values()) {
if (value == null) {
h = h * 31 + 1234;
} else {
h = h * 31 + ProxyPlaceholder.unwrap(value).hashCode();
}
}
this.hashCode = h;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof LoopExplosionState)) {
return false;
}
FrameState otherState = ((LoopExplosionState) obj).state;
FrameState thisState = state;
assert thisState.outerFrameState() == otherState.outerFrameState();
Iterator<ValueNode> thisIter = thisState.values().iterator();
Iterator<ValueNode> otherIter = otherState.values().iterator();
while (thisIter.hasNext() && otherIter.hasNext()) {
ValueNode thisValue = ProxyPlaceholder.unwrap(thisIter.next());
ValueNode otherValue = ProxyPlaceholder.unwrap(otherIter.next());
if (thisValue != otherValue) {
return false;
}
}
return thisIter.hasNext() == otherIter.hasNext();
}
@Override
public int hashCode() {
return hashCode;
}
}
protected static class InvokeData {
public final Invoke invoke;
public final ResolvedJavaType contextType;
public final int invokeOrderId;
public final int callTargetOrderId;
public final int stateAfterOrderId;
public final int nextOrderId;
public final int nextNextOrderId;
public final int exceptionOrderId;
public final int exceptionStateOrderId;
public final int exceptionNextOrderId;
public JavaConstant constantReceiver;
protected InvokeData(Invoke invoke, ResolvedJavaType contextType, int invokeOrderId, int callTargetOrderId, int stateAfterOrderId, int nextOrderId, int nextNextOrderId, int exceptionOrderId,
int exceptionStateOrderId, int exceptionNextOrderId) {
this.invoke = invoke;
this.contextType = contextType;
this.invokeOrderId = invokeOrderId;
this.callTargetOrderId = callTargetOrderId;
this.stateAfterOrderId = stateAfterOrderId;
this.nextOrderId = nextOrderId;
this.nextNextOrderId = nextNextOrderId;
this.exceptionOrderId = exceptionOrderId;
this.exceptionStateOrderId = exceptionStateOrderId;
this.exceptionNextOrderId = exceptionNextOrderId;
}
}
@NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
protected static final class ProxyPlaceholder extends FloatingNode implements Canonicalizable {
public static final NodeClass<ProxyPlaceholder> TYPE = NodeClass.create(ProxyPlaceholder.class);
@Input ValueNode value;
@Input(InputType.Unchecked) Node proxyPoint;
public ProxyPlaceholder(ValueNode value, MergeNode proxyPoint) {
super(TYPE, value.stamp(NodeView.DEFAULT));
this.value = value;
this.proxyPoint = proxyPoint;
}
void setValue(ValueNode value) {
updateUsages(this.value, value);
this.value = value;
}
@Override
public Node canonical(CanonicalizerTool tool) {
if (tool.allUsagesAvailable()) {
return value;
} else {
return this;
}
}
public static ValueNode unwrap(ValueNode value) {
ValueNode result = value;
while (result instanceof ProxyPlaceholder) {
result = ((ProxyPlaceholder) result).value;
}
return result;
}
}
protected final Architecture architecture;
protected final StructuredGraph graph;
protected final OptionValues options;
protected final DebugContext debug;
private final EconomicMap<NodeClass<?>, ArrayDeque<Node>> reusableFloatingNodes;
public GraphDecoder(Architecture architecture, StructuredGraph graph) {
this.architecture = architecture;
this.graph = graph;
this.options = graph.getOptions();
this.debug = graph.getDebug();
reusableFloatingNodes = EconomicMap.create(Equivalence.IDENTITY);
}
@SuppressWarnings("try")
public final void decode(EncodedGraph encodedGraph) {
try (DebugContext.Scope scope = debug.scope("GraphDecoder", graph)) {
MethodScope methodScope = new MethodScope(null, graph, encodedGraph, LoopExplosionKind.NONE);
decode(createInitialLoopScope(methodScope, null));
cleanupGraph(methodScope);
assert graph.verify();
} catch (Throwable ex) {
debug.handle(ex);
}
}
protected final LoopScope createInitialLoopScope(MethodScope methodScope, FixedWithNextNode startNode) {
LoopScope loopScope = new LoopScope(methodScope);
FixedNode firstNode;
if (startNode != null) {
registerNode(loopScope, GraphEncoder.START_NODE_ORDER_ID, AbstractBeginNode.prevBegin(startNode), false, false);
firstNode = makeStubNode(methodScope, loopScope, GraphEncoder.FIRST_NODE_ORDER_ID);
startNode.setNext(firstNode);
loopScope.nodesToProcess.set(GraphEncoder.FIRST_NODE_ORDER_ID);
} else {
firstNode = graph.start();
registerNode(loopScope, GraphEncoder.START_NODE_ORDER_ID, firstNode, false, false);
loopScope.nodesToProcess.set(GraphEncoder.START_NODE_ORDER_ID);
}
return loopScope;
}
protected final void decode(LoopScope initialLoopScope) {
LoopScope loopScope = initialLoopScope;
while (loopScope != null) {
MethodScope methodScope = loopScope.methodScope;
while (loopScope != null) {
while (!loopScope.nodesToProcess.isEmpty()) {
loopScope = processNextNode(methodScope, loopScope);
methodScope = loopScope.methodScope;
}
if (loopScope.nextIterations != null && !loopScope.nextIterations.isEmpty()) {
assert loopScope.nextIterations.peekFirst().loopIteration == loopScope.loopIteration + 1;
loopScope = loopScope.nextIterations.removeFirst();
} else {
propagateCreatedNodes(loopScope);
loopScope = loopScope.outer;
}
}
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
LoopDetector loopDetector = new LoopDetector(graph, methodScope);
loopDetector.run();
}
if (methodScope.isInlinedMethod()) {
finishInlining(methodScope);
}
loopScope = methodScope.callerLoopScope;
}
}
protected void finishInlining(@SuppressWarnings("unused") MethodScope inlineScope) {
}
private static void propagateCreatedNodes(LoopScope loopScope) {
if (loopScope.outer == null || loopScope.createdNodes != loopScope.outer.createdNodes) {
return;
}
for (int i = 0; i < loopScope.createdNodes.length; i++) {
if (loopScope.outer.createdNodes[i] == null) {
loopScope.outer.createdNodes[i] = loopScope.createdNodes[i];
}
}
}
protected LoopScope processNextNode(MethodScope methodScope, LoopScope loopScope) {
int nodeOrderId = loopScope.nodesToProcess.nextSetBit(0);
loopScope.nodesToProcess.clear(nodeOrderId);
FixedNode node = (FixedNode) lookupNode(loopScope, nodeOrderId);
if (node.isDeleted()) {
return loopScope;
}
if ((node instanceof MergeNode ||
(node instanceof LoopBeginNode && (methodScope.loopExplosion == LoopExplosionKind.FULL_UNROLL || methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE ||
methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN))) &&
((AbstractMergeNode) node).forwardEndCount() == 1) {
AbstractMergeNode merge = (AbstractMergeNode) node;
EndNode singleEnd = merge.forwardEndAt(0);
registerNode(loopScope, nodeOrderId, AbstractBeginNode.prevBegin(singleEnd), true, false);
FixedNode next = makeStubNode(methodScope, loopScope, nodeOrderId + GraphEncoder.BEGIN_NEXT_ORDER_ID_OFFSET);
singleEnd.replaceAtPredecessor(next);
merge.safeDelete();
singleEnd.safeDelete();
return loopScope;
}
LoopScope successorAddScope = loopScope;
boolean updatePredecessors = true;
if (node instanceof LoopExitNode) {
if (methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN || (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE && loopScope.loopDepth > 1)) {
LoopScope outerScope = loopScope.outer;
int nextIterationNumber = outerScope.nextIterations.isEmpty() ? outerScope.loopIteration + 1 : outerScope.nextIterations.getLast().loopIteration + 1;
successorAddScope = new LoopScope(methodScope, outerScope.outer, outerScope.loopDepth, nextIterationNumber, outerScope.loopBeginOrderId,
outerScope.initialCreatedNodes == null ? null : Arrays.copyOf(outerScope.initialCreatedNodes, outerScope.initialCreatedNodes.length),
Arrays.copyOf(loopScope.initialCreatedNodes, loopScope.initialCreatedNodes.length), outerScope.nextIterations, outerScope.iterationStates);
checkLoopExplosionIteration(methodScope, successorAddScope);
for (int id = outerScope.nodesToProcess.nextSetBit(0); id >= 0; id = outerScope.nodesToProcess.nextSetBit(id + 1)) {
successorAddScope.createdNodes[id] = null;
}
outerScope.nextIterations.addLast(successorAddScope);
} else {
successorAddScope = loopScope.outer;
}
updatePredecessors = methodScope.loopExplosion == LoopExplosionKind.NONE;
}
methodScope.reader.setByteIndex(methodScope.encodedGraph.nodeStartOffsets[nodeOrderId]);
int typeId = methodScope.reader.getUVInt();
assert node.getNodeClass() == methodScope.encodedGraph.getNodeClasses()[typeId];
makeFixedNodeInputs(methodScope, loopScope, node);
readProperties(methodScope, node);
makeSuccessorStubs(methodScope, successorAddScope, node, updatePredecessors);
LoopScope resultScope = loopScope;
if (node instanceof LoopBeginNode) {
if (methodScope.loopExplosion != LoopExplosionKind.NONE) {
handleLoopExplosionBegin(methodScope, loopScope, (LoopBeginNode) node);
}
} else if (node instanceof LoopExitNode) {
if (methodScope.loopExplosion != LoopExplosionKind.NONE) {
handleLoopExplosionProxyNodes(methodScope, loopScope, successorAddScope, (LoopExitNode) node, nodeOrderId);
} else {
handleProxyNodes(methodScope, loopScope, (LoopExitNode) node);
}
} else if (node instanceof MergeNode) {
handleMergeNode(((MergeNode) node));
} else if (node instanceof AbstractEndNode) {
LoopScope phiInputScope = loopScope;
LoopScope phiNodeScope = loopScope;
if (methodScope.loopExplosion != LoopExplosionKind.NONE && node instanceof LoopEndNode) {
node = handleLoopExplosionEnd(methodScope, loopScope, (LoopEndNode) node);
phiNodeScope = loopScope.nextIterations.getLast();
}
int mergeOrderId = readOrderId(methodScope);
AbstractMergeNode merge = (AbstractMergeNode) lookupNode(phiNodeScope, mergeOrderId);
if (merge == null) {
merge = (AbstractMergeNode) makeStubNode(methodScope, phiNodeScope, mergeOrderId);
if (merge instanceof LoopBeginNode) {
assert phiNodeScope == phiInputScope && phiNodeScope == loopScope;
resultScope = new LoopScope(methodScope, loopScope, loopScope.loopDepth + 1, 0, mergeOrderId,
methodScope.loopExplosion != LoopExplosionKind.NONE ? Arrays.copyOf(loopScope.createdNodes, loopScope.createdNodes.length) : null,
methodScope.loopExplosion != LoopExplosionKind.NONE ? Arrays.copyOf(loopScope.createdNodes, loopScope.createdNodes.length) : loopScope.createdNodes,
methodScope.loopExplosion != LoopExplosionKind.NONE ? new ArrayDeque<>(2) : null,
methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE ? EconomicMap.create(Equivalence.DEFAULT) : null);
phiInputScope = resultScope;
phiNodeScope = resultScope;
if (methodScope.loopExplosion != LoopExplosionKind.NONE) {
registerNode(loopScope, mergeOrderId, null, true, true);
}
loopScope.nodesToProcess.clear(mergeOrderId);
resultScope.nodesToProcess.set(mergeOrderId);
}
}
handlePhiFunctions(methodScope, phiInputScope, phiNodeScope, (AbstractEndNode) node, merge);
} else if (node instanceof Invoke) {
InvokeData invokeData = readInvokeData(methodScope, nodeOrderId, (Invoke) node);
resultScope = handleInvoke(methodScope, loopScope, invokeData);
} else if (node instanceof ReturnNode || node instanceof UnwindNode) {
methodScope.returnAndUnwindNodes.add((ControlSinkNode) node);
} else {
handleFixedNode(methodScope, loopScope, nodeOrderId, node);
}
return resultScope;
}
protected InvokeData readInvokeData(MethodScope methodScope, int invokeOrderId, Invoke invoke) {
ResolvedJavaType contextType = (ResolvedJavaType) readObject(methodScope);
int callTargetOrderId = readOrderId(methodScope);
int stateAfterOrderId = readOrderId(methodScope);
int nextOrderId = readOrderId(methodScope);
if (invoke instanceof InvokeWithExceptionNode) {
int nextNextOrderId = readOrderId(methodScope);
int exceptionOrderId = readOrderId(methodScope);
int exceptionStateOrderId = readOrderId(methodScope);
int exceptionNextOrderId = readOrderId(methodScope);
return new InvokeData(invoke, contextType, invokeOrderId, callTargetOrderId, stateAfterOrderId, nextOrderId, nextNextOrderId, exceptionOrderId, exceptionStateOrderId,
exceptionNextOrderId);
} else {
return new InvokeData(invoke, contextType, invokeOrderId, callTargetOrderId, stateAfterOrderId, nextOrderId, -1, -1, -1, -1);
}
}
protected LoopScope handleInvoke(MethodScope methodScope, LoopScope loopScope, InvokeData invokeData) {
assert invokeData.invoke.callTarget() == null : "callTarget edge is ignored during decoding of Invoke";
CallTargetNode callTarget = (CallTargetNode) ensureNodeCreated(methodScope, loopScope, invokeData.callTargetOrderId);
appendInvoke(methodScope, loopScope, invokeData, callTarget);
return loopScope;
}
protected void appendInvoke(MethodScope methodScope, LoopScope loopScope, InvokeData invokeData, CallTargetNode callTarget) {
if (invokeData.invoke instanceof InvokeWithExceptionNode) {
((InvokeWithExceptionNode) invokeData.invoke).setCallTarget(callTarget);
} else {
((InvokeNode) invokeData.invoke).setCallTarget(callTarget);
}
assert invokeData.invoke.stateAfter() == null && invokeData.invoke.stateDuring() == null : "FrameState edges are ignored during decoding of Invoke";
invokeData.invoke.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, invokeData.stateAfterOrderId));
invokeData.invoke.setNext(makeStubNode(methodScope, loopScope, invokeData.nextOrderId));
if (invokeData.invoke instanceof InvokeWithExceptionNode) {
((InvokeWithExceptionNode) invokeData.invoke).setExceptionEdge((AbstractBeginNode) makeStubNode(methodScope, loopScope, invokeData.exceptionOrderId));
}
}
protected void handleMergeNode(MergeNode merge) {
}
protected void handleLoopExplosionBegin(MethodScope methodScope, LoopScope loopScope, LoopBeginNode loopBegin) {
checkLoopExplosionIteration(methodScope, loopScope);
List<EndNode> predecessors = loopBegin.forwardEnds().snapshot();
FixedNode successor = loopBegin.next();
FrameState frameState = loopBegin.stateAfter();
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
LoopExplosionState queryState = new LoopExplosionState(frameState, null);
LoopExplosionState existingState = loopScope.iterationStates.get(queryState);
if (existingState != null) {
loopBegin.replaceAtUsagesAndDelete(existingState.merge);
successor.safeDelete();
for (EndNode predecessor : predecessors) {
existingState.merge.addForwardEnd(predecessor);
}
return;
}
}
MergeNode merge = graph.add(new MergeNode());
methodScope.loopExplosionMerges.add(merge);
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
if (loopScope.iterationStates.size() == 0 && loopScope.loopDepth == 1) {
if (methodScope.loopExplosionHead != null) {
throw new PermanentBailoutException("Graal implementation restriction: Method with %s loop explosion must not have more than one top-level loop", LoopExplosionKind.MERGE_EXPLODE);
}
methodScope.loopExplosionHead = merge;
}
List<ValueNode> newFrameStateValues = new ArrayList<>();
for (ValueNode frameStateValue : frameState.values) {
if (frameStateValue == null || frameStateValue.isConstant() || !graph.isNew(methodScope.methodStartMark, frameStateValue)) {
newFrameStateValues.add(frameStateValue);
} else {
ProxyPlaceholder newFrameStateValue = graph.unique(new ProxyPlaceholder(frameStateValue, merge));
newFrameStateValues.add(newFrameStateValue);
for (int i = 0; i < loopScope.createdNodes.length; i++) {
if (loopScope.createdNodes[i] == frameStateValue) {
loopScope.createdNodes[i] = newFrameStateValue;
}
}
if (loopScope.initialCreatedNodes != null) {
for (int i = 0; i < loopScope.initialCreatedNodes.length; i++) {
if (loopScope.initialCreatedNodes[i] == frameStateValue) {
loopScope.initialCreatedNodes[i] = newFrameStateValue;
}
}
}
}
}
FrameState newFrameState = graph.add(new FrameState(frameState.outerFrameState(), frameState.getCode(), frameState.bci, newFrameStateValues, frameState.localsSize(),
frameState.stackSize(), frameState.rethrowException(), frameState.duringCall(), frameState.monitorIds(), frameState.virtualObjectMappings()));
frameState.replaceAtUsagesAndDelete(newFrameState);
frameState = newFrameState;
}
loopBegin.replaceAtUsagesAndDelete(merge);
merge.setStateAfter(frameState);
merge.setNext(successor);
for (EndNode predecessor : predecessors) {
merge.addForwardEnd(predecessor);
}
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
LoopExplosionState explosionState = new LoopExplosionState(frameState, merge);
loopScope.iterationStates.put(explosionState, explosionState);
}
}
protected void checkLoopExplosionIteration(MethodScope methodScope, LoopScope loopScope) {
throw shouldNotReachHere("when subclass uses loop explosion, it needs to implement this method");
}
protected FixedNode handleLoopExplosionEnd(MethodScope methodScope, LoopScope loopScope, LoopEndNode loopEnd) {
EndNode replacementNode = graph.add(new EndNode());
loopEnd.replaceAtPredecessor(replacementNode);
loopEnd.safeDelete();
assert methodScope.loopExplosion != LoopExplosionKind.NONE;
if (methodScope.loopExplosion != LoopExplosionKind.FULL_UNROLL || loopScope.nextIterations.isEmpty()) {
int nextIterationNumber = loopScope.nextIterations.isEmpty() ? loopScope.loopIteration + 1 : loopScope.nextIterations.getLast().loopIteration + 1;
LoopScope nextIterationScope = new LoopScope(methodScope, loopScope.outer, loopScope.loopDepth, nextIterationNumber, loopScope.loopBeginOrderId,
Arrays.copyOf(loopScope.initialCreatedNodes, loopScope.initialCreatedNodes.length),
Arrays.copyOf(loopScope.initialCreatedNodes, loopScope.initialCreatedNodes.length), loopScope.nextIterations, loopScope.iterationStates);
checkLoopExplosionIteration(methodScope, nextIterationScope);
loopScope.nextIterations.addLast(nextIterationScope);
registerNode(nextIterationScope, loopScope.loopBeginOrderId, null, true, true);
makeStubNode(methodScope, nextIterationScope, loopScope.loopBeginOrderId);
}
return replacementNode;
}
protected void handleFixedNode(MethodScope methodScope, LoopScope loopScope, int nodeOrderId, FixedNode node) {
}
protected void handleProxyNodes(MethodScope methodScope, LoopScope loopScope, LoopExitNode loopExit) {
assert loopExit.stateAfter() == null;
int stateAfterOrderId = readOrderId(methodScope);
loopExit.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, stateAfterOrderId));
int numProxies = methodScope.reader.getUVInt();
for (int i = 0; i < numProxies; i++) {
int proxyOrderId = readOrderId(methodScope);
ProxyNode proxy = (ProxyNode) ensureNodeCreated(methodScope, loopScope, proxyOrderId);
if (loopScope.outer.createdNodes != loopScope.createdNodes) {
registerNode(loopScope.outer, proxyOrderId, proxy, false, false);
}
}
}
protected void handleLoopExplosionProxyNodes(MethodScope methodScope, LoopScope loopScope, LoopScope outerScope, LoopExitNode loopExit, int loopExitOrderId) {
assert loopExit.stateAfter() == null;
int stateAfterOrderId = readOrderId(methodScope);
BeginNode begin = graph.add(new BeginNode());
FixedNode loopExitSuccessor = loopExit.next();
loopExit.replaceAtPredecessor(begin);
MergeNode loopExitPlaceholder = null;
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE && loopScope.loopDepth == 1) {
loopExitPlaceholder = graph.add(new MergeNode());
methodScope.loopExplosionMerges.add(loopExitPlaceholder);
EndNode end = graph.add(new EndNode());
begin.setNext(end);
loopExitPlaceholder.addForwardEnd(end);
begin = graph.add(new BeginNode());
loopExitPlaceholder.setNext(begin);
}
MergeNode merge = null;
Node existingExit = lookupNode(outerScope, loopExitOrderId);
if (existingExit == null) {
registerNode(outerScope, loopExitOrderId, begin, false, false);
begin.setNext(loopExitSuccessor);
} else if (existingExit instanceof BeginNode) {
merge = graph.add(new MergeNode());
registerNode(outerScope, loopExitOrderId, merge, true, false);
EndNode firstEnd = graph.add(new EndNode());
((BeginNode) existingExit).setNext(firstEnd);
merge.addForwardEnd(firstEnd);
merge.setNext(loopExitSuccessor);
} else {
merge = (MergeNode) existingExit;
}
if (merge != null) {
EndNode end = graph.add(new EndNode());
begin.setNext(end);
merge.addForwardEnd(end);
}
int numProxies = methodScope.reader.getUVInt();
boolean phiCreated = false;
for (int i = 0; i < numProxies; i++) {
int proxyOrderId = readOrderId(methodScope);
ProxyNode proxy = (ProxyNode) ensureNodeCreated(methodScope, loopScope, proxyOrderId);
ValueNode phiInput = proxy.value();
if (loopExitPlaceholder != null) {
if (!phiInput.isConstant()) {
phiInput = graph.unique(new ProxyPlaceholder(phiInput, loopExitPlaceholder));
}
registerNode(loopScope, proxyOrderId, phiInput, true, false);
}
ValueNode replacement;
ValueNode existing = (ValueNode) outerScope.createdNodes[proxyOrderId];
if (existing == null || existing == phiInput) {
registerNode(outerScope, proxyOrderId, phiInput, true, false);
replacement = phiInput;
} else if (!merge.isPhiAtMerge(existing)) {
PhiNode phi;
if (proxy instanceof ValueProxyNode) {
phi = graph.addWithoutUnique(new ValuePhiNode(proxy.stamp(NodeView.DEFAULT), merge));
} else if (proxy instanceof GuardProxyNode) {
phi = graph.addWithoutUnique(new GuardPhiNode(merge));
} else {
throw GraalError.shouldNotReachHere();
}
for (int j = 0; j < merge.phiPredecessorCount() - 1; j++) {
phi.addInput(existing);
}
phi.addInput(phiInput);
registerNode(outerScope, proxyOrderId, phi, true, false);
replacement = phi;
phiCreated = true;
} else {
PhiNode phi = (PhiNode) existing;
phi.addInput(phiInput);
replacement = phi;
}
proxy.replaceAtUsagesAndDelete(replacement);
}
if (loopExitPlaceholder != null) {
registerNode(loopScope, stateAfterOrderId, null, true, true);
loopExitPlaceholder.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, stateAfterOrderId));
}
if (merge != null && (merge.stateAfter() == null || phiCreated)) {
FrameState oldStateAfter = merge.stateAfter();
registerNode(outerScope, stateAfterOrderId, null, true, true);
merge.setStateAfter((FrameState) ensureNodeCreated(methodScope, outerScope, stateAfterOrderId));
if (oldStateAfter != null) {
oldStateAfter.safeDelete();
}
}
loopExit.safeDelete();
assert loopExitSuccessor.predecessor() == null;
if (merge != null) {
merge.getNodeClass().getSuccessorEdges().update(merge, null, loopExitSuccessor);
} else {
begin.getNodeClass().getSuccessorEdges().update(begin, null, loopExitSuccessor);
}
}
protected void handlePhiFunctions(MethodScope methodScope, LoopScope phiInputScope, LoopScope phiNodeScope, AbstractEndNode end, AbstractMergeNode merge) {
if (end instanceof LoopEndNode) {
int numEnds = ((LoopBeginNode) merge).loopEnds().count();
((LoopBeginNode) merge).nextEndIndex = numEnds;
((LoopEndNode) end).endIndex = numEnds - 1;
} else {
if (merge.ends == null) {
merge.ends = new NodeInputList<>(merge);
}
merge.addForwardEnd((EndNode) end);
}
boolean lazyPhi = allowLazyPhis() && (!(merge instanceof LoopBeginNode) || methodScope.loopExplosion != LoopExplosionKind.NONE);
int numPhis = methodScope.reader.getUVInt();
for (int i = 0; i < numPhis; i++) {
int phiInputOrderId = readOrderId(methodScope);
int phiNodeOrderId = readOrderId(methodScope);
ValueNode phiInput = (ValueNode) ensureNodeCreated(methodScope, phiInputScope, phiInputOrderId);
ValueNode existing = (ValueNode) lookupNode(phiNodeScope, phiNodeOrderId);
if (existing != null && merge.phiPredecessorCount() == 1) {
assert methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN && phiNodeScope.loopIteration > 0;
existing = null;
}
if (lazyPhi && (existing == null || existing == phiInput)) {
registerNode(phiNodeScope, phiNodeOrderId, phiInput, true, false);
} else if (!merge.isPhiAtMerge(existing)) {
registerNode(phiNodeScope, phiNodeOrderId, null, true, true);
PhiNode phi = (PhiNode) ensureNodeCreated(methodScope, phiNodeScope, phiNodeOrderId);
phi.setMerge(merge);
for (int j = 0; j < merge.phiPredecessorCount() - 1; j++) {
phi.addInput(existing);
}
phi.addInput(phiInput);
} else {
PhiNode phi = (PhiNode) existing;
phi.addInput(phiInput);
}
}
}
protected boolean allowLazyPhis() {
return false;
}
protected void readProperties(MethodScope methodScope, Node node) {
NodeSourcePosition position = (NodeSourcePosition) readObject(methodScope);
Fields fields = node.getNodeClass().getData();
for (int pos = 0; pos < fields.getCount(); pos++) {
if (fields.getType(pos).isPrimitive()) {
long primitive = methodScope.reader.getSV();
fields.setRawPrimitive(node, pos, primitive);
} else {
Object value = readObject(methodScope);
fields.putObject(node, pos, value);
}
}
if (graph.trackNodeSourcePosition() && position != null) {
NodeSourcePosition callerBytecodePosition = methodScope.getCallerBytecodePosition(position);
node.setNodeSourcePosition(callerBytecodePosition);
if (node instanceof DeoptimizingGuard) {
((DeoptimizingGuard) node).addCallerToNoDeoptSuccessorPosition(callerBytecodePosition.getCaller());
}
}
}
protected void makeFixedNodeInputs(MethodScope methodScope, LoopScope loopScope, Node node) {
Edges edges = node.getNodeClass().getInputEdges();
for (int index = 0; index < edges.getDirectCount(); index++) {
if (skipDirectEdge(node, edges, index)) {
continue;
}
int orderId = readOrderId(methodScope);
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
edges.initializeNode(node, index, value);
if (value != null && !value.isDeleted()) {
edges.update(node, null, value);
}
}
if (node instanceof AbstractMergeNode) {
assert edges.getCount() - edges.getDirectCount() == 1 : "MergeNode has one variable size input (the ends)";
assert Edges.getNodeList(node, edges.getOffsets(), edges.getDirectCount()) != null : "Input list must have been already created";
} else {
for (int index = edges.getDirectCount(); index < edges.getCount(); index++) {
int size = methodScope.reader.getSVInt();
if (size != -1) {
NodeList<Node> nodeList = new NodeInputList<>(node, size);
edges.initializeList(node, index, nodeList);
for (int idx = 0; idx < size; idx++) {
int orderId = readOrderId(methodScope);
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
nodeList.initialize(idx, value);
if (value != null && !value.isDeleted()) {
edges.update(node, null, value);
}
}
}
}
}
}
protected void makeFloatingNodeInputs(MethodScope methodScope, LoopScope loopScope, Node node) {
Edges edges = node.getNodeClass().getInputEdges();
if (node instanceof PhiNode) {
assert edges.getDirectCount() == 1 : "PhiNode has one direct input (the MergeNode)";
assert edges.getCount() - edges.getDirectCount() == 1 : "PhiNode has one variable size input (the values)";
edges.initializeList(node, edges.getDirectCount(), new NodeInputList<>(node));
} else {
for (int index = 0; index < edges.getDirectCount(); index++) {
int orderId = readOrderId(methodScope);
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
edges.initializeNode(node, index, value);
}
for (int index = edges.getDirectCount(); index < edges.getCount(); index++) {
int size = methodScope.reader.getSVInt();
if (size != -1) {
NodeList<Node> nodeList = new NodeInputList<>(node, size);
edges.initializeList(node, index, nodeList);
for (int idx = 0; idx < size; idx++) {
int orderId = readOrderId(methodScope);
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
nodeList.initialize(idx, value);
}
}
}
}
}
protected Node ensureNodeCreated(MethodScope methodScope, LoopScope loopScope, int nodeOrderId) {
if (nodeOrderId == GraphEncoder.NULL_ORDER_ID) {
return null;
}
Node node = lookupNode(loopScope, nodeOrderId);
if (node != null) {
return node;
}
node = decodeFloatingNode(methodScope, loopScope, nodeOrderId);
if (node instanceof ProxyNode || node instanceof PhiNode) {
node = graph.addWithoutUnique(node);
} else {
Node newNode = handleFloatingNodeBeforeAdd(methodScope, loopScope, node);
if (newNode != node) {
releaseFloatingNode(node);
}
if (!newNode.isAlive()) {
newNode = addFloatingNode(methodScope, newNode);
}
node = handleFloatingNodeAfterAdd(methodScope, loopScope, newNode);
}
registerNode(loopScope, nodeOrderId, node, false, false);
return node;
}
protected Node addFloatingNode(@SuppressWarnings("unused") MethodScope methodScope, Node node) {
return graph.addWithoutUnique(node);
}
protected Node decodeFloatingNode(MethodScope methodScope, LoopScope loopScope, int nodeOrderId) {
long readerByteIndex = methodScope.reader.getByteIndex();
methodScope.reader.setByteIndex(methodScope.encodedGraph.nodeStartOffsets[nodeOrderId]);
NodeClass<?> nodeClass = methodScope.encodedGraph.getNodeClasses()[methodScope.reader.getUVInt()];
Node node = allocateFloatingNode(nodeClass);
if (node instanceof FixedNode) {
throw shouldNotReachHere("Not a floating node: " + node.getClass().getName());
}
makeFloatingNodeInputs(methodScope, loopScope, node);
readProperties(methodScope, node);
assert node.getNodeClass().getEdges(Edges.Type.Successors).getCount() == 0;
methodScope.reader.setByteIndex(readerByteIndex);
return node;
}
private Node allocateFloatingNode(NodeClass<?> nodeClass) {
ArrayDeque<? extends Node> cachedNodes = reusableFloatingNodes.get(nodeClass);
if (cachedNodes != null) {
Node node = cachedNodes.poll();
if (node != null) {
return node;
}
}
return nodeClass.allocateInstance();
}
private void releaseFloatingNode(Node node) {
ArrayDeque<Node> cachedNodes = reusableFloatingNodes.get(node.getNodeClass());
if (cachedNodes == null) {
cachedNodes = new ArrayDeque<>(2);
reusableFloatingNodes.put(node.getNodeClass(), cachedNodes);
}
cachedNodes.push(node);
}
protected Node handleFloatingNodeBeforeAdd(MethodScope methodScope, LoopScope loopScope, Node node) {
return node;
}
protected Node handleFloatingNodeAfterAdd(MethodScope methodScope, LoopScope loopScope, Node node) {
return node;
}
protected void makeSuccessorStubs(MethodScope methodScope, LoopScope loopScope, Node node, boolean updatePredecessors) {
Edges edges = node.getNodeClass().getSuccessorEdges();
for (int index = 0; index < edges.getDirectCount(); index++) {
if (skipDirectEdge(node, edges, index)) {
continue;
}
int orderId = readOrderId(methodScope);
Node value = makeStubNode(methodScope, loopScope, orderId);
edges.initializeNode(node, index, value);
if (updatePredecessors && value != null) {
edges.update(node, null, value);
}
}
for (int index = edges.getDirectCount(); index < edges.getCount(); index++) {
int size = methodScope.reader.getSVInt();
if (size != -1) {
NodeList<Node> nodeList = new NodeSuccessorList<>(node, size);
edges.initializeList(node, index, nodeList);
for (int idx = 0; idx < size; idx++) {
int orderId = readOrderId(methodScope);
Node value = makeStubNode(methodScope, loopScope, orderId);
nodeList.initialize(idx, value);
if (updatePredecessors && value != null) {
edges.update(node, null, value);
}
}
}
}
}
protected FixedNode makeStubNode(MethodScope methodScope, LoopScope loopScope, int nodeOrderId) {
if (nodeOrderId == GraphEncoder.NULL_ORDER_ID) {
return null;
}
FixedNode node = (FixedNode) lookupNode(loopScope, nodeOrderId);
if (node != null) {
return node;
}
long readerByteIndex = methodScope.reader.getByteIndex();
methodScope.reader.setByteIndex(methodScope.encodedGraph.nodeStartOffsets[nodeOrderId]);
NodeClass<?> nodeClass = methodScope.encodedGraph.getNodeClasses()[methodScope.reader.getUVInt()];
Node stubNode = nodeClass.allocateInstance();
if (graph.trackNodeSourcePosition()) {
stubNode.setNodeSourcePosition(NodeSourcePosition.placeholder(graph.method()));
}
node = (FixedNode) graph.add(stubNode);
methodScope.reader.setByteIndex(readerByteIndex);
registerNode(loopScope, nodeOrderId, node, false, false);
loopScope.nodesToProcess.set(nodeOrderId);
return node;
}
protected static boolean skipDirectEdge(Node node, Edges edges, int index) {
if (node instanceof Invoke) {
assert node instanceof InvokeNode || node instanceof InvokeWithExceptionNode : "The only two Invoke node classes. Got " + node.getClass();
if (edges.type() == Edges.Type.Successors) {
assert edges.getCount() == (node instanceof InvokeWithExceptionNode ? 2 : 1) : "InvokeNode has one successor (next); InvokeWithExceptionNode has two successors (next, exceptionEdge)";
return true;
} else {
assert edges.type() == Edges.Type.Inputs;
if (edges.getType(index) == CallTargetNode.class) {
return true;
} else if (edges.getType(index) == FrameState.class) {
assert edges.get(node, index) == null || edges.get(node, index) == ((Invoke) node).stateAfter() : "Only stateAfter can be a FrameState during encoding";
return true;
}
}
} else if (node instanceof LoopExitNode && edges.type() == Edges.Type.Inputs && edges.getType(index) == FrameState.class) {
return true;
}
return false;
}
protected Node lookupNode(LoopScope loopScope, int nodeOrderId) {
return loopScope.createdNodes[nodeOrderId];
}
protected void registerNode(LoopScope loopScope, int nodeOrderId, Node node, boolean allowOverwrite, boolean allowNull) {
assert node == null || node.isAlive();
assert allowNull || node != null;
assert allowOverwrite || lookupNode(loopScope, nodeOrderId) == null;
loopScope.createdNodes[nodeOrderId] = node;
}
protected int readOrderId(MethodScope methodScope) {
return methodScope.reader.getUVInt();
}
protected Object readObject(MethodScope methodScope) {
return methodScope.encodedGraph.getObjects()[methodScope.reader.getUVInt()];
}
protected void cleanupGraph(MethodScope methodScope) {
assert verifyEdges();
}
protected boolean verifyEdges() {
for (Node node : graph.getNodes()) {
assert node.isAlive();
for (Node i : node.inputs()) {
assert i.isAlive();
assert i.usages().contains(node);
}
for (Node s : node.successors()) {
assert s.isAlive();
assert s.predecessor() == node;
}
for (Node usage : node.usages()) {
assert usage.isAlive();
assert usage.inputs().contains(node) : node + " / " + usage + " / " + usage.inputs().count();
}
if (node.predecessor() != null) {
assert node.predecessor().isAlive();
assert node.predecessor().successors().contains(node);
}
}
return true;
}
}
class LoopDetector implements Runnable {
static class Loop {
MergeNode ;
List<EndNode> ends = new ArrayList<>(2);
List<AbstractEndNode> exits = new ArrayList<>();
boolean irreducible;
}
private final StructuredGraph graph;
private final MethodScope methodScope;
private Loop irreducibleLoopHandler;
private IntegerSwitchNode irreducibleLoopSwitch;
protected LoopDetector(StructuredGraph graph, MethodScope methodScope) {
this.graph = graph;
this.methodScope = methodScope;
}
@Override
public void run() {
DebugContext debug = graph.getDebug();
debug.dump(DebugContext.DETAILED_LEVEL, graph, "Before loop detection");
if (methodScope.loopExplosionHead == null) {
return;
}
List<Loop> orderedLoops = findLoops();
assert orderedLoops.get(orderedLoops.size() - 1) == irreducibleLoopHandler : "outermost loop must be the last element in the list";
for (Loop loop : orderedLoops) {
if (loop.ends.isEmpty()) {
assert loop == irreducibleLoopHandler;
continue;
}
findLoopExits(loop);
if (loop.irreducible) {
handleIrreducibleLoop(loop);
} else {
insertLoopNodes(loop);
}
debug.dump(DebugContext.DETAILED_LEVEL, graph, "After handling of loop %s", loop.header);
}
logIrreducibleLoops();
debug.dump(DebugContext.DETAILED_LEVEL, graph, "After loop detection");
}
private List<Loop> findLoops() {
EconomicMap<MergeNode, Loop> unorderedLoops = EconomicMap.create(Equivalence.IDENTITY);
List<Loop> orderedLoops = new ArrayList<>();
irreducibleLoopHandler = findOrCreateLoop(unorderedLoops, methodScope.loopExplosionHead);
NodeBitMap visited = graph.createNodeBitMap();
NodeBitMap active = graph.createNodeBitMap();
Deque<Node> stack = new ArrayDeque<>();
visited.mark(methodScope.loopExplosionHead);
stack.push(methodScope.loopExplosionHead);
while (!stack.isEmpty()) {
Node current = stack.peek();
assert visited.isMarked(current);
if (active.isMarked(current)) {
stack.pop();
active.clear(current);
if (current instanceof MergeNode) {
Loop loop = unorderedLoops.get((MergeNode) current);
if (loop != null) {
assert !orderedLoops.contains(loop);
orderedLoops.add(loop);
}
}
} else {
active.mark(current);
for (Node successor : current.cfgSuccessors()) {
if (active.isMarked(successor)) {
Loop loop = findOrCreateLoop(unorderedLoops, (MergeNode) successor);
assert !loop.ends.contains(current);
loop.ends.add((EndNode) current);
} else if (visited.isMarked(successor)) {
} else {
visited.mark(successor);
stack.push(successor);
}
}
}
}
return orderedLoops;
}
private Loop findOrCreateLoop(EconomicMap<MergeNode, Loop> unorderedLoops, MergeNode loopHeader) {
assert methodScope.loopExplosionMerges.contains(loopHeader) : loopHeader;
Loop loop = unorderedLoops.get(loopHeader);
if (loop == null) {
loop = new Loop();
loop.header = loopHeader;
unorderedLoops.put(loopHeader, loop);
}
return loop;
}
private void findLoopExits(Loop loop) {
List<Node> possibleExits = new ArrayList<>();
NodeBitMap visited = graph.createNodeBitMap();
Deque<Node> stack = new ArrayDeque<>();
for (EndNode loopEnd : loop.ends) {
stack.push(loopEnd);
visited.mark(loopEnd);
}
while (!stack.isEmpty()) {
Node current = stack.pop();
if (current == loop.header) {
continue;
}
if (!graph.isNew(methodScope.methodStartMark, current)) {
loop.irreducible = true;
return;
}
for (Node predecessor : current.cfgPredecessors()) {
if (predecessor instanceof LoopExitNode) {
LoopBeginNode innerLoopBegin = ((LoopExitNode) predecessor).loopBegin();
if (!visited.isMarked(innerLoopBegin)) {
stack.push(innerLoopBegin);
visited.mark(innerLoopBegin);
for (LoopExitNode exit : innerLoopBegin.loopExits()) {
possibleExits.add(exit);
}
}
} else if (!visited.isMarked(predecessor)) {
stack.push(predecessor);
visited.mark(predecessor);
if (predecessor instanceof ControlSplitNode) {
for (Node succ : predecessor.cfgSuccessors()) {
possibleExits.add(succ);
}
}
}
}
}
for (Node succ : possibleExits) {
if (!visited.contains(succ)) {
stack.push(succ);
visited.mark(succ);
assert !methodScope.loopExplosionMerges.contains(succ);
}
}
while (!stack.isEmpty()) {
Node current = stack.pop();
assert visited.isMarked(current);
assert current instanceof ControlSinkNode || current instanceof LoopEndNode || current.cfgSuccessors().iterator().hasNext() : "Must not reach a node that has not been decoded yet";
for (Node successor : current.cfgSuccessors()) {
if (visited.isMarked(successor)) {
} else if (methodScope.loopExplosionMerges.contains(successor)) {
assert successor instanceof MergeNode;
assert !loop.exits.contains(current);
loop.exits.add((AbstractEndNode) current);
} else {
visited.mark(successor);
stack.push(successor);
}
}
}
}
private void insertLoopNodes(Loop loop) {
MergeNode merge = loop.header;
FrameState stateAfter = merge.stateAfter().duplicate();
FixedNode afterMerge = merge.next();
merge.setNext(null);
EndNode preLoopEnd = graph.add(new EndNode());
LoopBeginNode loopBegin = graph.add(new LoopBeginNode());
merge.setNext(preLoopEnd);
loopBegin.addForwardEnd(preLoopEnd);
loopBegin.setNext(afterMerge);
loopBegin.setStateAfter(stateAfter);
List<PhiNode> mergePhis = merge.phis().snapshot();
List<PhiNode> loopBeginPhis = new ArrayList<>(mergePhis.size());
for (int i = 0; i < mergePhis.size(); i++) {
PhiNode mergePhi = mergePhis.get(i);
PhiNode loopBeginPhi = graph.addWithoutUnique(new ValuePhiNode(mergePhi.stamp(NodeView.DEFAULT), loopBegin));
mergePhi.replaceAtUsages(loopBeginPhi);
loopBeginPhi.addInput(mergePhi);
loopBeginPhis.add(loopBeginPhi);
}
for (EndNode endNode : loop.ends) {
for (int i = 0; i < mergePhis.size(); i++) {
PhiNode mergePhi = mergePhis.get(i);
PhiNode loopBeginPhi = loopBeginPhis.get(i);
loopBeginPhi.addInput(mergePhi.valueAt(endNode));
}
merge.removeEnd(endNode);
LoopEndNode loopEnd = graph.add(new LoopEndNode(loopBegin));
endNode.replaceAndDelete(loopEnd);
}
for (AbstractEndNode exit : loop.exits) {
AbstractMergeNode loopExplosionMerge = exit.merge();
assert methodScope.loopExplosionMerges.contains(loopExplosionMerge);
LoopExitNode loopExit = graph.add(new LoopExitNode(loopBegin));
exit.replaceAtPredecessor(loopExit);
loopExit.setNext(exit);
assignLoopExitState(loopExit, loopExplosionMerge, exit);
}
}
private void assignLoopExitState(LoopExitNode loopExit, AbstractMergeNode loopExplosionMerge, AbstractEndNode loopExplosionEnd) {
FrameState oldState = loopExplosionMerge.stateAfter();
EconomicSet<Node> loopBeginValues = EconomicSet.create(Equivalence.IDENTITY);
for (FrameState state = loopExit.loopBegin().stateAfter(); state != null; state = state.outerFrameState()) {
for (ValueNode value : state.values()) {
if (value != null && !value.isConstant() && !loopExit.loopBegin().isPhiAtMerge(value)) {
loopBeginValues.add(ProxyPlaceholder.unwrap(value));
}
}
}
List<ValueNode> newValues = new ArrayList<>(oldState.values().size());
for (ValueNode v : oldState.values()) {
ValueNode value = v;
ValueNode realValue = ProxyPlaceholder.unwrap(value);
if (realValue instanceof PhiNode && loopExplosionMerge.isPhiAtMerge(realValue)) {
value = ((PhiNode) realValue).valueAt(loopExplosionEnd);
realValue = ProxyPlaceholder.unwrap(value);
}
if (realValue == null || realValue.isConstant() || loopBeginValues.contains(realValue) || !graph.isNew(methodScope.methodStartMark, realValue)) {
newValues.add(realValue);
} else {
GraalError.guarantee(value instanceof ProxyPlaceholder && ((ProxyPlaceholder) value).proxyPoint == loopExplosionMerge,
"Value flowing out of loop, but we are not prepared to insert a ProxyNode");
ProxyPlaceholder proxyPlaceholder = (ProxyPlaceholder) value;
ValueProxyNode proxy = ProxyNode.forValue(proxyPlaceholder.value, loopExit, graph);
proxyPlaceholder.setValue(proxy);
newValues.add(proxy);
}
}
FrameState newState = new FrameState(oldState.outerFrameState(), oldState.getCode(), oldState.bci, newValues, oldState.localsSize(), oldState.stackSize(), oldState.rethrowException(),
oldState.duringCall(), oldState.monitorIds(), oldState.virtualObjectMappings());
assert loopExit.stateAfter() == null;
loopExit.setStateAfter(graph.add(newState));
}
private void handleIrreducibleLoop(Loop loop) {
assert loop != irreducibleLoopHandler;
FrameState loopState = loop.header.stateAfter();
FrameState explosionHeadState = irreducibleLoopHandler.header.stateAfter();
assert loopState.outerFrameState() == explosionHeadState.outerFrameState();
NodeInputList<ValueNode> loopValues = loopState.values();
NodeInputList<ValueNode> explosionHeadValues = explosionHeadState.values();
assert loopValues.size() == explosionHeadValues.size();
int loopVariableIndex = -1;
ValueNode loopValue = null;
ValueNode explosionHeadValue = null;
for (int i = 0; i < loopValues.size(); i++) {
ValueNode curLoopValue = loopValues.get(i);
ValueNode curExplosionHeadValue = explosionHeadValues.get(i);
if (curLoopValue != curExplosionHeadValue) {
if (loopVariableIndex != -1) {
throw bailout("must have only one variable that is changed in loop. " + loopValue + " != " + explosionHeadValue + " and " + curLoopValue + " != " + curExplosionHeadValue);
}
loopVariableIndex = i;
loopValue = curLoopValue;
explosionHeadValue = curExplosionHeadValue;
}
}
assert loopVariableIndex != -1;
ValuePhiNode loopVariablePhi;
SortedMap<Integer, AbstractBeginNode> dispatchTable = new TreeMap<>();
AbstractBeginNode unreachableDefaultSuccessor;
if (irreducibleLoopSwitch == null) {
assert !irreducibleLoopHandler.header.isPhiAtMerge(explosionHeadValue);
assert irreducibleLoopHandler.header.phis().isEmpty();
loopVariablePhi = graph.addWithoutUnique(new ValuePhiNode(explosionHeadValue.stamp(NodeView.DEFAULT).unrestricted(), irreducibleLoopHandler.header));
for (int i = 0; i < irreducibleLoopHandler.header.phiPredecessorCount(); i++) {
loopVariablePhi.addInput(explosionHeadValue);
}
FrameState oldFrameState = explosionHeadState;
List<ValueNode> newFrameStateValues = new ArrayList<>(explosionHeadValues.size());
for (int i = 0; i < explosionHeadValues.size(); i++) {
if (i == loopVariableIndex) {
newFrameStateValues.add(loopVariablePhi);
} else {
newFrameStateValues.add(explosionHeadValues.get(i));
}
}
FrameState newFrameState = graph.add(
new FrameState(oldFrameState.outerFrameState(), oldFrameState.getCode(), oldFrameState.bci, newFrameStateValues, oldFrameState.localsSize(),
oldFrameState.stackSize(), oldFrameState.rethrowException(), oldFrameState.duringCall(), oldFrameState.monitorIds(),
oldFrameState.virtualObjectMappings()));
oldFrameState.replaceAtUsages(newFrameState);
FixedNode handlerNext = irreducibleLoopHandler.header.next();
irreducibleLoopHandler.header.setNext(null);
BeginNode handlerBegin = graph.add(new BeginNode());
handlerBegin.setNext(handlerNext);
dispatchTable.put(asInt(explosionHeadValue), handlerBegin);
unreachableDefaultSuccessor = graph.add(new BeginNode());
DeoptimizeNode deopt = graph.add(new DeoptimizeNode(DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.UnreachedCode));
unreachableDefaultSuccessor.setNext(deopt);
} else {
assert irreducibleLoopHandler.header.isPhiAtMerge(explosionHeadValue);
assert irreducibleLoopHandler.header.phis().count() == 1 && irreducibleLoopHandler.header.phis().first() == explosionHeadValue;
assert irreducibleLoopSwitch.value() == explosionHeadValue;
loopVariablePhi = (ValuePhiNode) explosionHeadValue;
for (int i = 0; i < irreducibleLoopSwitch.keyCount(); i++) {
int key = irreducibleLoopSwitch.keyAt(i).asInt();
dispatchTable.put(key, irreducibleLoopSwitch.successorAtKey(key));
}
unreachableDefaultSuccessor = irreducibleLoopSwitch.defaultSuccessor();
assert irreducibleLoopHandler.header.next() == irreducibleLoopSwitch;
irreducibleLoopHandler.header.setNext(null);
irreducibleLoopSwitch.clearSuccessors();
irreducibleLoopSwitch.safeDelete();
}
assert loop.header.phis().isEmpty();
BeginNode dispatchBegin = graph.add(new BeginNode());
EndNode dispatchEnd = graph.add(new EndNode());
dispatchBegin.setNext(dispatchEnd);
loop.header.addForwardEnd(dispatchEnd);
int intLoopValue = asInt(loopValue);
assert !dispatchTable.containsKey(intLoopValue);
dispatchTable.put(intLoopValue, dispatchBegin);
for (EndNode end : loop.ends) {
loop.header.removeEnd(end);
irreducibleLoopHandler.ends.add(end);
irreducibleLoopHandler.header.addForwardEnd(end);
loopVariablePhi.addInput(loopValue);
}
irreducibleLoopSwitch = graph.add(createSwitch(loopVariablePhi, dispatchTable, unreachableDefaultSuccessor));
irreducibleLoopHandler.header.setNext(irreducibleLoopSwitch);
}
private static int asInt(ValueNode node) {
if (!node.isConstant() || node.asJavaConstant().getJavaKind() != JavaKind.Int) {
throw bailout("must have a loop variable of type int. " + node);
}
return node.asJavaConstant().asInt();
}
private static RuntimeException bailout(String msg) {
throw new PermanentBailoutException("Graal implementation restriction: Method with %s loop explosion %s", LoopExplosionKind.MERGE_EXPLODE, msg);
}
private static IntegerSwitchNode createSwitch(ValuePhiNode switchedValue, SortedMap<Integer, AbstractBeginNode> dispatchTable, AbstractBeginNode defaultSuccessor) {
int numKeys = dispatchTable.size();
int numSuccessors = numKeys + 1;
AbstractBeginNode[] switchSuccessors = new AbstractBeginNode[numSuccessors];
int[] switchKeys = new int[numKeys];
double[] switchKeyProbabilities = new double[numSuccessors];
int[] switchKeySuccessors = new int[numSuccessors];
int idx = 0;
for (Map.Entry<Integer, AbstractBeginNode> entry : dispatchTable.entrySet()) {
switchSuccessors[idx] = entry.getValue();
switchKeys[idx] = entry.getKey();
switchKeyProbabilities[idx] = 1d / numKeys;
switchKeySuccessors[idx] = idx;
idx++;
}
switchSuccessors[idx] = defaultSuccessor;
switchKeyProbabilities[idx] = 0;
switchKeySuccessors[idx] = idx;
return new IntegerSwitchNode(switchedValue, switchSuccessors, switchKeys, switchKeyProbabilities, switchKeySuccessors);
}
@SuppressWarnings("try")
private void logIrreducibleLoops() {
DebugContext debug = graph.getDebug();
try (DebugContext.Scope s = debug.scope("IrreducibleLoops")) {
if (debug.isLogEnabled(DebugContext.BASIC_LEVEL) && irreducibleLoopSwitch != null) {
StringBuilder msg = new StringBuilder("Inserted state machine to remove irreducible loops. Dispatching to the following states: ");
String sep = "";
for (int i = 0; i < irreducibleLoopSwitch.keyCount(); i++) {
msg.append(sep).append(irreducibleLoopSwitch.keyAt(i).asInt());
sep = ", ";
}
debug.log(DebugContext.BASIC_LEVEL, "%s", msg);
}
}
}
}