1 /*
   2  * Copyright (c) 2012, 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.replacements;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  26 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale;
  27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
  28 import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl.VERIFY_OOP;
  29 import static org.graalvm.compiler.hotspot.replacements.UnsafeAccess.UNSAFE;
  30 
  31 import org.graalvm.compiler.api.replacements.Fold;
  32 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter;
  33 import org.graalvm.compiler.core.common.SuppressFBWarnings;
  34 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  35 import org.graalvm.compiler.core.common.type.ObjectStamp;
  36 import org.graalvm.compiler.core.common.type.TypeReference;
  37 import org.graalvm.compiler.debug.GraalError;
  38 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  39 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  40 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  41 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  42 import org.graalvm.compiler.hotspot.nodes.ComputeObjectAddressNode;
  43 import org.graalvm.compiler.hotspot.word.KlassPointer;
  44 import org.graalvm.compiler.nodes.CanonicalizableLocation;
  45 import org.graalvm.compiler.nodes.CompressionNode;
  46 import org.graalvm.compiler.nodes.ConstantNode;
  47 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  48 import org.graalvm.compiler.nodes.NodeView;
  49 import org.graalvm.compiler.nodes.ValueNode;
  50 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  51 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  52 import org.graalvm.compiler.nodes.extended.RawLoadNode;
  53 import org.graalvm.compiler.nodes.extended.StoreHubNode;
  54 import org.graalvm.compiler.nodes.memory.Access;
  55 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  56 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  57 import org.graalvm.compiler.nodes.type.StampTool;
  58 import org.graalvm.compiler.replacements.ReplacementsUtil;
  59 import org.graalvm.compiler.replacements.nodes.ReadRegisterNode;
  60 import org.graalvm.compiler.replacements.nodes.WriteRegisterNode;
  61 import org.graalvm.compiler.word.Word;
  62 import org.graalvm.word.LocationIdentity;
  63 import org.graalvm.word.WordFactory;
  64 
  65 import jdk.vm.ci.code.CodeUtil;
  66 import jdk.vm.ci.code.Register;
  67 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  68 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  69 import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
  70 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  71 import jdk.vm.ci.meta.Assumptions;
  72 import jdk.vm.ci.meta.Assumptions.AssumptionResult;
  73 import jdk.vm.ci.meta.JavaKind;
  74 import jdk.vm.ci.meta.ResolvedJavaType;
  75 
  76 //JaCoCo Exclude
  77 
  78 /**
  79  * A collection of methods used in HotSpot snippets, substitutions and stubs.
  80  */
  81 public class HotSpotReplacementsUtil {
  82 
  83     abstract static class HotSpotOptimizingLocationIdentity extends NamedLocationIdentity implements CanonicalizableLocation {
  84 
  85         HotSpotOptimizingLocationIdentity(String name) {
  86             super(name, true);
  87         }
  88 
  89         @Override
  90         public abstract ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool);
  91 
  92         protected ValueNode findReadHub(ValueNode object) {
  93             ValueNode base = object;
  94             if (base instanceof CompressionNode) {
  95                 base = ((CompressionNode) base).getValue();
  96             }
  97             if (base instanceof Access) {
  98                 Access access = (Access) base;
  99                 if (access.getLocationIdentity().equals(HUB_LOCATION) || access.getLocationIdentity().equals(COMPRESSED_HUB_LOCATION)) {
 100                     AddressNode address = access.getAddress();
 101                     if (address instanceof OffsetAddressNode) {
 102                         OffsetAddressNode offset = (OffsetAddressNode) address;
 103                         return offset.getBase();
 104                     }
 105                 }
 106             } else if (base instanceof LoadHubNode) {
 107                 LoadHubNode loadhub = (LoadHubNode) base;
 108                 return loadhub.getValue();
 109             }
 110             return null;
 111         }
 112 
 113         /**
 114          * Fold reads that convert from Class -> Hub -> Class or vice versa.
 115          *
 116          * @param read
 117          * @param object
 118          * @param otherLocation
 119          * @return an earlier read or the original {@code read}
 120          */
 121         protected static ValueNode foldIndirection(ValueNode read, ValueNode object, LocationIdentity otherLocation) {
 122             if (object instanceof Access) {
 123                 Access access = (Access) object;
 124                 if (access.getLocationIdentity().equals(otherLocation)) {
 125                     AddressNode address = access.getAddress();
 126                     if (address instanceof OffsetAddressNode) {
 127                         OffsetAddressNode offset = (OffsetAddressNode) address;
 128                         assert offset.getBase().stamp(NodeView.DEFAULT).isCompatible(read.stamp(NodeView.DEFAULT));
 129                         return offset.getBase();
 130                     }
 131                 }
 132             }
 133             return read;
 134         }
 135     }
 136 
 137     public static HotSpotJVMCIRuntimeProvider runtime() {
 138         return HotSpotJVMCIRuntime.runtime();
 139     }
 140 
 141     @Fold
 142     public static GraalHotSpotVMConfig config(@InjectedParameter GraalHotSpotVMConfig config) {
 143         assert config != null;
 144         return config;
 145     }
 146 
 147     @Fold
 148     public static boolean useTLAB(@InjectedParameter GraalHotSpotVMConfig config) {
 149         return config.useTLAB;
 150     }
 151 
 152     @Fold
 153     public static boolean verifyOops(@InjectedParameter GraalHotSpotVMConfig config) {
 154         return config.verifyOops;
 155     }
 156 
 157     public static final LocationIdentity EXCEPTION_OOP_LOCATION = NamedLocationIdentity.mutable("ExceptionOop");
 158 
 159     /**
 160      * @see GraalHotSpotVMConfig#threadExceptionOopOffset
 161      */
 162     @Fold
 163     public static int threadExceptionOopOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 164         return config.threadExceptionOopOffset;
 165     }
 166 
 167     public static final LocationIdentity EXCEPTION_PC_LOCATION = NamedLocationIdentity.mutable("ExceptionPc");
 168 
 169     @Fold
 170     public static int threadExceptionPcOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 171         return config.threadExceptionPcOffset;
 172     }
 173 
 174     public static final LocationIdentity TLAB_TOP_LOCATION = NamedLocationIdentity.mutable("TlabTop");
 175 
 176     @Fold
 177     public static int threadTlabTopOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 178         return config.threadTlabTopOffset();
 179     }
 180 
 181     public static final LocationIdentity TLAB_END_LOCATION = NamedLocationIdentity.mutable("TlabEnd");
 182 
 183     @Fold
 184     static int threadTlabEndOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 185         return config.threadTlabEndOffset();
 186     }
 187 
 188     public static final LocationIdentity TLAB_START_LOCATION = NamedLocationIdentity.mutable("TlabStart");
 189 
 190     @Fold
 191     static int threadTlabStartOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 192         return config.threadTlabStartOffset();
 193     }
 194 
 195     public static final LocationIdentity PENDING_EXCEPTION_LOCATION = NamedLocationIdentity.mutable("PendingException");
 196 
 197     /**
 198      * @see GraalHotSpotVMConfig#pendingExceptionOffset
 199      */
 200     @Fold
 201     static int threadPendingExceptionOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 202         return config.pendingExceptionOffset;
 203     }
 204 
 205     public static final LocationIdentity PENDING_DEOPTIMIZATION_LOCATION = NamedLocationIdentity.mutable("PendingDeoptimization");
 206 
 207     /**
 208      * @see GraalHotSpotVMConfig#pendingDeoptimizationOffset
 209      */
 210     @Fold
 211     static int threadPendingDeoptimizationOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 212         return config.pendingDeoptimizationOffset;
 213     }
 214 
 215     public static final LocationIdentity OBJECT_RESULT_LOCATION = NamedLocationIdentity.mutable("ObjectResult");
 216 
 217     @Fold
 218     static int objectResultOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 219         return config.threadObjectResultOffset;
 220     }
 221 
 222     /**
 223      * @see GraalHotSpotVMConfig#threadExceptionOopOffset
 224      */
 225     public static Object readExceptionOop(Word thread) {
 226         return thread.readObject(threadExceptionOopOffset(INJECTED_VMCONFIG), EXCEPTION_OOP_LOCATION);
 227     }
 228 
 229     public static Word readExceptionPc(Word thread) {
 230         return thread.readWord(threadExceptionPcOffset(INJECTED_VMCONFIG), EXCEPTION_PC_LOCATION);
 231     }
 232 
 233     /**
 234      * @see GraalHotSpotVMConfig#threadExceptionOopOffset
 235      */
 236     public static void writeExceptionOop(Word thread, Object value) {
 237         thread.writeObject(threadExceptionOopOffset(INJECTED_VMCONFIG), value, EXCEPTION_OOP_LOCATION);
 238     }
 239 
 240     public static void writeExceptionPc(Word thread, Word value) {
 241         thread.writeWord(threadExceptionPcOffset(INJECTED_VMCONFIG), value, EXCEPTION_PC_LOCATION);
 242     }
 243 
 244     public static Word readTlabTop(Word thread) {
 245         return thread.readWord(threadTlabTopOffset(INJECTED_VMCONFIG), TLAB_TOP_LOCATION);
 246     }
 247 
 248     public static Word readTlabEnd(Word thread) {
 249         return thread.readWord(threadTlabEndOffset(INJECTED_VMCONFIG), TLAB_END_LOCATION);
 250     }
 251 
 252     public static Word readTlabStart(Word thread) {
 253         return thread.readWord(threadTlabStartOffset(INJECTED_VMCONFIG), TLAB_START_LOCATION);
 254     }
 255 
 256     public static void writeTlabTop(Word thread, Word top) {
 257         thread.writeWord(threadTlabTopOffset(INJECTED_VMCONFIG), top, TLAB_TOP_LOCATION);
 258     }
 259 
 260     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_NONVIRTUAL", justification = "foldable method parameters are injected")
 261     public static void initializeTlab(Word thread, Word start, Word end) {
 262         thread.writeWord(threadTlabStartOffset(INJECTED_VMCONFIG), start, TLAB_START_LOCATION);
 263         thread.writeWord(threadTlabTopOffset(INJECTED_VMCONFIG), start, TLAB_TOP_LOCATION);
 264         thread.writeWord(threadTlabEndOffset(INJECTED_VMCONFIG), end, TLAB_END_LOCATION);
 265     }
 266 
 267     /**
 268      * Clears the pending exception for the given thread.
 269      *
 270      * @return the pending exception, or null if there was none
 271      */
 272     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_NONVIRTUAL", justification = "foldable method parameters are injected")
 273     public static Object clearPendingException(Word thread) {
 274         Object result = thread.readObject(threadPendingExceptionOffset(INJECTED_VMCONFIG), PENDING_EXCEPTION_LOCATION);
 275         thread.writeObject(threadPendingExceptionOffset(INJECTED_VMCONFIG), null, PENDING_EXCEPTION_LOCATION);
 276         return result;
 277     }
 278 
 279     /**
 280      * Reads the pending deoptimization value for the given thread.
 281      *
 282      * @return {@code true} if there was a pending deoptimization
 283      */
 284     public static int readPendingDeoptimization(Word thread) {
 285         return thread.readInt(threadPendingDeoptimizationOffset(INJECTED_VMCONFIG), PENDING_DEOPTIMIZATION_LOCATION);
 286     }
 287 
 288     /**
 289      * Writes the pending deoptimization value for the given thread.
 290      */
 291     public static void writePendingDeoptimization(Word thread, int value) {
 292         thread.writeInt(threadPendingDeoptimizationOffset(INJECTED_VMCONFIG), value, PENDING_DEOPTIMIZATION_LOCATION);
 293     }
 294 
 295     /**
 296      * Gets and clears the object result from a runtime call stored in a thread local.
 297      *
 298      * @return the object that was in the thread local
 299      */
 300     public static Object getAndClearObjectResult(Word thread) {
 301         Object result = thread.readObject(objectResultOffset(INJECTED_VMCONFIG), OBJECT_RESULT_LOCATION);
 302         thread.writeObject(objectResultOffset(INJECTED_VMCONFIG), null, OBJECT_RESULT_LOCATION);
 303         return result;
 304     }
 305 
 306     public static final LocationIdentity JAVA_THREAD_THREAD_OBJECT_LOCATION = NamedLocationIdentity.mutable("JavaThread::_threadObj");
 307 
 308     @Fold
 309     public static int threadObjectOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 310         return config.threadObjectOffset;
 311     }
 312 
 313     public static final LocationIdentity JAVA_THREAD_OSTHREAD_LOCATION = NamedLocationIdentity.mutable("JavaThread::_osthread");
 314 
 315     @Fold
 316     public static int osThreadOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 317         return config.osThreadOffset;
 318     }
 319 
 320     @Fold
 321     public static int osThreadInterruptedOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 322         return config.osThreadInterruptedOffset;
 323     }
 324 
 325     @Fold
 326     public static JavaKind getWordKind() {
 327         return runtime().getHostJVMCIBackend().getCodeCache().getTarget().wordJavaKind;
 328     }
 329 
 330     @Fold
 331     public static int wordSize() {
 332         return runtime().getHostJVMCIBackend().getCodeCache().getTarget().wordSize;
 333     }
 334 
 335     @Fold
 336     public static int pageSize() {
 337         return UNSAFE.pageSize();
 338     }
 339 
 340     @Fold
 341     public static int heapWordSize(@InjectedParameter GraalHotSpotVMConfig config) {
 342         return config.heapWordSize;
 343     }
 344 
 345     public static final LocationIdentity PROTOTYPE_MARK_WORD_LOCATION = NamedLocationIdentity.mutable("PrototypeMarkWord");
 346 
 347     @Fold
 348     public static int prototypeMarkWordOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 349         return config.prototypeMarkWordOffset;
 350     }
 351 
 352     @Fold
 353     public static long arrayPrototypeMarkWord(@InjectedParameter GraalHotSpotVMConfig config) {
 354         return config.arrayPrototypeMarkWord();
 355     }
 356 
 357     public static final LocationIdentity KLASS_ACCESS_FLAGS_LOCATION = NamedLocationIdentity.immutable("Klass::_access_flags");
 358 
 359     @Fold
 360     public static int klassAccessFlagsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 361         return config.klassAccessFlagsOffset;
 362     }
 363 
 364     @Fold
 365     public static int jvmAccWrittenFlags(@InjectedParameter GraalHotSpotVMConfig config) {
 366         return config.jvmAccWrittenFlags;
 367     }
 368 
 369     public static final LocationIdentity KLASS_LAYOUT_HELPER_LOCATION = new HotSpotOptimizingLocationIdentity("Klass::_layout_helper") {
 370         @Override
 371         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 372             ValueNode javaObject = findReadHub(object);
 373             if (javaObject != null) {
 374                 if (javaObject.stamp(NodeView.DEFAULT) instanceof ObjectStamp) {
 375                     ObjectStamp stamp = (ObjectStamp) javaObject.stamp(NodeView.DEFAULT);
 376                     HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) stamp.javaType(tool.getMetaAccess());
 377                     if (type.isArray() && !type.getComponentType().isPrimitive()) {
 378                         int layout = type.layoutHelper();
 379                         return ConstantNode.forInt(layout);
 380                     }
 381                 }
 382             }
 383             return read;
 384         }
 385     };
 386 
 387     @Fold
 388     public static int klassLayoutHelperOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 389         return config.klassLayoutHelperOffset;
 390     }
 391 
 392     @NodeIntrinsic(value = KlassLayoutHelperNode.class)
 393     public static native int readLayoutHelper(KlassPointer object);
 394 
 395     /**
 396      * Checks if class {@code klass} is an array.
 397      *
 398      * See: Klass::layout_helper_is_array
 399      *
 400      * @param klassNonNull the class to be checked
 401      * @return true if klassNonNull is an array, false otherwise
 402      */
 403     public static boolean klassIsArray(KlassPointer klassNonNull) {
 404         /*
 405          * The less-than check only works if both values are ints. We use local variables to make
 406          * sure these are still ints and haven't changed.
 407          */
 408         final int layoutHelper = readLayoutHelper(klassNonNull);
 409         final int layoutHelperNeutralValue = config(INJECTED_VMCONFIG).klassLayoutHelperNeutralValue;
 410         return (layoutHelper < layoutHelperNeutralValue);
 411     }
 412 
 413     public static final LocationIdentity ARRAY_KLASS_COMPONENT_MIRROR = NamedLocationIdentity.immutable("ArrayKlass::_component_mirror");
 414 
 415     @Fold
 416     public static int arrayKlassComponentMirrorOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 417         return config.getFieldOffset("ArrayKlass::_component_mirror", Integer.class, "oop");
 418     }
 419 
 420     public static final LocationIdentity KLASS_SUPER_KLASS_LOCATION = NamedLocationIdentity.immutable("Klass::_super");
 421 
 422     @Fold
 423     public static int klassSuperKlassOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 424         return config.klassSuperKlassOffset;
 425     }
 426 
 427     public static final LocationIdentity MARK_WORD_LOCATION = NamedLocationIdentity.mutable("MarkWord");
 428 
 429     @Fold
 430     public static int markOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 431         return config.markOffset;
 432     }
 433 
 434     public static final LocationIdentity HUB_WRITE_LOCATION = NamedLocationIdentity.mutable("Hub:write");
 435 
 436     public static final LocationIdentity HUB_LOCATION = new HotSpotOptimizingLocationIdentity("Hub") {
 437         @Override
 438         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 439             TypeReference constantType = StampTool.typeReferenceOrNull(object);
 440             if (constantType != null && constantType.isExact()) {
 441                 return ConstantNode.forConstant(read.stamp(NodeView.DEFAULT), tool.getConstantReflection().asObjectHub(constantType.getType()), tool.getMetaAccess());
 442             }
 443             return read;
 444         }
 445     };
 446 
 447     public static final LocationIdentity COMPRESSED_HUB_LOCATION = new HotSpotOptimizingLocationIdentity("CompressedHub") {
 448         @Override
 449         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 450             TypeReference constantType = StampTool.typeReferenceOrNull(object);
 451             if (constantType != null && constantType.isExact()) {
 452                 return ConstantNode.forConstant(read.stamp(NodeView.DEFAULT), ((HotSpotMetaspaceConstant) tool.getConstantReflection().asObjectHub(constantType.getType())).compress(),
 453                                 tool.getMetaAccess());
 454             }
 455             return read;
 456         }
 457     };
 458 
 459     @Fold
 460     static int hubOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 461         return config.hubOffset;
 462     }
 463 
 464     public static void initializeObjectHeader(Word memory, Word markWord, KlassPointer hub) {
 465         memory.writeWord(markOffset(INJECTED_VMCONFIG), markWord, MARK_WORD_LOCATION);
 466         StoreHubNode.write(memory, hub);
 467     }
 468 
 469     @Fold
 470     public static int unlockedMask(@InjectedParameter GraalHotSpotVMConfig config) {
 471         return config.unlockedMask;
 472     }
 473 
 474     @Fold
 475     public static int monitorMask(@InjectedParameter GraalHotSpotVMConfig config) {
 476         return config.monitorMask;
 477     }
 478 
 479     @Fold
 480     public static int objectMonitorOwnerOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 481         return config.objectMonitorOwner;
 482     }
 483 
 484     @Fold
 485     public static int objectMonitorRecursionsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 486         return config.objectMonitorRecursions;
 487     }
 488 
 489     @Fold
 490     public static int objectMonitorCxqOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 491         return config.objectMonitorCxq;
 492     }
 493 
 494     @Fold
 495     public static int objectMonitorEntryListOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 496         return config.objectMonitorEntryList;
 497     }
 498 
 499     /**
 500      * Mask for a biasable, locked or unlocked mark word.
 501      *
 502      * <pre>
 503      * +----------------------------------+-+-+
 504      * |                                 1|1|1|
 505      * +----------------------------------+-+-+
 506      * </pre>
 507      *
 508      */
 509     @Fold
 510     public static int biasedLockMaskInPlace(@InjectedParameter GraalHotSpotVMConfig config) {
 511         return config.biasedLockMaskInPlace;
 512     }
 513 
 514     @Fold
 515     public static int epochMaskInPlace(@InjectedParameter GraalHotSpotVMConfig config) {
 516         return config.epochMaskInPlace;
 517     }
 518 
 519     /**
 520      * Pattern for a biasable, unlocked mark word.
 521      *
 522      * <pre>
 523      * +----------------------------------+-+-+
 524      * |                                 1|0|1|
 525      * +----------------------------------+-+-+
 526      * </pre>
 527      *
 528      */
 529     @Fold
 530     public static int biasedLockPattern(@InjectedParameter GraalHotSpotVMConfig config) {
 531         return config.biasedLockPattern;
 532     }
 533 
 534     @Fold
 535     public static int ageMaskInPlace(@InjectedParameter GraalHotSpotVMConfig config) {
 536         return config.ageMaskInPlace;
 537     }
 538 
 539     @Fold
 540     public static int metaspaceArrayLengthOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 541         return config.metaspaceArrayLengthOffset;
 542     }
 543 
 544     @Fold
 545     public static int metaspaceArrayBaseOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 546         return config.metaspaceArrayBaseOffset;
 547     }
 548 
 549     @Fold
 550     public static int arrayLengthOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 551         return config.arrayOopDescLengthOffset();
 552     }
 553 
 554     @Fold
 555     public static int arrayBaseOffset(JavaKind elementKind) {
 556         return getArrayBaseOffset(elementKind);
 557     }
 558 
 559     @Fold
 560     public static int arrayIndexScale(JavaKind elementKind) {
 561         return getArrayIndexScale(elementKind);
 562     }
 563 
 564     public static Word arrayStart(int[] a) {
 565         return WordFactory.unsigned(ComputeObjectAddressNode.get(a, getArrayBaseOffset(JavaKind.Int)));
 566     }
 567 
 568     @Fold
 569     public static int objectAlignment(@InjectedParameter GraalHotSpotVMConfig config) {
 570         return config.objectAlignment;
 571     }
 572 
 573     /**
 574      * Computes the size of the memory chunk allocated for an array. This size accounts for the
 575      * array header size, body size and any padding after the last element to satisfy object
 576      * alignment requirements.
 577      *
 578      * @param length the number of elements in the array
 579      * @param headerSize the size of the array header
 580      * @param log2ElementSize log2 of the size of an element in the array
 581      * @return the size of the memory chunk
 582      */
 583     public static int arrayAllocationSize(int length, int headerSize, int log2ElementSize) {
 584         int alignment = objectAlignment(INJECTED_VMCONFIG);
 585         int size = (length << log2ElementSize) + headerSize + (alignment - 1);
 586         int mask = ~(alignment - 1);
 587         return size & mask;
 588     }
 589 
 590     @Fold
 591     public static int instanceHeaderSize(@InjectedParameter GraalHotSpotVMConfig config) {
 592         return config.useCompressedClassPointers ? (2 * wordSize()) - 4 : 2 * wordSize();
 593     }
 594 
 595     @Fold
 596     public static byte dirtyCardValue(@InjectedParameter GraalHotSpotVMConfig config) {
 597         return config.dirtyCardValue;
 598     }
 599 
 600     @Fold
 601     public static byte g1YoungCardValue(@InjectedParameter GraalHotSpotVMConfig config) {
 602         return config.g1YoungCardValue;
 603     }
 604 
 605     @Fold
 606     public static int cardTableShift(@InjectedParameter GraalHotSpotVMConfig config) {
 607         return config.cardtableShift;
 608     }
 609 
 610     @Fold
 611     public static long cardTableStart(@InjectedParameter GraalHotSpotVMConfig config) {
 612         return config.cardtableStartAddress;
 613     }
 614 
 615     @Fold
 616     public static int g1CardQueueIndexOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 617         return config.g1CardQueueIndexOffset();
 618     }
 619 
 620     @Fold
 621     public static int g1CardQueueBufferOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 622         return config.g1CardQueueBufferOffset();
 623     }
 624 
 625     @Fold
 626     public static int logOfHeapRegionGrainBytes(@InjectedParameter GraalHotSpotVMConfig config) {
 627         return config.logOfHRGrainBytes;
 628     }
 629 
 630     @Fold
 631     public static int g1SATBQueueMarkingOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 632         return config.g1SATBQueueMarkingOffset();
 633     }
 634 
 635     @Fold
 636     public static int g1SATBQueueIndexOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 637         return config.g1SATBQueueIndexOffset();
 638     }
 639 
 640     @Fold
 641     public static int g1SATBQueueBufferOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 642         return config.g1SATBQueueBufferOffset();
 643     }
 644 
 645     public static final LocationIdentity KLASS_SUPER_CHECK_OFFSET_LOCATION = NamedLocationIdentity.immutable("Klass::_super_check_offset");
 646 
 647     @Fold
 648     public static int superCheckOffsetOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 649         return config.superCheckOffsetOffset;
 650     }
 651 
 652     public static final LocationIdentity SECONDARY_SUPER_CACHE_LOCATION = NamedLocationIdentity.mutable("SecondarySuperCache");
 653 
 654     @Fold
 655     public static int secondarySuperCacheOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 656         return config.secondarySuperCacheOffset;
 657     }
 658 
 659     public static final LocationIdentity SECONDARY_SUPERS_LOCATION = NamedLocationIdentity.immutable("SecondarySupers");
 660 
 661     @Fold
 662     public static int secondarySupersOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 663         return config.secondarySupersOffset;
 664     }
 665 
 666     public static final LocationIdentity DISPLACED_MARK_WORD_LOCATION = NamedLocationIdentity.mutable("DisplacedMarkWord");
 667 
 668     public static final LocationIdentity OBJECT_MONITOR_OWNER_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_owner");
 669 
 670     public static final LocationIdentity OBJECT_MONITOR_RECURSION_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_recursions");
 671 
 672     public static final LocationIdentity OBJECT_MONITOR_CXQ_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_cxq");
 673 
 674     public static final LocationIdentity OBJECT_MONITOR_ENTRY_LIST_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_EntryList");
 675 
 676     @Fold
 677     public static int lockDisplacedMarkOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 678         return config.basicLockDisplacedHeaderOffset;
 679     }
 680 
 681     @Fold
 682     public static boolean useBiasedLocking(@InjectedParameter GraalHotSpotVMConfig config) {
 683         return config.useBiasedLocking;
 684     }
 685 
 686     @Fold
 687     public static boolean useDeferredInitBarriers(@InjectedParameter GraalHotSpotVMConfig config) {
 688         return config.useDeferredInitBarriers;
 689     }
 690 
 691     @Fold
 692     public static boolean useG1GC(@InjectedParameter GraalHotSpotVMConfig config) {
 693         return config.useG1GC;
 694     }
 695 
 696     @Fold
 697     public static boolean useCMSIncrementalMode(@InjectedParameter GraalHotSpotVMConfig config) {
 698         return config.cmsIncrementalMode;
 699     }
 700 
 701     @Fold
 702     public static boolean useCompressedOops(@InjectedParameter GraalHotSpotVMConfig config) {
 703         return config.useCompressedOops;
 704     }
 705 
 706     @Fold
 707     static int uninitializedIdentityHashCodeValue(@InjectedParameter GraalHotSpotVMConfig config) {
 708         return config.uninitializedIdentityHashCodeValue;
 709     }
 710 
 711     @Fold
 712     static int identityHashCodeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 713         return config.identityHashCodeShift;
 714     }
 715 
 716     /**
 717      * Loads the hub of an object (without null checking it first).
 718      */
 719     public static KlassPointer loadHub(Object object) {
 720         return loadHubIntrinsic(object);
 721     }
 722 
 723     public static Object verifyOop(Object object) {
 724         if (verifyOops(INJECTED_VMCONFIG)) {
 725             verifyOopStub(VERIFY_OOP, object);
 726         }
 727         return object;
 728     }
 729 
 730     @NodeIntrinsic(ForeignCallNode.class)
 731     private static native Object verifyOopStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Object object);
 732 
 733     public static Word loadWordFromObject(Object object, int offset) {
 734         ReplacementsUtil.staticAssert(offset != hubOffset(INJECTED_VMCONFIG), "Use loadHubIntrinsic instead of loadWordFromObject");
 735         return loadWordFromObjectIntrinsic(object, offset, LocationIdentity.any(), getWordKind());
 736     }
 737 
 738     public static Word loadWordFromObject(Object object, int offset, LocationIdentity identity) {
 739         ReplacementsUtil.staticAssert(offset != hubOffset(INJECTED_VMCONFIG), "Use loadHubIntrinsic instead of loadWordFromObject");
 740         return loadWordFromObjectIntrinsic(object, offset, identity, getWordKind());
 741     }
 742 
 743     public static KlassPointer loadKlassFromObject(Object object, int offset, LocationIdentity identity) {
 744         ReplacementsUtil.staticAssert(offset != hubOffset(INJECTED_VMCONFIG), "Use loadHubIntrinsic instead of loadWordFromObject");
 745         return loadKlassFromObjectIntrinsic(object, offset, identity, getWordKind());
 746     }
 747 
 748     /**
 749      * Reads the value of a given register.
 750      *
 751      * @param register a register which must not be available to the register allocator
 752      * @return the value of {@code register} as a word
 753      */
 754     public static Word registerAsWord(@ConstantNodeParameter Register register) {
 755         return registerAsWord(register, true, false);
 756     }
 757 
 758     @NodeIntrinsic(value = ReadRegisterNode.class)
 759     public static native Word registerAsWord(@ConstantNodeParameter Register register, @ConstantNodeParameter boolean directUse, @ConstantNodeParameter boolean incoming);
 760 
 761     @NodeIntrinsic(value = WriteRegisterNode.class)
 762     public static native void writeRegisterAsWord(@ConstantNodeParameter Register register, Word value);
 763 
 764     @NodeIntrinsic(value = RawLoadNode.class)
 765     private static native Word loadWordFromObjectIntrinsic(Object object, long offset, @ConstantNodeParameter LocationIdentity locationIdentity, @ConstantNodeParameter JavaKind wordKind);
 766 
 767     @NodeIntrinsic(value = RawLoadNode.class)
 768     private static native KlassPointer loadKlassFromObjectIntrinsic(Object object, long offset, @ConstantNodeParameter LocationIdentity locationIdentity, @ConstantNodeParameter JavaKind wordKind);
 769 
 770     @NodeIntrinsic(value = LoadHubNode.class)
 771     public static native KlassPointer loadHubIntrinsic(Object object);
 772 
 773     @Fold
 774     public static int log2WordSize() {
 775         return CodeUtil.log2(wordSize());
 776     }
 777 
 778     public static final LocationIdentity CLASS_STATE_LOCATION = NamedLocationIdentity.mutable("ClassState");
 779 
 780     @Fold
 781     public static int instanceKlassInitStateOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 782         return config.instanceKlassInitStateOffset;
 783     }
 784 
 785     @Fold
 786     public static int instanceKlassStateFullyInitialized(@InjectedParameter GraalHotSpotVMConfig config) {
 787         return config.instanceKlassStateFullyInitialized;
 788     }
 789 
 790     /**
 791      *
 792      * @param hub the hub of an InstanceKlass
 793      * @return true is the InstanceKlass represented by hub is fully initialized
 794      */
 795     public static boolean isInstanceKlassFullyInitialized(KlassPointer hub) {
 796         return readInstanceKlassState(hub) == instanceKlassStateFullyInitialized(INJECTED_VMCONFIG);
 797     }
 798 
 799     private static byte readInstanceKlassState(KlassPointer hub) {
 800         return hub.readByte(instanceKlassInitStateOffset(INJECTED_VMCONFIG), CLASS_STATE_LOCATION);
 801     }
 802 
 803     public static final LocationIdentity KLASS_MODIFIER_FLAGS_LOCATION = NamedLocationIdentity.immutable("Klass::_modifier_flags");
 804 
 805     @Fold
 806     public static int klassModifierFlagsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 807         return config.klassModifierFlagsOffset;
 808     }
 809 
 810     public static final LocationIdentity CLASS_KLASS_LOCATION = new HotSpotOptimizingLocationIdentity("Class._klass") {
 811         @Override
 812         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 813             return foldIndirection(read, object, CLASS_MIRROR_LOCATION);
 814         }
 815     };
 816 
 817     @Fold
 818     public static int klassOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 819         return config.klassOffset;
 820     }
 821 
 822     public static final LocationIdentity CLASS_ARRAY_KLASS_LOCATION = new HotSpotOptimizingLocationIdentity("Class._array_klass") {
 823         @Override
 824         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 825             return foldIndirection(read, object, ARRAY_KLASS_COMPONENT_MIRROR);
 826         }
 827     };
 828 
 829     @Fold
 830     public static int arrayKlassOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 831         return config.arrayKlassOffset;
 832     }
 833 
 834     public static final LocationIdentity CLASS_MIRROR_LOCATION = NamedLocationIdentity.immutable("Klass::_java_mirror");
 835 
 836     public static final LocationIdentity CLASS_MIRROR_HANDLE_LOCATION = NamedLocationIdentity.immutable("Klass::_java_mirror handle");
 837 
 838     public static final LocationIdentity HEAP_TOP_LOCATION = NamedLocationIdentity.mutable("HeapTop");
 839 
 840     @Fold
 841     public static long heapTopAddress(@InjectedParameter GraalHotSpotVMConfig config) {
 842         return config.heapTopAddress;
 843     }
 844 
 845     public static final LocationIdentity HEAP_END_LOCATION = NamedLocationIdentity.mutable("HeapEnd");
 846 
 847     @Fold
 848     public static long heapEndAddress(@InjectedParameter GraalHotSpotVMConfig config) {
 849         return config.heapEndAddress;
 850     }
 851 
 852     @Fold
 853     public static long tlabIntArrayMarkWord(@InjectedParameter GraalHotSpotVMConfig config) {
 854         return config.tlabIntArrayMarkWord();
 855     }
 856 
 857     @Fold
 858     public static boolean inlineContiguousAllocationSupported(@InjectedParameter GraalHotSpotVMConfig config) {
 859         return config.inlineContiguousAllocationSupported;
 860     }
 861 
 862     @Fold
 863     public static int tlabAlignmentReserveInHeapWords(@InjectedParameter GraalHotSpotVMConfig config) {
 864         return config.tlabAlignmentReserve;
 865     }
 866 
 867     public static final LocationIdentity TLAB_SIZE_LOCATION = NamedLocationIdentity.mutable("TlabSize");
 868 
 869     @Fold
 870     public static int threadTlabSizeOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 871         return config.threadTlabSizeOffset();
 872     }
 873 
 874     public static final LocationIdentity TLAB_THREAD_ALLOCATED_BYTES_LOCATION = NamedLocationIdentity.mutable("TlabThreadAllocatedBytes");
 875 
 876     @Fold
 877     public static int threadAllocatedBytesOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 878         return config.threadAllocatedBytesOffset;
 879     }
 880 
 881     public static final LocationIdentity TLAB_REFILL_WASTE_LIMIT_LOCATION = NamedLocationIdentity.mutable("RefillWasteLimit");
 882 
 883     @Fold
 884     public static int tlabRefillWasteLimitOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 885         return config.tlabRefillWasteLimitOffset();
 886     }
 887 
 888     public static final LocationIdentity TLAB_NOF_REFILLS_LOCATION = NamedLocationIdentity.mutable("TlabNOfRefills");
 889 
 890     @Fold
 891     public static int tlabNumberOfRefillsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 892         return config.tlabNumberOfRefillsOffset();
 893     }
 894 
 895     public static final LocationIdentity TLAB_FAST_REFILL_WASTE_LOCATION = NamedLocationIdentity.mutable("TlabFastRefillWaste");
 896 
 897     @Fold
 898     public static int tlabFastRefillWasteOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 899         return config.tlabFastRefillWasteOffset();
 900     }
 901 
 902     public static final LocationIdentity TLAB_SLOW_ALLOCATIONS_LOCATION = NamedLocationIdentity.mutable("TlabSlowAllocations");
 903 
 904     @Fold
 905     public static int tlabSlowAllocationsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 906         return config.tlabSlowAllocationsOffset();
 907     }
 908 
 909     @Fold
 910     public static int tlabRefillWasteIncrement(@InjectedParameter GraalHotSpotVMConfig config) {
 911         return config.tlabRefillWasteIncrement;
 912     }
 913 
 914     @Fold
 915     public static boolean tlabStats(@InjectedParameter GraalHotSpotVMConfig config) {
 916         return config.tlabStats;
 917     }
 918 
 919     @Fold
 920     public static int layoutHelperHeaderSizeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 921         return config.layoutHelperHeaderSizeShift;
 922     }
 923 
 924     @Fold
 925     public static int layoutHelperHeaderSizeMask(@InjectedParameter GraalHotSpotVMConfig config) {
 926         return config.layoutHelperHeaderSizeMask;
 927     }
 928 
 929     @Fold
 930     public static int layoutHelperLog2ElementSizeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 931         return config.layoutHelperLog2ElementSizeShift;
 932     }
 933 
 934     @Fold
 935     public static int layoutHelperLog2ElementSizeMask(@InjectedParameter GraalHotSpotVMConfig config) {
 936         return config.layoutHelperLog2ElementSizeMask;
 937     }
 938 
 939     @Fold
 940     public static int layoutHelperElementTypeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 941         return config.layoutHelperElementTypeShift;
 942     }
 943 
 944     @Fold
 945     public static int layoutHelperElementTypeMask(@InjectedParameter GraalHotSpotVMConfig config) {
 946         return config.layoutHelperElementTypeMask;
 947     }
 948 
 949     @Fold
 950     public static int layoutHelperElementTypePrimitiveInPlace(@InjectedParameter GraalHotSpotVMConfig config) {
 951         return config.layoutHelperElementTypePrimitiveInPlace();
 952     }
 953 
 954     @NodeIntrinsic(ForeignCallNode.class)
 955     public static native int identityHashCode(@ConstantNodeParameter ForeignCallDescriptor descriptor, Object object);
 956 
 957     @Fold
 958     public static int verifiedEntryPointOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 959         return config.nmethodEntryOffset;
 960     }
 961 
 962     @Fold
 963     public static long gcTotalCollectionsAddress(@InjectedParameter GraalHotSpotVMConfig config) {
 964         return config.gcTotalCollectionsAddress();
 965     }
 966 
 967     @Fold
 968     public static long referentOffset() {
 969         try {
 970             return UNSAFE.objectFieldOffset(java.lang.ref.Reference.class.getDeclaredField("referent"));
 971         } catch (Exception e) {
 972             throw new GraalError(e);
 973         }
 974     }
 975 
 976     public static final LocationIdentity OBJ_ARRAY_KLASS_ELEMENT_KLASS_LOCATION = new HotSpotOptimizingLocationIdentity("ObjArrayKlass::_element_klass") {
 977         @Override
 978         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 979             ValueNode javaObject = findReadHub(object);
 980             if (javaObject != null) {
 981                 ResolvedJavaType type = StampTool.typeOrNull(javaObject);
 982                 if (type != null && type.isArray()) {
 983                     ResolvedJavaType element = type.getComponentType();
 984                     if (element != null && !element.isPrimitive() && !element.getElementalType().isInterface()) {
 985                         Assumptions assumptions = object.graph().getAssumptions();
 986                         AssumptionResult<ResolvedJavaType> leafType = element.findLeafConcreteSubtype();
 987                         if (leafType != null && leafType.canRecordTo(assumptions)) {
 988                             leafType.recordTo(assumptions);
 989                             return ConstantNode.forConstant(read.stamp(NodeView.DEFAULT), tool.getConstantReflection().asObjectHub(leafType.getResult()), tool.getMetaAccess());
 990                         }
 991                     }
 992                 }
 993             }
 994             return read;
 995         }
 996     };
 997 
 998     @Fold
 999     public static int arrayClassElementOffset(@InjectedParameter GraalHotSpotVMConfig config) {
1000         return config.arrayClassElementOffset;
1001     }
1002 
1003     public static final LocationIdentity PRIMARY_SUPERS_LOCATION = NamedLocationIdentity.immutable("PrimarySupers");
1004 
1005     public static final LocationIdentity METASPACE_ARRAY_LENGTH_LOCATION = NamedLocationIdentity.immutable("MetaspaceArrayLength");
1006 
1007     public static final LocationIdentity SECONDARY_SUPERS_ELEMENT_LOCATION = NamedLocationIdentity.immutable("SecondarySupersElement");
1008 }