43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.replacements;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.bytecode.Bytecode;
|
|
28 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
|
29 |
import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
|
|
30 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
32 |
import org.graalvm.compiler.core.common.type.StampPair;
|
|
33 |
import org.graalvm.compiler.core.common.type.TypeReference;
|
46640
|
34 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
35 |
import org.graalvm.compiler.debug.GraalError;
|
49451
|
36 |
import org.graalvm.compiler.debug.DebugCloseable;
|
|
37 |
import org.graalvm.compiler.graph.NodeSourcePosition;
|
46344
|
38 |
import org.graalvm.compiler.nodes.CallTargetNode;
|
43972
|
39 |
import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
|
|
40 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
41 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
|
42 |
import org.graalvm.compiler.nodes.FrameState;
|
|
43 |
import org.graalvm.compiler.nodes.ParameterNode;
|
|
44 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
45 |
import org.graalvm.compiler.nodes.StateSplit;
|
|
46 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
47 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
48 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
49 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
|
50 |
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
|
|
51 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
52 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
|
|
53 |
import org.graalvm.compiler.nodes.spi.StampProvider;
|
46344
|
54 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
55 |
|
|
56 |
import jdk.vm.ci.code.BailoutException;
|
|
57 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
58 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
59 |
import jdk.vm.ci.meta.JavaKind;
|
|
60 |
import jdk.vm.ci.meta.JavaType;
|
|
61 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
62 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
63 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
64 |
import jdk.vm.ci.meta.Signature;
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Implementation of {@link GraphBuilderContext} used to produce a graph for a method based on an
|
|
68 |
* {@link InvocationPlugin} for the method.
|
|
69 |
*/
|
|
70 |
public class IntrinsicGraphBuilder implements GraphBuilderContext, Receiver {
|
|
71 |
|
|
72 |
protected final MetaAccessProvider metaAccess;
|
|
73 |
protected final ConstantReflectionProvider constantReflection;
|
|
74 |
protected final ConstantFieldProvider constantFieldProvider;
|
|
75 |
protected final StampProvider stampProvider;
|
|
76 |
protected final StructuredGraph graph;
|
|
77 |
protected final Bytecode code;
|
|
78 |
protected final ResolvedJavaMethod method;
|
|
79 |
protected final int invokeBci;
|
|
80 |
protected FixedWithNextNode lastInstr;
|
|
81 |
protected ValueNode[] arguments;
|
|
82 |
protected ValueNode returnValue;
|
|
83 |
|
46640
|
84 |
public IntrinsicGraphBuilder(OptionValues options, DebugContext debug, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
|
|
85 |
StampProvider stampProvider, Bytecode code, int invokeBci) {
|
|
86 |
this(options, debug, metaAccess, constantReflection, constantFieldProvider, stampProvider, code, invokeBci, AllowAssumptions.YES);
|
43972
|
87 |
}
|
|
88 |
|
46640
|
89 |
protected IntrinsicGraphBuilder(OptionValues options, DebugContext debug, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
|
|
90 |
StampProvider stampProvider, Bytecode code, int invokeBci, AllowAssumptions allowAssumptions) {
|
43972
|
91 |
this.metaAccess = metaAccess;
|
|
92 |
this.constantReflection = constantReflection;
|
|
93 |
this.constantFieldProvider = constantFieldProvider;
|
|
94 |
this.stampProvider = stampProvider;
|
|
95 |
this.code = code;
|
|
96 |
this.method = code.getMethod();
|
51736
|
97 |
this.graph = new StructuredGraph.Builder(options, debug, allowAssumptions).method(method).setIsSubstitution(true).build();
|
49451
|
98 |
this.graph.setTrackNodeSourcePosition();
|
43972
|
99 |
this.invokeBci = invokeBci;
|
|
100 |
this.lastInstr = graph.start();
|
|
101 |
|
|
102 |
Signature sig = method.getSignature();
|
|
103 |
int max = sig.getParameterCount(false);
|
|
104 |
this.arguments = new ValueNode[max + (method.isStatic() ? 0 : 1)];
|
|
105 |
|
|
106 |
int javaIndex = 0;
|
|
107 |
int index = 0;
|
|
108 |
if (!method.isStatic()) {
|
|
109 |
// add the receiver
|
|
110 |
Stamp receiverStamp = StampFactory.objectNonNull(TypeReference.createWithoutAssumptions(method.getDeclaringClass()));
|
|
111 |
ValueNode receiver = graph.addWithoutUnique(new ParameterNode(javaIndex, StampPair.createSingle(receiverStamp)));
|
|
112 |
arguments[index] = receiver;
|
|
113 |
javaIndex = 1;
|
|
114 |
index = 1;
|
|
115 |
}
|
|
116 |
ResolvedJavaType accessingClass = method.getDeclaringClass();
|
|
117 |
for (int i = 0; i < max; i++) {
|
|
118 |
JavaType type = sig.getParameterType(i, accessingClass).resolve(accessingClass);
|
|
119 |
JavaKind kind = type.getJavaKind();
|
|
120 |
Stamp stamp;
|
|
121 |
if (kind == JavaKind.Object && type instanceof ResolvedJavaType) {
|
|
122 |
stamp = StampFactory.object(TypeReference.createWithoutAssumptions((ResolvedJavaType) type));
|
|
123 |
} else {
|
|
124 |
stamp = StampFactory.forKind(kind);
|
|
125 |
}
|
|
126 |
ValueNode param = graph.addWithoutUnique(new ParameterNode(index, StampPair.createSingle(stamp)));
|
|
127 |
arguments[index] = param;
|
|
128 |
javaIndex += kind.getSlotCount();
|
|
129 |
index++;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
private <T extends ValueNode> void updateLastInstruction(T v) {
|
|
134 |
if (v instanceof FixedNode) {
|
|
135 |
FixedNode fixedNode = (FixedNode) v;
|
|
136 |
lastInstr.setNext(fixedNode);
|
|
137 |
if (fixedNode instanceof FixedWithNextNode) {
|
|
138 |
FixedWithNextNode fixedWithNextNode = (FixedWithNextNode) fixedNode;
|
|
139 |
assert fixedWithNextNode.next() == null : "cannot append instruction to instruction which isn't end";
|
|
140 |
lastInstr = fixedWithNextNode;
|
|
141 |
} else {
|
|
142 |
lastInstr = null;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
@Override
|
|
148 |
public <T extends ValueNode> T append(T v) {
|
|
149 |
if (v.graph() != null) {
|
|
150 |
return v;
|
|
151 |
}
|
|
152 |
T added = graph.addOrUniqueWithInputs(v);
|
|
153 |
if (added == v) {
|
|
154 |
updateLastInstruction(v);
|
|
155 |
}
|
|
156 |
return added;
|
|
157 |
}
|
|
158 |
|
|
159 |
@Override
|
|
160 |
public void push(JavaKind kind, ValueNode value) {
|
|
161 |
assert kind != JavaKind.Void;
|
|
162 |
assert returnValue == null;
|
|
163 |
returnValue = value;
|
|
164 |
}
|
|
165 |
|
|
166 |
@Override
|
|
167 |
public void handleReplacedInvoke(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, boolean forceInlineEverything) {
|
|
168 |
throw GraalError.shouldNotReachHere();
|
|
169 |
}
|
|
170 |
|
|
171 |
@Override
|
46344
|
172 |
public void handleReplacedInvoke(CallTargetNode callTarget, JavaKind resultType) {
|
|
173 |
throw GraalError.shouldNotReachHere();
|
|
174 |
}
|
|
175 |
|
|
176 |
@Override
|
43972
|
177 |
public StampProvider getStampProvider() {
|
|
178 |
return stampProvider;
|
|
179 |
}
|
|
180 |
|
|
181 |
@Override
|
|
182 |
public MetaAccessProvider getMetaAccess() {
|
|
183 |
return metaAccess;
|
|
184 |
}
|
|
185 |
|
|
186 |
@Override
|
|
187 |
public ConstantReflectionProvider getConstantReflection() {
|
|
188 |
return constantReflection;
|
|
189 |
}
|
|
190 |
|
|
191 |
@Override
|
|
192 |
public ConstantFieldProvider getConstantFieldProvider() {
|
|
193 |
return constantFieldProvider;
|
|
194 |
}
|
|
195 |
|
|
196 |
@Override
|
|
197 |
public StructuredGraph getGraph() {
|
|
198 |
return graph;
|
|
199 |
}
|
|
200 |
|
|
201 |
@Override
|
|
202 |
public void setStateAfter(StateSplit sideEffect) {
|
|
203 |
assert sideEffect.hasSideEffect();
|
|
204 |
FrameState stateAfter = getGraph().add(new FrameState(BytecodeFrame.BEFORE_BCI));
|
|
205 |
sideEffect.setStateAfter(stateAfter);
|
|
206 |
}
|
|
207 |
|
|
208 |
@Override
|
|
209 |
public GraphBuilderContext getParent() {
|
|
210 |
return null;
|
|
211 |
}
|
|
212 |
|
|
213 |
@Override
|
|
214 |
public Bytecode getCode() {
|
|
215 |
return code;
|
|
216 |
}
|
|
217 |
|
|
218 |
@Override
|
|
219 |
public ResolvedJavaMethod getMethod() {
|
|
220 |
return method;
|
|
221 |
}
|
|
222 |
|
|
223 |
@Override
|
|
224 |
public int bci() {
|
|
225 |
return invokeBci;
|
|
226 |
}
|
|
227 |
|
|
228 |
@Override
|
|
229 |
public InvokeKind getInvokeKind() {
|
|
230 |
return method.isStatic() ? InvokeKind.Static : InvokeKind.Virtual;
|
|
231 |
}
|
|
232 |
|
|
233 |
@Override
|
|
234 |
public JavaType getInvokeReturnType() {
|
|
235 |
return method.getSignature().getReturnType(method.getDeclaringClass());
|
|
236 |
}
|
|
237 |
|
|
238 |
@Override
|
|
239 |
public int getDepth() {
|
|
240 |
return 0;
|
|
241 |
}
|
|
242 |
|
|
243 |
@Override
|
|
244 |
public boolean parsingIntrinsic() {
|
|
245 |
return true;
|
|
246 |
}
|
|
247 |
|
|
248 |
@Override
|
|
249 |
public IntrinsicContext getIntrinsic() {
|
|
250 |
throw GraalError.shouldNotReachHere();
|
|
251 |
}
|
|
252 |
|
|
253 |
@Override
|
|
254 |
public BailoutException bailout(String string) {
|
|
255 |
throw GraalError.shouldNotReachHere();
|
|
256 |
}
|
|
257 |
|
|
258 |
@Override
|
|
259 |
public ValueNode get(boolean performNullCheck) {
|
|
260 |
return arguments[0];
|
|
261 |
}
|
|
262 |
|
49451
|
263 |
@SuppressWarnings("try")
|
43972
|
264 |
public StructuredGraph buildGraph(InvocationPlugin plugin) {
|
49451
|
265 |
NodeSourcePosition position = graph.trackNodeSourcePosition() ? NodeSourcePosition.placeholder(method) : null;
|
|
266 |
try (DebugCloseable context = graph.withNodeSourcePosition(position)) {
|
|
267 |
Receiver receiver = method.isStatic() ? null : this;
|
|
268 |
if (plugin.execute(this, method, receiver, arguments)) {
|
|
269 |
assert (returnValue != null) == (method.getSignature().getReturnKind() != JavaKind.Void) : method;
|
|
270 |
append(new ReturnNode(returnValue));
|
|
271 |
return graph;
|
|
272 |
}
|
|
273 |
return null;
|
43972
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
@Override
|
|
278 |
public boolean intrinsify(BytecodeProvider bytecodeProvider, ResolvedJavaMethod targetMethod, ResolvedJavaMethod substitute, InvocationPlugin.Receiver receiver, ValueNode[] args) {
|
|
279 |
throw GraalError.shouldNotReachHere();
|
|
280 |
}
|
|
281 |
|
|
282 |
@Override
|
|
283 |
public String toString() {
|
|
284 |
return String.format("%s:intrinsic", method.format("%H.%n(%p)"));
|
|
285 |
}
|
|
286 |
}
|