001/*
002 * Copyright (c) 2011, 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.extended;
024
025import jdk.internal.jvmci.meta.*;
026import jdk.internal.jvmci.meta.Assumptions.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.calc.*;
034import com.oracle.graal.nodes.spi.*;
035import com.oracle.graal.nodes.virtual.*;
036
037/**
038 * Loads an object's class (i.e., this node can be created for {@code object.getClass()}).
039 */
040@NodeInfo
041public final class GetClassNode extends FloatingNode implements Lowerable, Canonicalizable, Virtualizable {
042
043    public static final NodeClass<GetClassNode> TYPE = NodeClass.create(GetClassNode.class);
044    @Input ValueNode object;
045
046    public ValueNode getObject() {
047        return object;
048    }
049
050    public GetClassNode(Stamp stamp, ValueNode object) {
051        super(TYPE, stamp);
052        this.object = object;
053        assert ((ObjectStamp) object.stamp()).nonNull();
054    }
055
056    @Override
057    public void lower(LoweringTool tool) {
058        tool.getLowerer().lower(this, tool);
059    }
060
061    public static ValueNode tryFold(MetaAccessProvider metaAccess, ValueNode object) {
062        if (metaAccess != null && object != null && object.stamp() instanceof ObjectStamp) {
063            ObjectStamp objectStamp = (ObjectStamp) object.stamp();
064
065            ResolvedJavaType exactType = null;
066            if (objectStamp.isExactType()) {
067                exactType = objectStamp.type();
068            } else if (objectStamp.type() != null && object.graph().getAssumptions() != null) {
069                AssumptionResult<ResolvedJavaType> leafConcreteSubtype = objectStamp.type().findLeafConcreteSubtype();
070                if (leafConcreteSubtype != null) {
071                    exactType = leafConcreteSubtype.getResult();
072                    object.graph().getAssumptions().record(leafConcreteSubtype);
073                }
074            }
075
076            if (exactType != null) {
077                return ConstantNode.forConstant(exactType.getJavaClass(), metaAccess);
078            }
079        }
080        return null;
081    }
082
083    @Override
084    public ValueNode canonical(CanonicalizerTool tool) {
085        ValueNode folded = tryFold(tool.getMetaAccess(), getObject());
086        return folded == null ? this : folded;
087    }
088
089    @Override
090    public void virtualize(VirtualizerTool tool) {
091        ValueNode alias = tool.getAlias(getObject());
092        if (alias instanceof VirtualObjectNode) {
093            VirtualObjectNode virtual = (VirtualObjectNode) alias;
094            Constant javaClass = virtual.type().getJavaClass();
095            tool.replaceWithValue(ConstantNode.forConstant(stamp(), javaClass, tool.getMetaAccessProvider(), graph()));
096        }
097    }
098}