1/*
2 * Copyright (c) 2016, 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.hotspot.phases.aot;
24
25import static org.graalvm.compiler.core.common.GraalOptions.InlineEverything;
26import static org.graalvm.compiler.core.common.GraalOptions.TrivialInliningSize;
27
28import java.util.Map;
29
30import org.graalvm.compiler.nodes.Invoke;
31import org.graalvm.compiler.nodes.spi.Replacements;
32import org.graalvm.compiler.options.Option;
33import org.graalvm.compiler.options.OptionKey;
34import org.graalvm.compiler.options.OptionType;
35import org.graalvm.compiler.options.OptionValues;
36import org.graalvm.compiler.phases.common.inlining.InliningUtil;
37import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
38import org.graalvm.compiler.phases.common.inlining.policy.GreedyInliningPolicy;
39import org.graalvm.compiler.phases.common.inlining.walker.MethodInvocation;
40
41import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
42
43public class AOTInliningPolicy extends GreedyInliningPolicy {
44    public static class Options {
45        // @formatter:off
46        @Option(help = "", type = OptionType.Expert)
47        public static final OptionKey<Double> AOTInliningDepthToSizeRate = new OptionKey<>(2.5);
48        @Option(help = "", type = OptionType.Expert)
49        public static final OptionKey<Integer> AOTInliningSizeMaximum = new OptionKey<>(300);
50        @Option(help = "", type = OptionType.Expert)
51        public static final OptionKey<Integer> AOTInliningSizeMinimum = new OptionKey<>(50);
52        // @formatter:on
53    }
54
55    public AOTInliningPolicy(Map<Invoke, Double> hints) {
56        super(hints);
57    }
58
59    protected double maxInliningSize(int inliningDepth, OptionValues options) {
60        return Math.max(Options.AOTInliningSizeMaximum.getValue(options) / (inliningDepth * Options.AOTInliningDepthToSizeRate.getValue(options)), Options.AOTInliningSizeMinimum.getValue(options));
61    }
62
63    @Override
64    public boolean isWorthInlining(Replacements replacements, MethodInvocation invocation, int inliningDepth, boolean fullyProcessed) {
65        final InlineInfo info = invocation.callee();
66
67        for (int i = 0; i < info.numberOfMethods(); ++i) {
68            HotSpotResolvedObjectType t = (HotSpotResolvedObjectType) info.methodAt(i).getDeclaringClass();
69            if (t.getFingerprint() == 0) {
70                return false;
71            }
72        }
73
74        final double probability = invocation.probability();
75        final double relevance = invocation.relevance();
76
77        OptionValues options = info.graph().getOptions();
78        if (InlineEverything.getValue(options)) {
79            InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "inline everything");
80            return true;
81        }
82
83        if (isIntrinsic(replacements, info)) {
84            InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "intrinsic");
85            return true;
86        }
87
88        if (info.shouldInline()) {
89            InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "forced inlining");
90            return true;
91        }
92
93        double inliningBonus = getInliningBonus(info);
94        int nodes = info.determineNodeCount();
95
96        if (nodes < TrivialInliningSize.getValue(options) * inliningBonus) {
97            InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "trivial (relevance=%f, probability=%f, bonus=%f, nodes=%d)", relevance, probability, inliningBonus, nodes);
98            return true;
99        }
100
101        double maximumNodes = computeMaximumSize(relevance, (int) (maxInliningSize(inliningDepth, options) * inliningBonus));
102        if (nodes <= maximumNodes) {
103            InliningUtil.logInlinedMethod(info, inliningDepth, fullyProcessed, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d <= %f)", relevance, probability, inliningBonus,
104                            nodes, maximumNodes);
105            return true;
106        }
107
108        InliningUtil.logNotInlinedMethod(info, inliningDepth, "relevance-based (relevance=%f, probability=%f, bonus=%f, nodes=%d > %f)", relevance, probability, inliningBonus, nodes, maximumNodes);
109        return false;
110    }
111}
112