1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.hotspot.meta;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  26 import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT;
  27 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.JAVA_THREAD_THREAD_OBJECT_LOCATION;
  28 import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsing;
  29 import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier;
  30 
  31 import java.lang.invoke.ConstantCallSite;
  32 import java.lang.invoke.MutableCallSite;
  33 import java.lang.invoke.VolatileCallSite;
  34 import java.lang.reflect.Method;
  35 import java.lang.reflect.Modifier;
  36 import java.math.BigInteger;
  37 import java.util.zip.CRC32;
  38 
  39 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  40 import org.graalvm.compiler.bytecode.BytecodeProvider;
  41 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
  42 import org.graalvm.compiler.core.common.type.StampFactory;
  43 import org.graalvm.compiler.debug.GraalError;
  44 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  45 import org.graalvm.compiler.hotspot.nodes.CurrentJavaThreadNode;
  46 import org.graalvm.compiler.hotspot.replacements.AESCryptSubstitutions;
  47 import org.graalvm.compiler.hotspot.replacements.BigIntegerSubstitutions;
  48 import org.graalvm.compiler.hotspot.replacements.CRC32Substitutions;
  49 import org.graalvm.compiler.hotspot.replacements.CRC32CSubstitutions;
  50 import org.graalvm.compiler.hotspot.replacements.CallSiteTargetNode;
  51 import org.graalvm.compiler.hotspot.replacements.CipherBlockChainingSubstitutions;
  52 import org.graalvm.compiler.hotspot.replacements.ClassGetHubNode;
  53 import org.graalvm.compiler.hotspot.replacements.HotSpotClassSubstitutions;
  54 import org.graalvm.compiler.hotspot.replacements.IdentityHashCodeNode;
  55 import org.graalvm.compiler.hotspot.replacements.ObjectCloneNode;
  56 import org.graalvm.compiler.hotspot.replacements.ObjectSubstitutions;
  57 import org.graalvm.compiler.hotspot.replacements.ReflectionGetCallerClassNode;
  58 import org.graalvm.compiler.hotspot.replacements.ReflectionSubstitutions;
  59 import org.graalvm.compiler.hotspot.replacements.SHA2Substitutions;
  60 import org.graalvm.compiler.hotspot.replacements.SHA5Substitutions;
  61 import org.graalvm.compiler.hotspot.replacements.SHASubstitutions;
  62 import org.graalvm.compiler.hotspot.replacements.ThreadSubstitutions;
  63 import org.graalvm.compiler.hotspot.replacements.arraycopy.ArrayCopyNode;
  64 import org.graalvm.compiler.hotspot.word.HotSpotWordTypes;
  65 import org.graalvm.compiler.nodes.ConstantNode;
  66 import org.graalvm.compiler.nodes.DynamicPiNode;
  67 import org.graalvm.compiler.nodes.FixedGuardNode;
  68 import org.graalvm.compiler.nodes.LogicNode;
  69 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  70 import org.graalvm.compiler.nodes.NodeView;
  71 import org.graalvm.compiler.nodes.PiNode;
  72 import org.graalvm.compiler.nodes.ValueNode;
  73 import org.graalvm.compiler.nodes.calc.AddNode;
  74 import org.graalvm.compiler.nodes.calc.IntegerConvertNode;
  75 import org.graalvm.compiler.nodes.calc.LeftShiftNode;
  76 import org.graalvm.compiler.nodes.graphbuilderconf.ForeignCallPlugin;
  77 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  78 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  79 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  80 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  81 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  82 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  83 import org.graalvm.compiler.nodes.graphbuilderconf.NodeIntrinsicPluginFactory;
  84 import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode;
  85 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
  86 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  87 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  88 import org.graalvm.compiler.nodes.spi.StampProvider;
  89 import org.graalvm.compiler.nodes.util.GraphUtil;
  90 import org.graalvm.compiler.options.OptionValues;
  91 import org.graalvm.compiler.phases.tiers.CompilerConfiguration;
  92 import org.graalvm.compiler.replacements.InlineDuringParsingPlugin;
  93 import org.graalvm.compiler.replacements.MethodHandlePlugin;
  94 import org.graalvm.compiler.replacements.NodeIntrinsificationProvider;
  95 import org.graalvm.compiler.replacements.ReplacementsImpl;
  96 import org.graalvm.compiler.replacements.StandardGraphBuilderPlugins;
  97 import org.graalvm.compiler.serviceprovider.GraalServices;
  98 import org.graalvm.compiler.serviceprovider.JDK9Method;
  99 import org.graalvm.compiler.word.WordOperationPlugin;
 100 import org.graalvm.compiler.word.WordTypes;
 101 import org.graalvm.word.LocationIdentity;
 102 
 103 import jdk.vm.ci.code.CodeUtil;
 104 import jdk.vm.ci.meta.ConstantReflectionProvider;
 105 import jdk.vm.ci.meta.DeoptimizationAction;
 106 import jdk.vm.ci.meta.DeoptimizationReason;
 107 import jdk.vm.ci.meta.JavaKind;
 108 import jdk.vm.ci.meta.MetaAccessProvider;
 109 import jdk.vm.ci.meta.ResolvedJavaMethod;
 110 import sun.misc.Unsafe;
 111 
 112 /**
 113  * Defines the {@link Plugins} used when running on HotSpot.
 114  */
 115 public class HotSpotGraphBuilderPlugins {
 116 
 117     /**
 118      * Creates a {@link Plugins} object that should be used when running on HotSpot.
 119      *
 120      * @param constantReflection
 121      * @param snippetReflection
 122      * @param foreignCalls
 123      * @param stampProvider
 124      */
 125     public static Plugins create(CompilerConfiguration compilerConfiguration, GraalHotSpotVMConfig config, HotSpotWordTypes wordTypes, MetaAccessProvider metaAccess,
 126                     ConstantReflectionProvider constantReflection, SnippetReflectionProvider snippetReflection, ForeignCallsProvider foreignCalls, StampProvider stampProvider,
 127                     ReplacementsImpl replacements) {
 128         InvocationPlugins invocationPlugins = new HotSpotInvocationPlugins(config, compilerConfiguration);
 129 
 130         Plugins plugins = new Plugins(invocationPlugins);
 131         NodeIntrinsificationProvider nodeIntrinsificationProvider = new NodeIntrinsificationProvider(metaAccess, snippetReflection, foreignCalls, wordTypes);
 132         HotSpotWordOperationPlugin wordOperationPlugin = new HotSpotWordOperationPlugin(snippetReflection, wordTypes);
 133         HotSpotNodePlugin nodePlugin = new HotSpotNodePlugin(wordOperationPlugin);
 134 
 135         plugins.appendTypePlugin(nodePlugin);
 136         plugins.appendNodePlugin(nodePlugin);
 137         OptionValues options = replacements.getOptions();
 138         if (!GeneratePIC.getValue(options)) {
 139             plugins.appendNodePlugin(new MethodHandlePlugin(constantReflection.getMethodHandleAccess(), true));
 140         }
 141         plugins.appendInlineInvokePlugin(replacements);
 142         if (InlineDuringParsing.getValue(options)) {
 143             plugins.appendInlineInvokePlugin(new InlineDuringParsingPlugin());
 144         }
 145 
 146         if (GeneratePIC.getValue(options)) {
 147             plugins.setClassInitializationPlugin(new HotSpotClassInitializationPlugin());
 148             if (TieredAOT.getValue(options)) {
 149                 plugins.setProfilingPlugin(new HotSpotAOTProfilingPlugin());
 150             }
 151         }
 152 
 153         invocationPlugins.defer(new Runnable() {
 154 
 155             @Override
 156             public void run() {
 157                 BytecodeProvider replacementBytecodeProvider = replacements.getDefaultReplacementBytecodeProvider();
 158                 registerObjectPlugins(invocationPlugins, options, replacementBytecodeProvider);
 159                 registerClassPlugins(plugins, config, replacementBytecodeProvider);
 160                 registerSystemPlugins(invocationPlugins, foreignCalls);
 161                 registerThreadPlugins(invocationPlugins, metaAccess, wordTypes, config, replacementBytecodeProvider);
 162                 if (!GeneratePIC.getValue(options)) {
 163                     registerCallSitePlugins(invocationPlugins);
 164                 }
 165                 registerReflectionPlugins(invocationPlugins, replacementBytecodeProvider);
 166                 registerConstantPoolPlugins(invocationPlugins, wordTypes, config, replacementBytecodeProvider);
 167                 registerAESPlugins(invocationPlugins, config, replacementBytecodeProvider);
 168                 registerCRC32Plugins(invocationPlugins, config, replacementBytecodeProvider);
 169                 registerCRC32CPlugins(invocationPlugins, config, replacementBytecodeProvider);
 170                 registerBigIntegerPlugins(invocationPlugins, config, replacementBytecodeProvider);
 171                 registerSHAPlugins(invocationPlugins, config, replacementBytecodeProvider);
 172                 registerUnsafePlugins(invocationPlugins, replacementBytecodeProvider);
 173                 StandardGraphBuilderPlugins.registerInvocationPlugins(metaAccess, snippetReflection, invocationPlugins, replacementBytecodeProvider, true);
 174 
 175                 for (NodeIntrinsicPluginFactory factory : GraalServices.load(NodeIntrinsicPluginFactory.class)) {
 176                     factory.registerPlugins(invocationPlugins, nodeIntrinsificationProvider);
 177                 }
 178             }
 179         });
 180         return plugins;
 181     }
 182 
 183     private static void registerObjectPlugins(InvocationPlugins plugins, OptionValues options, BytecodeProvider bytecodeProvider) {
 184         Registration r = new Registration(plugins, Object.class, bytecodeProvider);
 185         if (!GeneratePIC.getValue(options)) {
 186             // FIXME: clone() requires speculation and requires a fix in here (to check that
 187             // b.getAssumptions() != null), and in ReplacementImpl.getSubstitution() where there is
 188             // an instantiation of IntrinsicGraphBuilder using a constructor that sets
 189             // AllowAssumptions to YES automatically. The former has to inherit the assumptions
 190             // settings from the root compile instead. So, for now, I'm disabling it for
 191             // GeneratePIC.
 192             r.register1("clone", Receiver.class, new InvocationPlugin() {
 193                 @Override
 194                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 195                     ValueNode object = receiver.get();
 196                     b.addPush(JavaKind.Object, new ObjectCloneNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), object));
 197                     return true;
 198                 }
 199 
 200                 @Override
 201                 public boolean inlineOnly() {
 202                     return true;
 203                 }
 204             });
 205         }
 206         r.registerMethodSubstitution(ObjectSubstitutions.class, "hashCode", Receiver.class);
 207     }
 208 
 209     private static void registerClassPlugins(Plugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 210         Registration r = new Registration(plugins.getInvocationPlugins(), Class.class, bytecodeProvider);
 211 
 212         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "getModifiers", Receiver.class);
 213         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "isInterface", Receiver.class);
 214         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "isArray", Receiver.class);
 215         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "isPrimitive", Receiver.class);
 216         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "getSuperclass", Receiver.class);
 217 
 218         if (config.getFieldOffset("ArrayKlass::_component_mirror", Integer.class, "oop", Integer.MAX_VALUE) != Integer.MAX_VALUE) {
 219             r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "getComponentType", Receiver.class);
 220         }
 221 
 222         r.register2("cast", Receiver.class, Object.class, new InvocationPlugin() {
 223             @Override
 224             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
 225                 ValueNode javaClass = receiver.get();
 226                 LogicNode condition = b.append(InstanceOfDynamicNode.create(b.getAssumptions(), b.getConstantReflection(), javaClass, object, true));
 227                 if (condition.isTautology()) {
 228                     b.addPush(JavaKind.Object, object);
 229                 } else {
 230                     FixedGuardNode fixedGuard = b.add(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.InvalidateReprofile, false));
 231                     b.addPush(JavaKind.Object, new DynamicPiNode(object, fixedGuard, javaClass));
 232                 }
 233                 return true;
 234             }
 235 
 236             @Override
 237             public boolean inlineOnly() {
 238                 return true;
 239             }
 240         });
 241     }
 242 
 243     private static void registerCallSitePlugins(InvocationPlugins plugins) {
 244         InvocationPlugin plugin = new InvocationPlugin() {
 245             @Override
 246             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 247                 ValueNode callSite = receiver.get();
 248                 ValueNode folded = CallSiteTargetNode.tryFold(GraphUtil.originalValue(callSite), b.getMetaAccess(), b.getAssumptions());
 249                 if (folded != null) {
 250                     b.addPush(JavaKind.Object, folded);
 251                 } else {
 252                     b.addPush(JavaKind.Object, new CallSiteTargetNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), callSite));
 253                 }
 254                 return true;
 255             }
 256 
 257             @Override
 258             public boolean inlineOnly() {
 259                 return true;
 260             }
 261         };
 262         plugins.register(plugin, ConstantCallSite.class, "getTarget", Receiver.class);
 263         plugins.register(plugin, MutableCallSite.class, "getTarget", Receiver.class);
 264         plugins.register(plugin, VolatileCallSite.class, "getTarget", Receiver.class);
 265     }
 266 
 267     private static void registerReflectionPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) {
 268         Registration r = new Registration(plugins, reflectionClass, bytecodeProvider);
 269         r.register0("getCallerClass", new InvocationPlugin() {
 270             @Override
 271             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 272                 b.addPush(JavaKind.Object, new ReflectionGetCallerClassNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions())));
 273                 return true;
 274             }
 275 
 276             @Override
 277             public boolean inlineOnly() {
 278                 return true;
 279             }
 280         });
 281         r.registerMethodSubstitution(ReflectionSubstitutions.class, "getClassAccessFlags", Class.class);
 282     }
 283 
 284     private static void registerUnsafePlugins(InvocationPlugins plugins, BytecodeProvider replacementBytecodeProvider) {
 285         Registration r;
 286         if (Java8OrEarlier) {
 287             r = new Registration(plugins, Unsafe.class, replacementBytecodeProvider);
 288         } else {
 289             r = new Registration(plugins, "jdk.internal.misc.Unsafe", replacementBytecodeProvider);
 290         }
 291         r.registerMethodSubstitution(HotSpotUnsafeSubstitutions.class, HotSpotUnsafeSubstitutions.copyMemoryName, "copyMemory", Receiver.class, Object.class, long.class, Object.class, long.class,
 292                         long.class);
 293     }
 294 
 295     private static final LocationIdentity INSTANCE_KLASS_CONSTANTS = NamedLocationIdentity.immutable("InstanceKlass::_constants");
 296     private static final LocationIdentity CONSTANT_POOL_LENGTH = NamedLocationIdentity.immutable("ConstantPool::_length");
 297 
 298     /**
 299      * Emits a node to get the metaspace {@code ConstantPool} pointer given the value of the
 300      * {@code constantPoolOop} field in a ConstantPool value.
 301      *
 302      * @param constantPoolOop value of the {@code constantPoolOop} field in a ConstantPool value
 303      * @return a node representing the metaspace {@code ConstantPool} pointer associated with
 304      *         {@code constantPoolOop}
 305      */
 306     private static ValueNode getMetaspaceConstantPool(GraphBuilderContext b, ValueNode constantPoolOop, WordTypes wordTypes, GraalHotSpotVMConfig config) {
 307         // ConstantPool.constantPoolOop is in fact the holder class.
 308         ValueNode value = b.nullCheckedValue(constantPoolOop, DeoptimizationAction.None);
 309         ValueNode klass = b.add(ClassGetHubNode.create(value, b.getMetaAccess(), b.getConstantReflection(), false));
 310 
 311         boolean notCompressible = false;
 312         AddressNode constantsAddress = b.add(new OffsetAddressNode(klass, b.add(ConstantNode.forLong(config.instanceKlassConstantsOffset))));
 313         return WordOperationPlugin.readOp(b, wordTypes.getWordKind(), constantsAddress, INSTANCE_KLASS_CONSTANTS, BarrierType.NONE, notCompressible);
 314     }
 315 
 316     /**
 317      * Emits a node representing an element in a metaspace {@code ConstantPool}.
 318      *
 319      * @param constantPoolOop value of the {@code constantPoolOop} field in a ConstantPool value
 320      */
 321     private static boolean readMetaspaceConstantPoolElement(GraphBuilderContext b, ValueNode constantPoolOop, ValueNode index, JavaKind elementKind, WordTypes wordTypes, GraalHotSpotVMConfig config) {
 322         ValueNode constants = getMetaspaceConstantPool(b, constantPoolOop, wordTypes, config);
 323         int shift = CodeUtil.log2(wordTypes.getWordKind().getByteCount());
 324         ValueNode scaledIndex = b.add(new LeftShiftNode(IntegerConvertNode.convert(index, StampFactory.forKind(JavaKind.Long), NodeView.DEFAULT), b.add(ConstantNode.forInt(shift))));
 325         ValueNode offset = b.add(new AddNode(scaledIndex, b.add(ConstantNode.forLong(config.constantPoolSize))));
 326         AddressNode elementAddress = b.add(new OffsetAddressNode(constants, offset));
 327         boolean notCompressible = false;
 328         ValueNode elementValue = WordOperationPlugin.readOp(b, elementKind, elementAddress, NamedLocationIdentity.getArrayLocation(elementKind), BarrierType.NONE, notCompressible);
 329         b.addPush(elementKind, elementValue);
 330         return true;
 331     }
 332 
 333     private static void registerConstantPoolPlugins(InvocationPlugins plugins, WordTypes wordTypes, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 334         Registration r = new Registration(plugins, constantPoolClass, bytecodeProvider);
 335 
 336         r.register2("getSize0", Receiver.class, Object.class, new InvocationPlugin() {
 337             @Override
 338             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop) {
 339                 boolean notCompressible = false;
 340                 ValueNode constants = getMetaspaceConstantPool(b, constantPoolOop, wordTypes, config);
 341                 AddressNode lengthAddress = b.add(new OffsetAddressNode(constants, b.add(ConstantNode.forLong(config.constantPoolLengthOffset))));
 342                 ValueNode length = WordOperationPlugin.readOp(b, JavaKind.Int, lengthAddress, CONSTANT_POOL_LENGTH, BarrierType.NONE, notCompressible);
 343                 b.addPush(JavaKind.Int, length);
 344                 return true;
 345             }
 346         });
 347 
 348         r.register3("getIntAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 349             @Override
 350             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 351                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Int, wordTypes, config);
 352             }
 353         });
 354         r.register3("getLongAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 355             @Override
 356             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 357                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Long, wordTypes, config);
 358             }
 359         });
 360         r.register3("getFloatAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 361             @Override
 362             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 363                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Float, wordTypes, config);
 364             }
 365         });
 366         r.register3("getDoubleAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 367             @Override
 368             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 369                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Double, wordTypes, config);
 370             }
 371         });
 372     }
 373 
 374     private static void registerSystemPlugins(InvocationPlugins plugins, ForeignCallsProvider foreignCalls) {
 375         Registration r = new Registration(plugins, System.class);
 376         r.register0("currentTimeMillis", new ForeignCallPlugin(foreignCalls, HotSpotHostForeignCallsProvider.JAVA_TIME_MILLIS));
 377         r.register0("nanoTime", new ForeignCallPlugin(foreignCalls, HotSpotHostForeignCallsProvider.JAVA_TIME_NANOS));
 378         r.register1("identityHashCode", Object.class, new InvocationPlugin() {
 379             @Override
 380             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
 381                 b.addPush(JavaKind.Int, new IdentityHashCodeNode(object));
 382                 return true;
 383             }
 384 
 385             @Override
 386             public boolean inlineOnly() {
 387                 return true;
 388             }
 389         });
 390         r.register5("arraycopy", Object.class, int.class, Object.class, int.class, int.class, new InvocationPlugin() {
 391             @Override
 392             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode src, ValueNode srcPos, ValueNode dst, ValueNode dstPos, ValueNode length) {
 393                 b.add(new ArrayCopyNode(b.bci(), src, srcPos, dst, dstPos, length));
 394                 return true;
 395             }
 396 
 397             @Override
 398             public boolean inlineOnly() {
 399                 return true;
 400             }
 401         });
 402     }
 403 
 404     private static void registerThreadPlugins(InvocationPlugins plugins, MetaAccessProvider metaAccess, WordTypes wordTypes, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 405         Registration r = new Registration(plugins, Thread.class, bytecodeProvider);
 406         r.register0("currentThread", new InvocationPlugin() {
 407             @Override
 408             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 409                 CurrentJavaThreadNode thread = b.add(new CurrentJavaThreadNode(wordTypes.getWordKind()));
 410                 boolean compressible = false;
 411                 ValueNode offset = b.add(ConstantNode.forLong(config.threadObjectOffset));
 412                 AddressNode address = b.add(new OffsetAddressNode(thread, offset));
 413                 ValueNode javaThread = WordOperationPlugin.readOp(b, JavaKind.Object, address, JAVA_THREAD_THREAD_OBJECT_LOCATION, BarrierType.NONE, compressible);
 414                 boolean exactType = false;
 415                 boolean nonNull = true;
 416                 b.addPush(JavaKind.Object, new PiNode(javaThread, metaAccess.lookupJavaType(Thread.class), exactType, nonNull));
 417                 return true;
 418             }
 419         });
 420 
 421         r.registerMethodSubstitution(ThreadSubstitutions.class, "isInterrupted", Receiver.class, boolean.class);
 422     }
 423 
 424     public static final String cbcEncryptName;
 425     public static final String cbcDecryptName;
 426     public static final String aesEncryptName;
 427     public static final String aesDecryptName;
 428 
 429     public static final String reflectionClass;
 430     public static final String constantPoolClass;
 431 
 432     static {
 433         if (JDK9Method.Java8OrEarlier) {
 434             cbcEncryptName = "encrypt";
 435             cbcDecryptName = "decrypt";
 436             aesEncryptName = "encryptBlock";
 437             aesDecryptName = "decryptBlock";
 438             reflectionClass = "sun.reflect.Reflection";
 439             constantPoolClass = "sun.reflect.ConstantPool";
 440         } else {
 441             cbcEncryptName = "implEncrypt";
 442             cbcDecryptName = "implDecrypt";
 443             aesEncryptName = "implEncryptBlock";
 444             aesDecryptName = "implDecryptBlock";
 445             reflectionClass = "jdk.internal.reflect.Reflection";
 446             constantPoolClass = "jdk.internal.reflect.ConstantPool";
 447         }
 448     }
 449 
 450     private static void registerAESPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 451         if (config.useAESIntrinsics) {
 452             assert config.aescryptEncryptBlockStub != 0L;
 453             assert config.aescryptDecryptBlockStub != 0L;
 454             assert config.cipherBlockChainingEncryptAESCryptStub != 0L;
 455             assert config.cipherBlockChainingDecryptAESCryptStub != 0L;
 456             String arch = config.osArch;
 457             String decryptSuffix = arch.equals("sparc") ? "WithOriginalKey" : "";
 458             Registration r = new Registration(plugins, "com.sun.crypto.provider.CipherBlockChaining", bytecodeProvider);
 459             r.registerMethodSubstitution(CipherBlockChainingSubstitutions.class, cbcEncryptName, Receiver.class, byte[].class, int.class, int.class, byte[].class, int.class);
 460             r.registerMethodSubstitution(CipherBlockChainingSubstitutions.class, cbcDecryptName, cbcDecryptName + decryptSuffix, Receiver.class, byte[].class, int.class, int.class, byte[].class,
 461                             int.class);
 462             r = new Registration(plugins, "com.sun.crypto.provider.AESCrypt", bytecodeProvider);
 463             r.registerMethodSubstitution(AESCryptSubstitutions.class, aesEncryptName, Receiver.class, byte[].class, int.class, byte[].class, int.class);
 464             r.registerMethodSubstitution(AESCryptSubstitutions.class, aesDecryptName, aesDecryptName + decryptSuffix, Receiver.class, byte[].class, int.class, byte[].class, int.class);
 465         }
 466     }
 467 
 468     private static void registerBigIntegerPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 469         Registration r = new Registration(plugins, BigInteger.class, bytecodeProvider);
 470         if (config.useMultiplyToLenIntrinsic()) {
 471             assert config.multiplyToLen != 0L;
 472             if (Java8OrEarlier) {
 473                 try {
 474                     Method m = BigInteger.class.getDeclaredMethod("multiplyToLen", int[].class, int.class, int[].class, int.class, int[].class);
 475                     if (Modifier.isStatic(m.getModifiers())) {
 476                         r.registerMethodSubstitution(BigIntegerSubstitutions.class, "multiplyToLen", "multiplyToLenStatic", int[].class, int.class, int[].class, int.class,
 477                                         int[].class);
 478                     } else {
 479                         r.registerMethodSubstitution(BigIntegerSubstitutions.class, "multiplyToLen", Receiver.class, int[].class, int.class, int[].class, int.class,
 480                                         int[].class);
 481                     }
 482                 } catch (NoSuchMethodException | SecurityException e) {
 483                     throw new GraalError(e);
 484                 }
 485             } else {
 486                 r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMultiplyToLen", "multiplyToLenStatic", int[].class, int.class, int[].class, int.class,
 487                                 int[].class);
 488             }
 489         }
 490         if (config.useMulAddIntrinsic()) {
 491             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMulAdd", int[].class, int[].class, int.class, int.class, int.class);
 492         }
 493         if (config.useMontgomeryMultiplyIntrinsic()) {
 494             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMontgomeryMultiply", int[].class, int[].class, int[].class, int.class, long.class, int[].class);
 495         }
 496         if (config.useMontgomerySquareIntrinsic()) {
 497             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMontgomerySquare", int[].class, int[].class, int.class, long.class, int[].class);
 498         }
 499         if (config.useSquareToLenIntrinsic()) {
 500             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implSquareToLen", int[].class, int.class, int[].class, int.class);
 501         }
 502     }
 503 
 504     private static void registerSHAPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 505         if (config.useSHA1Intrinsics()) {
 506             assert config.sha1ImplCompress != 0L;
 507             Registration r = new Registration(plugins, "sun.security.provider.SHA", bytecodeProvider);
 508             r.registerMethodSubstitution(SHASubstitutions.class, SHASubstitutions.implCompressName, "implCompress0", Receiver.class, byte[].class, int.class);
 509         }
 510         if (config.useSHA256Intrinsics()) {
 511             assert config.sha256ImplCompress != 0L;
 512             Registration r = new Registration(plugins, "sun.security.provider.SHA2", bytecodeProvider);
 513             r.registerMethodSubstitution(SHA2Substitutions.class, SHA2Substitutions.implCompressName, "implCompress0", Receiver.class, byte[].class, int.class);
 514         }
 515         if (config.useSHA512Intrinsics()) {
 516             assert config.sha512ImplCompress != 0L;
 517             Registration r = new Registration(plugins, "sun.security.provider.SHA5", bytecodeProvider);
 518             r.registerMethodSubstitution(SHA5Substitutions.class, SHA5Substitutions.implCompressName, "implCompress0", Receiver.class, byte[].class, int.class);
 519         }
 520     }
 521 
 522     private static void registerCRC32Plugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 523         if (config.useCRC32Intrinsics) {
 524             Registration r = new Registration(plugins, CRC32.class, bytecodeProvider);
 525             r.registerMethodSubstitution(CRC32Substitutions.class, "update", int.class, int.class);
 526             if (Java8OrEarlier) {
 527                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateBytes", int.class, byte[].class, int.class, int.class);
 528                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateByteBuffer", int.class, long.class, int.class, int.class);
 529             } else {
 530                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateBytes0", int.class, byte[].class, int.class, int.class);
 531                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateByteBuffer0", int.class, long.class, int.class, int.class);
 532             }
 533         }
 534     }
 535 
 536     private static void registerCRC32CPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 537         if (config.useCRC32CIntrinsics) {
 538             Registration r = new Registration(plugins, "java.util.zip.CRC32C", bytecodeProvider);
 539             r.registerMethodSubstitution(CRC32CSubstitutions.class, "updateBytes", int.class, byte[].class, int.class, int.class);
 540             r.registerMethodSubstitution(CRC32CSubstitutions.class, "updateDirectByteBuffer", int.class, long.class, int.class, int.class);
 541         }
 542     }
 543 }