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(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 tool.replaceWith(tool.getEntry(virtual, idx)); 105 } 106 } 107 } 108 109 @Override 110 public Node canonical(CanonicalizerTool tool) { 111 ValueNode constant = tryConstantFold(array(), index(), tool.getMetaAccess(), tool.getConstantReflection()); 112 if (constant != null) { 113 return constant; 114 } 115 return this; 116 } 117 118 private static ValueNode tryConstantFold(ValueNode array, ValueNode index, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) { 119 if (array.isConstant() && !array.isNullConstant() && index.isConstant()) { 120 JavaConstant arrayConstant = array.asJavaConstant(); 121 if (arrayConstant != null) { 122 int stableDimension = ((ConstantNode) array).getStableDimension(); 123 if (stableDimension > 0) { 124 JavaConstant constant = constantReflection.readArrayElement(arrayConstant, index.asJavaConstant().asInt()); 125 boolean isDefaultStable = ((ConstantNode) array).isDefaultStable(); 126 if (constant != null && (isDefaultStable || !constant.isDefaultForKind())) { 127 return ConstantNode.forConstant(constant, stableDimension - 1, isDefaultStable, metaAccess); 128 } 129 } 130 } 131 } 132 return null; 133 } 134 }