author | dlong |
Thu, 15 Nov 2018 09:04:07 -0800 | |
changeset 52578 | 7dd81e82d083 |
parent 52220 | 9c260a6b6471 |
child 52910 | 583fd71c47d6 |
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 |
*/ |
|
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; |
|
49873 | 35 |
import static org.graalvm.compiler.serviceprovider.GraalServices.JAVA_SPECIFICATION_VERSION; |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
36 |
import static org.graalvm.compiler.serviceprovider.GraalServices.Java11OrEarlier; |
49873 | 37 |
import static org.graalvm.compiler.serviceprovider.GraalServices.Java8OrEarlier; |
43972 | 38 |
|
46963 | 39 |
import java.util.Arrays; |
40 |
||
43972 | 41 |
import org.graalvm.compiler.bytecode.BytecodeProvider; |
42 |
import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool.RoundingMode; |
|
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 |
||
50858 | 73 |
public static void register(Plugins plugins, BytecodeProvider replacementsBytecodeProvider, AMD64 arch, boolean arithmeticStubs, boolean explicitUnsafeNullChecks) { |
43972 | 74 |
InvocationPlugins invocationPlugins = plugins.getInvocationPlugins(); |
75 |
invocationPlugins.defer(new Runnable() { |
|
76 |
@Override |
|
77 |
public void run() { |
|
78 |
registerIntegerLongPlugins(invocationPlugins, IntegerSubstitutions.class, JavaKind.Int, arch, replacementsBytecodeProvider); |
|
79 |
registerIntegerLongPlugins(invocationPlugins, LongSubstitutions.class, JavaKind.Long, arch, replacementsBytecodeProvider); |
|
50858 | 80 |
registerPlatformSpecificUnsafePlugins(invocationPlugins, replacementsBytecodeProvider, explicitUnsafeNullChecks, |
50330 | 81 |
new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object, JavaKind.Boolean, JavaKind.Byte, JavaKind.Short, JavaKind.Char, JavaKind.Float, JavaKind.Double}); |
50858 | 82 |
registerUnsafePlugins(invocationPlugins, replacementsBytecodeProvider, explicitUnsafeNullChecks); |
46344 | 83 |
registerStringPlugins(invocationPlugins, arch, replacementsBytecodeProvider); |
51436 | 84 |
registerStringLatin1Plugins(invocationPlugins, arch, replacementsBytecodeProvider); |
85 |
registerStringUTF16Plugins(invocationPlugins, arch, replacementsBytecodeProvider); |
|
43972 | 86 |
registerMathPlugins(invocationPlugins, arch, arithmeticStubs, replacementsBytecodeProvider); |
46963 | 87 |
registerArraysEqualsPlugins(invocationPlugins, replacementsBytecodeProvider); |
43972 | 88 |
} |
89 |
}); |
|
90 |
} |
|
91 |
||
92 |
private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, AMD64 arch, BytecodeProvider bytecodeProvider) { |
|
93 |
Class<?> declaringClass = kind.toBoxedJavaClass(); |
|
94 |
Class<?> type = kind.toJavaClass(); |
|
95 |
Registration r = new Registration(plugins, declaringClass, bytecodeProvider); |
|
96 |
if (arch.getFeatures().contains(AMD64.CPUFeature.LZCNT) && arch.getFlags().contains(AMD64.Flag.UseCountLeadingZerosInstruction)) { |
|
97 |
r.register1("numberOfLeadingZeros", type, new InvocationPlugin() { |
|
98 |
@Override |
|
99 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
100 |
ValueNode folded = AMD64CountLeadingZerosNode.tryFold(value); |
|
101 |
if (folded != null) { |
|
102 |
b.addPush(JavaKind.Int, folded); |
|
103 |
} else { |
|
104 |
b.addPush(JavaKind.Int, new AMD64CountLeadingZerosNode(value)); |
|
105 |
} |
|
106 |
return true; |
|
107 |
} |
|
108 |
}); |
|
109 |
} else { |
|
110 |
r.registerMethodSubstitution(substituteDeclaringClass, "numberOfLeadingZeros", type); |
|
111 |
} |
|
112 |
if (arch.getFeatures().contains(AMD64.CPUFeature.BMI1) && arch.getFlags().contains(AMD64.Flag.UseCountTrailingZerosInstruction)) { |
|
113 |
r.register1("numberOfTrailingZeros", type, new InvocationPlugin() { |
|
114 |
@Override |
|
115 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
116 |
ValueNode folded = AMD64CountTrailingZerosNode.tryFold(value); |
|
117 |
if (folded != null) { |
|
118 |
b.addPush(JavaKind.Int, folded); |
|
119 |
} else { |
|
120 |
b.addPush(JavaKind.Int, new AMD64CountTrailingZerosNode(value)); |
|
121 |
} |
|
122 |
return true; |
|
123 |
} |
|
124 |
}); |
|
125 |
} else { |
|
126 |
r.registerMethodSubstitution(substituteDeclaringClass, "numberOfTrailingZeros", type); |
|
127 |
} |
|
128 |
||
129 |
if (arch.getFeatures().contains(AMD64.CPUFeature.POPCNT)) { |
|
130 |
r.register1("bitCount", type, new InvocationPlugin() { |
|
131 |
@Override |
|
132 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 133 |
b.push(JavaKind.Int, b.append(new BitCountNode(value).canonical(null))); |
43972 | 134 |
return true; |
135 |
} |
|
136 |
}); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
private static void registerMathPlugins(InvocationPlugins plugins, AMD64 arch, boolean arithmeticStubs, BytecodeProvider bytecodeProvider) { |
|
141 |
Registration r = new Registration(plugins, Math.class, bytecodeProvider); |
|
142 |
registerUnaryMath(r, "log", LOG); |
|
143 |
registerUnaryMath(r, "log10", LOG10); |
|
144 |
registerUnaryMath(r, "exp", EXP); |
|
145 |
registerBinaryMath(r, "pow", POW); |
|
146 |
if (arithmeticStubs) { |
|
147 |
registerUnaryMath(r, "sin", SIN); |
|
148 |
registerUnaryMath(r, "cos", COS); |
|
149 |
registerUnaryMath(r, "tan", TAN); |
|
150 |
} else { |
|
151 |
r.registerMethodSubstitution(AMD64MathSubstitutions.class, "sin", double.class); |
|
152 |
r.registerMethodSubstitution(AMD64MathSubstitutions.class, "cos", double.class); |
|
153 |
r.registerMethodSubstitution(AMD64MathSubstitutions.class, "tan", double.class); |
|
154 |
} |
|
155 |
||
156 |
if (arch.getFeatures().contains(CPUFeature.SSE4_1)) { |
|
157 |
registerRound(r, "rint", RoundingMode.NEAREST); |
|
158 |
registerRound(r, "ceil", RoundingMode.UP); |
|
159 |
registerRound(r, "floor", RoundingMode.DOWN); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) { |
|
164 |
r.register1(name, Double.TYPE, new InvocationPlugin() { |
|
165 |
@Override |
|
166 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 167 |
b.push(JavaKind.Double, b.append(UnaryMathIntrinsicNode.create(value, operation))); |
43972 | 168 |
return true; |
169 |
} |
|
170 |
}); |
|
171 |
} |
|
172 |
||
173 |
private static void registerBinaryMath(Registration r, String name, BinaryOperation operation) { |
|
174 |
r.register2(name, Double.TYPE, Double.TYPE, new InvocationPlugin() { |
|
175 |
@Override |
|
176 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
46393 | 177 |
b.push(JavaKind.Double, b.append(BinaryMathIntrinsicNode.create(x, y, operation))); |
43972 | 178 |
return true; |
179 |
} |
|
180 |
}); |
|
181 |
} |
|
182 |
||
183 |
private static void registerRound(Registration r, String name, RoundingMode mode) { |
|
184 |
r.register1(name, Double.TYPE, new InvocationPlugin() { |
|
185 |
@Override |
|
186 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) { |
|
187 |
b.push(JavaKind.Double, b.append(new AMD64RoundNode(arg, mode))); |
|
188 |
return true; |
|
189 |
} |
|
190 |
}); |
|
191 |
} |
|
192 |
||
46344 | 193 |
private static void registerStringPlugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) { |
49451 | 194 |
if (Java8OrEarlier) { |
46344 | 195 |
Registration r; |
196 |
r = new Registration(plugins, String.class, replacementsBytecodeProvider); |
|
197 |
r.setAllowOverwrite(true); |
|
49451 | 198 |
if (arch.getFeatures().contains(CPUFeature.SSE4_2)) { |
199 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", char[].class, int.class, |
|
200 |
int.class, char[].class, int.class, int.class, int.class); |
|
201 |
} |
|
51436 | 202 |
if (arch.getFeatures().contains(CPUFeature.SSSE3)) { |
203 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", Receiver.class, int.class, int.class); |
|
204 |
} |
|
49873 | 205 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "compareTo", Receiver.class, String.class); |
49451 | 206 |
} |
207 |
} |
|
208 |
||
51436 | 209 |
private static void registerStringLatin1Plugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) { |
49451 | 210 |
if (JAVA_SPECIFICATION_VERSION >= 9) { |
211 |
Registration r = new Registration(plugins, "java.lang.StringLatin1", replacementsBytecodeProvider); |
|
212 |
r.setAllowOverwrite(true); |
|
213 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "compareTo", byte[].class, byte[].class); |
|
214 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "compareToUTF16", byte[].class, byte[].class); |
|
52578 | 215 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "inflate", byte[].class, int.class, char[].class, int.class, int.class); |
216 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "inflate", byte[].class, int.class, byte[].class, int.class, int.class); |
|
217 |
||
51436 | 218 |
if (arch.getFeatures().contains(CPUFeature.SSSE3)) { |
219 |
r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "indexOf", byte[].class, int.class, int.class); |
|
220 |
} |
|
49451 | 221 |
} |
222 |
} |
|
223 |
||
51436 | 224 |
private static void registerStringUTF16Plugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) { |
49451 | 225 |
if (JAVA_SPECIFICATION_VERSION >= 9) { |
226 |
Registration r = new Registration(plugins, "java.lang.StringUTF16", replacementsBytecodeProvider); |
|
227 |
r.setAllowOverwrite(true); |
|
228 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compareTo", byte[].class, byte[].class); |
|
229 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compareToLatin1", byte[].class, byte[].class); |
|
52578 | 230 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compress", char[].class, int.class, byte[].class, int.class, int.class); |
231 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compress", byte[].class, int.class, byte[].class, int.class, int.class); |
|
232 |
||
51436 | 233 |
if (arch.getFeatures().contains(CPUFeature.SSSE3)) { |
234 |
r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "indexOfCharUnsafe", byte[].class, int.class, int.class, int.class); |
|
235 |
} |
|
46344 | 236 |
} |
237 |
} |
|
238 |
||
50858 | 239 |
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
|
240 |
registerUnsafePlugins(new Registration(plugins, Unsafe.class), explicitUnsafeNullChecks, new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}, true); |
50609 | 241 |
if (!Java8OrEarlier) { |
50858 | 242 |
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
|
243 |
new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long, JavaKind.Object}, Java11OrEarlier); |
43972 | 244 |
} |
50609 | 245 |
} |
50330 | 246 |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
247 |
private static void registerUnsafePlugins(Registration r, boolean explicitUnsafeNullChecks, JavaKind[] unsafeJavaKinds, boolean java11OrEarlier) { |
50330 | 248 |
for (JavaKind kind : unsafeJavaKinds) { |
43972 | 249 |
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
|
250 |
String kindName = (kind == JavaKind.Object && !java11OrEarlier) ? "Reference" : kind.name(); |
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
251 |
r.register4("getAndSet" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks) { |
43972 | 252 |
@Override |
253 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { |
|
254 |
// Emits a null-check for the otherwise unused receiver |
|
255 |
unsafe.get(); |
|
50858 | 256 |
createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndWriteNode(obj, offset, value, kind, loc)); |
43972 | 257 |
return true; |
258 |
} |
|
259 |
}); |
|
50330 | 260 |
if (kind != JavaKind.Boolean && kind.isNumericInteger()) { |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51436
diff
changeset
|
261 |
r.register4("getAndAdd" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks) { |
43972 | 262 |
@Override |
263 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) { |
|
264 |
// Emits a null-check for the otherwise unused receiver |
|
265 |
unsafe.get(); |
|
50858 | 266 |
createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndAddNode(b.add(new OffsetAddressNode(obj, offset)), delta, kind, loc)); |
43972 | 267 |
return true; |
268 |
} |
|
269 |
}); |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
for (JavaKind kind : new JavaKind[]{JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long}) { |
|
274 |
Class<?> javaClass = kind.toJavaClass(); |
|
50858 | 275 |
r.registerOptional3("get" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, explicitUnsafeNullChecks)); |
276 |
r.registerOptional4("put" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, explicitUnsafeNullChecks)); |
|
43972 | 277 |
} |
278 |
} |
|
46963 | 279 |
|
280 |
private static void registerArraysEqualsPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { |
|
281 |
Registration r = new Registration(plugins, Arrays.class, bytecodeProvider); |
|
282 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", float[].class, float[].class); |
|
283 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", double[].class, double[].class); |
|
284 |
} |
|
43972 | 285 |
} |