use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class ComputeInliningRelevance method getMaxProbabilityLoopExit.
/**
* Returns the most probable loop exit. If multiple successors share the maximum probability,
* one is returned and the others are enqueued in pathBeginNodes.
*/
private Node getMaxProbabilityLoopExit(LoopBeginNode loopBegin, ArrayList<FixedNode> pathBeginNodes) {
Node maxSux = null;
double maxProbability = 0.0;
int pathBeginCount = pathBeginNodes.size();
for (LoopExitNode sux : loopBegin.loopExits()) {
double probability = nodeProbabilities.applyAsDouble(sux);
if (probability > maxProbability) {
maxProbability = probability;
maxSux = sux;
truncate(pathBeginNodes, pathBeginCount);
} else if (probability == maxProbability) {
pathBeginNodes.add(sux);
}
}
return maxSux;
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class ComputeInliningRelevance method computeFastPathMinProbability.
/**
* Computes the minimum probability along the most probable path within the scope. During
* iteration, the method returns immediately once a loop exit is discovered.
*/
private double computeFastPathMinProbability(FixedNode scopeStart) {
ArrayList<FixedNode> pathBeginNodes = new ArrayList<>();
pathBeginNodes.add(scopeStart);
double minPathProbability = nodeProbabilities.applyAsDouble(scopeStart);
boolean isLoopScope = scopeStart instanceof LoopBeginNode;
do {
Node current = pathBeginNodes.remove(pathBeginNodes.size() - 1);
do {
if (isLoopScope && current instanceof LoopExitNode && ((LoopBeginNode) scopeStart).loopExits().contains((LoopExitNode) current)) {
return minPathProbability;
} else if (current instanceof LoopBeginNode && current != scopeStart) {
current = getMaxProbabilityLoopExit((LoopBeginNode) current, pathBeginNodes);
minPathProbability = getMinPathProbability((FixedNode) current, minPathProbability);
} else if (current instanceof ControlSplitNode) {
current = getMaxProbabilitySux((ControlSplitNode) current, pathBeginNodes);
minPathProbability = getMinPathProbability((FixedNode) current, minPathProbability);
} else {
assert current.successors().count() <= 1;
current = current.successors().first();
}
} while (current != null);
} while (!pathBeginNodes.isEmpty());
return minPathProbability;
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class EffectsClosure method processBlock.
@Override
protected BlockT processBlock(Block block, BlockT state) {
if (!state.isDead()) {
GraphEffectList effects = blockEffects.get(block);
/*
* If we enter an if branch that is known to be unreachable, we mark it as dead and
* cease to do any more analysis on it. At merges, these dead branches will be ignored.
*/
if (block.getBeginNode().predecessor() instanceof IfNode) {
IfNode ifNode = (IfNode) block.getBeginNode().predecessor();
LogicNode condition = ifNode.condition();
Node alias = getScalarAlias(condition);
if (alias instanceof LogicConstantNode) {
LogicConstantNode constant = (LogicConstantNode) alias;
boolean isTrueSuccessor = block.getBeginNode() == ifNode.trueSuccessor();
if (constant.getValue() != isTrueSuccessor) {
state.markAsDead();
effects.killIfBranch(ifNode, constant.getValue());
return state;
}
}
}
OptionValues options = block.getBeginNode().getOptions();
VirtualUtil.trace(options, debug, "\nBlock: %s, preds: %s, succ: %s (", block, block.getPredecessors(), block.getSuccessors());
// a lastFixedNode is needed in case we want to insert fixed nodes
FixedWithNextNode lastFixedNode = null;
Iterable<? extends Node> nodes = schedule != null ? schedule.getBlockToNodesMap().get(block) : block.getNodes();
for (Node node : nodes) {
// reset the aliases (may be non-null due to iterative loop processing)
aliases.set(node, null);
if (node instanceof LoopExitNode) {
LoopExitNode loopExit = (LoopExitNode) node;
for (ProxyNode proxy : loopExit.proxies()) {
aliases.set(proxy, null);
changed |= processNode(proxy, state, effects, lastFixedNode) && isSignificantNode(node);
}
processLoopExit(loopExit, loopEntryStates.get(loopExit.loopBegin()), state, blockEffects.get(block));
}
changed |= processNode(node, state, effects, lastFixedNode) && isSignificantNode(node);
if (node instanceof FixedWithNextNode) {
lastFixedNode = (FixedWithNextNode) node;
}
if (state.isDead()) {
break;
}
}
VirtualUtil.trace(options, debug, ")\n end state: %s\n", state);
}
return state;
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class LoopFragment method mergeEarlyExits.
/**
* Merges the early exits (i.e. loop exits) that were duplicated as part of this fragment, with
* the original fragment's exits.
*/
protected void mergeEarlyExits() {
assert isDuplicate();
StructuredGraph graph = graph();
for (AbstractBeginNode earlyExit : LoopFragment.toHirBlocks(original().loop().loop().getExits())) {
LoopExitNode loopEarlyExit = (LoopExitNode) earlyExit;
FixedNode next = loopEarlyExit.next();
if (loopEarlyExit.isDeleted() || !this.original().contains(loopEarlyExit)) {
continue;
}
AbstractBeginNode newEarlyExit = getDuplicatedNode(loopEarlyExit);
if (newEarlyExit == null) {
continue;
}
MergeNode merge = graph.add(new MergeNode());
EndNode originalEnd = graph.add(new EndNode());
EndNode newEnd = graph.add(new EndNode());
merge.addForwardEnd(originalEnd);
merge.addForwardEnd(newEnd);
loopEarlyExit.setNext(originalEnd);
newEarlyExit.setNext(newEnd);
merge.setNext(next);
FrameState exitState = loopEarlyExit.stateAfter();
if (exitState != null) {
FrameState originalExitState = exitState;
exitState = exitState.duplicateWithVirtualState();
loopEarlyExit.setStateAfter(exitState);
merge.setStateAfter(originalExitState);
/*
* Using the old exit's state as the merge's state is necessary because some of the
* VirtualState nodes contained in the old exit's state may be shared by other
* dominated VirtualStates. Those dominated virtual states need to see the
* proxy->phi update that are applied below.
*
* We now update the original fragment's nodes accordingly:
*/
originalExitState.applyToVirtual(node -> original.nodes.clearAndGrow(node));
exitState.applyToVirtual(node -> original.nodes.markAndGrow(node));
}
FrameState finalExitState = exitState;
for (Node anchored : loopEarlyExit.anchored().snapshot()) {
anchored.replaceFirstInput(loopEarlyExit, merge);
}
boolean newEarlyExitIsLoopExit = newEarlyExit instanceof LoopExitNode;
for (ProxyNode vpn : loopEarlyExit.proxies().snapshot()) {
if (vpn.hasNoUsages()) {
continue;
}
if (vpn.value() == null) {
assert vpn instanceof GuardProxyNode;
vpn.replaceAtUsages(null);
continue;
}
final ValueNode replaceWith;
ValueNode newVpn = prim(newEarlyExitIsLoopExit ? vpn : vpn.value());
if (newVpn != null) {
PhiNode phi;
if (vpn instanceof ValueProxyNode) {
phi = graph.addWithoutUnique(new ValuePhiNode(vpn.stamp(NodeView.DEFAULT), merge));
} else if (vpn instanceof GuardProxyNode) {
phi = graph.addWithoutUnique(new GuardPhiNode(merge));
} else {
throw GraalError.shouldNotReachHere();
}
phi.addInput(vpn);
phi.addInput(newVpn);
replaceWith = phi;
} else {
replaceWith = vpn.value();
}
vpn.replaceAtMatchingUsages(replaceWith, usage -> {
if (merge.isPhiAtMerge(usage)) {
return false;
}
if (usage instanceof VirtualState) {
VirtualState stateUsage = (VirtualState) usage;
if (finalExitState != null && finalExitState.isPartOfThisState(stateUsage)) {
return false;
}
}
return true;
});
}
}
}
use of org.graalvm.compiler.nodes.LoopExitNode in project graal by oracle.
the class LoopFragment method toHirExits.
public static NodeIterable<AbstractBeginNode> toHirExits(final Iterable<Block> blocks) {
return new NodeIterable<AbstractBeginNode>() {
@Override
public Iterator<AbstractBeginNode> iterator() {
final Iterator<Block> it = blocks.iterator();
return new Iterator<AbstractBeginNode>() {
@Override
public void remove() {
throw new UnsupportedOperationException();
}
/**
* Return the true LoopExitNode for this loop or the BeginNode for the block.
*/
@Override
public AbstractBeginNode next() {
Block next = it.next();
LoopExitNode exit = next.getLoopExit();
if (exit != null) {
return exit;
}
return next.getBeginNode();
}
@Override
public boolean hasNext() {
return it.hasNext();
}
};
}
};
}
Aggregations