43972
|
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.walker;
|
|
24 |
|
|
25 |
import java.util.BitSet;
|
|
26 |
import java.util.LinkedList;
|
|
27 |
import java.util.function.ToDoubleFunction;
|
|
28 |
|
48861
|
29 |
import org.graalvm.collections.EconomicSet;
|
|
30 |
import org.graalvm.collections.Equivalence;
|
43972
|
31 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
32 |
import org.graalvm.compiler.nodes.Invoke;
|
|
33 |
import org.graalvm.compiler.nodes.ParameterNode;
|
|
34 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
35 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
36 |
import org.graalvm.compiler.phases.common.inlining.policy.AbstractInliningPolicy;
|
|
37 |
import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache;
|
|
38 |
|
|
39 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* <p>
|
|
43 |
* A {@link CallsiteHolder} whose graph has been copied already and thus can be modified without
|
|
44 |
* affecting the original (usually cached) version.
|
|
45 |
* </p>
|
|
46 |
*
|
|
47 |
* <p>
|
|
48 |
* An instance of this class is derived from an
|
|
49 |
* {@link org.graalvm.compiler.phases.common.inlining.info.elem.InlineableGraph InlineableGraph} and
|
|
50 |
* contains a subset of the information there: just the {@link Invoke} nodes from it. Such nodes are
|
|
51 |
* candidates for depth-first search of further inlining opportunities (thus the adjective
|
|
52 |
* "explorable" given to this class)
|
|
53 |
* </p>
|
|
54 |
*
|
|
55 |
* @see InliningData#moveForward()
|
|
56 |
*/
|
|
57 |
public final class CallsiteHolderExplorable extends CallsiteHolder {
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Graph in which inlining may be performed at one or more of the callsites containined in
|
|
61 |
* {@link #remainingInvokes}.
|
|
62 |
*/
|
|
63 |
private final StructuredGraph graph;
|
|
64 |
|
|
65 |
private final LinkedList<Invoke> remainingInvokes;
|
|
66 |
private final double probability;
|
|
67 |
private final double relevance;
|
|
68 |
|
|
69 |
/**
|
|
70 |
* @see #getFixedParams()
|
|
71 |
*/
|
46344
|
72 |
private final EconomicSet<ParameterNode> fixedParams;
|
43972
|
73 |
|
|
74 |
private final ToDoubleFunction<FixedNode> probabilities;
|
|
75 |
private final ComputeInliningRelevance computeInliningRelevance;
|
|
76 |
|
46344
|
77 |
public CallsiteHolderExplorable(StructuredGraph graph, double probability, double relevance, BitSet freshlyInstantiatedArguments, LinkedList<Invoke> invokes) {
|
43972
|
78 |
assert graph != null;
|
|
79 |
this.graph = graph;
|
|
80 |
this.probability = probability;
|
|
81 |
this.relevance = relevance;
|
|
82 |
this.fixedParams = fixedParamsAt(freshlyInstantiatedArguments);
|
46344
|
83 |
remainingInvokes = invokes == null ? new InliningIterator(graph).apply() : invokes;
|
43972
|
84 |
if (remainingInvokes.isEmpty()) {
|
|
85 |
probabilities = null;
|
|
86 |
computeInliningRelevance = null;
|
|
87 |
} else {
|
|
88 |
probabilities = new FixedNodeProbabilityCache();
|
|
89 |
computeInliningRelevance = new ComputeInliningRelevance(graph, probabilities);
|
|
90 |
computeProbabilities();
|
|
91 |
}
|
|
92 |
assert repOK();
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* @see #getFixedParams()
|
|
97 |
*/
|
46344
|
98 |
private EconomicSet<ParameterNode> fixedParamsAt(BitSet freshlyInstantiatedArguments) {
|
43972
|
99 |
if (freshlyInstantiatedArguments == null || freshlyInstantiatedArguments.isEmpty()) {
|
46344
|
100 |
return EconomicSet.create(Equivalence.IDENTITY);
|
43972
|
101 |
}
|
46344
|
102 |
EconomicSet<ParameterNode> result = EconomicSet.create(Equivalence.IDENTITY);
|
43972
|
103 |
for (ParameterNode p : graph.getNodes(ParameterNode.TYPE)) {
|
|
104 |
if (freshlyInstantiatedArguments.get(p.index())) {
|
|
105 |
result.add(p);
|
|
106 |
}
|
|
107 |
}
|
|
108 |
return result;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* <p>
|
|
113 |
* Parameters for which the callsite targeting {@link #graph()} provides "fixed" arguments. That
|
|
114 |
* callsite isn't referenced by this instance. Instead, it belongs to the graph of the caller of
|
|
115 |
* this {@link CallsiteHolderExplorable}
|
|
116 |
* </p>
|
|
117 |
*
|
|
118 |
* <p>
|
|
119 |
* Constant arguments don't contribute to fixed-params: those params have been removed already,
|
|
120 |
* see {@link org.graalvm.compiler.phases.common.inlining.info.elem.InlineableGraph}.
|
|
121 |
* </p>
|
|
122 |
*
|
|
123 |
* <p>
|
|
124 |
* Instead, fixed-params are those receiving freshly instantiated arguments (possibly
|
|
125 |
* instantiated several levels up in the call-hierarchy)
|
|
126 |
* </p>
|
|
127 |
*/
|
46344
|
128 |
public EconomicSet<ParameterNode> getFixedParams() {
|
43972
|
129 |
return fixedParams;
|
|
130 |
}
|
|
131 |
|
|
132 |
public boolean repOK() {
|
|
133 |
for (Invoke invoke : remainingInvokes) {
|
|
134 |
if (!invoke.asNode().isAlive() || !containsInvoke(invoke)) {
|
|
135 |
assert false;
|
|
136 |
return false;
|
|
137 |
}
|
|
138 |
if (!allArgsNonNull(invoke)) {
|
|
139 |
assert false;
|
|
140 |
return false;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
for (ParameterNode fixedParam : fixedParams) {
|
|
144 |
if (!containsParam(fixedParam)) {
|
|
145 |
assert false;
|
|
146 |
return false;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
return true;
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
public ResolvedJavaMethod method() {
|
|
154 |
return graph == null ? null : graph.method();
|
|
155 |
}
|
|
156 |
|
|
157 |
@Override
|
|
158 |
public boolean hasRemainingInvokes() {
|
|
159 |
return !remainingInvokes.isEmpty();
|
|
160 |
}
|
|
161 |
|
|
162 |
@Override
|
|
163 |
public StructuredGraph graph() {
|
|
164 |
return graph;
|
|
165 |
}
|
|
166 |
|
|
167 |
public Invoke popInvoke() {
|
|
168 |
return remainingInvokes.removeFirst();
|
|
169 |
}
|
|
170 |
|
|
171 |
public void pushInvoke(Invoke invoke) {
|
|
172 |
remainingInvokes.push(invoke);
|
|
173 |
}
|
|
174 |
|
|
175 |
public static boolean allArgsNonNull(Invoke invoke) {
|
|
176 |
for (ValueNode arg : invoke.callTarget().arguments()) {
|
|
177 |
if (arg == null) {
|
|
178 |
assert false;
|
|
179 |
return false;
|
|
180 |
}
|
|
181 |
}
|
|
182 |
return true;
|
|
183 |
}
|
|
184 |
|
|
185 |
public boolean containsInvoke(Invoke invoke) {
|
|
186 |
for (Invoke i : graph().getInvokes()) {
|
|
187 |
if (i == invoke) {
|
|
188 |
return true;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
return false;
|
|
192 |
}
|
|
193 |
|
|
194 |
public boolean containsParam(ParameterNode param) {
|
|
195 |
for (ParameterNode p : graph.getNodes(ParameterNode.TYPE)) {
|
|
196 |
if (p == param) {
|
|
197 |
return true;
|
|
198 |
}
|
|
199 |
}
|
|
200 |
return false;
|
|
201 |
}
|
|
202 |
|
|
203 |
public void computeProbabilities() {
|
|
204 |
computeInliningRelevance.compute();
|
|
205 |
}
|
|
206 |
|
|
207 |
public double invokeProbability(Invoke invoke) {
|
|
208 |
return probability * probabilities.applyAsDouble(invoke.asNode());
|
|
209 |
}
|
|
210 |
|
|
211 |
public double invokeRelevance(Invoke invoke) {
|
|
212 |
return Math.min(AbstractInliningPolicy.CapInheritedRelevance, relevance) * computeInliningRelevance.getRelevance(invoke);
|
|
213 |
}
|
|
214 |
|
|
215 |
@Override
|
|
216 |
public String toString() {
|
|
217 |
return (graph != null ? method().format("%H.%n(%p)") : "<null method>") + remainingInvokes;
|
|
218 |
}
|
|
219 |
}
|