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 |
|
46640
|
27 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
28 |
import org.graalvm.compiler.graph.Node;
|
|
29 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
30 |
import org.graalvm.compiler.nodes.FrameState;
|
|
31 |
import org.graalvm.compiler.nodes.IfNode;
|
|
32 |
import org.graalvm.compiler.nodes.ParameterNode;
|
|
33 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
34 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
55509
|
35 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
43972
|
36 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
37 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
38 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
39 |
import org.graalvm.compiler.phases.common.FloatingReadPhase;
|
|
40 |
import org.graalvm.compiler.phases.common.GuardLoweringPhase;
|
|
41 |
import org.graalvm.compiler.phases.common.LoweringPhase;
|
|
42 |
import org.graalvm.compiler.phases.tiers.MidTierContext;
|
46640
|
43 |
import org.junit.Test;
|
43972
|
44 |
|
|
45 |
/**
|
|
46 |
* In the following tests, the usages of local variable "a" are replaced with the integer constant
|
|
47 |
* 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
|
|
48 |
* graph of the method that just has a "return 1" statement in it.
|
|
49 |
*/
|
|
50 |
public class IfCanonicalizerTest extends GraalCompilerTest {
|
|
51 |
|
|
52 |
private static final String REFERENCE_SNIPPET = "referenceSnippet";
|
|
53 |
|
|
54 |
@SuppressWarnings("all")
|
|
55 |
public static int referenceSnippet(int a) {
|
|
56 |
return 1;
|
|
57 |
}
|
|
58 |
|
|
59 |
@Test
|
|
60 |
public void test1() {
|
|
61 |
test("test1Snippet");
|
|
62 |
}
|
|
63 |
|
|
64 |
@SuppressWarnings("all")
|
|
65 |
public static int test1Snippet(int a) {
|
|
66 |
if (a == 0) {
|
|
67 |
return 1;
|
|
68 |
} else {
|
|
69 |
return 2;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
@Test
|
|
74 |
public void test2() {
|
|
75 |
test("test2Snippet");
|
|
76 |
}
|
|
77 |
|
|
78 |
@SuppressWarnings("all")
|
|
79 |
public static int test2Snippet(int a) {
|
|
80 |
if (a == 0) {
|
|
81 |
if (a == 0) {
|
|
82 |
if (a == 0) {
|
|
83 |
return 1;
|
|
84 |
}
|
|
85 |
}
|
|
86 |
} else {
|
|
87 |
return 2;
|
|
88 |
}
|
|
89 |
return 3;
|
|
90 |
}
|
|
91 |
|
|
92 |
@Test
|
|
93 |
public void test3() {
|
|
94 |
test("test3Snippet");
|
|
95 |
}
|
|
96 |
|
|
97 |
@SuppressWarnings("all")
|
|
98 |
public static int test3Snippet(int a) {
|
|
99 |
if (a == 0) {
|
|
100 |
if (a != 1) {
|
|
101 |
if (a == 1) {
|
|
102 |
return 3;
|
|
103 |
} else {
|
|
104 |
if (a >= 0) {
|
|
105 |
if (a <= 0) {
|
|
106 |
if (a > -1) {
|
|
107 |
if (a < 1) {
|
|
108 |
return 1;
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
} else {
|
|
116 |
return 2;
|
|
117 |
}
|
|
118 |
return 3;
|
|
119 |
}
|
|
120 |
|
|
121 |
@Test
|
|
122 |
public void test4() {
|
|
123 |
test("test4Snippet");
|
|
124 |
}
|
|
125 |
|
|
126 |
public static int test4Snippet(int a) {
|
|
127 |
if (a == 0) {
|
|
128 |
return 1;
|
|
129 |
}
|
|
130 |
return 1;
|
|
131 |
}
|
|
132 |
|
|
133 |
@Test
|
|
134 |
public void test5() {
|
|
135 |
test("test5Snippet");
|
|
136 |
}
|
|
137 |
|
|
138 |
public static int test5Snippet(int a) {
|
|
139 |
int val = 2;
|
|
140 |
if (a == 0) {
|
|
141 |
val = 1;
|
|
142 |
}
|
|
143 |
if (a * (3 + val) == 0) {
|
|
144 |
return 1;
|
|
145 |
}
|
|
146 |
return 1;
|
|
147 |
}
|
|
148 |
|
|
149 |
@Test
|
|
150 |
public void test6() {
|
46344
|
151 |
testCombinedIf("test6Snippet", 4);
|
43972
|
152 |
test("test6Snippet", new int[]{0});
|
|
153 |
}
|
|
154 |
|
|
155 |
public static int test6Snippet(int[] a) {
|
|
156 |
int i = a[0];
|
|
157 |
if (i >= 0 && i < a.length) {
|
|
158 |
return a[i];
|
|
159 |
}
|
|
160 |
return 1;
|
|
161 |
}
|
|
162 |
|
|
163 |
@Test
|
|
164 |
public void test7() {
|
|
165 |
testCombinedIf("test7Snippet", 1);
|
|
166 |
test("test7Snippet", -1);
|
|
167 |
}
|
|
168 |
|
|
169 |
public static int test7Snippet(int v) {
|
|
170 |
if (v >= 0 && v < 1024) {
|
|
171 |
return v + 1;
|
|
172 |
}
|
|
173 |
return v - 1;
|
|
174 |
}
|
|
175 |
|
|
176 |
@Test
|
|
177 |
public void test8() {
|
|
178 |
testCombinedIf("test8Snippet", 1);
|
|
179 |
test("test8Snippet", -1);
|
|
180 |
}
|
|
181 |
|
|
182 |
public static int test8Snippet(int v) {
|
|
183 |
if (v >= 0 && v <= 1024) {
|
|
184 |
return v + 1;
|
|
185 |
}
|
|
186 |
return v - 1;
|
|
187 |
}
|
|
188 |
|
|
189 |
@Test
|
|
190 |
public void test9() {
|
|
191 |
testCombinedIf("test9Snippet", 2);
|
|
192 |
test("test9Snippet", -1);
|
|
193 |
test("test9Snippet", 1025);
|
|
194 |
}
|
|
195 |
|
|
196 |
public static int test9Snippet(int n) {
|
|
197 |
return (n < 0) ? 1 : (n >= 1024) ? 1024 : n + 1;
|
|
198 |
}
|
|
199 |
|
46459
|
200 |
@Test
|
|
201 |
public void test10() {
|
|
202 |
// Exercise NormalizeCompareNode with long values
|
|
203 |
test("test10Snippet", 0, 1);
|
|
204 |
}
|
|
205 |
|
|
206 |
public static long test10Snippet(int x, int y) {
|
|
207 |
return (x < y) ? -1L : ((x == y) ? 0L : 1L);
|
|
208 |
}
|
|
209 |
|
|
210 |
@Test
|
|
211 |
public void test11() {
|
|
212 |
test("test11Snippet", 0, 1);
|
|
213 |
}
|
|
214 |
|
|
215 |
public static long test11Snippet(int x, int y) {
|
|
216 |
long normalizeCompare = normalizeCompareLong(x, y);
|
|
217 |
if (normalizeCompare == 0) {
|
|
218 |
return 5;
|
|
219 |
}
|
|
220 |
return 1;
|
|
221 |
}
|
|
222 |
|
|
223 |
private static Long normalizeCompareLong(int x, int y) {
|
|
224 |
return (x < y) ? -1L : ((x == y) ? 0L : 1L);
|
|
225 |
}
|
|
226 |
|
43972
|
227 |
private void testCombinedIf(String snippet, int count) {
|
|
228 |
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
|
55509
|
229 |
CoreProviders context = getProviders();
|
43972
|
230 |
new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
|
|
231 |
new FloatingReadPhase().apply(graph);
|
|
232 |
MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
|
|
233 |
new GuardLoweringPhase().apply(graph, midContext);
|
|
234 |
new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, midContext);
|
|
235 |
new CanonicalizerPhase().apply(graph, context);
|
|
236 |
assertDeepEquals(count, graph.getNodes().filter(IfNode.class).count());
|
|
237 |
}
|
|
238 |
|
|
239 |
private void test(String snippet) {
|
|
240 |
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
|
46640
|
241 |
DebugContext debug = graph.getDebug();
|
43972
|
242 |
ParameterNode param = graph.getNodes(ParameterNode.TYPE).iterator().next();
|
|
243 |
ConstantNode constant = ConstantNode.forInt(0, graph);
|
|
244 |
for (Node n : param.usages().snapshot()) {
|
|
245 |
if (!(n instanceof FrameState)) {
|
|
246 |
n.replaceFirstInput(param, constant);
|
|
247 |
}
|
|
248 |
}
|
46640
|
249 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
|
55509
|
250 |
new CanonicalizerPhase().apply(graph, getProviders());
|
43972
|
251 |
for (FrameState fs : param.usages().filter(FrameState.class).snapshot()) {
|
|
252 |
fs.replaceFirstInput(param, null);
|
|
253 |
param.safeDelete();
|
|
254 |
}
|
|
255 |
StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
|
|
256 |
assertEquals(referenceGraph, graph);
|
|
257 |
}
|
|
258 |
}
|