43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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.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;
|
|
31 |
|
43972
|
32 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
|
33 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
34 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
|
|
35 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
|
36 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
37 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
|
|
38 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
|
46344
|
39 |
import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
|
|
40 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
|
|
41 |
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
|
43972
|
42 |
|
|
43 |
import jdk.vm.ci.meta.JavaKind;
|
|
44 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
45 |
|
|
46 |
public class AArch64GraphBuilderPlugins {
|
|
47 |
|
46344
|
48 |
public static void register(Plugins plugins, BytecodeProvider bytecodeProvider) {
|
43972
|
49 |
InvocationPlugins invocationPlugins = plugins.getInvocationPlugins();
|
|
50 |
invocationPlugins.defer(new Runnable() {
|
|
51 |
@Override
|
|
52 |
public void run() {
|
|
53 |
registerIntegerLongPlugins(invocationPlugins, AArch64IntegerSubstitutions.class, JavaKind.Int, bytecodeProvider);
|
|
54 |
registerIntegerLongPlugins(invocationPlugins, AArch64LongSubstitutions.class, JavaKind.Long, bytecodeProvider);
|
46344
|
55 |
registerMathPlugins(invocationPlugins);
|
43972
|
56 |
}
|
|
57 |
});
|
|
58 |
}
|
|
59 |
|
|
60 |
private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, BytecodeProvider bytecodeProvider) {
|
|
61 |
Class<?> declaringClass = kind.toBoxedJavaClass();
|
|
62 |
Class<?> type = kind.toJavaClass();
|
|
63 |
Registration r = new Registration(plugins, declaringClass, bytecodeProvider);
|
|
64 |
r.register1("numberOfLeadingZeros", type, new InvocationPlugin() {
|
|
65 |
@Override
|
|
66 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
|
67 |
ValueNode folded = AArch64CountLeadingZerosNode.tryFold(value);
|
|
68 |
if (folded != null) {
|
|
69 |
b.addPush(JavaKind.Int, folded);
|
|
70 |
} else {
|
|
71 |
b.addPush(JavaKind.Int, new AArch64CountLeadingZerosNode(value));
|
|
72 |
}
|
|
73 |
return true;
|
|
74 |
}
|
|
75 |
});
|
|
76 |
r.register1("numberOfTrailingZeros", type, new InvocationPlugin() {
|
|
77 |
@Override
|
|
78 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
46344
|
79 |
ValueNode folded = AArch64CountTrailingZerosNode.tryFold(value);
|
43972
|
80 |
if (folded != null) {
|
|
81 |
b.addPush(JavaKind.Int, folded);
|
|
82 |
} else {
|
46344
|
83 |
b.addPush(JavaKind.Int, new AArch64CountTrailingZerosNode(value));
|
43972
|
84 |
}
|
|
85 |
return true;
|
|
86 |
}
|
|
87 |
});
|
|
88 |
r.registerMethodSubstitution(substituteDeclaringClass, "bitCount", type);
|
|
89 |
}
|
|
90 |
|
46344
|
91 |
private static void registerMathPlugins(InvocationPlugins plugins) {
|
|
92 |
Registration r = new Registration(plugins, Math.class);
|
|
93 |
registerUnaryMath(r, "sin", SIN);
|
|
94 |
registerUnaryMath(r, "cos", COS);
|
|
95 |
registerUnaryMath(r, "tan", TAN);
|
|
96 |
registerUnaryMath(r, "exp", EXP);
|
|
97 |
registerUnaryMath(r, "log", LOG);
|
|
98 |
registerUnaryMath(r, "log10", LOG10);
|
|
99 |
r.register2("pow", Double.TYPE, Double.TYPE, new InvocationPlugin() {
|
|
100 |
@Override
|
|
101 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) {
|
|
102 |
b.push(JavaKind.Double, b.recursiveAppend(BinaryMathIntrinsicNode.create(x, y, BinaryMathIntrinsicNode.BinaryOperation.POW)));
|
|
103 |
return true;
|
|
104 |
}
|
|
105 |
});
|
|
106 |
}
|
|
107 |
|
|
108 |
private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) {
|
|
109 |
r.register1(name, Double.TYPE, new InvocationPlugin() {
|
|
110 |
@Override
|
|
111 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
|
|
112 |
b.push(JavaKind.Double, b.recursiveAppend(UnaryMathIntrinsicNode.create(value, operation)));
|
|
113 |
return true;
|
|
114 |
}
|
|
115 |
});
|
43972
|
116 |
}
|
|
117 |
}
|