1 /* 2 * Copyright (c) 2009, 2018, 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.nodes.java; 26 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8; 29 30 import org.graalvm.compiler.core.common.calc.CanonicalCondition; 31 import org.graalvm.compiler.core.common.type.ObjectStamp; 32 import org.graalvm.compiler.core.common.type.Stamp; 33 import org.graalvm.compiler.core.common.type.StampFactory; 34 import org.graalvm.compiler.core.common.type.TypeReference; 35 import org.graalvm.compiler.graph.Node; 36 import org.graalvm.compiler.graph.NodeClass; 37 import org.graalvm.compiler.graph.spi.Canonicalizable; 38 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 39 import org.graalvm.compiler.graph.spi.Simplifiable; 40 import org.graalvm.compiler.graph.spi.SimplifierTool; 41 import org.graalvm.compiler.nodeinfo.NodeInfo; 42 import org.graalvm.compiler.nodes.ConstantNode; 43 import org.graalvm.compiler.nodes.DeoptimizeNode; 44 import org.graalvm.compiler.nodes.FixedGuardNode; 45 import org.graalvm.compiler.nodes.FixedWithNextNode; 46 import org.graalvm.compiler.nodes.LogicNode; 47 import org.graalvm.compiler.nodes.NodeView; 48 import org.graalvm.compiler.nodes.ValueNode; 49 import org.graalvm.compiler.nodes.calc.CompareNode; 50 import org.graalvm.compiler.nodes.extended.GuardingNode; 51 import org.graalvm.compiler.nodes.spi.Virtualizable; 52 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 53 import org.graalvm.compiler.nodes.type.StampTool; 54 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode; 55 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 56 57 import jdk.vm.ci.meta.Assumptions; 58 import jdk.vm.ci.meta.ConstantReflectionProvider; 59 import jdk.vm.ci.meta.DeoptimizationAction; 60 import jdk.vm.ci.meta.DeoptimizationReason; 61 import jdk.vm.ci.meta.JavaConstant; 62 import jdk.vm.ci.meta.JavaKind; 63 import jdk.vm.ci.meta.MetaAccessProvider; 64 import jdk.vm.ci.meta.ResolvedJavaType; 65 66 /** 67 * The {@code LoadIndexedNode} represents a read from an element of an array. 68 */ 69 @NodeInfo(cycles = CYCLES_8, size = SIZE_8) 70 public class LoadIndexedNode extends AccessIndexedNode implements Virtualizable, Canonicalizable, Simplifiable { 71 72 public static final NodeClass<LoadIndexedNode> TYPE = NodeClass.create(LoadIndexedNode.class); 73 74 /** 75 * Creates a new LoadIndexedNode. 76 * 77 * @param array the instruction producing the array 78 * @param index the instruction producing the index 79 * @param elementKind the element type 80 */ 81 public LoadIndexedNode(Assumptions assumptions, ValueNode array, ValueNode index, GuardingNode boundsCheck, JavaKind elementKind) { 82 this(TYPE, createStamp(assumptions, array, elementKind), array, index, boundsCheck, elementKind); 83 } 84 85 public static ValueNode create(Assumptions assumptions, ValueNode array, ValueNode index, GuardingNode boundsCheck, JavaKind elementKind, MetaAccessProvider metaAccess, 86 ConstantReflectionProvider constantReflection) { 87 ValueNode constant = tryConstantFold(array, index, metaAccess, constantReflection); 88 if (constant != null) { 89 return constant; 90 } 91 return new LoadIndexedNode(assumptions, array, index, boundsCheck, elementKind); 92 } 93 94 protected LoadIndexedNode(NodeClass<? extends LoadIndexedNode> c, Stamp stamp, ValueNode array, ValueNode index, GuardingNode boundsCheck, JavaKind elementKind) { 95 super(c, stamp, array, index, boundsCheck, elementKind); 96 } 97 98 private static Stamp createStamp(Assumptions assumptions, ValueNode array, JavaKind kind) { 99 ResolvedJavaType type = StampTool.typeOrNull(array); 100 if (kind == JavaKind.Object && type != null && type.isArray()) { 101 return StampFactory.object(TypeReference.createTrusted(assumptions, type.getComponentType())); 102 } else { 103 JavaKind preciseKind = determinePreciseArrayElementType(array, kind); 104 return StampFactory.forKind(preciseKind); 105 } 106 } 107 108 private static JavaKind determinePreciseArrayElementType(ValueNode array, JavaKind kind) { 109 if (kind == JavaKind.Byte) { 110 ResolvedJavaType javaType = ((ObjectStamp) array.stamp(NodeView.DEFAULT)).type(); 111 if (javaType != null && javaType.isArray() && javaType.getComponentType() != null && javaType.getComponentType().getJavaKind() == JavaKind.Boolean) { 112 return JavaKind.Boolean; 113 } 114 } 115 return kind; 116 } 117 118 @Override 119 public boolean inferStamp() { 120 return updateStamp(stamp.improveWith(createStamp(graph().getAssumptions(), array(), elementKind()))); 121 } 122 123 @Override 124 public void virtualize(VirtualizerTool tool) { 125 ValueNode alias = tool.getAlias(array()); 126 if (alias instanceof VirtualObjectNode) { 127 VirtualArrayNode virtual = (VirtualArrayNode) alias; 128 ValueNode indexValue = tool.getAlias(index()); 129 int idx = indexValue.isConstant() ? indexValue.asJavaConstant().asInt() : -1; 130 if (idx >= 0 && idx < virtual.entryCount()) { 131 ValueNode entry = tool.getEntry(virtual, idx); 132 if (stamp.isCompatible(entry.stamp(NodeView.DEFAULT))) { 133 tool.replaceWith(entry); 134 } else { 135 assert stamp(NodeView.DEFAULT).getStackKind() == JavaKind.Int && (entry.stamp(NodeView.DEFAULT).getStackKind() == JavaKind.Long || entry.getStackKind() == JavaKind.Double || 136 entry.getStackKind() == JavaKind.Illegal) : "Can only allow different stack kind two slot marker writes on one stot fields."; 137 } 138 } 139 } 140 } 141 142 @Override 143 public Node canonical(CanonicalizerTool tool) { 144 if (array().isNullConstant()) { 145 return new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException); 146 } 147 ValueNode constant = tryConstantFold(array(), index(), tool.getMetaAccess(), tool.getConstantReflection()); 148 if (constant != null) { 149 return constant; 150 } 151 return this; 152 } 153 154 @Override 155 public void simplify(SimplifierTool tool) { 156 if (tool.allUsagesAvailable() && hasNoUsages()) { 157 NodeView view = NodeView.from(tool); 158 ValueNode arrayLength = ArrayLengthNode.create(array, tool.getConstantReflection()); 159 LogicNode boundsCheck = CompareNode.createCompareNode(CanonicalCondition.BT, index, arrayLength, tool.getConstantReflection(), view); 160 if (boundsCheck.isTautology()) { 161 return; 162 } 163 if (graph().getGuardsStage().allowsGuardInsertion()) { 164 if (!arrayLength.isAlive()) { 165 arrayLength = graph().addOrUniqueWithInputs(arrayLength); 166 if (arrayLength instanceof FixedWithNextNode) { 167 FixedWithNextNode fixedArrayLength = (FixedWithNextNode) arrayLength; 168 graph().addBeforeFixed(this, fixedArrayLength); 169 } 170 } 171 boundsCheck = graph().addOrUniqueWithInputs(boundsCheck); 172 FixedGuardNode fixedGuard = new FixedGuardNode(boundsCheck, DeoptimizationReason.BoundsCheckException, DeoptimizationAction.InvalidateReprofile, false, getNodeSourcePosition()); 173 graph().replaceFixedWithFixed(this, graph().add(fixedGuard)); 174 } 175 } 176 } 177 178 private static ValueNode tryConstantFold(ValueNode array, ValueNode index, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) { 179 if (array.isConstant() && !array.isNullConstant() && index.isConstant()) { 180 JavaConstant arrayConstant = array.asJavaConstant(); 181 if (arrayConstant != null) { 182 int stableDimension = ((ConstantNode) array).getStableDimension(); 183 if (stableDimension > 0) { 184 JavaConstant constant = constantReflection.readArrayElement(arrayConstant, index.asJavaConstant().asInt()); 185 boolean isDefaultStable = ((ConstantNode) array).isDefaultStable(); 186 if (constant != null && (isDefaultStable || !constant.isDefaultForKind())) { 187 return ConstantNode.forConstant(constant, stableDimension - 1, isDefaultStable, metaAccess); 188 } 189 } 190 } 191 } 192 return null; 193 } 194 }