author | jwilhelm |
Thu, 22 Jun 2017 00:51:07 +0200 | |
changeset 46554 | aa1cfd918c4f |
parent 45514 | 0fc9cc73ce45 |
parent 46551 | d01034a83ab2 |
child 46807 | 8b2c620d7092 |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
*/ |
|
23 |
package org.graalvm.compiler.replacements; |
|
24 |
||
25 |
import static jdk.vm.ci.code.MemoryBarriers.JMM_POST_VOLATILE_READ; |
|
26 |
import static jdk.vm.ci.code.MemoryBarriers.JMM_POST_VOLATILE_WRITE; |
|
27 |
import static jdk.vm.ci.code.MemoryBarriers.JMM_PRE_VOLATILE_READ; |
|
28 |
import static jdk.vm.ci.code.MemoryBarriers.JMM_PRE_VOLATILE_WRITE; |
|
29 |
import static jdk.vm.ci.code.MemoryBarriers.LOAD_LOAD; |
|
30 |
import static jdk.vm.ci.code.MemoryBarriers.LOAD_STORE; |
|
31 |
import static jdk.vm.ci.code.MemoryBarriers.STORE_LOAD; |
|
32 |
import static jdk.vm.ci.code.MemoryBarriers.STORE_STORE; |
|
46459 | 33 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier; |
43972 | 34 |
|
35 |
import java.lang.reflect.Array; |
|
36 |
import java.lang.reflect.Field; |
|
37 |
import java.util.Arrays; |
|
38 |
||
39 |
import org.graalvm.compiler.api.directives.GraalDirectives; |
|
40 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; |
|
41 |
import org.graalvm.compiler.bytecode.BytecodeProvider; |
|
42 |
import org.graalvm.compiler.core.common.calc.Condition; |
|
43 |
import org.graalvm.compiler.core.common.calc.UnsignedMath; |
|
46371 | 44 |
import org.graalvm.compiler.core.common.type.ObjectStamp; |
43972 | 45 |
import org.graalvm.compiler.core.common.type.Stamp; |
46 |
import org.graalvm.compiler.core.common.type.StampFactory; |
|
47 |
import org.graalvm.compiler.core.common.type.TypeReference; |
|
48 |
import org.graalvm.compiler.debug.GraalError; |
|
49 |
import org.graalvm.compiler.graph.Edges; |
|
50 |
import org.graalvm.compiler.graph.Node; |
|
51 |
import org.graalvm.compiler.graph.NodeList; |
|
52 |
import org.graalvm.compiler.nodes.ConstantNode; |
|
53 |
import org.graalvm.compiler.nodes.DeoptimizeNode; |
|
54 |
import org.graalvm.compiler.nodes.FixedGuardNode; |
|
55 |
import org.graalvm.compiler.nodes.LogicNode; |
|
56 |
import org.graalvm.compiler.nodes.StructuredGraph; |
|
57 |
import org.graalvm.compiler.nodes.ValueNode; |
|
58 |
import org.graalvm.compiler.nodes.calc.AbsNode; |
|
59 |
import org.graalvm.compiler.nodes.calc.CompareNode; |
|
60 |
import org.graalvm.compiler.nodes.calc.ConditionalNode; |
|
61 |
import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; |
|
62 |
import org.graalvm.compiler.nodes.calc.NarrowNode; |
|
63 |
import org.graalvm.compiler.nodes.calc.ReinterpretNode; |
|
64 |
import org.graalvm.compiler.nodes.calc.RightShiftNode; |
|
65 |
import org.graalvm.compiler.nodes.calc.SignExtendNode; |
|
66 |
import org.graalvm.compiler.nodes.calc.SqrtNode; |
|
67 |
import org.graalvm.compiler.nodes.calc.UnsignedDivNode; |
|
68 |
import org.graalvm.compiler.nodes.calc.UnsignedRemNode; |
|
69 |
import org.graalvm.compiler.nodes.calc.ZeroExtendNode; |
|
70 |
import org.graalvm.compiler.nodes.debug.BindToRegisterNode; |
|
71 |
import org.graalvm.compiler.nodes.debug.BlackholeNode; |
|
72 |
import org.graalvm.compiler.nodes.debug.ControlFlowAnchorNode; |
|
73 |
import org.graalvm.compiler.nodes.debug.OpaqueNode; |
|
74 |
import org.graalvm.compiler.nodes.debug.SpillRegistersNode; |
|
75 |
import org.graalvm.compiler.nodes.extended.BoxNode; |
|
76 |
import org.graalvm.compiler.nodes.extended.BranchProbabilityNode; |
|
77 |
import org.graalvm.compiler.nodes.extended.GetClassNode; |
|
78 |
import org.graalvm.compiler.nodes.extended.MembarNode; |
|
46371 | 79 |
import org.graalvm.compiler.nodes.extended.RawLoadNode; |
80 |
import org.graalvm.compiler.nodes.extended.RawStoreNode; |
|
43972 | 81 |
import org.graalvm.compiler.nodes.extended.UnboxNode; |
82 |
import org.graalvm.compiler.nodes.extended.UnsafeMemoryLoadNode; |
|
83 |
import org.graalvm.compiler.nodes.extended.UnsafeMemoryStoreNode; |
|
84 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
|
85 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; |
|
86 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver; |
|
87 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; |
|
88 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; |
|
89 |
import org.graalvm.compiler.nodes.java.ClassIsAssignableFromNode; |
|
90 |
import org.graalvm.compiler.nodes.java.DynamicNewArrayNode; |
|
91 |
import org.graalvm.compiler.nodes.java.DynamicNewInstanceNode; |
|
92 |
import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode; |
|
93 |
import org.graalvm.compiler.nodes.java.LoadFieldNode; |
|
94 |
import org.graalvm.compiler.nodes.java.RegisterFinalizerNode; |
|
46371 | 95 |
import org.graalvm.compiler.nodes.java.UnsafeCompareAndSwapNode; |
43972 | 96 |
import org.graalvm.compiler.nodes.util.GraphUtil; |
97 |
import org.graalvm.compiler.nodes.virtual.EnsureVirtualizedNode; |
|
98 |
import org.graalvm.compiler.replacements.nodes.ReverseBytesNode; |
|
99 |
import org.graalvm.compiler.replacements.nodes.VirtualizableInvokeMacroNode; |
|
100 |
import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerAddExactNode; |
|
101 |
import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerMulExactNode; |
|
102 |
import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerSubExactNode; |
|
46551 | 103 |
import org.graalvm.word.LocationIdentity; |
43972 | 104 |
|
105 |
import jdk.vm.ci.meta.DeoptimizationAction; |
|
106 |
import jdk.vm.ci.meta.DeoptimizationReason; |
|
107 |
import jdk.vm.ci.meta.JavaConstant; |
|
108 |
import jdk.vm.ci.meta.JavaKind; |
|
109 |
import jdk.vm.ci.meta.MetaAccessProvider; |
|
110 |
import jdk.vm.ci.meta.ResolvedJavaField; |
|
111 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
112 |
import jdk.vm.ci.meta.ResolvedJavaType; |
|
113 |
import sun.misc.Unsafe; |
|
114 |
||
115 |
/** |
|
116 |
* Provides non-runtime specific {@link InvocationPlugin}s. |
|
117 |
*/ |
|
118 |
public class StandardGraphBuilderPlugins { |
|
119 |
||
120 |
public static void registerInvocationPlugins(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection, InvocationPlugins plugins, BytecodeProvider bytecodeProvider, |
|
121 |
boolean allowDeoptimization) { |
|
122 |
registerObjectPlugins(plugins); |
|
123 |
registerClassPlugins(plugins); |
|
124 |
registerMathPlugins(plugins, allowDeoptimization); |
|
125 |
registerUnsignedMathPlugins(plugins); |
|
126 |
registerStringPlugins(plugins, bytecodeProvider, snippetReflection); |
|
127 |
registerCharacterPlugins(plugins); |
|
128 |
registerShortPlugins(plugins); |
|
129 |
registerIntegerLongPlugins(plugins, JavaKind.Int); |
|
130 |
registerIntegerLongPlugins(plugins, JavaKind.Long); |
|
131 |
registerFloatPlugins(plugins); |
|
132 |
registerDoublePlugins(plugins); |
|
133 |
registerArraysPlugins(plugins, bytecodeProvider); |
|
134 |
registerArrayPlugins(plugins, bytecodeProvider); |
|
135 |
registerUnsafePlugins(plugins, bytecodeProvider); |
|
136 |
registerEdgesPlugins(metaAccess, plugins); |
|
137 |
registerGraalDirectivesPlugins(plugins); |
|
138 |
registerBoxingPlugins(plugins); |
|
139 |
registerJMHBlackholePlugins(plugins, bytecodeProvider); |
|
140 |
registerJFRThrowablePlugins(plugins, bytecodeProvider); |
|
141 |
registerMethodHandleImplPlugins(plugins, snippetReflection, bytecodeProvider); |
|
142 |
} |
|
143 |
||
144 |
private static final Field STRING_VALUE_FIELD; |
|
145 |
||
146 |
static { |
|
147 |
try { |
|
148 |
STRING_VALUE_FIELD = String.class.getDeclaredField("value"); |
|
149 |
} catch (NoSuchFieldException e) { |
|
150 |
throw new GraalError(e); |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
private static void registerStringPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider, SnippetReflectionProvider snippetReflection) { |
|
46344 | 155 |
final Registration r = new Registration(plugins, String.class, bytecodeProvider); |
43972 | 156 |
r.register1("hashCode", Receiver.class, new InvocationPlugin() { |
157 |
@Override |
|
158 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
159 |
if (receiver.isConstant()) { |
|
160 |
String s = snippetReflection.asObject(String.class, (JavaConstant) receiver.get().asConstant()); |
|
161 |
b.addPush(JavaKind.Int, b.add(ConstantNode.forInt(s.hashCode()))); |
|
162 |
return true; |
|
163 |
} |
|
164 |
return false; |
|
165 |
} |
|
166 |
}); |
|
46344 | 167 |
|
43972 | 168 |
if (Java8OrEarlier) { |
169 |
r.registerMethodSubstitution(StringSubstitutions.class, "equals", Receiver.class, Object.class); |
|
46344 | 170 |
r.register7("indexOf", char[].class, int.class, int.class, char[].class, int.class, int.class, int.class, new StringIndexOfConstantPlugin()); |
43972 | 171 |
|
46344 | 172 |
Registration sr = new Registration(plugins, StringSubstitutions.class); |
173 |
sr.register1("getValue", String.class, new InvocationPlugin() { |
|
43972 | 174 |
@Override |
175 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
176 |
ResolvedJavaField field = b.getMetaAccess().lookupJavaField(STRING_VALUE_FIELD); |
|
46371 | 177 |
b.addPush(JavaKind.Object, LoadFieldNode.create(b.getConstantFieldProvider(), b.getConstantReflection(), b.getMetaAccess(), |
178 |
b.getOptions(), b.getAssumptions(), value, field, false, false)); |
|
43972 | 179 |
return true; |
180 |
} |
|
181 |
}); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
private static void registerArraysPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
186 |
Registration r = new Registration(plugins, Arrays.class, bytecodeProvider); |
|
187 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", boolean[].class, boolean[].class); |
|
188 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", byte[].class, byte[].class); |
|
189 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", short[].class, short[].class); |
|
190 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", char[].class, char[].class); |
|
191 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", int[].class, int[].class); |
|
192 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", float[].class, float[].class); |
|
193 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", long[].class, long[].class); |
|
194 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", double[].class, double[].class); |
|
195 |
} |
|
196 |
||
197 |
private static void registerArrayPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
198 |
Registration r = new Registration(plugins, Array.class, bytecodeProvider); |
|
199 |
r.register2("newInstance", Class.class, int.class, new InvocationPlugin() { |
|
200 |
@Override |
|
201 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode componentType, ValueNode length) { |
|
202 |
b.addPush(JavaKind.Object, new DynamicNewArrayNode(componentType, length, true)); |
|
203 |
return true; |
|
204 |
} |
|
205 |
}); |
|
206 |
r.registerMethodSubstitution(ArraySubstitutions.class, "getLength", Object.class); |
|
207 |
} |
|
208 |
||
209 |
private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
210 |
Registration r; |
|
211 |
if (Java8OrEarlier) { |
|
212 |
r = new Registration(plugins, Unsafe.class); |
|
213 |
} else { |
|
214 |
r = new Registration(plugins, "jdk.internal.misc.Unsafe", bytecodeProvider); |
|
215 |
} |
|
216 |
||
217 |
for (JavaKind kind : JavaKind.values()) { |
|
218 |
if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { |
|
219 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); |
|
220 |
String kindName = kind.name(); |
|
221 |
String getName = "get" + kindName; |
|
222 |
String putName = "put" + kindName; |
|
223 |
// Object-based accesses |
|
224 |
r.register3(getName, Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, false)); |
|
225 |
r.register4(putName, Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, false)); |
|
226 |
// Volatile object-based accesses |
|
227 |
r.register3(getName + "Volatile", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, true)); |
|
228 |
r.register4(putName + "Volatile", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, true)); |
|
229 |
// Ordered object-based accesses |
|
230 |
if (Java8OrEarlier) { |
|
231 |
if (kind == JavaKind.Int || kind == JavaKind.Long || kind == JavaKind.Object) { |
|
232 |
r.register4("putOrdered" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, true)); |
|
233 |
} |
|
234 |
} else { |
|
235 |
r.register4("put" + kindName + "Release", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, true)); |
|
236 |
} |
|
237 |
if (kind != JavaKind.Boolean && kind != JavaKind.Object) { |
|
238 |
// Raw accesses to memory addresses |
|
239 |
r.register2(getName, Receiver.class, long.class, new UnsafeGetPlugin(kind, false)); |
|
240 |
r.register3(putName, Receiver.class, long.class, kind.toJavaClass(), new UnsafePutPlugin(kind, false)); |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
// Accesses to native memory addresses. |
|
246 |
r.register2("getAddress", Receiver.class, long.class, new UnsafeGetPlugin(JavaKind.Long, false)); |
|
247 |
r.register3("putAddress", Receiver.class, long.class, long.class, new UnsafePutPlugin(JavaKind.Long, false)); |
|
248 |
||
249 |
for (JavaKind kind : new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}) { |
|
250 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); |
|
45514
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
251 |
String casName; |
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
252 |
if (Java8OrEarlier) { |
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
253 |
casName = "compareAndSwap"; |
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
254 |
} else { |
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
255 |
casName = "compareAndSet"; |
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
256 |
} |
0fc9cc73ce45
8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
psandoz
parents:
43972
diff
changeset
|
257 |
r.register5(casName + kind.name(), Receiver.class, Object.class, long.class, javaClass, javaClass, new InvocationPlugin() { |
43972 | 258 |
@Override |
259 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode expected, ValueNode x) { |
|
260 |
// Emits a null-check for the otherwise unused receiver |
|
261 |
unsafe.get(); |
|
46344 | 262 |
b.addPush(JavaKind.Int, new UnsafeCompareAndSwapNode(object, offset, expected, x, kind, LocationIdentity.any())); |
43972 | 263 |
b.getGraph().markUnsafeAccess(); |
264 |
return true; |
|
265 |
} |
|
266 |
}); |
|
267 |
} |
|
268 |
||
269 |
r.register2("allocateInstance", Receiver.class, Class.class, new InvocationPlugin() { |
|
270 |
||
271 |
@Override |
|
272 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode clazz) { |
|
273 |
// Emits a null-check for the otherwise unused receiver |
|
274 |
unsafe.get(); |
|
46344 | 275 |
b.addPush(JavaKind.Object, new DynamicNewInstanceNode(b.nullCheckedValue(clazz, DeoptimizationAction.None), true)); |
43972 | 276 |
return true; |
277 |
} |
|
278 |
||
279 |
}); |
|
280 |
||
281 |
r.register1("loadFence", Receiver.class, new UnsafeFencePlugin(LOAD_LOAD | LOAD_STORE)); |
|
282 |
r.register1("storeFence", Receiver.class, new UnsafeFencePlugin(STORE_STORE | LOAD_STORE)); |
|
283 |
r.register1("fullFence", Receiver.class, new UnsafeFencePlugin(LOAD_LOAD | STORE_STORE | LOAD_STORE | STORE_LOAD)); |
|
284 |
} |
|
285 |
||
286 |
private static void registerIntegerLongPlugins(InvocationPlugins plugins, JavaKind kind) { |
|
287 |
Class<?> declaringClass = kind.toBoxedJavaClass(); |
|
288 |
Class<?> type = kind.toJavaClass(); |
|
289 |
Registration r = new Registration(plugins, declaringClass); |
|
290 |
r.register1("reverseBytes", type, new InvocationPlugin() { |
|
291 |
@Override |
|
292 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 293 |
b.push(kind, b.append(new ReverseBytesNode(value).canonical(null))); |
43972 | 294 |
return true; |
295 |
} |
|
296 |
}); |
|
297 |
r.register2("divideUnsigned", type, type, new InvocationPlugin() { |
|
298 |
@Override |
|
299 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode dividend, ValueNode divisor) { |
|
46393 | 300 |
b.push(kind, b.append(new UnsignedDivNode(dividend, divisor).canonical(null))); |
43972 | 301 |
return true; |
302 |
} |
|
303 |
}); |
|
304 |
r.register2("remainderUnsigned", type, type, new InvocationPlugin() { |
|
305 |
@Override |
|
306 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode dividend, ValueNode divisor) { |
|
46393 | 307 |
b.push(kind, b.append(new UnsignedRemNode(dividend, divisor).canonical(null))); |
43972 | 308 |
return true; |
309 |
} |
|
310 |
}); |
|
311 |
} |
|
312 |
||
313 |
private static void registerCharacterPlugins(InvocationPlugins plugins) { |
|
314 |
Registration r = new Registration(plugins, Character.class); |
|
315 |
r.register1("reverseBytes", char.class, new InvocationPlugin() { |
|
316 |
@Override |
|
317 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
318 |
// return (char) (Integer.reverse(i) >> 16); |
|
319 |
ReverseBytesNode reverse = b.add(new ReverseBytesNode(value)); |
|
320 |
RightShiftNode rightShift = b.add(new RightShiftNode(reverse, b.add(ConstantNode.forInt(16)))); |
|
321 |
ZeroExtendNode charCast = b.add(new ZeroExtendNode(b.add(new NarrowNode(rightShift, 16)), 32)); |
|
46393 | 322 |
b.push(JavaKind.Char, b.append(charCast.canonical(null))); |
43972 | 323 |
return true; |
324 |
} |
|
325 |
}); |
|
326 |
} |
|
327 |
||
328 |
private static void registerShortPlugins(InvocationPlugins plugins) { |
|
329 |
Registration r = new Registration(plugins, Short.class); |
|
330 |
r.register1("reverseBytes", short.class, new InvocationPlugin() { |
|
331 |
@Override |
|
332 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
333 |
// return (short) (Integer.reverse(i) >> 16); |
|
334 |
ReverseBytesNode reverse = b.add(new ReverseBytesNode(value)); |
|
335 |
RightShiftNode rightShift = b.add(new RightShiftNode(reverse, b.add(ConstantNode.forInt(16)))); |
|
336 |
SignExtendNode charCast = b.add(new SignExtendNode(b.add(new NarrowNode(rightShift, 16)), 32)); |
|
46393 | 337 |
b.push(JavaKind.Short, b.append(charCast.canonical(null))); |
43972 | 338 |
return true; |
339 |
} |
|
340 |
}); |
|
341 |
} |
|
342 |
||
343 |
private static void registerFloatPlugins(InvocationPlugins plugins) { |
|
344 |
Registration r = new Registration(plugins, Float.class); |
|
345 |
r.register1("floatToRawIntBits", float.class, new InvocationPlugin() { |
|
346 |
@Override |
|
347 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 348 |
b.push(JavaKind.Int, b.append(new ReinterpretNode(JavaKind.Int, value).canonical(null))); |
43972 | 349 |
return true; |
350 |
} |
|
351 |
}); |
|
352 |
r.register1("intBitsToFloat", int.class, new InvocationPlugin() { |
|
353 |
@Override |
|
354 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 355 |
b.push(JavaKind.Float, b.append(new ReinterpretNode(JavaKind.Float, value).canonical(null))); |
43972 | 356 |
return true; |
357 |
} |
|
358 |
}); |
|
359 |
} |
|
360 |
||
361 |
private static void registerDoublePlugins(InvocationPlugins plugins) { |
|
362 |
Registration r = new Registration(plugins, Double.class); |
|
363 |
r.register1("doubleToRawLongBits", double.class, new InvocationPlugin() { |
|
364 |
@Override |
|
365 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 366 |
b.push(JavaKind.Long, b.append(new ReinterpretNode(JavaKind.Long, value).canonical(null))); |
43972 | 367 |
return true; |
368 |
} |
|
369 |
}); |
|
370 |
r.register1("longBitsToDouble", long.class, new InvocationPlugin() { |
|
371 |
@Override |
|
372 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 373 |
b.push(JavaKind.Double, b.append(new ReinterpretNode(JavaKind.Double, value).canonical(null))); |
43972 | 374 |
return true; |
375 |
} |
|
376 |
}); |
|
377 |
} |
|
378 |
||
379 |
private static void registerMathPlugins(InvocationPlugins plugins, boolean allowDeoptimization) { |
|
380 |
Registration r = new Registration(plugins, Math.class); |
|
381 |
if (allowDeoptimization) { |
|
382 |
for (JavaKind kind : new JavaKind[]{JavaKind.Int, JavaKind.Long}) { |
|
383 |
Class<?> type = kind.toJavaClass(); |
|
384 |
r.register2("addExact", type, type, new InvocationPlugin() { |
|
385 |
@Override |
|
386 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
387 |
b.addPush(kind, new IntegerAddExactNode(x, y)); |
|
388 |
return true; |
|
389 |
} |
|
390 |
}); |
|
391 |
r.register2("subtractExact", type, type, new InvocationPlugin() { |
|
392 |
@Override |
|
393 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
394 |
b.addPush(kind, new IntegerSubExactNode(x, y)); |
|
395 |
return true; |
|
396 |
} |
|
397 |
}); |
|
398 |
r.register2("multiplyExact", type, type, new InvocationPlugin() { |
|
399 |
@Override |
|
400 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
401 |
b.addPush(kind, new IntegerMulExactNode(x, y)); |
|
402 |
return true; |
|
403 |
} |
|
404 |
}); |
|
405 |
} |
|
406 |
} |
|
407 |
r.register1("abs", Float.TYPE, new InvocationPlugin() { |
|
408 |
||
409 |
@Override |
|
410 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 411 |
b.push(JavaKind.Float, b.append(new AbsNode(value).canonical(null))); |
43972 | 412 |
return true; |
413 |
} |
|
414 |
}); |
|
415 |
r.register1("abs", Double.TYPE, new InvocationPlugin() { |
|
416 |
@Override |
|
417 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 418 |
b.push(JavaKind.Double, b.append(new AbsNode(value).canonical(null))); |
43972 | 419 |
return true; |
420 |
} |
|
421 |
}); |
|
422 |
r.register1("sqrt", Double.TYPE, new InvocationPlugin() { |
|
423 |
@Override |
|
424 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 425 |
b.push(JavaKind.Double, b.append(new SqrtNode(value).canonical(null))); |
43972 | 426 |
return true; |
427 |
} |
|
428 |
}); |
|
429 |
} |
|
430 |
||
46344 | 431 |
public static final class StringIndexOfConstantPlugin implements InvocationPlugin { |
432 |
@Override |
|
433 |
public boolean inlineOnly() { |
|
434 |
return true; |
|
435 |
} |
|
436 |
||
437 |
@Override |
|
438 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode arg1, ValueNode arg2, ValueNode arg3, ValueNode arg4, |
|
439 |
ValueNode arg5, ValueNode arg6, ValueNode arg7) { |
|
440 |
if (arg3.isConstant()) { |
|
441 |
b.addPush(JavaKind.Int, |
|
442 |
new StringIndexOfNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), arg1, arg2, arg3, arg4, arg5, arg6, arg7)); |
|
443 |
return true; |
|
444 |
} |
|
445 |
return false; |
|
446 |
} |
|
447 |
} |
|
448 |
||
43972 | 449 |
public static class UnsignedMathPlugin implements InvocationPlugin { |
450 |
private final Condition condition; |
|
451 |
||
452 |
public UnsignedMathPlugin(Condition condition) { |
|
453 |
this.condition = condition; |
|
454 |
} |
|
455 |
||
456 |
@Override |
|
457 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
458 |
// the mirroring and negation operations get the condition into canonical form |
|
459 |
boolean mirror = condition.canonicalMirror(); |
|
460 |
boolean negate = condition.canonicalNegate(); |
|
461 |
StructuredGraph graph = b.getGraph(); |
|
462 |
||
463 |
ValueNode lhs = mirror ? y : x; |
|
464 |
ValueNode rhs = mirror ? x : y; |
|
465 |
||
466 |
ValueNode trueValue = ConstantNode.forBoolean(!negate, graph); |
|
467 |
ValueNode falseValue = ConstantNode.forBoolean(negate, graph); |
|
468 |
||
469 |
Condition cond = mirror ? condition.mirror() : condition; |
|
470 |
if (negate) { |
|
471 |
cond = cond.negate(); |
|
472 |
} |
|
473 |
||
46393 | 474 |
LogicNode compare = CompareNode.createCompareNode(graph, b.getConstantReflection(), b.getMetaAccess(), b.getOptions(), null, cond, lhs, rhs); |
43972 | 475 |
b.addPush(JavaKind.Boolean, new ConditionalNode(compare, trueValue, falseValue)); |
476 |
return true; |
|
477 |
} |
|
478 |
} |
|
479 |
||
480 |
private static void registerUnsignedMathPlugins(InvocationPlugins plugins) { |
|
481 |
Registration r = new Registration(plugins, UnsignedMath.class); |
|
482 |
r.register2("aboveThan", int.class, int.class, new UnsignedMathPlugin(Condition.AT)); |
|
483 |
r.register2("aboveThan", long.class, long.class, new UnsignedMathPlugin(Condition.AT)); |
|
484 |
r.register2("belowThan", int.class, int.class, new UnsignedMathPlugin(Condition.BT)); |
|
485 |
r.register2("belowThan", long.class, long.class, new UnsignedMathPlugin(Condition.BT)); |
|
486 |
r.register2("aboveOrEqual", int.class, int.class, new UnsignedMathPlugin(Condition.AE)); |
|
487 |
r.register2("aboveOrEqual", long.class, long.class, new UnsignedMathPlugin(Condition.AE)); |
|
488 |
r.register2("belowOrEqual", int.class, int.class, new UnsignedMathPlugin(Condition.BE)); |
|
489 |
r.register2("belowOrEqual", long.class, long.class, new UnsignedMathPlugin(Condition.BE)); |
|
490 |
} |
|
491 |
||
492 |
protected static void registerBoxingPlugins(InvocationPlugins plugins) { |
|
493 |
for (JavaKind kind : JavaKind.values()) { |
|
494 |
if (kind.isPrimitive() && kind != JavaKind.Void) { |
|
495 |
new BoxPlugin(kind).register(plugins); |
|
496 |
new UnboxPlugin(kind).register(plugins); |
|
497 |
} |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
private static void registerObjectPlugins(InvocationPlugins plugins) { |
|
502 |
Registration r = new Registration(plugins, Object.class); |
|
503 |
r.register1("<init>", Receiver.class, new InvocationPlugin() { |
|
504 |
@Override |
|
505 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
506 |
/* |
|
507 |
* Object.<init> is a common instrumentation point so only perform this rewrite if |
|
508 |
* the current definition is the normal empty method with a single return bytecode. |
|
509 |
* The finalizer registration will instead be performed by the BytecodeParser. |
|
510 |
*/ |
|
511 |
if (targetMethod.getCodeSize() == 1) { |
|
512 |
ValueNode object = receiver.get(); |
|
513 |
if (RegisterFinalizerNode.mayHaveFinalizer(object, b.getAssumptions())) { |
|
514 |
b.add(new RegisterFinalizerNode(object)); |
|
515 |
} |
|
516 |
return true; |
|
517 |
} |
|
518 |
return false; |
|
519 |
} |
|
520 |
}); |
|
521 |
r.register1("getClass", Receiver.class, new InvocationPlugin() { |
|
522 |
@Override |
|
523 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
524 |
ValueNode object = receiver.get(); |
|
525 |
ValueNode folded = GetClassNode.tryFold(b.getMetaAccess(), b.getConstantReflection(), GraphUtil.originalValue(object)); |
|
526 |
if (folded != null) { |
|
527 |
b.addPush(JavaKind.Object, folded); |
|
528 |
} else { |
|
529 |
Stamp stamp = StampFactory.objectNonNull(TypeReference.createTrusted(b.getAssumptions(), b.getMetaAccess().lookupJavaType(Class.class))); |
|
530 |
b.addPush(JavaKind.Object, new GetClassNode(stamp, object)); |
|
531 |
} |
|
532 |
return true; |
|
533 |
} |
|
534 |
}); |
|
535 |
} |
|
536 |
||
537 |
private static void registerClassPlugins(InvocationPlugins plugins) { |
|
538 |
Registration r = new Registration(plugins, Class.class); |
|
539 |
r.register2("isInstance", Receiver.class, Object.class, new InvocationPlugin() { |
|
540 |
@Override |
|
541 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver type, ValueNode object) { |
|
46393 | 542 |
LogicNode condition = b.append(InstanceOfDynamicNode.create(b.getAssumptions(), b.getConstantReflection(), type.get(), object, false)); |
543 |
b.push(JavaKind.Boolean, b.append(new ConditionalNode(condition).canonical(null))); |
|
43972 | 544 |
return true; |
545 |
} |
|
546 |
}); |
|
547 |
r.register2("isAssignableFrom", Receiver.class, Class.class, new InvocationPlugin() { |
|
548 |
@Override |
|
549 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver type, ValueNode otherType) { |
|
46393 | 550 |
ClassIsAssignableFromNode condition = b.append(new ClassIsAssignableFromNode(type.get(), otherType)); |
551 |
b.push(JavaKind.Boolean, b.append(new ConditionalNode(condition).canonical(null))); |
|
43972 | 552 |
return true; |
553 |
} |
|
554 |
}); |
|
555 |
} |
|
556 |
||
557 |
/** |
|
558 |
* Substitutions for improving the performance of some critical methods in {@link Edges}. These |
|
559 |
* substitutions improve the performance by forcing the relevant methods to be inlined |
|
560 |
* (intrinsification being a special form of inlining) and removing a checked cast. |
|
561 |
*/ |
|
562 |
private static void registerEdgesPlugins(MetaAccessProvider metaAccess, InvocationPlugins plugins) { |
|
563 |
Registration r = new Registration(plugins, Edges.class); |
|
564 |
for (Class<?> c : new Class<?>[]{Node.class, NodeList.class}) { |
|
565 |
r.register2("get" + c.getSimpleName() + "Unsafe", Node.class, long.class, new InvocationPlugin() { |
|
566 |
@Override |
|
567 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode node, ValueNode offset) { |
|
46371 | 568 |
ObjectStamp stamp = StampFactory.object(TypeReference.createTrusted(b.getAssumptions(), metaAccess.lookupJavaType(c))); |
569 |
RawLoadNode value = b.add(new RawLoadNode(stamp, node, offset, LocationIdentity.any(), JavaKind.Object)); |
|
43972 | 570 |
b.addPush(JavaKind.Object, value); |
571 |
return true; |
|
572 |
} |
|
573 |
}); |
|
574 |
r.register3("put" + c.getSimpleName() + "Unsafe", Node.class, long.class, c, new InvocationPlugin() { |
|
575 |
@Override |
|
576 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode node, ValueNode offset, ValueNode value) { |
|
46344 | 577 |
b.add(new RawStoreNode(node, offset, value, JavaKind.Object, LocationIdentity.any())); |
43972 | 578 |
return true; |
579 |
} |
|
580 |
}); |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
public static class BoxPlugin implements InvocationPlugin { |
|
585 |
||
586 |
private final JavaKind kind; |
|
587 |
||
588 |
BoxPlugin(JavaKind kind) { |
|
589 |
this.kind = kind; |
|
590 |
} |
|
591 |
||
592 |
@Override |
|
593 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
594 |
if (b.parsingIntrinsic()) { |
|
595 |
ResolvedJavaMethod rootMethod = b.getGraph().method(); |
|
596 |
if (b.getMetaAccess().lookupJavaType(BoxingSnippets.class).isAssignableFrom(rootMethod.getDeclaringClass())) { |
|
597 |
// Disable invocation plugins for boxing snippets so that the |
|
598 |
// original JDK methods are inlined |
|
599 |
return false; |
|
600 |
} |
|
601 |
} |
|
602 |
ResolvedJavaType resultType = b.getMetaAccess().lookupJavaType(kind.toBoxedJavaClass()); |
|
603 |
b.addPush(JavaKind.Object, new BoxNode(value, resultType, kind)); |
|
604 |
return true; |
|
605 |
} |
|
606 |
||
607 |
void register(InvocationPlugins plugins) { |
|
608 |
plugins.register(this, kind.toBoxedJavaClass(), "valueOf", kind.toJavaClass()); |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
public static class UnboxPlugin implements InvocationPlugin { |
|
613 |
||
614 |
private final JavaKind kind; |
|
615 |
||
616 |
UnboxPlugin(JavaKind kind) { |
|
617 |
this.kind = kind; |
|
618 |
} |
|
619 |
||
620 |
@Override |
|
621 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
622 |
if (b.parsingIntrinsic()) { |
|
623 |
ResolvedJavaMethod rootMethod = b.getGraph().method(); |
|
624 |
if (b.getMetaAccess().lookupJavaType(BoxingSnippets.class).isAssignableFrom(rootMethod.getDeclaringClass())) { |
|
625 |
// Disable invocation plugins for unboxing snippets so that the |
|
626 |
// original JDK methods are inlined |
|
627 |
return false; |
|
628 |
} |
|
629 |
} |
|
630 |
ValueNode valueNode = UnboxNode.create(b.getMetaAccess(), b.getConstantReflection(), receiver.get(), kind); |
|
631 |
b.addPush(kind, valueNode); |
|
632 |
return true; |
|
633 |
} |
|
634 |
||
635 |
void register(InvocationPlugins plugins) { |
|
636 |
String name = kind.toJavaClass().getSimpleName() + "Value"; |
|
637 |
plugins.register(this, kind.toBoxedJavaClass(), name, Receiver.class); |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
public static class UnsafeGetPlugin implements InvocationPlugin { |
|
642 |
||
643 |
private final JavaKind returnKind; |
|
644 |
private final boolean isVolatile; |
|
645 |
||
646 |
public UnsafeGetPlugin(JavaKind returnKind, boolean isVolatile) { |
|
647 |
this.returnKind = returnKind; |
|
648 |
this.isVolatile = isVolatile; |
|
649 |
} |
|
650 |
||
651 |
@Override |
|
652 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode address) { |
|
653 |
// Emits a null-check for the otherwise unused receiver |
|
654 |
unsafe.get(); |
|
655 |
b.addPush(returnKind, new UnsafeMemoryLoadNode(address, returnKind, LocationIdentity.any())); |
|
656 |
b.getGraph().markUnsafeAccess(); |
|
657 |
return true; |
|
658 |
} |
|
659 |
||
660 |
@Override |
|
661 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset) { |
|
662 |
// Emits a null-check for the otherwise unused receiver |
|
663 |
unsafe.get(); |
|
664 |
if (isVolatile) { |
|
665 |
b.add(new MembarNode(JMM_PRE_VOLATILE_READ)); |
|
666 |
} |
|
46344 | 667 |
b.addPush(returnKind, new RawLoadNode(object, offset, returnKind, LocationIdentity.any())); |
43972 | 668 |
if (isVolatile) { |
669 |
b.add(new MembarNode(JMM_POST_VOLATILE_READ)); |
|
670 |
} |
|
671 |
b.getGraph().markUnsafeAccess(); |
|
672 |
return true; |
|
673 |
} |
|
674 |
} |
|
675 |
||
676 |
public static class UnsafePutPlugin implements InvocationPlugin { |
|
677 |
||
678 |
private final JavaKind kind; |
|
679 |
private final boolean isVolatile; |
|
680 |
||
681 |
public UnsafePutPlugin(JavaKind kind, boolean isVolatile) { |
|
682 |
this.kind = kind; |
|
683 |
this.isVolatile = isVolatile; |
|
684 |
} |
|
685 |
||
686 |
@Override |
|
687 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode address, ValueNode value) { |
|
688 |
// Emits a null-check for the otherwise unused receiver |
|
689 |
unsafe.get(); |
|
690 |
b.add(new UnsafeMemoryStoreNode(address, value, kind, LocationIdentity.any())); |
|
691 |
b.getGraph().markUnsafeAccess(); |
|
692 |
return true; |
|
693 |
} |
|
694 |
||
695 |
@Override |
|
696 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { |
|
697 |
// Emits a null-check for the otherwise unused receiver |
|
698 |
unsafe.get(); |
|
699 |
if (isVolatile) { |
|
700 |
b.add(new MembarNode(JMM_PRE_VOLATILE_WRITE)); |
|
701 |
} |
|
46344 | 702 |
b.add(new RawStoreNode(object, offset, value, kind, LocationIdentity.any())); |
43972 | 703 |
if (isVolatile) { |
704 |
b.add(new MembarNode(JMM_POST_VOLATILE_WRITE)); |
|
705 |
} |
|
706 |
b.getGraph().markUnsafeAccess(); |
|
707 |
return true; |
|
708 |
} |
|
709 |
} |
|
710 |
||
711 |
public static class UnsafeFencePlugin implements InvocationPlugin { |
|
712 |
||
713 |
private final int barriers; |
|
714 |
||
715 |
public UnsafeFencePlugin(int barriers) { |
|
716 |
this.barriers = barriers; |
|
717 |
} |
|
718 |
||
719 |
@Override |
|
720 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe) { |
|
721 |
// Emits a null-check for the otherwise unused receiver |
|
722 |
unsafe.get(); |
|
723 |
b.add(new MembarNode(barriers)); |
|
724 |
return true; |
|
725 |
} |
|
726 |
} |
|
727 |
||
728 |
private static void registerGraalDirectivesPlugins(InvocationPlugins plugins) { |
|
729 |
Registration r = new Registration(plugins, GraalDirectives.class); |
|
730 |
r.register0("deoptimize", new InvocationPlugin() { |
|
731 |
@Override |
|
732 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
733 |
b.add(new DeoptimizeNode(DeoptimizationAction.None, DeoptimizationReason.TransferToInterpreter)); |
|
734 |
return true; |
|
735 |
} |
|
736 |
}); |
|
737 |
||
738 |
r.register0("deoptimizeAndInvalidate", new InvocationPlugin() { |
|
739 |
@Override |
|
740 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
741 |
b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter)); |
|
742 |
return true; |
|
743 |
} |
|
744 |
}); |
|
745 |
||
746 |
r.register0("inCompiledCode", new InvocationPlugin() { |
|
747 |
@Override |
|
748 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
749 |
b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(true)); |
|
750 |
return true; |
|
751 |
} |
|
752 |
}); |
|
753 |
||
754 |
r.register0("controlFlowAnchor", new InvocationPlugin() { |
|
755 |
@Override |
|
756 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
757 |
b.add(new ControlFlowAnchorNode()); |
|
758 |
return true; |
|
759 |
} |
|
760 |
}); |
|
761 |
||
762 |
r.register2("injectBranchProbability", double.class, boolean.class, new InvocationPlugin() { |
|
763 |
@Override |
|
764 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode probability, ValueNode condition) { |
|
765 |
b.addPush(JavaKind.Boolean, new BranchProbabilityNode(probability, condition)); |
|
766 |
return true; |
|
767 |
} |
|
768 |
}); |
|
769 |
||
770 |
InvocationPlugin blackholePlugin = new InvocationPlugin() { |
|
771 |
@Override |
|
772 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
773 |
b.add(new BlackholeNode(value)); |
|
774 |
return true; |
|
775 |
} |
|
776 |
}; |
|
777 |
||
778 |
InvocationPlugin bindToRegisterPlugin = new InvocationPlugin() { |
|
779 |
@Override |
|
780 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
781 |
b.add(new BindToRegisterNode(value)); |
|
782 |
return true; |
|
783 |
} |
|
784 |
}; |
|
785 |
for (JavaKind kind : JavaKind.values()) { |
|
786 |
if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { |
|
787 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); |
|
788 |
r.register1("blackhole", javaClass, blackholePlugin); |
|
789 |
r.register1("bindToRegister", javaClass, bindToRegisterPlugin); |
|
790 |
||
791 |
r.register1("opaque", javaClass, new InvocationPlugin() { |
|
792 |
@Override |
|
793 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
794 |
b.addPush(kind, new OpaqueNode(value)); |
|
795 |
return true; |
|
796 |
} |
|
797 |
}); |
|
798 |
} |
|
799 |
} |
|
800 |
||
801 |
InvocationPlugin spillPlugin = new InvocationPlugin() { |
|
802 |
@Override |
|
803 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
804 |
b.add(new SpillRegistersNode()); |
|
805 |
return true; |
|
806 |
} |
|
807 |
}; |
|
808 |
r.register0("spillRegisters", spillPlugin); |
|
809 |
||
810 |
r.register1("guardingNonNull", Object.class, new InvocationPlugin() { |
|
811 |
@Override |
|
812 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46344 | 813 |
b.addPush(value.getStackKind(), b.nullCheckedValue(value)); |
43972 | 814 |
return true; |
815 |
} |
|
816 |
}); |
|
817 |
||
818 |
r.register1("ensureVirtualized", Object.class, new InvocationPlugin() { |
|
819 |
@Override |
|
820 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) { |
|
821 |
b.add(new EnsureVirtualizedNode(object, false)); |
|
822 |
return true; |
|
823 |
} |
|
824 |
}); |
|
825 |
r.register1("ensureVirtualizedHere", Object.class, new InvocationPlugin() { |
|
826 |
@Override |
|
827 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) { |
|
828 |
b.add(new EnsureVirtualizedNode(object, true)); |
|
829 |
return true; |
|
830 |
} |
|
831 |
}); |
|
832 |
} |
|
833 |
||
834 |
private static void registerJMHBlackholePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
835 |
InvocationPlugin blackholePlugin = new InvocationPlugin() { |
|
836 |
@Override |
|
837 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver blackhole, ValueNode value) { |
|
838 |
blackhole.get(); |
|
839 |
b.add(new BlackholeNode(value)); |
|
840 |
return true; |
|
841 |
} |
|
842 |
}; |
|
843 |
String[] names = {"org.openjdk.jmh.infra.Blackhole", "org.openjdk.jmh.logic.BlackHole"}; |
|
844 |
for (String name : names) { |
|
845 |
Registration r = new Registration(plugins, name, bytecodeProvider); |
|
846 |
for (JavaKind kind : JavaKind.values()) { |
|
847 |
if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { |
|
848 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); |
|
46344 | 849 |
r.registerOptional2("consume", Receiver.class, javaClass, blackholePlugin); |
43972 | 850 |
} |
851 |
} |
|
46344 | 852 |
r.registerOptional2("consume", Receiver.class, Object[].class, blackholePlugin); |
43972 | 853 |
} |
854 |
} |
|
855 |
||
856 |
private static void registerJFRThrowablePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
857 |
Registration r = new Registration(plugins, "oracle.jrockit.jfr.jdkevents.ThrowableTracer", bytecodeProvider); |
|
858 |
r.register2("traceThrowable", Throwable.class, String.class, new InvocationPlugin() { |
|
859 |
@Override |
|
860 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode throwable, ValueNode message) { |
|
861 |
b.add(new VirtualizableInvokeMacroNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), throwable, message)); |
|
862 |
return true; |
|
863 |
} |
|
864 |
||
865 |
@Override |
|
866 |
public boolean inlineOnly() { |
|
867 |
return true; |
|
868 |
} |
|
869 |
}); |
|
870 |
} |
|
871 |
||
872 |
private static void registerMethodHandleImplPlugins(InvocationPlugins plugins, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider) { |
|
873 |
Registration r = new Registration(plugins, "java.lang.invoke.MethodHandleImpl", bytecodeProvider); |
|
874 |
r.register2("profileBoolean", boolean.class, int[].class, new InvocationPlugin() { |
|
875 |
@Override |
|
876 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode result, ValueNode counters) { |
|
877 |
if (result.isConstant()) { |
|
878 |
b.push(JavaKind.Boolean, result); |
|
879 |
return true; |
|
880 |
} |
|
881 |
if (counters.isConstant()) { |
|
882 |
ValueNode newResult = result; |
|
883 |
int[] ctrs = snippetReflection.asObject(int[].class, (JavaConstant) counters.asConstant()); |
|
884 |
if (ctrs != null && ctrs.length == 2) { |
|
885 |
int falseCount = ctrs[0]; |
|
886 |
int trueCount = ctrs[1]; |
|
887 |
int totalCount = trueCount + falseCount; |
|
888 |
||
889 |
if (totalCount == 0) { |
|
890 |
b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter)); |
|
891 |
} else if (falseCount == 0 || trueCount == 0) { |
|
46371 | 892 |
boolean expected = falseCount == 0; |
46393 | 893 |
LogicNode condition = b.addWithInputs( |
894 |
IntegerEqualsNode.create(b.getConstantReflection(), b.getMetaAccess(), b.getOptions(), null, result, b.add(ConstantNode.forBoolean(!expected)))); |
|
43972 | 895 |
b.append(new FixedGuardNode(condition, DeoptimizationReason.UnreachedCode, DeoptimizationAction.InvalidateReprofile, true)); |
896 |
newResult = b.add(ConstantNode.forBoolean(expected)); |
|
897 |
} else { |
|
898 |
// We cannot use BranchProbabilityNode here since there's no guarantee |
|
899 |
// the result of MethodHandleImpl.profileBoolean() is used as the |
|
900 |
// test in an `if` statement (as required by BranchProbabilityNode). |
|
901 |
} |
|
902 |
} |
|
903 |
b.addPush(JavaKind.Boolean, newResult); |
|
904 |
return true; |
|
905 |
} |
|
906 |
return false; |
|
907 |
} |
|
908 |
}); |
|
909 |
} |
|
910 |
} |