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 */
23package org.graalvm.compiler.phases.common.inlining.policy;
24
25import static org.graalvm.compiler.phases.common.inlining.InliningPhase.Options.AlwaysInlineIntrinsics;
26
27import java.util.Map;
28
29import org.graalvm.compiler.nodes.Invoke;
30import org.graalvm.compiler.nodes.StructuredGraph;
31import org.graalvm.compiler.nodes.spi.Replacements;
32import org.graalvm.compiler.phases.common.inlining.InliningUtil;
33import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
34import org.graalvm.compiler.phases.common.inlining.info.elem.Inlineable;
35
36import jdk.vm.ci.meta.ProfilingInfo;
37import jdk.vm.ci.meta.ResolvedJavaMethod;
38
39public 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(info.graph().getOptions())) {
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        if (!onlyIntrinsics(replacements, info)) {
79            return false;
80        }
81        if (!info.shouldInline()) {
82            return false;
83        }
84        return true;
85    }
86
87    protected static int previousLowLevelGraphSize(InlineInfo info) {
88        int size = 0;
89        for (int i = 0; i < info.numberOfMethods(); i++) {
90            ResolvedJavaMethod m = info.methodAt(i);
91            ProfilingInfo profile = info.graph().getProfilingInfo(m);
92            int compiledGraphSize = profile.getCompilerIRSize(StructuredGraph.class);
93            if (compiledGraphSize > 0) {
94                size += compiledGraphSize;
95            }
96        }
97        return size;
98    }
99
100    protected static double determineInvokeProbability(InlineInfo info) {
101        double invokeProbability = 0;
102        for (int i = 0; i < info.numberOfMethods(); i++) {
103            Inlineable callee = info.inlineableElementAt(i);
104            Iterable<Invoke> invokes = callee.getInvokes();
105            if (invokes.iterator().hasNext()) {
106                for (Invoke invoke : invokes) {
107                    invokeProbability += callee.getProbability(invoke);
108                }
109            }
110        }
111        return invokeProbability;
112    }
113}
114