001/*
002 * Copyright (c) 2013, 2014, 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.lang.reflect.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034
035@NodeInfo
036public class DynamicNewInstanceNode extends AbstractNewObjectNode implements Canonicalizable {
037    public static final NodeClass<DynamicNewInstanceNode> TYPE = NodeClass.create(DynamicNewInstanceNode.class);
038
039    @Input ValueNode clazz;
040
041    public DynamicNewInstanceNode(ValueNode clazz, boolean fillContents) {
042        this(TYPE, clazz, fillContents, null);
043    }
044
045    protected DynamicNewInstanceNode(NodeClass<? extends DynamicNewInstanceNode> c, ValueNode clazz, boolean fillContents, FrameState stateBefore) {
046        super(c, StampFactory.objectNonNull(), fillContents, stateBefore);
047        this.clazz = clazz;
048    }
049
050    public ValueNode getInstanceType() {
051        return clazz;
052    }
053
054    @Override
055    public void simplify(SimplifierTool tool) {
056        /*
057         * Do not call the super implementation: we must not eliminate unused allocations because
058         * throwing an InstantiationException is a possible side effect of an unused allocation.
059         */
060    }
061
062    @Override
063    public Node canonical(CanonicalizerTool tool) {
064        if (clazz.isConstant()) {
065            ResolvedJavaType type = tool.getConstantReflection().asJavaType(clazz.asConstant());
066            if (type != null && type.isInitialized() && !throwsInstantiationException(type, tool.getMetaAccess())) {
067                return createNewInstanceNode(type);
068            }
069        }
070        return this;
071    }
072
073    /** Hook for subclasses to instantiate a subclass of {@link NewInstanceNode}. */
074    protected NewInstanceNode createNewInstanceNode(ResolvedJavaType type) {
075        return new NewInstanceNode(type, fillContents(), stateBefore());
076    }
077
078    public static boolean throwsInstantiationException(Class<?> type) {
079        return type.isPrimitive() || type.isArray() || type.isInterface() || Modifier.isAbstract(type.getModifiers()) || type == Class.class;
080    }
081
082    public static boolean throwsInstantiationException(ResolvedJavaType type, MetaAccessProvider metaAccess) {
083        return type.isPrimitive() || type.isArray() || type.isInterface() || Modifier.isAbstract(type.getModifiers()) || type.equals(metaAccess.lookupJavaType(Class.class));
084    }
085}