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