package org.graalvm.compiler.hotspot.replacements;
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.ConstantNode;
import org.graalvm.compiler.nodes.InvokeNode;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.spi.Lowerable;
import org.graalvm.compiler.nodes.spi.LoweringTool;
import org.graalvm.compiler.replacements.nodes.MacroStateSplitNode;
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
import jdk.vm.ci.meta.Assumptions;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.MetaAccessProvider;
@NodeInfo
public final class CallSiteTargetNode extends MacroStateSplitNode implements Canonicalizable, Lowerable {
public static final NodeClass<CallSiteTargetNode> TYPE = NodeClass.create(CallSiteTargetNode.class);
public CallSiteTargetNode(MacroParams p) {
super(TYPE, p);
}
private ValueNode getCallSite() {
return arguments.get(0);
}
public static ConstantNode tryFold(ValueNode callSite, MetaAccessProvider metaAccess, Assumptions assumptions) {
if (callSite != null && callSite.isConstant() && !callSite.isNullConstant()) {
HotSpotObjectConstant c = (HotSpotObjectConstant) callSite.asConstant();
JavaConstant target = c.getCallSiteTarget(assumptions);
if (target != null) {
return ConstantNode.forConstant(target, metaAccess);
}
}
return null;
}
@Override
public Node canonical(CanonicalizerTool tool) {
ConstantNode target = tryFold(getCallSite(), tool.getMetaAccess(), graph().getAssumptions());
if (target != null) {
return target;
}
return this;
}
@Override
public void lower(LoweringTool tool) {
ConstantNode target = tryFold(getCallSite(), tool.getMetaAccess(), graph().getAssumptions());
if (target != null) {
graph().replaceFixedWithFloating(this, target);
} else {
InvokeNode invoke = createInvoke();
graph().replaceFixedWithFixed(this, invoke);
invoke.lower(tool);
}
}
}