package org.graalvm.compiler.microbenchmarks.graal.util;
import java.lang.reflect.Method;
import java.util.List;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.java.GraphBuilderPhase;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
import org.graalvm.compiler.phases.OptimisticOptimizations;
import org.graalvm.compiler.phases.PhaseSuite;
import org.graalvm.compiler.phases.tiers.HighTierContext;
import jdk.vm.ci.meta.ResolvedJavaMethod;
public class GraalUtil {
public static Method getMethod(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
try {
if (parameterTypes == null) {
Method found = null;
for (Method m : declaringClass.getDeclaredMethods()) {
if (m.getName().equals(name)) {
if (found != null) {
throw new RuntimeException("more than one method named " + name + " in " + declaringClass);
}
found = m;
}
}
if (found == null) {
throw new NoSuchMethodException(declaringClass.getName() + "." + name);
}
return found;
} else {
return declaringClass.getDeclaredMethod(name, parameterTypes);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static MethodSpec getMethodSpec(Class<?> startClass) {
Class<?> c = startClass;
while (c != null) {
MethodSpec methodSpec = c.getAnnotation(MethodSpec.class);
if (methodSpec != null) {
return methodSpec;
}
c = c.getSuperclass();
}
throw new RuntimeException("Could not find class annotated with " + MethodSpec.class.getSimpleName() + " in hierarchy of " + startClass);
}
public static Method getMethodFromMethodSpec(Class<?> startClass) {
MethodSpec methodSpec = getMethodSpec(startClass);
Class<?> declaringClass = methodSpec.declaringClass();
if (declaringClass == MethodSpec.class) {
declaringClass = startClass;
}
String name = methodSpec.name();
Class<?>[] parameters = methodSpec.parameters();
if (parameters.length == 1 && parameters[0] == void.class) {
parameters = null;
}
return getMethod(declaringClass, name, parameters);
}
public static StructuredGraph getGraphFromMethodSpec(Class<?> startClass) {
MethodSpec methodSpec = getMethodSpec(startClass);
Class<?> declaringClass = methodSpec.declaringClass();
if (declaringClass == MethodSpec.class) {
declaringClass = startClass;
}
String name = methodSpec.name();
Class<?>[] parameters = methodSpec.parameters();
if (parameters.length == 1 && parameters[0] == void.class) {
parameters = null;
}
return getGraph(declaringClass, name, parameters);
}
public static StructuredGraph getGraph(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
return getGraph(getMethod(declaringClass, name, parameterTypes));
}
public static StructuredGraph getGraph(Method method) {
GraalState graal = new GraalState();
ResolvedJavaMethod javaMethod = graal.metaAccess.lookupJavaMethod(method);
return getGraph(graal, javaMethod);
}
public static StructuredGraph getGraph(GraalState graal, ResolvedJavaMethod javaMethod) {
return getGraph(graal, javaMethod, StructuredGraph.USE_PROFILING_INFO);
}
public static StructuredGraph getGraph(GraalState graal, ResolvedJavaMethod javaMethod, boolean useProfilingInfo) {
StructuredGraph graph = new StructuredGraph.Builder(graal.options, graal.debug, AllowAssumptions.YES).useProfilingInfo(
useProfilingInfo).method(javaMethod).build();
PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
graphBuilderSuite.appendPhase(new GraphBuilderPhase(GraphBuilderConfiguration.getDefault(new Plugins(new InvocationPlugins()))));
graphBuilderSuite.apply(graph, new HighTierContext(graal.providers, graphBuilderSuite, OptimisticOptimizations.ALL));
return graph;
}
public static Node[] getNodes(StructuredGraph graph) {
List<Node> nodeList = graph.getNodes().snapshot();
return nodeList.toArray(new Node[nodeList.size()]);
}
}