package org.graalvm.compiler.nodes;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.graph.IterableNodeType;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.extended.GuardingNode;
import org.graalvm.compiler.nodes.spi.LIRLowerable;
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import org.graalvm.compiler.nodes.spi.ValueProxy;
import org.graalvm.compiler.nodes.spi.Virtualizable;
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
import jdk.vm.ci.meta.JavaKind;
@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
public final class GuardedValueNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy {
public static final NodeClass<GuardedValueNode> TYPE = NodeClass.create(GuardedValueNode.class);
@Input ValueNode object;
protected final Stamp piStamp;
public GuardedValueNode(ValueNode object, GuardingNode guard, Stamp stamp) {
super(TYPE, computeStamp(stamp, object), guard);
this.object = object;
this.piStamp = stamp;
}
public GuardedValueNode(ValueNode object, GuardingNode guard) {
this(object, guard, object.stamp());
}
public ValueNode object() {
return object;
}
@Override
public void generate(NodeLIRBuilderTool generator) {
if (object.getStackKind() != JavaKind.Void && object.getStackKind() != JavaKind.Illegal) {
generator.setResult(this, generator.operand(object));
}
}
@Override
public boolean inferStamp() {
return updateStamp(computeStamp(piStamp, object()));
}
static Stamp computeStamp(Stamp piStamp, ValueNode object) {
return piStamp.improveWith(object.stamp());
}
@Override
public void virtualize(VirtualizerTool tool) {
ValueNode alias = tool.getAlias(object());
if (alias instanceof VirtualObjectNode) {
tool.replaceWithVirtual((VirtualObjectNode) alias);
}
}
@Override
public Node canonical(CanonicalizerTool tool) {
if (getGuard() == null) {
if (stamp().equals(object().stamp())) {
return object();
} else {
return new PiNode(object(), stamp());
}
}
return this;
}
@Override
public ValueNode getOriginalNode() {
return object;
}
}