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