001/*
002 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.java;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.spi.*;
033import com.oracle.graal.nodes.type.*;
034import com.oracle.graal.nodes.virtual.*;
035
036/**
037 * The {@code LoadIndexedNode} represents a read from an element of an array.
038 */
039@NodeInfo
040public class LoadIndexedNode extends AccessIndexedNode implements Virtualizable, Canonicalizable {
041
042    public static final NodeClass<LoadIndexedNode> TYPE = NodeClass.create(LoadIndexedNode.class);
043
044    /**
045     * Creates a new LoadIndexedNode.
046     *
047     * @param array the instruction producing the array
048     * @param index the instruction producing the index
049     * @param elementKind the element type
050     */
051    public LoadIndexedNode(ValueNode array, ValueNode index, Kind elementKind) {
052        this(TYPE, createStamp(array, elementKind), array, index, elementKind);
053    }
054
055    public static ValueNode create(ValueNode array, ValueNode index, Kind elementKind, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
056        ValueNode constant = tryConstantFold(array, index, metaAccess, constantReflection);
057        if (constant != null) {
058            return constant;
059        }
060        return new LoadIndexedNode(array, index, elementKind);
061    }
062
063    protected LoadIndexedNode(NodeClass<? extends LoadIndexedNode> c, Stamp stamp, ValueNode array, ValueNode index, Kind elementKind) {
064        super(c, stamp, array, index, elementKind);
065    }
066
067    private static Stamp createStamp(ValueNode array, Kind kind) {
068        ResolvedJavaType type = StampTool.typeOrNull(array);
069        if (kind == Kind.Object && type != null) {
070            return StampFactory.declaredTrusted(type.getComponentType());
071        } else {
072            return StampFactory.forKind(kind);
073        }
074    }
075
076    @Override
077    public boolean inferStamp() {
078        return updateStamp(createStamp(array(), elementKind()));
079    }
080
081    @Override
082    public void virtualize(VirtualizerTool tool) {
083        ValueNode alias = tool.getAlias(array());
084        if (alias instanceof VirtualObjectNode) {
085            VirtualArrayNode virtual = (VirtualArrayNode) alias;
086            ValueNode indexValue = tool.getAlias(index());
087            int idx = indexValue.isConstant() ? indexValue.asJavaConstant().asInt() : -1;
088            if (idx >= 0 && idx < virtual.entryCount()) {
089                tool.replaceWith(tool.getEntry(virtual, idx));
090            }
091        }
092    }
093
094    public Node canonical(CanonicalizerTool tool) {
095        ValueNode constant = tryConstantFold(array(), index(), tool.getMetaAccess(), tool.getConstantReflection());
096        if (constant != null) {
097            return constant;
098        }
099        return this;
100    }
101
102    private static ValueNode tryConstantFold(ValueNode array, ValueNode index, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
103        if (array.isConstant() && !array.isNullConstant() && index.isConstant()) {
104            JavaConstant arrayConstant = array.asJavaConstant();
105            if (arrayConstant != null) {
106                JavaConstant constant = constantReflection.readConstantArrayElement(arrayConstant, index.asJavaConstant().asInt());
107                if (constant != null) {
108                    return ConstantNode.forConstant(constant, metaAccess);
109                }
110            }
111        }
112        return null;
113    }
114}