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