1 /*
   2  * Copyright (c) 2009, 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.nodes.java;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
  27 
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.core.common.type.TypeReference;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.Canonicalizable;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.spi.Virtualizable;
  39 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  40 import org.graalvm.compiler.nodes.type.StampTool;
  41 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
  42 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  43 
  44 import jdk.vm.ci.meta.Assumptions;
  45 import jdk.vm.ci.meta.ConstantReflectionProvider;
  46 import jdk.vm.ci.meta.JavaConstant;
  47 import jdk.vm.ci.meta.JavaKind;
  48 import jdk.vm.ci.meta.MetaAccessProvider;
  49 import jdk.vm.ci.meta.ResolvedJavaType;
  50 
  51 /**
  52  * The {@code LoadIndexedNode} represents a read from an element of an array.
  53  */
  54 @NodeInfo(cycles = CYCLES_8, size = SIZE_8)
  55 public class LoadIndexedNode extends AccessIndexedNode implements Virtualizable, Canonicalizable {
  56 
  57     public static final NodeClass<LoadIndexedNode> TYPE = NodeClass.create(LoadIndexedNode.class);
  58 
  59     /**
  60      * Creates a new LoadIndexedNode.
  61      *
  62      * @param array the instruction producing the array
  63      * @param index the instruction producing the index
  64      * @param elementKind the element type
  65      */
  66     public LoadIndexedNode(Assumptions assumptions, ValueNode array, ValueNode index, JavaKind elementKind) {
  67         this(TYPE, createStamp(assumptions, array, elementKind), array, index, elementKind);
  68     }
  69 
  70     public static ValueNode create(Assumptions assumptions, ValueNode array, ValueNode index, JavaKind elementKind, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
  71         ValueNode constant = tryConstantFold(array, index, metaAccess, constantReflection);
  72         if (constant != null) {
  73             return constant;
  74         }
  75         return new LoadIndexedNode(assumptions, array, index, elementKind);
  76     }
  77 
  78     protected LoadIndexedNode(NodeClass<? extends LoadIndexedNode> c, Stamp stamp, ValueNode array, ValueNode index, JavaKind elementKind) {
  79         super(c, stamp, array, index, elementKind);
  80     }
  81 
  82     private static Stamp createStamp(Assumptions assumptions, ValueNode array, JavaKind kind) {
  83         ResolvedJavaType type = StampTool.typeOrNull(array);
  84         if (kind == JavaKind.Object && type != null && type.isArray()) {
  85             return StampFactory.object(TypeReference.createTrusted(assumptions, type.getComponentType()));
  86         } else {
  87             return StampFactory.forKind(kind);
  88         }
  89     }
  90 
  91     @Override
  92     public boolean inferStamp() {
  93         return updateStamp(stamp.improveWith(createStamp(graph().getAssumptions(), array(), elementKind())));
  94     }
  95 
  96     @Override
  97     public void virtualize(VirtualizerTool tool) {
  98         ValueNode alias = tool.getAlias(array());
  99         if (alias instanceof VirtualObjectNode) {
 100             VirtualArrayNode virtual = (VirtualArrayNode) alias;
 101             ValueNode indexValue = tool.getAlias(index());
 102             int idx = indexValue.isConstant() ? indexValue.asJavaConstant().asInt() : -1;
 103             if (idx >= 0 && idx < virtual.entryCount()) {
 104                 ValueNode entry = tool.getEntry(virtual, idx);
 105                 if (stamp.isCompatible(entry.stamp())) {
 106                     tool.replaceWith(entry);
 107                 } else {
 108                     assert stamp().getStackKind() == JavaKind.Int && (entry.stamp().getStackKind() == JavaKind.Long || entry.getStackKind() == JavaKind.Double ||
 109                                     entry.getStackKind() == JavaKind.Illegal) : "Can only allow different stack kind two slot marker writes on one stot fields.";
 110                 }
 111             }
 112         }
 113     }
 114 
 115     @Override
 116     public Node canonical(CanonicalizerTool tool) {
 117         ValueNode constant = tryConstantFold(array(), index(), tool.getMetaAccess(), tool.getConstantReflection());
 118         if (constant != null) {
 119             return constant;
 120         }
 121         return this;
 122     }
 123 
 124     private static ValueNode tryConstantFold(ValueNode array, ValueNode index, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
 125         if (array.isConstant() && !array.isNullConstant() && index.isConstant()) {
 126             JavaConstant arrayConstant = array.asJavaConstant();
 127             if (arrayConstant != null) {
 128                 int stableDimension = ((ConstantNode) array).getStableDimension();
 129                 if (stableDimension > 0) {
 130                     JavaConstant constant = constantReflection.readArrayElement(arrayConstant, index.asJavaConstant().asInt());
 131                     boolean isDefaultStable = ((ConstantNode) array).isDefaultStable();
 132                     if (constant != null && (isDefaultStable || !constant.isDefaultForKind())) {
 133                         return ConstantNode.forConstant(constant, stableDimension - 1, isDefaultStable, metaAccess);
 134                     }
 135                 }
 136             }
 137         }
 138         return null;
 139     }
 140 }