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 |
*/
|
|
23 |
package org.graalvm.compiler.replacements.amd64;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation.POW;
|
|
26 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.COS;
|
|
27 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.EXP;
|
|
28 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG;
|
|
29 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG10;
|
|
30 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.SIN;
|
|
31 |
import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.TAN;
|
46459
|
32 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier;
|
43972
|
33 |
|
46963
|
34 |
import java.util.Arrays;
|
|
35 |
|
43972
|
36 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
|
37 |
import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool.RoundingMode;
|
|
38 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
39 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
|
|
40 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
|
41 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
42 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
|
|
43 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
|
|
44 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
|
|
45 |
import org.graalvm.compiler.nodes.java.AtomicReadAndAddNode;
|
|
46 |
import org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode;
|
|
47 |
import org.graalvm.compiler.nodes.memory.address.AddressNode;
|
|
48 |
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
|
46963
|
49 |
import org.graalvm.compiler.replacements.ArraysSubstitutions;
|
43972
|
50 |
import org.graalvm.compiler.replacements.IntegerSubstitutions;
|
|
51 |
import org.graalvm.compiler.replacements.LongSubstitutions;
|
|
52 |
import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeGetPlugin;
|
|
53 |
import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafePutPlugin;
|
|
54 |
import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
|
|
55 |
import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
|
|
56 |
import org.graalvm.compiler.replacements.nodes.BitCountNode;
|
|
57 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
|
|
58 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
|
46551
|
59 |
import org.graalvm.word.LocationIdentity;
|
43972
|
60 |
|
|
61 |
import jdk.vm.ci.amd64.AMD64;
|
|
62 |
import jdk.vm.ci.amd64.AMD64.CPUFeature;
|
|
63 |
import jdk.vm.ci.meta.JavaKind;
|
|
64 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
65 |
import sun.misc.Unsafe;
|
|
66 |
|
|
67 |
public class AMD64GraphBuilderPlugins {
|
|
68 |
|
|
69 |
public static void register(Plugins plugins, BytecodeProvider replacementsBytecodeProvider, AMD64 arch, boolean arithmeticStubs) {
|
|
70 |
InvocationPlugins invocationPlugins = plugins.getInvocationPlugins();
|
|
71 |
invocationPlugins.defer(new Runnable() {
|
|
72 |
@Override
|
|
73 |
public void run() {
|
|
74 |
registerIntegerLongPlugins(invocationPlugins, IntegerSubstitutions.class, JavaKind.Int, arch, replacementsBytecodeProvider);
|
|
75 |
registerIntegerLongPlugins(invocationPlugins, LongSubstitutions.class, JavaKind.Long, arch, replacementsBytecodeProvider);
|
|
76 |
registerUnsafePlugins(invocationPlugins, replacementsBytecodeProvider);
|
46344
|
77 |
registerStringPlugins(invocationPlugins, arch, replacementsBytecodeProvider);
|
43972
|
78 |
registerMathPlugins(invocationPlugins, arch, arithmeticStubs, replacementsBytecodeProvider);
|
46963
|
79 |
registerArraysEqualsPlugins(invocationPlugins, replacementsBytecodeProvider);
|
43972
|
80 |
}
|
|
81 |
});
|
|
82 |
}
|
|
83 |
|
|
84 |
private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, AMD64 arch, BytecodeProvider bytecodeProvider) {
|
|
85 |
Class<?> declaringClass = kind.toBoxedJavaClass();
|
|
86 |
Class<?> type = kind.toJavaClass();
|
|
87 |
Registration r = new Registration(plugins, declaringClass, bytecodeProvider);
|
|
88 |
if (arch.getFeatures().contains(AMD64.CPUFeature.LZCNT) && arch.getFlags().contains(AMD64.Flag.UseCountLeadingZerosInstruction)) {
|
|
89 |
r.register1("numberOfLeadingZeros", type, new InvocationPlugin() {
|
|
90 |
@Override
|
|
91 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
|
92 |
ValueNode folded = AMD64CountLeadingZerosNode.tryFold(value);
|
|
93 |
if (folded != null) {
|
|
94 |
b.addPush(JavaKind.Int, folded);
|
|
95 |
} else {
|
|
96 |
b.addPush(JavaKind.Int, new AMD64CountLeadingZerosNode(value));
|
|
97 |
}
|
|
98 |
return true;
|
|
99 |
}
|
|
100 |
});
|
|
101 |
} else {
|
|
102 |
r.registerMethodSubstitution(substituteDeclaringClass, "numberOfLeadingZeros", type);
|
|
103 |
}
|
|
104 |
if (arch.getFeatures().contains(AMD64.CPUFeature.BMI1) && arch.getFlags().contains(AMD64.Flag.UseCountTrailingZerosInstruction)) {
|
|
105 |
r.register1("numberOfTrailingZeros", type, new InvocationPlugin() {
|
|
106 |
@Override
|
|
107 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
|
108 |
ValueNode folded = AMD64CountTrailingZerosNode.tryFold(value);
|
|
109 |
if (folded != null) {
|
|
110 |
b.addPush(JavaKind.Int, folded);
|
|
111 |
} else {
|
|
112 |
b.addPush(JavaKind.Int, new AMD64CountTrailingZerosNode(value));
|
|
113 |
}
|
|
114 |
return true;
|
|
115 |
}
|
|
116 |
});
|
|
117 |
} else {
|
|
118 |
r.registerMethodSubstitution(substituteDeclaringClass, "numberOfTrailingZeros", type);
|
|
119 |
}
|
|
120 |
|
|
121 |
if (arch.getFeatures().contains(AMD64.CPUFeature.POPCNT)) {
|
|
122 |
r.register1("bitCount", type, new InvocationPlugin() {
|
|
123 |
@Override
|
|
124 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
46393
|
125 |
b.push(JavaKind.Int, b.append(new BitCountNode(value).canonical(null)));
|
43972
|
126 |
return true;
|
|
127 |
}
|
|
128 |
});
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
private static void registerMathPlugins(InvocationPlugins plugins, AMD64 arch, boolean arithmeticStubs, BytecodeProvider bytecodeProvider) {
|
|
133 |
Registration r = new Registration(plugins, Math.class, bytecodeProvider);
|
|
134 |
registerUnaryMath(r, "log", LOG);
|
|
135 |
registerUnaryMath(r, "log10", LOG10);
|
|
136 |
registerUnaryMath(r, "exp", EXP);
|
|
137 |
registerBinaryMath(r, "pow", POW);
|
|
138 |
if (arithmeticStubs) {
|
|
139 |
registerUnaryMath(r, "sin", SIN);
|
|
140 |
registerUnaryMath(r, "cos", COS);
|
|
141 |
registerUnaryMath(r, "tan", TAN);
|
|
142 |
} else {
|
|
143 |
r.registerMethodSubstitution(AMD64MathSubstitutions.class, "sin", double.class);
|
|
144 |
r.registerMethodSubstitution(AMD64MathSubstitutions.class, "cos", double.class);
|
|
145 |
r.registerMethodSubstitution(AMD64MathSubstitutions.class, "tan", double.class);
|
|
146 |
}
|
|
147 |
|
|
148 |
if (arch.getFeatures().contains(CPUFeature.SSE4_1)) {
|
|
149 |
registerRound(r, "rint", RoundingMode.NEAREST);
|
|
150 |
registerRound(r, "ceil", RoundingMode.UP);
|
|
151 |
registerRound(r, "floor", RoundingMode.DOWN);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) {
|
|
156 |
r.register1(name, Double.TYPE, new InvocationPlugin() {
|
|
157 |
@Override
|
|
158 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
46393
|
159 |
b.push(JavaKind.Double, b.append(UnaryMathIntrinsicNode.create(value, operation)));
|
43972
|
160 |
return true;
|
|
161 |
}
|
|
162 |
});
|
|
163 |
}
|
|
164 |
|
|
165 |
private static void registerBinaryMath(Registration r, String name, BinaryOperation operation) {
|
|
166 |
r.register2(name, Double.TYPE, Double.TYPE, new InvocationPlugin() {
|
|
167 |
@Override
|
|
168 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) {
|
46393
|
169 |
b.push(JavaKind.Double, b.append(BinaryMathIntrinsicNode.create(x, y, operation)));
|
43972
|
170 |
return true;
|
|
171 |
}
|
|
172 |
});
|
|
173 |
}
|
|
174 |
|
|
175 |
private static void registerRound(Registration r, String name, RoundingMode mode) {
|
|
176 |
r.register1(name, Double.TYPE, new InvocationPlugin() {
|
|
177 |
@Override
|
|
178 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
|
|
179 |
b.push(JavaKind.Double, b.append(new AMD64RoundNode(arg, mode)));
|
|
180 |
return true;
|
|
181 |
}
|
|
182 |
});
|
|
183 |
}
|
|
184 |
|
46344
|
185 |
private static void registerStringPlugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) {
|
|
186 |
if (Java8OrEarlier && arch.getFeatures().contains(CPUFeature.SSE4_2)) {
|
|
187 |
Registration r;
|
|
188 |
r = new Registration(plugins, String.class, replacementsBytecodeProvider);
|
|
189 |
r.setAllowOverwrite(true);
|
|
190 |
r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", char[].class, int.class,
|
|
191 |
int.class, char[].class, int.class, int.class, int.class);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
43972
|
195 |
private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) {
|
|
196 |
Registration r;
|
|
197 |
if (Java8OrEarlier) {
|
|
198 |
r = new Registration(plugins, Unsafe.class);
|
|
199 |
} else {
|
|
200 |
r = new Registration(plugins, "jdk.internal.misc.Unsafe", replacementsBytecodeProvider);
|
|
201 |
}
|
|
202 |
for (JavaKind kind : new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}) {
|
|
203 |
Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass();
|
|
204 |
|
|
205 |
r.register4("getAndSet" + kind.name(), Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() {
|
|
206 |
@Override
|
|
207 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) {
|
|
208 |
// Emits a null-check for the otherwise unused receiver
|
|
209 |
unsafe.get();
|
|
210 |
b.addPush(kind, new AtomicReadAndWriteNode(object, offset, value, kind, LocationIdentity.any()));
|
|
211 |
b.getGraph().markUnsafeAccess();
|
|
212 |
return true;
|
|
213 |
}
|
|
214 |
});
|
|
215 |
if (kind != JavaKind.Object) {
|
|
216 |
r.register4("getAndAdd" + kind.name(), Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() {
|
|
217 |
@Override
|
|
218 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) {
|
|
219 |
// Emits a null-check for the otherwise unused receiver
|
|
220 |
unsafe.get();
|
|
221 |
AddressNode address = b.add(new OffsetAddressNode(object, offset));
|
|
222 |
b.addPush(kind, new AtomicReadAndAddNode(address, delta, LocationIdentity.any()));
|
|
223 |
b.getGraph().markUnsafeAccess();
|
|
224 |
return true;
|
|
225 |
}
|
|
226 |
});
|
|
227 |
}
|
|
228 |
}
|
|
229 |
|
|
230 |
for (JavaKind kind : new JavaKind[]{JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long}) {
|
|
231 |
Class<?> javaClass = kind.toJavaClass();
|
|
232 |
r.registerOptional3("get" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, false));
|
|
233 |
r.registerOptional4("put" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, false));
|
|
234 |
}
|
|
235 |
}
|
46963
|
236 |
|
|
237 |
private static void registerArraysEqualsPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) {
|
|
238 |
Registration r = new Registration(plugins, Arrays.class, bytecodeProvider);
|
|
239 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", float[].class, float[].class);
|
|
240 |
r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", double[].class, double[].class);
|
|
241 |
}
|
43972
|
242 |
}
|