001/*
002 * Copyright (c) 2013, 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;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.extended.*;
032import com.oracle.graal.nodes.spi.*;
033import com.oracle.graal.nodes.virtual.*;
034
035/**
036 * A node that changes the type of its input, usually narrowing it. For example, a GuardedValueNode
037 * is used to keep the nodes depending on guards inside a loop during speculative guard movement.
038 *
039 * A GuardedValueNode will only go away if its guard is null or {@link StructuredGraph#start()}.
040 */
041@NodeInfo
042public final class GuardedValueNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy {
043
044    public static final NodeClass<GuardedValueNode> TYPE = NodeClass.create(GuardedValueNode.class);
045    @Input ValueNode object;
046    protected final Stamp piStamp;
047
048    public GuardedValueNode(ValueNode object, GuardingNode guard, Stamp stamp) {
049        super(TYPE, stamp, guard);
050        this.object = object;
051        this.piStamp = stamp;
052    }
053
054    public GuardedValueNode(ValueNode object, GuardingNode guard) {
055        this(object, guard, object.stamp());
056    }
057
058    public ValueNode object() {
059        return object;
060    }
061
062    @Override
063    public void generate(NodeLIRBuilderTool generator) {
064        if (object.getKind() != Kind.Void && object.getKind() != Kind.Illegal) {
065            generator.setResult(this, generator.operand(object));
066        }
067    }
068
069    @Override
070    public boolean inferStamp() {
071        return updateStamp(piStamp.improveWith(object().stamp()));
072    }
073
074    @Override
075    public void virtualize(VirtualizerTool tool) {
076        ValueNode alias = tool.getAlias(object());
077        if (alias instanceof VirtualObjectNode) {
078            tool.replaceWithVirtual((VirtualObjectNode) alias);
079        }
080    }
081
082    @Override
083    public Node canonical(CanonicalizerTool tool) {
084        if (getGuard() == null) {
085            if (stamp().equals(object().stamp())) {
086                return object();
087            } else {
088                return new PiNode(object(), stamp());
089            }
090        }
091        return this;
092    }
093
094    @Override
095    public ValueNode getOriginalNode() {
096        return object;
097    }
098}