Search in sources :

Example 11 with LoopExitNode

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;
}
Also used : LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) StartNode(org.graalvm.compiler.nodes.StartNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) ControlSplitNode(org.graalvm.compiler.nodes.ControlSplitNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) ControlSinkNode(org.graalvm.compiler.nodes.ControlSinkNode)

Example 12 with LoopExitNode

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;
}
Also used : LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) StartNode(org.graalvm.compiler.nodes.StartNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) ControlSplitNode(org.graalvm.compiler.nodes.ControlSplitNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) ControlSinkNode(org.graalvm.compiler.nodes.ControlSinkNode) ArrayList(java.util.ArrayList) ControlSplitNode(org.graalvm.compiler.nodes.ControlSplitNode) FixedNode(org.graalvm.compiler.nodes.FixedNode)

Example 13 with LoopExitNode

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;
}
Also used : ProxyNode(org.graalvm.compiler.nodes.ProxyNode) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) OptionValues(org.graalvm.compiler.options.OptionValues) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) LogicConstantNode(org.graalvm.compiler.nodes.LogicConstantNode) AllocatedObjectNode(org.graalvm.compiler.nodes.virtual.AllocatedObjectNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) BoxNode(org.graalvm.compiler.nodes.extended.BoxNode) IfNode(org.graalvm.compiler.nodes.IfNode) VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) CommitAllocationNode(org.graalvm.compiler.nodes.virtual.CommitAllocationNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) LogicConstantNode(org.graalvm.compiler.nodes.LogicConstantNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) IfNode(org.graalvm.compiler.nodes.IfNode)

Example 14 with LoopExitNode

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;
            });
        }
    }
}
Also used : GuardProxyNode(org.graalvm.compiler.nodes.GuardProxyNode) ValueProxyNode(org.graalvm.compiler.nodes.ValueProxyNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) GuardPhiNode(org.graalvm.compiler.nodes.GuardPhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) GuardNode(org.graalvm.compiler.nodes.GuardNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) GuardProxyNode(org.graalvm.compiler.nodes.GuardProxyNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) CommitAllocationNode(org.graalvm.compiler.nodes.virtual.CommitAllocationNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ValueProxyNode(org.graalvm.compiler.nodes.ValueProxyNode) GuardPhiNode(org.graalvm.compiler.nodes.GuardPhiNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) MonitorEnterNode(org.graalvm.compiler.nodes.java.MonitorEnterNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) GuardProxyNode(org.graalvm.compiler.nodes.GuardProxyNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) GuardPhiNode(org.graalvm.compiler.nodes.GuardPhiNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) FrameState(org.graalvm.compiler.nodes.FrameState) VirtualState(org.graalvm.compiler.nodes.VirtualState) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) EndNode(org.graalvm.compiler.nodes.EndNode) ValueProxyNode(org.graalvm.compiler.nodes.ValueProxyNode) ValueNode(org.graalvm.compiler.nodes.ValueNode)

Example 15 with LoopExitNode

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();
                }
            };
        }
    };
}
Also used : NodeIterable(org.graalvm.compiler.graph.iterators.NodeIterable) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Iterator(java.util.Iterator) Block(org.graalvm.compiler.nodes.cfg.Block) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode)

Aggregations

LoopExitNode (org.graalvm.compiler.nodes.LoopExitNode)25 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)18 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)17 FixedNode (org.graalvm.compiler.nodes.FixedNode)16 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)14 Node (org.graalvm.compiler.graph.Node)13 EndNode (org.graalvm.compiler.nodes.EndNode)12 FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)10 LoopEndNode (org.graalvm.compiler.nodes.LoopEndNode)10 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)10 AbstractEndNode (org.graalvm.compiler.nodes.AbstractEndNode)9 PhiNode (org.graalvm.compiler.nodes.PhiNode)9 ProxyNode (org.graalvm.compiler.nodes.ProxyNode)9 ValueNode (org.graalvm.compiler.nodes.ValueNode)9 MergeNode (org.graalvm.compiler.nodes.MergeNode)8 ArrayList (java.util.ArrayList)6 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)6 ControlSplitNode (org.graalvm.compiler.nodes.ControlSplitNode)6 IfNode (org.graalvm.compiler.nodes.IfNode)6 FrameState (org.graalvm.compiler.nodes.FrameState)5