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  *
  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 8003585
  28  * @summary strength reduce or eliminate range checks for power-of-two sized arrays
  29  *
  30  * @run main/othervm -XX:CompileCommand=compileonly,compiler.rangechecks.PowerOf2SizedArraysChecks::test*
  31  *                   -XX:-BackgroundCompilation
  32  *                   compiler.rangechecks.PowerOf2SizedArraysChecks
  33  */
  34 
  35 package compiler.rangechecks;
  36 
  37 import java.util.function.BiFunction;
  38 import java.util.function.Function;
  39 
  40 public class PowerOf2SizedArraysChecks {
  41 
  42     static void check_result(String name, int x, int m, boolean expected, boolean res) {
  43         if (expected != res) {
  44             throw new RuntimeException("Bad result in " + name + " for x =  " + x + " m = " + m + " expected " + expected  + " got " + res);
  45         }
  46     }
  47 
  48     static void helper(String name, BiFunction<Integer, int[], Boolean> test, int[] x_values, int[] m_values, boolean[][] expected) {
  49         for (int i = 0; i < x_values.length; i++) {
  50             int x = x_values[i];
  51             for (int j = 0; j < m_values.length; j++) {
  52                 int m = m_values[j];
  53                 int[] array = new int[m];
  54                 boolean res = test.apply(x, array);
  55                 check_result(name, x, m, expected[i][j], res);
  56             }
  57         }
  58     }
  59 
  60     static void check_result(String name, int m, boolean expected, boolean res) {
  61         if (expected != res) {
  62             throw new RuntimeException("Bad result in " + name + " for m = " + m + " expected " + expected  + " got " + res);
  63         }
  64     }
  65 
  66     static void helper2(String name, Function<int[], Boolean> test, int[] m_values, boolean[] expected) {
  67         for (int j = 0; j < m_values.length; j++) {
  68             int m = m_values[j];
  69             int[] array = new int[m];
  70             boolean res = test.apply(array);
  71             check_result(name, m, expected[j], res);
  72         }
  73     }
  74 
  75     // ((x & m) u<= m) is always true
  76     static boolean test1(int x, int[] array) {
  77         int m = array.length;
  78         if ((x & m) < 0 || (x & m) > m) {
  79             return false;
  80         }
  81         return true;
  82     }
  83 
  84     // ((x & (m - 1)) u< m) iff (m > 0)
  85     static boolean test2(int x, int[] array) {
  86         int m = array.length;
  87         if ((x & (m-1)) < 0 || (x & (m-1)) >= m) {
  88             return false;
  89         }
  90         return true;
  91     }
  92 
  93     static boolean test3(int x, int[] array) {
  94         try {
  95             int v = array[x & (array.length-1)];
  96         } catch(ArrayIndexOutOfBoundsException aioobe) {
  97             return false;
  98         }
  99         return true;
 100     }
 101 
 102     // arraylength <= 0 to arraylength u<= 0
 103     static boolean test4(int[] array) {
 104         if (array.length <= 0) {
 105             return false;
 106         }
 107         return true;
 108     }
 109 
 110     // arraylength == 0 to arraylength u<= 0
 111     static boolean test5(int[] array) {
 112         if (array.length == 0) {
 113             return false;
 114         }
 115         return true;
 116     }
 117 
 118     // arraylength != 0 to arraylength u> 0
 119     static boolean test6(int[] array) {
 120         if (array.length != 0) {
 121             return false;
 122         }
 123         return true;
 124     }
 125 
 126     static public void main(String[] args) {
 127         int[] x_values = {-10, -5, 0, 5, 8, 16, 100};
 128         int[] m_values = { 16, 10, 0 };
 129 
 130         boolean[][] test1_expected = new boolean[x_values.length][m_values.length];
 131         for (int i = 0; i < x_values.length; i++) {
 132             for (int j = 0; j < m_values.length; j++) {
 133                 test1_expected[i][j] = true;
 134             }
 135         }
 136 
 137         boolean[][] test2_expected = new boolean[x_values.length][m_values.length];
 138         for (int i = 0; i < x_values.length; i++) {
 139             for (int j = 0; j < m_values.length; j++) {
 140                 test2_expected[i][j] = (m_values[j] > 0);
 141             }
 142         }
 143 
 144         boolean[] test4_expected = new boolean[m_values.length];
 145         for (int i = 0; i < m_values.length; i++) {
 146             test4_expected[i] = (m_values[i] > 0);
 147         }
 148         boolean[] test5_expected = new boolean[m_values.length];
 149         for (int i = 0; i < m_values.length; i++) {
 150             test5_expected[i] = (m_values[i] != 0);
 151         }
 152         boolean[] test6_expected = new boolean[m_values.length];
 153         for (int i = 0; i < m_values.length; i++) {
 154             test6_expected[i] = (m_values[i] == 0);
 155         }
 156 
 157         for (int i = 0; i < 20000; i++) {
 158             helper("test1", PowerOf2SizedArraysChecks::test1, x_values, m_values, test1_expected);
 159             helper("test2", PowerOf2SizedArraysChecks::test2, x_values, m_values, test2_expected);
 160             helper("test3", PowerOf2SizedArraysChecks::test3, x_values, m_values, test2_expected);
 161             helper2("test4", PowerOf2SizedArraysChecks::test4, m_values, test4_expected);
 162             helper2("test5", PowerOf2SizedArraysChecks::test5, m_values, test5_expected);
 163             helper2("test6", PowerOf2SizedArraysChecks::test6, m_values, test6_expected);
 164         }
 165     }
 166 }