Search in sources :

Example 1 with BlockIteratorClosure

use of org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure in project graal by oracle.

the class ReentrantBlockIteratorTest method getVisitedBlocksInOrder.

private List<Block> getVisitedBlocksInOrder(String snippet) {
    StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
    // after FSA to ensure HIR loop data structure does not contain loop exits
    graph.setGuardsStage(GuardsStage.AFTER_FSA);
    ArrayList<Block> blocks = new ArrayList<>();
    class VoidState {
    }
    final VoidState voidState = new VoidState();
    BlockIteratorClosure<VoidState> closure = new BlockIteratorClosure<VoidState>() {

        @Override
        protected VoidState getInitialState() {
            return voidState;
        }

        @Override
        protected VoidState processBlock(Block block, VoidState currentState) {
            // remember the visit order
            blocks.add(block);
            return currentState;
        }

        @Override
        protected VoidState merge(Block merge, List<VoidState> states) {
            return voidState;
        }

        @Override
        protected VoidState cloneState(VoidState oldState) {
            return voidState;
        }

        @Override
        protected List<VoidState> processLoop(Loop<Block> loop, VoidState initialState) {
            return ReentrantBlockIterator.processLoop(this, loop, initialState).exitStates;
        }
    };
    ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, false);
    ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
    // schedule for IGV
    new SchedulePhase(graph.getOptions()).apply(graph);
    return blocks;
}
Also used : Loop(org.graalvm.compiler.core.common.cfg.Loop) BlockIteratorClosure(org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure) SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) ArrayList(java.util.ArrayList) Block(org.graalvm.compiler.nodes.cfg.Block) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with BlockIteratorClosure

use of org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure 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)

Example 3 with BlockIteratorClosure

use of org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure in project graal by oracle.

the class EffectsClosure method applyEffects.

@Override
public void applyEffects() {
    final StructuredGraph graph = cfg.graph;
    final ArrayList<Node> obsoleteNodes = new ArrayList<>(0);
    final ArrayList<GraphEffectList> effectList = new ArrayList<>();
    /*
         * Effects are applied during a ordered iteration over the blocks to apply them in the
         * correct order, e.g., apply the effect that adds a node to the graph before the node is
         * used.
         */
    BlockIteratorClosure<Void> closure = new BlockIteratorClosure<Void>() {

        @Override
        protected Void getInitialState() {
            return null;
        }

        private void apply(GraphEffectList effects) {
            if (effects != null && !effects.isEmpty()) {
                effectList.add(effects);
            }
        }

        @Override
        protected Void processBlock(Block block, Void currentState) {
            apply(blockEffects.get(block));
            return currentState;
        }

        @Override
        protected Void merge(Block merge, List<Void> states) {
            return null;
        }

        @Override
        protected Void cloneState(Void oldState) {
            return oldState;
        }

        @Override
        protected List<Void> processLoop(Loop<Block> loop, Void initialState) {
            LoopInfo<Void> info = ReentrantBlockIterator.processLoop(this, loop, initialState);
            apply(loopMergeEffects.get(loop));
            return info.exitStates;
        }
    };
    ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
    for (GraphEffectList effects : effectList) {
        effects.apply(graph, obsoleteNodes, false);
    }
    /*
         * Effects that modify the cfg (e.g., removing a branch for an if that got a constant
         * condition) need to be performed after all other effects, because they change phi value
         * indexes.
         */
    for (GraphEffectList effects : effectList) {
        effects.apply(graph, obsoleteNodes, true);
    }
    debug.dump(DebugContext.DETAILED_LEVEL, graph, "After applying effects");
    assert VirtualUtil.assertNonReachable(graph, obsoleteNodes);
    for (Node node : obsoleteNodes) {
        if (node.isAlive() && node.hasNoUsages()) {
            if (node instanceof FixedWithNextNode) {
                assert ((FixedWithNextNode) node).next() == null;
            }
            node.replaceAtUsages(null);
            GraphUtil.killWithUnusedFloatingInputs(node);
        }
    }
}
Also used : Loop(org.graalvm.compiler.core.common.cfg.Loop) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) BlockIteratorClosure(org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure) 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) ArrayList(java.util.ArrayList) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) Block(org.graalvm.compiler.nodes.cfg.Block) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 Loop (org.graalvm.compiler.core.common.cfg.Loop)3 Block (org.graalvm.compiler.nodes.cfg.Block)3 BlockIteratorClosure (org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure)3 Node (org.graalvm.compiler.graph.Node)2 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)2 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)2 LoopExitNode (org.graalvm.compiler.nodes.LoopExitNode)2 PhiNode (org.graalvm.compiler.nodes.PhiNode)2 ProxyNode (org.graalvm.compiler.nodes.ProxyNode)2 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)2 ValueNode (org.graalvm.compiler.nodes.ValueNode)2 VirtualObjectNode (org.graalvm.compiler.nodes.virtual.VirtualObjectNode)2 SchedulePhase (org.graalvm.compiler.phases.schedule.SchedulePhase)2 NodeBitMap (org.graalvm.compiler.graph.NodeBitMap)1 AbstractEndNode (org.graalvm.compiler.nodes.AbstractEndNode)1 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)1 EndNode (org.graalvm.compiler.nodes.EndNode)1 FixedNode (org.graalvm.compiler.nodes.FixedNode)1