package org.graalvm.compiler.nodes.memory;
import static org.graalvm.compiler.nodeinfo.InputType.Guard;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.core.common.LocationIdentity;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.Simplifiable;
import org.graalvm.compiler.graph.spi.SimplifierTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.PiNode;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.extended.GuardingNode;
import org.graalvm.compiler.nodes.memory.address.AddressNode;
import org.graalvm.compiler.nodes.memory.address.AddressNode.Address;
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
import org.graalvm.compiler.nodes.spi.LIRLowerable;
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import org.graalvm.compiler.nodes.spi.Virtualizable;
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
@NodeInfo(nameTemplate = "Write#{p#location/s}")
public class WriteNode extends AbstractWriteNode implements LIRLowerable, Simplifiable, Virtualizable {
public static final NodeClass<WriteNode> TYPE = NodeClass.create(WriteNode.class);
@OptionalInput(Guard) protected GuardingNode storeCheckGuard;
protected WriteNode(ValueNode address, LocationIdentity location, ValueNode value, BarrierType barrierType) {
this((AddressNode) address, location, value, barrierType);
}
public WriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType) {
super(TYPE, address, location, value, barrierType);
}
public WriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, boolean initialization) {
super(TYPE, address, location, value, barrierType, initialization);
}
public WriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, GuardingNode guard, boolean initialization) {
this(TYPE, address, location, value, barrierType, guard, initialization);
}
protected WriteNode(NodeClass<? extends WriteNode> c, AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, GuardingNode guard, boolean initialization) {
super(c, address, location, value, barrierType, guard, initialization);
}
@Override
public void generate(NodeLIRBuilderTool gen) {
LIRKind writeKind = gen.getLIRGeneratorTool().getLIRKind(value().stamp());
gen.getLIRGeneratorTool().getArithmetic().emitStore(writeKind, gen.operand(address), gen.operand(value()), gen.state(this));
}
@Override
public void simplify(SimplifierTool tool) {
if (getAddress() instanceof OffsetAddressNode) {
OffsetAddressNode objAddress = (OffsetAddressNode) getAddress();
if (objAddress.getBase() instanceof PiNode && ((PiNode) objAddress.getBase()).getGuard() == getGuard()) {
OffsetAddressNode newAddress = graph().unique(new OffsetAddressNode(((PiNode) objAddress.getBase()).getOriginalNode(), objAddress.getOffset()));
setAddress(newAddress);
tool.addToWorkList(newAddress);
}
}
}
@NodeIntrinsic
public static native void writeMemory(Address address, @ConstantNodeParameter LocationIdentity location, Object value, @ConstantNodeParameter BarrierType barrierType);
@Override
public void virtualize(VirtualizerTool tool) {
throw GraalError.shouldNotReachHere("unexpected WriteNode before PEA");
}
@Override
public boolean canNullCheck() {
return true;
}
public void setStoreCheckGuard(GuardingNode newStoreCheckGuard) {
updateUsages((Node) this.storeCheckGuard, (Node) newStoreCheckGuard);
this.storeCheckGuard = newStoreCheckGuard;
}
}