InliningData.java revision 13264:48566d838608
1/*
2 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.phases.common.inlining.walker;
24
25import static org.graalvm.compiler.core.common.GraalOptions.Intrinsify;
26import static org.graalvm.compiler.core.common.GraalOptions.MaximumRecursiveInlining;
27import static org.graalvm.compiler.core.common.GraalOptions.MegamorphicInliningMinMethodProbability;
28
29import java.util.ArrayDeque;
30import java.util.ArrayList;
31import java.util.BitSet;
32import java.util.Collection;
33import java.util.Iterator;
34import java.util.LinkedList;
35import java.util.List;
36
37import org.graalvm.compiler.core.common.type.ObjectStamp;
38import org.graalvm.compiler.debug.CounterKey;
39import org.graalvm.compiler.debug.DebugContext;
40import org.graalvm.compiler.debug.GraalError;
41import org.graalvm.compiler.graph.Graph;
42import org.graalvm.compiler.graph.Node;
43import org.graalvm.compiler.nodes.CallTargetNode;
44import org.graalvm.compiler.nodes.Invoke;
45import org.graalvm.compiler.nodes.ParameterNode;
46import org.graalvm.compiler.nodes.StructuredGraph;
47import org.graalvm.compiler.nodes.ValueNode;
48import org.graalvm.compiler.nodes.java.AbstractNewObjectNode;
49import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
50import org.graalvm.compiler.nodes.virtual.AllocatedObjectNode;
51import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
52import org.graalvm.compiler.options.OptionValues;
53import org.graalvm.compiler.phases.OptimisticOptimizations;
54import org.graalvm.compiler.phases.common.CanonicalizerPhase;
55import org.graalvm.compiler.phases.common.inlining.InliningUtil;
56import org.graalvm.compiler.phases.common.inlining.info.AssumptionInlineInfo;
57import org.graalvm.compiler.phases.common.inlining.info.ExactInlineInfo;
58import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
59import org.graalvm.compiler.phases.common.inlining.info.MultiTypeGuardInlineInfo;
60import org.graalvm.compiler.phases.common.inlining.info.TypeGuardInlineInfo;
61import org.graalvm.compiler.phases.common.inlining.info.elem.Inlineable;
62import org.graalvm.compiler.phases.common.inlining.info.elem.InlineableGraph;
63import org.graalvm.compiler.phases.common.inlining.policy.InliningPolicy;
64import org.graalvm.compiler.phases.tiers.HighTierContext;
65import org.graalvm.compiler.phases.util.Providers;
66import org.graalvm.util.EconomicSet;
67import org.graalvm.util.Equivalence;
68
69import jdk.vm.ci.code.BailoutException;
70import jdk.vm.ci.meta.Assumptions.AssumptionResult;
71import jdk.vm.ci.meta.JavaTypeProfile;
72import jdk.vm.ci.meta.ResolvedJavaMethod;
73import jdk.vm.ci.meta.ResolvedJavaType;
74
75/**
76 * <p>
77 * The space of inlining decisions is explored depth-first with the help of a stack realized by
78 * {@link InliningData}. At any point in time, the topmost element of that stack consists of:
79 * <ul>
80 * <li>the callsite under consideration is tracked as a {@link MethodInvocation}.</li>
81 * <li>one or more {@link CallsiteHolder}s, all of them associated to the callsite above. Why more
82 * than one? Depending on the type-profile for the receiver more than one concrete method may be
83 * feasible target.</li>
84 * </ul>
85 * </p>
86 *
87 * <p>
88 * The bottom element in the stack consists of:
89 * <ul>
90 * <li>a single {@link MethodInvocation} (the
91 * {@link org.graalvm.compiler.phases.common.inlining.walker.MethodInvocation#isRoot root} one, ie
92 * the unknown caller of the root graph)</li>
93 * <li>a single {@link CallsiteHolder} (the root one, for the method on which inlining was called)
94 * </li>
95 * </ul>
96 * </p>
97 *
98 * @see #moveForward()
99 */
100public class InliningData {
101
102    // Counters
103    private static final CounterKey counterInliningPerformed = DebugContext.counter("InliningPerformed");
104    private static final CounterKey counterInliningRuns = DebugContext.counter("InliningRuns");
105    private static final CounterKey counterInliningConsidered = DebugContext.counter("InliningConsidered");
106
107    /**
108     * Call hierarchy from outer most call (i.e., compilation unit) to inner most callee.
109     */
110    private final ArrayDeque<CallsiteHolder> graphQueue = new ArrayDeque<>();
111    private final ArrayDeque<MethodInvocation> invocationQueue = new ArrayDeque<>();
112
113    private final HighTierContext context;
114    private final int maxMethodPerInlining;
115    private final CanonicalizerPhase canonicalizer;
116    private final InliningPolicy inliningPolicy;
117    private final StructuredGraph rootGraph;
118    private final DebugContext debug;
119
120    private int maxGraphs;
121
122    public InliningData(StructuredGraph rootGraph, HighTierContext context, int maxMethodPerInlining, CanonicalizerPhase canonicalizer, InliningPolicy inliningPolicy, LinkedList<Invoke> rootInvokes) {
123        assert rootGraph != null;
124        this.context = context;
125        this.maxMethodPerInlining = maxMethodPerInlining;
126        this.canonicalizer = canonicalizer;
127        this.inliningPolicy = inliningPolicy;
128        this.maxGraphs = 1;
129        this.rootGraph = rootGraph;
130        this.debug = rootGraph.getDebug();
131
132        invocationQueue.push(new MethodInvocation(null, 1.0, 1.0, null));
133        graphQueue.push(new CallsiteHolderExplorable(rootGraph, 1.0, 1.0, null, rootInvokes));
134    }
135
136    public static boolean isFreshInstantiation(ValueNode arg) {
137        return (arg instanceof AbstractNewObjectNode) || (arg instanceof AllocatedObjectNode) || (arg instanceof VirtualObjectNode);
138    }
139
140    private String checkTargetConditionsHelper(ResolvedJavaMethod method, int invokeBci) {
141        OptionValues options = rootGraph.getOptions();
142        if (method == null) {
143            return "the method is not resolved";
144        } else if (method.isNative() && (!Intrinsify.getValue(options) || !InliningUtil.canIntrinsify(context.getReplacements(), method, invokeBci))) {
145            return "it is a non-intrinsic native method";
146        } else if (method.isAbstract()) {
147            return "it is an abstract method";
148        } else if (!method.getDeclaringClass().isInitialized()) {
149            return "the method's class is not initialized";
150        } else if (!method.canBeInlined()) {
151            return "it is marked non-inlinable";
152        } else if (countRecursiveInlining(method) > MaximumRecursiveInlining.getValue(options)) {
153            return "it exceeds the maximum recursive inlining depth";
154        } else {
155            if (new OptimisticOptimizations(rootGraph.getProfilingInfo(method), options).lessOptimisticThan(context.getOptimisticOptimizations())) {
156                return "the callee uses less optimistic optimizations than caller";
157            } else {
158                return null;
159            }
160        }
161    }
162
163    private boolean checkTargetConditions(Invoke invoke, ResolvedJavaMethod method) {
164        final String failureMessage = checkTargetConditionsHelper(method, invoke.bci());
165        if (failureMessage == null) {
166            return true;
167        } else {
168            InliningUtil.logNotInlined(invoke, inliningDepth(), method, failureMessage);
169            return false;
170        }
171    }
172
173    /**
174     * Determines if inlining is possible at the given invoke node.
175     *
176     * @param invoke the invoke that should be inlined
177     * @return an instance of InlineInfo, or null if no inlining is possible at the given invoke
178     */
179    private InlineInfo getInlineInfo(Invoke invoke) {
180        final String failureMessage = InliningUtil.checkInvokeConditions(invoke);
181        if (failureMessage != null) {
182            InliningUtil.logNotInlinedMethod(invoke, failureMessage);
183            return null;
184        }
185        MethodCallTargetNode callTarget = (MethodCallTargetNode) invoke.callTarget();
186        ResolvedJavaMethod targetMethod = callTarget.targetMethod();
187
188        if (callTarget.invokeKind() == CallTargetNode.InvokeKind.Special || targetMethod.canBeStaticallyBound()) {
189            return getExactInlineInfo(invoke, targetMethod);
190        }
191
192        assert callTarget.invokeKind().isIndirect();
193
194        ResolvedJavaType holder = targetMethod.getDeclaringClass();
195        if (!(callTarget.receiver().stamp() instanceof ObjectStamp)) {
196            return null;
197        }
198        ObjectStamp receiverStamp = (ObjectStamp) callTarget.receiver().stamp();
199        if (receiverStamp.alwaysNull()) {
200            // Don't inline if receiver is known to be null
201            return null;
202        }
203        ResolvedJavaType contextType = invoke.getContextType();
204        if (receiverStamp.type() != null) {
205            // the invoke target might be more specific than the holder (happens after inlining:
206            // parameters lose their declared type...)
207            ResolvedJavaType receiverType = receiverStamp.type();
208            if (receiverType != null && holder.isAssignableFrom(receiverType)) {
209                holder = receiverType;
210                if (receiverStamp.isExactType()) {
211                    assert targetMethod.getDeclaringClass().isAssignableFrom(holder) : holder + " subtype of " + targetMethod.getDeclaringClass() + " for " + targetMethod;
212                    ResolvedJavaMethod resolvedMethod = holder.resolveConcreteMethod(targetMethod, contextType);
213                    if (resolvedMethod != null) {
214                        return getExactInlineInfo(invoke, resolvedMethod);
215                    }
216                }
217            }
218        }
219
220        if (holder.isArray()) {
221            // arrays can be treated as Objects
222            ResolvedJavaMethod resolvedMethod = holder.resolveConcreteMethod(targetMethod, contextType);
223            if (resolvedMethod != null) {
224                return getExactInlineInfo(invoke, resolvedMethod);
225            }
226        }
227
228        AssumptionResult<ResolvedJavaType> leafConcreteSubtype = holder.findLeafConcreteSubtype();
229        if (leafConcreteSubtype != null) {
230            ResolvedJavaMethod resolvedMethod = leafConcreteSubtype.getResult().resolveConcreteMethod(targetMethod, contextType);
231            if (resolvedMethod != null) {
232                if (leafConcreteSubtype.canRecordTo(callTarget.graph().getAssumptions())) {
233                    return getAssumptionInlineInfo(invoke, resolvedMethod, leafConcreteSubtype);
234                } else {
235                    return getTypeCheckedAssumptionInfo(invoke, resolvedMethod, leafConcreteSubtype.getResult());
236                }
237            }
238        }
239
240        AssumptionResult<ResolvedJavaMethod> concrete = holder.findUniqueConcreteMethod(targetMethod);
241        if (concrete != null && concrete.canRecordTo(callTarget.graph().getAssumptions())) {
242            return getAssumptionInlineInfo(invoke, concrete.getResult(), concrete);
243        }
244
245        // type check based inlining
246        return getTypeCheckedInlineInfo(invoke, targetMethod);
247    }
248
249    private InlineInfo getTypeCheckedAssumptionInfo(Invoke invoke, ResolvedJavaMethod method, ResolvedJavaType type) {
250        if (!checkTargetConditions(invoke, method)) {
251            return null;
252        }
253        return new TypeGuardInlineInfo(invoke, method, type);
254    }
255
256    private InlineInfo getTypeCheckedInlineInfo(Invoke invoke, ResolvedJavaMethod targetMethod) {
257        JavaTypeProfile typeProfile = ((MethodCallTargetNode) invoke.callTarget()).getProfile();
258        if (typeProfile == null) {
259            InliningUtil.logNotInlined(invoke, inliningDepth(), targetMethod, "no type profile exists");
260            return null;
261        }
262
263        JavaTypeProfile.ProfiledType[] ptypes = typeProfile.getTypes();
264        if (ptypes == null || ptypes.length <= 0) {
265            InliningUtil.logNotInlined(invoke, inliningDepth(), targetMethod, "no types in profile");
266            return null;
267        }
268        ResolvedJavaType contextType = invoke.getContextType();
269        double notRecordedTypeProbability = typeProfile.getNotRecordedProbability();
270        final OptimisticOptimizations optimisticOpts = context.getOptimisticOptimizations();
271        OptionValues options = invoke.asNode().getOptions();
272        if (ptypes.length == 1 && notRecordedTypeProbability == 0) {
273            if (!optimisticOpts.inlineMonomorphicCalls(options)) {
274                InliningUtil.logNotInlined(invoke, inliningDepth(), targetMethod, "inlining monomorphic calls is disabled");
275                return null;
276            }
277
278            ResolvedJavaType type = ptypes[0].getType();
279            assert type.isArray() || type.isConcrete();
280            ResolvedJavaMethod concrete = type.resolveConcreteMethod(targetMethod, contextType);
281            if (!checkTargetConditions(invoke, concrete)) {
282                return null;
283            }
284            return new TypeGuardInlineInfo(invoke, concrete, type);
285        } else {
286            invoke.setPolymorphic(true);
287
288            if (!optimisticOpts.inlinePolymorphicCalls(options) && notRecordedTypeProbability == 0) {
289                InliningUtil.logNotInlinedInvoke(invoke, inliningDepth(), targetMethod, "inlining polymorphic calls is disabled (%d types)", ptypes.length);
290                return null;
291            }
292            if (!optimisticOpts.inlineMegamorphicCalls(options) && notRecordedTypeProbability > 0) {
293                // due to filtering impossible types, notRecordedTypeProbability can be > 0 although
294                // the number of types is lower than what can be recorded in a type profile
295                InliningUtil.logNotInlinedInvoke(invoke, inliningDepth(), targetMethod, "inlining megamorphic calls is disabled (%d types, %f %% not recorded types)", ptypes.length,
296                                notRecordedTypeProbability * 100);
297                return null;
298            }
299
300            // Find unique methods and their probabilities.
301            ArrayList<ResolvedJavaMethod> concreteMethods = new ArrayList<>();
302            ArrayList<Double> concreteMethodsProbabilities = new ArrayList<>();
303            for (int i = 0; i < ptypes.length; i++) {
304                ResolvedJavaMethod concrete = ptypes[i].getType().resolveConcreteMethod(targetMethod, contextType);
305                if (concrete == null) {
306                    InliningUtil.logNotInlined(invoke, inliningDepth(), targetMethod, "could not resolve method");
307                    return null;
308                }
309                int index = concreteMethods.indexOf(concrete);
310                double curProbability = ptypes[i].getProbability();
311                if (index < 0) {
312                    index = concreteMethods.size();
313                    concreteMethods.add(concrete);
314                    concreteMethodsProbabilities.add(curProbability);
315                } else {
316                    concreteMethodsProbabilities.set(index, concreteMethodsProbabilities.get(index) + curProbability);
317                }
318            }
319
320            // Clear methods that fall below the threshold.
321            if (notRecordedTypeProbability > 0) {
322                ArrayList<ResolvedJavaMethod> newConcreteMethods = new ArrayList<>();
323                ArrayList<Double> newConcreteMethodsProbabilities = new ArrayList<>();
324                for (int i = 0; i < concreteMethods.size(); ++i) {
325                    if (concreteMethodsProbabilities.get(i) >= MegamorphicInliningMinMethodProbability.getValue(options)) {
326                        newConcreteMethods.add(concreteMethods.get(i));
327                        newConcreteMethodsProbabilities.add(concreteMethodsProbabilities.get(i));
328                    }
329                }
330
331                if (newConcreteMethods.isEmpty()) {
332                    // No method left that is worth inlining.
333                    InliningUtil.logNotInlinedInvoke(invoke, inliningDepth(), targetMethod, "no methods remaining after filtering less frequent methods (%d methods previously)",
334                                    concreteMethods.size());
335                    return null;
336                }
337
338                concreteMethods = newConcreteMethods;
339                concreteMethodsProbabilities = newConcreteMethodsProbabilities;
340            }
341
342            if (concreteMethods.size() > maxMethodPerInlining) {
343                InliningUtil.logNotInlinedInvoke(invoke, inliningDepth(), targetMethod, "polymorphic call with more than %d target methods", maxMethodPerInlining);
344                return null;
345            }
346
347            // Clean out types whose methods are no longer available.
348            ArrayList<JavaTypeProfile.ProfiledType> usedTypes = new ArrayList<>();
349            ArrayList<Integer> typesToConcretes = new ArrayList<>();
350            for (JavaTypeProfile.ProfiledType type : ptypes) {
351                ResolvedJavaMethod concrete = type.getType().resolveConcreteMethod(targetMethod, contextType);
352                int index = concreteMethods.indexOf(concrete);
353                if (index == -1) {
354                    notRecordedTypeProbability += type.getProbability();
355                } else {
356                    assert type.getType().isArray() || !type.getType().isAbstract() : type + " " + concrete;
357                    usedTypes.add(type);
358                    typesToConcretes.add(index);
359                }
360            }
361
362            if (usedTypes.isEmpty()) {
363                // No type left that is worth checking for.
364                InliningUtil.logNotInlinedInvoke(invoke, inliningDepth(), targetMethod, "no types remaining after filtering less frequent types (%d types previously)", ptypes.length);
365                return null;
366            }
367
368            for (ResolvedJavaMethod concrete : concreteMethods) {
369                if (!checkTargetConditions(invoke, concrete)) {
370                    InliningUtil.logNotInlined(invoke, inliningDepth(), targetMethod, "it is a polymorphic method call and at least one invoked method cannot be inlined");
371                    return null;
372                }
373            }
374            return new MultiTypeGuardInlineInfo(invoke, concreteMethods, usedTypes, typesToConcretes, notRecordedTypeProbability);
375        }
376    }
377
378    private InlineInfo getAssumptionInlineInfo(Invoke invoke, ResolvedJavaMethod concrete, AssumptionResult<?> takenAssumption) {
379        assert concrete.isConcrete();
380        if (checkTargetConditions(invoke, concrete)) {
381            return new AssumptionInlineInfo(invoke, concrete, takenAssumption);
382        }
383        return null;
384    }
385
386    private InlineInfo getExactInlineInfo(Invoke invoke, ResolvedJavaMethod targetMethod) {
387        assert targetMethod.isConcrete();
388        if (checkTargetConditions(invoke, targetMethod)) {
389            return new ExactInlineInfo(invoke, targetMethod);
390        }
391        return null;
392    }
393
394    @SuppressWarnings("try")
395    private void doInline(CallsiteHolderExplorable callerCallsiteHolder, MethodInvocation calleeInvocation) {
396        StructuredGraph callerGraph = callerCallsiteHolder.graph();
397        InlineInfo calleeInfo = calleeInvocation.callee();
398        try {
399            try (DebugContext.Scope scope = debug.scope("doInline", callerGraph)) {
400                EconomicSet<Node> canonicalizedNodes = EconomicSet.create(Equivalence.IDENTITY);
401                canonicalizedNodes.addAll(calleeInfo.invoke().asNode().usages());
402                EconomicSet<Node> parameterUsages = calleeInfo.inline(new Providers(context));
403                canonicalizedNodes.addAll(parameterUsages);
404                counterInliningRuns.increment(debug);
405                debug.dump(DebugContext.DETAILED_LEVEL, callerGraph, "after %s", calleeInfo);
406
407                Graph.Mark markBeforeCanonicalization = callerGraph.getMark();
408
409                canonicalizer.applyIncremental(callerGraph, context, canonicalizedNodes);
410
411                // process invokes that are possibly created during canonicalization
412                for (Node newNode : callerGraph.getNewNodes(markBeforeCanonicalization)) {
413                    if (newNode instanceof Invoke) {
414                        callerCallsiteHolder.pushInvoke((Invoke) newNode);
415                    }
416                }
417
418                callerCallsiteHolder.computeProbabilities();
419
420                counterInliningPerformed.increment(debug);
421            }
422        } catch (BailoutException bailout) {
423            throw bailout;
424        } catch (AssertionError | RuntimeException e) {
425            throw new GraalError(e).addContext(calleeInfo.toString());
426        } catch (GraalError e) {
427            throw e.addContext(calleeInfo.toString());
428        } catch (Throwable e) {
429            throw debug.handle(e);
430        }
431    }
432
433    /**
434     *
435     * This method attempts:
436     * <ol>
437     * <li>to inline at the callsite given by <code>calleeInvocation</code>, where that callsite
438     * belongs to the {@link CallsiteHolderExplorable} at the top of the {@link #graphQueue}
439     * maintained in this class.</li>
440     * <li>otherwise, to devirtualize the callsite in question.</li>
441     * </ol>
442     *
443     * @return true iff inlining was actually performed
444     */
445    private boolean tryToInline(MethodInvocation calleeInvocation, int inliningDepth) {
446        CallsiteHolderExplorable callerCallsiteHolder = (CallsiteHolderExplorable) currentGraph();
447        InlineInfo calleeInfo = calleeInvocation.callee();
448        assert callerCallsiteHolder.containsInvoke(calleeInfo.invoke());
449        counterInliningConsidered.increment(debug);
450
451        if (inliningPolicy.isWorthInlining(context.getReplacements(), calleeInvocation, inliningDepth, true)) {
452            doInline(callerCallsiteHolder, calleeInvocation);
453            return true;
454        }
455
456        if (context.getOptimisticOptimizations().devirtualizeInvokes(calleeInfo.graph().getOptions())) {
457            calleeInfo.tryToDevirtualizeInvoke(new Providers(context));
458        }
459
460        return false;
461    }
462
463    /**
464     * This method picks one of the callsites belonging to the current
465     * {@link CallsiteHolderExplorable}. Provided the callsite qualifies to be analyzed for
466     * inlining, this method prepares a new stack top in {@link InliningData} for such callsite,
467     * which comprises:
468     * <ul>
469     * <li>preparing a summary of feasible targets, ie preparing an {@link InlineInfo}</li>
470     * <li>based on it, preparing the stack top proper which consists of:</li>
471     * <ul>
472     * <li>one {@link MethodInvocation}</li>
473     * <li>a {@link CallsiteHolder} for each feasible target</li>
474     * </ul>
475     * </ul>
476     *
477     * <p>
478     * The thus prepared "stack top" is needed by {@link #moveForward()} to explore the space of
479     * inlining decisions (each decision one of: backtracking, delving, inlining).
480     * </p>
481     *
482     * <p>
483     * The {@link InlineInfo} used to get things rolling is kept around in the
484     * {@link MethodInvocation}, it will be needed in case of inlining, see
485     * {@link InlineInfo#inline(Providers)}
486     * </p>
487     */
488    private void processNextInvoke() {
489        CallsiteHolderExplorable callsiteHolder = (CallsiteHolderExplorable) currentGraph();
490        Invoke invoke = callsiteHolder.popInvoke();
491        InlineInfo info = getInlineInfo(invoke);
492
493        if (info != null) {
494            info.populateInlinableElements(context, currentGraph().graph(), canonicalizer, rootGraph.getOptions());
495            double invokeProbability = callsiteHolder.invokeProbability(invoke);
496            double invokeRelevance = callsiteHolder.invokeRelevance(invoke);
497            MethodInvocation methodInvocation = new MethodInvocation(info, invokeProbability, invokeRelevance, freshlyInstantiatedArguments(invoke, callsiteHolder.getFixedParams()));
498            pushInvocationAndGraphs(methodInvocation);
499        }
500    }
501
502    /**
503     * Gets the freshly instantiated arguments.
504     * <p>
505     * A freshly instantiated argument is either:
506     * <uL>
507     * <li>an {@link InliningData#isFreshInstantiation(org.graalvm.compiler.nodes.ValueNode)}</li>
508     * <li>a fixed-param, ie a {@link ParameterNode} receiving a freshly instantiated argument</li>
509     * </uL>
510     * </p>
511     *
512     * @return the positions of freshly instantiated arguments in the argument list of the
513     *         <code>invoke</code>, or null if no such positions exist.
514     */
515    public static BitSet freshlyInstantiatedArguments(Invoke invoke, EconomicSet<ParameterNode> fixedParams) {
516        assert fixedParams != null;
517        assert paramsAndInvokeAreInSameGraph(invoke, fixedParams);
518        BitSet result = null;
519        int argIdx = 0;
520        for (ValueNode arg : invoke.callTarget().arguments()) {
521            assert arg != null;
522            if (isFreshInstantiation(arg) || (arg instanceof ParameterNode && fixedParams.contains((ParameterNode) arg))) {
523                if (result == null) {
524                    result = new BitSet();
525                }
526                result.set(argIdx);
527            }
528            argIdx++;
529        }
530        return result;
531    }
532
533    private static boolean paramsAndInvokeAreInSameGraph(Invoke invoke, EconomicSet<ParameterNode> fixedParams) {
534        if (fixedParams.isEmpty()) {
535            return true;
536        }
537        for (ParameterNode p : fixedParams) {
538            if (p.graph() != invoke.asNode().graph()) {
539                return false;
540            }
541        }
542        return true;
543    }
544
545    public int graphCount() {
546        return graphQueue.size();
547    }
548
549    public boolean hasUnprocessedGraphs() {
550        return !graphQueue.isEmpty();
551    }
552
553    private CallsiteHolder currentGraph() {
554        return graphQueue.peek();
555    }
556
557    private void popGraph() {
558        graphQueue.pop();
559        assert graphQueue.size() <= maxGraphs;
560    }
561
562    private void popGraphs(int count) {
563        assert count >= 0;
564        for (int i = 0; i < count; i++) {
565            graphQueue.pop();
566        }
567    }
568
569    private static final Object[] NO_CONTEXT = {};
570
571    /**
572     * Gets the call hierarchy of this inlining from outer most call to inner most callee.
573     */
574    private Object[] inliningContext() {
575        if (!debug.isDumpEnabled(DebugContext.INFO_LEVEL)) {
576            return NO_CONTEXT;
577        }
578        Object[] result = new Object[graphQueue.size()];
579        int i = 0;
580        for (CallsiteHolder g : graphQueue) {
581            result[i++] = g.method();
582        }
583        return result;
584    }
585
586    private MethodInvocation currentInvocation() {
587        return invocationQueue.peekFirst();
588    }
589
590    private void pushInvocationAndGraphs(MethodInvocation methodInvocation) {
591        invocationQueue.addFirst(methodInvocation);
592        InlineInfo info = methodInvocation.callee();
593        maxGraphs += info.numberOfMethods();
594        assert graphQueue.size() <= maxGraphs;
595        for (int i = 0; i < info.numberOfMethods(); i++) {
596            CallsiteHolder ch = methodInvocation.buildCallsiteHolderForElement(i);
597            assert !contains(ch.graph());
598            graphQueue.push(ch);
599            assert graphQueue.size() <= maxGraphs;
600        }
601    }
602
603    private void popInvocation() {
604        maxGraphs -= invocationQueue.peekFirst().callee().numberOfMethods();
605        assert graphQueue.size() <= maxGraphs;
606        invocationQueue.removeFirst();
607    }
608
609    public int countRecursiveInlining(ResolvedJavaMethod method) {
610        int count = 0;
611        for (CallsiteHolder callsiteHolder : graphQueue) {
612            if (method.equals(callsiteHolder.method())) {
613                count++;
614            }
615        }
616        return count;
617    }
618
619    public int inliningDepth() {
620        assert invocationQueue.size() > 0;
621        return invocationQueue.size() - 1;
622    }
623
624    @Override
625    public String toString() {
626        StringBuilder result = new StringBuilder("Invocations: ");
627
628        for (MethodInvocation invocation : invocationQueue) {
629            if (invocation.callee() != null) {
630                result.append(invocation.callee().numberOfMethods());
631                result.append("x ");
632                result.append(invocation.callee().invoke());
633                result.append("; ");
634            }
635        }
636
637        result.append("\nGraphs: ");
638        for (CallsiteHolder graph : graphQueue) {
639            result.append(graph.graph());
640            result.append("; ");
641        }
642
643        return result.toString();
644    }
645
646    /**
647     * Gets a stack trace representing the current inlining stack represented by this object.
648     */
649    public Collection<StackTraceElement> getInvocationStackTrace() {
650        List<StackTraceElement> result = new ArrayList<>();
651        for (CallsiteHolder graph : graphQueue) {
652            result.add(graph.method().asStackTraceElement(0));
653        }
654
655        return result;
656    }
657
658    private boolean contains(StructuredGraph graph) {
659        assert graph != null;
660        for (CallsiteHolder info : graphQueue) {
661            if (info.graph() == graph) {
662                return true;
663            }
664        }
665        return false;
666    }
667
668    /**
669     * <p>
670     * The stack realized by {@link InliningData} grows and shrinks as choices are made among the
671     * alternatives below:
672     * <ol>
673     * <li>not worth inlining: pop stack top, which comprises:
674     * <ul>
675     * <li>pop any remaining graphs not yet delved into</li>
676     * <li>pop the current invocation</li>
677     * </ul>
678     * </li>
679     * <li>{@link #processNextInvoke() delve} into one of the callsites hosted in the current graph,
680     * such callsite is explored next by {@link #moveForward()}</li>
681     * <li>{@link #tryToInline(MethodInvocation, int) try to inline}: move past the current graph
682     * (remove it from the topmost element).
683     * <ul>
684     * <li>If that was the last one then {@link #tryToInline(MethodInvocation, int) try to inline}
685     * the callsite under consideration (ie, the "current invocation").</li>
686     * <li>Whether inlining occurs or not, that callsite is removed from the top of
687     * {@link InliningData} .</li>
688     * </ul>
689     * </li>
690     * </ol>
691     * </p>
692     *
693     * <p>
694     * Some facts about the alternatives above:
695     * <ul>
696     * <li>the first step amounts to backtracking, the 2nd one to depth-search, and the 3rd one also
697     * involves backtracking (however possibly after inlining).</li>
698     * <li>the choice of abandon-and-backtrack or delve-into depends on
699     * {@link InliningPolicy#isWorthInlining} and {@link InliningPolicy#continueInlining}.</li>
700     * <li>the 3rd choice is picked whenever none of the previous choices are made</li>
701     * </ul>
702     * </p>
703     *
704     * @return true iff inlining was actually performed
705     */
706    @SuppressWarnings("try")
707    public boolean moveForward() {
708
709        final MethodInvocation currentInvocation = currentInvocation();
710
711        final boolean backtrack = (!currentInvocation.isRoot() && !inliningPolicy.isWorthInlining(context.getReplacements(), currentInvocation, inliningDepth(), false));
712        if (backtrack) {
713            int remainingGraphs = currentInvocation.totalGraphs() - currentInvocation.processedGraphs();
714            assert remainingGraphs > 0;
715            popGraphs(remainingGraphs);
716            popInvocation();
717            return false;
718        }
719
720        final boolean delve = currentGraph().hasRemainingInvokes() && inliningPolicy.continueInlining(currentGraph().graph());
721        if (delve) {
722            processNextInvoke();
723            return false;
724        }
725
726        popGraph();
727        if (currentInvocation.isRoot()) {
728            return false;
729        }
730
731        // try to inline
732        assert currentInvocation.callee().invoke().asNode().isAlive();
733        currentInvocation.incrementProcessedGraphs();
734        if (currentInvocation.processedGraphs() == currentInvocation.totalGraphs()) {
735            /*
736             * "all of currentInvocation's graphs processed" amounts to
737             * "all concrete methods that come into question already had the callees they contain analyzed for inlining"
738             */
739            popInvocation();
740            try (DebugContext.Scope s = debug.scope("Inlining", inliningContext())) {
741                if (tryToInline(currentInvocation, inliningDepth() + 1)) {
742                    // Report real progress only if we inline into the root graph
743                    return currentGraph().graph() == rootGraph;
744                }
745                return false;
746            } catch (Throwable e) {
747                throw debug.handle(e);
748            }
749        }
750
751        return false;
752    }
753
754    /**
755     * Checks an invariant that {@link #moveForward()} must maintain: "the top invocation records
756     * how many concrete target methods (for it) remain on the {@link #graphQueue}; those targets
757     * 'belong' to the current invocation in question.
758     */
759    private boolean topGraphsForTopInvocation() {
760        if (invocationQueue.isEmpty()) {
761            assert graphQueue.isEmpty();
762            return true;
763        }
764        if (currentInvocation().isRoot()) {
765            if (!graphQueue.isEmpty()) {
766                assert graphQueue.size() == 1;
767            }
768            return true;
769        }
770        final int remainingGraphs = currentInvocation().totalGraphs() - currentInvocation().processedGraphs();
771        final Iterator<CallsiteHolder> iter = graphQueue.iterator();
772        for (int i = (remainingGraphs - 1); i >= 0; i--) {
773            if (!iter.hasNext()) {
774                assert false;
775                return false;
776            }
777            CallsiteHolder queuedTargetCH = iter.next();
778            Inlineable targetIE = currentInvocation().callee().inlineableElementAt(i);
779            InlineableGraph targetIG = (InlineableGraph) targetIE;
780            assert queuedTargetCH.method().equals(targetIG.getGraph().method());
781        }
782        return true;
783    }
784
785    /**
786     * This method checks invariants for this class. Named after shorthand for "internal
787     * representation is ok".
788     */
789    public boolean repOK() {
790        assert topGraphsForTopInvocation();
791        return true;
792    }
793}
794