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