author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58299 | 6df94ce3ab2f |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
58299 | 2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
43972 | 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 |
*/ |
|
50858 | 23 |
|
24 |
||
43972 | 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; |
|
46640 | 31 |
import org.graalvm.compiler.debug.DebugContext; |
43972 | 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; |
|
48190 | 36 |
import org.graalvm.compiler.nodes.java.MethodCallTargetNode; |
43972 | 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 |
||
48190 | 55 |
protected StructuredGraph testGraph(final String snippet) { |
56 |
return testGraph(snippet, null); |
|
57 |
} |
|
58 |
||
58040
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
59 |
protected StructuredGraph testGraph(final String snippet, boolean assertInvoke) { |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
60 |
return testGraph(snippet, null, assertInvoke); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
61 |
} |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
62 |
|
48190 | 63 |
protected StructuredGraph testGraph(final String snippet, String name) { |
58040
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
64 |
return testGraph(snippet, name, false); |
52578 | 65 |
} |
66 |
||
67 |
@SuppressWarnings("try") |
|
58040
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
68 |
protected StructuredGraph testGraph(final String snippet, String name, boolean assertInvoke) { |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
69 |
return testGraph(getResolvedJavaMethod(snippet), name, assertInvoke); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
70 |
} |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
71 |
|
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
72 |
@SuppressWarnings("try") |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
73 |
protected StructuredGraph testGraph(final ResolvedJavaMethod method, String name, boolean assertInvoke) { |
46640 | 74 |
DebugContext debug = getDebugContext(); |
52578 | 75 |
try (DebugContext.Scope s = debug.scope("MethodSubstitutionTest", method)) { |
76 |
StructuredGraph graph = parseEager(method, AllowAssumptions.YES, debug); |
|
43972 | 77 |
HighTierContext context = getDefaultHighTierContext(); |
46640 | 78 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); |
52910 | 79 |
createInliningPhase().apply(graph, context); |
46640 | 80 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); |
43972 | 81 |
new CanonicalizerPhase().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(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); |
|
86 |
} |
|
87 |
if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) { |
|
88 |
new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context); |
|
89 |
} |
|
90 |
assertNotInGraph(graph, MacroNode.class); |
|
48190 | 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(); |
|
58040
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
97 |
boolean found = call.targetMethod().getName().equals(name); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
98 |
if (assertInvoke) { |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
99 |
assertTrue(found, "Expected to find a call to %s", name); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
100 |
} else { |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
101 |
assertFalse(found, "Unexpected call to %s", name); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
102 |
} |
48190 | 103 |
} |
104 |
} |
|
105 |
||
106 |
} |
|
107 |
} else { |
|
58040
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
108 |
if (assertInvoke) { |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
109 |
assertInGraph(graph, Invoke.class); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
110 |
} else { |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
111 |
assertNotInGraph(graph, Invoke.class); |
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
54601
diff
changeset
|
112 |
} |
48190 | 113 |
} |
43972 | 114 |
return graph; |
115 |
} catch (Throwable e) { |
|
46640 | 116 |
throw debug.handle(e); |
43972 | 117 |
} |
118 |
} |
|
119 |
||
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 |
||
52578 | 129 |
protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, boolean forceCompilation, |
130 |
Object[] args1, Object[] args2) { |
|
43972 | 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 |
|
54601 | 136 |
StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null, graph.getOptions()); |
43972 | 137 |
if (replacement == null && !optional) { |
138 |
assertInGraph(graph, intrinsicClass); |
|
139 |
} |
|
140 |
||
141 |
// Force compilation |
|
52578 | 142 |
InstalledCode code = getCode(testMethod, null, forceCompilation); |
43972 | 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 |
||
156 |
protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?> clazz) { |
|
157 |
for (Node node : graph.getNodes()) { |
|
158 |
if (clazz.isInstance(node)) { |
|
159 |
return graph; |
|
160 |
} |
|
161 |
} |
|
162 |
fail("Graph does not contain a node of class " + clazz.getName()); |
|
163 |
return graph; |
|
164 |
} |
|
165 |
||
166 |
protected static Object executeVarargsSafe(InstalledCode code, Object... args) { |
|
167 |
try { |
|
168 |
return code.executeVarargs(args); |
|
169 |
} catch (InvalidInstalledCodeException e) { |
|
170 |
throw new RuntimeException(e); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
protected Object invokeSafe(ResolvedJavaMethod method, Object receiver, Object... args) { |
|
175 |
try { |
|
176 |
return invoke(method, receiver, args); |
|
46551 | 177 |
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { |
43972 | 178 |
throw new RuntimeException(e); |
179 |
} |
|
180 |
} |
|
181 |
||
182 |
} |