1 /*
2  * Copyright (c) 2015, Red Hat, Inc. 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  * @test
26  * @bug 8144028
27  * @summary Use AArch64 bit-test instructions in C2
28  *
29  * @run main/othervm -Xbatch -XX:-TieredCompilation
30  *      -XX:CompileCommand=dontinline,compiler.codegen.BitTests::*
31  *      compiler.codegen.BitTests
32  * @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1
33  *      compiler.codegen.BitTests
34  * @run main/othervm -Xbatch -XX:+TieredCompilation
35  *      compiler.codegen.BitTests
36  */
37 
38 package compiler.codegen;
39 
40 // Try to ensure that the bit test instructions TBZ/TBNZ, TST/TSTW
41 // don't generate incorrect code.  We can't guarantee that C2 will use
42 // bit test instructions for this test and it's not a bug if it
43 // doesn't.  However, these test cases are ideal candidates for each
44 // of the instruction forms.
45 public class BitTests {
46 
47     private final XorShift r = new XorShift();
48 
increment(long ctr)49     private final long increment(long ctr) {
50         return ctr + 1;
51     }
52 
increment(int ctr)53     private final int increment(int ctr) {
54         return ctr + 1;
55     }
56 
testIntSignedBranch(long counter)57     private final long testIntSignedBranch(long counter) {
58         if ((int) r.nextLong() < 0) {
59             counter = increment(counter);
60         }
61         return counter;
62     }
63 
testLongSignedBranch(long counter)64     private final long testLongSignedBranch(long counter) {
65         if (r.nextLong() < 0) {
66             counter = increment(counter);
67         }
68         return counter;
69     }
70 
testIntBitBranch(long counter)71     private final long testIntBitBranch(long counter) {
72         if (((int) r.nextLong() & (1 << 27)) != 0) {
73             counter = increment(counter);
74         }
75         if (((int) r.nextLong() & (1 << 27)) != 0) {
76             counter = increment(counter);
77         }
78         return counter;
79     }
80 
testLongBitBranch(long counter)81     private final long testLongBitBranch(long counter) {
82         if ((r.nextLong() & (1l << 50)) != 0) {
83             counter = increment(counter);
84         }
85         if ((r.nextLong() & (1l << 50)) != 0) {
86             counter = increment(counter);
87         }
88         return counter;
89     }
90 
testLongMaskBranch(long counter)91     private final long testLongMaskBranch(long counter) {
92         if (((r.nextLong() & 0x0800000000l) != 0)) {
93             counter++;
94         }
95         return counter;
96     }
97 
testIntMaskBranch(long counter)98     private final long testIntMaskBranch(long counter) {
99         if ((((int) r.nextLong() & 0x08) != 0)) {
100             counter++;
101         }
102         return counter;
103     }
104 
testLongMaskBranch(long counter, long mask)105     private final long testLongMaskBranch(long counter, long mask) {
106         if (((r.nextLong() & mask) != 0)) {
107             counter++;
108         }
109         return counter;
110     }
111 
testIntMaskBranch(long counter, int mask)112     private final long testIntMaskBranch(long counter, int mask) {
113         if ((((int) r.nextLong() & mask) != 0)) {
114             counter++;
115         }
116         return counter;
117     }
118 
step(long counter)119     private final long step(long counter) {
120         counter = testIntSignedBranch(counter);
121         counter = testLongSignedBranch(counter);
122         counter = testIntBitBranch(counter);
123         counter = testLongBitBranch(counter);
124         counter = testIntMaskBranch(counter);
125         counter = testLongMaskBranch(counter);
126         counter = testIntMaskBranch(counter, 0x8000);
127         counter = testLongMaskBranch(counter, 0x800000000l);
128         return counter;
129     }
130 
131 
132     private final long finalBits = 3;
133 
134     private long bits = 7;
135 
main(String[] args)136     public static void main(String[] args) {
137         BitTests t = new BitTests();
138 
139         long counter = 0;
140         for (int i = 0; i < 10000000; i++) {
141             counter = t.step((int) counter);
142         }
143         if (counter != 50001495) {
144             System.err.println("FAILED: counter = " + counter + ", should be 50001495.");
145             System.exit(97);
146         }
147         System.out.println("PASSED");
148     }
149 
150     // Marsaglia's xor-shift generator, used here because it is
151     // reproducible across all Java implementations.  It is also very
152     // fast.
153     static class XorShift {
154 
155         private long y;
156 
XorShift()157         XorShift() {
158             y = 2463534242l;
159         }
160 
nextLong()161         public long nextLong() {
162             y ^= (y << 13);
163             y ^= (y >>> 17);
164             return (y ^= (y << 5));
165 
166         }
167     }
168 }
169