1 /*
   2  * Copyright (c) 2016, 2019, 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  */
  23 
  24 
  25 package org.graalvm.compiler.hotspot.phases.aot;
  26 
  27 import static org.graalvm.compiler.core.common.GraalOptions.InlineEverything;
  28 import static org.graalvm.compiler.core.common.GraalOptions.TrivialInliningSize;
  29 
  30 import java.util.Map;
  31 
  32 import org.graalvm.compiler.core.common.GraalOptions;
  33 import org.graalvm.compiler.nodes.Invoke;
  34 import org.graalvm.compiler.nodes.spi.Replacements;
  35 import org.graalvm.compiler.options.Option;
  36 import org.graalvm.compiler.options.OptionKey;
  37 import org.graalvm.compiler.options.OptionType;
  38 import org.graalvm.compiler.options.OptionValues;
  39 import org.graalvm.compiler.phases.common.inlining.InliningUtil;
  40 import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
  41 import org.graalvm.compiler.phases.common.inlining.policy.GreedyInliningPolicy;
  42 import org.graalvm.compiler.phases.common.inlining.policy.InliningPolicy;
  43 import org.graalvm.compiler.phases.common.inlining.walker.MethodInvocation;
  44 
  45 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  46 
  47 public class AOTInliningPolicy extends GreedyInliningPolicy {
  48     public static class Options {
  49         // @formatter:off
  50         @Option(help = "", type = OptionType.Expert)
  51         public static final OptionKey<Double> AOTInliningDepthToSizeRate = new OptionKey<>(2.5);
  52         @Option(help = "", type = OptionType.Expert)
  53         public static final OptionKey<Integer> AOTInliningSizeMaximum = new OptionKey<>(300);
  54         @Option(help = "", type = OptionType.Expert)
  55         public static final OptionKey<Integer> AOTInliningSizeMinimum = new OptionKey<>(50);
  56         // @formatter:on
  57     }
  58 
  59     public AOTInliningPolicy(Map<Invoke, Double> hints) {
  60         super(hints);
  61     }
  62 
  63     protected double maxInliningSize(int inliningDepth, OptionValues options) {
  64         return Math.max(Options.AOTInliningSizeMaximum.getValue(options) / (inliningDepth * Options.AOTInliningDepthToSizeRate.getValue(options)), Options.AOTInliningSizeMinimum.getValue(options));
  65     }
  66 
  67     @Override
  68     public Decision isWorthInlining(Replacements replacements, MethodInvocation invocation, InlineInfo calleeInfo, int inliningDepth, boolean fullyProcessed) {
  69         OptionValues options = calleeInfo.graph().getOptions();
  70         final boolean isTracing = GraalOptions.TraceInlining.getValue(options);
  71         final InlineInfo info = invocation.callee();
  72 
  73         for (int i = 0; i < info.numberOfMethods(); ++i) {
  74             HotSpotResolvedObjectType t = (HotSpotResolvedObjectType) info.methodAt(i).getDeclaringClass();
  75             if (t.getFingerprint() == 0) {
  76                 return InliningPolicy.Decision.NO;
  77             }
  78         }
  79 
  80         final double probability = invocation.probability();
  81         final double relevance = invocation.relevance();
  82 
  83         if (InlineEverything.getValue(options)) {
  84             InliningUtil.traceInlinedMethod(info, inliningDepth, fullyProcessed, "inline everything");
  85             return InliningPolicy.Decision.YES.withReason(isTracing, "inline everything");
  86         }
  87 
  88         if (isIntrinsic(replacements, info)) {
  89             InliningUtil.traceInlinedMethod(info, inliningDepth, fullyProcessed, "intrinsic");
  90             return InliningPolicy.Decision.YES.withReason(isTracing, "intrinsic");
  91         }
  92 
  93         if (info.shouldInline()) {
  94             InliningUtil.traceInlinedMethod(info, inliningDepth, fullyProcessed, "forced inlining");
  95             return InliningPolicy.Decision.YES.withReason(isTracing, "forced inlining");
  96         }
  97 
  98         double inliningBonus = getInliningBonus(info);
  99         int nodes = info.determineNodeCount();
 100 
 101         if (nodes < TrivialInliningSize.getValue(options) * inliningBonus) {
 102             InliningUtil.traceInlinedMethod(info, inliningDepth, fullyProcessed, "trivial (relevance=%f, probability=%f, bonus=%f, nodes=%d)", relevance, probability, inliningBonus, nodes);
 103             return InliningPolicy.Decision.YES.withReason(isTracing, "trivial (relevance=%f, probability=%f, bonus=%f, nodes=%d)", relevance, probability, inliningBonus, nodes);
 104         }
 105 
 106         double maximumNodes = computeMaximumSize(relevance, (int) (maxInliningSize(inliningDepth, options) * inliningBonus));
 107         if (nodes <= maximumNodes) {
 108             InliningUtil.traceInlinedMethod(info, inliningDepth, fullyProcessed, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d <= %f)", relevance, probability, inliningBonus,
 109                             nodes, maximumNodes);
 110             return InliningPolicy.Decision.YES.withReason(isTracing, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d <= %f)", relevance, probability, inliningBonus,
 111                             nodes, maximumNodes);
 112         }
 113 
 114         InliningUtil.traceNotInlinedMethod(info, inliningDepth, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d > %f)", relevance, probability, inliningBonus, nodes, maximumNodes);
 115         return InliningPolicy.Decision.NO.withReason(isTracing, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d > %f)", relevance, probability, inliningBonus, nodes, maximumNodes);
 116     }
 117 }