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_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     }
 106 
 107     @NodeIntrinsic
 108     public static native int arrayLength(Object array);
 109 
 110     @Override
 111     public void virtualize(VirtualizerTool tool) {
 112         ValueNode alias = tool.getAlias(array());
 113         if (alias instanceof VirtualArrayNode) {
 114             VirtualArrayNode virtualArray = (VirtualArrayNode) alias;
 115             tool.replaceWithValue(ConstantNode.forInt(virtualArray.entryCount(), graph()));
 116         }
 117     }
 118 }