Search in sources :

Example 1 with TypeSwitchNode

use of org.graalvm.compiler.nodes.java.TypeSwitchNode in project graal by oracle.

the class MultiTypeGuardInlineInfo method createDispatchOnTypeBeforeInvoke.

private boolean createDispatchOnTypeBeforeInvoke(StructuredGraph graph, AbstractBeginNode[] successors, boolean invokeIsOnlySuccessor, StampProvider stampProvider, ConstantReflectionProvider constantReflection) {
    assert ptypes.size() >= 1;
    ValueNode nonNullReceiver = InliningUtil.nonNullReceiver(invoke);
    LoadHubNode hub = graph.unique(new LoadHubNode(stampProvider, nonNullReceiver));
    graph.getDebug().log("Type switch with %d types", concretes.size());
    ResolvedJavaType[] keys = new ResolvedJavaType[ptypes.size()];
    double[] keyProbabilities = new double[ptypes.size() + 1];
    int[] keySuccessors = new int[ptypes.size() + 1];
    double totalProbability = notRecordedTypeProbability;
    for (int i = 0; i < ptypes.size(); i++) {
        keys[i] = ptypes.get(i).getType();
        keyProbabilities[i] = ptypes.get(i).getProbability();
        totalProbability += keyProbabilities[i];
        keySuccessors[i] = invokeIsOnlySuccessor ? 0 : typesToConcretes.get(i);
        assert keySuccessors[i] < successors.length - 1 : "last successor is the unknownTypeSux";
    }
    keyProbabilities[keyProbabilities.length - 1] = notRecordedTypeProbability;
    keySuccessors[keySuccessors.length - 1] = successors.length - 1;
    // Normalize the probabilities.
    for (int i = 0; i < keyProbabilities.length; i++) {
        keyProbabilities[i] /= totalProbability;
    }
    TypeSwitchNode typeSwitch = graph.add(new TypeSwitchNode(hub, successors, keys, keyProbabilities, keySuccessors, constantReflection));
    FixedWithNextNode pred = (FixedWithNextNode) invoke.asNode().predecessor();
    pred.setNext(typeSwitch);
    return false;
}
Also used : FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) LoadHubNode(org.graalvm.compiler.nodes.extended.LoadHubNode) TypeSwitchNode(org.graalvm.compiler.nodes.java.TypeSwitchNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Example 2 with TypeSwitchNode

use of org.graalvm.compiler.nodes.java.TypeSwitchNode 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)

Aggregations

FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)2 TypeSwitchNode (org.graalvm.compiler.nodes.java.TypeSwitchNode)2 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)1 DebugContext (org.graalvm.compiler.debug.DebugContext)1 Node (org.graalvm.compiler.graph.Node)1 NodeBitMap (org.graalvm.compiler.graph.NodeBitMap)1 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)1 ControlSplitNode (org.graalvm.compiler.nodes.ControlSplitNode)1 DeoptimizeNode (org.graalvm.compiler.nodes.DeoptimizeNode)1 FixedNode (org.graalvm.compiler.nodes.FixedNode)1 InvokeNode (org.graalvm.compiler.nodes.InvokeNode)1 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)1 MergeNode (org.graalvm.compiler.nodes.MergeNode)1 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)1 ValueNode (org.graalvm.compiler.nodes.ValueNode)1 Block (org.graalvm.compiler.nodes.cfg.Block)1 ControlFlowAnchorNode (org.graalvm.compiler.nodes.debug.ControlFlowAnchorNode)1 LoadHubNode (org.graalvm.compiler.nodes.extended.LoadHubNode)1 OptionValues (org.graalvm.compiler.options.OptionValues)1