1 /* 2 * Copyright (c) 2012, 2018, 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 24 25 package org.graalvm.compiler.replacements.test; 26 27 import java.lang.reflect.InvocationTargetException; 28 29 import org.graalvm.compiler.api.replacements.MethodSubstitution; 30 import org.graalvm.compiler.core.test.GraalCompilerTest; 31 import org.graalvm.compiler.debug.DebugContext; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.nodes.Invoke; 34 import org.graalvm.compiler.nodes.StructuredGraph; 35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 36 import org.graalvm.compiler.nodes.java.MethodCallTargetNode; 37 import org.graalvm.compiler.nodes.spi.LoweringTool; 38 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 39 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; 40 import org.graalvm.compiler.phases.common.LoweringPhase; 41 import org.graalvm.compiler.phases.tiers.HighTierContext; 42 import org.graalvm.compiler.replacements.nodes.MacroNode; 43 44 import jdk.vm.ci.code.InstalledCode; 45 import jdk.vm.ci.code.InvalidInstalledCodeException; 46 import jdk.vm.ci.meta.ResolvedJavaMethod; 47 48 /** 49 * Tests if {@link MethodSubstitution}s are inlined correctly. Most test cases only assert that 50 * there are no remaining invocations in the graph. This is sufficient if the method that is being 51 * substituted is a native method. For Java methods, additional checks are necessary. 52 */ 53 public abstract class MethodSubstitutionTest extends GraalCompilerTest { 54 testGraph(final String snippet)55 protected StructuredGraph testGraph(final String snippet) { 56 return testGraph(snippet, null); 57 } 58 59 @SuppressWarnings("try") testGraph(final String snippet, String name)60 protected StructuredGraph testGraph(final String snippet, String name) { 61 return testGraph(getResolvedJavaMethod(snippet), name); 62 } 63 64 @SuppressWarnings("try") testGraph(final ResolvedJavaMethod method, String name)65 protected StructuredGraph testGraph(final ResolvedJavaMethod method, String name) { 66 DebugContext debug = getDebugContext(); 67 try (DebugContext.Scope s = debug.scope("MethodSubstitutionTest", method)) { 68 StructuredGraph graph = parseEager(method, AllowAssumptions.YES, debug); 69 HighTierContext context = getDefaultHighTierContext(); 70 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 71 createInliningPhase().apply(graph, context); 72 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 73 new CanonicalizerPhase().apply(graph, context); 74 new DeadCodeEliminationPhase().apply(graph); 75 // Try to ensure any macro nodes are lowered to expose any resulting invokes 76 if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) { 77 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 78 } 79 if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) { 80 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context); 81 } 82 assertNotInGraph(graph, MacroNode.class); 83 if (name != null) { 84 for (Node node : graph.getNodes()) { 85 if (node instanceof Invoke) { 86 Invoke invoke = (Invoke) node; 87 if (invoke.callTarget() instanceof MethodCallTargetNode) { 88 MethodCallTargetNode call = (MethodCallTargetNode) invoke.callTarget(); 89 assertTrue(!call.targetMethod().getName().equals(name), "Unexpected invoke of intrinsic %s", call.targetMethod()); 90 } 91 } 92 93 } 94 } else { 95 assertNotInGraph(graph, Invoke.class); 96 } 97 return graph; 98 } catch (Throwable e) { 99 throw debug.handle(e); 100 } 101 } 102 assertNotInGraph(StructuredGraph graph, Class<?> clazz)103 protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) { 104 for (Node node : graph.getNodes()) { 105 if (clazz.isInstance(node)) { 106 fail(node.toString()); 107 } 108 } 109 return graph; 110 } 111 testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, boolean forceCompilation, Object[] args1, Object[] args2)112 protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, boolean forceCompilation, 113 Object[] args1, Object[] args2) { 114 ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName, parameterTypes); 115 ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName); 116 StructuredGraph graph = testGraph(testMethodName); 117 118 // Check to see if the resulting graph contains the expected node 119 StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null, graph.getOptions()); 120 if (replacement == null && !optional) { 121 assertInGraph(graph, intrinsicClass); 122 } 123 124 // Force compilation 125 InstalledCode code = getCode(testMethod, null, forceCompilation); 126 assert optional || code != null; 127 128 for (int i = 0; i < args1.length; i++) { 129 Object arg1 = args1[i]; 130 Object arg2 = args2[i]; 131 Object expected = invokeSafe(realMethod, null, arg1, arg2); 132 // Verify that the original method and the substitution produce the same value 133 assertDeepEquals(expected, invokeSafe(testMethod, null, arg1, arg2)); 134 // Verify that the generated code and the original produce the same value 135 assertDeepEquals(expected, executeVarargsSafe(code, arg1, arg2)); 136 } 137 } 138 assertInGraph(StructuredGraph graph, Class<?> clazz)139 protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?> clazz) { 140 for (Node node : graph.getNodes()) { 141 if (clazz.isInstance(node)) { 142 return graph; 143 } 144 } 145 fail("Graph does not contain a node of class " + clazz.getName()); 146 return graph; 147 } 148 executeVarargsSafe(InstalledCode code, Object... args)149 protected static Object executeVarargsSafe(InstalledCode code, Object... args) { 150 try { 151 return code.executeVarargs(args); 152 } catch (InvalidInstalledCodeException e) { 153 throw new RuntimeException(e); 154 } 155 } 156 invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args)157 protected Object invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args) { 158 try { 159 return invoke(method, receiver, args); 160 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { 161 throw new RuntimeException(e); 162 } 163 } 164 165 } 166