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.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     }
 112 
 113     @NodeIntrinsic
 114     public static native int arrayLength(Object array);
 115 
 116     @Override
 117     public void virtualize(VirtualizerTool tool) {
 118         ValueNode alias = tool.getAlias(array());
 119         if (alias instanceof VirtualArrayNode) {
 120             VirtualArrayNode virtualArray = (VirtualArrayNode) alias;
 121             tool.replaceWithValue(ConstantNode.forInt(virtualArray.entryCount(), graph()));
 122         }
 123     }
 124 }