1 /*
   2  * Copyright (c) 2009, 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  */
  23 
  24 /**
  25  * @test
  26  * @bug 5057225
  27  * @summary Remove useless I2L conversions
  28  * @modules java.base/jdk.internal.misc
  29  * @library /testlibrary
  30  * @run main/othervm -Xcomp -XX:CompileOnly=Test5057225.doload Test5057225
  31  */
  32 
  33 import jdk.test.lib.Utils;
  34 
  35 public class Test5057225 {
  36     static byte[]  ba = new byte[]  { -1 };
  37     static short[] sa = new short[] { -1 };
  38     static int[]   ia = new int[]   { -1 };
  39 
  40     static final long[] BYTE_MASKS = {
  41          0x0FL,
  42          0x7FL,  // 7-bit
  43          0xFFL,
  44     };
  45 
  46     static final long[] SHORT_MASKS = {
  47         0x000FL,
  48         0x007FL,  // 7-bit
  49         0x00FFL,
  50         0x0FFFL,
  51         0x3FFFL,  // 14-bit
  52         0x7FFFL,  // 15-bit
  53         0xFFFFL,
  54     };
  55 
  56     static final long[] INT_MASKS = {
  57         0x0000000FL,
  58         0x0000007FL,  // 7-bit
  59         0x000000FFL,
  60         0x00000FFFL,
  61         0x00003FFFL,  // 14-bit
  62         0x00007FFFL,  // 15-bit
  63         0x0000FFFFL,
  64         0x00FFFFFFL,
  65         0x7FFFFFFFL,  // 31-bit
  66         0xFFFFFFFFL,
  67     };
  68 
  69     public static void main(String[] args) throws Exception {
  70         for (int i = 0; i < BYTE_MASKS.length; i++) {
  71             System.setProperty("value", "" + BYTE_MASKS[i]);
  72             loadAndRunClass("Test5057225$loadUB2L");
  73         }
  74 
  75         for (int i = 0; i < SHORT_MASKS.length; i++) {
  76             System.setProperty("value", "" + SHORT_MASKS[i]);
  77             loadAndRunClass("Test5057225$loadUS2L");
  78         }
  79 
  80         for (int i = 0; i < INT_MASKS.length; i++) {
  81             System.setProperty("value", "" + INT_MASKS[i]);
  82             loadAndRunClass("Test5057225$loadUI2L");
  83         }
  84     }
  85 
  86     static void check(long result, long expected) {
  87         if (result != expected)
  88             throw new InternalError(result + " != " + expected);
  89     }
  90 
  91     static void loadAndRunClass(String classname) throws Exception {
  92         Class cl = Class.forName(classname);
  93         ClassLoader apploader = cl.getClassLoader();
  94         ClassLoader loader
  95                 = Utils.getTestClassPathURLClassLoader(apploader.getParent());
  96         Class c = loader.loadClass(classname);
  97         Runnable r = (Runnable) c.newInstance();
  98         r.run();
  99     }
 100 
 101     public static class loadUB2L implements Runnable {
 102         static final long MASK;
 103         static {
 104             long value = 0;
 105             try {
 106                 value = Long.decode(System.getProperty("value"));
 107             } catch (Throwable e) {}
 108             MASK = value;
 109         }
 110 
 111         public void run() { check(doload(ba), MASK); }
 112         static long doload(byte[] ba) { return ba[0] & MASK; }
 113     }
 114 
 115     public static class loadUS2L implements Runnable {
 116         static final long MASK;
 117         static {
 118             long value = 0;
 119             try {
 120                 value = Long.decode(System.getProperty("value"));
 121             } catch (Throwable e) {}
 122             MASK = value;
 123         }
 124 
 125         public void run() { check(doload(sa), MASK); }
 126         static long doload(short[] sa) { return sa[0] & MASK; }
 127     }
 128 
 129     public static class loadUI2L implements Runnable {
 130         static final long MASK;
 131         static {
 132             long value = 0;
 133             try {
 134                 value = Long.decode(System.getProperty("value"));
 135             } catch (Throwable e) {}
 136             MASK = value;
 137         }
 138 
 139         public void run() { check(doload(ia), MASK); }
 140         static long doload(int[] ia) { return ia[0] & MASK; }
 141     }
 142 }