ConditionalEliminationTest13.java revision 12995:5e441a7ec5e3
1/*
2 * Copyright (c) 2015, 2017, 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 */
23package org.graalvm.compiler.core.test;
24
25import org.graalvm.compiler.debug.Debug;
26import org.graalvm.compiler.nodes.StructuredGraph;
27import org.graalvm.compiler.nodes.ValueNode;
28import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
29import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
30import org.graalvm.compiler.phases.common.CanonicalizerPhase;
31import org.graalvm.compiler.phases.tiers.PhaseContext;
32import org.junit.Test;
33
34import jdk.vm.ci.meta.ResolvedJavaMethod;
35
36public class ConditionalEliminationTest13 extends ConditionalEliminationTestBase {
37
38    @Override
39    protected InlineInvokePlugin.InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
40        return InlineInvokePlugin.InlineInfo.createStandardInlineInfo(method);
41    }
42
43    public static void referenceSnippet1(int a) {
44        if (Integer.compareUnsigned(a, a + 1) < 0) {
45            sink1 = 0;
46        } else {
47            sink0 = -1;
48        }
49    }
50
51    public static void testSnippet1(int a) {
52        if (Integer.compareUnsigned(a, a + 1) < 0 || a == 0) {
53            sink1 = 0;
54        } else {
55            sink0 = -1;
56        }
57    }
58
59    public static void referenceSnippet2(int a) {
60        if (0 < a) {
61            sink1 = 0;
62        }
63        sink0 = -1;
64    }
65
66    public static void testSnippet2(int a) {
67        if (0 < a) {
68            if (a == -1) {
69                sink2 = -2;
70            }
71            sink1 = 0;
72        }
73        sink0 = -1;
74    }
75
76    public static void testSnippet3(int a) {
77        if (0 < a) {
78            if (a == 1) {
79                sink2 = -2;
80            }
81            sink1 = 0;
82        }
83        sink0 = -1;
84    }
85
86    @SuppressWarnings("unused")
87    public static void referenceSnippet4(int a) {
88        sink1 = 0;
89    }
90
91    public static void testSnippet4(int a) {
92        if (Integer.compareUnsigned(a - 1, a) < 0 || a == 0) {
93            sink1 = 0;
94        } else {
95            sink0 = -1;
96        }
97    }
98
99    public static void testSnippet5(int a) {
100        if (a < 0) {
101            if (a == -1) {
102                sink2 = -2;
103            }
104            sink1 = 0;
105        }
106        sink0 = -1;
107    }
108
109    public static void referenceSnippet6(int a) {
110        if (a < 0) {
111            sink1 = 0;
112        }
113        sink0 = -1;
114    }
115
116    public static void testSnippet6(int a) {
117        if (a < 0) {
118            if (a == 0) {
119                sink2 = -2;
120            }
121            sink1 = 0;
122        }
123        sink0 = -1;
124    }
125
126    public static void testSnippet7(int a) {
127        if (0 < a) {
128            if (a == 0) {
129                sink2 = -2;
130            }
131            sink1 = 0;
132        }
133        sink0 = -1;
134    }
135
136    public static void testSnippet8(int a) {
137        if (Integer.compareUnsigned(a, a + 1) < 0 || a == 0xffff_ffff) {
138            sink1 = 0;
139        } else {
140            sink0 = -1;
141        }
142    }
143
144    public static void referenceSnippet9(int a) {
145        if (Integer.compareUnsigned(a - 1, a) < 0) {
146            sink1 = 0;
147        } else {
148            sink0 = -1;
149        }
150    }
151
152    public static void testSnippet9(int a) {
153        if (Integer.compareUnsigned(a - 1, a) < 0 || a == 0xffff_ffff) {
154            sink1 = 0;
155        } else {
156            sink0 = -1;
157        }
158    }
159
160    private static int either(int a, int b) {
161        return (sink0 + sink1 + sink2) == 0 ? a : b;
162    }
163
164    public static void testSnippet10(int a) {
165        if (Integer.compareUnsigned(a, a + either(1, 2)) < 0 || a == 0xffff_ffff || a == 0xffff_fffe) {
166            sink1 = 0;
167        } else {
168            sink0 = -1;
169        }
170    }
171
172    public static void referenceSnippet11(int a) {
173        if (Integer.compareUnsigned(a, Integer.MAX_VALUE + 1) > 0) {
174            sink1 = 0;
175        }
176        sink0 = -1;
177    }
178
179    public static void testSnippet11(int a) {
180        if (Integer.compareUnsigned(a, Integer.MAX_VALUE + 1) > 0) {
181            if (Integer.compareUnsigned(a, 42) <= 0) {
182                sink2 = -2;
183            }
184            sink1 = 0;
185        }
186        sink0 = -1;
187    }
188
189    public static void referenceSnippet12(int a) {
190        if (Integer.compareUnsigned(a, 0xffff_ffff) >= 0) {
191            sink1 = 0;
192        } else {
193            sink0 = -1;
194        }
195    }
196
197    public static void testSnippet12(int a) {
198        if (Integer.compareUnsigned(a, 0xffff_ffff) >= 0 && a == 0xffff_ffff) {
199            sink1 = 0;
200        } else {
201            sink0 = -1;
202        }
203    }
204
205    public static void testSnippet13(int a) {
206        int x = either(0, 1);
207        if (a <= a + x) {
208            if (a == Integer.MAX_VALUE) {
209                sink2 = -2;
210            }
211            sink1 = 0;
212        } else {
213            sink0 = -1;
214        }
215    }
216
217    public static void referenceSnippet14(int a) {
218        int x = either(0, 1);
219        if (a < a + x) {
220            sink1 = 0;
221        } else {
222            sink0 = -1;
223        }
224    }
225
226    public static void testSnippet14(int a) {
227        int x = either(0, 1);
228        if (a < a + x) {
229            if (a == Integer.MAX_VALUE) {
230                sink2 = -2;
231            }
232            sink1 = 0;
233        } else {
234            sink0 = -1;
235        }
236    }
237
238    @Test
239    public void test1() {
240        testConditionalElimination("testSnippet1", "referenceSnippet1");
241    }
242
243    @Test
244    public void test2() {
245        testConditionalElimination("testSnippet2", "referenceSnippet2");
246    }
247
248    @Test
249    public void test3() {
250        testConditionalElimination("testSnippet3", "testSnippet3");
251    }
252
253    @Test
254    public void test4() {
255        testConditionalElimination("testSnippet4", "referenceSnippet4");
256    }
257
258    @Test
259    public void test5() {
260        testConditionalElimination("testSnippet5", "testSnippet5");
261    }
262
263    @Test
264    public void test6() {
265        testConditionalElimination("testSnippet6", "referenceSnippet6");
266    }
267
268    @Test
269    public void test7() {
270        testConditionalElimination("testSnippet7", "referenceSnippet2");
271    }
272
273    @Test
274    public void test8() {
275        testConditionalElimination("testSnippet8", "referenceSnippet4");
276    }
277
278    @Test
279    public void test9() {
280        testConditionalElimination("testSnippet9", "referenceSnippet9");
281    }
282
283    @Test
284    public void test10() {
285        testConditionalElimination("testSnippet10", "referenceSnippet4");
286    }
287
288    @Test
289    public void test11() {
290        testConditionalElimination("testSnippet11", "referenceSnippet11");
291    }
292
293    @Test
294    public void test12() {
295        testConditionalElimination("testSnippet12", "referenceSnippet12");
296    }
297
298    @Test
299    public void test13() {
300        testConditionalElimination("testSnippet13", "testSnippet13");
301    }
302
303    @Test
304    public void test14() {
305        testConditionalElimination("testSnippet14", "referenceSnippet14");
306    }
307
308    @Override
309    protected void prepareGraph(StructuredGraph graph, CanonicalizerPhase canonicalizer, PhaseContext context, boolean applyLowering) {
310        super.prepareGraph(graph, canonicalizer, context, applyLowering);
311        graph.clearAllStateAfter();
312        graph.setGuardsStage(StructuredGraph.GuardsStage.AFTER_FSA);
313        Debug.dump(Debug.BASIC_LEVEL, graph, "After preparation");
314        canonicalizer.apply(graph, context);
315    }
316}
317