43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2011, 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.core.test;
|
|
26 |
|
|
27 |
import java.io.BufferedInputStream;
|
|
28 |
import java.io.ByteArrayInputStream;
|
|
29 |
import java.io.FileInputStream;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.InputStream;
|
|
32 |
|
46640
|
33 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
34 |
import org.graalvm.compiler.debug.TTY;
|
|
35 |
import org.graalvm.compiler.graph.Node;
|
|
36 |
import org.graalvm.compiler.nodeinfo.Verbosity;
|
|
37 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
38 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
39 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
40 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
41 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
|
42 |
import org.graalvm.compiler.nodes.cfg.Block;
|
|
43 |
import org.graalvm.compiler.nodes.java.InstanceOfNode;
|
|
44 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
46459
|
45 |
import org.graalvm.compiler.phases.common.ConditionalEliminationPhase;
|
43972
|
46 |
import org.graalvm.compiler.phases.schedule.SchedulePhase;
|
46459
|
47 |
import org.junit.Assert;
|
|
48 |
import org.junit.Ignore;
|
|
49 |
import org.junit.Test;
|
43972
|
50 |
|
|
51 |
/**
|
|
52 |
* In the following tests, the scalar type system of the compiler should be complete enough to see
|
|
53 |
* the relation between the different conditions.
|
|
54 |
*/
|
|
55 |
public class TypeSystemTest extends GraalCompilerTest {
|
|
56 |
|
|
57 |
@Test
|
|
58 |
public void test3() {
|
|
59 |
test("test3Snippet", "referenceSnippet3");
|
|
60 |
}
|
|
61 |
|
|
62 |
public static int referenceSnippet3(Object o) {
|
|
63 |
if (o == null) {
|
|
64 |
return 1;
|
|
65 |
} else {
|
|
66 |
return 2;
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
@SuppressWarnings("unused")
|
|
71 |
public static int test3Snippet(Object o) {
|
|
72 |
if (o == null) {
|
|
73 |
if (o != null) {
|
|
74 |
return 3;
|
|
75 |
} else {
|
|
76 |
return 1;
|
|
77 |
}
|
|
78 |
} else {
|
|
79 |
return 2;
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
@Test
|
|
84 |
public void test4() {
|
|
85 |
test("test4Snippet", "referenceSnippet3");
|
|
86 |
}
|
|
87 |
|
|
88 |
@SuppressWarnings("unused")
|
|
89 |
public static int test4Snippet(Object o) {
|
|
90 |
if (o == null) {
|
|
91 |
Object o2 = Integer.class;
|
|
92 |
if (o == o2) {
|
|
93 |
return 3;
|
|
94 |
} else {
|
|
95 |
return 1;
|
|
96 |
}
|
|
97 |
} else {
|
|
98 |
return 2;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
@Test
|
|
103 |
@Ignore
|
|
104 |
public void test5() {
|
|
105 |
test("test5Snippet", "referenceSnippet5");
|
|
106 |
}
|
|
107 |
|
|
108 |
public static int referenceSnippet5(Object o, Object a) {
|
|
109 |
if (o == null) {
|
|
110 |
if (a == Integer.class) {
|
|
111 |
return 1;
|
|
112 |
}
|
|
113 |
} else {
|
|
114 |
if (a == Double.class) {
|
|
115 |
return 11;
|
|
116 |
}
|
|
117 |
}
|
|
118 |
if (a == Integer.class) {
|
|
119 |
return 3;
|
|
120 |
}
|
|
121 |
return 5;
|
|
122 |
}
|
|
123 |
|
|
124 |
@SuppressWarnings("unused")
|
|
125 |
public static int test5Snippet(Object o, Object a) {
|
|
126 |
if (o == null) {
|
|
127 |
if (a == Integer.class) {
|
|
128 |
if (a == null) {
|
|
129 |
return 10;
|
|
130 |
}
|
|
131 |
return 1;
|
|
132 |
}
|
|
133 |
} else {
|
|
134 |
if (a == Double.class) {
|
|
135 |
if (a != null) {
|
|
136 |
return 11;
|
|
137 |
}
|
|
138 |
return 2;
|
|
139 |
}
|
|
140 |
}
|
|
141 |
if (a == Integer.class) {
|
|
142 |
return 3;
|
|
143 |
}
|
|
144 |
return 5;
|
|
145 |
}
|
|
146 |
|
|
147 |
@Test
|
|
148 |
public void test6() {
|
|
149 |
testHelper("test6Snippet", InstanceOfNode.class);
|
|
150 |
}
|
|
151 |
|
|
152 |
public static int test6Snippet(int i) throws IOException {
|
|
153 |
Object o = null;
|
|
154 |
|
|
155 |
if (i == 5) {
|
|
156 |
o = new FileInputStream("asdf");
|
|
157 |
}
|
|
158 |
if (i < 10) {
|
|
159 |
o = new ByteArrayInputStream(new byte[]{1, 2, 3});
|
|
160 |
}
|
|
161 |
if (i > 0) {
|
|
162 |
o = new BufferedInputStream(null);
|
|
163 |
}
|
|
164 |
|
|
165 |
return ((InputStream) o).available();
|
|
166 |
}
|
|
167 |
|
|
168 |
@Test
|
|
169 |
public void test7() {
|
|
170 |
test("test7Snippet", "referenceSnippet7");
|
|
171 |
}
|
|
172 |
|
|
173 |
public static int test7Snippet(int x) {
|
|
174 |
return ((x & 0xff) << 10) == ((x & 0x1f) + 1) ? 0 : x;
|
|
175 |
}
|
|
176 |
|
|
177 |
public static int referenceSnippet7(int x) {
|
|
178 |
return x;
|
|
179 |
}
|
|
180 |
|
|
181 |
private void test(String snippet, String referenceSnippet) {
|
|
182 |
StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
|
46640
|
183 |
DebugContext debug = graph.getDebug();
|
|
184 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
|
43972
|
185 |
/*
|
|
186 |
* When using FlowSensitiveReductionPhase instead of ConditionalEliminationPhase,
|
|
187 |
* tail-duplication gets activated thus resulting in a graph with more nodes than the
|
|
188 |
* reference graph.
|
|
189 |
*/
|
55509
|
190 |
new ConditionalEliminationPhase(false).apply(graph, getProviders());
|
|
191 |
new CanonicalizerPhase().apply(graph, getProviders());
|
43972
|
192 |
// a second canonicalizer is needed to process nested MaterializeNodes
|
55509
|
193 |
new CanonicalizerPhase().apply(graph, getProviders());
|
43972
|
194 |
StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.NO);
|
55509
|
195 |
new ConditionalEliminationPhase(false).apply(referenceGraph, getProviders());
|
|
196 |
new CanonicalizerPhase().apply(referenceGraph, getProviders());
|
|
197 |
new CanonicalizerPhase().apply(referenceGraph, getProviders());
|
43972
|
198 |
assertEquals(referenceGraph, graph);
|
|
199 |
}
|
|
200 |
|
|
201 |
@Override
|
|
202 |
protected void assertEquals(StructuredGraph expected, StructuredGraph graph) {
|
46640
|
203 |
DebugContext debug = graph.getDebug();
|
43972
|
204 |
if (getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) {
|
46640
|
205 |
debug.dump(DebugContext.BASIC_LEVEL, expected, "expected (node count)");
|
|
206 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "graph (node count)");
|
43972
|
207 |
Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount());
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
public static void outputGraph(StructuredGraph graph, String message) {
|
|
212 |
TTY.println("========================= " + message);
|
46344
|
213 |
SchedulePhase schedulePhase = new SchedulePhase(graph.getOptions());
|
43972
|
214 |
schedulePhase.apply(graph);
|
|
215 |
ScheduleResult schedule = graph.getLastSchedule();
|
|
216 |
for (Block block : schedule.getCFG().getBlocks()) {
|
|
217 |
TTY.print("Block " + block + " ");
|
|
218 |
if (block == schedule.getCFG().getStartBlock()) {
|
|
219 |
TTY.print("* ");
|
|
220 |
}
|
|
221 |
TTY.print("-> ");
|
|
222 |
for (Block succ : block.getSuccessors()) {
|
|
223 |
TTY.print(succ + " ");
|
|
224 |
}
|
|
225 |
TTY.println();
|
|
226 |
for (Node node : schedule.getBlockToNodesMap().get(block)) {
|
|
227 |
outputNode(node);
|
|
228 |
}
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
private static void outputNode(Node node) {
|
|
233 |
TTY.print(" " + node + " (usage count: " + node.getUsageCount() + ") (inputs:");
|
|
234 |
for (Node input : node.inputs()) {
|
|
235 |
TTY.print(" " + input.toString(Verbosity.Id));
|
|
236 |
}
|
|
237 |
TTY.println(")");
|
|
238 |
if (node instanceof AbstractMergeNode) {
|
|
239 |
for (PhiNode phi : ((AbstractMergeNode) node).phis()) {
|
|
240 |
outputNode(phi);
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
private <T extends Node> void testHelper(String snippet, Class<T> clazz) {
|
|
246 |
StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
|
55509
|
247 |
new CanonicalizerPhase().apply(graph, getProviders());
|
|
248 |
new CanonicalizerPhase().apply(graph, getProviders());
|
46640
|
249 |
DebugContext debug = graph.getDebug();
|
|
250 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph " + snippet);
|
43972
|
251 |
Assert.assertFalse("shouldn't have nodes of type " + clazz, graph.getNodes().filter(clazz).iterator().hasNext());
|
|
252 |
}
|
|
253 |
}
|