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_2; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1; 29 30 import org.graalvm.compiler.core.common.type.StampFactory; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.graph.spi.Canonicalizable; 33 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.ConstantNode; 36 import org.graalvm.compiler.nodes.FixedWithNextNode; 37 import org.graalvm.compiler.nodes.ValueNode; 38 import org.graalvm.compiler.nodes.spi.ArrayLengthProvider; 39 import org.graalvm.compiler.nodes.spi.Lowerable; 40 import org.graalvm.compiler.nodes.spi.LoweringTool; 41 import org.graalvm.compiler.nodes.spi.Virtualizable; 42 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 43 import org.graalvm.compiler.nodes.util.GraphUtil; 44 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode; 45 46 import jdk.vm.ci.meta.ConstantReflectionProvider; 47 48 /** 49 * The {@code ArrayLength} instruction gets the length of an array. 50 */ 51 @NodeInfo(cycles = CYCLES_2, size = SIZE_1) 52 public final class ArrayLengthNode extends FixedWithNextNode implements Canonicalizable.Unary<ValueNode>, Lowerable, Virtualizable { 53 54 public static final NodeClass<ArrayLengthNode> TYPE = NodeClass.create(ArrayLengthNode.class); 55 @Input ValueNode array; 56 57 public ValueNode array() { 58 return array; 59 } 60 61 @Override 62 public ValueNode getValue() { 63 return array; 64 } 65 66 public ArrayLengthNode(ValueNode array) { 67 super(TYPE, StampFactory.positiveInt()); 68 this.array = array; 69 } 70 71 public static ValueNode create(ValueNode forValue, ConstantReflectionProvider constantReflection) { 72 if (forValue instanceof NewArrayNode) { 73 NewArrayNode newArray = (NewArrayNode) forValue; 74 return newArray.length(); 75 } 76 77 ValueNode length = readArrayLength(forValue, constantReflection); 78 if (length != null) { 79 return length; 80 } 81 return new ArrayLengthNode(forValue); 82 } 83 84 @Override 85 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 86 ValueNode length = readArrayLength(forValue, tool.getConstantReflection()); 87 if (length != null) { 88 return length; 89 } 90 return this; 91 } 92 93 /** 94 * Gets the length of an array if possible. 95 * 96 * @return a node representing the length of {@code array} or null if it is not available 97 */ 98 public static ValueNode readArrayLength(ValueNode originalArray, ConstantReflectionProvider constantReflection) { 99 return GraphUtil.arrayLength(originalArray, ArrayLengthProvider.FindLengthMode.CANONICALIZE_READ, constantReflection); 100 } 101 102 @Override 103 public void lower(LoweringTool tool) { 104 tool.getLowerer().lower(this, tool); 105 } | 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_2; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1; 29 30 import org.graalvm.compiler.core.common.type.StampFactory; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.graph.spi.Canonicalizable; 33 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.ConstantNode; 36 import org.graalvm.compiler.nodes.DeoptimizeNode; 37 import org.graalvm.compiler.nodes.FixedWithNextNode; 38 import org.graalvm.compiler.nodes.ValueNode; 39 import org.graalvm.compiler.nodes.spi.ArrayLengthProvider; 40 import org.graalvm.compiler.nodes.spi.Lowerable; 41 import org.graalvm.compiler.nodes.spi.LoweringTool; 42 import org.graalvm.compiler.nodes.spi.Virtualizable; 43 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 44 import org.graalvm.compiler.nodes.util.GraphUtil; 45 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode; 46 47 import jdk.vm.ci.meta.ConstantReflectionProvider; 48 import jdk.vm.ci.meta.DeoptimizationAction; 49 import jdk.vm.ci.meta.DeoptimizationReason; 50 51 /** 52 * The {@code ArrayLength} instruction gets the length of an array. 53 */ 54 @NodeInfo(cycles = CYCLES_2, size = SIZE_1) 55 public final class ArrayLengthNode extends FixedWithNextNode implements Canonicalizable.Unary<ValueNode>, Lowerable, Virtualizable { 56 57 public static final NodeClass<ArrayLengthNode> TYPE = NodeClass.create(ArrayLengthNode.class); 58 @Input ValueNode array; 59 60 public ValueNode array() { 61 return array; 62 } 63 64 @Override 65 public ValueNode getValue() { 66 return array; 67 } 68 69 public ArrayLengthNode(ValueNode array) { 70 super(TYPE, StampFactory.positiveInt()); 71 this.array = array; 72 } 73 74 public static ValueNode create(ValueNode forValue, ConstantReflectionProvider constantReflection) { 75 if (forValue instanceof NewArrayNode) { 76 NewArrayNode newArray = (NewArrayNode) forValue; 77 return newArray.length(); 78 } 79 80 ValueNode length = readArrayLength(forValue, constantReflection); 81 if (length != null) { 82 return length; 83 } 84 return new ArrayLengthNode(forValue); 85 } 86 87 @Override 88 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 89 if (forValue.isNullConstant()) { 90 return new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException); 91 } 92 ValueNode length = readArrayLength(forValue, tool.getConstantReflection()); 93 if (length != null) { 94 return length; 95 } 96 return this; 97 } 98 99 /** 100 * Gets the length of an array if possible. 101 * 102 * @return a node representing the length of {@code array} or null if it is not available 103 */ 104 public static ValueNode readArrayLength(ValueNode originalArray, ConstantReflectionProvider constantReflection) { 105 return GraphUtil.arrayLength(originalArray, ArrayLengthProvider.FindLengthMode.CANONICALIZE_READ, constantReflection); 106 } 107 108 @Override 109 public void lower(LoweringTool tool) { 110 tool.getLowerer().lower(this, tool); 111 } |