001/*
002 * Copyright (c) 2014, 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.hotspot.replacements;
024
025import jdk.internal.jvmci.hotspot.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.hotspot.word.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.calc.*;
035import com.oracle.graal.nodes.spi.*;
036
037/**
038 * Read {@code Klass::_java_mirror} and incorporate non-null type information into stamp. This is
039 * also used by {@link ClassGetHubNode} to eliminate chains of {@code klass._java_mirror._klass}.
040 */
041@NodeInfo
042public final class HubGetClassNode extends FloatingGuardedNode implements Lowerable, Canonicalizable, ConvertNode {
043    public static final NodeClass<HubGetClassNode> TYPE = NodeClass.create(HubGetClassNode.class);
044    @Input protected ValueNode hub;
045
046    public HubGetClassNode(@InjectedNodeParameter MetaAccessProvider metaAccess, ValueNode hub) {
047        super(TYPE, StampFactory.declaredNonNull(metaAccess.lookupJavaType(Class.class)), null);
048        this.hub = hub;
049    }
050
051    public ValueNode getHub() {
052        return hub;
053    }
054
055    @Override
056    public Node canonical(CanonicalizerTool tool) {
057        if (tool.allUsagesAvailable() && hasNoUsages()) {
058            return null;
059        } else {
060            MetaAccessProvider metaAccess = tool.getMetaAccess();
061            if (metaAccess != null) {
062                if (hub.isConstant()) {
063                    ResolvedJavaType exactType = tool.getConstantReflection().asJavaType(hub.asJavaConstant());
064                    return ConstantNode.forConstant(exactType.getJavaClass(), metaAccess);
065                }
066            }
067            return this;
068        }
069    }
070
071    @Override
072    public void lower(LoweringTool tool) {
073        tool.getLowerer().lower(this, tool);
074    }
075
076    @NodeIntrinsic
077    public static native Class<?> readClass(KlassPointer hub);
078
079    @Override
080    public ValueNode getValue() {
081        return hub;
082    }
083
084    @Override
085    public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
086        if (JavaConstant.NULL_POINTER.equals(c)) {
087            return c;
088        }
089        return constantReflection.asJavaType(c).getJavaClass();
090    }
091
092    @Override
093    public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
094        if (JavaConstant.NULL_POINTER.equals(c)) {
095            return c;
096        }
097        ResolvedJavaType type = constantReflection.asJavaType(c);
098        if (type instanceof HotSpotResolvedObjectType) {
099            return ((HotSpotResolvedObjectType) type).getObjectHub();
100        } else {
101            assert type instanceof HotSpotResolvedPrimitiveType;
102            return JavaConstant.NULL_POINTER;
103        }
104    }
105
106    @Override
107    public boolean isLossless() {
108        /*
109         * Any concrete Klass* has a corresponding java.lang.Class
110         */
111        return true;
112    }
113}