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 static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
  26 
  27 import java.lang.reflect.Method;
  28 import java.util.List;
  29 
  30 import jdk.vm.ci.meta.MetaAccessProvider;
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.java.GraphBuilderPhase;
  35 import org.graalvm.compiler.nodes.StructuredGraph;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  37 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  39 import org.graalvm.compiler.phases.OptimisticOptimizations;
  40 import org.graalvm.compiler.phases.PhaseSuite;
  41 import org.graalvm.compiler.phases.tiers.HighTierContext;
  42 
  43 public class GraalUtil {
  44 
  45     public static Method getMethod(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
  46         try {
  47             if (parameterTypes == null) {
  48                 Method found = null;
  49                 for (Method m : declaringClass.getDeclaredMethods()) {
  50                     if (m.getName().equals(name)) {
  51                         if (found != null) {
  52                             throw new RuntimeException("more than one method named " + name + " in " + declaringClass);
  53                         }
  54                         found = m;
  55                     }
  56                 }
  57                 if (found == null) {
  58                     throw new NoSuchMethodException(declaringClass.getName() + "." + name);
  59                 }
  60                 return found;
  61             } else {
  62                 return declaringClass.getDeclaredMethod(name, parameterTypes);
  63             }
  64         } catch (Exception e) {
  65             throw new RuntimeException(e);
  66         }
  67     }
  68 
  69     /**
  70      * Gets the first {@link MethodSpec} annotation encountered in the class hierarchy terminated by
  71      * {@code startClass}.
  72      */
  73     public static MethodSpec getMethodSpec(Class<?> startClass) {
  74         Class<?> c = startClass;
  75         while (c != null) {
  76             MethodSpec methodSpec = c.getAnnotation(MethodSpec.class);
  77             if (methodSpec != null) {
  78                 return methodSpec;
  79             }
  80             c = c.getSuperclass();
  81         }
  82         throw new RuntimeException("Could not find class annotated with " + MethodSpec.class.getSimpleName() + " in hierarchy of " + startClass);
  83     }
  84 
  85     /**
  86      * Gets the method specified by the first {@link MethodSpec} annotation encountered in the class
  87      * hierarchy terminated by {@code startClass}.
  88      */
  89     public static Method getMethodFromMethodSpec(Class<?> startClass) {
  90         MethodSpec methodSpec = getMethodSpec(startClass);
  91         Class<?> declaringClass = methodSpec.declaringClass();
  92         if (declaringClass == MethodSpec.class) {
  93             declaringClass = startClass;
  94         }
  95         String name = methodSpec.name();
  96         Class<?>[] parameters = methodSpec.parameters();
  97         if (parameters.length == 1 && parameters[0] == void.class) {
  98             parameters = null;
  99         }
 100         return getMethod(declaringClass, name, parameters);
 101     }
 102 
 103     /**
 104      * Gets the graph for the method specified by the first {@link MethodSpec} annotation
 105      * encountered in the class hierarchy terminated by {@code startClass}.
 106      */
 107     public static StructuredGraph getGraphFromMethodSpec(Class<?> startClass) {
 108         MethodSpec methodSpec = getMethodSpec(startClass);
 109         Class<?> declaringClass = methodSpec.declaringClass();
 110         if (declaringClass == MethodSpec.class) {
 111             declaringClass = startClass;
 112         }
 113         String name = methodSpec.name();
 114         Class<?>[] parameters = methodSpec.parameters();
 115         if (parameters.length == 1 && parameters[0] == void.class) {
 116             parameters = null;
 117         }
 118         return getGraph(declaringClass, name, parameters);
 119     }
 120 
 121     public static StructuredGraph getGraph(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
 122         return getGraph(getMethod(declaringClass, name, parameterTypes));
 123     }
 124 
 125     public static StructuredGraph getGraph(Method method) {
 126         GraalState graal = new GraalState();
 127         ResolvedJavaMethod javaMethod = graal.metaAccess.lookupJavaMethod(method);
 128         return getGraph(graal, javaMethod);
 129     }
 130 
 131     public static StructuredGraph getGraph(GraalState graal, ResolvedJavaMethod javaMethod) {
 132         return getGraph(graal, javaMethod, StructuredGraph.USE_PROFILING_INFO);
 133     }
 134 
 135     public static StructuredGraph getGraph(GraalState graal, ResolvedJavaMethod javaMethod, boolean useProfilingInfo) {
 136         StructuredGraph graph = new StructuredGraph(javaMethod, StructuredGraph.AllowAssumptions.YES, useProfilingInfo, INVALID_COMPILATION_ID);
 137         PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
 138         MetaAccessProvider metaAccess = graal.providers.getMetaAccess();
 139         graphBuilderSuite.appendPhase(new GraphBuilderPhase(GraphBuilderConfiguration.getDefault(new Plugins(new InvocationPlugins(metaAccess)))));
 140         graphBuilderSuite.apply(graph, new HighTierContext(graal.providers, graphBuilderSuite, OptimisticOptimizations.ALL));
 141         return graph;
 142     }
 143 
 144     public static Node[] getNodes(StructuredGraph graph) {
 145         List<Node> nodeList = graph.getNodes().snapshot();
 146         return nodeList.toArray(new Node[nodeList.size()]);
 147     }
 148 }