1/*
2 * Copyright (c) 2013, 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;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
27
28import org.graalvm.compiler.graph.Node;
29import org.graalvm.compiler.graph.NodeClass;
30import org.graalvm.compiler.graph.spi.Canonicalizable;
31import org.graalvm.compiler.graph.spi.CanonicalizerTool;
32import org.graalvm.compiler.nodeinfo.NodeInfo;
33import org.graalvm.compiler.nodes.extended.GuardingNode;
34import org.graalvm.compiler.nodes.spi.LIRLowerable;
35import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
36import org.graalvm.compiler.nodes.spi.ValueProxy;
37import org.graalvm.compiler.nodes.spi.Virtualizable;
38import org.graalvm.compiler.nodes.spi.VirtualizerTool;
39import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
40
41import jdk.vm.ci.meta.JavaKind;
42
43/**
44 * A node that changes the type of its input, usually narrowing it. For example, a GuardedValueNode
45 * is used to keep the nodes depending on guards inside a loop during speculative guard movement.
46 *
47 * A GuardedValueNode will only go away if its guard is null or {@link StructuredGraph#start()}.
48 */
49@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
50public final class GuardedValueNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, Canonicalizable, ValueProxy {
51
52    public static final NodeClass<GuardedValueNode> TYPE = NodeClass.create(GuardedValueNode.class);
53    @Input ValueNode object;
54
55    public GuardedValueNode(ValueNode object, GuardingNode guard) {
56        super(TYPE, object.stamp(), guard);
57        this.object = object;
58    }
59
60    public ValueNode object() {
61        return object;
62    }
63
64    @Override
65    public void generate(NodeLIRBuilderTool generator) {
66        if (object.getStackKind() != JavaKind.Void && object.getStackKind() != JavaKind.Illegal) {
67            generator.setResult(this, generator.operand(object));
68        }
69    }
70
71    @Override
72    public boolean inferStamp() {
73        return updateStamp(object().stamp());
74    }
75
76    @Override
77    public void virtualize(VirtualizerTool tool) {
78        ValueNode alias = tool.getAlias(object());
79        if (alias instanceof VirtualObjectNode) {
80            tool.replaceWithVirtual((VirtualObjectNode) alias);
81        }
82    }
83
84    @Override
85    public Node canonical(CanonicalizerTool tool) {
86        if (getGuard() == null) {
87            if (stamp().equals(object().stamp())) {
88                return object();
89            } else {
90                return PiNode.create(object(), stamp());
91            }
92        }
93        return this;
94    }
95
96    @Override
97    public ValueNode getOriginalNode() {
98        return object;
99    }
100}
101