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