author | jwilhelm |
Tue, 23 Apr 2019 22:55:09 +0200 | |
changeset 54601 | c40b2a190173 |
parent 54328 | 37648a9c4a6a |
child 54724 | 62f373a53296 |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
54601 | 2 |
* Copyright (c) 2015, 2019, 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.aarch64; |
26 |
||
50858 | 27 |
import static org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.registerPlatformSpecificUnsafePlugins; |
46344 | 28 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.COS; |
29 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.EXP; |
|
30 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG; |
|
31 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG10; |
|
32 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.SIN; |
|
33 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.TAN; |
|
54084 | 34 |
import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.JAVA_SPECIFICATION_VERSION; |
35 |
import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.Java11OrEarlier; |
|
36 |
import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.Java8OrEarlier; |
|
46344 | 37 |
|
43972 | 38 |
import org.graalvm.compiler.bytecode.BytecodeProvider; |
50330 | 39 |
import org.graalvm.compiler.lir.aarch64.AArch64ArithmeticLIRGeneratorTool.RoundingMode; |
43972 | 40 |
import org.graalvm.compiler.nodes.ValueNode; |
41 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; |
|
42 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
|
43 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; |
|
50330 | 44 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver; |
43972 | 45 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; |
46 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; |
|
50330 | 47 |
import org.graalvm.compiler.nodes.java.AtomicReadAndAddNode; |
50609 | 48 |
import org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode; |
50330 | 49 |
import org.graalvm.compiler.nodes.memory.address.AddressNode; |
50 |
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; |
|
46344 | 51 |
import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode; |
52 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode; |
|
53 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation; |
|
50330 | 54 |
import jdk.internal.vm.compiler.word.LocationIdentity; |
43972 | 55 |
|
56 |
import jdk.vm.ci.meta.JavaKind; |
|
57 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
50330 | 58 |
import sun.misc.Unsafe; |
43972 | 59 |
|
60 |
public class AArch64GraphBuilderPlugins { |
|
61 |
||
54328 | 62 |
public static void register(Plugins plugins, BytecodeProvider bytecodeProvider, boolean explicitUnsafeNullChecks, boolean registerMathPlugins) { |
43972 | 63 |
InvocationPlugins invocationPlugins = plugins.getInvocationPlugins(); |
64 |
invocationPlugins.defer(new Runnable() { |
|
65 |
@Override |
|
66 |
public void run() { |
|
54601 | 67 |
registerIntegerLongPlugins(invocationPlugins, JavaKind.Int, bytecodeProvider); |
68 |
registerIntegerLongPlugins(invocationPlugins, JavaKind.Long, bytecodeProvider); |
|
54328 | 69 |
if (registerMathPlugins) { |
70 |
registerMathPlugins(invocationPlugins); |
|
71 |
} |
|
49873 | 72 |
registerStringLatin1Plugins(invocationPlugins, bytecodeProvider); |
73 |
registerStringUTF16Plugins(invocationPlugins, bytecodeProvider); |
|
50609 | 74 |
registerUnsafePlugins(invocationPlugins, bytecodeProvider); |
50330 | 75 |
// This is temporarily disabled until we implement correct emitting of the CAS |
76 |
// instructions of the proper width. |
|
50858 | 77 |
registerPlatformSpecificUnsafePlugins(invocationPlugins, bytecodeProvider, explicitUnsafeNullChecks, |
50330 | 78 |
new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}); |
43972 | 79 |
} |
80 |
}); |
|
81 |
} |
|
82 |
||
54601 | 83 |
private static void registerIntegerLongPlugins(InvocationPlugins plugins, JavaKind kind, BytecodeProvider bytecodeProvider) { |
43972 | 84 |
Class<?> declaringClass = kind.toBoxedJavaClass(); |
85 |
Class<?> type = kind.toJavaClass(); |
|
86 |
Registration r = new Registration(plugins, declaringClass, bytecodeProvider); |
|
87 |
r.register1("numberOfLeadingZeros", type, new InvocationPlugin() { |
|
88 |
@Override |
|
89 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
90 |
ValueNode folded = AArch64CountLeadingZerosNode.tryFold(value); |
|
91 |
if (folded != null) { |
|
92 |
b.addPush(JavaKind.Int, folded); |
|
93 |
} else { |
|
94 |
b.addPush(JavaKind.Int, new AArch64CountLeadingZerosNode(value)); |
|
95 |
} |
|
96 |
return true; |
|
97 |
} |
|
98 |
}); |
|
99 |
r.register1("numberOfTrailingZeros", type, new InvocationPlugin() { |
|
100 |
@Override |
|
101 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46344 | 102 |
ValueNode folded = AArch64CountTrailingZerosNode.tryFold(value); |
43972 | 103 |
if (folded != null) { |
104 |
b.addPush(JavaKind.Int, folded); |
|
105 |
} else { |
|
46344 | 106 |
b.addPush(JavaKind.Int, new AArch64CountTrailingZerosNode(value)); |
43972 | 107 |
} |
108 |
return true; |
|
109 |
} |
|
110 |
}); |
|
54601 | 111 |
r.register1("bitCount", type, new InvocationPlugin() { |
112 |
@Override |
|
113 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
114 |
b.push(JavaKind.Int, b.append(new AArch64BitCountNode(value).canonical(null))); |
|
115 |
return true; |
|
116 |
} |
|
117 |
}); |
|
43972 | 118 |
} |
119 |
||
46344 | 120 |
private static void registerMathPlugins(InvocationPlugins plugins) { |
121 |
Registration r = new Registration(plugins, Math.class); |
|
122 |
registerUnaryMath(r, "sin", SIN); |
|
123 |
registerUnaryMath(r, "cos", COS); |
|
124 |
registerUnaryMath(r, "tan", TAN); |
|
125 |
registerUnaryMath(r, "exp", EXP); |
|
126 |
registerUnaryMath(r, "log", LOG); |
|
127 |
registerUnaryMath(r, "log10", LOG10); |
|
128 |
r.register2("pow", Double.TYPE, Double.TYPE, new InvocationPlugin() { |
|
129 |
@Override |
|
130 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { |
|
46393 | 131 |
b.push(JavaKind.Double, b.append(BinaryMathIntrinsicNode.create(x, y, BinaryMathIntrinsicNode.BinaryOperation.POW))); |
46344 | 132 |
return true; |
133 |
} |
|
134 |
}); |
|
50330 | 135 |
registerRound(r, "rint", RoundingMode.NEAREST); |
136 |
registerRound(r, "ceil", RoundingMode.UP); |
|
137 |
registerRound(r, "floor", RoundingMode.DOWN); |
|
46344 | 138 |
} |
139 |
||
140 |
private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) { |
|
141 |
r.register1(name, Double.TYPE, new InvocationPlugin() { |
|
142 |
@Override |
|
143 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { |
|
46393 | 144 |
b.push(JavaKind.Double, b.append(UnaryMathIntrinsicNode.create(value, operation))); |
46344 | 145 |
return true; |
146 |
} |
|
147 |
}); |
|
43972 | 148 |
} |
49873 | 149 |
|
50330 | 150 |
private static void registerRound(Registration r, String name, RoundingMode mode) { |
151 |
r.register1(name, Double.TYPE, new InvocationPlugin() { |
|
152 |
@Override |
|
153 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) { |
|
154 |
b.push(JavaKind.Double, b.append(new AArch64RoundNode(arg, mode))); |
|
155 |
return true; |
|
156 |
} |
|
157 |
}); |
|
158 |
} |
|
159 |
||
49873 | 160 |
private static void registerStringLatin1Plugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) { |
161 |
if (JAVA_SPECIFICATION_VERSION >= 9) { |
|
162 |
Registration r = new Registration(plugins, "java.lang.StringLatin1", replacementsBytecodeProvider); |
|
163 |
r.setAllowOverwrite(true); |
|
164 |
r.registerMethodSubstitution(AArch64StringLatin1Substitutions.class, "compareTo", byte[].class, byte[].class); |
|
165 |
r.registerMethodSubstitution(AArch64StringLatin1Substitutions.class, "compareToUTF16", byte[].class, byte[].class); |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
private static void registerStringUTF16Plugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) { |
|
170 |
if (JAVA_SPECIFICATION_VERSION >= 9) { |
|
171 |
Registration r = new Registration(plugins, "java.lang.StringUTF16", replacementsBytecodeProvider); |
|
172 |
r.setAllowOverwrite(true); |
|
173 |
r.registerMethodSubstitution(AArch64StringUTF16Substitutions.class, "compareTo", byte[].class, byte[].class); |
|
174 |
r.registerMethodSubstitution(AArch64StringUTF16Substitutions.class, "compareToLatin1", byte[].class, byte[].class); |
|
175 |
} |
|
176 |
} |
|
177 |
||
50609 | 178 |
private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) { |
52910 | 179 |
registerUnsafePlugins(new Registration(plugins, Unsafe.class), |
180 |
new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}, "Object"); |
|
52578 | 181 |
if (!Java8OrEarlier) { |
52910 | 182 |
registerUnsafePlugins(new Registration(plugins, "jdk.internal.misc.Unsafe", replacementsBytecodeProvider), |
183 |
new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}, |
|
184 |
Java11OrEarlier ? "Object" : "Reference"); |
|
50330 | 185 |
} |
52578 | 186 |
} |
187 |
||
52910 | 188 |
private static void registerUnsafePlugins(Registration r, JavaKind[] unsafeJavaKinds, String objectKindName) { |
50330 | 189 |
|
50609 | 190 |
for (JavaKind kind : unsafeJavaKinds) { |
50330 | 191 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); |
52910 | 192 |
String kindName = kind == JavaKind.Object ? objectKindName : kind.name(); |
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
50858
diff
changeset
|
193 |
r.register4("getAndSet" + kindName, Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() { |
50609 | 194 |
@Override |
195 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { |
|
196 |
// Emits a null-check for the otherwise unused receiver |
|
197 |
unsafe.get(); |
|
198 |
b.addPush(kind, new AtomicReadAndWriteNode(object, offset, value, kind, LocationIdentity.any())); |
|
199 |
b.getGraph().markUnsafeAccess(); |
|
200 |
return true; |
|
201 |
} |
|
202 |
}); |
|
203 |
||
204 |
if (kind != JavaKind.Boolean && kind.isNumericInteger()) { |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
50858
diff
changeset
|
205 |
r.register4("getAndAdd" + kindName, Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() { |
50330 | 206 |
@Override |
207 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) { |
|
208 |
// Emits a null-check for the otherwise unused receiver |
|
209 |
unsafe.get(); |
|
210 |
AddressNode address = b.add(new OffsetAddressNode(object, offset)); |
|
211 |
b.addPush(kind, new AtomicReadAndAddNode(address, delta, kind, LocationIdentity.any())); |
|
212 |
b.getGraph().markUnsafeAccess(); |
|
213 |
return true; |
|
214 |
} |
|
215 |
}); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
43972 | 219 |
} |