Search in sources :

Example 1 with WeightedCallTargets

use of org.jikesrvm.adaptive.database.callgraph.WeightedCallTargets in project JikesRVM by JikesRVM.

the class DefaultInlineOracle method shouldInline.

@Override
public InlineDecision shouldInline(final CompilationState state) {
    final OptOptions opts = state.getOptions();
    final boolean verbose = opts.PRINT_DETAILED_INLINE_REPORT;
    if (!opts.INLINE) {
        return NO("inlining not enabled");
    }
    final RVMMethod staticCallee = state.obtainTarget();
    final NormalMethod rootMethod = state.getRootMethod();
    final RVMMethod caller = state.getMethod();
    final int bcIndex = state.getRealBytecodeIndex();
    if (verbose)
        VM.sysWriteln("Begin inline decision for " + "<" + caller + "," + bcIndex + "," + staticCallee + ">");
    // Stage 1: We definitely don't inline certain methods
    if (!state.isInvokeInterface()) {
        if (staticCallee.isNative()) {
            reportUnguardedDecisionIfVerbose("NO: native method", verbose);
            return NO("native method");
        }
        if (hasNoInlinePragma(staticCallee, state)) {
            reportUnguardedDecisionIfVerbose("NO: pragmaNoInline", verbose);
            return NO("pragmaNoInline");
        }
        // traces (see StackTrace).
        if (staticCallee.isObjectInitializer() && staticCallee.getDeclaringClass().isAssignableToThrowable()) {
            reportUnguardedDecisionIfVerbose("NO: constructor of class assignable to throwable", verbose);
            return NO("constructor of class assignable to throwable");
        }
    }
    // at worse replace one call instruction with another one).
    if (!state.isInvokeInterface() && !staticCallee.isAbstract()) {
        // above test passes
        if (state.getHasPreciseTarget() || !needsGuard(staticCallee)) {
            // call is guardless
            int inlinedSizeEstimate = inlinedSizeEstimate((NormalMethod) staticCallee, state);
            if (inlinedSizeEstimate < opts.INLINE_MAX_ALWAYS_INLINE_TARGET_SIZE) {
                // inlining is desirable
                if (!state.getSequence().containsMethod(staticCallee)) {
                    // not recursive
                    reportUnguardedDecisionIfVerbose("YES: trivial guardless inline", verbose);
                    return YES(staticCallee, "trivial inline");
                }
            }
            if (hasInlinePragma(staticCallee, state)) {
                // inlining is desirable
                if (!state.getSequence().containsMethod(staticCallee)) {
                    // not recursive
                    reportUnguardedDecisionIfVerbose("YES: pragma inline", verbose);
                    return YES(staticCallee, "pragma inline");
                }
            }
        }
    }
    if (opts.getOptLevel() == 0) {
        // at opt level 0, trivial unguarded inlines are the only kind we consider
        reportUnguardedDecisionIfVerbose("NO: only do trivial inlines at O0", verbose);
        return NO("Only do trivial inlines at O0");
    }
    // than faster boot image compilation.
    if (VM.runningVM && rootMethod.inlinedSizeEstimate() > opts.INLINE_MASSIVE_METHOD_SIZE) {
        reportUnguardedDecisionIfVerbose("NO: only do trivial inlines into massive methods when the VM is running", verbose);
        return NO("Root method is massive; no non-trivial inlines");
    }
    // Stage 3: Determine based on profile data and static information
    // what are the possible targets of this call.
    WeightedCallTargets targets = null;
    boolean purelyStatic = true;
    if (Controller.dcgAvailable() && Controller.options.ADAPTIVE_INLINING) {
        targets = Controller.dcg.getCallTargets(caller, bcIndex);
        if (targets != null) {
            reportProfilingIfVerbose("Found profile data", verbose);
            purelyStatic = false;
            WeightedCallTargets filteredTargets = targets.filter(staticCallee, state.getHasPreciseTarget());
            if (targets != filteredTargets) {
                reportProfilingIfVerbose("Profiled callees filtered based on static information", verbose);
                targets = filteredTargets;
                if (targets == null) {
                    reportProfilingIfVerbose("After filterting no profile data...", verbose);
                    // After filtering, no matching profile data, fall back to
                    // static information to avoid degradations
                    targets = WeightedCallTargets.create(staticCallee, 0);
                    purelyStatic = true;
                }
            }
        }
    }
    // we are inspecting it to determine how/whether to do the inline guard.
    synchronized (RVMClass.classLoadListener) {
        boolean guardOverrideOnStaticCallee = false;
        if (targets == null) {
            reportUnguardedDecisionIfVerbose("no profile data", verbose);
            // be able to share all the decision making logic.
            if (state.isInvokeInterface()) {
                if (opts.INLINE_GUARDED_INTERFACES) {
                    RVMMethod singleImpl = InterfaceHierarchy.getUniqueImplementation(staticCallee);
                    if (singleImpl != null && hasBody(singleImpl)) {
                        if (verbose) {
                            VM.sysWriteln("\tFound a single implementation " + singleImpl + " of an interface method " + staticCallee);
                        }
                        targets = WeightedCallTargets.create(singleImpl, 0);
                        guardOverrideOnStaticCallee = true;
                    }
                }
            } else {
                // invokestatic, invokevirtual, invokespecial
                if (staticCallee.isAbstract()) {
                    // look for single non-abstract implementation of the abstract method
                    RVMClass klass = staticCallee.getDeclaringClass();
                    while (true) {
                        RVMClass[] subClasses = klass.getSubClasses();
                        // multiple subclasses => multiple targets
                        if (subClasses.length != 1)
                            break;
                        RVMMethod singleImpl = subClasses[0].findDeclaredMethod(staticCallee.getName(), staticCallee.getDescriptor());
                        if (singleImpl != null && !singleImpl.isAbstract()) {
                            // found something
                            reportProfilingIfVerbose("single impl of abstract method", verbose);
                            targets = WeightedCallTargets.create(singleImpl, 0);
                            guardOverrideOnStaticCallee = true;
                            break;
                        }
                        // keep crawling down the hierarchy
                        klass = subClasses[0];
                    }
                } else {
                    targets = WeightedCallTargets.create(staticCallee, 0);
                }
            }
        }
        // If there is a precise target, then targets contains exactly that target method.
        if (targets == null)
            return NO("No potential targets identified");
        // Stage 4: We have one or more targets.  Determine what if anything should be done with them.
        final ArrayList<RVMMethod> methodsToInline = new ArrayList<RVMMethod>();
        final ArrayList<Boolean> methodsNeedGuard = new ArrayList<Boolean>();
        final double callSiteWeight = targets.totalWeight();
        // real closures anyone?
        final boolean goosc = guardOverrideOnStaticCallee;
        // real closures anyone?
        final boolean ps = purelyStatic;
        targets.visitTargets(new WeightedCallTargets.Visitor() {

            @Override
            public void visit(RVMMethod callee, double weight) {
                if (hasBody(callee)) {
                    reportInitialProfileState(verbose, callee, weight);
                    // Don't inline recursively and respect no inline pragmas
                    InlineSequence seq = state.getSequence();
                    if (seq.containsMethod(callee)) {
                        reportSelectionIfVerbose("Reject: recursive", verbose);
                        return;
                    }
                    if (hasNoInlinePragma(callee, state)) {
                        reportSelectionIfVerbose("Reject: noinline pragma", verbose);
                        return;
                    }
                    // more or less figure out the guard situation early -- impacts size estimate.
                    boolean needsGuard = !state.getHasPreciseTarget() && (staticCallee != callee || needsGuard(staticCallee));
                    if (needsGuard && isForbiddenSpeculation(state.getRootMethod(), callee)) {
                        reportSelectionIfVerbose("Reject: forbidden speculation", verbose);
                        return;
                    }
                    boolean currentlyFinal = (goosc || (staticCallee == callee)) && isCurrentlyFinal(callee, !opts.guardWithClassTest());
                    boolean preEx = needsGuard && state.getIsExtant() && opts.INLINE_PREEX && currentlyFinal;
                    if (needsGuard && !preEx) {
                        if (!opts.INLINE_GUARDED) {
                            reportSelectionIfVerbose("Reject: guarded inlining disabled", verbose);
                            return;
                        }
                        if (!currentlyFinal && ps) {
                            reportSelectionIfVerbose("Reject: multiple targets and no profile data", verbose);
                            return;
                        }
                    }
                    // Estimate cost of performing this inlining action.
                    // Includes cost of guard & off-branch call if they are going to be generated.
                    boolean decideYes = false;
                    if (hasInlinePragma(callee, state)) {
                        reportSelectionIfVerbose("Select: pragma inline", verbose);
                        decideYes = true;
                    } else {
                        // Preserve previous inlining decisions
                        // Not the best thing in the world due to phase shifts, but
                        // it does buy some degree of stability. So, it is probably the lesser
                        // of two evils.
                        CompiledMethod prev = state.getRootMethod().getCurrentCompiledMethod();
                        if (prev != null && prev.getCompilerType() == CompiledMethod.OPT) {
                            if (((OptCompiledMethod) prev).getMCMap().hasInlinedEdge(caller, bcIndex, callee)) {
                                reportSelectionIfVerbose("Select: Previously inlined", verbose);
                                decideYes = true;
                            }
                        }
                        if (!decideYes) {
                            int inlinedSizeEstimate = inlinedSizeEstimate((NormalMethod) callee, state);
                            int cost = inliningActionCost(inlinedSizeEstimate, needsGuard, preEx, opts);
                            int maxCost = opts.INLINE_MAX_TARGET_SIZE;
                            if (callSiteWeight > Controller.options.INLINE_AI_SEED_MULTIPLIER) {
                                // real profile data with enough samples for us to trust it.
                                // Use weight and shape of call site distribution to compute
                                // a higher maxCost.
                                double fractionOfSample = weight / callSiteWeight;
                                if (needsGuard && fractionOfSample < opts.INLINE_AI_MIN_CALLSITE_FRACTION) {
                                    // This call accounts for less than INLINE_AI_MIN_CALLSITE_FRACTION
                                    // of the profiled targets at this call site.
                                    // It is highly unlikely to be profitable to inline it.
                                    reportSelectionIfVerbose("Reject: less than INLINE_AI_MIN_CALLSITE_FRACTION of distribution", verbose);
                                    maxCost = 0;
                                } else {
                                    if (cost > maxCost) {
                                        /* We're going to increase the maximum callee size (maxCost) we're willing
                       * to inline based on how "hot" (what % of the total weight in the
                       * dynamic call graph) the edge is.
                       */
                                        double adjustedWeight = AdaptiveInlining.adjustedWeight(weight);
                                        if (adjustedWeight > Controller.options.INLINE_AI_HOT_CALLSITE_THRESHOLD) {
                                            /* A truly hot edge; use the max allowable callee size */
                                            maxCost = opts.INLINE_AI_MAX_TARGET_SIZE;
                                        } else {
                                            /* A warm edge, we will use a value between the static default and the max allowable.
                         * The code below simply does a linear interpolation between 2x static default
                         * and max allowable.
                         * Other alternatives would be to do a log interpolation or some other step function.
                         */
                                            int range = opts.INLINE_AI_MAX_TARGET_SIZE - 2 * opts.INLINE_MAX_TARGET_SIZE;
                                            double slope = (range) / Controller.options.INLINE_AI_HOT_CALLSITE_THRESHOLD;
                                            int scaledAdj = (int) (slope * adjustedWeight);
                                            maxCost += opts.INLINE_MAX_TARGET_SIZE + scaledAdj;
                                        }
                                    }
                                }
                            }
                            // Somewhat bogus, but if we get really deeply inlined we start backing off.
                            int curDepth = state.getInlineDepth();
                            if (curDepth > opts.INLINE_MAX_INLINE_DEPTH) {
                                maxCost /= (curDepth - opts.INLINE_MAX_INLINE_DEPTH + 1);
                            }
                            decideYes = cost <= maxCost;
                            if (decideYes) {
                                reportSelectionIfVerbose("Accept: cost of " + cost + " was below threshold " + maxCost, verbose);
                            } else {
                                reportSelectionIfVerbose("Reject: cost of " + cost + " was above threshold " + maxCost, verbose);
                            }
                        }
                    }
                    if (decideYes) {
                        // Ok, we're going to inline it.
                        // Record that and also whether or not we think it needs a guard.
                        methodsToInline.add(callee);
                        if (preEx) {
                            ClassLoadingDependencyManager cldm = (ClassLoadingDependencyManager) RVMClass.classLoadListener;
                            if (ClassLoadingDependencyManager.TRACE || ClassLoadingDependencyManager.DEBUG) {
                                cldm.report("PREEX_INLINE: Inlined " + callee + " into " + caller);
                            }
                            cldm.addNotOverriddenDependency(callee, state.getCompiledMethod());
                            if (goosc) {
                                cldm.addNotOverriddenDependency(staticCallee, state.getCompiledMethod());
                            }
                            methodsNeedGuard.add(Boolean.FALSE);
                        } else {
                            methodsNeedGuard.add(needsGuard);
                        }
                    }
                }
            }

            private void reportInitialProfileState(final boolean verbose, RVMMethod callee, double weight) {
                double adjustedWeight = AdaptiveInlining.adjustedWeight(weight);
                String sampleString = " samples (";
                if (Double.isNaN(adjustedWeight)) {
                    sampleString += "no DCG available)";
                } else {
                    sampleString += (100 * adjustedWeight) + "%)";
                }
                reportProfilingIfVerbose("Evaluating target " + callee + " with " + weight + sampleString, verbose);
            }
        });
        // Stage 5: Choose guards and package up the results in an InlineDecision object
        if (methodsToInline.isEmpty()) {
            InlineDecision d = NO("No desirable targets");
            reportGuardedDecisionIfVerbose(d, verbose);
            return d;
        } else if (methodsToInline.size() == 1) {
            RVMMethod target = methodsToInline.get(0);
            boolean needsGuard = methodsNeedGuard.get(0);
            if (needsGuard) {
                if ((guardOverrideOnStaticCallee || target == staticCallee) && isCurrentlyFinal(target, !opts.guardWithClassTest())) {
                    InlineDecision d = guardedYES(target, chooseGuard(caller, target, staticCallee, state, true), "Guarded inline of single static target");
                    /*
             * Determine if it is allowable to put an OSR point in the failed case of
             * the guarded inline instead of generating a real call instruction.
             * There are several conditions that must be met for this to be allowable:
             *   (1) OSR guarded inlining and recompilation must both be enabled
             *   (2) The current context must be an interruptible method
             *   (3) The application must be started.  This is a rough proxy for the VM
             *       being fully booted so we can actually get through the OSR process.
             *       Note: One implication of this requirement is that we will
             *       never put an OSR on an off-branch of a guarded inline in bootimage
             *       code.
             */
                    if (opts.OSR_GUARDED_INLINING && Controller.options.ENABLE_RECOMPILATION && caller.isInterruptible() && OptimizingCompiler.getAppStarted()) {
                        if (VM.VerifyAssertions)
                            VM._assert(VM.runningVM);
                        d.setOSRTestFailed();
                    }
                    if (verbose)
                        VM.sysWriteln("\tDecide: " + d);
                    return d;
                } else {
                    InlineDecision d = guardedYES(target, chooseGuard(caller, target, staticCallee, state, false), "Guarded inlining of one potential target");
                    reportGuardedDecisionIfVerbose(d, verbose);
                    return d;
                }
            } else {
                InlineDecision d = YES(target, "Unique and desirable target");
                reportGuardedDecisionIfVerbose(d, verbose);
                return d;
            }
        } else {
            RVMMethod[] methods = new RVMMethod[methodsNeedGuard.size()];
            byte[] guards = new byte[methods.length];
            int idx = 0;
            Iterator<RVMMethod> methodIterator = methodsToInline.iterator();
            Iterator<Boolean> guardIterator = methodsNeedGuard.iterator();
            while (methodIterator.hasNext()) {
                RVMMethod target = methodIterator.next();
                boolean needsGuard = guardIterator.next();
                if (VM.VerifyAssertions) {
                    if (!needsGuard) {
                        VM.sysWriteln("Error, inlining for " + methodsToInline.size() + " targets");
                        VM.sysWriteln("Inlining into " + rootMethod + " at bytecode index " + bcIndex);
                        VM.sysWriteln("Method: " + target + " doesn't need a guard");
                        for (int i = 0; i < methodsToInline.size(); i++) {
                            VM.sysWriteln("  Method " + i + ": " + methodsToInline.get(i));
                            VM.sysWriteln("  NeedsGuard: " + methodsNeedGuard.get(i));
                        }
                        VM._assert(VM.NOT_REACHED);
                    }
                }
                methods[idx] = target;
                guards[idx] = chooseGuard(caller, target, staticCallee, state, false);
                idx++;
            }
            InlineDecision d = guardedYES(methods, guards, "Inline multiple targets");
            reportGuardedDecisionIfVerbose(d, verbose);
            return d;
        }
    }
}
Also used : OptCompiledMethod(org.jikesrvm.compilers.opt.runtimesupport.OptCompiledMethod) ArrayList(java.util.ArrayList) OptOptions(org.jikesrvm.compilers.opt.OptOptions) OptCompiledMethod(org.jikesrvm.compilers.opt.runtimesupport.OptCompiledMethod) CompiledMethod(org.jikesrvm.compilers.common.CompiledMethod) RVMClass(org.jikesrvm.classloader.RVMClass) RVMMethod(org.jikesrvm.classloader.RVMMethod) WeightedCallTargets(org.jikesrvm.adaptive.database.callgraph.WeightedCallTargets) NormalMethod(org.jikesrvm.classloader.NormalMethod) Iterator(java.util.Iterator)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 WeightedCallTargets (org.jikesrvm.adaptive.database.callgraph.WeightedCallTargets)1 NormalMethod (org.jikesrvm.classloader.NormalMethod)1 RVMClass (org.jikesrvm.classloader.RVMClass)1 RVMMethod (org.jikesrvm.classloader.RVMMethod)1 CompiledMethod (org.jikesrvm.compilers.common.CompiledMethod)1 OptOptions (org.jikesrvm.compilers.opt.OptOptions)1 OptCompiledMethod (org.jikesrvm.compilers.opt.runtimesupport.OptCompiledMethod)1