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