package org.graalvm.compiler.nodes;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
import org.graalvm.compiler.core.common.type.ObjectStamp;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.core.common.type.TypeReference;
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.graphbuilderconf.GraphBuilderContext;
import org.graalvm.compiler.nodes.memory.ReadNode;
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.type.StampTool;
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
import jdk.vm.ci.meta.ResolvedJavaType;
@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
public class PiNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy {
public static final NodeClass<PiNode> TYPE = NodeClass.create(PiNode.class);
@Input ValueNode object;
protected Stamp piStamp;
public ValueNode object() {
return object;
}
protected PiNode(NodeClass<? extends PiNode> c, ValueNode object, Stamp stamp, GuardingNode guard) {
super(c, stamp, guard);
this.object = object;
this.piStamp = stamp;
assert piStamp.isCompatible(object.stamp(NodeView.DEFAULT)) : "Object stamp not compatible to piStamp";
inferStamp();
}
public PiNode(ValueNode object, Stamp stamp) {
this(object, stamp, null);
}
public PiNode(ValueNode object, Stamp stamp, ValueNode guard) {
this(TYPE, object, stamp, (GuardingNode) guard);
}
public PiNode(ValueNode object, ValueNode guard) {
this(object, AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT)), guard);
}
public PiNode(ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) {
this(object, StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType),
nonNull || StampTool.isPointerNonNull(object.stamp(NodeView.DEFAULT))));
}
public static ValueNode create(ValueNode object, Stamp stamp) {
ValueNode value = canonical(object, stamp, null);
if (value != null) {
return value;
}
return new PiNode(object, stamp);
}
public static ValueNode create(ValueNode object, Stamp stamp, ValueNode guard) {
ValueNode value = canonical(object, stamp, (GuardingNode) guard);
if (value != null) {
return value;
}
return new PiNode(object, stamp, guard);
}
public static ValueNode create(ValueNode object, ValueNode guard) {
Stamp stamp = AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT));
ValueNode value = canonical(object, stamp, (GuardingNode) guard);
if (value != null) {
return value;
}
return new PiNode(object, stamp, guard);
}
@SuppressWarnings("unused")
public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode object, ValueNode guard) {
Stamp stamp = AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT));
ValueNode value = canonical(object, stamp, (GuardingNode) guard);
if (value == null) {
value = new PiNode(object, stamp, guard);
}
b.push(JavaKind.Object, b.append(value));
return true;
}
@SuppressWarnings("unused")
public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) {
Stamp stamp = StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType),
nonNull || StampTool.isPointerNonNull(object.stamp(NodeView.DEFAULT)));
ValueNode value = canonical(object, stamp, null);
if (value == null) {
value = new PiNode(object, stamp);
}
b.push(JavaKind.Object, b.append(value));
return true;
}
public final Stamp piStamp() {
return piStamp;
}
public void strengthenPiStamp(Stamp newPiStamp) {
assert this.piStamp.join(newPiStamp).equals(newPiStamp) : "stamp can only improve";
this.piStamp = newPiStamp;
}
@Override
public void generate(NodeLIRBuilderTool generator) {
if (generator.hasOperand(object)) {
generator.setResult(this, generator.operand(object));
}
}
@Override
public boolean inferStamp() {
return updateStamp(computeStamp());
}
private Stamp computeStamp() {
return piStamp.improveWith(object().stamp(NodeView.DEFAULT));
}
@Override
public void virtualize(VirtualizerTool tool) {
ValueNode alias = tool.getAlias(object());
if (alias instanceof VirtualObjectNode) {
VirtualObjectNode virtual = (VirtualObjectNode) alias;
if (StampTool.typeOrNull(this) != null && StampTool.typeOrNull(this).isAssignableFrom(virtual.type())) {
tool.replaceWithVirtual(virtual);
}
}
}
public static ValueNode canonical(ValueNode object, Stamp stamp, GuardingNode guard) {
Stamp computedStamp = stamp.improveWith(object.stamp(NodeView.DEFAULT));
if (computedStamp.equals(object.stamp(NodeView.DEFAULT))) {
return object;
}
if (guard == null) {
if (object instanceof ReadNode && !object.hasMoreThanOneUsage()) {
ReadNode readNode = (ReadNode) object;
readNode.setStamp(readNode.stamp(NodeView.DEFAULT).improveWith(stamp));
return readNode;
}
} else {
for (Node n : guard.asNode().usages()) {
if (n instanceof PiNode) {
PiNode otherPi = (PiNode) n;
if (object == otherPi.object() && computedStamp.equals(otherPi.stamp(NodeView.DEFAULT))) {
Stamp newStamp = stamp.join(otherPi.piStamp);
if (newStamp.equals(otherPi.piStamp)) {
return otherPi;
}
}
}
}
}
return null;
}
@Override
public Node canonical(CanonicalizerTool tool) {
Node value = canonical(object(), stamp(NodeView.DEFAULT), getGuard());
if (value != null) {
return value;
}
return this;
}
@Override
public ValueNode getOriginalNode() {
return object;
}
public void setOriginalNode(ValueNode newNode) {
this.updateUsages(object, newNode);
this.object = newNode;
assert piStamp.isCompatible(object.stamp(NodeView.DEFAULT)) : "New object stamp not compatible to piStamp";
}
public static Class<?> asNonNullClass(Object object) {
return asNonNullClassIntrinsic(object, Class.class, true, true);
}
public static Class<?> asNonNullObject(Object object) {
return asNonNullClassIntrinsic(object, Object.class, false, true);
}
@NodeIntrinsic(PiNode.class)
private static native Class<?> asNonNullClassIntrinsic(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
@NodeIntrinsic(PiNode.Placeholder.class)
public static native Object piCastToSnippetReplaceeStamp(Object object);
@NodeIntrinsic
public static native Object piCastNonNull(Object object, GuardingNode guard);
@NodeIntrinsic
public static native Class<?> piCastNonNullClass(Class<?> type, GuardingNode guard);
public static Object piCastNonNull(Object object, @ConstantNodeParameter Class<?> toType) {
return piCast(object, toType, false, true);
}
@NodeIntrinsic
public static native Object piCast(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
public static class Placeholder extends FloatingGuardedNode {
public static final NodeClass<Placeholder> TYPE = NodeClass.create(Placeholder.class);
@Input ValueNode object;
public ValueNode object() {
return object;
}
protected Placeholder(NodeClass<? extends Placeholder> c, ValueNode object) {
super(c, PlaceholderStamp.SINGLETON, null);
this.object = object;
}
public Placeholder(ValueNode object) {
this(TYPE, object);
}
public void makeReplacement(Stamp snippetReplaceeStamp) {
ValueNode value = graph().maybeAddOrUnique(PiNode.create(object(), snippetReplaceeStamp, null));
replaceAndDelete(value);
}
}
public static final class PlaceholderStamp extends ObjectStamp {
private static final PlaceholderStamp SINGLETON = new PlaceholderStamp();
public static PlaceholderStamp singleton() {
return SINGLETON;
}
private PlaceholderStamp() {
super(null, false, false, false);
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public boolean equals(Object obj) {
return this == obj;
}
@Override
public String toString() {
return "PlaceholderStamp";
}
}
}