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