use of org.graalvm.compiler.graph.Graph.DuplicationReplacement in project graal by oracle.
the class LoopFragmentInside method getDuplicationReplacement.
@Override
@SuppressWarnings("try")
protected DuplicationReplacement getDuplicationReplacement() {
final LoopBeginNode loopBegin = loop().loopBegin();
final StructuredGraph graph = graph();
return new DuplicationReplacement() {
private EconomicMap<Node, Node> seenNode = EconomicMap.create(Equivalence.IDENTITY);
@Override
public Node replacement(Node original) {
try (DebugCloseable position = original.withNodeSourcePosition()) {
if (original == loopBegin) {
Node value = seenNode.get(original);
if (value != null) {
return value;
}
AbstractBeginNode newValue = graph.add(new BeginNode());
seenNode.put(original, newValue);
return newValue;
}
if (original instanceof LoopExitNode && ((LoopExitNode) original).loopBegin() == loopBegin) {
Node value = seenNode.get(original);
if (value != null) {
return value;
}
AbstractBeginNode newValue = graph.add(new BeginNode());
seenNode.put(original, newValue);
return newValue;
}
if (original instanceof LoopEndNode && ((LoopEndNode) original).loopBegin() == loopBegin) {
Node value = seenNode.get(original);
if (value != null) {
return value;
}
EndNode newValue = graph.add(new EndNode());
seenNode.put(original, newValue);
return newValue;
}
return original;
}
}
};
}
use of org.graalvm.compiler.graph.Graph.DuplicationReplacement in project graal by oracle.
the class LoopFragmentWhole method getDuplicationReplacement.
@Override
protected DuplicationReplacement getDuplicationReplacement() {
final FixedNode entry = loop().entryPoint();
final Graph graph = this.graph();
return new DuplicationReplacement() {
private EndNode endNode;
@Override
public Node replacement(Node o) {
if (o == entry) {
if (endNode == null) {
endNode = graph.add(new EndNode());
}
return endNode;
}
return o;
}
};
}
use of org.graalvm.compiler.graph.Graph.DuplicationReplacement in project graal by oracle.
the class LoopFragment method patchNodes.
protected void patchNodes(final DuplicationReplacement dataFix) {
if (isDuplicate() && !nodesReady) {
assert !original.isDuplicate();
final DuplicationReplacement cfgFix = original().getDuplicationReplacement();
DuplicationReplacement dr;
if (cfgFix == null && dataFix != null) {
dr = dataFix;
} else if (cfgFix != null && dataFix == null) {
dr = cfgFix;
} else if (cfgFix != null && dataFix != null) {
dr = new DuplicationReplacement() {
@Override
public Node replacement(Node o) {
Node r1 = dataFix.replacement(o);
if (r1 != o) {
assert cfgFix.replacement(o) == o;
return r1;
}
Node r2 = cfgFix.replacement(o);
if (r2 != o) {
return r2;
}
return o;
}
};
} else {
dr = null;
}
beforeDuplication();
NodeIterable<Node> nodesIterable = original().nodes();
duplicationMap = graph().addDuplicates(nodesIterable, graph(), nodesIterable.count(), dr);
finishDuplication();
nodes = new NodeBitMap(graph());
nodes.markAll(duplicationMap.getValues());
nodesReady = true;
} else {
// TODO (gd) apply fix ?
}
}
use of org.graalvm.compiler.graph.Graph.DuplicationReplacement in project graal by oracle.
the class InliningUtil method inline.
/**
* Performs an actual inlining, thereby replacing the given invoke with the given
* {@code inlineGraph}.
*
* @param invoke the invoke that will be replaced
* @param inlineGraph the graph that the invoke will be replaced with
* @param receiverNullCheck true if a null check needs to be generated for non-static inlinings,
* false if no such check is required
* @param inlineeMethod the actual method being inlined. Maybe be null for snippets.
* @param reason the reason for inlining, used in tracing
* @param phase the phase that invoked inlining
*/
@SuppressWarnings("try")
public static UnmodifiableEconomicMap<Node, Node> inline(Invoke invoke, StructuredGraph inlineGraph, boolean receiverNullCheck, ResolvedJavaMethod inlineeMethod, String reason, String phase) {
FixedNode invokeNode = invoke.asNode();
StructuredGraph graph = invokeNode.graph();
final NodeInputList<ValueNode> parameters = invoke.callTarget().arguments();
assert inlineGraph.getGuardsStage().ordinal() >= graph.getGuardsStage().ordinal();
assert !invokeNode.graph().isAfterFloatingReadPhase() : "inline isn't handled correctly after floating reads phase";
if (receiverNullCheck && !((MethodCallTargetNode) invoke.callTarget()).isStatic()) {
nonNullReceiver(invoke);
}
ArrayList<Node> nodes = new ArrayList<>(inlineGraph.getNodes().count());
ArrayList<ReturnNode> returnNodes = new ArrayList<>(4);
ArrayList<Invoke> partialIntrinsicExits = new ArrayList<>();
UnwindNode unwindNode = null;
final StartNode entryPointNode = inlineGraph.start();
FixedNode firstCFGNode = entryPointNode.next();
if (firstCFGNode == null) {
throw new IllegalStateException("Inlined graph is in invalid state: " + inlineGraph);
}
for (Node node : inlineGraph.getNodes()) {
if (node == entryPointNode || (node == entryPointNode.stateAfter() && node.usages().count() == 1) || node instanceof ParameterNode) {
// Do nothing.
} else {
nodes.add(node);
if (node instanceof ReturnNode) {
returnNodes.add((ReturnNode) node);
} else if (node instanceof Invoke) {
Invoke invokeInInlineGraph = (Invoke) node;
if (invokeInInlineGraph.bci() == BytecodeFrame.UNKNOWN_BCI) {
ResolvedJavaMethod target1 = inlineeMethod;
ResolvedJavaMethod target2 = invokeInInlineGraph.callTarget().targetMethod();
assert target1.equals(target2) : String.format("invoke in inlined method expected to be partial intrinsic exit (i.e., call to %s), not a call to %s", target1.format("%H.%n(%p)"), target2.format("%H.%n(%p)"));
partialIntrinsicExits.add(invokeInInlineGraph);
}
} else if (node instanceof UnwindNode) {
assert unwindNode == null;
unwindNode = (UnwindNode) node;
}
}
}
final AbstractBeginNode prevBegin = AbstractBeginNode.prevBegin(invokeNode);
DuplicationReplacement localReplacement = new DuplicationReplacement() {
@Override
public Node replacement(Node node) {
if (node instanceof ParameterNode) {
return parameters.get(((ParameterNode) node).index());
} else if (node == entryPointNode) {
return prevBegin;
}
return node;
}
};
assert invokeNode.successors().first() != null : invoke;
assert invokeNode.predecessor() != null;
Mark mark = graph.getMark();
// Instead, attach the inlining log of the child graph to the current inlining log.
EconomicMap<Node, Node> duplicates;
try (InliningLog.UpdateScope scope = graph.getInliningLog().openDefaultUpdateScope()) {
duplicates = graph.addDuplicates(nodes, inlineGraph, inlineGraph.getNodeCount(), localReplacement);
if (scope != null) {
graph.getInliningLog().addDecision(invoke, true, reason, phase, duplicates, inlineGraph.getInliningLog());
}
}
FrameState stateAfter = invoke.stateAfter();
assert stateAfter == null || stateAfter.isAlive();
FrameState stateAtExceptionEdge = null;
if (invoke instanceof InvokeWithExceptionNode) {
InvokeWithExceptionNode invokeWithException = ((InvokeWithExceptionNode) invoke);
if (unwindNode != null) {
ExceptionObjectNode obj = (ExceptionObjectNode) invokeWithException.exceptionEdge();
stateAtExceptionEdge = obj.stateAfter();
}
}
updateSourcePositions(invoke, inlineGraph, duplicates, !Objects.equals(inlineGraph.method(), inlineeMethod), mark);
if (stateAfter != null) {
processFrameStates(invoke, inlineGraph, duplicates, stateAtExceptionEdge, returnNodes.size() > 1);
int callerLockDepth = stateAfter.nestedLockDepth();
if (callerLockDepth != 0) {
for (MonitorIdNode original : inlineGraph.getNodes(MonitorIdNode.TYPE)) {
MonitorIdNode monitor = (MonitorIdNode) duplicates.get(original);
processMonitorId(invoke.stateAfter(), monitor);
}
}
} else {
assert checkContainsOnlyInvalidOrAfterFrameState(duplicates);
}
firstCFGNode = (FixedNode) duplicates.get(firstCFGNode);
for (int i = 0; i < returnNodes.size(); i++) {
returnNodes.set(i, (ReturnNode) duplicates.get(returnNodes.get(i)));
}
for (Invoke exit : partialIntrinsicExits) {
// A partial intrinsic exit must be replaced with a call to
// the intrinsified method.
Invoke dup = (Invoke) duplicates.get(exit.asNode());
if (dup instanceof InvokeNode) {
InvokeNode repl = graph.add(new InvokeNode(invoke.callTarget(), invoke.bci()));
dup.intrinsify(repl.asNode());
} else {
((InvokeWithExceptionNode) dup).replaceWithNewBci(invoke.bci());
}
}
if (unwindNode != null) {
unwindNode = (UnwindNode) duplicates.get(unwindNode);
}
finishInlining(invoke, graph, firstCFGNode, returnNodes, unwindNode, inlineGraph.getAssumptions(), inlineGraph);
GraphUtil.killCFG(invokeNode);
return duplicates;
}
Aggregations