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