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 instanceHeaderSize(@InjectedParameter GraalHotSpotVMConfig config) {
 570         return config.useCompressedClassPointers ? (2 * wordSize()) - 4 : 2 * wordSize();
 571     }
 572 
 573     @Fold
 574     public static byte dirtyCardValue(@InjectedParameter GraalHotSpotVMConfig config) {
 575         return config.dirtyCardValue;
 576     }
 577 
 578     @Fold
 579     public static byte g1YoungCardValue(@InjectedParameter GraalHotSpotVMConfig config) {
 580         return config.g1YoungCardValue;
 581     }
 582 
 583     @Fold
 584     public static int cardTableShift(@InjectedParameter GraalHotSpotVMConfig config) {
 585         return config.cardtableShift;
 586     }
 587 
 588     @Fold
 589     public static long cardTableStart(@InjectedParameter GraalHotSpotVMConfig config) {
 590         return config.cardtableStartAddress;
 591     }
 592 
 593     @Fold
 594     public static int g1CardQueueIndexOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 595         return config.g1CardQueueIndexOffset();
 596     }
 597 
 598     @Fold
 599     public static int g1CardQueueBufferOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 600         return config.g1CardQueueBufferOffset();
 601     }
 602 
 603     @Fold
 604     public static int logOfHeapRegionGrainBytes(@InjectedParameter GraalHotSpotVMConfig config) {
 605         return config.logOfHRGrainBytes;
 606     }
 607 
 608     @Fold
 609     public static int g1SATBQueueMarkingOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 610         return config.g1SATBQueueMarkingOffset();
 611     }
 612 
 613     @Fold
 614     public static int g1SATBQueueIndexOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 615         return config.g1SATBQueueIndexOffset();
 616     }
 617 
 618     @Fold
 619     public static int g1SATBQueueBufferOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 620         return config.g1SATBQueueBufferOffset();
 621     }
 622 
 623     public static final LocationIdentity KLASS_SUPER_CHECK_OFFSET_LOCATION = NamedLocationIdentity.immutable("Klass::_super_check_offset");
 624 
 625     @Fold
 626     public static int superCheckOffsetOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 627         return config.superCheckOffsetOffset;
 628     }
 629 
 630     public static final LocationIdentity SECONDARY_SUPER_CACHE_LOCATION = NamedLocationIdentity.mutable("SecondarySuperCache");
 631 
 632     @Fold
 633     public static int secondarySuperCacheOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 634         return config.secondarySuperCacheOffset;
 635     }
 636 
 637     public static final LocationIdentity SECONDARY_SUPERS_LOCATION = NamedLocationIdentity.immutable("SecondarySupers");
 638 
 639     @Fold
 640     public static int secondarySupersOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 641         return config.secondarySupersOffset;
 642     }
 643 
 644     public static final LocationIdentity DISPLACED_MARK_WORD_LOCATION = NamedLocationIdentity.mutable("DisplacedMarkWord");
 645 
 646     public static final LocationIdentity OBJECT_MONITOR_OWNER_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_owner");
 647 
 648     public static final LocationIdentity OBJECT_MONITOR_RECURSION_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_recursions");
 649 
 650     public static final LocationIdentity OBJECT_MONITOR_CXQ_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_cxq");
 651 
 652     public static final LocationIdentity OBJECT_MONITOR_ENTRY_LIST_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_EntryList");
 653 
 654     @Fold
 655     public static int lockDisplacedMarkOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 656         return config.basicLockDisplacedHeaderOffset;
 657     }
 658 
 659     @Fold
 660     public static boolean useBiasedLocking(@InjectedParameter GraalHotSpotVMConfig config) {
 661         return config.useBiasedLocking;
 662     }
 663 
 664     @Fold
 665     public static boolean useDeferredInitBarriers(@InjectedParameter GraalHotSpotVMConfig config) {
 666         return config.useDeferredInitBarriers;
 667     }
 668 
 669     @Fold
 670     public static boolean useG1GC(@InjectedParameter GraalHotSpotVMConfig config) {
 671         return config.useG1GC;
 672     }
 673 
 674     @Fold
 675     public static boolean useCMSIncrementalMode(@InjectedParameter GraalHotSpotVMConfig config) {
 676         return config.cmsIncrementalMode;
 677     }
 678 
 679     @Fold
 680     public static boolean useCompressedOops(@InjectedParameter GraalHotSpotVMConfig config) {
 681         return config.useCompressedOops;
 682     }
 683 
 684     @Fold
 685     static int uninitializedIdentityHashCodeValue(@InjectedParameter GraalHotSpotVMConfig config) {
 686         return config.uninitializedIdentityHashCodeValue;
 687     }
 688 
 689     @Fold
 690     static int identityHashCodeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 691         return config.identityHashCodeShift;
 692     }
 693 
 694     /**
 695      * Loads the hub of an object (without null checking it first).
 696      */
 697     public static KlassPointer loadHub(Object object) {
 698         return loadHubIntrinsic(object);
 699     }
 700 
 701     public static Object verifyOop(Object object) {
 702         if (verifyOops(INJECTED_VMCONFIG)) {
 703             verifyOopStub(VERIFY_OOP, object);
 704         }
 705         return object;
 706     }
 707 
 708     @NodeIntrinsic(ForeignCallNode.class)
 709     private static native Object verifyOopStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Object object);
 710 
 711     public static Word loadWordFromObject(Object object, int offset) {
 712         ReplacementsUtil.staticAssert(offset != hubOffset(INJECTED_VMCONFIG), "Use loadHubIntrinsic instead of loadWordFromObject");
 713         return loadWordFromObjectIntrinsic(object, offset, LocationIdentity.any(), getWordKind());
 714     }
 715 
 716     public static Word loadWordFromObject(Object object, int offset, LocationIdentity identity) {
 717         ReplacementsUtil.staticAssert(offset != hubOffset(INJECTED_VMCONFIG), "Use loadHubIntrinsic instead of loadWordFromObject");
 718         return loadWordFromObjectIntrinsic(object, offset, identity, getWordKind());
 719     }
 720 
 721     public static KlassPointer loadKlassFromObject(Object object, int offset, LocationIdentity identity) {
 722         ReplacementsUtil.staticAssert(offset != hubOffset(INJECTED_VMCONFIG), "Use loadHubIntrinsic instead of loadWordFromObject");
 723         return loadKlassFromObjectIntrinsic(object, offset, identity, getWordKind());
 724     }
 725 
 726     /**
 727      * Reads the value of a given register.
 728      *
 729      * @param register a register which must not be available to the register allocator
 730      * @return the value of {@code register} as a word
 731      */
 732     public static Word registerAsWord(@ConstantNodeParameter Register register) {
 733         return registerAsWord(register, true, false);
 734     }
 735 
 736     @NodeIntrinsic(value = ReadRegisterNode.class)
 737     public static native Word registerAsWord(@ConstantNodeParameter Register register, @ConstantNodeParameter boolean directUse, @ConstantNodeParameter boolean incoming);
 738 
 739     @NodeIntrinsic(value = WriteRegisterNode.class)
 740     public static native void writeRegisterAsWord(@ConstantNodeParameter Register register, Word value);
 741 
 742     @NodeIntrinsic(value = RawLoadNode.class)
 743     private static native Word loadWordFromObjectIntrinsic(Object object, long offset, @ConstantNodeParameter LocationIdentity locationIdentity, @ConstantNodeParameter JavaKind wordKind);
 744 
 745     @NodeIntrinsic(value = RawLoadNode.class)
 746     private static native KlassPointer loadKlassFromObjectIntrinsic(Object object, long offset, @ConstantNodeParameter LocationIdentity locationIdentity, @ConstantNodeParameter JavaKind wordKind);
 747 
 748     @NodeIntrinsic(value = LoadHubNode.class)
 749     public static native KlassPointer loadHubIntrinsic(Object object);
 750 
 751     @Fold
 752     public static int log2WordSize() {
 753         return CodeUtil.log2(wordSize());
 754     }
 755 
 756     public static final LocationIdentity CLASS_STATE_LOCATION = NamedLocationIdentity.mutable("ClassState");
 757 
 758     @Fold
 759     public static int instanceKlassInitStateOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 760         return config.instanceKlassInitStateOffset;
 761     }
 762 
 763     @Fold
 764     public static int instanceKlassStateFullyInitialized(@InjectedParameter GraalHotSpotVMConfig config) {
 765         return config.instanceKlassStateFullyInitialized;
 766     }
 767 
 768     /**
 769      *
 770      * @param hub the hub of an InstanceKlass
 771      * @return true is the InstanceKlass represented by hub is fully initialized
 772      */
 773     public static boolean isInstanceKlassFullyInitialized(KlassPointer hub) {
 774         return readInstanceKlassState(hub) == instanceKlassStateFullyInitialized(INJECTED_VMCONFIG);
 775     }
 776 
 777     private static byte readInstanceKlassState(KlassPointer hub) {
 778         return hub.readByte(instanceKlassInitStateOffset(INJECTED_VMCONFIG), CLASS_STATE_LOCATION);
 779     }
 780 
 781     public static final LocationIdentity KLASS_MODIFIER_FLAGS_LOCATION = NamedLocationIdentity.immutable("Klass::_modifier_flags");
 782 
 783     @Fold
 784     public static int klassModifierFlagsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 785         return config.klassModifierFlagsOffset;
 786     }
 787 
 788     public static final LocationIdentity CLASS_KLASS_LOCATION = new HotSpotOptimizingLocationIdentity("Class._klass") {
 789         @Override
 790         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 791             return foldIndirection(read, object, CLASS_MIRROR_LOCATION);
 792         }
 793     };
 794 
 795     @Fold
 796     public static int klassOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 797         return config.klassOffset;
 798     }
 799 
 800     public static final LocationIdentity CLASS_ARRAY_KLASS_LOCATION = new HotSpotOptimizingLocationIdentity("Class._array_klass") {
 801         @Override
 802         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 803             return foldIndirection(read, object, ARRAY_KLASS_COMPONENT_MIRROR);
 804         }
 805     };
 806 
 807     @Fold
 808     public static int arrayKlassOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 809         return config.arrayKlassOffset;
 810     }
 811 
 812     public static final LocationIdentity CLASS_MIRROR_LOCATION = NamedLocationIdentity.immutable("Klass::_java_mirror");
 813 
 814     public static final LocationIdentity CLASS_MIRROR_HANDLE_LOCATION = NamedLocationIdentity.immutable("Klass::_java_mirror handle");
 815 
 816     public static final LocationIdentity HEAP_TOP_LOCATION = NamedLocationIdentity.mutable("HeapTop");
 817 
 818     @Fold
 819     public static long heapTopAddress(@InjectedParameter GraalHotSpotVMConfig config) {
 820         return config.heapTopAddress;
 821     }
 822 
 823     public static final LocationIdentity HEAP_END_LOCATION = NamedLocationIdentity.mutable("HeapEnd");
 824 
 825     @Fold
 826     public static long heapEndAddress(@InjectedParameter GraalHotSpotVMConfig config) {
 827         return config.heapEndAddress;
 828     }
 829 
 830     @Fold
 831     public static long tlabIntArrayMarkWord(@InjectedParameter GraalHotSpotVMConfig config) {
 832         return config.tlabIntArrayMarkWord();
 833     }
 834 
 835     @Fold
 836     public static boolean inlineContiguousAllocationSupported(@InjectedParameter GraalHotSpotVMConfig config) {
 837         return config.inlineContiguousAllocationSupported;
 838     }
 839 
 840     @Fold
 841     public static int tlabAlignmentReserveInHeapWords(@InjectedParameter GraalHotSpotVMConfig config) {
 842         return config.tlabAlignmentReserve;
 843     }
 844 
 845     public static final LocationIdentity TLAB_SIZE_LOCATION = NamedLocationIdentity.mutable("TlabSize");
 846 
 847     @Fold
 848     public static int threadTlabSizeOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 849         return config.threadTlabSizeOffset();
 850     }
 851 
 852     public static final LocationIdentity TLAB_THREAD_ALLOCATED_BYTES_LOCATION = NamedLocationIdentity.mutable("TlabThreadAllocatedBytes");
 853 
 854     @Fold
 855     public static int threadAllocatedBytesOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 856         return config.threadAllocatedBytesOffset;
 857     }
 858 
 859     public static final LocationIdentity TLAB_REFILL_WASTE_LIMIT_LOCATION = NamedLocationIdentity.mutable("RefillWasteLimit");
 860 
 861     @Fold
 862     public static int tlabRefillWasteLimitOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 863         return config.tlabRefillWasteLimitOffset();
 864     }
 865 
 866     public static final LocationIdentity TLAB_NOF_REFILLS_LOCATION = NamedLocationIdentity.mutable("TlabNOfRefills");
 867 
 868     @Fold
 869     public static int tlabNumberOfRefillsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 870         return config.tlabNumberOfRefillsOffset();
 871     }
 872 
 873     public static final LocationIdentity TLAB_FAST_REFILL_WASTE_LOCATION = NamedLocationIdentity.mutable("TlabFastRefillWaste");
 874 
 875     @Fold
 876     public static int tlabFastRefillWasteOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 877         return config.tlabFastRefillWasteOffset();
 878     }
 879 
 880     public static final LocationIdentity TLAB_SLOW_ALLOCATIONS_LOCATION = NamedLocationIdentity.mutable("TlabSlowAllocations");
 881 
 882     @Fold
 883     public static int tlabSlowAllocationsOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 884         return config.tlabSlowAllocationsOffset();
 885     }
 886 
 887     @Fold
 888     public static int tlabRefillWasteIncrement(@InjectedParameter GraalHotSpotVMConfig config) {
 889         return config.tlabRefillWasteIncrement;
 890     }
 891 
 892     @Fold
 893     public static boolean tlabStats(@InjectedParameter GraalHotSpotVMConfig config) {
 894         return config.tlabStats;
 895     }
 896 
 897     @Fold
 898     public static int layoutHelperHeaderSizeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 899         return config.layoutHelperHeaderSizeShift;
 900     }
 901 
 902     @Fold
 903     public static int layoutHelperHeaderSizeMask(@InjectedParameter GraalHotSpotVMConfig config) {
 904         return config.layoutHelperHeaderSizeMask;
 905     }
 906 
 907     @Fold
 908     public static int layoutHelperLog2ElementSizeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 909         return config.layoutHelperLog2ElementSizeShift;
 910     }
 911 
 912     @Fold
 913     public static int layoutHelperLog2ElementSizeMask(@InjectedParameter GraalHotSpotVMConfig config) {
 914         return config.layoutHelperLog2ElementSizeMask;
 915     }
 916 
 917     @Fold
 918     public static int layoutHelperElementTypeShift(@InjectedParameter GraalHotSpotVMConfig config) {
 919         return config.layoutHelperElementTypeShift;
 920     }
 921 
 922     @Fold
 923     public static int layoutHelperElementTypeMask(@InjectedParameter GraalHotSpotVMConfig config) {
 924         return config.layoutHelperElementTypeMask;
 925     }
 926 
 927     @Fold
 928     public static int layoutHelperElementTypePrimitiveInPlace(@InjectedParameter GraalHotSpotVMConfig config) {
 929         return config.layoutHelperElementTypePrimitiveInPlace();
 930     }
 931 
 932     @NodeIntrinsic(ForeignCallNode.class)
 933     public static native int identityHashCode(@ConstantNodeParameter ForeignCallDescriptor descriptor, Object object);
 934 
 935     @Fold
 936     public static int verifiedEntryPointOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 937         return config.nmethodEntryOffset;
 938     }
 939 
 940     @Fold
 941     public static long gcTotalCollectionsAddress(@InjectedParameter GraalHotSpotVMConfig config) {
 942         return config.gcTotalCollectionsAddress();
 943     }
 944 
 945     @Fold
 946     public static long referentOffset() {
 947         try {
 948             return UNSAFE.objectFieldOffset(java.lang.ref.Reference.class.getDeclaredField("referent"));
 949         } catch (Exception e) {
 950             throw new GraalError(e);
 951         }
 952     }
 953 
 954     public static final LocationIdentity OBJ_ARRAY_KLASS_ELEMENT_KLASS_LOCATION = new HotSpotOptimizingLocationIdentity("ObjArrayKlass::_element_klass") {
 955         @Override
 956         public ValueNode canonicalizeRead(ValueNode read, AddressNode location, ValueNode object, CanonicalizerTool tool) {
 957             ValueNode javaObject = findReadHub(object);
 958             if (javaObject != null) {
 959                 ResolvedJavaType type = StampTool.typeOrNull(javaObject);
 960                 if (type != null && type.isArray()) {
 961                     ResolvedJavaType element = type.getComponentType();
 962                     if (element != null && !element.isPrimitive() && !element.getElementalType().isInterface()) {
 963                         Assumptions assumptions = object.graph().getAssumptions();
 964                         AssumptionResult<ResolvedJavaType> leafType = element.findLeafConcreteSubtype();
 965                         if (leafType != null && leafType.canRecordTo(assumptions)) {
 966                             leafType.recordTo(assumptions);
 967                             return ConstantNode.forConstant(read.stamp(NodeView.DEFAULT), tool.getConstantReflection().asObjectHub(leafType.getResult()), tool.getMetaAccess());
 968                         }
 969                     }
 970                 }
 971             }
 972             return read;
 973         }
 974     };
 975 
 976     @Fold
 977     public static int arrayClassElementOffset(@InjectedParameter GraalHotSpotVMConfig config) {
 978         return config.arrayClassElementOffset;
 979     }
 980 
 981     public static final LocationIdentity PRIMARY_SUPERS_LOCATION = NamedLocationIdentity.immutable("PrimarySupers");
 982 
 983     public static final LocationIdentity METASPACE_ARRAY_LENGTH_LOCATION = NamedLocationIdentity.immutable("MetaspaceArrayLength");
 984 
 985     public static final LocationIdentity SECONDARY_SUPERS_ELEMENT_LOCATION = NamedLocationIdentity.immutable("SecondarySupersElement");
 986 }