aoqi@0: /* aoqi@0: * Copyright (c) 2011 Hewlett-Packard Company. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 5091921 aoqi@0: * @summary Sign flip issues in loop optimizer aoqi@0: * aoqi@0: * @run main/othervm -Xcomp -XX:CompileOnly=Test5091921 -XX:MaxInlineSize=1 Test5091921 aoqi@0: */ aoqi@0: aoqi@0: public class Test5091921 { aoqi@0: private static int result = 0; aoqi@0: aoqi@0: aoqi@0: /* Test for the bug of transforming indx >= MININT to indx > MININT-1 */ aoqi@0: public static int test_ge1(int limit) { aoqi@0: int indx; aoqi@0: int sum = 0; aoqi@0: for (indx = 500; indx >= limit; indx -= 2) { aoqi@0: sum += 2000 / indx; aoqi@0: result = sum; aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: /* Test for the bug of transforming indx <= MAXINT to indx < MAXINT+1 */ aoqi@0: public static int test_le1(int limit) { aoqi@0: int indx; aoqi@0: int sum = 0; aoqi@0: for (indx = -500; indx <= limit; indx += 2) aoqi@0: { aoqi@0: sum += 3000 / indx; aoqi@0: result = sum; aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: /* Run with -Xcomp -XX:CompileOnly=wrap1.test1 -XX:MaxInlineSize=1 */ aoqi@0: /* limit reset to ((limit-init+stride-1)/stride)*stride+init */ aoqi@0: /* Calculation may overflow */ aoqi@0: public static volatile int c = 1; aoqi@0: public static int test_wrap1(int limit) aoqi@0: { aoqi@0: int indx; aoqi@0: int sum = 0; aoqi@0: for (indx = 0xffffffff; indx < limit; indx += 0x20000000) aoqi@0: { aoqi@0: sum += c; aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: /* Test for range check elimination with bit flip issue for aoqi@0: scale*i+offset 0x80000000) aoqi@0: { aoqi@0: // this test is not issued in pre-loop but issued in main loop aoqi@0: // trick rce into thinking expression is false when indx >= 0 aoqi@0: // in fact it is false when indx==0x80000001 aoqi@0: if (indx - 9 < -9) aoqi@0: { aoqi@0: sum += indx; aoqi@0: result = sum; aoqi@0: sum ^= b[indx & 7]; aoqi@0: result = sum; aoqi@0: } aoqi@0: else aoqi@0: break; aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: sum += b[indx & 3]; aoqi@0: result = sum; aoqi@0: } aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: /* Test for range check elimination with bit flip issue for aoqi@0: scale*i 1 */ aoqi@0: static int[] box6 = {1,2,3,4,5,6,7,8,9}; aoqi@0: public static int test_rce6(int[] b, int limit) aoqi@0: { aoqi@0: int indx; aoqi@0: int sum = b[1]; aoqi@0: result = sum; aoqi@0: for (indx = 0x80000000; indx < limit; ++indx) aoqi@0: { aoqi@0: if (indx > 0x80000000) aoqi@0: { aoqi@0: // harmless rce target aoqi@0: if (indx < 0) aoqi@0: { aoqi@0: sum += result; aoqi@0: result = sum; aoqi@0: } aoqi@0: else aoqi@0: break; aoqi@0: // this test is not issued in pre-loop but issued in main loop aoqi@0: // trick rce into thinking expression is false when indx >= 0 aoqi@0: // in fact it is false when indx==0x80000001 aoqi@0: // In compilers that transform mulI to shiftI may mask this issue. aoqi@0: if (indx * 28 + 1 < 0) aoqi@0: { aoqi@0: sum += indx; aoqi@0: result = sum; aoqi@0: sum ^= b[indx & 7]; aoqi@0: result = sum; aoqi@0: } aoqi@0: else aoqi@0: break; aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: sum += b[indx & 3]; aoqi@0: result = sum; aoqi@0: } aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: /* Test for range check elimination with i <= limit */ aoqi@0: static int[] box7 = {1,2,3,4,5,6,7,8,9,0x7fffffff}; aoqi@0: public static int test_rce7(int[] b) aoqi@0: { aoqi@0: int indx; aoqi@0: int max = b[9]; aoqi@0: int sum = b[7]; aoqi@0: result = sum; aoqi@0: for (indx = 0; indx < b.length; ++indx) aoqi@0: { aoqi@0: if (indx <= max) aoqi@0: { aoqi@0: sum += (indx ^ 15) + ((result != 0) ? 0 : sum); aoqi@0: result = sum; aoqi@0: } aoqi@0: else aoqi@0: throw new RuntimeException(); aoqi@0: } aoqi@0: for (indx = -7; indx < b.length; ++indx) aoqi@0: { aoqi@0: if (indx <= 9) aoqi@0: { aoqi@0: sum += (sum ^ 15) + ((result != 0) ? 0 : sum); aoqi@0: result = sum; aoqi@0: } aoqi@0: else aoqi@0: throw new RuntimeException(); aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: /* Test for range check elimination with i >= limit */ aoqi@0: static int[] box8 = {-1,0,1,2,3,4,5,6,7,8,0x80000000}; aoqi@0: public static int test_rce8(int[] b) aoqi@0: { aoqi@0: int indx; aoqi@0: int sum = b[5]; aoqi@0: int min = b[10]; aoqi@0: result = sum; aoqi@0: for (indx = b.length-1; indx >= 0; --indx) aoqi@0: { aoqi@0: if (indx >= min) aoqi@0: { aoqi@0: sum += (sum ^ 9) + ((result != 0) ? 0 :sum); aoqi@0: result = sum; aoqi@0: } aoqi@0: else aoqi@0: throw new RuntimeException(); aoqi@0: } aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: public static void main(String[] args) aoqi@0: { aoqi@0: result=1; aoqi@0: int r = 0; aoqi@0: try { aoqi@0: r = test_ge1(0x80000000); aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_ge1 FAILED"); aoqi@0: System.exit(1); aoqi@0: } aoqi@0: catch (ArithmeticException e1) { aoqi@0: System.out.println("test_ge1: Expected exception caught"); aoqi@0: if (result != 5986) { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_ge1 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: } aoqi@0: System.out.println("test_ge1 WORKED"); aoqi@0: aoqi@0: result=0; aoqi@0: try aoqi@0: { aoqi@0: r = test_le1(0x7fffffff); aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_le1 FAILED"); aoqi@0: System.exit(1); aoqi@0: } aoqi@0: catch (ArithmeticException e1) aoqi@0: { aoqi@0: System.out.println("test_le1: Expected exception caught"); aoqi@0: if (result != -9039) aoqi@0: { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_le1 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: } aoqi@0: System.out.println("test_le1 WORKED"); aoqi@0: aoqi@0: result=0; aoqi@0: r = test_wrap1(0x7fffffff); aoqi@0: if (r != 4) aoqi@0: { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_wrap1 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: System.out.println("test_wrap1 WORKED"); aoqi@0: } aoqi@0: aoqi@0: result=0; aoqi@0: r = test_rce5(box5,0x80000100); aoqi@0: if (result != 3) aoqi@0: { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_rce5 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: System.out.println("test_rce5 WORKED"); aoqi@0: } aoqi@0: aoqi@0: result=0; aoqi@0: r = test_rce6(box6,0x80000100); aoqi@0: if (result != 6) aoqi@0: { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_rce6 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: System.out.println("test_rce6 WORKED"); aoqi@0: } aoqi@0: aoqi@0: result=0; aoqi@0: r = test_rce7(box7); aoqi@0: if (result != 14680079) aoqi@0: { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_rce7 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: System.out.println("test_rce7 WORKED"); aoqi@0: } aoqi@0: aoqi@0: result=0; aoqi@0: r = test_rce8(box8); aoqi@0: if (result != 16393) aoqi@0: { aoqi@0: System.out.println(result); aoqi@0: System.out.println("test_rce8 FAILED"); aoqi@0: System.exit(97); aoqi@0: } aoqi@0: else aoqi@0: { aoqi@0: System.out.println("test_rce8 WORKED"); aoqi@0: } aoqi@0: } aoqi@0: }