1 /*
2  * Copyright (c) 2014, 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  * @test
26  * @bug 8031321
27  * @summary Support BMI1 instructions on x86/x64
28  * @run main/othervm -Xbatch -XX:-TieredCompilation -XX:CompileCommand=compileonly,BMITests.* BMI1
29  *
30  */
31 
32 class MemI {
33   public int x;
MemI(int x)34   public MemI(int x) { this.x = x; }
35 }
36 
37 class MemL {
38   public long x;
MemL(long x)39   public MemL(long x) { this.x = x; }
40 }
41 
42 class BMITests {
andnl(int src1, int src2)43   static int andnl(int src1, int src2) {
44     return ~src1 & src2;
45   }
andnq(long src1, long src2)46   static long andnq(long src1, long src2) {
47     return ~src1 & src2;
48   }
andnl(int src1, MemI src2)49   static int andnl(int src1, MemI src2) {
50     return ~src1 & src2.x;
51   }
andnq(long src1, MemL src2)52   static long andnq(long src1, MemL src2) {
53     return ~src1 & src2.x;
54   }
blsil(int src1)55   static int blsil(int src1) {
56     return src1 & -src1;
57   }
blsiq(long src1)58   static long blsiq(long src1) {
59     return src1 & -src1;
60   }
blsil(MemI src1)61   static int blsil(MemI src1) {
62     return src1.x & -src1.x;
63   }
blsiq(MemL src1)64   static long blsiq(MemL src1) {
65     return src1.x & -src1.x;
66   }
blsmskl(int src1)67   static int blsmskl(int src1) {
68     return (src1 - 1) ^ src1;
69   }
blsmskq(long src1)70   static long blsmskq(long src1) {
71     return (src1 - 1) ^ src1;
72   }
blsmskl(MemI src1)73   static int blsmskl(MemI src1) {
74     return (src1.x - 1) ^ src1.x;
75   }
blsmskq(MemL src1)76   static long blsmskq(MemL src1) {
77     return (src1.x - 1) ^ src1.x;
78   }
blsrl(int src1)79   static int blsrl(int src1) {
80     return (src1 - 1) & src1;
81   }
blsrq(long src1)82   static long blsrq(long src1) {
83     return (src1 - 1) & src1;
84   }
blsrl(MemI src1)85   static int blsrl(MemI src1) {
86     return (src1.x - 1) & src1.x;
87   }
blsrq(MemL src1)88   static long blsrq(MemL src1) {
89     return (src1.x - 1) & src1.x;
90   }
lzcntl(int src1)91   static int lzcntl(int src1) {
92     return Integer.numberOfLeadingZeros(src1);
93   }
lzcntq(long src1)94   static int lzcntq(long src1) {
95     return Long.numberOfLeadingZeros(src1);
96   }
tzcntl(int src1)97   static int tzcntl(int src1) {
98     return Integer.numberOfTrailingZeros(src1);
99   }
tzcntq(long src1)100   static int tzcntq(long src1) {
101     return Long.numberOfTrailingZeros(src1);
102   }
103 }
104 
105 public class BMI1 {
106   private final static int ITERATIONS = 1000000;
107 
main(String[] args)108   public static void main(String[] args) {
109     int ix = 0x01234567;
110     int iy = 0x89abcdef;
111     MemI imy = new MemI(iy);
112     long lx = 0x0123456701234567L;
113     long ly = 0x89abcdef89abcdefL;
114     MemL lmy = new MemL(ly);
115 
116     { // match(Set dst (AndI (XorI src1 minus_1) src2))
117       int z = BMITests.andnl(ix, iy);
118       for (int i = 0; i < ITERATIONS; i++) {
119         int ii = BMITests.andnl(ix, iy);
120         if (ii != z) {
121           throw new Error("andnl with register failed");
122         }
123       }
124     }
125     { // match(Set dst (AndL (XorL src1 minus_1) src2))
126       long z = BMITests.andnq(lx, ly);
127       for (int i = 0; i < ITERATIONS; i++) {
128         long ll = BMITests.andnq(lx, ly);
129         if (ll != z) {
130           throw new Error("andnq with register failed");
131         }
132       }
133     }
134     { // match(Set dst (AndI (XorI src1 minus_1) (LoadI src2)))
135       int z = BMITests.andnl(ix, imy);
136       for (int i = 0; i < ITERATIONS; i++) {
137         int ii = BMITests.andnl(ix, imy);
138         if (ii != z) {
139           throw new Error("andnl with memory failed");
140         }
141       }
142     }
143     { // match(Set dst (AndL (XorL src1 minus_1) (LoadL src2)))
144       long z = BMITests.andnq(lx, lmy);
145       for (int i = 0; i < ITERATIONS; i++) {
146         long ll = BMITests.andnq(lx, lmy);
147         if (ll != z) {
148           throw new Error("andnq with memory failed");
149         }
150       }
151     }
152     { // match(Set dst (AndI (SubI imm_zero src) src))
153       int z = BMITests.blsil(ix);
154       for (int i = 0; i < ITERATIONS; i++) {
155         int ii = BMITests.blsil(ix);
156         if (ii != z) {
157           throw new Error("blsil with register failed");
158         }
159       }
160     }
161     { // match(Set dst (AndL (SubL imm_zero src) src))
162       long z = BMITests.blsiq(lx);
163       for (int i = 0; i < ITERATIONS; i++) {
164         long ll = BMITests.blsiq(lx);
165         if (ll != z) {
166           throw new Error("blsiq with register failed");
167         }
168       }
169     }
170     { // match(Set dst (AndI (SubI imm_zero (LoadI src) ) (LoadI src) ))
171       int z = BMITests.blsil(imy);
172       for (int i = 0; i < ITERATIONS; i++) {
173         int ii = BMITests.blsil(imy);
174         if (ii != z) {
175           throw new Error("blsil with memory failed");
176         }
177       }
178     }
179     { // match(Set dst (AndL (SubL imm_zero (LoadL src) ) (LoadL src) ))
180       long z = BMITests.blsiq(lmy);
181       for (int i = 0; i < ITERATIONS; i++) {
182         long ll = BMITests.blsiq(lmy);
183         if (ll != z) {
184           throw new Error("blsiq with memory failed");
185         }
186       }
187     }
188 
189     { // match(Set dst (XorI (AddI src minus_1) src))
190       int z = BMITests.blsmskl(ix);
191       for (int i = 0; i < ITERATIONS; i++) {
192         int ii = BMITests.blsmskl(ix);
193         if (ii != z) {
194           throw new Error("blsmskl with register failed");
195         }
196       }
197     }
198     { // match(Set dst (XorL (AddL src minus_1) src))
199       long z = BMITests.blsmskq(lx);
200       for (int i = 0; i < ITERATIONS; i++) {
201         long ll = BMITests.blsmskq(lx);
202         if (ll != z) {
203           throw new Error("blsmskq with register failed");
204         }
205       }
206     }
207     { // match(Set dst (XorI (AddI (LoadI src) minus_1) (LoadI src) ) )
208       int z = BMITests.blsmskl(imy);
209       for (int i = 0; i < ITERATIONS; i++) {
210         int ii = BMITests.blsmskl(imy);
211         if (ii != z) {
212           throw new Error("blsmskl with memory failed");
213         }
214       }
215     }
216     {  // match(Set dst (XorL (AddL (LoadL src) minus_1) (LoadL src) ) )
217       long z = BMITests.blsmskq(lmy);
218       for (int i = 0; i < ITERATIONS; i++) {
219         long ll = BMITests.blsmskq(lmy);
220         if (ll != z) {
221           throw new Error("blsmskq with memory failed");
222         }
223       }
224     }
225 
226     { //  match(Set dst (AndI (AddI src minus_1) src) )
227       int z = BMITests.blsrl(ix);
228       for (int i = 0; i < ITERATIONS; i++) {
229         int ii = BMITests.blsrl(ix);
230         if (ii != z) {
231           throw new Error("blsrl with register failed");
232         }
233       }
234     }
235     { // match(Set dst (AndL (AddL src minus_1) src) )
236       long z = BMITests.blsrq(lx);
237       for (int i = 0; i < ITERATIONS; i++) {
238         long ll = BMITests.blsrq(lx);
239         if (ll != z) {
240           throw new Error("blsrq with register failed");
241         }
242       }
243     }
244     { // match(Set dst (AndI (AddI (LoadI src) minus_1) (LoadI src) ) )
245       int z = BMITests.blsrl(imy);
246       for (int i = 0; i < ITERATIONS; i++) {
247         int ii = BMITests.blsrl(imy);
248         if (ii != z) {
249           throw new Error("blsrl with memory failed");
250         }
251       }
252     }
253     { // match(Set dst (AndL (AddL (LoadL src) minus_1) (LoadL src)) )
254       long z = BMITests.blsrq(lmy);
255       for (int i = 0; i < ITERATIONS; i++) {
256         long ll = BMITests.blsrq(lmy);
257         if (ll != z) {
258           throw new Error("blsrq with memory failed");
259         }
260       }
261     }
262 
263     {
264       int z = BMITests.lzcntl(ix);
265       for (int i = 0; i < ITERATIONS; i++) {
266         int ii = BMITests.lzcntl(ix);
267         if (ii != z) {
268           throw new Error("lzcntl failed");
269         }
270       }
271     }
272     {
273       int z = BMITests.lzcntq(lx);
274       for (int i = 0; i < ITERATIONS; i++) {
275         int ii = BMITests.lzcntq(lx);
276         if (ii != z) {
277           throw new Error("lzcntq failed");
278         }
279       }
280     }
281 
282     {
283       int z = BMITests.tzcntl(ix);
284       for (int i = 0; i < ITERATIONS; i++) {
285         int ii = BMITests.tzcntl(ix);
286         if (ii != z) {
287           throw new Error("tzcntl failed");
288         }
289       }
290     }
291     {
292       int z = BMITests.tzcntq(lx);
293       for (int i = 0; i < ITERATIONS; i++) {
294         int ii = BMITests.tzcntq(lx);
295         if (ii != z) {
296           throw new Error("tzcntq failed");
297         }
298       }
299     }
300   }
301 }
302