1 /*
   2  * Copyright (c) 2015, 2016, 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.amd64;
  24 
  25 import static org.graalvm.compiler.core.common.util.Util.Java8OrEarlier;
  26 import static org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation.POW;
  27 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.COS;
  28 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.EXP;
  29 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG;
  30 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG10;
  31 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.SIN;
  32 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.TAN;
  33 
  34 import org.graalvm.compiler.bytecode.BytecodeProvider;
  35 import org.graalvm.compiler.core.common.LocationIdentity;
  36 import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool.RoundingMode;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  41 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  42 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  43 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  44 import org.graalvm.compiler.nodes.java.AtomicReadAndAddNode;
  45 import org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode;
  46 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  47 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  48 import org.graalvm.compiler.replacements.IntegerSubstitutions;
  49 import org.graalvm.compiler.replacements.LongSubstitutions;
  50 import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafeGetPlugin;
  51 import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins.UnsafePutPlugin;
  52 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
  53 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
  54 import org.graalvm.compiler.replacements.nodes.BitCountNode;
  55 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
  56 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  57 
  58 import jdk.vm.ci.amd64.AMD64;
  59 import jdk.vm.ci.amd64.AMD64.CPUFeature;
  60 import jdk.vm.ci.meta.JavaKind;
  61 import jdk.vm.ci.meta.ResolvedJavaMethod;
  62 import sun.misc.Unsafe;
  63 
  64 public class AMD64GraphBuilderPlugins {
  65 
  66     public static void register(Plugins plugins, BytecodeProvider replacementsBytecodeProvider, AMD64 arch, boolean arithmeticStubs) {
  67         InvocationPlugins invocationPlugins = plugins.getInvocationPlugins();
  68         invocationPlugins.defer(new Runnable() {
  69             @Override
  70             public void run() {
  71                 registerIntegerLongPlugins(invocationPlugins, IntegerSubstitutions.class, JavaKind.Int, arch, replacementsBytecodeProvider);
  72                 registerIntegerLongPlugins(invocationPlugins, LongSubstitutions.class, JavaKind.Long, arch, replacementsBytecodeProvider);
  73                 registerUnsafePlugins(invocationPlugins, replacementsBytecodeProvider);
  74                 registerMathPlugins(invocationPlugins, arch, arithmeticStubs, replacementsBytecodeProvider);
  75             }
  76         });
  77     }
  78 
  79     private static void registerIntegerLongPlugins(InvocationPlugins plugins, Class<?> substituteDeclaringClass, JavaKind kind, AMD64 arch, BytecodeProvider bytecodeProvider) {
  80         Class<?> declaringClass = kind.toBoxedJavaClass();
  81         Class<?> type = kind.toJavaClass();
  82         Registration r = new Registration(plugins, declaringClass, bytecodeProvider);
  83         if (arch.getFeatures().contains(AMD64.CPUFeature.LZCNT) && arch.getFlags().contains(AMD64.Flag.UseCountLeadingZerosInstruction)) {
  84             r.register1("numberOfLeadingZeros", type, new InvocationPlugin() {
  85                 @Override
  86                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
  87                     ValueNode folded = AMD64CountLeadingZerosNode.tryFold(value);
  88                     if (folded != null) {
  89                         b.addPush(JavaKind.Int, folded);
  90                     } else {
  91                         b.addPush(JavaKind.Int, new AMD64CountLeadingZerosNode(value));
  92                     }
  93                     return true;
  94                 }
  95             });
  96         } else {
  97             r.registerMethodSubstitution(substituteDeclaringClass, "numberOfLeadingZeros", type);
  98         }
  99         if (arch.getFeatures().contains(AMD64.CPUFeature.BMI1) && arch.getFlags().contains(AMD64.Flag.UseCountTrailingZerosInstruction)) {
 100             r.register1("numberOfTrailingZeros", type, new InvocationPlugin() {
 101                 @Override
 102                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 103                     ValueNode folded = AMD64CountTrailingZerosNode.tryFold(value);
 104                     if (folded != null) {
 105                         b.addPush(JavaKind.Int, folded);
 106                     } else {
 107                         b.addPush(JavaKind.Int, new AMD64CountTrailingZerosNode(value));
 108                     }
 109                     return true;
 110                 }
 111             });
 112         } else {
 113             r.registerMethodSubstitution(substituteDeclaringClass, "numberOfTrailingZeros", type);
 114         }
 115 
 116         if (arch.getFeatures().contains(AMD64.CPUFeature.POPCNT)) {
 117             r.register1("bitCount", type, new InvocationPlugin() {
 118                 @Override
 119                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 120                     b.push(JavaKind.Int, b.recursiveAppend(new BitCountNode(value).canonical(null)));
 121                     return true;
 122                 }
 123             });
 124         }
 125     }
 126 
 127     private static void registerMathPlugins(InvocationPlugins plugins, AMD64 arch, boolean arithmeticStubs, BytecodeProvider bytecodeProvider) {
 128         Registration r = new Registration(plugins, Math.class, bytecodeProvider);
 129         registerUnaryMath(r, "log", LOG);
 130         registerUnaryMath(r, "log10", LOG10);
 131         registerUnaryMath(r, "exp", EXP);
 132         registerBinaryMath(r, "pow", POW);
 133         if (arithmeticStubs) {
 134             registerUnaryMath(r, "sin", SIN);
 135             registerUnaryMath(r, "cos", COS);
 136             registerUnaryMath(r, "tan", TAN);
 137         } else {
 138             r.registerMethodSubstitution(AMD64MathSubstitutions.class, "sin", double.class);
 139             r.registerMethodSubstitution(AMD64MathSubstitutions.class, "cos", double.class);
 140             r.registerMethodSubstitution(AMD64MathSubstitutions.class, "tan", double.class);
 141         }
 142 
 143         if (arch.getFeatures().contains(CPUFeature.SSE4_1)) {
 144             registerRound(r, "rint", RoundingMode.NEAREST);
 145             registerRound(r, "ceil", RoundingMode.UP);
 146             registerRound(r, "floor", RoundingMode.DOWN);
 147         }
 148     }
 149 
 150     private static void registerUnaryMath(Registration r, String name, UnaryOperation operation) {
 151         r.register1(name, Double.TYPE, new InvocationPlugin() {
 152             @Override
 153             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
 154                 b.push(JavaKind.Double, b.recursiveAppend(UnaryMathIntrinsicNode.create(value, operation)));
 155                 return true;
 156             }
 157         });
 158     }
 159 
 160     private static void registerBinaryMath(Registration r, String name, BinaryOperation operation) {
 161         r.register2(name, Double.TYPE, Double.TYPE, new InvocationPlugin() {
 162             @Override
 163             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) {
 164                 b.push(JavaKind.Double, b.recursiveAppend(BinaryMathIntrinsicNode.create(x, y, operation)));
 165                 return true;
 166             }
 167         });
 168     }
 169 
 170     private static void registerRound(Registration r, String name, RoundingMode mode) {
 171         r.register1(name, Double.TYPE, new InvocationPlugin() {
 172             @Override
 173             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
 174                 b.push(JavaKind.Double, b.append(new AMD64RoundNode(arg, mode)));
 175                 return true;
 176             }
 177         });
 178     }
 179 
 180     private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementsBytecodeProvider) {
 181         Registration r;
 182         if (Java8OrEarlier) {
 183             r = new Registration(plugins, Unsafe.class);
 184         } else {
 185             r = new Registration(plugins, "jdk.internal.misc.Unsafe", replacementsBytecodeProvider);
 186         }
 187         for (JavaKind kind : new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}) {
 188             Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass();
 189 
 190             r.register4("getAndSet" + kind.name(), Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() {
 191                 @Override
 192                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) {
 193                     // Emits a null-check for the otherwise unused receiver
 194                     unsafe.get();
 195                     b.addPush(kind, new AtomicReadAndWriteNode(object, offset, value, kind, LocationIdentity.any()));
 196                     b.getGraph().markUnsafeAccess();
 197                     return true;
 198                 }
 199             });
 200             if (kind != JavaKind.Object) {
 201                 r.register4("getAndAdd" + kind.name(), Receiver.class, Object.class, long.class, javaClass, new InvocationPlugin() {
 202                     @Override
 203                     public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) {
 204                         // Emits a null-check for the otherwise unused receiver
 205                         unsafe.get();
 206                         AddressNode address = b.add(new OffsetAddressNode(object, offset));
 207                         b.addPush(kind, new AtomicReadAndAddNode(address, delta, LocationIdentity.any()));
 208                         b.getGraph().markUnsafeAccess();
 209                         return true;
 210                     }
 211                 });
 212             }
 213         }
 214 
 215         for (JavaKind kind : new JavaKind[]{JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long}) {
 216             Class<?> javaClass = kind.toJavaClass();
 217             r.registerOptional3("get" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, false));
 218             r.registerOptional4("put" + kind.name() + "Unaligned", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, false));
 219         }
 220     }
 221 }