1 /*
   2  * Copyright (c) 2015, 2016, 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 jdk.vm.ci.meta.DeoptimizationAction.InvalidateRecompile;
  26 import static jdk.vm.ci.meta.DeoptimizationReason.Unresolved;
  27 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  28 import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT;
  29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.JAVA_THREAD_THREAD_OBJECT_LOCATION;
  30 import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsing;
  31 import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier;
  32 
  33 import java.lang.invoke.ConstantCallSite;
  34 import java.lang.invoke.MutableCallSite;
  35 import java.lang.invoke.VolatileCallSite;
  36 import java.lang.reflect.Method;
  37 import java.lang.reflect.Modifier;
  38 import java.math.BigInteger;
  39 import java.util.zip.CRC32;
  40 
  41 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  42 import org.graalvm.compiler.bytecode.BytecodeProvider;
  43 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
  44 import org.graalvm.compiler.debug.GraalError;
  45 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  46 import org.graalvm.compiler.hotspot.nodes.CurrentJavaThreadNode;
  47 import org.graalvm.compiler.hotspot.replacements.AESCryptSubstitutions;
  48 import org.graalvm.compiler.hotspot.replacements.BigIntegerSubstitutions;
  49 import org.graalvm.compiler.hotspot.replacements.CRC32Substitutions;
  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.DeoptimizeNode;
  67 import org.graalvm.compiler.nodes.DynamicPiNode;
  68 import org.graalvm.compiler.nodes.FixedGuardNode;
  69 import org.graalvm.compiler.nodes.LogicNode;
  70 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  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.LeftShiftNode;
  75 import org.graalvm.compiler.nodes.graphbuilderconf.ForeignCallPlugin;
  76 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  77 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  78 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  79 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  80 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  81 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  82 import org.graalvm.compiler.nodes.graphbuilderconf.NodeIntrinsicPluginFactory;
  83 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
  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.hotspot.HotSpotObjectConstant;
 105 import jdk.vm.ci.hotspot.HotSpotResolvedJavaType;
 106 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
 107 import jdk.vm.ci.meta.ConstantReflectionProvider;
 108 import jdk.vm.ci.meta.DeoptimizationAction;
 109 import jdk.vm.ci.meta.DeoptimizationReason;
 110 import jdk.vm.ci.meta.JavaConstant;
 111 import jdk.vm.ci.meta.JavaKind;
 112 import jdk.vm.ci.meta.MetaAccessProvider;
 113 import jdk.vm.ci.meta.ResolvedJavaMethod;
 114 
 115 /**
 116  * Defines the {@link Plugins} used when running on HotSpot.
 117  */
 118 public class HotSpotGraphBuilderPlugins {
 119 
 120     /**
 121      * Creates a {@link Plugins} object that should be used when running on HotSpot.
 122      *
 123      * @param constantReflection
 124      * @param snippetReflection
 125      * @param foreignCalls
 126      * @param stampProvider
 127      */
 128     public static Plugins create(CompilerConfiguration compilerConfiguration, GraalHotSpotVMConfig config, HotSpotWordTypes wordTypes, MetaAccessProvider metaAccess,
 129                     ConstantReflectionProvider constantReflection, SnippetReflectionProvider snippetReflection, ForeignCallsProvider foreignCalls, StampProvider stampProvider,
 130                     ReplacementsImpl replacements) {
 131         InvocationPlugins invocationPlugins = new HotSpotInvocationPlugins(config, compilerConfiguration);
 132 
 133         Plugins plugins = new Plugins(invocationPlugins);
 134         NodeIntrinsificationProvider nodeIntrinsificationProvider = new NodeIntrinsificationProvider(metaAccess, snippetReflection, foreignCalls, wordTypes);
 135         HotSpotWordOperationPlugin wordOperationPlugin = new HotSpotWordOperationPlugin(snippetReflection, wordTypes);
 136         HotSpotNodePlugin nodePlugin = new HotSpotNodePlugin(wordOperationPlugin);
 137 
 138         plugins.appendTypePlugin(nodePlugin);
 139         plugins.appendNodePlugin(nodePlugin);
 140         OptionValues options = replacements.getOptions();
 141         if (GeneratePIC.getValue(options)) {
 142             // AOT needs to filter out bad invokes
 143             plugins.prependNodePlugin(new NodePlugin() {
 144                 @Override
 145                 public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
 146                     if (b.parsingIntrinsic()) {
 147                         return false;
 148                     }
 149                     // check if the holder has a valid fingerprint
 150                     if (((HotSpotResolvedObjectType) method.getDeclaringClass()).getFingerprint() == 0) {
 151                         // Deopt otherwise
 152                         b.append(new DeoptimizeNode(InvalidateRecompile, Unresolved));
 153                         return true;
 154                     }
 155                     // the last argument that may come from appendix, check if it is a supported
 156                     // constant type
 157                     if (args.length > 0) {
 158                         JavaConstant constant = args[args.length - 1].asJavaConstant();
 159                         if (constant != null && constant instanceof HotSpotObjectConstant) {
 160                             HotSpotResolvedJavaType type = (HotSpotResolvedJavaType) ((HotSpotObjectConstant) constant).getType();
 161                             Class<?> clazz = type.mirror();
 162                             if (clazz.equals(String.class)) {
 163                                 return false;
 164                             }
 165                             if (Class.class.isAssignableFrom(clazz) && ((HotSpotResolvedObjectType) type).getFingerprint() != 0) {
 166                                 return false;
 167                             }
 168                             b.append(new DeoptimizeNode(InvalidateRecompile, Unresolved));
 169                             return true;
 170                         }
 171                     }
 172                     return false;
 173                 }
 174             });
 175         }
 176         plugins.appendNodePlugin(new MethodHandlePlugin(constantReflection.getMethodHandleAccess(), true));
 177         plugins.appendInlineInvokePlugin(replacements);
 178         if (InlineDuringParsing.getValue(options)) {
 179             plugins.appendInlineInvokePlugin(new InlineDuringParsingPlugin());
 180         }
 181 
 182         if (GeneratePIC.getValue(options)) {
 183             plugins.setClassInitializationPlugin(new HotSpotClassInitializationPlugin());
 184             if (TieredAOT.getValue(options)) {
 185                 plugins.setProfilingPlugin(new HotSpotAOTProfilingPlugin());
 186             }
 187         }
 188 
 189         invocationPlugins.defer(new Runnable() {
 190 
 191             @Override
 192             public void run() {
 193                 BytecodeProvider replacementBytecodeProvider = replacements.getDefaultReplacementBytecodeProvider();
 194                 registerObjectPlugins(invocationPlugins, options, replacementBytecodeProvider);
 195                 registerClassPlugins(plugins, config, replacementBytecodeProvider);
 196                 registerSystemPlugins(invocationPlugins, foreignCalls);
 197                 registerThreadPlugins(invocationPlugins, metaAccess, wordTypes, config, replacementBytecodeProvider);
 198                 registerCallSitePlugins(invocationPlugins);
 199                 registerReflectionPlugins(invocationPlugins, replacementBytecodeProvider);
 200                 registerConstantPoolPlugins(invocationPlugins, wordTypes, config, replacementBytecodeProvider);
 201                 registerAESPlugins(invocationPlugins, config, replacementBytecodeProvider);
 202                 registerCRC32Plugins(invocationPlugins, config, replacementBytecodeProvider);
 203                 registerBigIntegerPlugins(invocationPlugins, config, replacementBytecodeProvider);
 204                 registerSHAPlugins(invocationPlugins, config, replacementBytecodeProvider);
 205                 StandardGraphBuilderPlugins.registerInvocationPlugins(metaAccess, snippetReflection, invocationPlugins, replacementBytecodeProvider, true);
 206 
 207                 for (NodeIntrinsicPluginFactory factory : GraalServices.load(NodeIntrinsicPluginFactory.class)) {
 208                     factory.registerPlugins(invocationPlugins, nodeIntrinsificationProvider);
 209                 }
 210             }
 211         });
 212         return plugins;
 213     }
 214 
 215     private static void registerObjectPlugins(InvocationPlugins plugins, OptionValues options, BytecodeProvider bytecodeProvider) {
 216         Registration r = new Registration(plugins, Object.class, bytecodeProvider);
 217         if (!GeneratePIC.getValue(options)) {
 218             // FIXME: clone() requires speculation and requires a fix in here (to check that
 219             // b.getAssumptions() != null), and in ReplacementImpl.getSubstitution() where there is
 220             // an instantiation of IntrinsicGraphBuilder using a constructor that sets
 221             // AllowAssumptions to YES automatically. The former has to inherit the assumptions
 222             // settings from the root compile instead. So, for now, I'm disabling it for
 223             // GeneratePIC.
 224             r.register1("clone", Receiver.class, new InvocationPlugin() {
 225                 @Override
 226                 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 227                     ValueNode object = receiver.get();
 228                     b.addPush(JavaKind.Object, new ObjectCloneNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), object));
 229                     return true;
 230                 }
 231 
 232                 @Override
 233                 public boolean inlineOnly() {
 234                     return true;
 235                 }
 236             });
 237         }
 238         r.registerMethodSubstitution(ObjectSubstitutions.class, "hashCode", Receiver.class);
 239     }
 240 
 241     private static void registerClassPlugins(Plugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 242         Registration r = new Registration(plugins.getInvocationPlugins(), Class.class, bytecodeProvider);
 243 
 244         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "getModifiers", Receiver.class);
 245         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "isInterface", Receiver.class);
 246         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "isArray", Receiver.class);
 247         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "isPrimitive", Receiver.class);
 248         r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "getSuperclass", Receiver.class);
 249 
 250         if (config.getFieldOffset("ArrayKlass::_component_mirror", Integer.class, "oop", Integer.MAX_VALUE) != Integer.MAX_VALUE) {
 251             r.registerMethodSubstitution(HotSpotClassSubstitutions.class, "getComponentType", Receiver.class);
 252         }
 253 
 254         r.register2("cast", Receiver.class, Object.class, new InvocationPlugin() {
 255             @Override
 256             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
 257                 ValueNode javaClass = receiver.get();
 258                 LogicNode condition = b.append(InstanceOfDynamicNode.create(b.getAssumptions(), b.getConstantReflection(), javaClass, object, true));
 259                 if (condition.isTautology()) {
 260                     b.addPush(JavaKind.Object, object);
 261                 } else {
 262                     FixedGuardNode fixedGuard = b.add(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.InvalidateReprofile, false));
 263                     b.addPush(JavaKind.Object, new DynamicPiNode(object, fixedGuard, javaClass));
 264                 }
 265                 return true;
 266             }
 267 
 268             @Override
 269             public boolean inlineOnly() {
 270                 return true;
 271             }
 272         });
 273     }
 274 
 275     private static void registerCallSitePlugins(InvocationPlugins plugins) {
 276         InvocationPlugin plugin = new InvocationPlugin() {
 277             @Override
 278             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 279                 ValueNode callSite = receiver.get();
 280                 ValueNode folded = CallSiteTargetNode.tryFold(GraphUtil.originalValue(callSite), b.getMetaAccess(), b.getAssumptions());
 281                 if (folded != null) {
 282                     b.addPush(JavaKind.Object, folded);
 283                 } else {
 284                     b.addPush(JavaKind.Object, new CallSiteTargetNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), callSite));
 285                 }
 286                 return true;
 287             }
 288 
 289             @Override
 290             public boolean inlineOnly() {
 291                 return true;
 292             }
 293         };
 294         plugins.register(plugin, ConstantCallSite.class, "getTarget", Receiver.class);
 295         plugins.register(plugin, MutableCallSite.class, "getTarget", Receiver.class);
 296         plugins.register(plugin, VolatileCallSite.class, "getTarget", Receiver.class);
 297     }
 298 
 299     private static void registerReflectionPlugins(InvocationPlugins plugins, BytecodeProvider bytecodeProvider) {
 300         Registration r = new Registration(plugins, reflectionClass, bytecodeProvider);
 301         r.register0("getCallerClass", new InvocationPlugin() {
 302             @Override
 303             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 304                 b.addPush(JavaKind.Object, new ReflectionGetCallerClassNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions())));
 305                 return true;
 306             }
 307 
 308             @Override
 309             public boolean inlineOnly() {
 310                 return true;
 311             }
 312         });
 313         r.registerMethodSubstitution(ReflectionSubstitutions.class, "getClassAccessFlags", Class.class);
 314     }
 315 
 316     private static final LocationIdentity INSTANCE_KLASS_CONSTANTS = NamedLocationIdentity.immutable("InstanceKlass::_constants");
 317     private static final LocationIdentity CONSTANT_POOL_LENGTH = NamedLocationIdentity.immutable("ConstantPool::_length");
 318 
 319     /**
 320      * Emits a node to get the metaspace {@code ConstantPool} pointer given the value of the
 321      * {@code constantPoolOop} field in a ConstantPool value.
 322      *
 323      * @param constantPoolOop value of the {@code constantPoolOop} field in a ConstantPool value
 324      * @return a node representing the metaspace {@code ConstantPool} pointer associated with
 325      *         {@code constantPoolOop}
 326      */
 327     private static ValueNode getMetaspaceConstantPool(GraphBuilderContext b, ValueNode constantPoolOop, WordTypes wordTypes, GraalHotSpotVMConfig config) {
 328         // ConstantPool.constantPoolOop is in fact the holder class.
 329         ValueNode value = b.nullCheckedValue(constantPoolOop, DeoptimizationAction.None);
 330         ValueNode klass = b.add(ClassGetHubNode.create(value, b.getMetaAccess(), b.getConstantReflection(), false));
 331 
 332         boolean notCompressible = false;
 333         AddressNode constantsAddress = b.add(new OffsetAddressNode(klass, b.add(ConstantNode.forLong(config.instanceKlassConstantsOffset))));
 334         return WordOperationPlugin.readOp(b, wordTypes.getWordKind(), constantsAddress, INSTANCE_KLASS_CONSTANTS, BarrierType.NONE, notCompressible);
 335     }
 336 
 337     /**
 338      * Emits a node representing an element in a metaspace {@code ConstantPool}.
 339      *
 340      * @param constantPoolOop value of the {@code constantPoolOop} field in a ConstantPool value
 341      */
 342     private static boolean readMetaspaceConstantPoolElement(GraphBuilderContext b, ValueNode constantPoolOop, ValueNode index, JavaKind elementKind, WordTypes wordTypes, GraalHotSpotVMConfig config) {
 343         ValueNode constants = getMetaspaceConstantPool(b, constantPoolOop, wordTypes, config);
 344         int shift = CodeUtil.log2(wordTypes.getWordKind().getByteCount());
 345         ValueNode scaledIndex = b.add(new LeftShiftNode(index, b.add(ConstantNode.forInt(shift))));
 346         ValueNode offset = b.add(new AddNode(scaledIndex, b.add(ConstantNode.forInt(config.constantPoolSize))));
 347         AddressNode elementAddress = b.add(new OffsetAddressNode(constants, offset));
 348         boolean notCompressible = false;
 349         ValueNode elementValue = WordOperationPlugin.readOp(b, elementKind, elementAddress, NamedLocationIdentity.getArrayLocation(elementKind), BarrierType.NONE, notCompressible);
 350         b.addPush(elementKind, elementValue);
 351         return true;
 352     }
 353 
 354     private static void registerConstantPoolPlugins(InvocationPlugins plugins, WordTypes wordTypes, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 355         Registration r = new Registration(plugins, constantPoolClass, bytecodeProvider);
 356 
 357         r.register2("getSize0", Receiver.class, Object.class, new InvocationPlugin() {
 358             @Override
 359             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop) {
 360                 boolean notCompressible = false;
 361                 ValueNode constants = getMetaspaceConstantPool(b, constantPoolOop, wordTypes, config);
 362                 AddressNode lengthAddress = b.add(new OffsetAddressNode(constants, b.add(ConstantNode.forLong(config.constantPoolLengthOffset))));
 363                 ValueNode length = WordOperationPlugin.readOp(b, JavaKind.Int, lengthAddress, CONSTANT_POOL_LENGTH, BarrierType.NONE, notCompressible);
 364                 b.addPush(JavaKind.Int, length);
 365                 return true;
 366             }
 367         });
 368 
 369         r.register3("getIntAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 370             @Override
 371             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 372                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Int, wordTypes, config);
 373             }
 374         });
 375         r.register3("getLongAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 376             @Override
 377             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 378                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Long, wordTypes, config);
 379             }
 380         });
 381         r.register3("getFloatAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 382             @Override
 383             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 384                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Float, wordTypes, config);
 385             }
 386         });
 387         r.register3("getDoubleAt0", Receiver.class, Object.class, int.class, new InvocationPlugin() {
 388             @Override
 389             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode constantPoolOop, ValueNode index) {
 390                 return readMetaspaceConstantPoolElement(b, constantPoolOop, index, JavaKind.Double, wordTypes, config);
 391             }
 392         });
 393     }
 394 
 395     private static void registerSystemPlugins(InvocationPlugins plugins, ForeignCallsProvider foreignCalls) {
 396         Registration r = new Registration(plugins, System.class);
 397         r.register0("currentTimeMillis", new ForeignCallPlugin(foreignCalls, HotSpotHostForeignCallsProvider.JAVA_TIME_MILLIS));
 398         r.register0("nanoTime", new ForeignCallPlugin(foreignCalls, HotSpotHostForeignCallsProvider.JAVA_TIME_NANOS));
 399         r.register1("identityHashCode", Object.class, new InvocationPlugin() {
 400             @Override
 401             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
 402                 b.addPush(JavaKind.Int, new IdentityHashCodeNode(object));
 403                 return true;
 404             }
 405 
 406             @Override
 407             public boolean inlineOnly() {
 408                 return true;
 409             }
 410         });
 411         r.register5("arraycopy", Object.class, int.class, Object.class, int.class, int.class, new InvocationPlugin() {
 412             @Override
 413             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode src, ValueNode srcPos, ValueNode dst, ValueNode dstPos, ValueNode length) {
 414                 b.add(new ArrayCopyNode(b.bci(), src, srcPos, dst, dstPos, length));
 415                 return true;
 416             }
 417 
 418             @Override
 419             public boolean inlineOnly() {
 420                 return true;
 421             }
 422         });
 423     }
 424 
 425     private static void registerThreadPlugins(InvocationPlugins plugins, MetaAccessProvider metaAccess, WordTypes wordTypes, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 426         Registration r = new Registration(plugins, Thread.class, bytecodeProvider);
 427         r.register0("currentThread", new InvocationPlugin() {
 428             @Override
 429             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 430                 CurrentJavaThreadNode thread = b.add(new CurrentJavaThreadNode(wordTypes.getWordKind()));
 431                 boolean compressible = false;
 432                 ValueNode offset = b.add(ConstantNode.forLong(config.threadObjectOffset));
 433                 AddressNode address = b.add(new OffsetAddressNode(thread, offset));
 434                 ValueNode javaThread = WordOperationPlugin.readOp(b, JavaKind.Object, address, JAVA_THREAD_THREAD_OBJECT_LOCATION, BarrierType.NONE, compressible);
 435                 boolean exactType = false;
 436                 boolean nonNull = true;
 437                 b.addPush(JavaKind.Object, new PiNode(javaThread, metaAccess.lookupJavaType(Thread.class), exactType, nonNull));
 438                 return true;
 439             }
 440         });
 441 
 442         r.registerMethodSubstitution(ThreadSubstitutions.class, "isInterrupted", Receiver.class, boolean.class);
 443     }
 444 
 445     public static final String cbcEncryptName;
 446     public static final String cbcDecryptName;
 447     public static final String aesEncryptName;
 448     public static final String aesDecryptName;
 449 
 450     public static final String reflectionClass;
 451     public static final String constantPoolClass;
 452 
 453     static {
 454         if (JDK9Method.Java8OrEarlier) {
 455             cbcEncryptName = "encrypt";
 456             cbcDecryptName = "decrypt";
 457             aesEncryptName = "encryptBlock";
 458             aesDecryptName = "decryptBlock";
 459             reflectionClass = "sun.reflect.Reflection";
 460             constantPoolClass = "sun.reflect.ConstantPool";
 461         } else {
 462             cbcEncryptName = "implEncrypt";
 463             cbcDecryptName = "implDecrypt";
 464             aesEncryptName = "implEncryptBlock";
 465             aesDecryptName = "implDecryptBlock";
 466             reflectionClass = "jdk.internal.reflect.Reflection";
 467             constantPoolClass = "jdk.internal.reflect.ConstantPool";
 468         }
 469     }
 470 
 471     private static void registerAESPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 472         if (config.useAESIntrinsics) {
 473             assert config.aescryptEncryptBlockStub != 0L;
 474             assert config.aescryptDecryptBlockStub != 0L;
 475             assert config.cipherBlockChainingEncryptAESCryptStub != 0L;
 476             assert config.cipherBlockChainingDecryptAESCryptStub != 0L;
 477             String arch = config.osArch;
 478             String decryptSuffix = arch.equals("sparc") ? "WithOriginalKey" : "";
 479             Registration r = new Registration(plugins, "com.sun.crypto.provider.CipherBlockChaining", bytecodeProvider);
 480             r.registerMethodSubstitution(CipherBlockChainingSubstitutions.class, cbcEncryptName, Receiver.class, byte[].class, int.class, int.class, byte[].class, int.class);
 481             r.registerMethodSubstitution(CipherBlockChainingSubstitutions.class, cbcDecryptName, cbcDecryptName + decryptSuffix, Receiver.class, byte[].class, int.class, int.class, byte[].class,
 482                             int.class);
 483             r = new Registration(plugins, "com.sun.crypto.provider.AESCrypt", bytecodeProvider);
 484             r.registerMethodSubstitution(AESCryptSubstitutions.class, aesEncryptName, Receiver.class, byte[].class, int.class, byte[].class, int.class);
 485             r.registerMethodSubstitution(AESCryptSubstitutions.class, aesDecryptName, aesDecryptName + decryptSuffix, Receiver.class, byte[].class, int.class, byte[].class, int.class);
 486         }
 487     }
 488 
 489     private static void registerBigIntegerPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 490         Registration r = new Registration(plugins, BigInteger.class, bytecodeProvider);
 491         if (config.useMultiplyToLenIntrinsic()) {
 492             assert config.multiplyToLen != 0L;
 493             if (Java8OrEarlier) {
 494                 try {
 495                     Method m = BigInteger.class.getDeclaredMethod("multiplyToLen", int[].class, int.class, int[].class, int.class, int[].class);
 496                     if (Modifier.isStatic(m.getModifiers())) {
 497                         r.registerMethodSubstitution(BigIntegerSubstitutions.class, "multiplyToLen", "multiplyToLenStatic", int[].class, int.class, int[].class, int.class,
 498                                         int[].class);
 499                     } else {
 500                         r.registerMethodSubstitution(BigIntegerSubstitutions.class, "multiplyToLen", Receiver.class, int[].class, int.class, int[].class, int.class,
 501                                         int[].class);
 502                     }
 503                 } catch (NoSuchMethodException | SecurityException e) {
 504                     throw new GraalError(e);
 505                 }
 506             } else {
 507                 r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMultiplyToLen", "multiplyToLenStatic", int[].class, int.class, int[].class, int.class,
 508                                 int[].class);
 509             }
 510         }
 511         if (config.useMulAddIntrinsic()) {
 512             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMulAdd", int[].class, int[].class, int.class, int.class, int.class);
 513         }
 514         if (config.useMontgomeryMultiplyIntrinsic()) {
 515             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMontgomeryMultiply", int[].class, int[].class, int[].class, int.class, long.class, int[].class);
 516         }
 517         if (config.useMontgomerySquareIntrinsic()) {
 518             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implMontgomerySquare", int[].class, int[].class, int.class, long.class, int[].class);
 519         }
 520         if (config.useSquareToLenIntrinsic()) {
 521             r.registerMethodSubstitution(BigIntegerSubstitutions.class, "implSquareToLen", int[].class, int.class, int[].class, int.class);
 522         }
 523     }
 524 
 525     private static void registerSHAPlugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 526         if (config.useSHA1Intrinsics()) {
 527             assert config.sha1ImplCompress != 0L;
 528             Registration r = new Registration(plugins, "sun.security.provider.SHA", bytecodeProvider);
 529             r.registerMethodSubstitution(SHASubstitutions.class, SHASubstitutions.implCompressName, "implCompress0", Receiver.class, byte[].class, int.class);
 530         }
 531         if (config.useSHA256Intrinsics()) {
 532             assert config.sha256ImplCompress != 0L;
 533             Registration r = new Registration(plugins, "sun.security.provider.SHA2", bytecodeProvider);
 534             r.registerMethodSubstitution(SHA2Substitutions.class, SHA2Substitutions.implCompressName, "implCompress0", Receiver.class, byte[].class, int.class);
 535         }
 536         if (config.useSHA512Intrinsics()) {
 537             assert config.sha512ImplCompress != 0L;
 538             Registration r = new Registration(plugins, "sun.security.provider.SHA5", bytecodeProvider);
 539             r.registerMethodSubstitution(SHA5Substitutions.class, SHA5Substitutions.implCompressName, "implCompress0", Receiver.class, byte[].class, int.class);
 540         }
 541     }
 542 
 543     private static void registerCRC32Plugins(InvocationPlugins plugins, GraalHotSpotVMConfig config, BytecodeProvider bytecodeProvider) {
 544         if (config.useCRC32Intrinsics) {
 545             Registration r = new Registration(plugins, CRC32.class, bytecodeProvider);
 546             r.registerMethodSubstitution(CRC32Substitutions.class, "update", int.class, int.class);
 547             if (Java8OrEarlier) {
 548                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateBytes", int.class, byte[].class, int.class, int.class);
 549                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateByteBuffer", int.class, long.class, int.class, int.class);
 550             } else {
 551                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateBytes0", int.class, byte[].class, int.class, int.class);
 552                 r.registerMethodSubstitution(CRC32Substitutions.class, "updateByteBuffer0", int.class, long.class, int.class, int.class);
 553             }
 554         }
 555     }
 556 }