package org.graalvm.compiler.hotspot.phases;
import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required;
import org.graalvm.compiler.common.PermanentBailoutException;
import org.graalvm.compiler.core.common.cfg.Loop;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.iterators.NodeIterable;
import org.graalvm.compiler.loop.LoopsData;
import org.graalvm.compiler.loop.phases.LoopTransformations;
import org.graalvm.compiler.nodeinfo.InputType;
import org.graalvm.compiler.nodeinfo.Verbosity;
import org.graalvm.compiler.nodes.AbstractBeginNode;
import org.graalvm.compiler.nodes.EntryMarkerNode;
import org.graalvm.compiler.nodes.EntryProxyNode;
import org.graalvm.compiler.nodes.FixedNode;
import org.graalvm.compiler.nodes.FrameState;
import org.graalvm.compiler.nodes.StartNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.cfg.Block;
import org.graalvm.compiler.nodes.extended.OSRLocalNode;
import org.graalvm.compiler.nodes.extended.OSRStartNode;
import org.graalvm.compiler.nodes.util.GraphUtil;
import org.graalvm.compiler.phases.Phase;
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
import jdk.vm.ci.runtime.JVMCICompiler;
public class OnStackReplacementPhase extends Phase {
@Override
protected void run(StructuredGraph graph) {
if (graph.getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI) {
assert graph.getNodes(EntryMarkerNode.TYPE).isEmpty();
return;
}
Debug.dump(Debug.INFO_LOG_LEVEL, graph, "OnStackReplacement initial");
EntryMarkerNode osr;
int maxIterations = -1;
int iterations = 0;
do {
osr = getEntryMarker(graph);
LoopsData loops = new LoopsData(graph);
Loop<Block> l = loops.getCFG().getNodeToBlock().get(osr).getLoop();
if (l == null) {
break;
}
iterations++;
if (maxIterations == -1) {
maxIterations = l.getDepth();
} else if (iterations > maxIterations) {
throw GraalError.shouldNotReachHere();
}
while (l.getParent() != null) {
l = l.getParent();
}
LoopTransformations.peel(loops.loop(l));
osr.replaceAtUsages(InputType.Guard, AbstractBeginNode.prevBegin((FixedNode) osr.predecessor()));
for (Node usage : osr.usages().snapshot()) {
EntryProxyNode proxy = (EntryProxyNode) usage;
proxy.replaceAndDelete(proxy.value());
}
GraphUtil.removeFixedWithUnusedInputs(osr);
Debug.dump(Debug.INFO_LOG_LEVEL, graph, "OnStackReplacement loop peeling result");
} while (true);
FrameState osrState = osr.stateAfter();
osr.setStateAfter(null);
OSRStartNode osrStart = graph.add(new OSRStartNode());
StartNode start = graph.start();
FixedNode next = osr.next();
osr.setNext(null);
osrStart.setNext(next);
graph.setStart(osrStart);
osrStart.setStateAfter(osrState);
for (int i = 0; i < osrState.localsSize(); i++) {
ValueNode value = osrState.localAt(i);
if (value instanceof EntryProxyNode) {
EntryProxyNode proxy = (EntryProxyNode) value;
proxy.replaceAndDelete(graph.addOrUnique(new OSRLocalNode(i, proxy.stamp().unrestricted())));
} else {
assert value == null || value instanceof OSRLocalNode;
}
}
osr.replaceAtUsages(InputType.Guard, osrStart);
assert osr.usages().isEmpty();
GraphUtil.killCFG(start);
Debug.dump(Debug.INFO_LOG_LEVEL, graph, "OnStackReplacement result");
new DeadCodeEliminationPhase(Required).apply(graph);
}
private static EntryMarkerNode getEntryMarker(StructuredGraph graph) {
NodeIterable<EntryMarkerNode> osrNodes = graph.getNodes(EntryMarkerNode.TYPE);
EntryMarkerNode osr = osrNodes.first();
if (osr == null) {
throw new PermanentBailoutException("No OnStackReplacementNode generated");
}
if (osrNodes.count() > 1) {
throw new GraalError("Multiple OnStackReplacementNodes generated");
}
if (osr.stateAfter().locksSize() != 0) {
throw new PermanentBailoutException("OSR with locks not supported");
}
if (osr.stateAfter().stackSize() != 0) {
throw new PermanentBailoutException("OSR with stack entries not supported: %s", osr.stateAfter().toString(Verbosity.Debugger));
}
return osr;
}
@Override
public float codeSizeIncrease() {
return 5.0f;
}
}