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