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