Search in sources :

Example 1 with NodeBitMap

use of org.graalvm.compiler.graph.NodeBitMap in project graal by oracle.

the class DefaultLoopPolicies method shouldUnswitch.

@Override
public boolean shouldUnswitch(LoopEx loop, List<ControlSplitNode> controlSplits) {
    int phis = 0;
    StructuredGraph graph = loop.loopBegin().graph();
    DebugContext debug = graph.getDebug();
    NodeBitMap branchNodes = graph.createNodeBitMap();
    for (ControlSplitNode controlSplit : controlSplits) {
        for (Node successor : controlSplit.successors()) {
            AbstractBeginNode branch = (AbstractBeginNode) successor;
            // this may count twice because of fall-through in switches
            loop.nodesInLoopBranch(branchNodes, branch);
        }
        Block postDomBlock = loop.loopsData().getCFG().blockFor(controlSplit).getPostdominator();
        if (postDomBlock != null) {
            IsolatedInitialization.UNSWITCH_SPLIT_WITH_PHIS.increment(debug);
            phis += ((MergeNode) postDomBlock.getBeginNode()).phis().count();
        }
    }
    int inBranchTotal = branchNodes.count();
    CountingClosure stateNodesCount = new CountingClosure();
    double loopFrequency = loop.loopBegin().loopFrequency();
    OptionValues options = loop.loopBegin().getOptions();
    int maxDiff = Options.LoopUnswitchTrivial.getValue(options) + (int) (Options.LoopUnswitchFrequencyBoost.getValue(options) * (loopFrequency - 1.0 + phis));
    maxDiff = Math.min(maxDiff, Options.LoopUnswitchMaxIncrease.getValue(options));
    int remainingGraphSpace = MaximumDesiredSize.getValue(options) - graph.getNodeCount();
    maxDiff = Math.min(maxDiff, remainingGraphSpace);
    loop.loopBegin().stateAfter().applyToVirtual(stateNodesCount);
    int loopTotal = loop.size() - loop.loopBegin().phis().count() - stateNodesCount.count - 1;
    int actualDiff = (loopTotal - inBranchTotal);
    ControlSplitNode firstSplit = controlSplits.get(0);
    if (firstSplit instanceof TypeSwitchNode) {
        int copies = firstSplit.successors().count() - 1;
        for (Node succ : firstSplit.successors()) {
            FixedNode current = (FixedNode) succ;
            while (current instanceof FixedWithNextNode) {
                current = ((FixedWithNextNode) current).next();
            }
            if (current instanceof DeoptimizeNode) {
                copies--;
            }
        }
        actualDiff = actualDiff * copies;
    }
    debug.log("shouldUnswitch(%s, %s) : delta=%d (%.2f%% inside of branches), max=%d, f=%.2f, phis=%d -> %b", loop, controlSplits, actualDiff, (double) (inBranchTotal) / loopTotal * 100, maxDiff, loopFrequency, phis, actualDiff <= maxDiff);
    if (actualDiff <= maxDiff) {
        // check whether we're allowed to unswitch this loop
        return loop.canDuplicateLoop();
    } else {
        return false;
    }
}
Also used : FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) OptionValues(org.graalvm.compiler.options.OptionValues) NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) ControlFlowAnchorNode(org.graalvm.compiler.nodes.debug.ControlFlowAnchorNode) ControlSplitNode(org.graalvm.compiler.nodes.ControlSplitNode) InvokeNode(org.graalvm.compiler.nodes.InvokeNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) DeoptimizeNode(org.graalvm.compiler.nodes.DeoptimizeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) Node(org.graalvm.compiler.graph.Node) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) TypeSwitchNode(org.graalvm.compiler.nodes.java.TypeSwitchNode) DebugContext(org.graalvm.compiler.debug.DebugContext) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) TypeSwitchNode(org.graalvm.compiler.nodes.java.TypeSwitchNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) Block(org.graalvm.compiler.nodes.cfg.Block) ControlSplitNode(org.graalvm.compiler.nodes.ControlSplitNode) DeoptimizeNode(org.graalvm.compiler.nodes.DeoptimizeNode)

Example 2 with NodeBitMap

use of org.graalvm.compiler.graph.NodeBitMap 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);
        }
    }
}
Also used : ProxyNode(org.graalvm.compiler.nodes.ProxyNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) MemoryPhiNode(org.graalvm.compiler.nodes.memory.MemoryPhiNode) GuardPhiNode(org.graalvm.compiler.nodes.GuardPhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) MemoryPhiNode(org.graalvm.compiler.nodes.memory.MemoryPhiNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) AddNode(org.graalvm.compiler.nodes.calc.AddNode) BeginNode(org.graalvm.compiler.nodes.BeginNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) IfNode(org.graalvm.compiler.nodes.IfNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) SubNode(org.graalvm.compiler.nodes.calc.SubNode) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) SafepointNode(org.graalvm.compiler.nodes.SafepointNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) GuardPhiNode(org.graalvm.compiler.nodes.GuardPhiNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) LinkedList(java.util.LinkedList) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ValueNode(org.graalvm.compiler.nodes.ValueNode)

Example 3 with NodeBitMap

use of org.graalvm.compiler.graph.NodeBitMap in project graal by oracle.

the class CFGPrinter method printNodes.

private void printNodes(Block block) {
    printedNodes = new NodeBitMap(cfg.graph);
    begin("IR");
    out.println("HIR");
    out.disableIndentation();
    if (block.getBeginNode() instanceof AbstractMergeNode) {
        // Currently phi functions are not in the schedule, so print them separately here.
        for (ValueNode phi : ((AbstractMergeNode) block.getBeginNode()).phis()) {
            printNode(phi, false);
        }
    }
    Node cur = block.getBeginNode();
    while (true) {
        printNode(cur, false);
        if (cur == block.getEndNode()) {
            UnmodifiableMapCursor<Node, Block> cursor = latestScheduling.getEntries();
            while (cursor.advance()) {
                if (cursor.getValue() == block && !inFixedSchedule(cursor.getKey()) && !printedNodes.isMarked(cursor.getKey())) {
                    printNode(cursor.getKey(), true);
                }
            }
            break;
        }
        assert cur.successors().count() == 1;
        cur = cur.successors().first();
    }
    out.enableIndentation();
    end("IR");
    printedNodes = null;
}
Also used : NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) FloatingNode(org.graalvm.compiler.nodes.calc.FloatingNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Node(org.graalvm.compiler.graph.Node) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Block(org.graalvm.compiler.nodes.cfg.Block) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode)

Example 4 with NodeBitMap

use of org.graalvm.compiler.graph.NodeBitMap in project graal by oracle.

the class GraphOrder method createOrder.

private static List<Node> createOrder(StructuredGraph graph) {
    final ArrayList<Node> nodes = new ArrayList<>();
    final NodeBitMap visited = graph.createNodeBitMap();
    new StatelessPostOrderNodeIterator(graph.start()) {

        @Override
        protected void node(FixedNode node) {
            visitForward(nodes, visited, node, false);
        }
    }.apply();
    return nodes;
}
Also used : NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) StatelessPostOrderNodeIterator(org.graalvm.compiler.phases.graph.StatelessPostOrderNodeIterator) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) ArrayList(java.util.ArrayList) FixedNode(org.graalvm.compiler.nodes.FixedNode)

Example 5 with NodeBitMap

use of org.graalvm.compiler.graph.NodeBitMap in project graal by oracle.

the class GraphOrder method assertSchedulableGraph.

/**
 * This method schedules the graph and makes sure that, for every node, all inputs are available
 * at the position where it is scheduled. This is a very expensive assertion.
 */
public static boolean assertSchedulableGraph(final StructuredGraph graph) {
    assert graph.getGuardsStage() != GuardsStage.AFTER_FSA : "Cannot use the BlockIteratorClosure after FrameState Assignment, HIR Loop Data Structures are no longer valid.";
    try {
        final SchedulePhase schedulePhase = new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS, true);
        final EconomicMap<LoopBeginNode, NodeBitMap> loopEntryStates = EconomicMap.create(Equivalence.IDENTITY);
        schedulePhase.apply(graph, false);
        final ScheduleResult schedule = graph.getLastSchedule();
        BlockIteratorClosure<NodeBitMap> closure = new BlockIteratorClosure<NodeBitMap>() {

            @Override
            protected List<NodeBitMap> processLoop(Loop<Block> loop, NodeBitMap initialState) {
                return ReentrantBlockIterator.processLoop(this, loop, initialState).exitStates;
            }

            @Override
            protected NodeBitMap processBlock(final Block block, final NodeBitMap currentState) {
                final List<Node> list = graph.getLastSchedule().getBlockToNodesMap().get(block);
                /*
                     * A stateAfter is not valid directly after its associated state split, but
                     * right before the next fixed node. Therefore a pending stateAfter is kept that
                     * will be checked at the correct position.
                     */
                FrameState pendingStateAfter = null;
                for (final Node node : list) {
                    if (node instanceof ValueNode) {
                        FrameState stateAfter = node instanceof StateSplit ? ((StateSplit) node).stateAfter() : null;
                        if (node instanceof FullInfopointNode) {
                            stateAfter = ((FullInfopointNode) node).getState();
                        }
                        if (pendingStateAfter != null && node instanceof FixedNode) {
                            pendingStateAfter.applyToNonVirtual(new NodeClosure<Node>() {

                                @Override
                                public void apply(Node usage, Node nonVirtualNode) {
                                    assert currentState.isMarked(nonVirtualNode) || nonVirtualNode instanceof VirtualObjectNode || nonVirtualNode instanceof ConstantNode : nonVirtualNode + " not available at virtualstate " + usage + " before " + node + " in block " + block + " \n" + list;
                                }
                            });
                            pendingStateAfter = null;
                        }
                        if (node instanceof AbstractMergeNode) {
                            // phis aren't scheduled, so they need to be added explicitly
                            currentState.markAll(((AbstractMergeNode) node).phis());
                            if (node instanceof LoopBeginNode) {
                                // remember the state at the loop entry, it's restored at exits
                                loopEntryStates.put((LoopBeginNode) node, currentState.copy());
                            }
                        } else if (node instanceof ProxyNode) {
                            assert false : "proxy nodes should not be in the schedule";
                        } else if (node instanceof LoopExitNode) {
                            if (graph.hasValueProxies()) {
                                for (ProxyNode proxy : ((LoopExitNode) node).proxies()) {
                                    for (Node input : proxy.inputs()) {
                                        if (input != proxy.proxyPoint()) {
                                            assert currentState.isMarked(input) : input + " not available at " + proxy + " in block " + block + "\n" + list;
                                        }
                                    }
                                }
                                // loop contents are only accessible via proxies at the exit
                                currentState.clearAll();
                                currentState.markAll(loopEntryStates.get(((LoopExitNode) node).loopBegin()));
                            }
                            // Loop proxies aren't scheduled, so they need to be added
                            // explicitly
                            currentState.markAll(((LoopExitNode) node).proxies());
                        } else {
                            for (Node input : node.inputs()) {
                                if (input != stateAfter) {
                                    if (input instanceof FrameState) {
                                        ((FrameState) input).applyToNonVirtual(new VirtualState.NodeClosure<Node>() {

                                            @Override
                                            public void apply(Node usage, Node nonVirtual) {
                                                assert currentState.isMarked(nonVirtual) : nonVirtual + " not available at " + node + " in block " + block + "\n" + list;
                                            }
                                        });
                                    } else {
                                        assert currentState.isMarked(input) || input instanceof VirtualObjectNode || input instanceof ConstantNode : input + " not available at " + node + " in block " + block + "\n" + list;
                                    }
                                }
                            }
                        }
                        if (node instanceof AbstractEndNode) {
                            AbstractMergeNode merge = ((AbstractEndNode) node).merge();
                            for (PhiNode phi : merge.phis()) {
                                ValueNode phiValue = phi.valueAt((AbstractEndNode) node);
                                assert phiValue == null || currentState.isMarked(phiValue) || phiValue instanceof ConstantNode : phiValue + " not available at phi " + phi + " / end " + node + " in block " + block;
                            }
                        }
                        if (stateAfter != null) {
                            assert pendingStateAfter == null;
                            pendingStateAfter = stateAfter;
                        }
                        currentState.mark(node);
                    }
                }
                if (pendingStateAfter != null) {
                    pendingStateAfter.applyToNonVirtual(new NodeClosure<Node>() {

                        @Override
                        public void apply(Node usage, Node nonVirtualNode) {
                            assert currentState.isMarked(nonVirtualNode) || nonVirtualNode instanceof VirtualObjectNode || nonVirtualNode instanceof ConstantNode : nonVirtualNode + " not available at virtualstate " + usage + " at end of block " + block + " \n" + list;
                        }
                    });
                }
                return currentState;
            }

            @Override
            protected NodeBitMap merge(Block merge, List<NodeBitMap> states) {
                NodeBitMap result = states.get(0);
                for (int i = 1; i < states.size(); i++) {
                    result.intersect(states.get(i));
                }
                return result;
            }

            @Override
            protected NodeBitMap getInitialState() {
                NodeBitMap ret = graph.createNodeBitMap();
                ret.markAll(graph.getNodes().filter(ConstantNode.class));
                return ret;
            }

            @Override
            protected NodeBitMap cloneState(NodeBitMap oldState) {
                return oldState.copy();
            }
        };
        ReentrantBlockIterator.apply(closure, schedule.getCFG().getStartBlock());
    } catch (Throwable t) {
        graph.getDebug().handle(t);
    }
    return true;
}
Also used : VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase) ScheduleResult(org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) FrameState(org.graalvm.compiler.nodes.FrameState) VirtualState(org.graalvm.compiler.nodes.VirtualState) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) ArrayList(java.util.ArrayList) List(java.util.List) Loop(org.graalvm.compiler.core.common.cfg.Loop) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) BlockIteratorClosure(org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Block(org.graalvm.compiler.nodes.cfg.Block) StateSplit(org.graalvm.compiler.nodes.StateSplit)

Aggregations

NodeBitMap (org.graalvm.compiler.graph.NodeBitMap)14 Node (org.graalvm.compiler.graph.Node)11 FixedNode (org.graalvm.compiler.nodes.FixedNode)9 PhiNode (org.graalvm.compiler.nodes.PhiNode)9 ValueNode (org.graalvm.compiler.nodes.ValueNode)9 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)8 EndNode (org.graalvm.compiler.nodes.EndNode)7 AbstractEndNode (org.graalvm.compiler.nodes.AbstractEndNode)6 LoopExitNode (org.graalvm.compiler.nodes.LoopExitNode)6 ProxyNode (org.graalvm.compiler.nodes.ProxyNode)6 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)5 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)5 Block (org.graalvm.compiler.nodes.cfg.Block)5 VirtualObjectNode (org.graalvm.compiler.nodes.virtual.VirtualObjectNode)5 ArrayList (java.util.ArrayList)4 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)4 MergeNode (org.graalvm.compiler.nodes.MergeNode)4 ValuePhiNode (org.graalvm.compiler.nodes.ValuePhiNode)4 ArrayDeque (java.util.ArrayDeque)3 FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)3