Search in sources :

Example 1 with NE

use of org.graalvm.compiler.core.common.calc.Condition.NE in project graal by oracle.

the class LoopPredicationPhase method run.

@Override
@SuppressWarnings("try")
protected void run(StructuredGraph graph, MidTierContext context) {
    DebugContext debug = graph.getDebug();
    final SpeculationLog speculationLog = graph.getSpeculationLog();
    if (graph.hasLoops() && graph.getGuardsStage().allowsFloatingGuards() && context.getOptimisticOptimizations().useLoopLimitChecks(graph.getOptions()) && speculationLog != null) {
        LoopsData data = context.getLoopsDataProvider().getLoopsData(graph);
        final ControlFlowGraph cfg = data.getCFG();
        try (DebugContext.Scope s = debug.scope("predication", cfg)) {
            for (LoopEx loop : data.loops()) {
                // Only inner most loops.
                if (!loop.loop().getChildren().isEmpty()) {
                    continue;
                }
                if (!loop.detectCounted()) {
                    continue;
                }
                final FrameState state = loop.loopBegin().stateAfter();
                final BytecodePosition pos = new BytecodePosition(null, state.getMethod(), state.bci);
                SpeculationLog.SpeculationReason reason = LOOP_PREDICATION.createSpeculationReason(pos);
                if (speculationLog.maySpeculate(reason)) {
                    final CountedLoopInfo counted = loop.counted();
                    final InductionVariable counter = counted.getLimitCheckedIV();
                    final Condition condition = ((CompareNode) counted.getLimitTest().condition()).condition().asCondition();
                    final boolean inverted = loop.counted().isInverted();
                    if ((((IntegerStamp) counter.valueNode().stamp(NodeView.DEFAULT)).getBits() == 32) && !counted.isUnsignedCheck() && ((condition != NE && condition != EQ) || (counter.isConstantStride() && Math.abs(counter.constantStride()) == 1)) && (loop.loopBegin().isMainLoop() || loop.loopBegin().isSimpleLoop())) {
                        NodeIterable<GuardNode> guards = loop.whole().nodes().filter(GuardNode.class);
                        if (LoopPredicationMainPath.getValue(graph.getOptions())) {
                            // C2 only applies loop predication to guards dominating the
                            // backedge.
                            // The following logic emulates that behavior.
                            final NodeIterable<LoopEndNode> loopEndNodes = loop.loopBegin().loopEnds();
                            final Block end = data.getCFG().commonDominatorFor(loopEndNodes);
                            guards = guards.filter(guard -> {
                                final ValueNode anchor = ((GuardNode) guard).getAnchor().asNode();
                                final Block anchorBlock = data.getCFG().getNodeToBlock().get(anchor);
                                return AbstractControlFlowGraph.dominates(anchorBlock, end);
                            });
                        }
                        final AbstractBeginNode body = loop.counted().getBody();
                        final Block bodyBlock = cfg.getNodeToBlock().get(body);
                        for (GuardNode guard : guards) {
                            final AnchoringNode anchor = guard.getAnchor();
                            final Block anchorBlock = cfg.getNodeToBlock().get(anchor.asNode());
                            // for inverted loop the anchor can dominate the body
                            if (!inverted) {
                                if (!AbstractControlFlowGraph.dominates(bodyBlock, anchorBlock)) {
                                    continue;
                                }
                            }
                            processGuard(loop, guard);
                        }
                    }
                }
            }
        } catch (Throwable t) {
            throw debug.handle(t);
        }
    }
}
Also used : Condition(org.graalvm.compiler.core.common.calc.Condition) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) GuardNode(org.graalvm.compiler.nodes.GuardNode) AnchoringNode(org.graalvm.compiler.nodes.extended.AnchoringNode) EQ(org.graalvm.compiler.core.common.calc.Condition.EQ) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) IntegerConvertNode(org.graalvm.compiler.nodes.calc.IntegerConvertNode) CountedLoopInfo(org.graalvm.compiler.nodes.loop.CountedLoopInfo) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) StampFactory(org.graalvm.compiler.core.common.type.StampFactory) DebugContext(org.graalvm.compiler.debug.DebugContext) EconomicMap(org.graalvm.collections.EconomicMap) BytecodePosition(jdk.vm.ci.code.BytecodePosition) GuardedValueNode(org.graalvm.compiler.nodes.GuardedValueNode) InductionVariable(org.graalvm.compiler.nodes.loop.InductionVariable) LoopEx(org.graalvm.compiler.nodes.loop.LoopEx) SpeculationReasonGroup(org.graalvm.compiler.serviceprovider.SpeculationReasonGroup) Condition(org.graalvm.compiler.core.common.calc.Condition) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) LoopPredicationMainPath(org.graalvm.compiler.core.common.GraalOptions.LoopPredicationMainPath) BasePhase(org.graalvm.compiler.phases.BasePhase) NodeView(org.graalvm.compiler.nodes.NodeView) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) MidTierContext(org.graalvm.compiler.phases.tiers.MidTierContext) LoopsData(org.graalvm.compiler.nodes.loop.LoopsData) NE(org.graalvm.compiler.core.common.calc.Condition.NE) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) MultiGuardNode(org.graalvm.compiler.nodes.extended.MultiGuardNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) FrameState(org.graalvm.compiler.nodes.FrameState) NodeIterable(org.graalvm.compiler.graph.iterators.NodeIterable) Block(org.graalvm.compiler.nodes.cfg.Block) Node(org.graalvm.compiler.graph.Node) GuardingNode(org.graalvm.compiler.nodes.extended.GuardingNode) SpeculationLog(jdk.vm.ci.meta.SpeculationLog) MathUtil(org.graalvm.compiler.nodes.loop.MathUtil) AbstractControlFlowGraph(org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph) LoopsData(org.graalvm.compiler.nodes.loop.LoopsData) BytecodePosition(jdk.vm.ci.code.BytecodePosition) CountedLoopInfo(org.graalvm.compiler.nodes.loop.CountedLoopInfo) GuardNode(org.graalvm.compiler.nodes.GuardNode) MultiGuardNode(org.graalvm.compiler.nodes.extended.MultiGuardNode) AnchoringNode(org.graalvm.compiler.nodes.extended.AnchoringNode) DebugContext(org.graalvm.compiler.debug.DebugContext) FrameState(org.graalvm.compiler.nodes.FrameState) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) SpeculationLog(jdk.vm.ci.meta.SpeculationLog) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) AbstractControlFlowGraph(org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph) LoopEx(org.graalvm.compiler.nodes.loop.LoopEx) GuardedValueNode(org.graalvm.compiler.nodes.GuardedValueNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Block(org.graalvm.compiler.nodes.cfg.Block) InductionVariable(org.graalvm.compiler.nodes.loop.InductionVariable)

Aggregations

BytecodePosition (jdk.vm.ci.code.BytecodePosition)1 SpeculationLog (jdk.vm.ci.meta.SpeculationLog)1 EconomicMap (org.graalvm.collections.EconomicMap)1 LoopPredicationMainPath (org.graalvm.compiler.core.common.GraalOptions.LoopPredicationMainPath)1 Condition (org.graalvm.compiler.core.common.calc.Condition)1 EQ (org.graalvm.compiler.core.common.calc.Condition.EQ)1 NE (org.graalvm.compiler.core.common.calc.Condition.NE)1 AbstractControlFlowGraph (org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph)1 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)1 StampFactory (org.graalvm.compiler.core.common.type.StampFactory)1 DebugContext (org.graalvm.compiler.debug.DebugContext)1 Node (org.graalvm.compiler.graph.Node)1 NodeIterable (org.graalvm.compiler.graph.iterators.NodeIterable)1 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)1 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)1 FrameState (org.graalvm.compiler.nodes.FrameState)1 GuardNode (org.graalvm.compiler.nodes.GuardNode)1 GuardedValueNode (org.graalvm.compiler.nodes.GuardedValueNode)1 LogicNode (org.graalvm.compiler.nodes.LogicNode)1 LoopEndNode (org.graalvm.compiler.nodes.LoopEndNode)1