1 /*
   2  * Copyright (c) 2015, 2020, 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 
  24 
  25 package org.graalvm.compiler.replacements.sparc;
  26 
  27 import static org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.registerPlatformSpecificUnsafePlugins;
  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;
  34 
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  37 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  41 import org.graalvm.compiler.nodes.spi.Replacements;
  42 import org.graalvm.compiler.replacements.IntegerSubstitutions;
  43 import org.graalvm.compiler.replacements.LongSubstitutions;
  44 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
  45 import org.graalvm.compiler.replacements.nodes.BitCountNode;
  46 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
  47 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  48 
  49 import jdk.vm.ci.meta.JavaKind;
  50 import jdk.vm.ci.meta.ResolvedJavaMethod;
  51 
  52 public class SPARCGraphBuilderPlugins {
  53 
  54     public static void register(Plugins plugins, Replacements replacements, boolean explicitUnsafeNullChecks) {
  55         InvocationPlugins invocationPlugins = plugins.getInvocationPlugins();
  56         invocationPlugins.defer(new Runnable() {
  57             @Override
  58             public void run() {
  59                 registerIntegerLongPlugins(invocationPlugins, IntegerSubstitutions.class, JavaKind.Int, replacements);
  60                 registerIntegerLongPlugins(invocationPlugins, LongSubstitutions.class, JavaKind.Long, replacements);
  61                 registerMathPlugins(invocationPlugins);
  62                 // This is temporarily disabled until we implement correct emitting of the CAS
  63                 // instructions of the proper width.
  64                 registerPlatformSpecificUnsafePlugins(invocationPlugins, replacements, explicitUnsafeNullChecks,
  65                                 new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object});
  66             }
  67         });
  68     }
  69 
  70     private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, Replacements replacements) {
  71         Class<?> declaringClass = kind.toBoxedJavaClass();
  72         Class<?> type = kind.toJavaClass();
  73         Registration r = new Registration(plugins, declaringClass, replacements);
  74         r.registerMethodSubstitution(substituteDeclaringClass, "numberOfLeadingZeros", type);
  75         r.registerMethodSubstitution(substituteDeclaringClass, "numberOfTrailingZeros", type);
  76 
  77         r.register1("bitCount", type, new InvocationPlugin() {
  78             @Override
  79             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
  80                 b.push(JavaKind.Int, b.append(new BitCountNode(value).canonical(null)));
  81                 return true;
  82             }
  83         });
  84     }
  85 
  86     private static void registerMathPlugins(InvocationPlugins plugins) {
  87         Registration r = new Registration(plugins, Math.class);
  88         registerUnaryMath(r, "sin", SIN);
  89         registerUnaryMath(r, "cos", COS);
  90         registerUnaryMath(r, "tan", TAN);
  91         registerUnaryMath(r, "exp", EXP);
  92         registerUnaryMath(r, "log", LOG);
  93         registerUnaryMath(r, "log10", LOG10);
  94         r.register2("pow", Double.TYPE, Double.TYPE, new InvocationPlugin() {
  95             @Override
  96             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) {
  97                 b.push(JavaKind.Double, b.append(BinaryMathIntrinsicNode.create(x, y, BinaryMathIntrinsicNode.BinaryOperation.POW)));
  98                 return true;
  99             }
 100         });
 101     }
 102 
 103     private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) {
 104         r.register1(name, Double.TYPE, new InvocationPlugin() {
 105             @Override
 106             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 107                 b.push(JavaKind.Double, b.append(UnaryMathIntrinsicNode.create(value, operation)));
 108                 return true;
 109             }
 110         });
 111     }
 112 }