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