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