1 /*
   2  * Copyright (c) 2011, 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.replacements;
  24 
  25 import static org.graalvm.compiler.nodes.NamedLocationIdentity.ARRAY_LENGTH_LOCATION;
  26 import static org.graalvm.compiler.nodes.java.ArrayLengthNode.readArrayLength;
  27 import static jdk.vm.ci.code.MemoryBarriers.JMM_POST_VOLATILE_READ;
  28 import static jdk.vm.ci.code.MemoryBarriers.JMM_POST_VOLATILE_WRITE;
  29 import static jdk.vm.ci.code.MemoryBarriers.JMM_PRE_VOLATILE_READ;
  30 import static jdk.vm.ci.code.MemoryBarriers.JMM_PRE_VOLATILE_WRITE;
  31 import static jdk.vm.ci.meta.DeoptimizationAction.InvalidateReprofile;
  32 import static jdk.vm.ci.meta.DeoptimizationReason.BoundsCheckException;
  33 import static jdk.vm.ci.meta.DeoptimizationReason.NullCheckException;
  34 
  35 import java.util.ArrayList;
  36 import java.util.BitSet;
  37 import java.util.List;
  38 
  39 import org.graalvm.compiler.api.directives.GraalDirectives;
  40 import org.graalvm.compiler.api.replacements.Snippet;
  41 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  42 import org.graalvm.compiler.core.common.LocationIdentity;
  43 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  44 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
  45 import org.graalvm.compiler.core.common.type.IntegerStamp;
  46 import org.graalvm.compiler.core.common.type.ObjectStamp;
  47 import org.graalvm.compiler.core.common.type.Stamp;
  48 import org.graalvm.compiler.core.common.type.StampFactory;
  49 import org.graalvm.compiler.core.common.type.TypeReference;
  50 import org.graalvm.compiler.debug.GraalError;
  51 import org.graalvm.compiler.graph.Node;
  52 import org.graalvm.compiler.nodes.ConstantNode;
  53 import org.graalvm.compiler.nodes.FieldLocationIdentity;
  54 import org.graalvm.compiler.nodes.FixedNode;
  55 import org.graalvm.compiler.nodes.LogicNode;
  56 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  57 import org.graalvm.compiler.nodes.PiNode;
  58 import org.graalvm.compiler.nodes.StructuredGraph;
  59 import org.graalvm.compiler.nodes.ValueNode;
  60 import org.graalvm.compiler.nodes.calc.AddNode;
  61 import org.graalvm.compiler.nodes.calc.IntegerBelowNode;
  62 import org.graalvm.compiler.nodes.calc.IntegerConvertNode;
  63 import org.graalvm.compiler.nodes.calc.IsNullNode;
  64 import org.graalvm.compiler.nodes.calc.LeftShiftNode;
  65 import org.graalvm.compiler.nodes.calc.NarrowNode;
  66 import org.graalvm.compiler.nodes.calc.RightShiftNode;
  67 import org.graalvm.compiler.nodes.calc.SignExtendNode;
  68 import org.graalvm.compiler.nodes.calc.SubNode;
  69 import org.graalvm.compiler.nodes.calc.ZeroExtendNode;
  70 import org.graalvm.compiler.nodes.debug.VerifyHeapNode;
  71 import org.graalvm.compiler.nodes.extended.BoxNode;
  72 import org.graalvm.compiler.nodes.extended.FixedValueAnchorNode;
  73 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  74 import org.graalvm.compiler.nodes.extended.GuardedUnsafeLoadNode;
  75 import org.graalvm.compiler.nodes.extended.GuardingNode;
  76 import org.graalvm.compiler.nodes.extended.JavaReadNode;
  77 import org.graalvm.compiler.nodes.extended.JavaWriteNode;
  78 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  79 import org.graalvm.compiler.nodes.extended.MembarNode;
  80 import org.graalvm.compiler.nodes.extended.UnboxNode;
  81 import org.graalvm.compiler.nodes.extended.UnsafeLoadNode;
  82 import org.graalvm.compiler.nodes.extended.UnsafeMemoryLoadNode;
  83 import org.graalvm.compiler.nodes.extended.UnsafeMemoryStoreNode;
  84 import org.graalvm.compiler.nodes.extended.UnsafeStoreNode;
  85 import org.graalvm.compiler.nodes.java.AbstractNewArrayNode;
  86 import org.graalvm.compiler.nodes.java.AbstractNewObjectNode;
  87 import org.graalvm.compiler.nodes.java.AccessIndexedNode;
  88 import org.graalvm.compiler.nodes.java.ArrayLengthNode;
  89 import org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode;
  90 import org.graalvm.compiler.nodes.java.CompareAndSwapNode;
  91 import org.graalvm.compiler.nodes.java.FinalFieldBarrierNode;
  92 import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode;
  93 import org.graalvm.compiler.nodes.java.InstanceOfNode;
  94 import org.graalvm.compiler.nodes.java.LoadFieldNode;
  95 import org.graalvm.compiler.nodes.java.LoadIndexedNode;
  96 import org.graalvm.compiler.nodes.java.LoweredAtomicReadAndWriteNode;
  97 import org.graalvm.compiler.nodes.java.LoweredCompareAndSwapNode;
  98 import org.graalvm.compiler.nodes.java.MonitorEnterNode;
  99 import org.graalvm.compiler.nodes.java.MonitorIdNode;
 100 import org.graalvm.compiler.nodes.java.NewArrayNode;
 101 import org.graalvm.compiler.nodes.java.NewInstanceNode;
 102 import org.graalvm.compiler.nodes.java.RawMonitorEnterNode;
 103 import org.graalvm.compiler.nodes.java.StoreFieldNode;
 104 import org.graalvm.compiler.nodes.java.StoreIndexedNode;
 105 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
 106 import org.graalvm.compiler.nodes.memory.ReadNode;
 107 import org.graalvm.compiler.nodes.memory.WriteNode;
 108 import org.graalvm.compiler.nodes.memory.address.AddressNode;
 109 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
 110 import org.graalvm.compiler.nodes.memory.address.RawAddressNode;
 111 import org.graalvm.compiler.nodes.spi.Lowerable;
 112 import org.graalvm.compiler.nodes.spi.LoweringProvider;
 113 import org.graalvm.compiler.nodes.spi.LoweringTool;
 114 import org.graalvm.compiler.nodes.type.StampTool;
 115 import org.graalvm.compiler.nodes.util.GraphUtil;
 116 import org.graalvm.compiler.nodes.virtual.AllocatedObjectNode;
 117 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
 118 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
 119 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
 120 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
 121 import org.graalvm.compiler.phases.util.Providers;
 122 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
 123 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
 124 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
 125 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
 126 
 127 import jdk.vm.ci.code.CodeUtil;
 128 import jdk.vm.ci.code.MemoryBarriers;
 129 import jdk.vm.ci.code.TargetDescription;
 130 import jdk.vm.ci.meta.DeoptimizationAction;
 131 import jdk.vm.ci.meta.DeoptimizationReason;
 132 import jdk.vm.ci.meta.JavaConstant;
 133 import jdk.vm.ci.meta.JavaKind;
 134 import jdk.vm.ci.meta.MetaAccessProvider;
 135 import jdk.vm.ci.meta.ResolvedJavaField;
 136 import jdk.vm.ci.meta.ResolvedJavaMethod;
 137 import jdk.vm.ci.meta.ResolvedJavaType;
 138 
 139 /**
 140  * VM-independent lowerings for standard Java nodes. VM-specific methods are abstract and must be
 141  * implemented by VM-specific subclasses.
 142  */
 143 public abstract class DefaultJavaLoweringProvider implements LoweringProvider {
 144 
 145     protected final MetaAccessProvider metaAccess;
 146     protected final ForeignCallsProvider foreignCalls;
 147     protected final TargetDescription target;
 148 
 149     private BoxingSnippets.Templates boxingSnippets;
 150 
 151     public DefaultJavaLoweringProvider(MetaAccessProvider metaAccess, ForeignCallsProvider foreignCalls, TargetDescription target) {
 152         this.metaAccess = metaAccess;
 153         this.foreignCalls = foreignCalls;
 154         this.target = target;
 155     }
 156 
 157     public void initialize(Providers providers, SnippetReflectionProvider snippetReflection) {
 158         boxingSnippets = new BoxingSnippets.Templates(providers, snippetReflection, target);
 159         providers.getReplacements().registerSnippetTemplateCache(new SnippetCounterNode.SnippetCounterSnippets.Templates(providers, snippetReflection, target));
 160     }
 161 
 162     public final TargetDescription getTarget() {
 163         return target;
 164     }
 165 
 166     @Override
 167     public void lower(Node n, LoweringTool tool) {
 168         assert n instanceof Lowerable;
 169         StructuredGraph graph = (StructuredGraph) n.graph();
 170         if (n instanceof LoadFieldNode) {
 171             lowerLoadFieldNode((LoadFieldNode) n, tool);
 172         } else if (n instanceof StoreFieldNode) {
 173             lowerStoreFieldNode((StoreFieldNode) n, tool);
 174         } else if (n instanceof LoadIndexedNode) {
 175             lowerLoadIndexedNode((LoadIndexedNode) n, tool);
 176         } else if (n instanceof StoreIndexedNode) {
 177             lowerStoreIndexedNode((StoreIndexedNode) n, tool);
 178         } else if (n instanceof ArrayLengthNode) {
 179             lowerArrayLengthNode((ArrayLengthNode) n, tool);
 180         } else if (n instanceof LoadHubNode) {
 181             lowerLoadHubNode((LoadHubNode) n, tool);
 182         } else if (n instanceof MonitorEnterNode) {
 183             lowerMonitorEnterNode((MonitorEnterNode) n, tool, graph);
 184         } else if (n instanceof CompareAndSwapNode) {
 185             lowerCompareAndSwapNode((CompareAndSwapNode) n);
 186         } else if (n instanceof AtomicReadAndWriteNode) {
 187             lowerAtomicReadAndWriteNode((AtomicReadAndWriteNode) n);
 188         } else if (n instanceof UnsafeLoadNode) {
 189             lowerUnsafeLoadNode((UnsafeLoadNode) n, tool);
 190         } else if (n instanceof UnsafeMemoryLoadNode) {
 191             lowerUnsafeMemoryLoadNode((UnsafeMemoryLoadNode) n);
 192         } else if (n instanceof UnsafeStoreNode) {
 193             lowerUnsafeStoreNode((UnsafeStoreNode) n);
 194         } else if (n instanceof UnsafeMemoryStoreNode) {
 195             lowerUnsafeMemoryStoreNode((UnsafeMemoryStoreNode) n);
 196         } else if (n instanceof JavaReadNode) {
 197             lowerJavaReadNode((JavaReadNode) n);
 198         } else if (n instanceof JavaWriteNode) {
 199             lowerJavaWriteNode((JavaWriteNode) n);
 200         } else if (n instanceof CommitAllocationNode) {
 201             lowerCommitAllocationNode((CommitAllocationNode) n, tool);
 202         } else if (n instanceof BoxNode) {
 203             boxingSnippets.lower((BoxNode) n, tool);
 204         } else if (n instanceof UnboxNode) {
 205             boxingSnippets.lower((UnboxNode) n, tool);
 206         } else if (n instanceof VerifyHeapNode) {
 207             lowerVerifyHeap((VerifyHeapNode) n);
 208         } else if (n instanceof UnaryMathIntrinsicNode) {
 209             lowerUnaryMath((UnaryMathIntrinsicNode) n, tool);
 210         } else if (n instanceof BinaryMathIntrinsicNode) {
 211             lowerBinaryMath((BinaryMathIntrinsicNode) n, tool);
 212         } else {
 213             throw GraalError.shouldNotReachHere("Node implementing Lowerable not handled: " + n);
 214         }
 215     }
 216 
 217     private void lowerBinaryMath(BinaryMathIntrinsicNode math, LoweringTool tool) {
 218         if (tool.getLoweringStage() == LoweringTool.StandardLoweringStage.HIGH_TIER) {
 219             return;
 220         }
 221         ResolvedJavaMethod method = math.graph().method();
 222         if (method != null) {
 223             if (method.getAnnotation(Snippet.class) != null) {
 224                 /*
 225                  * In the context of the snippet use the LIR lowering instead of the Node lowering.
 226                  */
 227                 return;
 228             }
 229             if (method.getName().equalsIgnoreCase(math.getOperation().name()) && tool.getMetaAccess().lookupJavaType(Math.class).equals(method.getDeclaringClass())) {
 230                 /*
 231                  * A root compilation of the intrinsic method should emit the full assembly
 232                  * implementation.
 233                  */
 234                 return;
 235             }
 236 
 237         }
 238         ForeignCallDescriptor foreignCall = toForeignCall(math.getOperation());
 239         if (foreignCall != null) {
 240             StructuredGraph graph = math.graph();
 241             ForeignCallNode call = graph.add(new ForeignCallNode(foreignCalls, toForeignCall(math.getOperation()), math.getX(), math.getY()));
 242             graph.addAfterFixed(tool.lastFixedNode(), call);
 243             math.replaceAtUsages(call);
 244         }
 245     }
 246 
 247     private void lowerUnaryMath(UnaryMathIntrinsicNode math, LoweringTool tool) {
 248         if (tool.getLoweringStage() == LoweringTool.StandardLoweringStage.HIGH_TIER) {
 249             return;
 250         }
 251         ResolvedJavaMethod method = math.graph().method();
 252         if (method != null) {
 253             if (method.getAnnotation(Snippet.class) != null) {
 254                 /*
 255                  * In the context of the snippet use the LIR lowering instead of the Node lowering.
 256                  */
 257                 return;
 258             }
 259             if (method.getName().equalsIgnoreCase(math.getOperation().name()) && tool.getMetaAccess().lookupJavaType(Math.class).equals(method.getDeclaringClass())) {
 260                 /*
 261                  * A root compilation of the intrinsic method should emit the full assembly
 262                  * implementation.
 263                  */
 264                 return;
 265             }
 266 
 267         }
 268         ForeignCallDescriptor foreignCall = toForeignCall(math.getOperation());
 269         if (foreignCall != null) {
 270             StructuredGraph graph = math.graph();
 271             ForeignCallNode call = math.graph().add(new ForeignCallNode(foreignCalls, foreignCall, math.getValue()));
 272             graph.addAfterFixed(tool.lastFixedNode(), call);
 273             math.replaceAtUsages(call);
 274         }
 275     }
 276 
 277     protected ForeignCallDescriptor toForeignCall(UnaryOperation operation) {
 278         return operation.foreignCallDescriptor;
 279     }
 280 
 281     protected ForeignCallDescriptor toForeignCall(BinaryOperation operation) {
 282         return operation.foreignCallDescriptor;
 283     }
 284 
 285     protected void lowerVerifyHeap(VerifyHeapNode n) {
 286         GraphUtil.removeFixedWithUnusedInputs(n);
 287     }
 288 
 289     protected AddressNode createOffsetAddress(StructuredGraph graph, ValueNode object, long offset) {
 290         ValueNode o = ConstantNode.forIntegerKind(target.wordJavaKind, offset, graph);
 291         return graph.unique(new OffsetAddressNode(object, o));
 292     }
 293 
 294     protected AddressNode createFieldAddress(StructuredGraph graph, ValueNode object, ResolvedJavaField field) {
 295         int offset = fieldOffset(field);
 296         if (offset >= 0) {
 297             return createOffsetAddress(graph, object, offset);
 298         } else {
 299             return null;
 300         }
 301     }
 302 
 303     protected void lowerLoadFieldNode(LoadFieldNode loadField, LoweringTool tool) {
 304         assert loadField.getStackKind() != JavaKind.Illegal;
 305         StructuredGraph graph = loadField.graph();
 306         ResolvedJavaField field = loadField.field();
 307         ValueNode object = loadField.isStatic() ? staticFieldBase(graph, field) : loadField.object();
 308         Stamp loadStamp = loadStamp(loadField.stamp(), field.getJavaKind());
 309 
 310         AddressNode address = createFieldAddress(graph, object, field);
 311         assert address != null : "Field that is loaded must not be eliminated: " + field.getDeclaringClass().toJavaName(true) + "." + field.getName();
 312 
 313         ReadNode memoryRead = graph.add(new ReadNode(address, fieldLocationIdentity(field), loadStamp, fieldLoadBarrierType(field)));
 314         ValueNode readValue = implicitLoadConvert(graph, field.getJavaKind(), memoryRead);
 315         loadField.replaceAtUsages(readValue);
 316         graph.replaceFixed(loadField, memoryRead);
 317 
 318         memoryRead.setGuard(createNullCheck(object, memoryRead, tool));
 319 
 320         if (loadField.isVolatile()) {
 321             MembarNode preMembar = graph.add(new MembarNode(JMM_PRE_VOLATILE_READ));
 322             graph.addBeforeFixed(memoryRead, preMembar);
 323             MembarNode postMembar = graph.add(new MembarNode(JMM_POST_VOLATILE_READ));
 324             graph.addAfterFixed(memoryRead, postMembar);
 325         }
 326     }
 327 
 328     protected void lowerStoreFieldNode(StoreFieldNode storeField, LoweringTool tool) {
 329         StructuredGraph graph = storeField.graph();
 330         ResolvedJavaField field = storeField.field();
 331         ValueNode object = storeField.isStatic() ? staticFieldBase(graph, field) : storeField.object();
 332         ValueNode value = implicitStoreConvert(graph, storeField.field().getJavaKind(), storeField.value());
 333         AddressNode address = createFieldAddress(graph, object, field);
 334         assert address != null;
 335 
 336         WriteNode memoryWrite = graph.add(new WriteNode(address, fieldLocationIdentity(field), value, fieldStoreBarrierType(storeField.field())));
 337         memoryWrite.setStateAfter(storeField.stateAfter());
 338         graph.replaceFixedWithFixed(storeField, memoryWrite);
 339         memoryWrite.setGuard(createNullCheck(object, memoryWrite, tool));
 340 
 341         if (storeField.isVolatile()) {
 342             MembarNode preMembar = graph.add(new MembarNode(JMM_PRE_VOLATILE_WRITE));
 343             graph.addBeforeFixed(memoryWrite, preMembar);
 344             MembarNode postMembar = graph.add(new MembarNode(JMM_POST_VOLATILE_WRITE));
 345             graph.addAfterFixed(memoryWrite, postMembar);
 346         }
 347     }
 348 
 349     public AddressNode createArrayAddress(StructuredGraph graph, ValueNode array, JavaKind elementKind, ValueNode index) {
 350         ValueNode wordIndex;
 351         if (target.wordSize > 4) {
 352             wordIndex = graph.unique(new SignExtendNode(index, target.wordSize * 8));
 353         } else {
 354             assert target.wordSize == 4 : "unsupported word size";
 355             wordIndex = index;
 356         }
 357 
 358         int shift = CodeUtil.log2(arrayScalingFactor(elementKind));
 359         ValueNode scaledIndex = graph.unique(new LeftShiftNode(wordIndex, ConstantNode.forInt(shift, graph)));
 360 
 361         int base = arrayBaseOffset(elementKind);
 362         ValueNode offset = graph.unique(new AddNode(scaledIndex, ConstantNode.forIntegerKind(target.wordJavaKind, base, graph)));
 363 
 364         return graph.unique(new OffsetAddressNode(array, offset));
 365     }
 366 
 367     protected void lowerLoadIndexedNode(LoadIndexedNode loadIndexed, LoweringTool tool) {
 368         StructuredGraph graph = loadIndexed.graph();
 369         JavaKind elementKind = loadIndexed.elementKind();
 370         Stamp loadStamp = loadStamp(loadIndexed.stamp(), elementKind);
 371 
 372         PiNode pi = getBoundsCheckedIndex(loadIndexed, tool, null);
 373         ValueNode checkedIndex = pi;
 374         if (checkedIndex == null) {
 375             checkedIndex = loadIndexed.index();
 376         }
 377 
 378         AddressNode address = createArrayAddress(graph, loadIndexed.array(), elementKind, checkedIndex);
 379         ReadNode memoryRead = graph.add(new ReadNode(address, NamedLocationIdentity.getArrayLocation(elementKind), loadStamp, BarrierType.NONE));
 380         ValueNode readValue = implicitLoadConvert(graph, elementKind, memoryRead);
 381 
 382         if (pi != null) {
 383             memoryRead.setGuard(pi.getGuard());
 384         }
 385 
 386         loadIndexed.replaceAtUsages(readValue);
 387         graph.replaceFixed(loadIndexed, memoryRead);
 388     }
 389 
 390     protected void lowerStoreIndexedNode(StoreIndexedNode storeIndexed, LoweringTool tool) {
 391         StructuredGraph graph = storeIndexed.graph();
 392 
 393         GuardingNode[] nullCheckReturn = new GuardingNode[1];
 394         PiNode pi = getBoundsCheckedIndex(storeIndexed, tool, nullCheckReturn);
 395         ValueNode checkedIndex;
 396         GuardingNode boundsCheck;
 397         if (pi == null) {
 398             checkedIndex = storeIndexed.index();
 399             boundsCheck = null;
 400         } else {
 401             checkedIndex = pi;
 402             boundsCheck = pi.getGuard();
 403         }
 404 
 405         JavaKind elementKind = storeIndexed.elementKind();
 406 
 407         ValueNode value = storeIndexed.value();
 408         ValueNode array = storeIndexed.array();
 409         LogicNode condition = null;
 410         if (elementKind == JavaKind.Object && !StampTool.isPointerAlwaysNull(value)) {
 411             /* Array store check. */
 412             TypeReference arrayType = StampTool.typeReferenceOrNull(array);
 413             if (arrayType != null && arrayType.isExact()) {
 414                 ResolvedJavaType elementType = arrayType.getType().getComponentType();
 415                 if (!elementType.isJavaLangObject()) {
 416                     TypeReference typeReference = TypeReference.createTrusted(storeIndexed.graph().getAssumptions(), elementType);
 417                     LogicNode typeTest = graph.addOrUniqueWithInputs(InstanceOfNode.create(typeReference, value));
 418                     condition = LogicNode.or(graph.unique(IsNullNode.create(value)), typeTest, GraalDirectives.UNLIKELY_PROBABILITY);
 419                 }
 420             } else {
 421                 /*
 422                  * The guard on the read hub should be the null check of the array that was
 423                  * introduced earlier.
 424                  */
 425                 GuardingNode nullCheck = nullCheckReturn[0];
 426                 assert nullCheckReturn[0] != null || createNullCheck(array, storeIndexed, tool) == null;
 427                 ValueNode arrayClass = createReadHub(graph, graph.unique(new PiNode(array, (ValueNode) nullCheck)), tool);
 428                 ValueNode componentHub = createReadArrayComponentHub(graph, arrayClass, storeIndexed);
 429                 LogicNode typeTest = graph.unique(InstanceOfDynamicNode.create(graph.getAssumptions(), tool.getConstantReflection(), componentHub, value, false));
 430                 condition = LogicNode.or(graph.unique(IsNullNode.create(value)), typeTest, GraalDirectives.UNLIKELY_PROBABILITY);
 431             }
 432         }
 433 
 434         AddressNode address = createArrayAddress(graph, array, elementKind, checkedIndex);
 435         WriteNode memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), implicitStoreConvert(graph, elementKind, value),
 436                         arrayStoreBarrierType(storeIndexed.elementKind())));
 437         memoryWrite.setGuard(boundsCheck);
 438         if (condition != null) {
 439             GuardingNode storeCheckGuard = tool.createGuard(storeIndexed, condition, DeoptimizationReason.ArrayStoreException, DeoptimizationAction.InvalidateReprofile);
 440             memoryWrite.setStoreCheckGuard(storeCheckGuard);
 441         }
 442         memoryWrite.setStateAfter(storeIndexed.stateAfter());
 443         graph.replaceFixedWithFixed(storeIndexed, memoryWrite);
 444     }
 445 
 446     protected void lowerArrayLengthNode(ArrayLengthNode arrayLengthNode, LoweringTool tool) {
 447         StructuredGraph graph = arrayLengthNode.graph();
 448         ValueNode array = arrayLengthNode.array();
 449 
 450         AddressNode address = createOffsetAddress(graph, array, arrayLengthOffset());
 451         ReadNode arrayLengthRead = graph.add(new ReadNode(address, ARRAY_LENGTH_LOCATION, StampFactory.positiveInt(), BarrierType.NONE));
 452         arrayLengthRead.setGuard(createNullCheck(array, arrayLengthNode, tool));
 453         graph.replaceFixedWithFixed(arrayLengthNode, arrayLengthRead);
 454     }
 455 
 456     protected void lowerLoadHubNode(LoadHubNode loadHub, LoweringTool tool) {
 457         StructuredGraph graph = loadHub.graph();
 458         if (tool.getLoweringStage() != LoweringTool.StandardLoweringStage.LOW_TIER) {
 459             return;
 460         }
 461         if (graph.getGuardsStage().allowsFloatingGuards()) {
 462             return;
 463         }
 464         ValueNode hub = createReadHub(graph, loadHub.getValue(), tool);
 465         loadHub.replaceAtUsagesAndDelete(hub);
 466     }
 467 
 468     protected void lowerMonitorEnterNode(MonitorEnterNode monitorEnter, LoweringTool tool, StructuredGraph graph) {
 469         ValueNode object = monitorEnter.object();
 470         GuardingNode nullCheck = createNullCheck(object, monitorEnter, tool);
 471         if (nullCheck != null) {
 472             object = graph.unique(new PiNode(object, ((ObjectStamp) object.stamp()).improveWith(StampFactory.objectNonNull()), (ValueNode) nullCheck));
 473         }
 474         ValueNode hub = graph.addOrUnique(LoadHubNode.create(object, tool.getStampProvider(), tool.getMetaAccess(), tool.getConstantReflection()));
 475         RawMonitorEnterNode rawMonitorEnter = graph.add(new RawMonitorEnterNode(object, hub, monitorEnter.getMonitorId()));
 476         rawMonitorEnter.setStateBefore(monitorEnter.stateBefore());
 477         rawMonitorEnter.setStateAfter(monitorEnter.stateAfter());
 478         graph.replaceFixedWithFixed(monitorEnter, rawMonitorEnter);
 479     }
 480 
 481     protected void lowerCompareAndSwapNode(CompareAndSwapNode cas) {
 482         StructuredGraph graph = cas.graph();
 483         JavaKind valueKind = cas.getValueKind();
 484 
 485         ValueNode expectedValue = implicitStoreConvert(graph, valueKind, cas.expected());
 486         ValueNode newValue = implicitStoreConvert(graph, valueKind, cas.newValue());
 487 
 488         AddressNode address = graph.unique(new OffsetAddressNode(cas.object(), cas.offset()));
 489         LoweredCompareAndSwapNode atomicNode = graph.add(new LoweredCompareAndSwapNode(address, cas.getLocationIdentity(), expectedValue, newValue, compareAndSwapBarrierType(cas)));
 490         atomicNode.setStateAfter(cas.stateAfter());
 491         graph.replaceFixedWithFixed(cas, atomicNode);
 492     }
 493 
 494     protected void lowerAtomicReadAndWriteNode(AtomicReadAndWriteNode n) {
 495         StructuredGraph graph = n.graph();
 496         JavaKind valueKind = n.getValueKind();
 497 
 498         ValueNode newValue = implicitStoreConvert(graph, valueKind, n.newValue());
 499 
 500         AddressNode address = graph.unique(new OffsetAddressNode(n.object(), n.offset()));
 501         LoweredAtomicReadAndWriteNode memoryRead = graph.add(new LoweredAtomicReadAndWriteNode(address, n.getLocationIdentity(), newValue, atomicReadAndWriteBarrierType(n)));
 502         memoryRead.setStateAfter(n.stateAfter());
 503 
 504         ValueNode readValue = implicitLoadConvert(graph, valueKind, memoryRead);
 505         n.stateAfter().replaceFirstInput(n, memoryRead);
 506         n.replaceAtUsages(readValue);
 507         graph.replaceFixedWithFixed(n, memoryRead);
 508     }
 509 
 510     /**
 511      * @param tool utility for performing the lowering
 512      */
 513     protected void lowerUnsafeLoadNode(UnsafeLoadNode load, LoweringTool tool) {
 514         StructuredGraph graph = load.graph();
 515         if (load instanceof GuardedUnsafeLoadNode) {
 516             GuardedUnsafeLoadNode guardedLoad = (GuardedUnsafeLoadNode) load;
 517             GuardingNode guard = guardedLoad.getGuard();
 518             if (guard == null) {
 519                 // can float freely if the guard folded away
 520                 ReadNode memoryRead = createUnsafeRead(graph, load, null);
 521                 memoryRead.setForceFixed(false);
 522                 graph.replaceFixedWithFixed(load, memoryRead);
 523             } else {
 524                 // must be guarded, but flows below the guard
 525                 ReadNode memoryRead = createUnsafeRead(graph, load, guard);
 526                 graph.replaceFixedWithFixed(load, memoryRead);
 527             }
 528         } else {
 529             // never had a guarding condition so it must be fixed, creation of the read will force
 530             // it to be fixed
 531             ReadNode memoryRead = createUnsafeRead(graph, load, null);
 532             graph.replaceFixedWithFixed(load, memoryRead);
 533         }
 534     }
 535 
 536     protected AddressNode createUnsafeAddress(StructuredGraph graph, ValueNode object, ValueNode offset) {
 537         if (object.isConstant() && object.asConstant().isDefaultForKind()) {
 538             return graph.unique(new RawAddressNode(offset));
 539         } else {
 540             return graph.unique(new OffsetAddressNode(object, offset));
 541         }
 542     }
 543 
 544     protected ReadNode createUnsafeRead(StructuredGraph graph, UnsafeLoadNode load, GuardingNode guard) {
 545         boolean compressible = load.accessKind() == JavaKind.Object;
 546         JavaKind readKind = load.accessKind();
 547         Stamp loadStamp = loadStamp(load.stamp(), readKind, compressible);
 548         AddressNode address = createUnsafeAddress(graph, load.object(), load.offset());
 549         ReadNode memoryRead = graph.add(new ReadNode(address, load.getLocationIdentity(), loadStamp, guard, BarrierType.NONE));
 550         if (guard == null) {
 551             // An unsafe read must not float otherwise it may float above
 552             // a test guaranteeing the read is safe.
 553             memoryRead.setForceFixed(true);
 554         }
 555         ValueNode readValue = implicitLoadConvert(graph, readKind, memoryRead, compressible);
 556         load.replaceAtUsages(readValue);
 557         return memoryRead;
 558     }
 559 
 560     protected void lowerUnsafeMemoryLoadNode(UnsafeMemoryLoadNode load) {
 561         StructuredGraph graph = load.graph();
 562         JavaKind readKind = load.getKind();
 563         assert readKind != JavaKind.Object;
 564         Stamp loadStamp = loadStamp(load.stamp(), readKind, false);
 565         AddressNode address = graph.unique(new RawAddressNode(load.getAddress()));
 566         ReadNode memoryRead = graph.add(new ReadNode(address, load.getLocationIdentity(), loadStamp, BarrierType.NONE));
 567         // An unsafe read must not float otherwise it may float above
 568         // a test guaranteeing the read is safe.
 569         memoryRead.setForceFixed(true);
 570         ValueNode readValue = implicitLoadConvert(graph, readKind, memoryRead, false);
 571         load.replaceAtUsages(readValue);
 572         graph.replaceFixedWithFixed(load, memoryRead);
 573     }
 574 
 575     protected void lowerUnsafeStoreNode(UnsafeStoreNode store) {
 576         StructuredGraph graph = store.graph();
 577         boolean compressible = store.value().getStackKind() == JavaKind.Object;
 578         JavaKind valueKind = store.accessKind();
 579         ValueNode value = implicitStoreConvert(graph, valueKind, store.value(), compressible);
 580         AddressNode address = createUnsafeAddress(graph, store.object(), store.offset());
 581         WriteNode write = graph.add(new WriteNode(address, store.getLocationIdentity(), value, unsafeStoreBarrierType(store)));
 582         write.setStateAfter(store.stateAfter());
 583         graph.replaceFixedWithFixed(store, write);
 584     }
 585 
 586     protected void lowerUnsafeMemoryStoreNode(UnsafeMemoryStoreNode store) {
 587         StructuredGraph graph = store.graph();
 588         assert store.getValue().getStackKind() != JavaKind.Object;
 589         JavaKind valueKind = store.getKind();
 590         ValueNode value = implicitStoreConvert(graph, valueKind, store.getValue(), false);
 591         AddressNode address = graph.unique(new RawAddressNode(store.getAddress()));
 592         WriteNode write = graph.add(new WriteNode(address, store.getLocationIdentity(), value, BarrierType.NONE));
 593         write.setStateAfter(store.stateAfter());
 594         graph.replaceFixedWithFixed(store, write);
 595     }
 596 
 597     protected void lowerJavaReadNode(JavaReadNode read) {
 598         StructuredGraph graph = read.graph();
 599         JavaKind valueKind = read.getReadKind();
 600         Stamp loadStamp = loadStamp(read.stamp(), valueKind, read.isCompressible());
 601 
 602         ReadNode memoryRead = graph.add(new ReadNode(read.getAddress(), read.getLocationIdentity(), loadStamp, read.getBarrierType()));
 603         GuardingNode guard = read.getGuard();
 604         ValueNode readValue = implicitLoadConvert(graph, valueKind, memoryRead, read.isCompressible());
 605         if (guard == null) {
 606             // An unsafe read must not float otherwise it may float above
 607             // a test guaranteeing the read is safe.
 608             memoryRead.setForceFixed(true);
 609         } else {
 610             memoryRead.setGuard(guard);
 611         }
 612         read.replaceAtUsages(readValue);
 613         graph.replaceFixed(read, memoryRead);
 614     }
 615 
 616     protected void lowerJavaWriteNode(JavaWriteNode write) {
 617         StructuredGraph graph = write.graph();
 618         JavaKind valueKind = write.getWriteKind();
 619         ValueNode value = implicitStoreConvert(graph, valueKind, write.value(), write.isCompressible());
 620 
 621         WriteNode memoryWrite = graph.add(new WriteNode(write.getAddress(), write.getLocationIdentity(), value, write.getBarrierType(), write.isInitialization()));
 622         memoryWrite.setStateAfter(write.stateAfter());
 623         graph.replaceFixedWithFixed(write, memoryWrite);
 624         memoryWrite.setGuard(write.getGuard());
 625     }
 626 
 627     protected void lowerCommitAllocationNode(CommitAllocationNode commit, LoweringTool tool) {
 628         StructuredGraph graph = commit.graph();
 629         if (graph.getGuardsStage() == StructuredGraph.GuardsStage.FIXED_DEOPTS) {
 630             List<AbstractNewObjectNode> recursiveLowerings = new ArrayList<>();
 631 
 632             ValueNode[] allocations = new ValueNode[commit.getVirtualObjects().size()];
 633             BitSet omittedValues = new BitSet();
 634             int valuePos = 0;
 635             for (int objIndex = 0; objIndex < commit.getVirtualObjects().size(); objIndex++) {
 636                 VirtualObjectNode virtual = commit.getVirtualObjects().get(objIndex);
 637                 int entryCount = virtual.entryCount();
 638                 AbstractNewObjectNode newObject;
 639                 if (virtual instanceof VirtualInstanceNode) {
 640                     newObject = graph.add(createNewInstanceFromVirtual(virtual));
 641                 } else {
 642                     newObject = graph.add(createNewArrayFromVirtual(virtual, ConstantNode.forInt(entryCount, graph)));
 643                 }
 644                 recursiveLowerings.add(newObject);
 645                 graph.addBeforeFixed(commit, newObject);
 646                 allocations[objIndex] = newObject;
 647                 for (int i = 0; i < entryCount; i++) {
 648                     ValueNode value = commit.getValues().get(valuePos);
 649                     if (value instanceof VirtualObjectNode) {
 650                         value = allocations[commit.getVirtualObjects().indexOf(value)];
 651                     }
 652                     if (value == null) {
 653                         omittedValues.set(valuePos);
 654                     } else if (!(value.isConstant() && value.asConstant().isDefaultForKind())) {
 655                         // Constant.illegal is always the defaultForKind, so it is skipped
 656                         JavaKind valueKind = value.getStackKind();
 657                         JavaKind entryKind = virtual.entryKind(i);
 658 
 659                         // Truffle requires some leniency in terms of what can be put where:
 660                         assert valueKind.getStackKind() == entryKind.getStackKind() ||
 661                                         (valueKind == JavaKind.Long || valueKind == JavaKind.Double || (valueKind == JavaKind.Int && virtual instanceof VirtualArrayNode));
 662                         AddressNode address = null;
 663                         BarrierType barrierType = null;
 664                         if (virtual instanceof VirtualInstanceNode) {
 665                             ResolvedJavaField field = ((VirtualInstanceNode) virtual).field(i);
 666                             long offset = fieldOffset(field);
 667                             if (offset >= 0) {
 668                                 address = createOffsetAddress(graph, newObject, offset);
 669                                 barrierType = fieldInitializationBarrier(entryKind);
 670                             }
 671                         } else {
 672                             address = createOffsetAddress(graph, newObject, arrayBaseOffset(entryKind) + i * arrayScalingFactor(entryKind));
 673                             barrierType = arrayInitializationBarrier(entryKind);
 674                         }
 675                         if (address != null) {
 676                             WriteNode write = new WriteNode(address, initLocationIdentity(), implicitStoreConvert(graph, entryKind, value), barrierType);
 677                             graph.addAfterFixed(newObject, graph.add(write));
 678                         }
 679                     }
 680                     valuePos++;
 681 
 682                 }
 683             }
 684             valuePos = 0;
 685 
 686             for (int objIndex = 0; objIndex < commit.getVirtualObjects().size(); objIndex++) {
 687                 VirtualObjectNode virtual = commit.getVirtualObjects().get(objIndex);
 688                 int entryCount = virtual.entryCount();
 689                 ValueNode newObject = allocations[objIndex];
 690                 for (int i = 0; i < entryCount; i++) {
 691                     if (omittedValues.get(valuePos)) {
 692                         ValueNode value = commit.getValues().get(valuePos);
 693                         assert value instanceof VirtualObjectNode;
 694                         ValueNode allocValue = allocations[commit.getVirtualObjects().indexOf(value)];
 695                         if (!(allocValue.isConstant() && allocValue.asConstant().isDefaultForKind())) {
 696                             assert virtual.entryKind(i) == JavaKind.Object && allocValue.getStackKind() == JavaKind.Object;
 697                             AddressNode address;
 698                             BarrierType barrierType;
 699                             if (virtual instanceof VirtualInstanceNode) {
 700                                 VirtualInstanceNode virtualInstance = (VirtualInstanceNode) virtual;
 701                                 address = createFieldAddress(graph, newObject, virtualInstance.field(i));
 702                                 barrierType = BarrierType.IMPRECISE;
 703                             } else {
 704                                 address = createArrayAddress(graph, newObject, virtual.entryKind(i), ConstantNode.forInt(i, graph));
 705                                 barrierType = BarrierType.PRECISE;
 706                             }
 707                             if (address != null) {
 708                                 WriteNode write = new WriteNode(address, initLocationIdentity(), implicitStoreConvert(graph, JavaKind.Object, allocValue), barrierType);
 709                                 graph.addBeforeFixed(commit, graph.add(write));
 710                             }
 711                         }
 712                     }
 713                     valuePos++;
 714                 }
 715             }
 716 
 717             finishAllocatedObjects(tool, commit, allocations);
 718             graph.removeFixed(commit);
 719 
 720             for (AbstractNewObjectNode recursiveLowering : recursiveLowerings) {
 721                 recursiveLowering.lower(tool);
 722             }
 723         }
 724     }
 725 
 726     public NewInstanceNode createNewInstanceFromVirtual(VirtualObjectNode virtual) {
 727         return new NewInstanceNode(virtual.type(), true);
 728     }
 729 
 730     protected NewArrayNode createNewArrayFromVirtual(VirtualObjectNode virtual, ValueNode length) {
 731         return new NewArrayNode(((VirtualArrayNode) virtual).componentType(), length, true);
 732     }
 733 
 734     public void finishAllocatedObjects(LoweringTool tool, CommitAllocationNode commit, ValueNode[] allocations) {
 735         StructuredGraph graph = commit.graph();
 736         for (int objIndex = 0; objIndex < commit.getVirtualObjects().size(); objIndex++) {
 737             FixedValueAnchorNode anchor = graph.add(new FixedValueAnchorNode(allocations[objIndex]));
 738             allocations[objIndex] = anchor;
 739             graph.addBeforeFixed(commit, anchor);
 740         }
 741         for (int objIndex = 0; objIndex < commit.getVirtualObjects().size(); objIndex++) {
 742             for (MonitorIdNode monitorId : commit.getLocks(objIndex)) {
 743                 MonitorEnterNode enter = graph.add(new MonitorEnterNode(allocations[objIndex], monitorId));
 744                 graph.addBeforeFixed(commit, enter);
 745                 enter.lower(tool);
 746             }
 747         }
 748         for (Node usage : commit.usages().snapshot()) {
 749             AllocatedObjectNode addObject = (AllocatedObjectNode) usage;
 750             int index = commit.getVirtualObjects().indexOf(addObject.getVirtualObject());
 751             addObject.replaceAtUsagesAndDelete(allocations[index]);
 752         }
 753         insertAllocationBarrier(commit, graph);
 754     }
 755 
 756     /**
 757      * Insert the required {@link MemoryBarriers#STORE_STORE} barrier for an allocation and also
 758      * include the {@link MemoryBarriers#LOAD_STORE} required for final fields if any final fields
 759      * are being written, as if {@link FinalFieldBarrierNode} were emitted.
 760      */
 761     private void insertAllocationBarrier(CommitAllocationNode commit, StructuredGraph graph) {
 762         int barrier = MemoryBarriers.STORE_STORE;
 763         outer: for (VirtualObjectNode vobj : commit.getVirtualObjects()) {
 764             for (ResolvedJavaField field : vobj.type().getInstanceFields(true)) {
 765                 if (field.isFinal()) {
 766                     barrier = barrier | MemoryBarriers.LOAD_STORE;
 767                     break outer;
 768                 }
 769             }
 770         }
 771         graph.addAfterFixed(commit, graph.add(new MembarNode(barrier, initLocationIdentity())));
 772     }
 773 
 774     /**
 775      * @param field the field whose barrier type should be returned
 776      */
 777     protected BarrierType fieldLoadBarrierType(ResolvedJavaField field) {
 778         return BarrierType.NONE;
 779     }
 780 
 781     protected BarrierType fieldStoreBarrierType(ResolvedJavaField field) {
 782         if (field.getJavaKind() == JavaKind.Object) {
 783             return BarrierType.IMPRECISE;
 784         }
 785         return BarrierType.NONE;
 786     }
 787 
 788     protected BarrierType arrayStoreBarrierType(JavaKind elementKind) {
 789         if (elementKind == JavaKind.Object) {
 790             return BarrierType.PRECISE;
 791         }
 792         return BarrierType.NONE;
 793     }
 794 
 795     public BarrierType fieldInitializationBarrier(JavaKind entryKind) {
 796         return entryKind == JavaKind.Object ? BarrierType.IMPRECISE : BarrierType.NONE;
 797     }
 798 
 799     public BarrierType arrayInitializationBarrier(JavaKind entryKind) {
 800         return entryKind == JavaKind.Object ? BarrierType.PRECISE : BarrierType.NONE;
 801     }
 802 
 803     protected BarrierType unsafeStoreBarrierType(UnsafeStoreNode store) {
 804         return storeBarrierType(store.object(), store.value());
 805     }
 806 
 807     protected BarrierType compareAndSwapBarrierType(CompareAndSwapNode cas) {
 808         return storeBarrierType(cas.object(), cas.expected());
 809     }
 810 
 811     protected BarrierType atomicReadAndWriteBarrierType(AtomicReadAndWriteNode n) {
 812         return storeBarrierType(n.object(), n.newValue());
 813     }
 814 
 815     protected BarrierType storeBarrierType(ValueNode object, ValueNode value) {
 816         if (value.getStackKind() == JavaKind.Object) {
 817             ResolvedJavaType type = StampTool.typeOrNull(object);
 818             if (type != null && !type.isArray()) {
 819                 return BarrierType.IMPRECISE;
 820             } else {
 821                 return BarrierType.PRECISE;
 822             }
 823         }
 824         return BarrierType.NONE;
 825     }
 826 
 827     public abstract int fieldOffset(ResolvedJavaField field);
 828 
 829     public FieldLocationIdentity fieldLocationIdentity(ResolvedJavaField field) {
 830         return new FieldLocationIdentity(field);
 831     }
 832 
 833     public abstract ValueNode staticFieldBase(StructuredGraph graph, ResolvedJavaField field);
 834 
 835     public abstract int arrayLengthOffset();
 836 
 837     public abstract int arrayBaseOffset(JavaKind elementKind);
 838 
 839     public int arrayScalingFactor(JavaKind elementKind) {
 840         return target.arch.getPlatformKind(elementKind).getSizeInBytes();
 841     }
 842 
 843     public abstract LocationIdentity initLocationIdentity();
 844 
 845     public Stamp loadStamp(Stamp stamp, JavaKind kind) {
 846         return loadStamp(stamp, kind, true);
 847     }
 848 
 849     /**
 850      * @param compressible whether the stamp should be compressible
 851      */
 852     protected Stamp loadStamp(Stamp stamp, JavaKind kind, boolean compressible) {
 853         switch (kind) {
 854             case Boolean:
 855             case Byte:
 856                 return IntegerStamp.OPS.getNarrow().foldStamp(32, 8, stamp);
 857             case Char:
 858             case Short:
 859                 return IntegerStamp.OPS.getNarrow().foldStamp(32, 16, stamp);
 860         }
 861         return stamp;
 862     }
 863 
 864     public final ValueNode implicitLoadConvert(StructuredGraph graph, JavaKind kind, ValueNode value) {
 865         return implicitLoadConvert(graph, kind, value, true);
 866     }
 867 
 868     public ValueNode implicitLoadConvert(JavaKind kind, ValueNode value) {
 869         return implicitLoadConvert(kind, value, true);
 870     }
 871 
 872     protected final ValueNode implicitLoadConvert(StructuredGraph graph, JavaKind kind, ValueNode value, boolean compressible) {
 873         ValueNode ret = implicitLoadConvert(kind, value, compressible);
 874         if (!ret.isAlive()) {
 875             ret = graph.addOrUnique(ret);
 876         }
 877         return ret;
 878     }
 879 
 880     /**
 881      * @param compressible whether the covert should be compressible
 882      */
 883     protected ValueNode implicitLoadConvert(JavaKind kind, ValueNode value, boolean compressible) {
 884         switch (kind) {
 885             case Byte:
 886             case Short:
 887                 return new SignExtendNode(value, 32);
 888             case Boolean:
 889             case Char:
 890                 return new ZeroExtendNode(value, 32);
 891         }
 892         return value;
 893     }
 894 
 895     public final ValueNode implicitStoreConvert(StructuredGraph graph, JavaKind kind, ValueNode value) {
 896         return implicitStoreConvert(graph, kind, value, true);
 897     }
 898 
 899     public ValueNode implicitStoreConvert(JavaKind kind, ValueNode value) {
 900         return implicitStoreConvert(kind, value, true);
 901     }
 902 
 903     protected final ValueNode implicitStoreConvert(StructuredGraph graph, JavaKind kind, ValueNode value, boolean compressible) {
 904         ValueNode ret = implicitStoreConvert(kind, value, compressible);
 905         if (!ret.isAlive()) {
 906             ret = graph.addOrUnique(ret);
 907         }
 908         return ret;
 909     }
 910 
 911     /**
 912      * @param compressible whether the covert should be compressible
 913      */
 914     protected ValueNode implicitStoreConvert(JavaKind kind, ValueNode value, boolean compressible) {
 915         switch (kind) {
 916             case Boolean:
 917             case Byte:
 918                 return new NarrowNode(value, 8);
 919             case Char:
 920             case Short:
 921                 return new NarrowNode(value, 16);
 922         }
 923         return value;
 924     }
 925 
 926     protected abstract ValueNode createReadHub(StructuredGraph graph, ValueNode object, LoweringTool tool);
 927 
 928     protected abstract ValueNode createReadArrayComponentHub(StructuredGraph graph, ValueNode arrayHub, FixedNode anchor);
 929 
 930     protected PiNode getBoundsCheckedIndex(AccessIndexedNode n, LoweringTool tool, GuardingNode[] nullCheckReturn) {
 931         StructuredGraph graph = n.graph();
 932         ValueNode array = n.array();
 933         ValueNode arrayLength = readArrayLength(array, tool.getConstantReflection());
 934         if (arrayLength == null) {
 935             Stamp stamp = StampFactory.positiveInt();
 936             AddressNode address = createOffsetAddress(graph, array, arrayLengthOffset());
 937             ReadNode readArrayLength = graph.add(new ReadNode(address, ARRAY_LENGTH_LOCATION, stamp, BarrierType.NONE));
 938             graph.addBeforeFixed(n, readArrayLength);
 939             GuardingNode nullCheck = createNullCheck(array, readArrayLength, tool);
 940             if (nullCheckReturn != null) {
 941                 nullCheckReturn[0] = nullCheck;
 942             }
 943             readArrayLength.setGuard(nullCheck);
 944             arrayLength = readArrayLength;
 945         } else {
 946             if (array instanceof AbstractNewArrayNode) {
 947                 arrayLength = n.graph().addOrUnique(new PiNode(arrayLength, StampFactory.positiveInt()));
 948             }
 949             arrayLength = arrayLength.isAlive() ? arrayLength : graph.addOrUniqueWithInputs(arrayLength);
 950         }
 951 
 952         if (arrayLength.isConstant() && n.index().isConstant()) {
 953             int l = arrayLength.asJavaConstant().asInt();
 954             int i = n.index().asJavaConstant().asInt();
 955             if (i >= 0 && i < l) {
 956                 // unneeded range check
 957                 return null;
 958             }
 959         }
 960 
 961         GuardingNode guard = tool.createGuard(n, graph.unique(new IntegerBelowNode(n.index(), arrayLength)), BoundsCheckException, InvalidateReprofile);
 962         IntegerStamp lengthStamp = (IntegerStamp) arrayLength.stamp();
 963         IntegerStamp indexStamp = StampFactory.forInteger(32, 0, lengthStamp.upperBound() - 1);
 964         return graph.unique(new PiNode(n.index(), indexStamp, guard.asNode()));
 965     }
 966 
 967     protected GuardingNode createNullCheck(ValueNode object, FixedNode before, LoweringTool tool) {
 968         if (StampTool.isPointerNonNull(object)) {
 969             return null;
 970         }
 971         return tool.createGuard(before, before.graph().unique(IsNullNode.create(object)), NullCheckException, InvalidateReprofile, JavaConstant.NULL_POINTER, true);
 972     }
 973 
 974     @Override
 975     public ValueNode reconstructArrayIndex(JavaKind elementKind, AddressNode address) {
 976         StructuredGraph graph = address.graph();
 977         ValueNode offset = ((OffsetAddressNode) address).getOffset();
 978 
 979         int base = arrayBaseOffset(elementKind);
 980         ValueNode scaledIndex = graph.unique(new SubNode(offset, ConstantNode.forIntegerStamp(offset.stamp(), base, graph)));
 981 
 982         int shift = CodeUtil.log2(arrayScalingFactor(elementKind));
 983         ValueNode ret = graph.unique(new RightShiftNode(scaledIndex, ConstantNode.forInt(shift, graph)));
 984         return IntegerConvertNode.convert(ret, StampFactory.forKind(JavaKind.Int), graph);
 985     }
 986 }