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