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.Assumptions.AssumptionResult;
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.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.spi.*;
034
035/**
036 * Loads an object's hub. The object is not null-checked by this operation.
037 */
038@NodeInfo
039public final class LoadHubNode extends FloatingGuardedNode implements Lowerable, Canonicalizable, Virtualizable {
040
041    public static final NodeClass<LoadHubNode> TYPE = NodeClass.create(LoadHubNode.class);
042    @Input ValueNode value;
043
044    public ValueNode getValue() {
045        return value;
046    }
047
048    private static Stamp hubStamp(StampProvider stampProvider, ValueNode value) {
049        assert value.stamp() instanceof ObjectStamp;
050        return stampProvider.createHubStamp(((ObjectStamp) value.stamp()));
051    }
052
053    public static ValueNode create(ValueNode value, StampProvider stampProvider, MetaAccessProvider metaAccess) {
054        Stamp stamp = hubStamp(stampProvider, value);
055        ValueNode synonym = findSynonym(value, stamp, null, metaAccess);
056        if (synonym != null) {
057            return synonym;
058        }
059        return new LoadHubNode(stamp, value, null);
060    }
061
062    public LoadHubNode(@InjectedNodeParameter StampProvider stampProvider, ValueNode value) {
063        this(stampProvider, value, null);
064    }
065
066    public LoadHubNode(@InjectedNodeParameter StampProvider stampProvider, ValueNode value, ValueNode guard) {
067        this(hubStamp(stampProvider, value), value, guard);
068    }
069
070    private LoadHubNode(Stamp stamp, ValueNode value, ValueNode guard) {
071        super(TYPE, stamp, (GuardingNode) guard);
072        assert value != guard;
073        this.value = value;
074    }
075
076    @Override
077    public void lower(LoweringTool tool) {
078        tool.getLowerer().lower(this, tool);
079    }
080
081    @Override
082    public ValueNode canonical(CanonicalizerTool tool) {
083        MetaAccessProvider metaAccess = tool.getMetaAccess();
084        ValueNode curValue = getValue();
085        ValueNode newNode = findSynonym(curValue, stamp(), graph(), metaAccess);
086        if (newNode != null) {
087            return newNode;
088        }
089        return this;
090    }
091
092    public static ValueNode findSynonym(ValueNode curValue, Stamp stamp, StructuredGraph graph, MetaAccessProvider metaAccess) {
093        ResolvedJavaType exactType = findSynonymType(graph, metaAccess, curValue);
094        if (exactType != null) {
095            return ConstantNode.forConstant(stamp, exactType.getObjectHub(), metaAccess);
096        }
097        return null;
098    }
099
100    public static ResolvedJavaType findSynonymType(StructuredGraph graph, MetaAccessProvider metaAccess, ValueNode curValue) {
101        ResolvedJavaType exactType = null;
102        if (metaAccess != null && curValue.stamp() instanceof ObjectStamp) {
103            ObjectStamp objectStamp = (ObjectStamp) curValue.stamp();
104            if (objectStamp.isExactType()) {
105                exactType = objectStamp.type();
106            } else if (objectStamp.type() != null && graph != null && graph.getAssumptions() != null) {
107                AssumptionResult<ResolvedJavaType> leafConcreteSubtype = objectStamp.type().findLeafConcreteSubtype();
108                if (leafConcreteSubtype != null) {
109                    exactType = leafConcreteSubtype.getResult();
110                    graph.getAssumptions().record(leafConcreteSubtype);
111                }
112            }
113        }
114        return exactType;
115    }
116
117    @Override
118    public void virtualize(VirtualizerTool tool) {
119        ValueNode alias = tool.getAlias(getValue());
120        ResolvedJavaType type = findSynonymType(graph(), tool.getMetaAccessProvider(), alias);
121        if (type != null) {
122            tool.replaceWithValue(ConstantNode.forConstant(stamp(), type.getObjectHub(), tool.getMetaAccessProvider(), graph()));
123        }
124    }
125}