1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.microbenchmarks.graal.util;
  24 
  25 import java.lang.reflect.Method;
  26 import java.util.List;
  27 
  28 import org.graalvm.compiler.api.test.Graal;
  29 import org.graalvm.compiler.graph.Node;
  30 import org.graalvm.compiler.java.GraphBuilderPhase;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  36 import org.graalvm.compiler.options.OptionValues;
  37 import org.graalvm.compiler.phases.OptimisticOptimizations;
  38 import org.graalvm.compiler.phases.PhaseSuite;
  39 import org.graalvm.compiler.phases.tiers.HighTierContext;
  40 
  41 import jdk.vm.ci.meta.MetaAccessProvider;
  42 import jdk.vm.ci.meta.ResolvedJavaMethod;
  43 
  44 public class GraalUtil {
  45 
  46     public static Method getMethod(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
  47         try {
  48             if (parameterTypes == null) {
  49                 Method found = null;
  50                 for (Method m : declaringClass.getDeclaredMethods()) {
  51                     if (m.getName().equals(name)) {
  52                         if (found != null) {
  53                             throw new RuntimeException("more than one method named " + name + " in " + declaringClass);
  54                         }
  55                         found = m;
  56                     }
  57                 }
  58                 if (found == null) {
  59                     throw new NoSuchMethodException(declaringClass.getName() + "." + name);
  60                 }
  61                 return found;
  62             } else {
  63                 return declaringClass.getDeclaredMethod(name, parameterTypes);
  64             }
  65         } catch (Exception e) {
  66             throw new RuntimeException(e);
  67         }
  68     }
  69 
  70     /**
  71      * Gets the first {@link MethodSpec} annotation encountered in the class hierarchy terminated by
  72      * {@code startClass}.
  73      */
  74     public static MethodSpec getMethodSpec(Class<?> startClass) {
  75         Class<?> c = startClass;
  76         while (c != null) {
  77             MethodSpec methodSpec = c.getAnnotation(MethodSpec.class);
  78             if (methodSpec != null) {
  79                 return methodSpec;
  80             }
  81             c = c.getSuperclass();
  82         }
  83         throw new RuntimeException("Could not find class annotated with " + MethodSpec.class.getSimpleName() + " in hierarchy of " + startClass);
  84     }
  85 
  86     /**
  87      * Gets the method specified by the first {@link MethodSpec} annotation encountered in the class
  88      * hierarchy terminated by {@code startClass}.
  89      */
  90     public static Method getMethodFromMethodSpec(Class<?> startClass) {
  91         MethodSpec methodSpec = getMethodSpec(startClass);
  92         Class<?> declaringClass = methodSpec.declaringClass();
  93         if (declaringClass == MethodSpec.class) {
  94             declaringClass = startClass;
  95         }
  96         String name = methodSpec.name();
  97         Class<?>[] parameters = methodSpec.parameters();
  98         if (parameters.length == 1 && parameters[0] == void.class) {
  99             parameters = null;
 100         }
 101         return getMethod(declaringClass, name, parameters);
 102     }
 103 
 104     /**
 105      * Gets the graph for the method specified by the first {@link MethodSpec} annotation
 106      * encountered in the class hierarchy terminated by {@code startClass}.
 107      */
 108     public static StructuredGraph getGraphFromMethodSpec(Class<?> startClass) {
 109         MethodSpec methodSpec = getMethodSpec(startClass);
 110         Class<?> declaringClass = methodSpec.declaringClass();
 111         if (declaringClass == MethodSpec.class) {
 112             declaringClass = startClass;
 113         }
 114         String name = methodSpec.name();
 115         Class<?>[] parameters = methodSpec.parameters();
 116         if (parameters.length == 1 && parameters[0] == void.class) {
 117             parameters = null;
 118         }
 119         return getGraph(declaringClass, name, parameters);
 120     }
 121 
 122     public static StructuredGraph getGraph(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
 123         return getGraph(getMethod(declaringClass, name, parameterTypes));
 124     }
 125 
 126     public static StructuredGraph getGraph(Method method) {
 127         GraalState graal = new GraalState();
 128         ResolvedJavaMethod javaMethod = graal.metaAccess.lookupJavaMethod(method);
 129         return getGraph(graal, javaMethod);
 130     }
 131 
 132     public static StructuredGraph getGraph(GraalState graal, ResolvedJavaMethod javaMethod) {
 133         return getGraph(graal, javaMethod, StructuredGraph.USE_PROFILING_INFO);
 134     }
 135 
 136     public static StructuredGraph getGraph(GraalState graal, ResolvedJavaMethod javaMethod, boolean useProfilingInfo) {
 137         StructuredGraph graph = new StructuredGraph.Builder(Graal.getRequiredCapability(OptionValues.class), AllowAssumptions.YES).useProfilingInfo(useProfilingInfo).method(javaMethod).build();
 138         PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
 139         MetaAccessProvider metaAccess = graal.providers.getMetaAccess();
 140         graphBuilderSuite.appendPhase(new GraphBuilderPhase(GraphBuilderConfiguration.getDefault(new Plugins(new InvocationPlugins(metaAccess)))));
 141         graphBuilderSuite.apply(graph, new HighTierContext(graal.providers, graphBuilderSuite, OptimisticOptimizations.ALL));
 142         return graph;
 143     }
 144 
 145     public static Node[] getNodes(StructuredGraph graph) {
 146         List<Node> nodeList = graph.getNodes().snapshot();
 147         return nodeList.toArray(new Node[nodeList.size()]);
 148     }
 149 }