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 */
23package org.graalvm.compiler.replacements.aarch64;
24
25import org.graalvm.compiler.bytecode.BytecodeProvider;
26import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
27import org.graalvm.compiler.nodes.ValueNode;
28import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
29import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
30import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
31import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
32import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
33import org.graalvm.compiler.replacements.nodes.BitScanForwardNode;
34
35import jdk.vm.ci.meta.JavaKind;
36import jdk.vm.ci.meta.ResolvedJavaMethod;
37
38public class AArch64GraphBuilderPlugins {
39
40    public static void register(Plugins plugins, ForeignCallsProvider foreignCalls, BytecodeProvider bytecodeProvider) {
41        InvocationPlugins invocationPlugins = plugins.getInvocationPlugins();
42        invocationPlugins.defer(new Runnable() {
43            @Override
44            public void run() {
45                registerIntegerLongPlugins(invocationPlugins, AArch64IntegerSubstitutions.class, JavaKind.Int, bytecodeProvider);
46                registerIntegerLongPlugins(invocationPlugins, AArch64LongSubstitutions.class, JavaKind.Long, bytecodeProvider);
47                registerMathPlugins(invocationPlugins, foreignCalls);
48            }
49        });
50    }
51
52    private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, BytecodeProvider bytecodeProvider) {
53        Class<?> declaringClass = kind.toBoxedJavaClass();
54        Class<?> type = kind.toJavaClass();
55        Registration r = new Registration(plugins, declaringClass, bytecodeProvider);
56        r.register1("numberOfLeadingZeros", type, new InvocationPlugin() {
57            @Override
58            public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
59                ValueNode folded = AArch64CountLeadingZerosNode.tryFold(value);
60                if (folded != null) {
61                    b.addPush(JavaKind.Int, folded);
62                } else {
63                    b.addPush(JavaKind.Int, new AArch64CountLeadingZerosNode(value));
64                }
65                return true;
66            }
67        });
68        r.register1("numberOfTrailingZeros", type, new InvocationPlugin() {
69            @Override
70            public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
71                ValueNode folded = BitScanForwardNode.tryFold(value);
72                if (folded != null) {
73                    b.addPush(JavaKind.Int, folded);
74                } else {
75                    b.addPush(JavaKind.Int, new BitScanForwardNode(value));
76                }
77                return true;
78            }
79        });
80        r.registerMethodSubstitution(substituteDeclaringClass, "bitCount", type);
81    }
82
83    @SuppressWarnings("unused")
84    private static void registerMathPlugins(InvocationPlugins plugins, ForeignCallsProvider foreignCalls) {
85        // Registration r = new Registration(plugins, Math.class);
86        // r.register1("sin", Double.TYPE, new ForeignCallPlugin(foreignCalls, ARITHMETIC_SIN));
87        // r.register1("cos", Double.TYPE, new ForeignCallPlugin(foreignCalls, ARITHMETIC_COS));
88        // r.register1("tan", Double.TYPE, new ForeignCallPlugin(foreignCalls, ARITHMETIC_TAN));
89        // r.register1("exp", Double.TYPE, new ForeignCallPlugin(foreignCalls, ARITHMETIC_EXP));
90        // r.register1("log", Double.TYPE, new ForeignCallPlugin(foreignCalls, ARITHMETIC_LOG));
91        // r.register1("log10", Double.TYPE, new ForeignCallPlugin(foreignCalls, ARITHMETIC_LOG10));
92        // r.register2("pow", Double.TYPE, Double.TYPE, new ForeignCallPlugin(foreignCalls,
93        // ARITHMETIC_POW));
94    }
95}
96