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