author | jwilhelm |
Wed, 20 Mar 2019 22:53:44 +0100 | |
changeset 54204 | 55c262f4f5a1 |
parent 54084 | 84f10bbf993f |
child 54914 | 9feb4852536f |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
52910 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
43972 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
50858 | 23 |
|
24 |
||
43972 | 25 |
package org.graalvm.compiler.replacements.amd64; |
26 |
||
50858 | 27 |
import static org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.registerPlatformSpecificUnsafePlugins; |
43972 | 28 |
import static org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation.POW; |
29 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.COS; |
|
30 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.EXP; |
|
31 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG; |
|
32 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG10; |
|
33 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.SIN; |
|
34 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.TAN; |
|
54084 | 35 |
import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.Java11OrEarlier; |
36 |
import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.Java8OrEarlier; |
|
43972 | 37 |
|
46963 | 38 |
import java.util.Arrays; |
39 |
||
43972 | 40 |
import org.graalvm.compiler.bytecode.BytecodeProvider; |
41 |
import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool.RoundingMode; |
|
52910 | 42 |
import org.graalvm.compiler.nodes.PauseNode; |
43972 | 43 |
import org.graalvm.compiler.nodes.ValueNode; |
44 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; |
|
45 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
|
46 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; |
|
47 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver; |
|
48 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; |
|
49 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; |
|
50 |
import org.graalvm.compiler.nodes.java.AtomicReadAndAddNode; |
|
51 |
import org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode; |
|
52 |
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; |
|
46963 | 53 |
import org.graalvm.compiler.replacements.ArraysSubstitutions; |
43972 | 54 |
import org.graalvm.compiler.replacements.IntegerSubstitutions; |
55 |
import org.graalvm.compiler.replacements.LongSubstitutions; |
|
50858 | 56 |
import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeAccessPlugin; |
43972 | 57 |
import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeGetPlugin; |
58 |
import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafePutPlugin; |
|
59 |
import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode; |
|
60 |
import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation; |
|
61 |
import org.graalvm.compiler.replacements.nodes.BitCountNode; |
|
62 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode; |
|
63 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation; |
|
64 |
||
65 |
import jdk.vm.ci.amd64.AMD64; |
|
66 |
import jdk.vm.ci.amd64.AMD64.CPUFeature; |
|
67 |
import jdk.vm.ci.meta.JavaKind; |
|
68 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
69 |
import sun.misc.Unsafe; |
|
70 |
||
71 |
public class AMD64GraphBuilderPlugins { |
|
72 |
||
54204 | 73 |
public static void register(Plugins plugins, BytecodeProvider replacementsBytecodeProvider, AMD64 arch, boolean explicitUnsafeNullChecks, boolean emitJDK9StringSubstitutions) { |
43972 | 74 |
InvocationPlugins invocationPlugins = plugins.getInvocationPlugins(); |
75 |
invocationPlugins.defer(new Runnable() { |
|
76 |
@Override |
|
77 |
public void run() { |
|
52910 | 78 |
registerThreadPlugins(invocationPlugins, arch); |
43972 | 79 |
registerIntegerLongPlugins(invocationPlugins, IntegerSubstitutions.class, JavaKind.Int, arch, replacementsBytecodeProvider); |
80 |
registerIntegerLongPlugins(invocationPlugins, LongSubstitutions.class, JavaKind.Long, arch, replacementsBytecodeProvider); |
|
50858 | 81 |
registerPlatformSpecificUnsafePlugins(invocationPlugins, replacementsBytecodeProvider, explicitUnsafeNullChecks, |
50330 | 82 |
new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object, JavaKind.Boolean, JavaKind.Byte, JavaKind.Short, JavaKind.Char, JavaKind.Float, JavaKind.Double}); |
50858 | 83 |
registerUnsafePlugins(invocationPlugins, replacementsBytecodeProvider, explicitUnsafeNullChecks); |
54084 | 84 |
registerStringPlugins(invocationPlugins, replacementsBytecodeProvider); |
54204 | 85 |
if (emitJDK9StringSubstitutions) { |
86 |
registerStringLatin1Plugins(invocationPlugins, replacementsBytecodeProvider); |
|
87 |
registerStringUTF16Plugins(invocationPlugins, replacementsBytecodeProvider); |
|
88 |
} |
|
54084 | 89 |
registerMathPlugins(invocationPlugins, arch, replacementsBytecodeProvider); |
46963 | 90 |
registerArraysEqualsPlugins(invocationPlugins, replacementsBytecodeProvider); |
43972 | 91 |
} |
92 |
}); |
|
93 |
} |
|
94 |
||
52910 | 95 |
private static void registerThreadPlugins(InvocationPlugins plugins, AMD64 arch) { |
96 |
if (!Java8OrEarlier) { |
|
97 |
// Pause instruction introduced with SSE2 |
|
98 |
if (arch.getFeatures().contains(AMD64.CPUFeature.SSE2)) { |
|
99 |
Registration r = new Registration(plugins, Thread.class); |
|
100 |
r.register0("onSpinWait", new InvocationPlugin() { |
|
101 |
@Override |
|
102 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { |
|
103 |
b.append(new PauseNode()); |
|
104 |
return true; |
|
105 |
} |
|
106 |
}); |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
||
43972 | 111 |
private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, AMD64 arch, BytecodeProvider bytecodeProvider) { |
112 |
Class<?> declaringClass = kind.toBoxedJavaClass(); |
|
113 |
Class<?> type = kind.toJavaClass(); |
|
114 |
Registration r = new Registration(plugins, declaringClass, bytecodeProvider); |
|
115 |
if (arch.getFeatures().contains(AMD64.CPUFeature.LZCNT) && arch.getFlags().contains(AMD64.Flag.UseCountLeadingZerosInstruction)) { |
|
116 |
r.register1("numberOfLeadingZeros", type, new InvocationPlugin() { |
|
117 |
@Override |
|
118 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
119 |
ValueNode folded = AMD64CountLeadingZerosNode.tryFold(value); |
|
120 |
if (folded != null) { |
|
121 |
b.addPush(JavaKind.Int, folded); |
|
122 |
} else { |
|
123 |
b.addPush(JavaKind.Int, new AMD64CountLeadingZerosNode(value)); |
|
124 |
} |
|
125 |
return true; |
|
126 |
} |
|
127 |
}); |
|
128 |
} else { |
|
129 |
r.registerMethodSubstitution(substituteDeclaringClass, "numberOfLeadingZeros", type); |
|
130 |
} |
|
131 |
if (arch.getFeatures().contains(AMD64.CPUFeature.BMI1) && arch.getFlags().contains(AMD64.Flag.UseCountTrailingZerosInstruction)) { |
|
132 |
r.register1("numberOfTrailingZeros", type, new InvocationPlugin() { |
|
133 |
@Override |
|
134 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
135 |
ValueNode folded = AMD64CountTrailingZerosNode.tryFold(value); |
|
136 |
if (folded != null) { |
|
137 |
b.addPush(JavaKind.Int, folded); |
|
138 |
} else { |
|
139 |
b.addPush(JavaKind.Int, new AMD64CountTrailingZerosNode(value)); |
|
140 |
} |
|
141 |
return true; |
|
142 |
} |
|
143 |
}); |
|
144 |
} else { |
|
145 |
r.registerMethodSubstitution(substituteDeclaringClass, "numberOfTrailingZeros", type); |
|
146 |
} |
|
147 |
||
148 |
if (arch.getFeatures().contains(AMD64.CPUFeature.POPCNT)) { |
|
149 |
r.register1("bitCount", type, new InvocationPlugin() { |
|
150 |
@Override |
|
151 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 152 |
b.push(JavaKind.Int, b.append(new BitCountNode(value).canonical(null))); |
43972 | 153 |
return true; |
154 |
} |
|
155 |
}); |
|
156 |
} |
|
157 |
} |
|
158 |
||
54084 | 159 |
private static void registerMathPlugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider bytecodeProvider) { |
43972 | 160 |
Registration r = new Registration(plugins, Math.class, bytecodeProvider); |
161 |
registerUnaryMath(r, "log", LOG); |
|
162 |
registerUnaryMath(r, "log10", LOG10); |
|
163 |
registerUnaryMath(r, "exp", EXP); |
|
164 |
registerBinaryMath(r, "pow", POW); |
|
54084 | 165 |
registerUnaryMath(r, "sin", SIN); |
166 |
registerUnaryMath(r, "cos", COS); |
|
167 |
registerUnaryMath(r, "tan", TAN); |
|
43972 | 168 |
|
169 |
if (arch.getFeatures().contains(CPUFeature.SSE4_1)) { |
|
170 |
registerRound(r, "rint", RoundingMode.NEAREST); |
|
171 |
registerRound(r, "ceil", RoundingMode.UP); |
|
172 |
registerRound(r, "floor", RoundingMode.DOWN); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) { |
|
177 |
r.register1(name, Double.TYPE, new InvocationPlugin() { |
|
178 |
@Override |
|
179 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 180 |
b.push(JavaKind.Double, b.append(UnaryMathIntrinsicNode.create(value, operation))); |
43972 | 181 |
return true; |
182 |
} |
|
183 |
}); |
|
184 |
} |
|
185 |
||
186 |
private static void registerBinaryMath(Registration r, String name, BinaryOperation operation) { |
|
187 |
r.register2(name, Double.TYPE, Double.TYPE, new InvocationPlugin() { |
|
188 |
@Override |
|
189 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
46393 | 190 |
b.push(JavaKind.Double, b.append(BinaryMathIntrinsicNode.create(x, y, operation))); |
43972 | 191 |
return true; |
192 |
} |
|
193 |
}); |
|
194 |
} |
|
195 |
||
196 |
private static void registerRound(Registration r, String name, RoundingMode mode) { |
|
197 |
r.register1(name, Double.TYPE, new InvocationPlugin() { |
|
198 |
@Override |
|
199 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) { |
|
200 |
b.push(JavaKind.Double, b.append(new AMD64RoundNode(arg, mode))); |
|
201 |
return true; |
|
202 |
} |
|
203 |
}); |
|
204 |
} |
|
205 |
||
54084 | 206 |
private static void registerStringPlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) { |
49451 | 207 |
if (Java8OrEarlier) { |
46344 | 208 |
Registration r; |
209 |
r = new Registration(plugins, String.class, replacementsBytecodeProvider); |
|
210 |
r.setAllowOverwrite(true); |
|
54084 | 211 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", char[].class, int.class, |
212 |
int.class, char[].class, int.class, int.class, int.class); |
|
213 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", Receiver.class, int.class, int.class); |
|
49873 | 214 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "compareTo", Receiver.class, String.class); |
49451 | 215 |
} |
216 |
} |
|
217 |
||
54084 | 218 |
private static void registerStringLatin1Plugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) { |
54204 | 219 |
Registration r = new Registration(plugins, "java.lang.StringLatin1", replacementsBytecodeProvider); |
220 |
r.setAllowOverwrite(true); |
|
221 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "compareTo", byte[].class, byte[].class); |
|
222 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "compareToUTF16", byte[].class, byte[].class); |
|
223 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "inflate", byte[].class, int.class, char[].class, int.class, int.class); |
|
224 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "inflate", byte[].class, int.class, byte[].class, int.class, int.class); |
|
225 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "indexOf", byte[].class, int.class, int.class); |
|
226 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "indexOf", byte[].class, int.class, byte[].class, int.class, int.class); |
|
49451 | 227 |
} |
228 |
||
54084 | 229 |
private static void registerStringUTF16Plugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) { |
54204 | 230 |
Registration r = new Registration(plugins, "java.lang.StringUTF16", replacementsBytecodeProvider); |
231 |
r.setAllowOverwrite(true); |
|
232 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compareTo", byte[].class, byte[].class); |
|
233 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compareToLatin1", byte[].class, byte[].class); |
|
234 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compress", char[].class, int.class, byte[].class, int.class, int.class); |
|
235 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compress", byte[].class, int.class, byte[].class, int.class, int.class); |
|
236 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "indexOfCharUnsafe", byte[].class, int.class, int.class, int.class); |
|
237 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "indexOfUnsafe", byte[].class, int.class, byte[].class, int.class, int.class); |
|
238 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "indexOfLatin1Unsafe", byte[].class, int.class, byte[].class, int.class, int.class); |
|
46344 | 239 |
} |
240 |
||
50858 | 241 |
private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider, boolean explicitUnsafeNullChecks) { |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
242 |
registerUnsafePlugins(new Registration(plugins, Unsafe.class), explicitUnsafeNullChecks, new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}, true); |
50609 | 243 |
if (!Java8OrEarlier) { |
50858 | 244 |
registerUnsafePlugins(new Registration(plugins, "jdk.internal.misc.Unsafe", replacementsBytecodeProvider), explicitUnsafeNullChecks, |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
245 |
new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long, JavaKind.Object}, Java11OrEarlier); |
43972 | 246 |
} |
50609 | 247 |
} |
50330 | 248 |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
249 |
private static void registerUnsafePlugins(Registration r, boolean explicitUnsafeNullChecks, JavaKind[] unsafeJavaKinds, boolean java11OrEarlier) { |
50330 | 250 |
for (JavaKind kind : unsafeJavaKinds) { |
43972 | 251 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
252 |
String kindName = (kind == JavaKind.Object && !java11OrEarlier) ? "Reference" : kind.name(); |
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
253 |
r.register4("getAndSet" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks) { |
43972 | 254 |
@Override |
255 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { |
|
256 |
// Emits a null-check for the otherwise unused receiver |
|
257 |
unsafe.get(); |
|
50858 | 258 |
createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndWriteNode(obj, offset, value, kind, loc)); |
43972 | 259 |
return true; |
260 |
} |
|
261 |
}); |
|
50330 | 262 |
if (kind != JavaKind.Boolean && kind.isNumericInteger()) { |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
263 |
r.register4("getAndAdd" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks) { |
43972 | 264 |
@Override |
265 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) { |
|
266 |
// Emits a null-check for the otherwise unused receiver |
|
267 |
unsafe.get(); |
|
50858 | 268 |
createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndAddNode(b.add(new OffsetAddressNode(obj, offset)), delta, kind, loc)); |
43972 | 269 |
return true; |
270 |
} |
|
271 |
}); |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
for (JavaKind kind : new JavaKind[]{JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long}) { |
|
276 |
Class<?> javaClass = kind.toJavaClass(); |
|
50858 | 277 |
r.registerOptional3("get" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, explicitUnsafeNullChecks)); |
278 |
r.registerOptional4("put" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, explicitUnsafeNullChecks)); |
|
43972 | 279 |
} |
280 |
} |
|
46963 | 281 |
|
282 |
private static void registerArraysEqualsPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
283 |
Registration r = new Registration(plugins, Arrays.class, bytecodeProvider); |
|
284 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", float[].class, float[].class); |
|
285 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", double[].class, double[].class); |
|
286 |
} |
|
43972 | 287 |
} |