1 /* 2 * Copyright (c) 2012, 2019, 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 import java.util.Arrays; 29 30 import org.graalvm.compiler.api.replacements.MethodSubstitution; 31 import org.graalvm.compiler.core.test.GraalCompilerTest; 32 import org.graalvm.compiler.debug.DebugContext; 33 import org.graalvm.compiler.graph.Node; 34 import org.graalvm.compiler.nodes.Invoke; 35 import org.graalvm.compiler.nodes.StructuredGraph; 36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 37 import org.graalvm.compiler.nodes.java.MethodCallTargetNode; 38 import org.graalvm.compiler.nodes.spi.LoweringTool; 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 testGraph(final String snippet, boolean assertInvoke)59 protected StructuredGraph testGraph(final String snippet, boolean assertInvoke) { 60 return testGraph(snippet, null, assertInvoke); 61 } 62 testGraph(final String snippet, String name)63 protected StructuredGraph testGraph(final String snippet, String name) { 64 return testGraph(snippet, name, false); 65 } 66 67 @SuppressWarnings("try") testGraph(final String snippet, String name, boolean assertInvoke)68 protected StructuredGraph testGraph(final String snippet, String name, boolean assertInvoke) { 69 return testGraph(getResolvedJavaMethod(snippet), name, assertInvoke); 70 } 71 72 @SuppressWarnings("try") testGraph(final ResolvedJavaMethod method, String name, boolean assertInvoke)73 protected StructuredGraph testGraph(final ResolvedJavaMethod method, String name, boolean assertInvoke) { 74 DebugContext debug = getDebugContext(); 75 try (DebugContext.Scope s = debug.scope("MethodSubstitutionTest", method)) { 76 StructuredGraph graph = parseEager(method, AllowAssumptions.YES, debug); 77 HighTierContext context = getDefaultHighTierContext(); 78 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 79 createInliningPhase().apply(graph, context); 80 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 81 createCanonicalizerPhase().apply(graph, context); 82 new DeadCodeEliminationPhase().apply(graph); 83 // Try to ensure any macro nodes are lowered to expose any resulting invokes 84 if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) { 85 new LoweringPhase(this.createCanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 86 } 87 if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) { 88 new LoweringPhase(this.createCanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context); 89 } 90 assertNotInGraph(graph, MacroNode.class); 91 if (name != null) { 92 for (Node node : graph.getNodes()) { 93 if (node instanceof Invoke) { 94 Invoke invoke = (Invoke) node; 95 if (invoke.callTarget() instanceof MethodCallTargetNode) { 96 MethodCallTargetNode call = (MethodCallTargetNode) invoke.callTarget(); 97 boolean found = call.targetMethod().getName().equals(name); 98 if (assertInvoke) { 99 assertTrue(found, "Expected to find a call to %s", name); 100 } else { 101 assertFalse(found, "Unexpected call to %s", name); 102 } 103 } 104 } 105 106 } 107 } else { 108 if (assertInvoke) { 109 assertInGraph(graph, Invoke.class); 110 } else { 111 assertNotInGraph(graph, Invoke.class); 112 } 113 } 114 return graph; 115 } catch (Throwable e) { 116 throw debug.handle(e); 117 } 118 } 119 assertNotInGraph(StructuredGraph graph, Class<?> clazz)120 protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) { 121 for (Node node : graph.getNodes()) { 122 if (clazz.isInstance(node)) { 123 fail(node.toString()); 124 } 125 } 126 return graph; 127 } 128 testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, boolean forceCompilation, Object[] args1, Object[] args2)129 protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, boolean forceCompilation, 130 Object[] args1, Object[] args2) { 131 ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName, parameterTypes); 132 ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName); 133 StructuredGraph graph = testGraph(testMethodName); 134 135 // Check to see if the resulting graph contains the expected node 136 StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null, graph.getOptions()); 137 if (replacement == null && !optional) { 138 assertInGraph(graph, intrinsicClass); 139 } 140 141 // Force compilation 142 InstalledCode code = getCode(testMethod, null, forceCompilation); 143 assert optional || code != null; 144 145 for (int i = 0; i < args1.length; i++) { 146 Object arg1 = args1[i]; 147 Object arg2 = args2[i]; 148 Object expected = invokeSafe(realMethod, null, arg1, arg2); 149 // Verify that the original method and the substitution produce the same value 150 assertDeepEquals(expected, invokeSafe(testMethod, null, arg1, arg2)); 151 // Verify that the generated code and the original produce the same value 152 assertDeepEquals(expected, executeVarargsSafe(code, arg1, arg2)); 153 } 154 } 155 assertInGraph(StructuredGraph graph, Class<?>... clazzes)156 protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?>... clazzes) { 157 for (Node node : graph.getNodes()) { 158 for (Class<?> clazz : clazzes) { 159 if (clazz.isInstance(node)) { 160 return graph; 161 } 162 } 163 } 164 if (clazzes.length == 1) { 165 fail("Graph does not contain a node of class " + clazzes[0].getName()); 166 } else { 167 fail("Graph does not contain a node of one these classes class " + Arrays.toString(clazzes)); 168 169 } 170 return graph; 171 } 172 executeVarargsSafe(InstalledCode code, Object... args)173 protected static Object executeVarargsSafe(InstalledCode code, Object... args) { 174 try { 175 return code.executeVarargs(args); 176 } catch (InvalidInstalledCodeException e) { 177 throw new RuntimeException(e); 178 } 179 } 180 invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args)181 protected Object invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args) { 182 try { 183 return invoke(method, receiver, args); 184 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { 185 throw new RuntimeException(e); 186 } 187 } 188 189 } 190