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 java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.spi.*;
034import com.oracle.graal.nodes.type.*;
035import com.oracle.graal.nodes.virtual.*;
036
037/**
038 * The {@code NewArrayNode} is used for all array allocations where the element type is know at
039 * compile time.
040 */
041// JaCoCo Exclude
042@NodeInfo
043public class NewArrayNode extends AbstractNewArrayNode implements VirtualizableAllocation {
044
045    public static final NodeClass<NewArrayNode> TYPE = NodeClass.create(NewArrayNode.class);
046
047    public NewArrayNode(ResolvedJavaType elementType, ValueNode length, boolean fillContents) {
048        this(elementType, length, fillContents, null);
049    }
050
051    public NewArrayNode(ResolvedJavaType elementType, ValueNode length, boolean fillContents, FrameState stateBefore) {
052        this(TYPE, elementType, length, fillContents, stateBefore);
053    }
054
055    protected NewArrayNode(NodeClass<? extends NewArrayNode> c, ResolvedJavaType elementType, ValueNode length, boolean fillContents, FrameState stateBefore) {
056        super(c, StampFactory.exactNonNull(elementType.getArrayClass()), length, fillContents, stateBefore);
057    }
058
059    @NodeIntrinsic
060    private static native Object newArray(@ConstantNodeParameter Class<?> elementType, int length, @ConstantNodeParameter boolean fillContents);
061
062    public static Object newUninitializedArray(Class<?> elementType, int length) {
063        return newArray(elementType, length, false);
064    }
065
066    /**
067     * Gets the element type of the array.
068     *
069     * @return the element type of the array
070     */
071    public ResolvedJavaType elementType() {
072        return StampTool.typeOrNull(this).getComponentType();
073    }
074
075    @Override
076    public void virtualize(VirtualizerTool tool) {
077        ValueNode lengthAlias = tool.getAlias(length());
078        if (lengthAlias.asConstant() != null) {
079            int constantLength = lengthAlias.asJavaConstant().asInt();
080            if (constantLength >= 0 && constantLength < tool.getMaximumEntryCount()) {
081                ValueNode[] state = new ValueNode[constantLength];
082                ConstantNode defaultForKind = constantLength == 0 ? null : defaultElementValue();
083                for (int i = 0; i < constantLength; i++) {
084                    state[i] = defaultForKind;
085                }
086                VirtualObjectNode virtualObject = createVirtualArrayNode(constantLength);
087                tool.createVirtualObject(virtualObject, state, Collections.<MonitorIdNode> emptyList(), false);
088                tool.replaceWithVirtual(virtualObject);
089            }
090        }
091    }
092
093    protected VirtualArrayNode createVirtualArrayNode(int constantLength) {
094        return new VirtualArrayNode(elementType(), constantLength);
095    }
096
097    /* Factored out in a separate method so that subclasses can override it. */
098    protected ConstantNode defaultElementValue() {
099        return ConstantNode.defaultForKind(elementType().getKind(), graph());
100    }
101}