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.amd64;
  26 
  27 import static org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.registerPlatformSpecificUnsafePlugins;
  28 import static org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation.POW;
  29 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.COS;
  30 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.EXP;
  31 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG;
  32 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG10;
  33 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.SIN;
  34 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.TAN;
  35 import static org.graalvm.compiler.serviceprovider.GraalServices.JAVA_SPECIFICATION_VERSION;
  36 import static org.graalvm.compiler.serviceprovider.GraalServices.Java11OrEarlier;
  37 import static org.graalvm.compiler.serviceprovider.GraalServices.Java8OrEarlier;
  38 
  39 import java.util.Arrays;
  40 
  41 import org.graalvm.compiler.bytecode.BytecodeProvider;
  42 import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool.RoundingMode;
  43 import org.graalvm.compiler.nodes.PauseNode;
  44 import org.graalvm.compiler.nodes.ValueNode;
  45 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  46 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  47 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  48 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  49 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  50 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  51 import org.graalvm.compiler.nodes.java.AtomicReadAndAddNode;
  52 import org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode;
  53 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  54 import org.graalvm.compiler.replacements.ArraysSubstitutions;
  55 import org.graalvm.compiler.replacements.IntegerSubstitutions;
  56 import org.graalvm.compiler.replacements.LongSubstitutions;
  57 import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeAccessPlugin;
  58 import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeGetPlugin;
  59 import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafePutPlugin;
  60 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
  61 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
  62 import org.graalvm.compiler.replacements.nodes.BitCountNode;
  63 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
  64 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  65 
  66 import jdk.vm.ci.amd64.AMD64;
  67 import jdk.vm.ci.amd64.AMD64.CPUFeature;
  68 import jdk.vm.ci.meta.JavaKind;
  69 import jdk.vm.ci.meta.ResolvedJavaMethod;
  70 import sun.misc.Unsafe;
  71 
  72 public class AMD64GraphBuilderPlugins {
  73 
  74     public static void register(Plugins plugins, BytecodeProvider replacementsBytecodeProvider, AMD64 arch, boolean arithmeticStubs, boolean explicitUnsafeNullChecks) {
  75         InvocationPlugins invocationPlugins = plugins.getInvocationPlugins();
  76         invocationPlugins.defer(new Runnable() {
  77             @Override
  78             public void run() {
  79                 registerThreadPlugins(invocationPlugins, arch);
  80                 registerIntegerLongPlugins(invocationPlugins, IntegerSubstitutions.class, JavaKind.Int, arch, replacementsBytecodeProvider);
  81                 registerIntegerLongPlugins(invocationPlugins, LongSubstitutions.class, JavaKind.Long, arch, replacementsBytecodeProvider);
  82                 registerPlatformSpecificUnsafePlugins(invocationPlugins, replacementsBytecodeProvider, explicitUnsafeNullChecks,
  83                                 new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object, JavaKind.Boolean, JavaKind.Byte, JavaKind.Short, JavaKind.Char, JavaKind.Float, JavaKind.Double});
  84                 registerUnsafePlugins(invocationPlugins, replacementsBytecodeProvider, explicitUnsafeNullChecks);
  85                 registerStringPlugins(invocationPlugins, arch, replacementsBytecodeProvider);
  86                 registerStringLatin1Plugins(invocationPlugins, arch, replacementsBytecodeProvider);
  87                 registerStringUTF16Plugins(invocationPlugins, arch, replacementsBytecodeProvider);
  88                 registerMathPlugins(invocationPlugins, arch, arithmeticStubs, replacementsBytecodeProvider);
  89                 registerArraysEqualsPlugins(invocationPlugins, replacementsBytecodeProvider);
  90             }
  91         });
  92     }
  93 
  94     private static void registerThreadPlugins(InvocationPlugins plugins, AMD64 arch) {
  95         if (!Java8OrEarlier) {
  96             // Pause instruction introduced with SSE2
  97             if (arch.getFeatures().contains(AMD64.CPUFeature.SSE2)) {
  98                 Registration r = new Registration(plugins, Thread.class);
  99                 r.register0("onSpinWait", new InvocationPlugin() {
 100                     @Override
 101                     public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 102                         b.append(new PauseNode());
 103                         return true;
 104                     }
 105                 });
 106             }
 107         }
 108     }
 109 
 110     private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, AMD64 arch, BytecodeProvider bytecodeProvider) {
 111         Class<?> declaringClass = kind.toBoxedJavaClass();
 112         Class<?> type = kind.toJavaClass();
 113         Registration r = new Registration(plugins, declaringClass, bytecodeProvider);
 114         if (arch.getFeatures().contains(AMD64.CPUFeature.LZCNT) && arch.getFlags().contains(AMD64.Flag.UseCountLeadingZerosInstruction)) {
 115             r.register1("numberOfLeadingZeros", type, new InvocationPlugin() {
 116                 @Override
 117                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 118                     ValueNode folded = AMD64CountLeadingZerosNode.tryFold(value);
 119                     if (folded != null) {
 120                         b.addPush(JavaKind.Int, folded);
 121                     } else {
 122                         b.addPush(JavaKind.Int, new AMD64CountLeadingZerosNode(value));
 123                     }
 124                     return true;
 125                 }
 126             });
 127         } else {
 128             r.registerMethodSubstitution(substituteDeclaringClass, "numberOfLeadingZeros", type);
 129         }
 130         if (arch.getFeatures().contains(AMD64.CPUFeature.BMI1) && arch.getFlags().contains(AMD64.Flag.UseCountTrailingZerosInstruction)) {
 131             r.register1("numberOfTrailingZeros", type, new InvocationPlugin() {
 132                 @Override
 133                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 134                     ValueNode folded = AMD64CountTrailingZerosNode.tryFold(value);
 135                     if (folded != null) {
 136                         b.addPush(JavaKind.Int, folded);
 137                     } else {
 138                         b.addPush(JavaKind.Int, new AMD64CountTrailingZerosNode(value));
 139                     }
 140                     return true;
 141                 }
 142             });
 143         } else {
 144             r.registerMethodSubstitution(substituteDeclaringClass, "numberOfTrailingZeros", type);
 145         }
 146 
 147         if (arch.getFeatures().contains(AMD64.CPUFeature.POPCNT)) {
 148             r.register1("bitCount", type, new InvocationPlugin() {
 149                 @Override
 150                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 151                     b.push(JavaKind.Int, b.append(new BitCountNode(value).canonical(null)));
 152                     return true;
 153                 }
 154             });
 155         }
 156     }
 157 
 158     private static void registerMathPlugins(InvocationPlugins plugins, AMD64 arch, boolean arithmeticStubs, BytecodeProvider bytecodeProvider) {
 159         Registration r = new Registration(plugins, Math.class, bytecodeProvider);
 160         registerUnaryMath(r, "log", LOG);
 161         registerUnaryMath(r, "log10", LOG10);
 162         registerUnaryMath(r, "exp", EXP);
 163         registerBinaryMath(r, "pow", POW);
 164         if (arithmeticStubs) {
 165             registerUnaryMath(r, "sin", SIN);
 166             registerUnaryMath(r, "cos", COS);
 167             registerUnaryMath(r, "tan", TAN);
 168         } else {
 169             r.registerMethodSubstitution(AMD64MathSubstitutions.class, "sin", double.class);
 170             r.registerMethodSubstitution(AMD64MathSubstitutions.class, "cos", double.class);
 171             r.registerMethodSubstitution(AMD64MathSubstitutions.class, "tan", double.class);
 172         }
 173 
 174         if (arch.getFeatures().contains(CPUFeature.SSE4_1)) {
 175             registerRound(r, "rint", RoundingMode.NEAREST);
 176             registerRound(r, "ceil", RoundingMode.UP);
 177             registerRound(r, "floor", RoundingMode.DOWN);
 178         }
 179     }
 180 
 181     private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) {
 182         r.register1(name, Double.TYPE, new InvocationPlugin() {
 183             @Override
 184             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 185                 b.push(JavaKind.Double, b.append(UnaryMathIntrinsicNode.create(value, operation)));
 186                 return true;
 187             }
 188         });
 189     }
 190 
 191     private static void registerBinaryMath(Registration r, String name, BinaryOperation operation) {
 192         r.register2(name, Double.TYPE, Double.TYPE, new InvocationPlugin() {
 193             @Override
 194             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) {
 195                 b.push(JavaKind.Double, b.append(BinaryMathIntrinsicNode.create(x, y, operation)));
 196                 return true;
 197             }
 198         });
 199     }
 200 
 201     private static void registerRound(Registration r, String name, RoundingMode mode) {
 202         r.register1(name, Double.TYPE, new InvocationPlugin() {
 203             @Override
 204             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
 205                 b.push(JavaKind.Double, b.append(new AMD64RoundNode(arg, mode)));
 206                 return true;
 207             }
 208         });
 209     }
 210 
 211     private static void registerStringPlugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) {
 212         if (Java8OrEarlier) {
 213             Registration r;
 214             r = new Registration(plugins, String.class, replacementsBytecodeProvider);
 215             r.setAllowOverwrite(true);
 216             if (arch.getFeatures().contains(CPUFeature.SSE4_2)) {
 217                 r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", char[].class, int.class,
 218                                 int.class, char[].class, int.class, int.class, int.class);
 219             }
 220             if (arch.getFeatures().contains(CPUFeature.SSSE3)) {
 221                 r.registerMethodSubstitution(AMD64StringSubstitutions.class, "indexOf", Receiver.class, int.class, int.class);
 222             }
 223             r.registerMethodSubstitution(AMD64StringSubstitutions.class, "compareTo", Receiver.class, String.class);
 224         }
 225     }
 226 
 227     private static void registerStringLatin1Plugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) {
 228         if (JAVA_SPECIFICATION_VERSION >= 9) {
 229             Registration r = new Registration(plugins, "java.lang.StringLatin1", replacementsBytecodeProvider);
 230             r.setAllowOverwrite(true);
 231             r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "compareTo", byte[].class, byte[].class);
 232             r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "compareToUTF16", byte[].class, byte[].class);
 233             r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "inflate", byte[].class, int.class, char[].class, int.class, int.class);
 234             r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "inflate", byte[].class, int.class, byte[].class, int.class, int.class);
 235 
 236             if (arch.getFeatures().contains(CPUFeature.SSSE3)) {
 237                 r.registerMethodSubstitution(AMD64StringLatin1Substitutions.class, "indexOf", byte[].class, int.class, int.class);
 238             }
 239         }
 240     }
 241 
 242     private static void registerStringUTF16Plugins(InvocationPlugins plugins, AMD64 arch, BytecodeProvider replacementsBytecodeProvider) {
 243         if (JAVA_SPECIFICATION_VERSION >= 9) {
 244             Registration r = new Registration(plugins, "java.lang.StringUTF16", replacementsBytecodeProvider);
 245             r.setAllowOverwrite(true);
 246             r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compareTo", byte[].class, byte[].class);
 247             r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compareToLatin1", byte[].class, byte[].class);
 248             r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compress", char[].class, int.class, byte[].class, int.class, int.class);
 249             r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "compress", byte[].class, int.class, byte[].class, int.class, int.class);
 250 
 251             if (arch.getFeatures().contains(CPUFeature.SSSE3)) {
 252                 r.registerMethodSubstitution(AMD64StringUTF16Substitutions.class, "indexOfCharUnsafe", byte[].class, int.class, int.class, int.class);
 253             }
 254         }
 255     }
 256 
 257     private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider, boolean explicitUnsafeNullChecks) {
 258         registerUnsafePlugins(new Registration(plugins, Unsafe.class), explicitUnsafeNullChecks, new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}, true);
 259         if (!Java8OrEarlier) {
 260             registerUnsafePlugins(new Registration(plugins, "jdk.internal.misc.Unsafe", replacementsBytecodeProvider), explicitUnsafeNullChecks,
 261                             new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long, JavaKind.Object}, Java11OrEarlier);
 262         }
 263     }
 264 
 265     private static void registerUnsafePlugins(Registration r, boolean explicitUnsafeNullChecks, JavaKind[] unsafeJavaKinds, boolean java11OrEarlier) {
 266         for (JavaKind kind : unsafeJavaKinds) {
 267             Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass();
 268             String kindName = (kind == JavaKind.Object && !java11OrEarlier) ? "Reference" : kind.name();
 269             r.register4("getAndSet" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks) {
 270                 @Override
 271                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) {
 272                     // Emits a null-check for the otherwise unused receiver
 273                     unsafe.get();
 274                     createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndWriteNode(obj, offset, value, kind, loc));
 275                     return true;
 276                 }
 277             });
 278             if (kind != JavaKind.Boolean && kind.isNumericInteger()) {
 279                 r.register4("getAndAdd" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks) {
 280                     @Override
 281                     public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) {
 282                         // Emits a null-check for the otherwise unused receiver
 283                         unsafe.get();
 284                         createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndAddNode(b.add(new OffsetAddressNode(obj, offset)), delta, kind, loc));
 285                         return true;
 286                     }
 287                 });
 288             }
 289         }
 290 
 291         for (JavaKind kind : new JavaKind[]{JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long}) {
 292             Class<?> javaClass = kind.toJavaClass();
 293             r.registerOptional3("get" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, explicitUnsafeNullChecks));
 294             r.registerOptional4("put" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, explicitUnsafeNullChecks));
 295         }
 296     }
 297 
 298     private static void registerArraysEqualsPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) {
 299         Registration r = new Registration(plugins, Arrays.class, bytecodeProvider);
 300         r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", float[].class, float[].class);
 301         r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", double[].class, double[].class);
 302     }
 303 }