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