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; 26 27 import static jdk.vm.ci.code.MemoryBarriers.JMM_POST_VOLATILE_READ; 28 import static jdk.vm.ci.code.MemoryBarriers.JMM_POST_VOLATILE_WRITE; 29 import static jdk.vm.ci.code.MemoryBarriers.JMM_PRE_VOLATILE_READ; 30 import static jdk.vm.ci.code.MemoryBarriers.JMM_PRE_VOLATILE_WRITE; 31 import static jdk.vm.ci.code.MemoryBarriers.LOAD_LOAD; 32 import static jdk.vm.ci.code.MemoryBarriers.LOAD_STORE; 33 import static jdk.vm.ci.code.MemoryBarriers.STORE_LOAD; 34 import static jdk.vm.ci.code.MemoryBarriers.STORE_STORE; 35 import static org.graalvm.compiler.nodes.NamedLocationIdentity.OFF_HEAP_LOCATION; 36 import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.Java11OrEarlier; 37 import static org.graalvm.compiler.serviceprovider.JavaVersionUtil.Java8OrEarlier; 38 39 import java.lang.reflect.Array; 40 import java.lang.reflect.Field; 41 import java.util.Arrays; 42 43 import org.graalvm.compiler.api.directives.GraalDirectives; 44 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 45 import org.graalvm.compiler.bytecode.BytecodeProvider; 46 import org.graalvm.compiler.core.common.calc.Condition; 47 import org.graalvm.compiler.core.common.calc.Condition.CanonicalizedCondition; 48 import org.graalvm.compiler.core.common.calc.UnsignedMath; 49 import org.graalvm.compiler.core.common.type.ObjectStamp; 50 import org.graalvm.compiler.core.common.type.Stamp; 51 import org.graalvm.compiler.core.common.type.StampFactory; 52 import org.graalvm.compiler.core.common.type.TypeReference; 53 import org.graalvm.compiler.debug.GraalError; 54 import org.graalvm.compiler.graph.Edges; 55 import org.graalvm.compiler.graph.Node; 56 import org.graalvm.compiler.graph.NodeList; 57 import org.graalvm.compiler.nodes.AbstractBeginNode; 58 import org.graalvm.compiler.nodes.BeginNode; 59 import org.graalvm.compiler.nodes.ConstantNode; 60 import org.graalvm.compiler.nodes.DeoptimizeNode; 61 import org.graalvm.compiler.nodes.EndNode; 62 import org.graalvm.compiler.nodes.FixedGuardNode; 63 import org.graalvm.compiler.nodes.FixedWithNextNode; 64 import org.graalvm.compiler.nodes.IfNode; 65 import org.graalvm.compiler.nodes.LogicNode; 66 import org.graalvm.compiler.nodes.MergeNode; 67 import org.graalvm.compiler.nodes.NamedLocationIdentity; 68 import org.graalvm.compiler.nodes.NodeView; 69 import org.graalvm.compiler.nodes.StateSplit; 70 import org.graalvm.compiler.nodes.StructuredGraph; 71 import org.graalvm.compiler.nodes.ValueNode; 72 import org.graalvm.compiler.nodes.ValuePhiNode; 73 import org.graalvm.compiler.nodes.calc.AbsNode; 74 import org.graalvm.compiler.nodes.calc.CompareNode; 75 import org.graalvm.compiler.nodes.calc.ConditionalNode; 76 import org.graalvm.compiler.nodes.calc.FloatEqualsNode; 77 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; 78 import org.graalvm.compiler.nodes.calc.IsNullNode; 79 import org.graalvm.compiler.nodes.calc.NarrowNode; 80 import org.graalvm.compiler.nodes.calc.ReinterpretNode; 81 import org.graalvm.compiler.nodes.calc.RightShiftNode; 82 import org.graalvm.compiler.nodes.calc.SignExtendNode; 83 import org.graalvm.compiler.nodes.calc.SqrtNode; 84 import org.graalvm.compiler.nodes.calc.UnsignedDivNode; 85 import org.graalvm.compiler.nodes.calc.UnsignedRemNode; 86 import org.graalvm.compiler.nodes.calc.ZeroExtendNode; 87 import org.graalvm.compiler.nodes.debug.BindToRegisterNode; 88 import org.graalvm.compiler.nodes.debug.BlackholeNode; 89 import org.graalvm.compiler.nodes.debug.ControlFlowAnchorNode; 90 import org.graalvm.compiler.nodes.debug.SpillRegistersNode; 91 import org.graalvm.compiler.nodes.extended.BoxNode; 92 import org.graalvm.compiler.nodes.extended.BranchProbabilityNode; 93 import org.graalvm.compiler.nodes.extended.BytecodeExceptionNode.BytecodeExceptionKind; 94 import org.graalvm.compiler.nodes.extended.GetClassNode; 95 import org.graalvm.compiler.nodes.extended.GuardingNode; 96 import org.graalvm.compiler.nodes.extended.JavaReadNode; 97 import org.graalvm.compiler.nodes.extended.JavaWriteNode; 98 import org.graalvm.compiler.nodes.extended.MembarNode; 99 import org.graalvm.compiler.nodes.extended.OpaqueNode; 100 import org.graalvm.compiler.nodes.extended.RawLoadNode; 101 import org.graalvm.compiler.nodes.extended.RawStoreNode; 102 import org.graalvm.compiler.nodes.extended.UnboxNode; 103 import org.graalvm.compiler.nodes.extended.UnsafeMemoryLoadNode; 104 import org.graalvm.compiler.nodes.extended.UnsafeMemoryStoreNode; 105 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 106 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; 107 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver; 108 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; 109 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; 110 import org.graalvm.compiler.nodes.java.ClassIsAssignableFromNode; 111 import org.graalvm.compiler.nodes.java.DynamicNewArrayNode; 112 import org.graalvm.compiler.nodes.java.DynamicNewInstanceNode; 113 import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode; 114 import org.graalvm.compiler.nodes.java.LoadFieldNode; 115 import org.graalvm.compiler.nodes.java.RegisterFinalizerNode; 116 import org.graalvm.compiler.nodes.java.UnsafeCompareAndExchangeNode; 117 import org.graalvm.compiler.nodes.java.UnsafeCompareAndSwapNode; 118 import org.graalvm.compiler.nodes.memory.HeapAccess; 119 import org.graalvm.compiler.nodes.memory.address.IndexAddressNode; 120 import org.graalvm.compiler.nodes.type.StampTool; 121 import org.graalvm.compiler.nodes.util.GraphUtil; 122 import org.graalvm.compiler.nodes.virtual.EnsureVirtualizedNode; 123 import org.graalvm.compiler.replacements.nodes.ProfileBooleanNode; 124 import org.graalvm.compiler.replacements.nodes.ReverseBytesNode; 125 import org.graalvm.compiler.replacements.nodes.VirtualizableInvokeMacroNode; 126 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerAddExactNode; 127 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerAddExactOverflowNode; 128 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerAddExactSplitNode; 129 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerExactArithmeticSplitNode; 130 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerMulExactNode; 131 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerMulExactOverflowNode; 132 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerMulExactSplitNode; 133 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerSubExactNode; 134 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerSubExactOverflowNode; 135 import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerSubExactSplitNode; 136 import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup; 137 import jdk.internal.vm.compiler.word.LocationIdentity; 138 139 import jdk.vm.ci.code.BytecodePosition; 140 import jdk.vm.ci.meta.DeoptimizationAction; 141 import jdk.vm.ci.meta.DeoptimizationReason; 142 import jdk.vm.ci.meta.JavaConstant; 143 import jdk.vm.ci.meta.JavaKind; 144 import jdk.vm.ci.meta.MetaAccessProvider; 145 import jdk.vm.ci.meta.ResolvedJavaField; 146 import jdk.vm.ci.meta.ResolvedJavaMethod; 147 import jdk.vm.ci.meta.ResolvedJavaType; 148 import jdk.vm.ci.meta.SpeculationLog; 149 import jdk.vm.ci.meta.SpeculationLog.Speculation; 150 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason; 151 import sun.misc.Unsafe; 152 153 /** 154 * Provides non-runtime specific {@link InvocationPlugin}s. 155 */ 156 public class StandardGraphBuilderPlugins { 157 158 public static void registerInvocationPlugins(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection, InvocationPlugins plugins, BytecodeProvider bytecodeProvider, 159 boolean allowDeoptimization, boolean explicitUnsafeNullChecks) { 160 registerObjectPlugins(plugins); 161 registerClassPlugins(plugins); 162 registerMathPlugins(plugins, allowDeoptimization); 163 registerStrictMathPlugins(plugins); 164 registerUnsignedMathPlugins(plugins); 165 registerStringPlugins(plugins, bytecodeProvider, snippetReflection); 166 registerCharacterPlugins(plugins); 167 registerShortPlugins(plugins); 168 registerIntegerLongPlugins(plugins, JavaKind.Int); 169 registerIntegerLongPlugins(plugins, JavaKind.Long); 170 registerFloatPlugins(plugins); 171 registerDoublePlugins(plugins); 172 registerArraysPlugins(plugins, bytecodeProvider); 173 registerArrayPlugins(plugins, bytecodeProvider); 174 registerUnsafePlugins(plugins, bytecodeProvider, explicitUnsafeNullChecks); 175 registerEdgesPlugins(metaAccess, plugins); 176 registerGraalDirectivesPlugins(plugins); 177 registerBoxingPlugins(plugins); 178 registerJMHBlackholePlugins(plugins, bytecodeProvider); 179 registerJFRThrowablePlugins(plugins, bytecodeProvider); 180 registerMethodHandleImplPlugins(plugins, snippetReflection, bytecodeProvider); 181 registerJcovCollectPlugins(plugins, bytecodeProvider); 182 } 183 184 private static final Field STRING_VALUE_FIELD; 185 private static final Field STRING_CODER_FIELD; 186 187 static { 188 Field coder = null; 189 try { 190 STRING_VALUE_FIELD = String.class.getDeclaredField("value"); 191 if (!Java8OrEarlier) { 192 coder = String.class.getDeclaredField("coder"); 193 } 194 } catch (NoSuchFieldException e) { 195 throw new GraalError(e); 196 } 197 STRING_CODER_FIELD = coder; 198 } 199 200 private static void registerStringPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider, SnippetReflectionProvider snippetReflection) { 201 final Registration r = new Registration(plugins, String.class, bytecodeProvider); 202 r.register1("hashCode", Receiver.class, new InvocationPlugin() { 203 @Override 204 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 205 if (receiver.isConstant()) { 206 String s = snippetReflection.asObject(String.class, (JavaConstant) receiver.get().asConstant()); 207 if (s != null) { 208 b.addPush(JavaKind.Int, b.add(ConstantNode.forInt(s.hashCode()))); 209 return true; 210 } 211 } 212 return false; 213 } 214 }); 215 r.register1("intern", Receiver.class, new InvocationPlugin() { 216 @Override 217 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 218 if (receiver.isConstant()) { 219 String s = snippetReflection.asObject(String.class, (JavaConstant) receiver.get().asConstant()); 220 if (s != null) { 221 JavaConstant interned = snippetReflection.forObject(s.intern()); 222 b.addPush(JavaKind.Object, b.add(ConstantNode.forConstant(interned, b.getMetaAccess(), b.getGraph()))); 223 return true; 224 } 225 } 226 return false; 227 } 228 }); 229 230 if (Java8OrEarlier) { 231 r.registerMethodSubstitution(StringSubstitutions.class, "equals", Receiver.class, Object.class); 232 233 r.register7("indexOf", char[].class, int.class, int.class, char[].class, int.class, int.class, int.class, new StringIndexOfConstantPlugin()); 234 235 Registration sr = new Registration(plugins, StringSubstitutions.class); 236 sr.register1("getValue", String.class, new InvocationPlugin() { 237 @Override 238 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 239 ResolvedJavaField field = b.getMetaAccess().lookupJavaField(STRING_VALUE_FIELD); 240 b.addPush(JavaKind.Object, LoadFieldNode.create(b.getConstantFieldProvider(), b.getConstantReflection(), b.getMetaAccess(), 241 b.getOptions(), b.getAssumptions(), value, field, false, false)); 242 return true; 243 } 244 }); 245 } else { 246 r.registerMethodSubstitution(JDK9StringSubstitutions.class, "equals", Receiver.class, Object.class); 247 Registration utf16sub = new Registration(plugins, StringUTF16Substitutions.class, bytecodeProvider); 248 utf16sub.register2("getCharDirect", byte[].class, int.class, new InvocationPlugin() { 249 @Override 250 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg1, ValueNode arg2) { 251 b.addPush(JavaKind.Char, new JavaReadNode(JavaKind.Char, new IndexAddressNode(arg1, arg2, JavaKind.Byte), NamedLocationIdentity.getArrayLocation(JavaKind.Byte), 252 HeapAccess.BarrierType.NONE, false)); 253 return true; 254 } 255 }); 256 utf16sub.register3("putCharDirect", byte[].class, int.class, int.class, new InvocationPlugin() { 257 @Override 258 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg1, ValueNode arg2, ValueNode arg3) { 259 b.add(new JavaWriteNode(JavaKind.Char, new IndexAddressNode(arg1, arg2, JavaKind.Byte), NamedLocationIdentity.getArrayLocation(JavaKind.Byte), arg3, 260 HeapAccess.BarrierType.NONE, false)); 261 return true; 262 } 263 }); 264 265 final Registration latin1r = new Registration(plugins, "java.lang.StringLatin1", bytecodeProvider); 266 latin1r.register5("indexOf", byte[].class, int.class, byte[].class, int.class, int.class, new StringLatin1IndexOfConstantPlugin()); 267 268 final Registration utf16r = new Registration(plugins, "java.lang.StringUTF16", bytecodeProvider); 269 utf16r.register5("indexOfUnsafe", byte[].class, int.class, byte[].class, int.class, int.class, new StringUTF16IndexOfConstantPlugin()); 270 utf16r.setAllowOverwrite(true); 271 utf16r.registerMethodSubstitution(StringUTF16Substitutions.class, "getChar", byte[].class, int.class); 272 utf16r.registerMethodSubstitution(StringUTF16Substitutions.class, "putChar", byte[].class, int.class, int.class); 273 274 Registration sr = new Registration(plugins, JDK9StringSubstitutions.class); 275 sr.register1("getValue", String.class, new InvocationPlugin() { 276 @Override 277 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 278 ResolvedJavaField field = b.getMetaAccess().lookupJavaField(STRING_VALUE_FIELD); 279 b.addPush(JavaKind.Object, LoadFieldNode.create(b.getConstantFieldProvider(), b.getConstantReflection(), b.getMetaAccess(), 280 b.getOptions(), b.getAssumptions(), value, field, false, false)); 281 return true; 282 } 283 }); 284 sr.register1("getCoder", String.class, new InvocationPlugin() { 285 @Override 286 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 287 ResolvedJavaField field = b.getMetaAccess().lookupJavaField(STRING_CODER_FIELD); 288 b.addPush(JavaKind.Int, LoadFieldNode.create(b.getConstantFieldProvider(), b.getConstantReflection(), b.getMetaAccess(), 289 b.getOptions(), b.getAssumptions(), value, field, false, false)); 290 return true; 291 } 292 }); 293 } 294 } 295 296 private static void registerArraysPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { 297 Registration r = new Registration(plugins, Arrays.class, bytecodeProvider); 298 r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", boolean[].class, boolean[].class); 299 r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", byte[].class, byte[].class); 300 r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", short[].class, short[].class); 301 r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", char[].class, char[].class); 302 r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", int[].class, int[].class); 303 r.registerMethodSubstitution(ArraysSubstitutions.class, "equals", long[].class, long[].class); 304 } 305 306 private static void registerArrayPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { 307 Registration r = new Registration(plugins, Array.class, bytecodeProvider); 308 r.register2("newInstance", Class.class, int.class, new InvocationPlugin() { 309 @Override 310 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode componentType, ValueNode length) { 311 b.addPush(JavaKind.Object, new DynamicNewArrayNode(componentType, length, true)); 312 return true; 313 } 314 }); 315 r.registerMethodSubstitution(ArraySubstitutions.class, "getLength", Object.class); 316 } 317 318 /** 319 * The intrinsic for {@link Math#sqrt(double)} is shared with {@link StrictMath#sqrt(double)}. 320 * 321 * @see "http://hg.openjdk.java.net/jdk/jdk/file/621efe32eb0b/src/hotspot/share/oops/method.cpp#l1504" 322 */ 323 static final class MathSqrtPlugin implements InvocationPlugin { 324 @Override 325 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 326 b.push(JavaKind.Double, b.append(SqrtNode.create(value, NodeView.DEFAULT))); 327 return true; 328 } 329 } 330 331 private abstract static class UnsafeCompareAndUpdatePluginsRegistrar { 332 public void register(Registration r, String casPrefix, boolean explicitUnsafeNullChecks, JavaKind[] compareAndSwapTypes, boolean java11OrEarlier) { 333 for (JavaKind kind : compareAndSwapTypes) { 334 Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); 335 String kindName = (kind == JavaKind.Object && !java11OrEarlier) ? "Reference" : kind.name(); 336 r.register5(casPrefix + kindName, Receiver.class, Object.class, long.class, javaClass, javaClass, new UnsafeAccessPlugin(returnKind(kind), explicitUnsafeNullChecks) { 337 @Override 338 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode expected, ValueNode x) { 339 // Emits a null-check for the otherwise unused receiver 340 unsafe.get(); 341 createUnsafeAccess(object, b, (obj, loc) -> UnsafeCompareAndUpdatePluginsRegistrar.this.createNode(obj, offset, expected, x, kind, loc)); 342 return true; 343 } 344 }); 345 } 346 } 347 348 public abstract FixedWithNextNode createNode(ValueNode object, ValueNode offset, ValueNode expected, ValueNode newValue, JavaKind kind, LocationIdentity identity); 349 350 public abstract JavaKind returnKind(JavaKind accessKind); 351 } 352 353 private static class UnsafeCompareAndSwapPluginsRegistrar extends UnsafeCompareAndUpdatePluginsRegistrar { 354 @Override 355 public FixedWithNextNode createNode(ValueNode object, ValueNode offset, ValueNode expected, ValueNode newValue, JavaKind kind, LocationIdentity identity) { 356 return new UnsafeCompareAndSwapNode(object, offset, expected, newValue, kind, identity); 357 } 358 359 @Override 360 public JavaKind returnKind(JavaKind accessKind) { 361 return JavaKind.Boolean.getStackKind(); 362 } 363 } 364 365 private static UnsafeCompareAndSwapPluginsRegistrar unsafeCompareAndSwapPluginsRegistrar = new UnsafeCompareAndSwapPluginsRegistrar(); 366 367 private static class UnsafeCompareAndExchangePluginsRegistrar extends UnsafeCompareAndUpdatePluginsRegistrar { 368 @Override 369 public FixedWithNextNode createNode(ValueNode object, ValueNode offset, ValueNode expected, ValueNode newValue, JavaKind kind, LocationIdentity identity) { 370 return new UnsafeCompareAndExchangeNode(object, offset, expected, newValue, kind, identity); 371 } 372 373 @Override 374 public JavaKind returnKind(JavaKind accessKind) { 375 if (accessKind.isNumericInteger()) { 376 return accessKind.getStackKind(); 377 } else { 378 return accessKind; 379 } 380 } 381 } 382 383 private static UnsafeCompareAndExchangePluginsRegistrar unsafeCompareAndExchangePluginsRegistrar = new UnsafeCompareAndExchangePluginsRegistrar(); 384 385 public static void registerPlatformSpecificUnsafePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider, boolean explicitUnsafeNullChecks, JavaKind[] supportedCasKinds) { 386 registerPlatformSpecificUnsafePlugins(supportedCasKinds, new Registration(plugins, Unsafe.class), true, explicitUnsafeNullChecks); 387 if (!Java8OrEarlier) { 388 registerPlatformSpecificUnsafePlugins(supportedCasKinds, new Registration(plugins, "jdk.internal.misc.Unsafe", bytecodeProvider), false, explicitUnsafeNullChecks); 389 } 390 391 } 392 393 private static void registerPlatformSpecificUnsafePlugins(JavaKind[] supportedCasKinds, Registration r, boolean java8OrEarlier, boolean explicitUnsafeNullChecks) { 394 if (java8OrEarlier) { 395 unsafeCompareAndSwapPluginsRegistrar.register(r, "compareAndSwap", explicitUnsafeNullChecks, new JavaKind[]{JavaKind.Int, JavaKind.Long, JavaKind.Object}, true); 396 } else { 397 unsafeCompareAndSwapPluginsRegistrar.register(r, "compareAndSet", explicitUnsafeNullChecks, supportedCasKinds, Java11OrEarlier); 398 unsafeCompareAndExchangePluginsRegistrar.register(r, "compareAndExchange", explicitUnsafeNullChecks, supportedCasKinds, Java11OrEarlier); 399 } 400 } 401 402 private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider, boolean explicitUnsafeNullChecks) { 403 registerUnsafePlugins(new Registration(plugins, Unsafe.class), true, explicitUnsafeNullChecks); 404 if (!Java8OrEarlier) { 405 registerUnsafePlugins(new Registration(plugins, "jdk.internal.misc.Unsafe", bytecodeProvider), false, explicitUnsafeNullChecks); 406 } 407 } 408 409 private static void registerUnsafePlugins(Registration r, boolean sunMiscUnsafe, boolean explicitUnsafeNullChecks) { 410 for (JavaKind kind : JavaKind.values()) { 411 if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { 412 Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); 413 String kindName = (kind == JavaKind.Object && !sunMiscUnsafe && !Java11OrEarlier) ? "Reference" : kind.name(); 414 String getName = "get" + kindName; 415 String putName = "put" + kindName; 416 // Object-based accesses 417 r.register3(getName, Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, explicitUnsafeNullChecks)); 418 r.register4(putName, Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, explicitUnsafeNullChecks)); 419 // Volatile object-based accesses 420 r.register3(getName + "Volatile", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, AccessKind.VOLATILE, explicitUnsafeNullChecks)); 421 r.register4(putName + "Volatile", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, AccessKind.VOLATILE, explicitUnsafeNullChecks)); 422 // Ordered object-based accesses 423 if (sunMiscUnsafe) { 424 if (kind == JavaKind.Int || kind == JavaKind.Long || kind == JavaKind.Object) { 425 r.register4("putOrdered" + kindName, Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, AccessKind.RELEASE_ACQUIRE, explicitUnsafeNullChecks)); 426 } 427 } else { 428 r.register4("put" + kindName + "Release", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, AccessKind.RELEASE_ACQUIRE, explicitUnsafeNullChecks)); 429 r.register3("get" + kindName + "Acquire", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, AccessKind.RELEASE_ACQUIRE, explicitUnsafeNullChecks)); 430 r.register4("put" + kindName + "Opaque", Receiver.class, Object.class, long.class, javaClass, new UnsafePutPlugin(kind, AccessKind.OPAQUE, explicitUnsafeNullChecks)); 431 r.register3("get" + kindName + "Opaque", Receiver.class, Object.class, long.class, new UnsafeGetPlugin(kind, AccessKind.OPAQUE, explicitUnsafeNullChecks)); 432 } 433 if (kind != JavaKind.Boolean && kind != JavaKind.Object) { 434 // Raw accesses to memory addresses 435 r.register2(getName, Receiver.class, long.class, new UnsafeGetPlugin(kind, explicitUnsafeNullChecks)); 436 r.register3(putName, Receiver.class, long.class, kind.toJavaClass(), new UnsafePutPlugin(kind, explicitUnsafeNullChecks)); 437 } 438 } 439 } 440 441 // Accesses to native memory addresses. 442 r.register2("getAddress", Receiver.class, long.class, new UnsafeGetPlugin(JavaKind.Long, explicitUnsafeNullChecks)); 443 r.register3("putAddress", Receiver.class, long.class, long.class, new UnsafePutPlugin(JavaKind.Long, explicitUnsafeNullChecks)); 444 445 r.register2("allocateInstance", Receiver.class, Class.class, new InvocationPlugin() { 446 447 @Override 448 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode clazz) { 449 // Emits a null-check for the otherwise unused receiver 450 unsafe.get(); 451 b.addPush(JavaKind.Object, new DynamicNewInstanceNode(b.nullCheckedValue(clazz, DeoptimizationAction.None), true)); 452 return true; 453 } 454 455 }); 456 457 r.register1("loadFence", Receiver.class, new UnsafeFencePlugin(LOAD_LOAD | LOAD_STORE)); 458 r.register1("storeFence", Receiver.class, new UnsafeFencePlugin(STORE_STORE | LOAD_STORE)); 459 r.register1("fullFence", Receiver.class, new UnsafeFencePlugin(LOAD_LOAD | STORE_STORE | LOAD_STORE | STORE_LOAD)); 460 } 461 462 private static void registerIntegerLongPlugins(InvocationPlugins plugins, JavaKind kind) { 463 Class<?> declaringClass = kind.toBoxedJavaClass(); 464 Class<?> type = kind.toJavaClass(); 465 Registration r = new Registration(plugins, declaringClass); 466 r.register1("reverseBytes", type, new InvocationPlugin() { 467 @Override 468 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 469 b.push(kind, b.append(new ReverseBytesNode(value).canonical(null))); 470 return true; 471 } 472 }); 473 r.register2("divideUnsigned", type, type, new InvocationPlugin() { 474 @Override 475 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode dividend, ValueNode divisor) { 476 b.push(kind, b.append(UnsignedDivNode.create(dividend, divisor, null, NodeView.DEFAULT))); 477 return true; 478 } 479 }); 480 r.register2("remainderUnsigned", type, type, new InvocationPlugin() { 481 @Override 482 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode dividend, ValueNode divisor) { 483 b.push(kind, b.append(UnsignedRemNode.create(dividend, divisor, null, NodeView.DEFAULT))); 484 return true; 485 } 486 }); 487 } 488 489 private static void registerCharacterPlugins(InvocationPlugins plugins) { 490 Registration r = new Registration(plugins, Character.class); 491 r.register1("reverseBytes", char.class, new InvocationPlugin() { 492 @Override 493 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 494 // return (char) (Integer.reverse(i) >> 16); 495 ReverseBytesNode reverse = b.add(new ReverseBytesNode(value)); 496 RightShiftNode rightShift = b.add(new RightShiftNode(reverse, b.add(ConstantNode.forInt(16)))); 497 ZeroExtendNode charCast = b.add(new ZeroExtendNode(b.add(new NarrowNode(rightShift, 16)), 32)); 498 b.push(JavaKind.Char, b.append(charCast.canonical(null))); 499 return true; 500 } 501 }); 502 } 503 504 private static void registerShortPlugins(InvocationPlugins plugins) { 505 Registration r = new Registration(plugins, Short.class); 506 r.register1("reverseBytes", short.class, new InvocationPlugin() { 507 @Override 508 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 509 // return (short) (Integer.reverse(i) >> 16); 510 ReverseBytesNode reverse = b.add(new ReverseBytesNode(value)); 511 RightShiftNode rightShift = b.add(new RightShiftNode(reverse, b.add(ConstantNode.forInt(16)))); 512 SignExtendNode charCast = b.add(new SignExtendNode(b.add(new NarrowNode(rightShift, 16)), 32)); 513 b.push(JavaKind.Short, b.append(charCast.canonical(null))); 514 return true; 515 } 516 }); 517 } 518 519 private static void registerFloatPlugins(InvocationPlugins plugins) { 520 Registration r = new Registration(plugins, Float.class); 521 r.register1("floatToRawIntBits", float.class, new InvocationPlugin() { 522 @Override 523 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 524 b.push(JavaKind.Int, b.append(ReinterpretNode.create(JavaKind.Int, value, NodeView.DEFAULT))); 525 return true; 526 } 527 }); 528 r.register1("floatToIntBits", float.class, new InvocationPlugin() { 529 @Override 530 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 531 LogicNode notNan = b.append(FloatEqualsNode.create(value, value, NodeView.DEFAULT)); 532 ValueNode raw = b.append(ReinterpretNode.create(JavaKind.Int, value, NodeView.DEFAULT)); 533 ValueNode result = b.append(ConditionalNode.create(notNan, raw, ConstantNode.forInt(0x7fc00000), NodeView.DEFAULT)); 534 b.push(JavaKind.Int, result); 535 return true; 536 } 537 }); 538 r.register1("intBitsToFloat", int.class, new InvocationPlugin() { 539 @Override 540 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 541 b.push(JavaKind.Float, b.append(ReinterpretNode.create(JavaKind.Float, value, NodeView.DEFAULT))); 542 return true; 543 } 544 }); 545 } 546 547 private static void registerDoublePlugins(InvocationPlugins plugins) { 548 Registration r = new Registration(plugins, Double.class); 549 r.register1("doubleToRawLongBits", double.class, new InvocationPlugin() { 550 @Override 551 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 552 b.push(JavaKind.Long, b.append(ReinterpretNode.create(JavaKind.Long, value, NodeView.DEFAULT))); 553 return true; 554 } 555 }); 556 r.register1("doubleToLongBits", double.class, new InvocationPlugin() { 557 @Override 558 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 559 LogicNode notNan = b.append(FloatEqualsNode.create(value, value, NodeView.DEFAULT)); 560 ValueNode raw = b.append(ReinterpretNode.create(JavaKind.Long, value, NodeView.DEFAULT)); 561 ValueNode result = b.append(ConditionalNode.create(notNan, raw, ConstantNode.forLong(0x7ff8000000000000L), NodeView.DEFAULT)); 562 b.push(JavaKind.Long, result); 563 return true; 564 } 565 }); 566 r.register1("longBitsToDouble", long.class, new InvocationPlugin() { 567 @Override 568 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 569 b.push(JavaKind.Double, b.append(ReinterpretNode.create(JavaKind.Double, value, NodeView.DEFAULT))); 570 return true; 571 } 572 }); 573 } 574 575 public enum IntegerExactOp { 576 INTEGER_ADD_EXACT, 577 INTEGER_INCREMENT_EXACT, 578 INTEGER_SUBTRACT_EXACT, 579 INTEGER_DECREMENT_EXACT, 580 INTEGER_MULTIPLY_EXACT 581 } 582 583 private static GuardingNode createIntegerExactArithmeticGuardNode(GraphBuilderContext b, ValueNode x, ValueNode y, IntegerExactOp op) { 584 LogicNode overflowCheck; 585 switch (op) { 586 case INTEGER_ADD_EXACT: 587 case INTEGER_INCREMENT_EXACT: { 588 overflowCheck = new IntegerAddExactOverflowNode(x, y); 589 break; 590 } 591 case INTEGER_SUBTRACT_EXACT: 592 case INTEGER_DECREMENT_EXACT: { 593 overflowCheck = new IntegerSubExactOverflowNode(x, y); 594 break; 595 } 596 case INTEGER_MULTIPLY_EXACT: { 597 overflowCheck = new IntegerMulExactOverflowNode(x, y); 598 break; 599 } 600 default: 601 throw GraalError.shouldNotReachHere("Unknown integer exact operation."); 602 } 603 return b.add(new FixedGuardNode(overflowCheck, DeoptimizationReason.ArithmeticException, DeoptimizationAction.InvalidateRecompile, true)); 604 } 605 606 private static ValueNode createIntegerExactArithmeticNode(GraphBuilderContext b, ValueNode x, ValueNode y, IntegerExactOp op) { 607 switch (op) { 608 case INTEGER_ADD_EXACT: 609 case INTEGER_INCREMENT_EXACT: 610 return new IntegerAddExactNode(x, y, createIntegerExactArithmeticGuardNode(b, x, y, op)); 611 case INTEGER_SUBTRACT_EXACT: 612 case INTEGER_DECREMENT_EXACT: 613 return new IntegerSubExactNode(x, y, createIntegerExactArithmeticGuardNode(b, x, y, op)); 614 case INTEGER_MULTIPLY_EXACT: 615 return new IntegerMulExactNode(x, y, createIntegerExactArithmeticGuardNode(b, x, y, op)); 616 default: 617 throw GraalError.shouldNotReachHere("Unknown integer exact operation."); 618 } 619 } 620 621 private static IntegerExactArithmeticSplitNode createIntegerExactSplit(ValueNode x, ValueNode y, AbstractBeginNode exceptionEdge, IntegerExactOp op) { 622 switch (op) { 623 case INTEGER_ADD_EXACT: 624 case INTEGER_INCREMENT_EXACT: 625 return new IntegerAddExactSplitNode(x.stamp(NodeView.DEFAULT).unrestricted(), x, y, null, exceptionEdge); 626 case INTEGER_SUBTRACT_EXACT: 627 case INTEGER_DECREMENT_EXACT: 628 return new IntegerSubExactSplitNode(x.stamp(NodeView.DEFAULT).unrestricted(), x, y, null, exceptionEdge); 629 case INTEGER_MULTIPLY_EXACT: 630 return new IntegerMulExactSplitNode(x.stamp(NodeView.DEFAULT).unrestricted(), x, y, null, exceptionEdge); 631 default: 632 throw GraalError.shouldNotReachHere("Unknown integer exact operation."); 633 } 634 } 635 636 private static void createIntegerExactOperation(GraphBuilderContext b, JavaKind kind, ValueNode x, ValueNode y, IntegerExactOp op) { 637 if (b.needsExplicitException()) { 638 BytecodeExceptionKind exceptionKind = kind == JavaKind.Int ? BytecodeExceptionKind.INTEGER_EXACT_OVERFLOW : BytecodeExceptionKind.LONG_EXACT_OVERFLOW; 639 AbstractBeginNode exceptionEdge = b.genExplicitExceptionEdge(exceptionKind); 640 IntegerExactArithmeticSplitNode split = b.addPush(kind, createIntegerExactSplit(x, y, exceptionEdge, op)); 641 split.setNext(b.add(new BeginNode())); 642 } else { 643 b.addPush(kind, createIntegerExactArithmeticNode(b, x, y, op)); 644 } 645 } 646 647 private static void registerMathPlugins(InvocationPlugins plugins, boolean allowDeoptimization) { 648 Registration r = new Registration(plugins, Math.class); 649 if (allowDeoptimization) { 650 for (JavaKind kind : new JavaKind[]{JavaKind.Int, JavaKind.Long}) { 651 Class<?> type = kind.toJavaClass(); 652 r.register1("decrementExact", type, new InvocationPlugin() { 653 @Override 654 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x) { 655 ConstantNode y = b.add(ConstantNode.forIntegerKind(kind, 1)); 656 createIntegerExactOperation(b, kind, x, y, IntegerExactOp.INTEGER_DECREMENT_EXACT); 657 return true; 658 } 659 }); 660 661 r.register1("incrementExact", type, new InvocationPlugin() { 662 @Override 663 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x) { 664 ConstantNode y = b.add(ConstantNode.forIntegerKind(kind, 1)); 665 createIntegerExactOperation(b, kind, x, y, IntegerExactOp.INTEGER_INCREMENT_EXACT); 666 return true; 667 } 668 }); 669 r.register2("addExact", type, type, new InvocationPlugin() { 670 @Override 671 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { 672 createIntegerExactOperation(b, kind, x, y, IntegerExactOp.INTEGER_ADD_EXACT); 673 return true; 674 } 675 }); 676 r.register2("subtractExact", type, type, new InvocationPlugin() { 677 @Override 678 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { 679 createIntegerExactOperation(b, kind, x, y, IntegerExactOp.INTEGER_SUBTRACT_EXACT); 680 return true; 681 } 682 }); 683 684 r.register2("multiplyExact", type, type, new InvocationPlugin() { 685 @Override 686 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { 687 createIntegerExactOperation(b, kind, x, y, IntegerExactOp.INTEGER_MULTIPLY_EXACT); 688 return true; 689 } 690 }); 691 } 692 } 693 r.register1("abs", Float.TYPE, new InvocationPlugin() { 694 695 @Override 696 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 697 b.push(JavaKind.Float, b.append(new AbsNode(value).canonical(null))); 698 return true; 699 } 700 }); 701 r.register1("abs", Double.TYPE, new InvocationPlugin() { 702 @Override 703 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 704 b.push(JavaKind.Double, b.append(new AbsNode(value).canonical(null))); 705 return true; 706 } 707 }); 708 r.register1("sqrt", Double.TYPE, new MathSqrtPlugin()); 709 } 710 711 private static void registerStrictMathPlugins(InvocationPlugins plugins) { 712 Registration r = new Registration(plugins, StrictMath.class); 713 r.register1("sqrt", Double.TYPE, new MathSqrtPlugin()); 714 } 715 716 public static final class StringIndexOfConstantPlugin implements InvocationPlugin { 717 @Override 718 public boolean inlineOnly() { 719 return true; 720 } 721 722 @Override 723 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode source, ValueNode sourceOffset, ValueNode sourceCount, 724 ValueNode target, ValueNode targetOffset, ValueNode targetCount, ValueNode origFromIndex) { 725 if (target.isConstant()) { 726 b.addPush(JavaKind.Int, new StringIndexOfNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), source, sourceOffset, sourceCount, 727 target, targetOffset, targetCount, origFromIndex)); 728 return true; 729 } 730 return false; 731 } 732 } 733 734 public static final class StringLatin1IndexOfConstantPlugin implements InvocationPlugin { 735 @Override 736 public boolean inlineOnly() { 737 return true; 738 } 739 740 @Override 741 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, 742 ValueNode source, ValueNode sourceCount, ValueNode target, ValueNode targetCount, ValueNode origFromIndex) { 743 if (target.isConstant()) { 744 b.addPush(JavaKind.Int, new StringLatin1IndexOfNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), 745 source, sourceCount, target, targetCount, origFromIndex)); 746 return true; 747 } 748 return false; 749 } 750 } 751 752 public static final class StringUTF16IndexOfConstantPlugin implements InvocationPlugin { 753 @Override 754 public boolean inlineOnly() { 755 return true; 756 } 757 758 @Override 759 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, 760 ValueNode source, ValueNode sourceCount, ValueNode target, ValueNode targetCount, ValueNode origFromIndex) { 761 if (target.isConstant()) { 762 b.addPush(JavaKind.Int, new StringUTF16IndexOfNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), 763 source, sourceCount, target, targetCount, origFromIndex)); 764 return true; 765 } 766 return false; 767 } 768 } 769 770 public static class UnsignedMathPlugin implements InvocationPlugin { 771 private final Condition condition; 772 773 public UnsignedMathPlugin(Condition condition) { 774 this.condition = condition; 775 } 776 777 @Override 778 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode x, ValueNode y) { 779 CanonicalizedCondition canonical = condition.canonicalize(); 780 StructuredGraph graph = b.getGraph(); 781 782 ValueNode lhs = canonical.mustMirror() ? y : x; 783 ValueNode rhs = canonical.mustMirror() ? x : y; 784 785 ValueNode trueValue = ConstantNode.forBoolean(!canonical.mustNegate(), graph); 786 ValueNode falseValue = ConstantNode.forBoolean(canonical.mustNegate(), graph); 787 788 LogicNode compare = CompareNode.createCompareNode(graph, b.getConstantReflection(), b.getMetaAccess(), b.getOptions(), null, canonical.getCanonicalCondition(), lhs, rhs, NodeView.DEFAULT); 789 b.addPush(JavaKind.Boolean, new ConditionalNode(compare, trueValue, falseValue)); 790 return true; 791 } 792 } 793 794 private static void registerUnsignedMathPlugins(InvocationPlugins plugins) { 795 Registration r = new Registration(plugins, UnsignedMath.class); 796 r.register2("aboveThan", int.class, int.class, new UnsignedMathPlugin(Condition.AT)); 797 r.register2("aboveThan", long.class, long.class, new UnsignedMathPlugin(Condition.AT)); 798 r.register2("belowThan", int.class, int.class, new UnsignedMathPlugin(Condition.BT)); 799 r.register2("belowThan", long.class, long.class, new UnsignedMathPlugin(Condition.BT)); 800 r.register2("aboveOrEqual", int.class, int.class, new UnsignedMathPlugin(Condition.AE)); 801 r.register2("aboveOrEqual", long.class, long.class, new UnsignedMathPlugin(Condition.AE)); 802 r.register2("belowOrEqual", int.class, int.class, new UnsignedMathPlugin(Condition.BE)); 803 r.register2("belowOrEqual", long.class, long.class, new UnsignedMathPlugin(Condition.BE)); 804 } 805 806 protected static void registerBoxingPlugins(InvocationPlugins plugins) { 807 for (JavaKind kind : JavaKind.values()) { 808 if (kind.isPrimitive() && kind != JavaKind.Void) { 809 new BoxPlugin(kind).register(plugins); 810 new UnboxPlugin(kind).register(plugins); 811 } 812 } 813 } 814 815 private static void registerObjectPlugins(InvocationPlugins plugins) { 816 Registration r = new Registration(plugins, Object.class); 817 r.register1("<init>", Receiver.class, new InvocationPlugin() { 818 @Override 819 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 820 /* 821 * Object.<init> is a common instrumentation point so only perform this rewrite if 822 * the current definition is the normal empty method with a single return bytecode. 823 * The finalizer registration will instead be performed by the BytecodeParser. 824 */ 825 if (targetMethod.getCodeSize() == 1) { 826 ValueNode object = receiver.get(); 827 if (RegisterFinalizerNode.mayHaveFinalizer(object, b.getAssumptions())) { 828 b.add(new RegisterFinalizerNode(object)); 829 } 830 return true; 831 } 832 return false; 833 } 834 }); 835 r.register1("getClass", Receiver.class, new InvocationPlugin() { 836 @Override 837 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 838 ValueNode object = receiver.get(); 839 ValueNode folded = GetClassNode.tryFold(b.getMetaAccess(), b.getConstantReflection(), NodeView.DEFAULT, GraphUtil.originalValue(object)); 840 if (folded != null) { 841 b.addPush(JavaKind.Object, folded); 842 } else { 843 Stamp stamp = StampFactory.objectNonNull(TypeReference.createTrusted(b.getAssumptions(), b.getMetaAccess().lookupJavaType(Class.class))); 844 b.addPush(JavaKind.Object, new GetClassNode(stamp, object)); 845 } 846 return true; 847 } 848 }); 849 } 850 851 private static void registerClassPlugins(InvocationPlugins plugins) { 852 Registration r = new Registration(plugins, Class.class); 853 r.register2("isInstance", Receiver.class, Object.class, new InvocationPlugin() { 854 @Override 855 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver type, ValueNode object) { 856 LogicNode condition = b.append(InstanceOfDynamicNode.create(b.getAssumptions(), b.getConstantReflection(), type.get(), object, false)); 857 b.push(JavaKind.Boolean, b.append(new ConditionalNode(condition).canonical(null))); 858 return true; 859 } 860 }); 861 r.register2("isAssignableFrom", Receiver.class, Class.class, new InvocationPlugin() { 862 @Override 863 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver type, ValueNode otherType) { 864 ClassIsAssignableFromNode condition = b.append(new ClassIsAssignableFromNode(type.get(), otherType)); 865 b.push(JavaKind.Boolean, b.append(new ConditionalNode(condition).canonical(null))); 866 return true; 867 } 868 }); 869 870 r.register2("cast", Receiver.class, Object.class, new InvocationPlugin() { 871 @Override 872 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) { 873 b.genCheckcastDynamic(object, receiver.get()); 874 return true; 875 } 876 877 @Override 878 public boolean inlineOnly() { 879 return true; 880 } 881 }); 882 } 883 884 /** 885 * Substitutions for improving the performance of some critical methods in {@link Edges}. These 886 * substitutions improve the performance by forcing the relevant methods to be inlined 887 * (intrinsification being a special form of inlining) and removing a checked cast. 888 */ 889 private static void registerEdgesPlugins(MetaAccessProvider metaAccess, InvocationPlugins plugins) { 890 Registration r = new Registration(plugins, Edges.class); 891 for (Class<?> c : new Class<?>[]{Node.class, NodeList.class}) { 892 r.register2("get" + c.getSimpleName() + "Unsafe", Node.class, long.class, new InvocationPlugin() { 893 @Override 894 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode node, ValueNode offset) { 895 ObjectStamp stamp = StampFactory.object(TypeReference.createTrusted(b.getAssumptions(), metaAccess.lookupJavaType(c))); 896 RawLoadNode value = b.add(new RawLoadNode(stamp, node, offset, LocationIdentity.any(), JavaKind.Object)); 897 b.addPush(JavaKind.Object, value); 898 return true; 899 } 900 }); 901 r.register3("put" + c.getSimpleName() + "Unsafe", Node.class, long.class, c, new InvocationPlugin() { 902 @Override 903 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode node, ValueNode offset, ValueNode value) { 904 b.add(new RawStoreNode(node, offset, value, JavaKind.Object, LocationIdentity.any())); 905 return true; 906 } 907 }); 908 } 909 } 910 911 public static class BoxPlugin implements InvocationPlugin { 912 913 private final JavaKind kind; 914 915 BoxPlugin(JavaKind kind) { 916 this.kind = kind; 917 } 918 919 @Override 920 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 921 if (b.parsingIntrinsic()) { 922 ResolvedJavaMethod rootMethod = b.getGraph().method(); 923 if (b.getMetaAccess().lookupJavaType(BoxingSnippets.class).isAssignableFrom(rootMethod.getDeclaringClass())) { 924 // Disable invocation plugins for boxing snippets so that the 925 // original JDK methods are inlined 926 return false; 927 } 928 } 929 ResolvedJavaType resultType = b.getMetaAccess().lookupJavaType(kind.toBoxedJavaClass()); 930 b.addPush(JavaKind.Object, new BoxNode(value, resultType, kind)); 931 return true; 932 } 933 934 void register(InvocationPlugins plugins) { 935 plugins.register(this, kind.toBoxedJavaClass(), "valueOf", kind.toJavaClass()); 936 } 937 } 938 939 public static class UnboxPlugin implements InvocationPlugin { 940 941 private final JavaKind kind; 942 943 UnboxPlugin(JavaKind kind) { 944 this.kind = kind; 945 } 946 947 @Override 948 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 949 if (b.parsingIntrinsic()) { 950 ResolvedJavaMethod rootMethod = b.getGraph().method(); 951 if (b.getMetaAccess().lookupJavaType(BoxingSnippets.class).isAssignableFrom(rootMethod.getDeclaringClass())) { 952 // Disable invocation plugins for unboxing snippets so that the 953 // original JDK methods are inlined 954 return false; 955 } 956 } 957 ValueNode valueNode = UnboxNode.create(b.getMetaAccess(), b.getConstantReflection(), receiver.get(), kind); 958 b.addPush(kind, valueNode); 959 return true; 960 } 961 962 void register(InvocationPlugins plugins) { 963 String name = kind.toJavaClass().getSimpleName() + "Value"; 964 plugins.register(this, kind.toBoxedJavaClass(), name, Receiver.class); 965 } 966 } 967 968 /** 969 * The new memory order modes (JDK9+) are defined with cumulative effect, from weakest to 970 * strongest: Plain, Opaque, Release/Acquire, and Volatile. The existing Plain and Volatile 971 * modes are defined compatibly with their pre-JDK 9 forms. Any guaranteed property of a weaker 972 * mode, plus more, holds for a stronger mode. (Conversely, implementations are allowed to use a 973 * stronger mode than requested for any access.) In JDK 9, these are provided without a full 974 * formal specification. 975 */ 976 enum AccessKind { 977 PLAIN(0, 0, 0, 0, false), 978 /** 979 * Opaque accesses are wrapped by dummy membars to avoid floating/hoisting, this is stronger 980 * than required since Opaque mode does not directly impose any ordering constraints with 981 * respect to other variables beyond Plain mode. 982 */ 983 OPAQUE(0, 0, 0, 0, true), 984 RELEASE_ACQUIRE(0, LOAD_LOAD | LOAD_STORE, LOAD_STORE | STORE_STORE, 0, true), 985 VOLATILE(JMM_PRE_VOLATILE_READ, JMM_POST_VOLATILE_READ, JMM_PRE_VOLATILE_WRITE, JMM_POST_VOLATILE_WRITE, true); 986 987 public final boolean emitBarriers; 988 public final int preReadBarriers; 989 public final int postReadBarriers; 990 public final int preWriteBarriers; 991 public final int postWriteBarriers; 992 993 AccessKind(int preReadBarriers, int postReadBarriers, int preWriteBarriers, int postWriteBarriers, boolean emitBarriers) { 994 this.emitBarriers = emitBarriers; 995 this.preReadBarriers = preReadBarriers; 996 this.postReadBarriers = postReadBarriers; 997 this.preWriteBarriers = preWriteBarriers; 998 this.postWriteBarriers = postWriteBarriers; 999 } 1000 } 1001 1002 /** 1003 * Unsafe access relative to null object is an access to off-heap memory. As linear pointer 1004 * compression uses non-zero null, here null object must be replaced with zero constant. 1005 */ 1006 public abstract static class UnsafeAccessPlugin implements InvocationPlugin { 1007 @FunctionalInterface 1008 public interface UnsafeNodeConstructor { 1009 FixedWithNextNode create(ValueNode value, LocationIdentity location); 1010 } 1011 1012 protected final JavaKind unsafeAccessKind; 1013 private final boolean explicitUnsafeNullChecks; 1014 1015 public UnsafeAccessPlugin(JavaKind kind, boolean explicitUnsafeNullChecks) { 1016 unsafeAccessKind = kind; 1017 this.explicitUnsafeNullChecks = explicitUnsafeNullChecks; 1018 } 1019 1020 private static FixedWithNextNode createObjectAccessNode(ValueNode value, UnsafeNodeConstructor nodeConstructor) { 1021 return nodeConstructor.create(value, LocationIdentity.ANY_LOCATION); 1022 } 1023 1024 private static FixedWithNextNode createMemoryAccessNode(StructuredGraph graph, UnsafeNodeConstructor nodeConstructor) { 1025 return nodeConstructor.create(ConstantNode.forLong(0L, graph), OFF_HEAP_LOCATION); 1026 } 1027 1028 private static boolean isLoad(ValueNode node) { 1029 return node.getStackKind() != JavaKind.Void; 1030 } 1031 1032 private void setResult(ValueNode node, GraphBuilderContext b) { 1033 if (isLoad(node)) { 1034 b.addPush(unsafeAccessKind, node); 1035 } else { 1036 b.add(node); 1037 } 1038 } 1039 1040 protected final void createUnsafeAccess(ValueNode value, GraphBuilderContext b, UnsafeNodeConstructor nodeConstructor) { 1041 StructuredGraph graph = b.getGraph(); 1042 graph.markUnsafeAccess(); 1043 /* For unsafe access object pointers can only be stored in the heap */ 1044 if (unsafeAccessKind == JavaKind.Object) { 1045 setResult(createObjectAccessNode(value, nodeConstructor), b); 1046 } else if (StampTool.isPointerAlwaysNull(value)) { 1047 setResult(createMemoryAccessNode(graph, nodeConstructor), b); 1048 } else if (!explicitUnsafeNullChecks || StampTool.isPointerNonNull(value)) { 1049 setResult(createObjectAccessNode(value, nodeConstructor), b); 1050 } else { 1051 FixedWithNextNode objectAccess = graph.add(createObjectAccessNode(value, nodeConstructor)); 1052 FixedWithNextNode memoryAccess = graph.add(createMemoryAccessNode(graph, nodeConstructor)); 1053 FixedWithNextNode[] accessNodes = new FixedWithNextNode[]{objectAccess, memoryAccess}; 1054 1055 LogicNode condition = graph.addOrUniqueWithInputs(IsNullNode.create(value)); 1056 b.add(new IfNode(condition, memoryAccess, objectAccess, 0.5)); 1057 1058 MergeNode merge = b.append(new MergeNode()); 1059 for (FixedWithNextNode node : accessNodes) { 1060 EndNode endNode = graph.add(new EndNode()); 1061 node.setNext(endNode); 1062 if (node instanceof StateSplit) { 1063 if (isLoad(node)) { 1064 /* 1065 * Temporarily push the access node so that the frame state has the node 1066 * on the expression stack. 1067 */ 1068 b.push(unsafeAccessKind, node); 1069 } 1070 b.setStateAfter((StateSplit) node); 1071 if (isLoad(node)) { 1072 ValueNode popped = b.pop(unsafeAccessKind); 1073 assert popped == node; 1074 } 1075 } 1076 merge.addForwardEnd(endNode); 1077 } 1078 1079 if (isLoad(objectAccess)) { 1080 ValuePhiNode phi = new ValuePhiNode(objectAccess.stamp(NodeView.DEFAULT), merge, accessNodes); 1081 b.push(unsafeAccessKind, graph.addOrUnique(phi)); 1082 } 1083 b.setStateAfter(merge); 1084 } 1085 } 1086 } 1087 1088 public static class UnsafeGetPlugin extends UnsafeAccessPlugin { 1089 private final AccessKind accessKind; 1090 1091 public UnsafeGetPlugin(JavaKind returnKind, boolean explicitUnsafeNullChecks) { 1092 this(returnKind, AccessKind.PLAIN, explicitUnsafeNullChecks); 1093 } 1094 1095 public UnsafeGetPlugin(JavaKind kind, AccessKind accessKind, boolean explicitUnsafeNullChecks) { 1096 super(kind, explicitUnsafeNullChecks); 1097 this.accessKind = accessKind; 1098 } 1099 1100 @Override 1101 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode address) { 1102 // Emits a null-check for the otherwise unused receiver 1103 unsafe.get(); 1104 b.addPush(unsafeAccessKind, new UnsafeMemoryLoadNode(address, unsafeAccessKind, OFF_HEAP_LOCATION)); 1105 b.getGraph().markUnsafeAccess(); 1106 return true; 1107 } 1108 1109 @Override 1110 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset) { 1111 // Emits a null-check for the otherwise unused receiver 1112 unsafe.get(); 1113 if (accessKind.emitBarriers) { 1114 b.add(new MembarNode(accessKind.preReadBarriers)); 1115 } 1116 createUnsafeAccess(object, b, (obj, loc) -> new RawLoadNode(obj, offset, unsafeAccessKind, loc)); 1117 if (accessKind.emitBarriers) { 1118 b.add(new MembarNode(accessKind.postReadBarriers)); 1119 } 1120 return true; 1121 } 1122 } 1123 1124 public static class UnsafePutPlugin extends UnsafeAccessPlugin { 1125 private final AccessKind accessKind; 1126 1127 public UnsafePutPlugin(JavaKind kind, boolean explicitUnsafeNullChecks) { 1128 this(kind, AccessKind.PLAIN, explicitUnsafeNullChecks); 1129 } 1130 1131 private UnsafePutPlugin(JavaKind kind, AccessKind accessKind, boolean explicitUnsafeNullChecks) { 1132 super(kind, explicitUnsafeNullChecks); 1133 this.accessKind = accessKind; 1134 } 1135 1136 @Override 1137 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode address, ValueNode value) { 1138 assert !accessKind.emitBarriers : "Barriers for address based Unsafe put is not supported."; 1139 // Emits a null-check for the otherwise unused receiver 1140 unsafe.get(); 1141 b.add(new UnsafeMemoryStoreNode(address, value, unsafeAccessKind, OFF_HEAP_LOCATION)); 1142 b.getGraph().markUnsafeAccess(); 1143 return true; 1144 } 1145 1146 @Override 1147 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { 1148 // Emits a null-check for the otherwise unused receiver 1149 unsafe.get(); 1150 if (accessKind.emitBarriers) { 1151 b.add(new MembarNode(accessKind.preWriteBarriers)); 1152 } 1153 ValueNode maskedValue = b.maskSubWordValue(value, unsafeAccessKind); 1154 createUnsafeAccess(object, b, (obj, loc) -> new RawStoreNode(obj, offset, maskedValue, unsafeAccessKind, loc)); 1155 if (accessKind.emitBarriers) { 1156 b.add(new MembarNode(accessKind.postWriteBarriers)); 1157 } 1158 return true; 1159 } 1160 } 1161 1162 public static class UnsafeFencePlugin implements InvocationPlugin { 1163 1164 private final int barriers; 1165 1166 public UnsafeFencePlugin(int barriers) { 1167 this.barriers = barriers; 1168 } 1169 1170 @Override 1171 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe) { 1172 // Emits a null-check for the otherwise unused receiver 1173 unsafe.get(); 1174 b.add(new MembarNode(barriers)); 1175 return true; 1176 } 1177 } 1178 1179 private static final SpeculationReasonGroup DIRECTIVE_SPECULATIONS = new SpeculationReasonGroup("GraalDirective", BytecodePosition.class); 1180 1181 private static void registerGraalDirectivesPlugins(InvocationPlugins plugins) { 1182 Registration r = new Registration(plugins, GraalDirectives.class); 1183 r.register0("deoptimize", new InvocationPlugin() { 1184 @Override 1185 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 1186 b.add(new DeoptimizeNode(DeoptimizationAction.None, DeoptimizationReason.TransferToInterpreter)); 1187 return true; 1188 } 1189 }); 1190 1191 r.register0("deoptimizeAndInvalidate", new InvocationPlugin() { 1192 @Override 1193 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 1194 b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter)); 1195 return true; 1196 } 1197 }); 1198 1199 r.register0("deoptimizeAndInvalidateWithSpeculation", new InvocationPlugin() { 1200 @Override 1201 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 1202 GraalError.guarantee(b.getGraph().getSpeculationLog() != null, "A speculation log is needed to use `deoptimizeAndInvalidateWithSpeculation`"); 1203 BytecodePosition pos = new BytecodePosition(null, b.getMethod(), b.bci()); 1204 SpeculationReason reason = DIRECTIVE_SPECULATIONS.createSpeculationReason(pos); 1205 Speculation speculation; 1206 if (b.getGraph().getSpeculationLog().maySpeculate(reason)) { 1207 speculation = b.getGraph().getSpeculationLog().speculate(reason); 1208 } else { 1209 speculation = SpeculationLog.NO_SPECULATION; 1210 } 1211 b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter, speculation)); 1212 return true; 1213 } 1214 }); 1215 1216 r.register0("inCompiledCode", new InvocationPlugin() { 1217 @Override 1218 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 1219 b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(true)); 1220 return true; 1221 } 1222 }); 1223 1224 r.register0("controlFlowAnchor", new InvocationPlugin() { 1225 @Override 1226 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 1227 b.add(new ControlFlowAnchorNode()); 1228 return true; 1229 } 1230 }); 1231 1232 r.register2("injectBranchProbability", double.class, boolean.class, new InvocationPlugin() { 1233 @Override 1234 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode probability, ValueNode condition) { 1235 b.addPush(JavaKind.Boolean, new BranchProbabilityNode(probability, condition)); 1236 return true; 1237 } 1238 }); 1239 1240 InvocationPlugin blackholePlugin = new InvocationPlugin() { 1241 @Override 1242 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 1243 b.add(new BlackholeNode(value)); 1244 return true; 1245 } 1246 }; 1247 1248 InvocationPlugin bindToRegisterPlugin = new InvocationPlugin() { 1249 @Override 1250 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 1251 b.add(new BindToRegisterNode(value)); 1252 return true; 1253 } 1254 }; 1255 for (JavaKind kind : JavaKind.values()) { 1256 if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { 1257 Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); 1258 r.register1("blackhole", javaClass, blackholePlugin); 1259 r.register1("bindToRegister", javaClass, bindToRegisterPlugin); 1260 1261 r.register1("opaque", javaClass, new InvocationPlugin() { 1262 @Override 1263 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 1264 b.addPush(kind, new OpaqueNode(value)); 1265 return true; 1266 } 1267 }); 1268 } 1269 } 1270 1271 InvocationPlugin spillPlugin = new InvocationPlugin() { 1272 @Override 1273 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { 1274 b.add(new SpillRegistersNode()); 1275 return true; 1276 } 1277 }; 1278 r.register0("spillRegisters", spillPlugin); 1279 1280 r.register1("guardingNonNull", Object.class, new InvocationPlugin() { 1281 @Override 1282 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) { 1283 b.addPush(value.getStackKind(), b.nullCheckedValue(value)); 1284 return true; 1285 } 1286 }); 1287 1288 r.register1("ensureVirtualized", Object.class, new InvocationPlugin() { 1289 @Override 1290 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) { 1291 b.add(new EnsureVirtualizedNode(object, false)); 1292 return true; 1293 } 1294 }); 1295 r.register1("ensureVirtualizedHere", Object.class, new InvocationPlugin() { 1296 @Override 1297 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) { 1298 b.add(new EnsureVirtualizedNode(object, true)); 1299 return true; 1300 } 1301 }); 1302 } 1303 1304 private static void registerJMHBlackholePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { 1305 InvocationPlugin blackholePlugin = new InvocationPlugin() { 1306 @Override 1307 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver blackhole, ValueNode value) { 1308 blackhole.get(); 1309 b.add(new BlackholeNode(value)); 1310 return true; 1311 } 1312 1313 @Override 1314 public boolean isDecorator() { 1315 return true; 1316 } 1317 }; 1318 String[] names = {"org.openjdk.jmh.infra.Blackhole", "org.openjdk.jmh.logic.BlackHole"}; 1319 for (String name : names) { 1320 Registration r = new Registration(plugins, name, bytecodeProvider); 1321 for (JavaKind kind : JavaKind.values()) { 1322 if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { 1323 Class<?> javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); 1324 r.registerOptional2("consume", Receiver.class, javaClass, blackholePlugin); 1325 } 1326 } 1327 r.registerOptional2("consume", Receiver.class, Object[].class, blackholePlugin); 1328 } 1329 } 1330 1331 private static void registerJFRThrowablePlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { 1332 Registration r = new Registration(plugins, "oracle.jrockit.jfr.jdkevents.ThrowableTracer", bytecodeProvider); 1333 r.register2("traceThrowable", Throwable.class, String.class, new InvocationPlugin() { 1334 @Override 1335 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode throwable, ValueNode message) { 1336 b.add(new VirtualizableInvokeMacroNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), throwable, message)); 1337 return true; 1338 } 1339 1340 @Override 1341 public boolean inlineOnly() { 1342 return true; 1343 } 1344 }); 1345 } 1346 1347 private static void registerMethodHandleImplPlugins(InvocationPlugins plugins, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider) { 1348 Registration r = new Registration(plugins, "java.lang.invoke.MethodHandleImpl", bytecodeProvider); 1349 // In later JDKs this no longer exists and the usage is replace by Class.cast which is 1350 // already an intrinsic 1351 r.registerOptional2("castReference", Class.class, Object.class, new InvocationPlugin() { 1352 @Override 1353 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode javaClass, ValueNode object) { 1354 b.genCheckcastDynamic(object, javaClass); 1355 return true; 1356 } 1357 1358 @Override 1359 public boolean inlineOnly() { 1360 return true; 1361 } 1362 }); 1363 r.register2("profileBoolean", boolean.class, int[].class, new InvocationPlugin() { 1364 @Override 1365 public boolean inlineOnly() { 1366 return true; 1367 } 1368 1369 @Override 1370 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode result, ValueNode counters) { 1371 if (result.isConstant()) { 1372 b.push(JavaKind.Boolean, result); 1373 return true; 1374 } 1375 if (counters.isConstant()) { 1376 ValueNode newResult = result; 1377 int[] ctrs = snippetReflection.asObject(int[].class, (JavaConstant) counters.asConstant()); 1378 if (ctrs != null && ctrs.length == 2) { 1379 int falseCount = ctrs[0]; 1380 int trueCount = ctrs[1]; 1381 int totalCount = trueCount + falseCount; 1382 1383 if (totalCount == 0) { 1384 b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter)); 1385 } else if (falseCount == 0 || trueCount == 0) { 1386 boolean expected = falseCount == 0; 1387 LogicNode condition = b.add( 1388 IntegerEqualsNode.create(b.getConstantReflection(), b.getMetaAccess(), b.getOptions(), null, result, b.add(ConstantNode.forBoolean(!expected)), 1389 NodeView.DEFAULT)); 1390 b.append(new FixedGuardNode(condition, DeoptimizationReason.UnreachedCode, DeoptimizationAction.InvalidateReprofile, true)); 1391 newResult = b.add(ConstantNode.forBoolean(expected)); 1392 } else { 1393 // We cannot use BranchProbabilityNode here since there's no guarantee 1394 // the result of MethodHandleImpl.profileBoolean() is used as the 1395 // test in an `if` statement (as required by BranchProbabilityNode). 1396 } 1397 } 1398 b.addPush(JavaKind.Boolean, newResult); 1399 return true; 1400 } 1401 b.addPush(JavaKind.Boolean, 1402 new ProfileBooleanNode(snippetReflection, b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), result, counters)); 1403 return true; 1404 } 1405 }); 1406 } 1407 1408 /** 1409 * Registers a plugin to ignore {@code com.sun.tdk.jcov.runtime.Collect.hit} within an 1410 * intrinsic. 1411 */ 1412 private static void registerJcovCollectPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) { 1413 Registration r = new Registration(plugins, "com.sun.tdk.jcov.runtime.Collect", bytecodeProvider); 1414 r.register1("hit", int.class, new InvocationPlugin() { 1415 @Override 1416 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) { 1417 if (b.parsingIntrinsic()) { 1418 return true; 1419 } 1420 return false; 1421 } 1422 }); 1423 } 1424 }