1/*
2 * Copyright (c) 2015, 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.api.directives.GraalDirectives;
26import org.junit.Ignore;
27import org.junit.Test;
28
29/**
30 * Collection of tests for {@link org.graalvm.compiler.phases.common.ConditionalEliminationPhase}
31 * including those that triggered bugs in this phase.
32 */
33public class ConditionalEliminationTest11 extends ConditionalEliminationTestBase {
34    @SuppressWarnings("all")
35    public static int referenceSnippet(int a) {
36        if ((a & 15) != 15) {
37            GraalDirectives.deoptimizeAndInvalidate();
38        }
39        return 0;
40    }
41
42    @Test
43    public void test1() {
44        testConditionalElimination("test1Snippet", "referenceSnippet");
45    }
46
47    @SuppressWarnings("all")
48    public static int test1Snippet(int a) {
49        if ((a & 8) != 8) {
50            GraalDirectives.deoptimizeAndInvalidate();
51        }
52        if ((a & 15) != 15) {
53            GraalDirectives.deoptimizeAndInvalidate();
54        }
55        return 0;
56    }
57
58    @SuppressWarnings("all")
59    public static int test2Snippet(int a) {
60        if ((a & 8) == 0) {
61            GraalDirectives.deoptimizeAndInvalidate();
62        }
63        if ((a & 15) != 15) {
64            GraalDirectives.deoptimizeAndInvalidate();
65        }
66        return 0;
67    }
68
69    @Test
70    public void test2() {
71        testConditionalElimination("test2Snippet", "referenceSnippet");
72    }
73
74    @SuppressWarnings("all")
75    public static int test3Snippet(int a) {
76        if ((a & 15) != 15) {
77            GraalDirectives.deoptimizeAndInvalidate();
78        }
79        if ((a & 8) != 8) {
80            GraalDirectives.deoptimizeAndInvalidate();
81        }
82        return 0;
83    }
84
85    @Test
86    public void test3() {
87        // Test forward elimination of bitwise tests
88        testConditionalElimination("test3Snippet", "referenceSnippet");
89    }
90
91    @SuppressWarnings("all")
92    public static int test4Snippet(int a) {
93        if ((a & 15) != 15) {
94            GraalDirectives.deoptimizeAndInvalidate();
95        }
96        if ((a & 8) == 0) {
97            GraalDirectives.deoptimizeAndInvalidate();
98        }
99        return 0;
100    }
101
102    @Test
103    public void test4() {
104        // Test forward elimination of bitwise tests
105        testConditionalElimination("test4Snippet", "referenceSnippet");
106    }
107
108    public static int test5Snippet(int a) {
109        if ((a & 5) == 5) {
110            GraalDirectives.deoptimizeAndInvalidate();
111        }
112        if ((a & 7) != 0) {
113            return 0;
114        }
115        return 1;
116    }
117
118    @Test
119    public void test5() {
120        // Shouldn't be possible to optimize this
121        testConditionalElimination("test5Snippet", "test5Snippet");
122    }
123
124    public static int test6Snippet(int a) {
125        if ((a & 8) != 0) {
126            GraalDirectives.deoptimizeAndInvalidate();
127        }
128        if ((a & 15) != 15) {
129            GraalDirectives.deoptimizeAndInvalidate();
130        }
131        return 0;
132    }
133
134    public static int reference6Snippet(int a) {
135        if ((a & 8) != 0) {
136            GraalDirectives.deoptimizeAndInvalidate();
137        }
138        GraalDirectives.deoptimizeAndInvalidate();
139        return 0;
140    }
141
142    @Test
143    public void test6() {
144        testConditionalElimination("test6Snippet", "reference6Snippet");
145    }
146
147    public static int test7Snippet(int a) {
148        if ((a & 15) == 15) {
149            GraalDirectives.deoptimizeAndInvalidate();
150        }
151        if ((a & 8) == 8) {
152            GraalDirectives.deoptimizeAndInvalidate();
153        }
154        return a;
155    }
156
157    public static int reference7Snippet(int a) {
158        if ((a & 8) == 8) {
159            GraalDirectives.deoptimizeAndInvalidate();
160        }
161        return a;
162    }
163
164    @Test
165    public void test7() {
166        testConditionalElimination("test7Snippet", "reference7Snippet");
167    }
168
169    public static int test8Snippet(int a) {
170        if ((a & 16) == 16) {
171            GraalDirectives.deoptimizeAndInvalidate();
172        }
173        if ((a & 8) != 8) {
174            GraalDirectives.deoptimizeAndInvalidate();
175        }
176        if ((a & 44) != 44) {
177            GraalDirectives.deoptimizeAndInvalidate();
178        }
179        return a;
180    }
181
182    public static int reference8Snippet(int a) {
183        if ((a & 60) != 44) {
184            GraalDirectives.deoptimizeAndInvalidate();
185        }
186        return a;
187    }
188
189    @Ignore("requires merging of bit tests")
190    @Test
191    public void test8() {
192        testConditionalElimination("test8Snippet", "reference8Snippet");
193    }
194
195    public static int test9Snippet(int a) {
196        if ((a & 16) == 16) {
197            GraalDirectives.deoptimizeAndInvalidate();
198        }
199        if ((a & 8) != 8) {
200            GraalDirectives.deoptimizeAndInvalidate();
201        }
202        if ((a & 44) != 44) {
203            GraalDirectives.deoptimizeAndInvalidate();
204        }
205        if (a != 44) {
206            GraalDirectives.deoptimizeAndInvalidate();
207        }
208        return a;
209    }
210
211    public static int reference9Snippet(int a) {
212        if (a != 44) {
213            GraalDirectives.deoptimizeAndInvalidate();
214        }
215        return a;
216    }
217
218    @Test
219    public void test9() {
220        testConditionalElimination("test9Snippet", "reference9Snippet");
221    }
222
223    static class ByteHolder {
224        public byte b;
225
226        byte byteValue() {
227            return b;
228        }
229    }
230
231    public static int test10Snippet(ByteHolder b) {
232        int v = b.byteValue();
233        long a = v & 0xffffffff;
234        if (v != 44) {
235            GraalDirectives.deoptimizeAndInvalidate();
236        }
237        if ((a & 16) == 16) {
238            GraalDirectives.deoptimizeAndInvalidate();
239        }
240        if ((a & 8) != 8) {
241            GraalDirectives.deoptimizeAndInvalidate();
242        }
243        if ((a & 44) != 44) {
244            GraalDirectives.deoptimizeAndInvalidate();
245        }
246
247        return v;
248    }
249
250    public static int reference10Snippet(ByteHolder b) {
251        byte v = b.byteValue();
252        if (v != 44) {
253            GraalDirectives.deoptimizeAndInvalidate();
254        }
255        return v;
256    }
257
258    @Test
259    @Ignore
260    public void test10() {
261        testConditionalElimination("test10Snippet", "reference10Snippet");
262    }
263
264    public static int test11Snippet(ByteHolder b) {
265        int v = b.byteValue();
266        long a = v & 0xffffffff;
267
268        if ((a & 16) == 16) {
269            GraalDirectives.deoptimizeAndInvalidate();
270        }
271        if ((a & 8) != 8) {
272            GraalDirectives.deoptimizeAndInvalidate();
273        }
274        if ((a & 44) != 44) {
275            GraalDirectives.deoptimizeAndInvalidate();
276        }
277        if (v != 44) {
278            GraalDirectives.deoptimizeAndInvalidate();
279        }
280        return v;
281    }
282
283    public static int reference11Snippet(ByteHolder b) {
284        byte v = b.byteValue();
285        if (v != 44) {
286            GraalDirectives.deoptimizeAndInvalidate();
287        }
288        return v;
289    }
290
291    @Test
292    @Ignore
293    public void test11() {
294        testConditionalElimination("test11Snippet", "reference11Snippet");
295    }
296
297}
298