Search in sources :

Example 11 with LoopsData

use of org.graalvm.compiler.loop.LoopsData in project graal by oracle.

the class LoopPartialUnrollPhase method run.

@Override
@SuppressWarnings("try")
protected void run(StructuredGraph graph, PhaseContext context) {
    if (graph.hasLoops()) {
        HashSetNodeEventListener listener = new HashSetNodeEventListener();
        boolean changed = true;
        while (changed) {
            changed = false;
            try (Graph.NodeEventScope nes = graph.trackNodeEvents(listener)) {
                LoopsData dataCounted = new LoopsData(graph);
                dataCounted.detectedCountedLoops();
                Graph.Mark mark = graph.getMark();
                boolean prePostInserted = false;
                for (LoopEx loop : dataCounted.countedLoops()) {
                    if (!LoopTransformations.isUnrollableLoop(loop)) {
                        continue;
                    }
                    if (getPolicies().shouldPartiallyUnroll(loop)) {
                        if (loop.loopBegin().isSimpleLoop()) {
                            // First perform the pre/post transformation and do the partial
                            // unroll when we come around again.
                            LoopTransformations.insertPrePostLoops(loop);
                            prePostInserted = true;
                        } else {
                            LoopTransformations.partialUnroll(loop);
                        }
                        changed = true;
                    }
                }
                dataCounted.deleteUnusedNodes();
                if (!listener.getNodes().isEmpty()) {
                    canonicalizer.applyIncremental(graph, context, listener.getNodes());
                    listener.getNodes().clear();
                }
                assert !prePostInserted || checkCounted(graph, mark);
            }
        }
    }
}
Also used : HashSetNodeEventListener(org.graalvm.compiler.phases.common.util.HashSetNodeEventListener) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) Graph(org.graalvm.compiler.graph.Graph) LoopsData(org.graalvm.compiler.loop.LoopsData) LoopEx(org.graalvm.compiler.loop.LoopEx)

Example 12 with LoopsData

use of org.graalvm.compiler.loop.LoopsData in project graal by oracle.

the class LoopPeelingPhase method run.

@Override
@SuppressWarnings("try")
protected void run(StructuredGraph graph, PhaseContext context) {
    DebugContext debug = graph.getDebug();
    if (graph.hasLoops()) {
        LoopsData data = new LoopsData(graph);
        try (DebugContext.Scope s = debug.scope("peeling", data.getCFG())) {
            for (LoopEx loop : data.outerFirst()) {
                if (getPolicies().shouldPeel(loop, data.getCFG(), context.getMetaAccess())) {
                    debug.log("Peeling %s", loop);
                    LoopTransformations.peel(loop);
                    debug.dump(DebugContext.DETAILED_LEVEL, graph, "Peeling %s", loop);
                }
            }
            data.deleteUnusedNodes();
        } catch (Throwable t) {
            throw debug.handle(t);
        }
    }
}
Also used : LoopsData(org.graalvm.compiler.loop.LoopsData) LoopEx(org.graalvm.compiler.loop.LoopEx) DebugContext(org.graalvm.compiler.debug.DebugContext)

Example 13 with LoopsData

use of org.graalvm.compiler.loop.LoopsData in project graal by oracle.

the class LoopSafepointEliminationPhase method run.

@Override
protected void run(StructuredGraph graph, MidTierContext context) {
    LoopsData loops = new LoopsData(graph);
    if (context.getOptimisticOptimizations().useLoopLimitChecks(graph.getOptions()) && graph.getGuardsStage().allowsFloatingGuards()) {
        loops.detectedCountedLoops();
        for (LoopEx loop : loops.countedLoops()) {
            if (loop.loop().getChildren().isEmpty() && loop.counted().getStamp().getBits() <= 32) {
                boolean hasSafepoint = false;
                for (LoopEndNode loopEnd : loop.loopBegin().loopEnds()) {
                    hasSafepoint |= loopEnd.canSafepoint();
                }
                if (hasSafepoint) {
                    loop.counted().createOverFlowGuard();
                    loop.loopBegin().disableSafepoint();
                }
            }
        }
    }
    for (LoopEx loop : loops.loops()) {
        for (LoopEndNode loopEnd : loop.loopBegin().loopEnds()) {
            Block b = loops.getCFG().blockFor(loopEnd);
            blocks: while (b != loop.loop().getHeader()) {
                assert b != null;
                for (FixedNode node : b.getNodes()) {
                    if (node instanceof Invoke || (node instanceof ForeignCallNode && ((ForeignCallNode) node).isGuaranteedSafepoint())) {
                        loopEnd.disableSafepoint();
                        break blocks;
                    }
                }
                b = b.getDominator();
            }
        }
    }
    loops.deleteUnusedNodes();
}
Also used : LoopsData(org.graalvm.compiler.loop.LoopsData) ForeignCallNode(org.graalvm.compiler.nodes.extended.ForeignCallNode) LoopEx(org.graalvm.compiler.loop.LoopEx) Block(org.graalvm.compiler.nodes.cfg.Block) FixedNode(org.graalvm.compiler.nodes.FixedNode) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) Invoke(org.graalvm.compiler.nodes.Invoke)

Example 14 with LoopsData

use of org.graalvm.compiler.loop.LoopsData in project graal by oracle.

the class LoopUnswitchingPhase method run.

@Override
protected void run(StructuredGraph graph) {
    DebugContext debug = graph.getDebug();
    if (graph.hasLoops()) {
        boolean unswitched;
        do {
            unswitched = false;
            final LoopsData dataUnswitch = new LoopsData(graph);
            for (LoopEx loop : dataUnswitch.outerFirst()) {
                if (getPolicies().shouldTryUnswitch(loop)) {
                    List<ControlSplitNode> controlSplits = LoopTransformations.findUnswitchable(loop);
                    if (controlSplits != null) {
                        UNSWITCH_CANDIDATES.increment(debug);
                        if (getPolicies().shouldUnswitch(loop, controlSplits)) {
                            if (debug.isLogEnabled()) {
                                logUnswitch(loop, controlSplits);
                            }
                            LoopTransformations.unswitch(loop, controlSplits);
                            debug.dump(DebugContext.DETAILED_LEVEL, graph, "After unswitch %s", controlSplits);
                            UNSWITCHED.increment(debug);
                            unswitched = true;
                            break;
                        }
                    }
                } else {
                    UNSWITCH_EARLY_REJECTS.increment(debug);
                }
            }
        } while (unswitched);
    }
}
Also used : LoopsData(org.graalvm.compiler.loop.LoopsData) LoopEx(org.graalvm.compiler.loop.LoopEx) ControlSplitNode(org.graalvm.compiler.nodes.ControlSplitNode) DebugContext(org.graalvm.compiler.debug.DebugContext)

Example 15 with LoopsData

use of org.graalvm.compiler.loop.LoopsData in project graal by oracle.

the class LoopPartialUnrollTest method buildGraph.

@SuppressWarnings("try")
public StructuredGraph buildGraph(String name, boolean partialUnroll) {
    CompilationIdentifier id = new CompilationIdentifier() {

        @Override
        public String toString(Verbosity verbosity) {
            return name;
        }
    };
    ResolvedJavaMethod method = getResolvedJavaMethod(name);
    OptionValues options = new OptionValues(getInitialOptions(), DefaultLoopPolicies.Options.UnrollMaxIterations, 2);
    StructuredGraph graph = parse(builder(method, StructuredGraph.AllowAssumptions.YES, id, options), getEagerGraphBuilderSuite());
    try (DebugContext.Scope buildScope = graph.getDebug().scope(name, method, graph)) {
        MidTierContext context = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, null);
        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
        canonicalizer.apply(graph, context);
        new RemoveValueProxyPhase().apply(graph);
        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
        new FloatingReadPhase().apply(graph);
        new DeadCodeEliminationPhase().apply(graph);
        new ConditionalEliminationPhase(true).apply(graph, context);
        ComputeLoopFrequenciesClosure.compute(graph);
        new GuardLoweringPhase().apply(graph, context);
        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
        new FrameStateAssignmentPhase().apply(graph);
        new DeoptimizationGroupingPhase().apply(graph, context);
        canonicalizer.apply(graph, context);
        new ConditionalEliminationPhase(true).apply(graph, context);
        if (partialUnroll) {
            LoopsData dataCounted = new LoopsData(graph);
            dataCounted.detectedCountedLoops();
            for (LoopEx loop : dataCounted.countedLoops()) {
                LoopFragmentInside newSegment = loop.inside().duplicate();
                newSegment.insertWithinAfter(loop, false);
            }
            canonicalizer.apply(graph, getDefaultMidTierContext());
        }
        new DeadCodeEliminationPhase().apply(graph);
        canonicalizer.apply(graph, context);
        graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "before compare");
        return graph;
    } catch (Throwable e) {
        throw getDebugContext().handle(e);
    }
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) FrameStateAssignmentPhase(org.graalvm.compiler.phases.common.FrameStateAssignmentPhase) LoopsData(org.graalvm.compiler.loop.LoopsData) OptionValues(org.graalvm.compiler.options.OptionValues) LoopFragmentInside(org.graalvm.compiler.loop.LoopFragmentInside) RemoveValueProxyPhase(org.graalvm.compiler.phases.common.RemoveValueProxyPhase) GuardLoweringPhase(org.graalvm.compiler.phases.common.GuardLoweringPhase) LoweringPhase(org.graalvm.compiler.phases.common.LoweringPhase) DebugContext(org.graalvm.compiler.debug.DebugContext) FloatingReadPhase(org.graalvm.compiler.phases.common.FloatingReadPhase) MidTierContext(org.graalvm.compiler.phases.tiers.MidTierContext) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) LoopEx(org.graalvm.compiler.loop.LoopEx) ConditionalEliminationPhase(org.graalvm.compiler.phases.common.ConditionalEliminationPhase) CanonicalizerPhase(org.graalvm.compiler.phases.common.CanonicalizerPhase) DeoptimizationGroupingPhase(org.graalvm.compiler.phases.common.DeoptimizationGroupingPhase) DeadCodeEliminationPhase(org.graalvm.compiler.phases.common.DeadCodeEliminationPhase) GuardLoweringPhase(org.graalvm.compiler.phases.common.GuardLoweringPhase) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

LoopsData (org.graalvm.compiler.loop.LoopsData)17 LoopEx (org.graalvm.compiler.loop.LoopEx)14 DebugContext (org.graalvm.compiler.debug.DebugContext)6 GraalCompilerTest (org.graalvm.compiler.core.test.GraalCompilerTest)3 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)3 Block (org.graalvm.compiler.nodes.cfg.Block)3 HashSet (java.util.HashSet)2 Node (org.graalvm.compiler.graph.Node)2 FixedNode (org.graalvm.compiler.nodes.FixedNode)2 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)2 CanonicalizerPhase (org.graalvm.compiler.phases.common.CanonicalizerPhase)2 DeadCodeEliminationPhase (org.graalvm.compiler.phases.common.DeadCodeEliminationPhase)2 Test (org.junit.Test)2 JavaConstant (jdk.vm.ci.meta.JavaConstant)1 ResolvedJavaField (jdk.vm.ci.meta.ResolvedJavaField)1 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)1 CompilationIdentifier (org.graalvm.compiler.core.common.CompilationIdentifier)1 PermanentBailoutException (org.graalvm.compiler.core.common.PermanentBailoutException)1 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)1 Stamp (org.graalvm.compiler.core.common.type.Stamp)1