Path: blob/master/test/hotspot/jtreg/compiler/rangechecks/PowerOf2SizedArraysChecks.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 800358526* @summary strength reduce or eliminate range checks for power-of-two sized arrays27*28* @run main/othervm -XX:CompileCommand=compileonly,compiler.rangechecks.PowerOf2SizedArraysChecks::test*29* -XX:-BackgroundCompilation30* compiler.rangechecks.PowerOf2SizedArraysChecks31*/3233package compiler.rangechecks;3435import java.util.function.BiFunction;36import java.util.function.Function;3738public class PowerOf2SizedArraysChecks {3940static void check_result(String name, int x, int m, boolean expected, boolean res) {41if (expected != res) {42throw new RuntimeException("Bad result in " + name + " for x = " + x + " m = " + m + " expected " + expected + " got " + res);43}44}4546static void helper(String name, BiFunction<Integer, int[], Boolean> test, int[] x_values, int[] m_values, boolean[][] expected) {47for (int i = 0; i < x_values.length; i++) {48int x = x_values[i];49for (int j = 0; j < m_values.length; j++) {50int m = m_values[j];51int[] array = new int[m];52boolean res = test.apply(x, array);53check_result(name, x, m, expected[i][j], res);54}55}56}5758static void check_result(String name, int m, boolean expected, boolean res) {59if (expected != res) {60throw new RuntimeException("Bad result in " + name + " for m = " + m + " expected " + expected + " got " + res);61}62}6364static void helper2(String name, Function<int[], Boolean> test, int[] m_values, boolean[] expected) {65for (int j = 0; j < m_values.length; j++) {66int m = m_values[j];67int[] array = new int[m];68boolean res = test.apply(array);69check_result(name, m, expected[j], res);70}71}7273// ((x & m) u<= m) is always true74static boolean test1(int x, int[] array) {75int m = array.length;76if ((x & m) < 0 || (x & m) > m) {77return false;78}79return true;80}8182// ((x & (m - 1)) u< m) iff (m > 0)83static boolean test2(int x, int[] array) {84int m = array.length;85if ((x & (m-1)) < 0 || (x & (m-1)) >= m) {86return false;87}88return true;89}9091static boolean test3(int x, int[] array) {92try {93int v = array[x & (array.length-1)];94} catch(ArrayIndexOutOfBoundsException aioobe) {95return false;96}97return true;98}99100// arraylength <= 0 to arraylength u<= 0101static boolean test4(int[] array) {102if (array.length <= 0) {103return false;104}105return true;106}107108// arraylength == 0 to arraylength u<= 0109static boolean test5(int[] array) {110if (array.length == 0) {111return false;112}113return true;114}115116// arraylength != 0 to arraylength u> 0117static boolean test6(int[] array) {118if (array.length != 0) {119return false;120}121return true;122}123124static public void main(String[] args) {125int[] x_values = {-10, -5, 0, 5, 8, 16, 100};126int[] m_values = { 16, 10, 0 };127128boolean[][] test1_expected = new boolean[x_values.length][m_values.length];129for (int i = 0; i < x_values.length; i++) {130for (int j = 0; j < m_values.length; j++) {131test1_expected[i][j] = true;132}133}134135boolean[][] test2_expected = new boolean[x_values.length][m_values.length];136for (int i = 0; i < x_values.length; i++) {137for (int j = 0; j < m_values.length; j++) {138test2_expected[i][j] = (m_values[j] > 0);139}140}141142boolean[] test4_expected = new boolean[m_values.length];143for (int i = 0; i < m_values.length; i++) {144test4_expected[i] = (m_values[i] > 0);145}146boolean[] test5_expected = new boolean[m_values.length];147for (int i = 0; i < m_values.length; i++) {148test5_expected[i] = (m_values[i] != 0);149}150boolean[] test6_expected = new boolean[m_values.length];151for (int i = 0; i < m_values.length; i++) {152test6_expected[i] = (m_values[i] == 0);153}154155for (int i = 0; i < 20000; i++) {156helper("test1", PowerOf2SizedArraysChecks::test1, x_values, m_values, test1_expected);157helper("test2", PowerOf2SizedArraysChecks::test2, x_values, m_values, test2_expected);158helper("test3", PowerOf2SizedArraysChecks::test3, x_values, m_values, test2_expected);159helper2("test4", PowerOf2SizedArraysChecks::test4, m_values, test4_expected);160helper2("test5", PowerOf2SizedArraysChecks::test5, m_values, test5_expected);161helper2("test6", PowerOf2SizedArraysChecks::test6, m_values, test6_expected);162}163}164}165166167