package org.graalvm.compiler.phases.verify;
import org.graalvm.compiler.core.common.type.ObjectStamp;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeInputList;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.graalvm.compiler.nodes.spi.Virtualizable;
import org.graalvm.compiler.phases.VerifyPhase;
import org.graalvm.compiler.phases.tiers.PhaseContext;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public class VerifyVirtualizableUsage extends VerifyPhase<PhaseContext> {
@Override
public boolean checkContract() {
return false;
}
@Override
protected boolean verify(StructuredGraph graph, PhaseContext context) {
final ResolvedJavaType graphType = context.getMetaAccess().lookupJavaType(Graph.class);
final ResolvedJavaType virtualizableType = context.getMetaAccess().lookupJavaType(Virtualizable.class);
final ResolvedJavaType constantNodeType = context.getMetaAccess().lookupJavaType(ConstantNode.class);
if (virtualizableType.isAssignableFrom(graph.method().getDeclaringClass()) && graph.method().getName().equals("virtualize")) {
for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
int bci = t.invoke().bci();
ResolvedJavaMethod callee = t.targetMethod();
String calleeName = callee.getName();
if (callee.getDeclaringClass().equals(graphType)) {
if (calleeName.equals("add") || calleeName.equals("addWithoutUnique") || calleeName.equals("addOrUnique") || calleeName.equals("addWithoutUniqueWithInputs") ||
calleeName.equals("addOrUniqueWithInputs")) {
verifyVirtualizableEffectArguments(constantNodeType, graph.method(), callee, bci, t.arguments(), 1);
}
}
}
}
return true;
}
private static void verifyVirtualizableEffectArguments(ResolvedJavaType constantNodeType, ResolvedJavaMethod caller, ResolvedJavaMethod callee, int bciCaller,
NodeInputList<? extends Node> arguments, int startIdx) {
int i = 0;
for (Node arg : arguments) {
if (i >= startIdx) {
Stamp argStamp = ((ValueNode) arg).stamp();
if (argStamp instanceof ObjectStamp) {
ObjectStamp objectStamp = (ObjectStamp) argStamp;
ResolvedJavaType argStampType = objectStamp.type();
if (!(argStampType.equals(constantNodeType))) {
StackTraceElement e = caller.asStackTraceElement(bciCaller);
throw new VerificationError("%s:Parameter %d in call to %s (which has effects on the graph) is not a " +
"constant and thus not safe to apply during speculative virtualization.", e, i, callee.format("%H.%n(%p)"));
}
}
}
i++;
}
}
}