43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2015, 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;
|
|
24 |
|
46640
|
25 |
import static jdk.vm.ci.meta.DeoptimizationAction.InvalidateReprofile;
|
|
26 |
import static jdk.vm.ci.meta.DeoptimizationReason.NullCheckException;
|
|
27 |
import static org.graalvm.compiler.core.common.GraalOptions.HotSpotPrintInlining;
|
|
28 |
|
|
29 |
import java.lang.reflect.Constructor;
|
|
30 |
import java.util.ArrayDeque;
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.List;
|
46963
|
33 |
import java.util.Objects;
|
46640
|
34 |
import java.util.function.Consumer;
|
|
35 |
|
49873
|
36 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
|
37 |
import jdk.internal.vm.compiler.collections.EconomicSet;
|
|
38 |
import jdk.internal.vm.compiler.collections.Equivalence;
|
|
39 |
import jdk.internal.vm.compiler.collections.UnmodifiableEconomicMap;
|
|
40 |
import jdk.internal.vm.compiler.collections.UnmodifiableMapCursor;
|
43972
|
41 |
import org.graalvm.compiler.api.replacements.MethodSubstitution;
|
49451
|
42 |
import org.graalvm.compiler.api.replacements.Snippet;
|
46344
|
43 |
import org.graalvm.compiler.core.common.GraalOptions;
|
43972
|
44 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
45 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
46 |
import org.graalvm.compiler.core.common.type.TypeReference;
|
|
47 |
import org.graalvm.compiler.core.common.util.Util;
|
49873
|
48 |
import org.graalvm.compiler.debug.DebugCloseable;
|
46640
|
49 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
50 |
import org.graalvm.compiler.debug.GraalError;
|
|
51 |
import org.graalvm.compiler.graph.GraalGraphError;
|
|
52 |
import org.graalvm.compiler.graph.Graph.DuplicationReplacement;
|
49451
|
53 |
import org.graalvm.compiler.graph.Graph.Mark;
|
46344
|
54 |
import org.graalvm.compiler.graph.Graph.NodeEventScope;
|
43972
|
55 |
import org.graalvm.compiler.graph.Node;
|
|
56 |
import org.graalvm.compiler.graph.NodeInputList;
|
46509
|
57 |
import org.graalvm.compiler.graph.NodeMap;
|
43972
|
58 |
import org.graalvm.compiler.graph.NodeSourcePosition;
|
|
59 |
import org.graalvm.compiler.graph.NodeWorkList;
|
|
60 |
import org.graalvm.compiler.nodeinfo.Verbosity;
|
|
61 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
62 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
63 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
64 |
import org.graalvm.compiler.nodes.BeginNode;
|
|
65 |
import org.graalvm.compiler.nodes.CallTargetNode;
|
|
66 |
import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
|
|
67 |
import org.graalvm.compiler.nodes.DeoptimizeNode;
|
46509
|
68 |
import org.graalvm.compiler.nodes.EndNode;
|
43972
|
69 |
import org.graalvm.compiler.nodes.FixedGuardNode;
|
|
70 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
71 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
|
72 |
import org.graalvm.compiler.nodes.FrameState;
|
49451
|
73 |
import org.graalvm.compiler.nodes.InliningLog;
|
43972
|
74 |
import org.graalvm.compiler.nodes.Invoke;
|
|
75 |
import org.graalvm.compiler.nodes.InvokeNode;
|
|
76 |
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
|
|
77 |
import org.graalvm.compiler.nodes.KillingBeginNode;
|
|
78 |
import org.graalvm.compiler.nodes.LogicNode;
|
|
79 |
import org.graalvm.compiler.nodes.MergeNode;
|
48190
|
80 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
81 |
import org.graalvm.compiler.nodes.ParameterNode;
|
46509
|
82 |
import org.graalvm.compiler.nodes.PhiNode;
|
43972
|
83 |
import org.graalvm.compiler.nodes.PiNode;
|
|
84 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
85 |
import org.graalvm.compiler.nodes.StartNode;
|
|
86 |
import org.graalvm.compiler.nodes.StateSplit;
|
|
87 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
49451
|
88 |
import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
|
43972
|
89 |
import org.graalvm.compiler.nodes.UnwindNode;
|
|
90 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
91 |
import org.graalvm.compiler.nodes.calc.IsNullNode;
|
|
92 |
import org.graalvm.compiler.nodes.extended.ForeignCallNode;
|
|
93 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
|
94 |
import org.graalvm.compiler.nodes.java.ExceptionObjectNode;
|
|
95 |
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
|
|
96 |
import org.graalvm.compiler.nodes.java.MonitorExitNode;
|
|
97 |
import org.graalvm.compiler.nodes.java.MonitorIdNode;
|
|
98 |
import org.graalvm.compiler.nodes.spi.Replacements;
|
|
99 |
import org.graalvm.compiler.nodes.type.StampTool;
|
|
100 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
|
101 |
import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
|
46344
|
102 |
import org.graalvm.compiler.phases.common.util.HashSetNodeEventListener;
|
46393
|
103 |
import org.graalvm.compiler.phases.util.ValueMergeUtil;
|
43972
|
104 |
|
46640
|
105 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
106 |
import jdk.vm.ci.meta.Assumptions;
|
|
107 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
108 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
|
109 |
import jdk.vm.ci.meta.JavaKind;
|
|
110 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
111 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
43972
|
112 |
|
46393
|
113 |
public class InliningUtil extends ValueMergeUtil {
|
43972
|
114 |
|
|
115 |
private static final String inliningDecisionsScopeString = "InliningDecisions";
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Print a HotSpot-style inlining message to the console.
|
|
119 |
*/
|
|
120 |
private static void printInlining(final InlineInfo info, final int inliningDepth, final boolean success, final String msg, final Object... args) {
|
|
121 |
printInlining(info.methodAt(0), info.invoke(), inliningDepth, success, msg, args);
|
|
122 |
}
|
|
123 |
|
49451
|
124 |
/**
|
|
125 |
* @see #printInlining
|
|
126 |
*/
|
43972
|
127 |
private static void printInlining(final ResolvedJavaMethod method, final Invoke invoke, final int inliningDepth, final boolean success, final String msg, final Object... args) {
|
46344
|
128 |
if (HotSpotPrintInlining.getValue(invoke.asNode().getOptions())) {
|
|
129 |
Util.printInlining(method, invoke.bci(), inliningDepth, success, msg, args);
|
|
130 |
}
|
43972
|
131 |
}
|
|
132 |
|
49451
|
133 |
/**
|
|
134 |
* Trace a decision to inline a method.
|
|
135 |
*
|
|
136 |
* This prints a HotSpot-style inlining message to the console, and it also logs the decision to
|
|
137 |
* the logging stream.
|
|
138 |
*
|
|
139 |
* Phases that perform inlining should use this method to trace the inlining decisions, and use
|
|
140 |
* the {@link #traceNotInlinedMethod} methods only for debugging purposes.
|
|
141 |
*/
|
|
142 |
public static void traceInlinedMethod(InlineInfo info, int inliningDepth, boolean allowLogging, String msg, Object... args) {
|
|
143 |
traceMethod(info, inliningDepth, allowLogging, true, msg, args);
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Trace a decision to inline a method.
|
|
148 |
*
|
|
149 |
* This prints a HotSpot-style inlining message to the console, and it also logs the decision to
|
|
150 |
* the logging stream.
|
|
151 |
*
|
|
152 |
* Phases that perform inlining should use this method to trace the inlining decisions, and use
|
|
153 |
* the {@link #traceNotInlinedMethod} methods only for debugging purposes.
|
|
154 |
*/
|
|
155 |
public static void traceInlinedMethod(Invoke invoke, int inliningDepth, boolean allowLogging, ResolvedJavaMethod method, String msg, Object... args) {
|
|
156 |
traceMethod(invoke, inliningDepth, allowLogging, true, method, msg, args);
|
43972
|
157 |
}
|
|
158 |
|
49451
|
159 |
/**
|
|
160 |
* Trace a decision to not inline a method.
|
|
161 |
*
|
|
162 |
* This prints a HotSpot-style inlining message to the console, and it also logs the decision to
|
|
163 |
* the logging stream.
|
|
164 |
*
|
|
165 |
* Phases that perform inlining should use this method to trace the inlining decisions, and use
|
|
166 |
* the {@link #traceNotInlinedMethod} methods only for debugging purposes.
|
|
167 |
*/
|
|
168 |
public static void traceNotInlinedMethod(InlineInfo info, int inliningDepth, String msg, Object... args) {
|
|
169 |
traceMethod(info, inliningDepth, true, false, msg, args);
|
43972
|
170 |
}
|
|
171 |
|
49451
|
172 |
/**
|
|
173 |
* Trace a decision about not inlining a method.
|
|
174 |
*
|
|
175 |
* This prints a HotSpot-style inlining message to the console, and it also logs the decision to
|
|
176 |
* the logging stream.
|
|
177 |
*
|
|
178 |
* Phases that perform inlining should use this method to trace the inlining decisions, and use
|
|
179 |
* the {@link #traceNotInlinedMethod} methods only for debugging purposes.
|
|
180 |
*/
|
|
181 |
public static void traceNotInlinedMethod(Invoke invoke, int inliningDepth, ResolvedJavaMethod method, String msg, Object... args) {
|
|
182 |
traceMethod(invoke, inliningDepth, true, false, method, msg, args);
|
|
183 |
}
|
|
184 |
|
|
185 |
private static void traceMethod(Invoke invoke, int inliningDepth, boolean allowLogging, boolean success, ResolvedJavaMethod method, String msg, Object... args) {
|
43972
|
186 |
if (allowLogging) {
|
49451
|
187 |
DebugContext debug = invoke.asNode().getDebug();
|
|
188 |
printInlining(method, invoke, inliningDepth, success, msg, args);
|
|
189 |
if (shouldLogMethod(debug)) {
|
|
190 |
String methodString = methodName(method, invoke);
|
|
191 |
logMethod(debug, methodString, success, msg, args);
|
43972
|
192 |
}
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
49451
|
196 |
private static void traceMethod(InlineInfo info, int inliningDepth, boolean allowLogging, boolean success, String msg, final Object... args) {
|
|
197 |
if (allowLogging) {
|
|
198 |
printInlining(info, inliningDepth, success, msg, args);
|
|
199 |
DebugContext debug = info.graph().getDebug();
|
|
200 |
if (shouldLogMethod(debug)) {
|
|
201 |
logMethod(debug, methodName(info), success, msg, args);
|
43972
|
202 |
}
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
49451
|
206 |
/**
|
|
207 |
* Output a generic inlining decision to the logging stream (e.g. inlining termination
|
|
208 |
* condition).
|
|
209 |
*
|
|
210 |
* Used for debugging purposes.
|
|
211 |
*/
|
|
212 |
public static void logInliningDecision(DebugContext debug, final String msg, final Object... args) {
|
|
213 |
logInlining(debug, msg, args);
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* Output a decision about not inlining a method to the logging stream, for debugging purposes.
|
|
218 |
*/
|
43972
|
219 |
public static void logNotInlinedMethod(Invoke invoke, String msg) {
|
46640
|
220 |
DebugContext debug = invoke.asNode().getDebug();
|
49451
|
221 |
if (shouldLogMethod(debug)) {
|
43972
|
222 |
String methodString = invoke.toString();
|
|
223 |
if (invoke.callTarget() == null) {
|
|
224 |
methodString += " callTarget=null";
|
|
225 |
} else {
|
|
226 |
String targetName = invoke.callTarget().targetName();
|
|
227 |
if (!methodString.endsWith(targetName)) {
|
|
228 |
methodString += " " + targetName;
|
|
229 |
}
|
|
230 |
}
|
49451
|
231 |
logMethod(debug, methodString, false, msg, new Object[0]);
|
43972
|
232 |
}
|
|
233 |
}
|
|
234 |
|
49451
|
235 |
private static void logMethod(DebugContext debug, final String methodString, final boolean success, final String msg, final Object... args) {
|
43972
|
236 |
String inliningMsg = "inlining " + methodString + ": " + msg;
|
|
237 |
if (!success) {
|
|
238 |
inliningMsg = "not " + inliningMsg;
|
|
239 |
}
|
49451
|
240 |
logInlining(debug, inliningMsg, args);
|
43972
|
241 |
}
|
|
242 |
|
|
243 |
@SuppressWarnings("try")
|
49451
|
244 |
private static void logInlining(DebugContext debug, final String msg, final Object... args) {
|
|
245 |
try (DebugContext.Scope s = debug.scope(inliningDecisionsScopeString)) {
|
|
246 |
// Can't use log here since we are varargs
|
|
247 |
if (debug.isLogEnabled()) {
|
|
248 |
debug.logv(msg, args);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
@SuppressWarnings("try")
|
|
254 |
private static boolean shouldLogMethod(DebugContext debug) {
|
46640
|
255 |
try (DebugContext.Scope s = debug.scope(inliningDecisionsScopeString)) {
|
|
256 |
return debug.isLogEnabled();
|
43972
|
257 |
}
|
|
258 |
}
|
|
259 |
|
|
260 |
private static String methodName(ResolvedJavaMethod method, Invoke invoke) {
|
|
261 |
if (invoke != null && invoke.stateAfter() != null) {
|
|
262 |
return methodName(invoke.stateAfter(), invoke.bci()) + ": " + method.format("%H.%n(%p):%r") + " (" + method.getCodeSize() + " bytes)";
|
|
263 |
} else {
|
|
264 |
return method.format("%H.%n(%p):%r") + " (" + method.getCodeSize() + " bytes)";
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
private static String methodName(InlineInfo info) {
|
|
269 |
if (info == null) {
|
|
270 |
return "null";
|
|
271 |
} else if (info.invoke() != null && info.invoke().stateAfter() != null) {
|
|
272 |
return methodName(info.invoke().stateAfter(), info.invoke().bci()) + ": " + info.toString();
|
|
273 |
} else {
|
|
274 |
return info.toString();
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
private static String methodName(FrameState frameState, int bci) {
|
|
279 |
StringBuilder sb = new StringBuilder();
|
|
280 |
if (frameState.outerFrameState() != null) {
|
|
281 |
sb.append(methodName(frameState.outerFrameState(), frameState.outerFrameState().bci));
|
|
282 |
sb.append("->");
|
|
283 |
}
|
46459
|
284 |
ResolvedJavaMethod method = frameState.getMethod();
|
|
285 |
sb.append(method != null ? method.format("%h.%n") : "?");
|
43972
|
286 |
sb.append("@").append(bci);
|
|
287 |
return sb.toString();
|
|
288 |
}
|
|
289 |
|
|
290 |
public static void replaceInvokeCallTarget(Invoke invoke, StructuredGraph graph, InvokeKind invokeKind, ResolvedJavaMethod targetMethod) {
|
|
291 |
MethodCallTargetNode oldCallTarget = (MethodCallTargetNode) invoke.callTarget();
|
|
292 |
MethodCallTargetNode newCallTarget = graph.add(new MethodCallTargetNode(invokeKind, targetMethod, oldCallTarget.arguments().toArray(new ValueNode[0]), oldCallTarget.returnStamp(),
|
|
293 |
oldCallTarget.getProfile()));
|
|
294 |
invoke.asNode().replaceFirstInput(oldCallTarget, newCallTarget);
|
|
295 |
}
|
|
296 |
|
46344
|
297 |
public static PiNode createAnchoredReceiver(StructuredGraph graph, GuardingNode anchor, ResolvedJavaType commonType, ValueNode receiver, boolean exact) {
|
43972
|
298 |
return createAnchoredReceiver(graph, anchor, receiver,
|
|
299 |
exact ? StampFactory.objectNonNull(TypeReference.createExactTrusted(commonType)) : StampFactory.objectNonNull(TypeReference.createTrusted(graph.getAssumptions(), commonType)));
|
|
300 |
}
|
|
301 |
|
46344
|
302 |
private static PiNode createAnchoredReceiver(StructuredGraph graph, GuardingNode anchor, ValueNode receiver, Stamp stamp) {
|
43972
|
303 |
// to avoid that floating reads on receiver fields float above the type check
|
46344
|
304 |
return graph.unique(new PiNode(receiver, stamp, (ValueNode) anchor));
|
43972
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* @return null iff the check succeeds, otherwise a (non-null) descriptive message.
|
|
309 |
*/
|
|
310 |
public static String checkInvokeConditions(Invoke invoke) {
|
|
311 |
if (invoke.predecessor() == null || !invoke.asNode().isAlive()) {
|
|
312 |
return "the invoke is dead code";
|
|
313 |
}
|
|
314 |
if (!(invoke.callTarget() instanceof MethodCallTargetNode)) {
|
|
315 |
return "the invoke has already been lowered, or has been created as a low-level node";
|
|
316 |
}
|
|
317 |
MethodCallTargetNode callTarget = (MethodCallTargetNode) invoke.callTarget();
|
|
318 |
if (callTarget.targetMethod() == null) {
|
|
319 |
return "target method is null";
|
|
320 |
}
|
|
321 |
assert invoke.stateAfter() != null : invoke;
|
|
322 |
if (!invoke.useForInlining()) {
|
|
323 |
return "the invoke is marked to be not used for inlining";
|
|
324 |
}
|
|
325 |
ValueNode receiver = callTarget.receiver();
|
|
326 |
if (receiver != null && receiver.isConstant() && receiver.isNullConstant()) {
|
|
327 |
return "receiver is null";
|
|
328 |
}
|
|
329 |
return null;
|
|
330 |
}
|
|
331 |
|
|
332 |
/**
|
49451
|
333 |
* Performs an actual inlining, thereby replacing the given invoke with the given
|
|
334 |
* {@code inlineGraph}.
|
43972
|
335 |
*
|
|
336 |
* @param invoke the invoke that will be replaced
|
|
337 |
* @param inlineGraph the graph that the invoke will be replaced with
|
|
338 |
* @param receiverNullCheck true if a null check needs to be generated for non-static inlinings,
|
|
339 |
* false if no such check is required
|
|
340 |
* @param inlineeMethod the actual method being inlined. Maybe be null for snippets.
|
|
341 |
*/
|
|
342 |
@SuppressWarnings("try")
|
46344
|
343 |
public static UnmodifiableEconomicMap<Node, Node> inline(Invoke invoke, StructuredGraph inlineGraph, boolean receiverNullCheck, ResolvedJavaMethod inlineeMethod) {
|
49873
|
344 |
try {
|
|
345 |
return inline(invoke, inlineGraph, receiverNullCheck, inlineeMethod, "reason not specified", "phase not specified");
|
|
346 |
} catch (GraalError ex) {
|
|
347 |
ex.addContext("inlining into", invoke.asNode().graph().method());
|
|
348 |
ex.addContext("inlinee", inlineGraph.method());
|
|
349 |
throw ex;
|
|
350 |
}
|
49451
|
351 |
}
|
|
352 |
|
|
353 |
/**
|
|
354 |
* Performs an actual inlining, thereby replacing the given invoke with the given
|
|
355 |
* {@code inlineGraph}.
|
|
356 |
*
|
|
357 |
* @param invoke the invoke that will be replaced
|
|
358 |
* @param inlineGraph the graph that the invoke will be replaced with
|
|
359 |
* @param receiverNullCheck true if a null check needs to be generated for non-static inlinings,
|
|
360 |
* false if no such check is required
|
|
361 |
* @param inlineeMethod the actual method being inlined. Maybe be null for snippets.
|
|
362 |
* @param reason the reason for inlining, used in tracing
|
|
363 |
* @param phase the phase that invoked inlining
|
|
364 |
*/
|
|
365 |
@SuppressWarnings("try")
|
|
366 |
public static UnmodifiableEconomicMap<Node, Node> inline(Invoke invoke, StructuredGraph inlineGraph, boolean receiverNullCheck, ResolvedJavaMethod inlineeMethod, String reason, String phase) {
|
46344
|
367 |
FixedNode invokeNode = invoke.asNode();
|
|
368 |
StructuredGraph graph = invokeNode.graph();
|
46640
|
369 |
final NodeInputList<ValueNode> parameters = invoke.callTarget().arguments();
|
43972
|
370 |
|
46640
|
371 |
assert inlineGraph.getGuardsStage().ordinal() >= graph.getGuardsStage().ordinal();
|
|
372 |
assert !invokeNode.graph().isAfterFloatingReadPhase() : "inline isn't handled correctly after floating reads phase";
|
43972
|
373 |
|
46640
|
374 |
if (receiverNullCheck && !((MethodCallTargetNode) invoke.callTarget()).isStatic()) {
|
|
375 |
nonNullReceiver(invoke);
|
|
376 |
}
|
43972
|
377 |
|
46640
|
378 |
ArrayList<Node> nodes = new ArrayList<>(inlineGraph.getNodes().count());
|
|
379 |
ArrayList<ReturnNode> returnNodes = new ArrayList<>(4);
|
|
380 |
ArrayList<Invoke> partialIntrinsicExits = new ArrayList<>();
|
|
381 |
UnwindNode unwindNode = null;
|
|
382 |
final StartNode entryPointNode = inlineGraph.start();
|
|
383 |
FixedNode firstCFGNode = entryPointNode.next();
|
|
384 |
if (firstCFGNode == null) {
|
|
385 |
throw new IllegalStateException("Inlined graph is in invalid state: " + inlineGraph);
|
|
386 |
}
|
|
387 |
for (Node node : inlineGraph.getNodes()) {
|
|
388 |
if (node == entryPointNode || (node == entryPointNode.stateAfter() && node.usages().count() == 1) || node instanceof ParameterNode) {
|
|
389 |
// Do nothing.
|
|
390 |
} else {
|
|
391 |
nodes.add(node);
|
|
392 |
if (node instanceof ReturnNode) {
|
|
393 |
returnNodes.add((ReturnNode) node);
|
|
394 |
} else if (node instanceof Invoke) {
|
|
395 |
Invoke invokeInInlineGraph = (Invoke) node;
|
|
396 |
if (invokeInInlineGraph.bci() == BytecodeFrame.UNKNOWN_BCI) {
|
|
397 |
ResolvedJavaMethod target1 = inlineeMethod;
|
|
398 |
ResolvedJavaMethod target2 = invokeInInlineGraph.callTarget().targetMethod();
|
|
399 |
assert target1.equals(target2) : String.format("invoke in inlined method expected to be partial intrinsic exit (i.e., call to %s), not a call to %s",
|
|
400 |
target1.format("%H.%n(%p)"), target2.format("%H.%n(%p)"));
|
|
401 |
partialIntrinsicExits.add(invokeInInlineGraph);
|
43972
|
402 |
}
|
46640
|
403 |
} else if (node instanceof UnwindNode) {
|
|
404 |
assert unwindNode == null;
|
|
405 |
unwindNode = (UnwindNode) node;
|
43972
|
406 |
}
|
|
407 |
}
|
46640
|
408 |
}
|
43972
|
409 |
|
46640
|
410 |
final AbstractBeginNode prevBegin = AbstractBeginNode.prevBegin(invokeNode);
|
|
411 |
DuplicationReplacement localReplacement = new DuplicationReplacement() {
|
43972
|
412 |
|
46640
|
413 |
@Override
|
|
414 |
public Node replacement(Node node) {
|
|
415 |
if (node instanceof ParameterNode) {
|
|
416 |
return parameters.get(((ParameterNode) node).index());
|
|
417 |
} else if (node == entryPointNode) {
|
|
418 |
return prevBegin;
|
43972
|
419 |
}
|
46640
|
420 |
return node;
|
|
421 |
}
|
|
422 |
};
|
|
423 |
|
|
424 |
assert invokeNode.successors().first() != null : invoke;
|
|
425 |
assert invokeNode.predecessor() != null;
|
|
426 |
|
49451
|
427 |
Mark mark = graph.getMark();
|
|
428 |
// Instead, attach the inlining log of the child graph to the current inlining log.
|
|
429 |
EconomicMap<Node, Node> duplicates;
|
|
430 |
try (InliningLog.UpdateScope scope = graph.getInliningLog().openDefaultUpdateScope()) {
|
|
431 |
duplicates = graph.addDuplicates(nodes, inlineGraph, inlineGraph.getNodeCount(), localReplacement);
|
|
432 |
if (scope != null) {
|
49873
|
433 |
graph.getInliningLog().addDecision(invoke, true, phase, duplicates, inlineGraph.getInliningLog(), reason);
|
49451
|
434 |
}
|
|
435 |
}
|
43972
|
436 |
|
46640
|
437 |
FrameState stateAfter = invoke.stateAfter();
|
|
438 |
assert stateAfter == null || stateAfter.isAlive();
|
43972
|
439 |
|
46640
|
440 |
FrameState stateAtExceptionEdge = null;
|
|
441 |
if (invoke instanceof InvokeWithExceptionNode) {
|
|
442 |
InvokeWithExceptionNode invokeWithException = ((InvokeWithExceptionNode) invoke);
|
|
443 |
if (unwindNode != null) {
|
|
444 |
ExceptionObjectNode obj = (ExceptionObjectNode) invokeWithException.exceptionEdge();
|
|
445 |
stateAtExceptionEdge = obj.stateAfter();
|
|
446 |
}
|
|
447 |
}
|
43972
|
448 |
|
49451
|
449 |
updateSourcePositions(invoke, inlineGraph, duplicates, !Objects.equals(inlineGraph.method(), inlineeMethod), mark);
|
46640
|
450 |
if (stateAfter != null) {
|
|
451 |
processFrameStates(invoke, inlineGraph, duplicates, stateAtExceptionEdge, returnNodes.size() > 1);
|
|
452 |
int callerLockDepth = stateAfter.nestedLockDepth();
|
|
453 |
if (callerLockDepth != 0) {
|
|
454 |
for (MonitorIdNode original : inlineGraph.getNodes(MonitorIdNode.TYPE)) {
|
|
455 |
MonitorIdNode monitor = (MonitorIdNode) duplicates.get(original);
|
|
456 |
processMonitorId(invoke.stateAfter(), monitor);
|
43972
|
457 |
}
|
|
458 |
}
|
46640
|
459 |
} else {
|
|
460 |
assert checkContainsOnlyInvalidOrAfterFrameState(duplicates);
|
|
461 |
}
|
43972
|
462 |
|
46640
|
463 |
firstCFGNode = (FixedNode) duplicates.get(firstCFGNode);
|
|
464 |
for (int i = 0; i < returnNodes.size(); i++) {
|
|
465 |
returnNodes.set(i, (ReturnNode) duplicates.get(returnNodes.get(i)));
|
|
466 |
}
|
|
467 |
for (Invoke exit : partialIntrinsicExits) {
|
|
468 |
// A partial intrinsic exit must be replaced with a call to
|
|
469 |
// the intrinsified method.
|
|
470 |
Invoke dup = (Invoke) duplicates.get(exit.asNode());
|
|
471 |
if (dup instanceof InvokeNode) {
|
|
472 |
InvokeNode repl = graph.add(new InvokeNode(invoke.callTarget(), invoke.bci()));
|
|
473 |
dup.intrinsify(repl.asNode());
|
43972
|
474 |
} else {
|
46640
|
475 |
((InvokeWithExceptionNode) dup).replaceWithNewBci(invoke.bci());
|
43972
|
476 |
}
|
46640
|
477 |
}
|
|
478 |
if (unwindNode != null) {
|
|
479 |
unwindNode = (UnwindNode) duplicates.get(unwindNode);
|
|
480 |
}
|
43972
|
481 |
|
46640
|
482 |
finishInlining(invoke, graph, firstCFGNode, returnNodes, unwindNode, inlineGraph.getAssumptions(), inlineGraph);
|
|
483 |
GraphUtil.killCFG(invokeNode);
|
43972
|
484 |
|
46640
|
485 |
return duplicates;
|
43972
|
486 |
}
|
|
487 |
|
46344
|
488 |
/**
|
46509
|
489 |
* Inline {@code inlineGraph} into the current replacing the node {@code Invoke} and return the
|
46344
|
490 |
* set of nodes which should be canonicalized. The set should only contain nodes which modified
|
|
491 |
* by the inlining since the current graph and {@code inlineGraph} are expected to already be
|
|
492 |
* canonical.
|
|
493 |
*
|
|
494 |
* @param invoke
|
|
495 |
* @param inlineGraph
|
|
496 |
* @param receiverNullCheck
|
|
497 |
* @param inlineeMethod
|
|
498 |
* @return the set of nodes to canonicalize
|
|
499 |
*/
|
|
500 |
@SuppressWarnings("try")
|
49873
|
501 |
public static EconomicSet<Node> inlineForCanonicalization(Invoke invoke, StructuredGraph inlineGraph, boolean receiverNullCheck, ResolvedJavaMethod inlineeMethod, String reason, String phase) {
|
|
502 |
return inlineForCanonicalization(invoke, inlineGraph, receiverNullCheck, inlineeMethod, null, reason, phase);
|
49451
|
503 |
}
|
|
504 |
|
46536
|
505 |
@SuppressWarnings("try")
|
|
506 |
public static EconomicSet<Node> inlineForCanonicalization(Invoke invoke, StructuredGraph inlineGraph, boolean receiverNullCheck, ResolvedJavaMethod inlineeMethod,
|
49451
|
507 |
Consumer<UnmodifiableEconomicMap<Node, Node>> duplicatesConsumer, String reason, String phase) {
|
46344
|
508 |
HashSetNodeEventListener listener = new HashSetNodeEventListener();
|
|
509 |
/*
|
|
510 |
* This code relies on the fact that Graph.addDuplicates doesn't trigger the
|
|
511 |
* NodeEventListener to track only nodes which were modified into the process of inlining
|
|
512 |
* the graph into the current graph.
|
|
513 |
*/
|
|
514 |
try (NodeEventScope nes = invoke.asNode().graph().trackNodeEvents(listener)) {
|
49451
|
515 |
UnmodifiableEconomicMap<Node, Node> duplicates = InliningUtil.inline(invoke, inlineGraph, receiverNullCheck, inlineeMethod, reason, phase);
|
46536
|
516 |
if (duplicatesConsumer != null) {
|
|
517 |
duplicatesConsumer.accept(duplicates);
|
|
518 |
}
|
46344
|
519 |
}
|
|
520 |
return listener.getNodes();
|
|
521 |
}
|
|
522 |
|
|
523 |
private static ValueNode finishInlining(Invoke invoke, StructuredGraph graph, FixedNode firstNode, List<ReturnNode> returnNodes, UnwindNode unwindNode, Assumptions inlinedAssumptions,
|
|
524 |
StructuredGraph inlineGraph) {
|
43972
|
525 |
FixedNode invokeNode = invoke.asNode();
|
|
526 |
FrameState stateAfter = invoke.stateAfter();
|
|
527 |
assert stateAfter == null || stateAfter.isAlive();
|
|
528 |
|
|
529 |
invokeNode.replaceAtPredecessor(firstNode);
|
|
530 |
|
|
531 |
if (invoke instanceof InvokeWithExceptionNode) {
|
|
532 |
InvokeWithExceptionNode invokeWithException = ((InvokeWithExceptionNode) invoke);
|
|
533 |
if (unwindNode != null && unwindNode.isAlive()) {
|
|
534 |
assert unwindNode.predecessor() != null;
|
|
535 |
assert invokeWithException.exceptionEdge().successors().count() == 1;
|
|
536 |
ExceptionObjectNode obj = (ExceptionObjectNode) invokeWithException.exceptionEdge();
|
|
537 |
obj.replaceAtUsages(unwindNode.exception());
|
|
538 |
Node n = obj.next();
|
|
539 |
obj.setNext(null);
|
|
540 |
unwindNode.replaceAndDelete(n);
|
|
541 |
|
|
542 |
obj.replaceAtPredecessor(null);
|
|
543 |
obj.safeDelete();
|
|
544 |
} else {
|
|
545 |
invokeWithException.killExceptionEdge();
|
|
546 |
}
|
|
547 |
|
|
548 |
// get rid of memory kill
|
|
549 |
AbstractBeginNode begin = invokeWithException.next();
|
|
550 |
if (begin instanceof KillingBeginNode) {
|
|
551 |
AbstractBeginNode newBegin = new BeginNode();
|
|
552 |
graph.addAfterFixed(begin, graph.add(newBegin));
|
|
553 |
begin.replaceAtUsages(newBegin);
|
|
554 |
graph.removeFixed(begin);
|
|
555 |
}
|
|
556 |
} else {
|
|
557 |
if (unwindNode != null && unwindNode.isAlive()) {
|
49451
|
558 |
DeoptimizeNode deoptimizeNode = addDeoptimizeNode(graph, DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.NotCompiledExceptionHandler);
|
43972
|
559 |
unwindNode.replaceAndDelete(deoptimizeNode);
|
|
560 |
}
|
|
561 |
}
|
|
562 |
|
|
563 |
ValueNode returnValue;
|
|
564 |
if (!returnNodes.isEmpty()) {
|
|
565 |
FixedNode n = invoke.next();
|
|
566 |
invoke.setNext(null);
|
|
567 |
if (returnNodes.size() == 1) {
|
|
568 |
ReturnNode returnNode = returnNodes.get(0);
|
|
569 |
returnValue = returnNode.result();
|
|
570 |
invokeNode.replaceAtUsages(returnValue);
|
|
571 |
returnNode.replaceAndDelete(n);
|
|
572 |
} else {
|
46509
|
573 |
MergeNode merge = graph.add(new MergeNode());
|
43972
|
574 |
merge.setStateAfter(stateAfter);
|
46344
|
575 |
returnValue = mergeReturns(merge, returnNodes);
|
43972
|
576 |
invokeNode.replaceAtUsages(returnValue);
|
46509
|
577 |
if (merge.isPhiAtMerge(returnValue)) {
|
|
578 |
fixFrameStates(graph, merge, (PhiNode) returnValue);
|
|
579 |
}
|
43972
|
580 |
merge.setNext(n);
|
|
581 |
}
|
|
582 |
} else {
|
|
583 |
returnValue = null;
|
|
584 |
invokeNode.replaceAtUsages(null);
|
|
585 |
GraphUtil.killCFG(invoke.next());
|
|
586 |
}
|
|
587 |
|
|
588 |
// Copy assumptions from inlinee to caller
|
|
589 |
Assumptions assumptions = graph.getAssumptions();
|
|
590 |
if (assumptions != null) {
|
|
591 |
if (inlinedAssumptions != null) {
|
|
592 |
assumptions.record(inlinedAssumptions);
|
|
593 |
}
|
|
594 |
} else {
|
46344
|
595 |
assert inlinedAssumptions == null : String.format("cannot inline graph (%s) which makes assumptions into a graph (%s) that doesn't", inlineGraph, graph);
|
43972
|
596 |
}
|
|
597 |
|
|
598 |
// Copy inlined methods from inlinee to caller
|
|
599 |
graph.updateMethods(inlineGraph);
|
|
600 |
|
|
601 |
// Update the set of accessed fields
|
46344
|
602 |
if (GraalOptions.GeneratePIC.getValue(graph.getOptions())) {
|
|
603 |
graph.updateFields(inlineGraph);
|
|
604 |
}
|
43972
|
605 |
|
|
606 |
if (inlineGraph.hasUnsafeAccess()) {
|
|
607 |
graph.markUnsafeAccess();
|
|
608 |
}
|
46566
|
609 |
assert inlineGraph.getSpeculationLog() == null || inlineGraph.getSpeculationLog() == graph.getSpeculationLog() : "Only the root graph should have a speculation log";
|
43972
|
610 |
|
|
611 |
return returnValue;
|
|
612 |
}
|
|
613 |
|
46509
|
614 |
private static void fixFrameStates(StructuredGraph graph, MergeNode originalMerge, PhiNode returnPhi) {
|
|
615 |
// It is possible that some of the frame states that came from AFTER_BCI reference a Phi
|
|
616 |
// node that was created to merge multiple returns. This can create cycles
|
|
617 |
// (see GR-3949 and GR-3957).
|
|
618 |
// To detect this, we follow the control paths starting from the merge node,
|
|
619 |
// split the Phi node inputs at merges and assign the proper input to each frame state.
|
|
620 |
NodeMap<Node> seen = new NodeMap<>(graph);
|
|
621 |
ArrayDeque<Node> workList = new ArrayDeque<>();
|
|
622 |
ArrayDeque<ValueNode> valueList = new ArrayDeque<>();
|
|
623 |
workList.push(originalMerge);
|
|
624 |
valueList.push(returnPhi);
|
|
625 |
while (!workList.isEmpty()) {
|
|
626 |
Node current = workList.pop();
|
|
627 |
ValueNode currentValue = valueList.pop();
|
|
628 |
if (seen.containsKey(current)) {
|
|
629 |
continue;
|
|
630 |
}
|
|
631 |
seen.put(current, current);
|
|
632 |
if (current instanceof StateSplit && current != originalMerge) {
|
|
633 |
StateSplit stateSplit = (StateSplit) current;
|
|
634 |
FrameState state = stateSplit.stateAfter();
|
|
635 |
if (state != null && state.values().contains(returnPhi)) {
|
|
636 |
int index = 0;
|
|
637 |
FrameState duplicate = state.duplicate();
|
|
638 |
for (ValueNode value : state.values()) {
|
|
639 |
if (value == returnPhi) {
|
|
640 |
duplicate.values().set(index, currentValue);
|
|
641 |
}
|
|
642 |
index++;
|
|
643 |
}
|
|
644 |
stateSplit.setStateAfter(duplicate);
|
|
645 |
GraphUtil.tryKillUnused(state);
|
|
646 |
}
|
|
647 |
}
|
|
648 |
if (current instanceof AbstractMergeNode) {
|
|
649 |
AbstractMergeNode currentMerge = (AbstractMergeNode) current;
|
|
650 |
for (EndNode pred : currentMerge.cfgPredecessors()) {
|
|
651 |
ValueNode newValue = currentValue;
|
|
652 |
if (currentMerge.isPhiAtMerge(currentValue)) {
|
|
653 |
PhiNode currentPhi = (PhiNode) currentValue;
|
|
654 |
newValue = currentPhi.valueAt(pred);
|
|
655 |
}
|
|
656 |
workList.push(pred);
|
|
657 |
valueList.push(newValue);
|
|
658 |
}
|
|
659 |
} else if (current.predecessor() != null) {
|
|
660 |
workList.push(current.predecessor());
|
|
661 |
valueList.push(currentValue);
|
|
662 |
}
|
43972
|
663 |
}
|
|
664 |
}
|
|
665 |
|
|
666 |
@SuppressWarnings("try")
|
49451
|
667 |
private static void updateSourcePositions(Invoke invoke, StructuredGraph inlineGraph, UnmodifiableEconomicMap<Node, Node> duplicates, boolean isSub, Mark mark) {
|
|
668 |
FixedNode invokeNode = invoke.asNode();
|
|
669 |
boolean isSubstitution = isSub || inlineGraph.method().getAnnotation(MethodSubstitution.class) != null || inlineGraph.method().getAnnotation(Snippet.class) != null;
|
|
670 |
StructuredGraph invokeGraph = invokeNode.graph();
|
|
671 |
assert !invokeGraph.trackNodeSourcePosition() || inlineGraph.trackNodeSourcePosition() ||
|
|
672 |
isSubstitution : String.format("trackNodeSourcePosition mismatch %s %s != %s %s", invokeGraph, invokeGraph.trackNodeSourcePosition(), inlineGraph,
|
|
673 |
inlineGraph.trackNodeSourcePosition());
|
|
674 |
if (invokeGraph.trackNodeSourcePosition() && invoke.stateAfter() != null) {
|
|
675 |
final NodeSourcePosition invokePos = invoke.asNode().getNodeSourcePosition();
|
|
676 |
updateSourcePosition(invokeGraph, duplicates, mark, invokePos, isSubstitution);
|
|
677 |
}
|
|
678 |
}
|
43972
|
679 |
|
49451
|
680 |
public static void updateSourcePosition(StructuredGraph invokeGraph, UnmodifiableEconomicMap<Node, Node> duplicates, Mark mark, NodeSourcePosition invokePos, boolean isSubstitution) {
|
|
681 |
/*
|
|
682 |
* Not every duplicate node is newly created, so only update the position of the newly
|
|
683 |
* created nodes.
|
|
684 |
*/
|
|
685 |
EconomicSet<Node> newNodes = EconomicSet.create(Equivalence.DEFAULT);
|
|
686 |
newNodes.addAll(invokeGraph.getNewNodes(mark));
|
|
687 |
EconomicMap<NodeSourcePosition, NodeSourcePosition> posMap = EconomicMap.create(Equivalence.DEFAULT);
|
|
688 |
UnmodifiableMapCursor<Node, Node> cursor = duplicates.getEntries();
|
49873
|
689 |
ResolvedJavaMethod inlineeRoot = null;
|
49451
|
690 |
while (cursor.advance()) {
|
49873
|
691 |
Node value = cursor.getValue();
|
|
692 |
if (!newNodes.contains(value)) {
|
49451
|
693 |
continue;
|
|
694 |
}
|
49873
|
695 |
if (isSubstitution && invokePos == null) {
|
|
696 |
// There's no caller information so the source position for this node will be
|
|
697 |
// invalid, so it should be cleared.
|
|
698 |
value.clearNodeSourcePosition();
|
49451
|
699 |
} else {
|
49873
|
700 |
NodeSourcePosition pos = cursor.getKey().getNodeSourcePosition();
|
|
701 |
if (pos != null) {
|
|
702 |
if (inlineeRoot == null) {
|
|
703 |
assert (inlineeRoot = pos.getRootMethod()) != null;
|
|
704 |
} else {
|
|
705 |
assert pos.verifyRootMethod(inlineeRoot);
|
|
706 |
}
|
|
707 |
NodeSourcePosition callerPos = posMap.get(pos);
|
|
708 |
if (callerPos == null) {
|
|
709 |
callerPos = pos.addCaller(invokePos, isSubstitution);
|
|
710 |
posMap.put(pos, callerPos);
|
|
711 |
}
|
|
712 |
value.setNodeSourcePosition(callerPos);
|
|
713 |
} else {
|
|
714 |
if (isSubstitution) {
|
|
715 |
/*
|
|
716 |
* If no other position is provided at least attribute the substituted node
|
|
717 |
* to the original invoke.
|
|
718 |
*/
|
|
719 |
value.setNodeSourcePosition(invokePos);
|
|
720 |
}
|
43972
|
721 |
}
|
|
722 |
}
|
|
723 |
}
|
49873
|
724 |
assert invokeGraph.verifySourcePositions();
|
43972
|
725 |
}
|
|
726 |
|
|
727 |
public static void processMonitorId(FrameState stateAfter, MonitorIdNode monitorIdNode) {
|
|
728 |
if (stateAfter != null) {
|
|
729 |
int callerLockDepth = stateAfter.nestedLockDepth();
|
|
730 |
monitorIdNode.setLockDepth(monitorIdNode.getLockDepth() + callerLockDepth);
|
|
731 |
}
|
|
732 |
}
|
|
733 |
|
46509
|
734 |
protected static void processFrameStates(Invoke invoke, StructuredGraph inlineGraph, EconomicMap<Node, Node> duplicates, FrameState stateAtExceptionEdge,
|
46344
|
735 |
boolean alwaysDuplicateStateAfter) {
|
43972
|
736 |
FrameState stateAtReturn = invoke.stateAfter();
|
|
737 |
FrameState outerFrameState = null;
|
|
738 |
JavaKind invokeReturnKind = invoke.asNode().getStackKind();
|
46509
|
739 |
EconomicMap<Node, Node> replacements = EconomicMap.create();
|
43972
|
740 |
for (FrameState original : inlineGraph.getNodes(FrameState.TYPE)) {
|
|
741 |
FrameState frameState = (FrameState) duplicates.get(original);
|
|
742 |
if (frameState != null && frameState.isAlive()) {
|
|
743 |
if (outerFrameState == null) {
|
|
744 |
outerFrameState = stateAtReturn.duplicateModifiedDuringCall(invoke.bci(), invokeReturnKind);
|
|
745 |
}
|
46509
|
746 |
processFrameState(frameState, invoke, replacements, inlineGraph.method(), stateAtExceptionEdge, outerFrameState, alwaysDuplicateStateAfter, invoke.callTarget().targetMethod(),
|
43972
|
747 |
invoke.callTarget().arguments());
|
|
748 |
}
|
|
749 |
}
|
46509
|
750 |
// If processing the frame states replaced any nodes, update the duplicates map.
|
|
751 |
duplicates.replaceAll((key, value) -> replacements.containsKey(value) ? replacements.get(value) : value);
|
43972
|
752 |
}
|
|
753 |
|
46509
|
754 |
public static FrameState processFrameState(FrameState frameState, Invoke invoke, EconomicMap<Node, Node> replacements, ResolvedJavaMethod inlinedMethod, FrameState stateAtExceptionEdge,
|
|
755 |
FrameState outerFrameState,
|
43972
|
756 |
boolean alwaysDuplicateStateAfter, ResolvedJavaMethod invokeTargetMethod, List<ValueNode> invokeArgsList) {
|
|
757 |
assert outerFrameState == null || !outerFrameState.isDeleted() : outerFrameState;
|
46509
|
758 |
final FrameState stateAtReturn = invoke.stateAfter();
|
43972
|
759 |
JavaKind invokeReturnKind = invoke.asNode().getStackKind();
|
|
760 |
|
|
761 |
if (frameState.bci == BytecodeFrame.AFTER_BCI) {
|
46509
|
762 |
return handleAfterBciFrameState(frameState, invoke, alwaysDuplicateStateAfter);
|
43972
|
763 |
} else if (stateAtExceptionEdge != null && isStateAfterException(frameState)) {
|
|
764 |
// pop exception object from invoke's stateAfter and replace with this frameState's
|
|
765 |
// exception object (top of stack)
|
|
766 |
FrameState stateAfterException = stateAtExceptionEdge;
|
|
767 |
if (frameState.stackSize() > 0 && stateAtExceptionEdge.stackAt(0) != frameState.stackAt(0)) {
|
|
768 |
stateAfterException = stateAtExceptionEdge.duplicateModified(JavaKind.Object, JavaKind.Object, frameState.stackAt(0));
|
|
769 |
}
|
|
770 |
frameState.replaceAndDelete(stateAfterException);
|
|
771 |
return stateAfterException;
|
49451
|
772 |
} else if ((frameState.bci == BytecodeFrame.UNWIND_BCI && frameState.graph().getGuardsStage() == GuardsStage.FLOATING_GUARDS) || frameState.bci == BytecodeFrame.AFTER_EXCEPTION_BCI) {
|
|
773 |
/*
|
|
774 |
* This path converts the frame states relevant for exception unwinding to
|
|
775 |
* deoptimization. This is only allowed in configurations when Graal compiles code for
|
|
776 |
* speculative execution (e.g., JIT compilation in HotSpot) but not when compiled code
|
|
777 |
* must be deoptimization free (e.g., AOT compilation for native image generation).
|
|
778 |
* There is currently no global flag in StructuredGraph to distinguish such modes, but
|
|
779 |
* the GuardsStage during inlining indicates the mode in which Graal operates.
|
|
780 |
*/
|
46509
|
781 |
handleMissingAfterExceptionFrameState(frameState, invoke, replacements, alwaysDuplicateStateAfter);
|
|
782 |
return frameState;
|
43972
|
783 |
} else if (frameState.bci == BytecodeFrame.BEFORE_BCI) {
|
|
784 |
// This is an intrinsic. Deoptimizing within an intrinsic
|
|
785 |
// must re-execute the intrinsified invocation
|
|
786 |
assert frameState.outerFrameState() == null;
|
|
787 |
ValueNode[] invokeArgs = invokeArgsList.isEmpty() ? NO_ARGS : invokeArgsList.toArray(new ValueNode[invokeArgsList.size()]);
|
|
788 |
FrameState stateBeforeCall = stateAtReturn.duplicateModifiedBeforeCall(invoke.bci(), invokeReturnKind, invokeTargetMethod.getSignature().toParameterKinds(!invokeTargetMethod.isStatic()),
|
|
789 |
invokeArgs);
|
|
790 |
frameState.replaceAndDelete(stateBeforeCall);
|
|
791 |
return stateBeforeCall;
|
|
792 |
} else {
|
|
793 |
// only handle the outermost frame states
|
|
794 |
if (frameState.outerFrameState() == null) {
|
|
795 |
assert checkInlineeFrameState(invoke, inlinedMethod, frameState);
|
|
796 |
frameState.setOuterFrameState(outerFrameState);
|
|
797 |
}
|
|
798 |
return frameState;
|
|
799 |
}
|
|
800 |
}
|
|
801 |
|
46509
|
802 |
private static FrameState handleAfterBciFrameState(FrameState frameState, Invoke invoke, boolean alwaysDuplicateStateAfter) {
|
|
803 |
FrameState stateAtReturn = invoke.stateAfter();
|
|
804 |
JavaKind invokeReturnKind = invoke.asNode().getStackKind();
|
|
805 |
FrameState stateAfterReturn = stateAtReturn;
|
|
806 |
if (frameState.getCode() == null) {
|
|
807 |
// This is a frame state for a side effect within an intrinsic
|
|
808 |
// that was parsed for post-parse intrinsification
|
|
809 |
for (Node usage : frameState.usages()) {
|
|
810 |
if (usage instanceof ForeignCallNode) {
|
|
811 |
// A foreign call inside an intrinsic needs to have
|
|
812 |
// the BCI of the invoke being intrinsified
|
|
813 |
ForeignCallNode foreign = (ForeignCallNode) usage;
|
|
814 |
foreign.setBci(invoke.bci());
|
|
815 |
}
|
|
816 |
}
|
|
817 |
}
|
|
818 |
|
|
819 |
// pop return kind from invoke's stateAfter and replace with this frameState's return
|
|
820 |
// value (top of stack)
|
|
821 |
assert !frameState.rethrowException() : frameState;
|
|
822 |
if (frameState.stackSize() > 0 && (alwaysDuplicateStateAfter || stateAfterReturn.stackAt(0) != frameState.stackAt(0))) {
|
|
823 |
// A non-void return value.
|
|
824 |
stateAfterReturn = stateAtReturn.duplicateModified(invokeReturnKind, invokeReturnKind, frameState.stackAt(0));
|
|
825 |
} else {
|
|
826 |
// A void return value.
|
|
827 |
stateAfterReturn = stateAtReturn.duplicate();
|
|
828 |
}
|
|
829 |
assert stateAfterReturn.bci != BytecodeFrame.UNKNOWN_BCI;
|
|
830 |
|
|
831 |
// Return value does no longer need to be limited by the monitor exit.
|
|
832 |
for (MonitorExitNode n : frameState.usages().filter(MonitorExitNode.class)) {
|
|
833 |
n.clearEscapedReturnValue();
|
|
834 |
}
|
|
835 |
|
|
836 |
frameState.replaceAndDelete(stateAfterReturn);
|
|
837 |
return stateAfterReturn;
|
|
838 |
}
|
|
839 |
|
43972
|
840 |
static boolean checkInlineeFrameState(Invoke invoke, ResolvedJavaMethod inlinedMethod, FrameState frameState) {
|
|
841 |
assert frameState.bci != BytecodeFrame.AFTER_EXCEPTION_BCI : frameState;
|
|
842 |
assert frameState.bci != BytecodeFrame.BEFORE_BCI : frameState;
|
|
843 |
assert frameState.bci != BytecodeFrame.UNKNOWN_BCI : frameState;
|
|
844 |
if (frameState.bci != BytecodeFrame.INVALID_FRAMESTATE_BCI) {
|
|
845 |
ResolvedJavaMethod method = frameState.getMethod();
|
|
846 |
if (method.equals(inlinedMethod)) {
|
|
847 |
// Normal inlining expects all outermost inlinee frame states to
|
|
848 |
// denote the inlinee method
|
|
849 |
} else if (method.equals(invoke.callTarget().targetMethod())) {
|
|
850 |
// This occurs when an intrinsic calls back to the original
|
|
851 |
// method to handle a slow path. During parsing of such a
|
|
852 |
// partial intrinsic, these calls are given frame states
|
|
853 |
// that exclude the outer frame state denoting a position
|
|
854 |
// in the intrinsic code.
|
|
855 |
assert inlinedMethod.getAnnotation(
|
|
856 |
MethodSubstitution.class) != null : "expected an intrinsic when inlinee frame state matches method of call target but does not match the method of the inlinee graph: " +
|
|
857 |
frameState;
|
|
858 |
} else if (method.getName().equals(inlinedMethod.getName())) {
|
|
859 |
// This can happen for method substitutions.
|
|
860 |
} else {
|
|
861 |
throw new AssertionError(String.format("inlinedMethod=%s frameState.method=%s frameState=%s invoke.method=%s", inlinedMethod, method, frameState,
|
|
862 |
invoke.callTarget().targetMethod()));
|
|
863 |
}
|
|
864 |
}
|
|
865 |
return true;
|
|
866 |
}
|
|
867 |
|
|
868 |
private static final ValueNode[] NO_ARGS = {};
|
|
869 |
|
|
870 |
private static boolean isStateAfterException(FrameState frameState) {
|
|
871 |
return frameState.bci == BytecodeFrame.AFTER_EXCEPTION_BCI || (frameState.bci == BytecodeFrame.UNWIND_BCI && !frameState.getMethod().isSynchronized());
|
|
872 |
}
|
|
873 |
|
46509
|
874 |
public static FrameState handleMissingAfterExceptionFrameState(FrameState nonReplaceableFrameState, Invoke invoke, EconomicMap<Node, Node> replacements, boolean alwaysDuplicateStateAfter) {
|
49451
|
875 |
StructuredGraph graph = nonReplaceableFrameState.graph();
|
43972
|
876 |
NodeWorkList workList = graph.createNodeWorkList();
|
|
877 |
workList.add(nonReplaceableFrameState);
|
|
878 |
for (Node node : workList) {
|
|
879 |
FrameState fs = (FrameState) node;
|
|
880 |
for (Node usage : fs.usages().snapshot()) {
|
|
881 |
if (!usage.isAlive()) {
|
|
882 |
continue;
|
|
883 |
}
|
|
884 |
if (usage instanceof FrameState) {
|
|
885 |
workList.add(usage);
|
|
886 |
} else {
|
|
887 |
StateSplit stateSplit = (StateSplit) usage;
|
|
888 |
FixedNode fixedStateSplit = stateSplit.asNode();
|
|
889 |
if (fixedStateSplit instanceof AbstractMergeNode) {
|
|
890 |
AbstractMergeNode merge = (AbstractMergeNode) fixedStateSplit;
|
|
891 |
while (merge.isAlive()) {
|
|
892 |
AbstractEndNode end = merge.forwardEnds().first();
|
49451
|
893 |
DeoptimizeNode deoptimizeNode = addDeoptimizeNode(graph, DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.NotCompiledExceptionHandler);
|
43972
|
894 |
end.replaceAtPredecessor(deoptimizeNode);
|
|
895 |
GraphUtil.killCFG(end);
|
|
896 |
}
|
46509
|
897 |
} else if (fixedStateSplit instanceof ExceptionObjectNode) {
|
|
898 |
// The target invoke does not have an exception edge. This means that the
|
|
899 |
// bytecode parser made the wrong assumption of making an
|
|
900 |
// InvokeWithExceptionNode for the partial intrinsic exit. We therefore
|
|
901 |
// replace the InvokeWithExceptionNode with a normal
|
|
902 |
// InvokeNode -- the deoptimization occurs when the invoke throws.
|
|
903 |
InvokeWithExceptionNode oldInvoke = (InvokeWithExceptionNode) fixedStateSplit.predecessor();
|
|
904 |
FrameState oldFrameState = oldInvoke.stateAfter();
|
|
905 |
InvokeNode newInvoke = oldInvoke.replaceWithInvoke();
|
|
906 |
newInvoke.setStateAfter(oldFrameState.duplicate());
|
|
907 |
if (replacements != null) {
|
|
908 |
replacements.put(oldInvoke, newInvoke);
|
|
909 |
}
|
|
910 |
handleAfterBciFrameState(newInvoke.stateAfter(), invoke, alwaysDuplicateStateAfter);
|
43972
|
911 |
} else {
|
49451
|
912 |
FixedNode deoptimizeNode = addDeoptimizeNode(graph, DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.NotCompiledExceptionHandler);
|
43972
|
913 |
if (fixedStateSplit instanceof AbstractBeginNode) {
|
|
914 |
deoptimizeNode = BeginNode.begin(deoptimizeNode);
|
|
915 |
}
|
|
916 |
fixedStateSplit.replaceAtPredecessor(deoptimizeNode);
|
|
917 |
GraphUtil.killCFG(fixedStateSplit);
|
|
918 |
}
|
|
919 |
}
|
|
920 |
}
|
|
921 |
}
|
|
922 |
return nonReplaceableFrameState;
|
|
923 |
}
|
|
924 |
|
49451
|
925 |
private static DeoptimizeNode addDeoptimizeNode(StructuredGraph graph, DeoptimizationAction action, DeoptimizationReason reason) {
|
|
926 |
GraalError.guarantee(graph.getGuardsStage() == GuardsStage.FLOATING_GUARDS, "Cannot introduce speculative deoptimization when Graal is used with fixed guards");
|
|
927 |
return graph.add(new DeoptimizeNode(action, reason));
|
|
928 |
}
|
|
929 |
|
46344
|
930 |
/**
|
|
931 |
* Ensure that all states are either {@link BytecodeFrame#INVALID_FRAMESTATE_BCI} or one of
|
|
932 |
* {@link BytecodeFrame#AFTER_BCI} or {@link BytecodeFrame#BEFORE_BCI}. Mixing of before and
|
|
933 |
* after isn't allowed.
|
|
934 |
*/
|
|
935 |
private static boolean checkContainsOnlyInvalidOrAfterFrameState(UnmodifiableEconomicMap<Node, Node> duplicates) {
|
|
936 |
int okBci = BytecodeFrame.INVALID_FRAMESTATE_BCI;
|
|
937 |
for (Node node : duplicates.getValues()) {
|
43972
|
938 |
if (node instanceof FrameState) {
|
|
939 |
FrameState frameState = (FrameState) node;
|
46344
|
940 |
if (frameState.bci == BytecodeFrame.INVALID_FRAMESTATE_BCI) {
|
|
941 |
continue;
|
|
942 |
}
|
|
943 |
if (frameState.bci == BytecodeFrame.AFTER_BCI || frameState.bci == BytecodeFrame.BEFORE_BCI) {
|
|
944 |
if (okBci == BytecodeFrame.INVALID_FRAMESTATE_BCI) {
|
|
945 |
okBci = frameState.bci;
|
|
946 |
} else {
|
|
947 |
assert okBci == frameState.bci : node.toString(Verbosity.Debugger);
|
|
948 |
}
|
|
949 |
} else {
|
|
950 |
assert false : node.toString(Verbosity.Debugger);
|
|
951 |
}
|
43972
|
952 |
}
|
|
953 |
}
|
|
954 |
return true;
|
|
955 |
}
|
|
956 |
|
|
957 |
/**
|
|
958 |
* Gets the receiver for an invoke, adding a guard if necessary to ensure it is non-null, and
|
|
959 |
* ensuring that the resulting type is compatible with the method being invoked.
|
|
960 |
*/
|
49873
|
961 |
@SuppressWarnings("try")
|
43972
|
962 |
public static ValueNode nonNullReceiver(Invoke invoke) {
|
49873
|
963 |
try (DebugCloseable position = invoke.asNode().withNodeSourcePosition()) {
|
|
964 |
MethodCallTargetNode callTarget = (MethodCallTargetNode) invoke.callTarget();
|
|
965 |
assert !callTarget.isStatic() : callTarget.targetMethod();
|
|
966 |
StructuredGraph graph = callTarget.graph();
|
|
967 |
ValueNode oldReceiver = callTarget.arguments().get(0);
|
|
968 |
ValueNode newReceiver = oldReceiver;
|
|
969 |
if (newReceiver.getStackKind() == JavaKind.Object) {
|
46344
|
970 |
|
49873
|
971 |
if (invoke.getInvokeKind() == InvokeKind.Special) {
|
|
972 |
Stamp paramStamp = newReceiver.stamp(NodeView.DEFAULT);
|
|
973 |
Stamp stamp = paramStamp.join(StampFactory.object(TypeReference.create(graph.getAssumptions(), callTarget.targetMethod().getDeclaringClass())));
|
|
974 |
if (!stamp.equals(paramStamp)) {
|
|
975 |
// The verifier and previous optimizations guarantee unconditionally that
|
|
976 |
// the
|
|
977 |
// receiver is at least of the type of the method holder for a special
|
|
978 |
// invoke.
|
|
979 |
newReceiver = graph.unique(new PiNode(newReceiver, stamp));
|
|
980 |
}
|
|
981 |
}
|
|
982 |
|
|
983 |
if (!StampTool.isPointerNonNull(newReceiver)) {
|
|
984 |
LogicNode condition = graph.unique(IsNullNode.create(newReceiver));
|
|
985 |
FixedGuardNode fixedGuard = graph.add(new FixedGuardNode(condition, NullCheckException, InvalidateReprofile, true));
|
|
986 |
PiNode nonNullReceiver = graph.unique(new PiNode(newReceiver, StampFactory.objectNonNull(), fixedGuard));
|
|
987 |
graph.addBeforeFixed(invoke.asNode(), fixedGuard);
|
|
988 |
newReceiver = nonNullReceiver;
|
46344
|
989 |
}
|
|
990 |
}
|
|
991 |
|
49873
|
992 |
if (newReceiver != oldReceiver) {
|
|
993 |
callTarget.replaceFirstInput(oldReceiver, newReceiver);
|
43972
|
994 |
}
|
49873
|
995 |
return newReceiver;
|
43972
|
996 |
}
|
|
997 |
}
|
|
998 |
|
|
999 |
public static boolean canIntrinsify(Replacements replacements, ResolvedJavaMethod target, int invokeBci) {
|
|
1000 |
return replacements.hasSubstitution(target, invokeBci);
|
|
1001 |
}
|
|
1002 |
|
49451
|
1003 |
public static StructuredGraph getIntrinsicGraph(Replacements replacements, ResolvedJavaMethod target, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition) {
|
|
1004 |
return replacements.getSubstitution(target, invokeBci, trackNodeSourcePosition, replaceePosition);
|
43972
|
1005 |
}
|
|
1006 |
|
|
1007 |
public static FixedWithNextNode inlineMacroNode(Invoke invoke, ResolvedJavaMethod concrete, Class<? extends FixedWithNextNode> macroNodeClass) throws GraalError {
|
|
1008 |
StructuredGraph graph = invoke.asNode().graph();
|
|
1009 |
if (!concrete.equals(((MethodCallTargetNode) invoke.callTarget()).targetMethod())) {
|
|
1010 |
assert ((MethodCallTargetNode) invoke.callTarget()).invokeKind().hasReceiver();
|
|
1011 |
InliningUtil.replaceInvokeCallTarget(invoke, graph, InvokeKind.Special, concrete);
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
FixedWithNextNode macroNode = createMacroNodeInstance(macroNodeClass, invoke);
|
|
1015 |
|
|
1016 |
CallTargetNode callTarget = invoke.callTarget();
|
|
1017 |
if (invoke instanceof InvokeNode) {
|
|
1018 |
graph.replaceFixedWithFixed((InvokeNode) invoke, graph.add(macroNode));
|
|
1019 |
} else {
|
|
1020 |
InvokeWithExceptionNode invokeWithException = (InvokeWithExceptionNode) invoke;
|
|
1021 |
invokeWithException.killExceptionEdge();
|
|
1022 |
graph.replaceSplitWithFixed(invokeWithException, graph.add(macroNode), invokeWithException.next());
|
|
1023 |
}
|
|
1024 |
GraphUtil.killWithUnusedFloatingInputs(callTarget);
|
|
1025 |
return macroNode;
|
|
1026 |
}
|
|
1027 |
|
|
1028 |
private static FixedWithNextNode createMacroNodeInstance(Class<? extends FixedWithNextNode> macroNodeClass, Invoke invoke) throws GraalError {
|
|
1029 |
try {
|
|
1030 |
Constructor<?> cons = macroNodeClass.getDeclaredConstructor(Invoke.class);
|
|
1031 |
return (FixedWithNextNode) cons.newInstance(invoke);
|
|
1032 |
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException e) {
|
|
1033 |
throw new GraalGraphError(e).addContext(invoke.asNode()).addContext("macroSubstitution", macroNodeClass);
|
|
1034 |
}
|
|
1035 |
}
|
|
1036 |
|
|
1037 |
/**
|
|
1038 |
* This method exclude InstrumentationNode from inlining heuristics.
|
|
1039 |
*/
|
|
1040 |
public static int getNodeCount(StructuredGraph graph) {
|
46344
|
1041 |
return graph.getNodeCount();
|
43972
|
1042 |
}
|
|
1043 |
|
|
1044 |
}
|