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