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