LoadHubNode.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.nodes.extended;
24
25import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
26
27import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
28import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
29
30import org.graalvm.compiler.core.common.type.ObjectStamp;
31import org.graalvm.compiler.core.common.type.Stamp;
32import org.graalvm.compiler.core.common.type.TypeReference;
33import org.graalvm.compiler.graph.NodeClass;
34import org.graalvm.compiler.graph.spi.Canonicalizable;
35import org.graalvm.compiler.graph.spi.CanonicalizerTool;
36import org.graalvm.compiler.nodeinfo.NodeInfo;
37import org.graalvm.compiler.nodes.ConstantNode;
38import org.graalvm.compiler.nodes.ValueNode;
39import org.graalvm.compiler.nodes.calc.FloatingNode;
40import org.graalvm.compiler.nodes.spi.Lowerable;
41import org.graalvm.compiler.nodes.spi.LoweringTool;
42import org.graalvm.compiler.nodes.spi.StampProvider;
43import org.graalvm.compiler.nodes.spi.Virtualizable;
44import org.graalvm.compiler.nodes.spi.VirtualizerTool;
45import org.graalvm.compiler.nodes.type.StampTool;
46
47import jdk.vm.ci.meta.ConstantReflectionProvider;
48import jdk.vm.ci.meta.MetaAccessProvider;
49
50/**
51 * Loads an object's hub. The object is not null-checked by this operation.
52 */
53@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
54public final class LoadHubNode extends FloatingNode implements Lowerable, Canonicalizable, Virtualizable {
55
56    public static final NodeClass<LoadHubNode> TYPE = NodeClass.create(LoadHubNode.class);
57    @Input ValueNode value;
58
59    public ValueNode getValue() {
60        return value;
61    }
62
63    private static Stamp hubStamp(StampProvider stampProvider, ValueNode value) {
64        assert value.stamp() instanceof ObjectStamp;
65        return stampProvider.createHubStamp(((ObjectStamp) value.stamp()));
66    }
67
68    public static ValueNode create(ValueNode value, StampProvider stampProvider, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
69        Stamp stamp = hubStamp(stampProvider, value);
70        ValueNode synonym = findSynonym(value, stamp, metaAccess, constantReflection);
71        if (synonym != null) {
72            return synonym;
73        }
74        return new LoadHubNode(stamp, value);
75    }
76
77    public LoadHubNode(@InjectedNodeParameter StampProvider stampProvider, ValueNode value) {
78        this(hubStamp(stampProvider, value), value);
79    }
80
81    public LoadHubNode(Stamp stamp, ValueNode value) {
82        super(TYPE, stamp);
83        this.value = value;
84    }
85
86    @Override
87    public void lower(LoweringTool tool) {
88        tool.getLowerer().lower(this, tool);
89    }
90
91    @Override
92    public ValueNode canonical(CanonicalizerTool tool) {
93        if (!GeneratePIC.getValue(tool.getOptions())) {
94            MetaAccessProvider metaAccess = tool.getMetaAccess();
95            ValueNode curValue = getValue();
96            ValueNode newNode = findSynonym(curValue, stamp(), metaAccess, tool.getConstantReflection());
97            if (newNode != null) {
98                return newNode;
99            }
100        }
101        return this;
102    }
103
104    public static ValueNode findSynonym(ValueNode curValue, Stamp stamp, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
105        if (!GeneratePIC.getValue(curValue.getOptions())) {
106            TypeReference type = StampTool.typeReferenceOrNull(curValue);
107            if (type != null && type.isExact()) {
108                return ConstantNode.forConstant(stamp, constantReflection.asObjectHub(type.getType()), metaAccess);
109            }
110        }
111        return null;
112    }
113
114    @Override
115    public void virtualize(VirtualizerTool tool) {
116        if (!GeneratePIC.getValue(tool.getOptions())) {
117            ValueNode alias = tool.getAlias(getValue());
118            TypeReference type = StampTool.typeReferenceOrNull(alias);
119            if (type != null && type.isExact()) {
120                tool.replaceWithValue(ConstantNode.forConstant(stamp(), tool.getConstantReflectionProvider().asObjectHub(type.getType()), tool.getMetaAccessProvider(), graph()));
121            }
122        }
123    }
124}
125