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