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