use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class GraphUtil method fixSurvivingAffectedMerges.
private static void fixSurvivingAffectedMerges(EconomicSet<Node> markedNodes, EconomicMap<AbstractMergeNode, List<AbstractEndNode>> unmarkedMerges) {
MapCursor<AbstractMergeNode, List<AbstractEndNode>> cursor = unmarkedMerges.getEntries();
while (cursor.advance()) {
AbstractMergeNode merge = cursor.getKey();
for (AbstractEndNode end : cursor.getValue()) {
merge.removeEnd(end);
}
if (merge.phiPredecessorCount() == 1) {
if (merge instanceof LoopBeginNode) {
LoopBeginNode loopBegin = (LoopBeginNode) merge;
assert merge.forwardEndCount() == 1;
for (LoopExitNode loopExit : loopBegin.loopExits().snapshot()) {
if (markedNodes.contains(loopExit)) {
/*
* disconnect from loop begin so that reduceDegenerateLoopBegin doesn't
* transform it into a new beginNode
*/
loopExit.replaceFirstInput(loopBegin, null);
}
}
merge.graph().reduceDegenerateLoopBegin(loopBegin);
} else {
merge.graph().reduceTrivialMerge(merge);
}
} else {
assert merge.phiPredecessorCount() > 1 : merge;
}
}
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class BytecodeParser method parseAndInlineCallee.
protected void parseAndInlineCallee(ResolvedJavaMethod targetMethod, ValueNode[] args, IntrinsicContext calleeIntrinsicContext) {
FixedWithNextNode calleeBeforeUnwindNode = null;
ValueNode calleeUnwindValue = null;
try (IntrinsicScope s = calleeIntrinsicContext != null && !parsingIntrinsic() ? new IntrinsicScope(this, targetMethod.getSignature().toParameterKinds(!targetMethod.isStatic()), args) : null) {
BytecodeParser parser = graphBuilderInstance.createBytecodeParser(graph, this, targetMethod, INVOCATION_ENTRY_BCI, calleeIntrinsicContext);
FrameStateBuilder startFrameState = new FrameStateBuilder(parser, parser.code, graph);
if (!targetMethod.isStatic()) {
args[0] = nullCheckedValue(args[0]);
}
startFrameState.initializeFromArgumentsArray(args);
parser.build(this.lastInstr, startFrameState);
if (parser.returnDataList == null) {
/* Callee does not return. */
lastInstr = null;
} else {
ValueNode calleeReturnValue;
MergeNode returnMergeNode = null;
if (s != null) {
s.returnDataList = parser.returnDataList;
}
if (parser.returnDataList.size() == 1) {
/* Callee has a single return, we can continue parsing at that point. */
ReturnToCallerData singleReturnData = parser.returnDataList.get(0);
lastInstr = singleReturnData.beforeReturnNode;
calleeReturnValue = singleReturnData.returnValue;
} else {
assert parser.returnDataList.size() > 1;
/* Callee has multiple returns, we need to insert a control flow merge. */
returnMergeNode = graph.add(new MergeNode());
calleeReturnValue = ValueMergeUtil.mergeValueProducers(returnMergeNode, parser.returnDataList, returnData -> returnData.beforeReturnNode, returnData -> returnData.returnValue);
}
if (calleeReturnValue != null) {
frameState.push(targetMethod.getSignature().getReturnKind().getStackKind(), calleeReturnValue);
}
if (returnMergeNode != null) {
returnMergeNode.setStateAfter(createFrameState(stream.nextBCI(), returnMergeNode));
lastInstr = finishInstruction(returnMergeNode, frameState);
}
}
/*
* Propagate any side effects into the caller when parsing intrinsics.
*/
if (parser.frameState.isAfterSideEffect() && parsingIntrinsic()) {
for (StateSplit sideEffect : parser.frameState.sideEffects()) {
frameState.addSideEffect(sideEffect);
}
}
calleeBeforeUnwindNode = parser.getBeforeUnwindNode();
if (calleeBeforeUnwindNode != null) {
calleeUnwindValue = parser.getUnwindValue();
assert calleeUnwindValue != null;
}
}
/*
* Method handleException will call createTarget, which wires this exception edge to the
* corresponding exception dispatch block in the caller. In the case where it wires to the
* caller's unwind block, any FrameState created meanwhile, e.g., FrameState for
* LoopExitNode, would be instantiated with AFTER_EXCEPTION_BCI. Such frame states should
* not be fixed by IntrinsicScope.close, as they denote the states of the caller. Thus, the
* following code should be placed outside the IntrinsicScope, so that correctly created
* FrameStates are not replaced.
*/
if (calleeBeforeUnwindNode != null) {
calleeBeforeUnwindNode.setNext(handleException(calleeUnwindValue, bci(), false));
}
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class BytecodeParser method checkLoopExit.
private Target checkLoopExit(FixedNode target, BciBlock targetBlock, FrameStateBuilder state) {
if (currentBlock != null) {
long exits = currentBlock.loops & ~targetBlock.loops;
if (exits != 0) {
LoopExitNode firstLoopExit = null;
LoopExitNode lastLoopExit = null;
int pos = 0;
ArrayList<BciBlock> exitLoops = new ArrayList<>(Long.bitCount(exits));
do {
long lMask = 1L << pos;
if ((exits & lMask) != 0) {
exitLoops.add(blockMap.getLoopHeader(pos));
exits &= ~lMask;
}
pos++;
} while (exits != 0);
Collections.sort(exitLoops, new Comparator<BciBlock>() {
@Override
public int compare(BciBlock o1, BciBlock o2) {
return Long.bitCount(o2.loops) - Long.bitCount(o1.loops);
}
});
int bci = targetBlock.startBci;
if (targetBlock instanceof ExceptionDispatchBlock) {
bci = ((ExceptionDispatchBlock) targetBlock).deoptBci;
}
FrameStateBuilder newState = state.copy();
for (BciBlock loop : exitLoops) {
LoopBeginNode loopBegin = (LoopBeginNode) getFirstInstruction(loop);
LoopExitNode loopExit = graph.add(new LoopExitNode(loopBegin));
if (lastLoopExit != null) {
lastLoopExit.setNext(loopExit);
}
if (firstLoopExit == null) {
firstLoopExit = loopExit;
}
lastLoopExit = loopExit;
debug.log("Target %s Exits %s, scanning framestates...", targetBlock, loop);
newState.clearNonLiveLocals(targetBlock, liveness, true);
newState.insertLoopProxies(loopExit, getEntryState(loop));
loopExit.setStateAfter(newState.create(bci, loopExit));
}
lastLoopExit.setNext(target);
return new Target(firstLoopExit, newState);
}
}
return new Target(target, state);
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class LoopTransformations method insertPrePostLoops.
// This function splits candidate loops into pre, main and post loops,
// dividing the iteration space to facilitate the majority of iterations
// being executed in a main loop, which will have RCE implemented upon it.
// The initial loop form is constrained to single entry/exit, but can have
// flow. The translation looks like:
//
// @formatter:off
//
// (Simple Loop entry) (Pre Loop Entry)
// | |
// (LoopBeginNode) (LoopBeginNode)
// | |
// (Loop Control Test)<------ ==> (Loop control Test)<------
// / \ \ / \ \
// (Loop Exit) (Loop Body) | (Loop Exit) (Loop Body) |
// | | | | | |
// (continue code) (Loop End) | if (M < length)* (Loop End) |
// \ / / \ \ /
// -----> / | ----->
// / if ( ... )*
// / / \
// / / \
// / / \
// | / (Main Loop Entry)
// | | |
// | | (LoopBeginNode)
// | | |
// | | (Loop Control Test)<------
// | | / \ \
// | | (Loop Exit) (Loop Body) |
// \ \ | | |
// \ \ | (Loop End) |
// \ \ | \ /
// \ \ | ------>
// \ \ |
// (Main Loop Merge)*
// |
// (Post Loop Entry)
// |
// (LoopBeginNode)
// |
// (Loop Control Test)<-----
// / \ \
// (Loop Exit) (Loop Body) |
// | | |
// (continue code) (Loop End) |
// \ /
// ----->
//
// Key: "*" = optional.
// @formatter:on
//
// The value "M" is the maximal value of the loop trip for the original
// loop. The value of "length" is applicable to the number of arrays found
// in the loop but is reduced if some or all of the arrays are known to be
// the same length as "M". The maximum number of tests can be equal to the
// number of arrays in the loop, where multiple instances of an array are
// subsumed into a single test for that arrays length.
//
// If the optional main loop entry tests are absent, the Pre Loop exit
// connects to the Main loops entry and there is no merge hanging off the
// main loops exit to converge flow from said tests. All split use data
// flow is mitigated through phi(s) in the main merge if present and
// passed through the main and post loop phi(s) from the originating pre
// loop with final phi(s) and data flow patched to the "continue code".
// The pre loop is constrained to one iteration for now and will likely
// be updated to produce vector alignment if applicable.
public static LoopBeginNode insertPrePostLoops(LoopEx loop) {
StructuredGraph graph = loop.loopBegin().graph();
graph.getDebug().log("LoopTransformations.insertPrePostLoops %s", loop);
LoopFragmentWhole preLoop = loop.whole();
CountedLoopInfo preCounted = loop.counted();
IfNode preLimit = preCounted.getLimitTest();
assert preLimit != null;
LoopBeginNode preLoopBegin = loop.loopBegin();
InductionVariable preIv = preCounted.getCounter();
LoopExitNode preLoopExitNode = preLoopBegin.getSingleLoopExit();
FixedNode continuationNode = preLoopExitNode.next();
// Each duplication is inserted after the original, ergo create the post loop first
LoopFragmentWhole mainLoop = preLoop.duplicate();
LoopFragmentWhole postLoop = preLoop.duplicate();
preLoopBegin.incrementSplits();
preLoopBegin.incrementSplits();
preLoopBegin.setPreLoop();
graph.getDebug().dump(DebugContext.VERBOSE_LEVEL, graph, "After duplication");
LoopBeginNode mainLoopBegin = mainLoop.getDuplicatedNode(preLoopBegin);
mainLoopBegin.setMainLoop();
LoopBeginNode postLoopBegin = postLoop.getDuplicatedNode(preLoopBegin);
postLoopBegin.setPostLoop();
EndNode postEndNode = getBlockEndAfterLoopExit(postLoopBegin);
AbstractMergeNode postMergeNode = postEndNode.merge();
LoopExitNode postLoopExitNode = postLoopBegin.getSingleLoopExit();
// Update the main loop phi initialization to carry from the pre loop
for (PhiNode prePhiNode : preLoopBegin.phis()) {
PhiNode mainPhiNode = mainLoop.getDuplicatedNode(prePhiNode);
mainPhiNode.setValueAt(0, prePhiNode);
}
EndNode mainEndNode = getBlockEndAfterLoopExit(mainLoopBegin);
AbstractMergeNode mainMergeNode = mainEndNode.merge();
AbstractEndNode postEntryNode = postLoopBegin.forwardEnd();
// In the case of no Bounds tests, we just flow right into the main loop
AbstractBeginNode mainLandingNode = BeginNode.begin(postEntryNode);
LoopExitNode mainLoopExitNode = mainLoopBegin.getSingleLoopExit();
mainLoopExitNode.setNext(mainLandingNode);
preLoopExitNode.setNext(mainLoopBegin.forwardEnd());
// Add and update any phi edges as per merge usage as needed and update usages
processPreLoopPhis(loop, mainLoop, postLoop);
continuationNode.predecessor().clearSuccessors();
postLoopExitNode.setNext(continuationNode);
cleanupMerge(postMergeNode, postLoopExitNode);
cleanupMerge(mainMergeNode, mainLandingNode);
// Change the preLoop to execute one iteration for now
updateMainLoopLimit(preLimit, preIv, mainLoop);
updatePreLoopLimit(preLimit, preIv, preCounted);
preLoopBegin.setLoopFrequency(1);
mainLoopBegin.setLoopFrequency(Math.max(0.0, mainLoopBegin.loopFrequency() - 2));
postLoopBegin.setLoopFrequency(Math.max(0.0, postLoopBegin.loopFrequency() - 1));
// The pre and post loops don't require safepoints at all
for (SafepointNode safepoint : preLoop.nodes().filter(SafepointNode.class)) {
graph.removeFixed(safepoint);
}
for (SafepointNode safepoint : postLoop.nodes().filter(SafepointNode.class)) {
graph.removeFixed(safepoint);
}
graph.getDebug().dump(DebugContext.DETAILED_LEVEL, graph, "InsertPrePostLoops %s", loop);
return mainLoopBegin;
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class LoopFragmentInside method patchPeeling.
private void patchPeeling(LoopFragmentInside peel) {
LoopBeginNode loopBegin = loop().loopBegin();
StructuredGraph graph = loopBegin.graph();
List<PhiNode> newPhis = new LinkedList<>();
NodeBitMap usagesToPatch = nodes.copy();
for (LoopExitNode exit : exits()) {
markStateNodes(exit, usagesToPatch);
for (ProxyNode proxy : exit.proxies()) {
usagesToPatch.markAndGrow(proxy);
}
}
markStateNodes(loopBegin, usagesToPatch);
List<PhiNode> oldPhis = loopBegin.phis().snapshot();
for (PhiNode phi : oldPhis) {
if (phi.hasNoUsages()) {
continue;
}
ValueNode first;
if (loopBegin.loopEnds().count() == 1) {
// back edge value
ValueNode b = phi.valueAt(loopBegin.loopEnds().first());
// corresponding value in the peel
first = peel.prim(b);
} else {
first = peel.mergedInitializers.get(phi);
}
// create a new phi (we don't patch the old one since some usages of the old one may
// still be valid)
PhiNode newPhi = patchPhi(graph, phi, loopBegin);
newPhi.addInput(first);
for (LoopEndNode end : loopBegin.orderedLoopEnds()) {
newPhi.addInput(phi.valueAt(end));
}
peel.putDuplicatedNode(phi, newPhi);
newPhis.add(newPhi);
for (Node usage : phi.usages().snapshot()) {
// patch only usages that should use the new phi ie usages that were peeled
if (usagesToPatch.isMarkedAndGrow(usage)) {
usage.replaceFirstInput(phi, newPhi);
}
}
}
// new corresponding phis
for (PhiNode phi : newPhis) {
for (int i = 0; i < phi.valueCount(); i++) {
ValueNode v = phi.valueAt(i);
if (loopBegin.isPhiAtMerge(v)) {
PhiNode newV = peel.getDuplicatedNode((ValuePhiNode) v);
if (newV != null) {
phi.setValueAt(i, newV);
}
}
}
}
boolean progress = true;
while (progress) {
progress = false;
int i = 0;
outer: while (i < oldPhis.size()) {
PhiNode oldPhi = oldPhis.get(i);
for (Node usage : oldPhi.usages()) {
if (usage instanceof PhiNode && oldPhis.contains(usage)) {
// Do not mark.
} else {
// Mark alive by removing from delete set.
oldPhis.remove(i);
progress = true;
continue outer;
}
}
i++;
}
}
for (PhiNode deadPhi : oldPhis) {
deadPhi.clearInputs();
}
for (PhiNode deadPhi : oldPhis) {
if (deadPhi.isAlive()) {
GraphUtil.killWithUnusedFloatingInputs(deadPhi);
}
}
}
Aggregations