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