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