1 /*
   2  * Copyright (c) 2011, 2018, 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.phases.common.inlining.policy;
  26 
  27 import static org.graalvm.compiler.phases.common.inlining.InliningPhase.Options.AlwaysInlineIntrinsics;
  28 
  29 import java.util.Map;
  30 
  31 import org.graalvm.compiler.nodes.Invoke;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.spi.Replacements;
  34 import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
  35 import org.graalvm.compiler.phases.common.inlining.info.elem.Inlineable;
  36 
  37 import jdk.vm.ci.meta.ProfilingInfo;
  38 import jdk.vm.ci.meta.ResolvedJavaMethod;
  39 
  40 public abstract class AbstractInliningPolicy implements InliningPolicy {
  41     public static final float RelevanceCapForInlining = 1.0f;
  42     public static final float CapInheritedRelevance = 1.0f;
  43     protected final Map<Invoke, Double> hints;
  44 
  45     public AbstractInliningPolicy(Map<Invoke, Double> hints) {
  46         this.hints = hints;
  47     }
  48 
  49     protected double computeMaximumSize(double relevance, int configuredMaximum) {
  50         double inlineRatio = Math.min(RelevanceCapForInlining, relevance);
  51         return configuredMaximum * inlineRatio;
  52     }
  53 
  54     protected double getInliningBonus(InlineInfo info) {
  55         if (hints != null && hints.containsKey(info.invoke())) {
  56             return hints.get(info.invoke());
  57         }
  58         return 1;
  59     }
  60 
  61     protected boolean isIntrinsic(Replacements replacements, InlineInfo info) {
  62         if (AlwaysInlineIntrinsics.getValue(info.graph().getOptions())) {
  63             return onlyIntrinsics(replacements, info);
  64         } else {
  65             return onlyForcedIntrinsics(replacements, info);
  66         }
  67     }
  68 
  69     private static boolean onlyIntrinsics(Replacements replacements, InlineInfo info) {
  70         for (int i = 0; i < info.numberOfMethods(); i++) {
  71             if (!replacements.hasSubstitution(info.methodAt(i), info.invoke().bci())) {
  72                 return false;
  73             }
  74         }
  75         return true;
  76     }
  77 
  78     private static boolean onlyForcedIntrinsics(Replacements replacements, InlineInfo info) {
  79         if (!onlyIntrinsics(replacements, info)) {
  80             return false;
  81         }
  82         if (!info.shouldInline()) {
  83             return false;
  84         }
  85         return true;
  86     }
  87 
  88     protected int previousLowLevelGraphSize(InlineInfo info) {
  89         int size = 0;
  90         for (int i = 0; i < info.numberOfMethods(); i++) {
  91             ResolvedJavaMethod m = info.methodAt(i);
  92             ProfilingInfo profile = info.graph().getProfilingInfo(m);
  93             int compiledGraphSize = profile.getCompilerIRSize(StructuredGraph.class);
  94             if (compiledGraphSize > 0) {
  95                 size += compiledGraphSize;
  96             }
  97         }
  98         return size;
  99     }
 100 
 101     protected double determineInvokeProbability(InlineInfo info) {
 102         double invokeProbability = 0;
 103         for (int i = 0; i < info.numberOfMethods(); i++) {
 104             Inlineable callee = info.inlineableElementAt(i);
 105             Iterable<Invoke> invokes = callee.getInvokes();
 106             if (invokes.iterator().hasNext()) {
 107                 for (Invoke invoke : invokes) {
 108                     invokeProbability += callee.getProbability(invoke);
 109                 }
 110             }
 111         }
 112         return invokeProbability;
 113     }
 114 }