1 /*
   2  * Copyright (c) 2011, 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.phases.common.inlining.policy;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.InlineEverything;
  26 import static org.graalvm.compiler.core.common.GraalOptions.LimitInlinedInvokes;
  27 import static org.graalvm.compiler.core.common.GraalOptions.MaximumDesiredSize;
  28 import static org.graalvm.compiler.core.common.GraalOptions.MaximumInliningSize;
  29 import static org.graalvm.compiler.core.common.GraalOptions.SmallCompiledLowLevelGraphSize;
  30 import static org.graalvm.compiler.core.common.GraalOptions.TrivialInliningSize;
  31 
  32 import java.util.Map;
  33 
  34 import org.graalvm.compiler.debug.Debug;
  35 import org.graalvm.compiler.debug.DebugCounter;
  36 import org.graalvm.compiler.nodes.Invoke;
  37 import org.graalvm.compiler.nodes.StructuredGraph;
  38 import org.graalvm.compiler.nodes.spi.Replacements;
  39 import org.graalvm.compiler.options.OptionValues;
  40 import org.graalvm.compiler.phases.common.inlining.InliningUtil;
  41 import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
  42 import org.graalvm.compiler.phases.common.inlining.walker.MethodInvocation;
  43 
  44 public class GreedyInliningPolicy extends AbstractInliningPolicy {
  45 
  46     private static final DebugCounter inliningStoppedByMaxDesiredSizeCounter = Debug.counter("InliningStoppedByMaxDesiredSize");
  47 
  48     public GreedyInliningPolicy(Map<Invoke, Double> hints) {
  49         super(hints);
  50     }
  51 
  52     @Override
  53     public boolean continueInlining(StructuredGraph currentGraph) {
  54         if (InliningUtil.getNodeCount(currentGraph) >= MaximumDesiredSize.getValue(currentGraph.getOptions())) {
  55             InliningUtil.logInliningDecision("inlining is cut off by MaximumDesiredSize");
  56             inliningStoppedByMaxDesiredSizeCounter.increment();
  57             return false;
  58         }
  59         return true;
  60     }
  61 
  62     @Override
  63     public boolean isWorthInlining(Replacements replacements, MethodInvocation invocation, int inliningDepth, boolean fullyProcessed) {
  64 
  65         final InlineInfo info = invocation.callee();
  66         OptionValues options = info.graph().getOptions();
  67         final double probability = invocation.probability();
  68         final double relevance = invocation.relevance();
  69 
  70         if (InlineEverything.getValue(options)) {
  71             InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "inline everything");
  72             return true;
  73         }
  74 
  75         if (isIntrinsic(replacements, info)) {
  76             InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "intrinsic");
  77             return true;
  78         }
  79 
  80         if (info.shouldInline()) {
  81             InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "forced inlining");
  82             return true;
  83         }
  84 
  85         double inliningBonus = getInliningBonus(info);
  86         int nodes = info.determineNodeCount();
  87         int lowLevelGraphSize = previousLowLevelGraphSize(info);
  88 
  89         if (SmallCompiledLowLevelGraphSize.getValue(options) > 0 && lowLevelGraphSize > SmallCompiledLowLevelGraphSize.getValue(options) * inliningBonus) {
  90             InliningUtil.logNotInlinedMethod(info, inliningDepth, "too large previous low-level graph (low-level-nodes: %d, relevance=%f, probability=%f, bonus=%f, nodes=%d)", lowLevelGraphSize,
  91                             relevance, probability, inliningBonus, nodes);
  92             return false;
  93         }
  94 
  95         if (nodes < TrivialInliningSize.getValue(options) * 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         /*
 101          * TODO (chaeubl): invoked methods that are on important paths but not yet compiled -> will
 102          * be compiled anyways and it is likely that we are the only caller... might be useful to
 103          * inline those methods but increases bootstrap time (maybe those methods are also getting
 104          * queued in the compilation queue concurrently)
 105          */
 106         double invokes = determineInvokeProbability(info);
 107         if (LimitInlinedInvokes.getValue(options) > 0 && fullyProcessed && invokes > LimitInlinedInvokes.getValue(options) * inliningBonus) {
 108             InliningUtil.logNotInlinedMethod(info, inliningDepth, "callee invoke probability is too high (invokeP=%f, relevance=%f, probability=%f, bonus=%f, nodes=%d)", invokes, relevance,
 109                             probability, inliningBonus, nodes);
 110             return false;
 111         }
 112 
 113         double maximumNodes = computeMaximumSize(relevance, (int) (MaximumInliningSize.getValue(options) * inliningBonus));
 114         if (nodes <= maximumNodes) {
 115             InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d <= %f)", relevance, probability, inliningBonus,
 116                             nodes, maximumNodes);
 117             return true;
 118         }
 119 
 120         InliningUtil.logNotInlinedMethod(info, inliningDepth, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d > %f)", relevance, probability, inliningBonus, nodes, maximumNodes);
 121         return false;
 122     }
 123 }