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