Path: blob/master/test/hotspot/jtreg/compiler/integerArithmetic/TestIntegerComparison.java
41152 views
/*1* Copyright (c) 2014, 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* @test TestIntegerComparison25* @bug 8043284 804278626* @summary Tests optimizations of signed and unsigned integer comparison.27*28* @run main/othervm -Xcomp29* -XX:CompileCommand=compileonly,compiler.integerArithmetic.TestIntegerComparison::testSigned30* -XX:CompileCommand=compileonly,compiler.integerArithmetic.TestIntegerComparison::testUnsigned31* compiler.integerArithmetic.TestIntegerComparison32*/33package compiler.integerArithmetic;3435public class TestIntegerComparison {36/**37* Tests optimization of signed integer comparison (see BoolNode::Ideal).38* The body of the if statement is unreachable and should not be compiled.39*40* @param c Character (value in the integer range [0, 65535])41*/42public static void testSigned(char c) {43// The following addition may overflow. The result is in one44// of the two ranges [IntMax] and [IntMin, IntMin + CharMax - 1].45int result = c + Integer.MAX_VALUE;46// CmpINode has to consider both result ranges instead of only47// the general [IntMin, IntMax] range to be able to prove that48// result is always unequal to CharMax.49if (result == Character.MAX_VALUE) {50// Unreachable51throw new RuntimeException("Should not reach here!");52}53}5455/**56* Tests optimization of unsigned integer comparison (see CmpUNode::Value).57* The body of the if statement is unreachable and should not be compiled.58*59* @param c Character (value in the integer range [0, 65535])60*/61public static void testUnsigned(char c) {62/*63* The following if statement consisting of two CmpIs is replaced64* by a CmpU during optimization (see 'IfNode::fold_compares').65*66* The signed (lo < i) and (i < hi) are replaced by the unsigned67* (i - (lo+1) < hi - (lo+1)). In this case the unsigned comparison68* equals (result - 2) < 98 leading to the following CmpUNode:69*70* CmpU (AddI result, -2) 9871*72* With the value of result this is simplified to:73*74* CmpU (AddI c, -(CharMax - IntMin)) 9875*76* The subtraction may underflow. The result is in one of the two77* ranges [IntMin], [IntMax - CharMax + 1]. Both ranges have to be78* considered instead of only the general [IntMin, IntMax] to prove79* that due to the overflow the signed comparison result < 98 is80* always false.81*/82int result = c - (Character.MAX_VALUE - Integer.MIN_VALUE) + 2;83if (1 < result && result < 100) {84// Unreachable85throw new RuntimeException("Should not reach here!");86}87}8889/**90* Tests optimizations of signed and unsigned integer comparison.91*/92public static void main(String[] args) {93// We use characters to get a limited integer range for free94for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) {95testSigned((char) i);96testUnsigned((char) i);97}98}99}100101102