package org.graalvm.compiler.phases.common.inlining;
import java.util.LinkedList;
import java.util.Map;
import org.graalvm.compiler.nodes.Invoke;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.options.Option;
import org.graalvm.compiler.options.OptionType;
import org.graalvm.compiler.options.OptionKey;
import org.graalvm.compiler.phases.common.AbstractInliningPhase;
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
import org.graalvm.compiler.phases.common.inlining.policy.GreedyInliningPolicy;
import org.graalvm.compiler.phases.common.inlining.policy.InliningPolicy;
import org.graalvm.compiler.phases.common.inlining.walker.InliningData;
import org.graalvm.compiler.phases.tiers.HighTierContext;
public class InliningPhase extends AbstractInliningPhase {
public static class Options {
@Option(help = "Unconditionally inline intrinsics", type = OptionType.Debug)
public static final OptionKey<Boolean> AlwaysInlineIntrinsics = new OptionKey<>(false);
@Option(help = "Per-compilation method inlining exploration limit before giving up (use 0 to disable)", type = OptionType.Debug)
public static final OptionKey<Integer> MethodInlineBailoutLimit = new OptionKey<>(5000);
}
private final InliningPolicy inliningPolicy;
private final CanonicalizerPhase canonicalizer;
private LinkedList<Invoke> rootInvokes = null;
private int maxMethodPerInlining = Integer.MAX_VALUE;
public InliningPhase(CanonicalizerPhase canonicalizer) {
this(new GreedyInliningPolicy(null), canonicalizer);
}
public InliningPhase(Map<Invoke, Double> hints, CanonicalizerPhase canonicalizer) {
this(new GreedyInliningPolicy(hints), canonicalizer);
}
public InliningPhase(InliningPolicy policy, CanonicalizerPhase canonicalizer) {
this.inliningPolicy = policy;
this.canonicalizer = canonicalizer;
}
public CanonicalizerPhase getCanonicalizer() {
return canonicalizer;
}
@Override
public float codeSizeIncrease() {
return 10_000f;
}
public void setMaxMethodsPerInlining(int max) {
maxMethodPerInlining = max;
}
public void setRootInvokes(LinkedList<Invoke> rootInvokes) {
this.rootInvokes = rootInvokes;
}
@Override
protected void run(final StructuredGraph graph, final HighTierContext context) {
final InliningData data = new InliningData(graph, context, maxMethodPerInlining, canonicalizer, inliningPolicy, rootInvokes);
int count = 0;
assert data.repOK();
int limit = Options.MethodInlineBailoutLimit.getValue(graph.getOptions());
while (data.hasUnprocessedGraphs()) {
boolean wasInlined = data.moveForward();
assert data.repOK();
count++;
if (!wasInlined) {
if (limit > 0 && count == limit) {
break;
}
}
}
assert data.inliningDepth() == 0 || count == limit;
assert data.graphCount() == 0 || count == limit;
}
}