1 /*
   2  * Copyright (c) 2014, 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 TestIntegerComparison
  26  * @bug 8043284 8042786
  27  * @summary Tests optimizations of signed and unsigned integer comparison.
  28  *
  29  * @run main/othervm -Xcomp
  30  *      -XX:CompileCommand=compileonly,compiler.integerArithmetic.TestIntegerComparison::testSigned
  31  *      -XX:CompileCommand=compileonly,compiler.integerArithmetic.TestIntegerComparison::testUnsigned
  32  *      compiler.integerArithmetic.TestIntegerComparison
  33  */
  34 package compiler.integerArithmetic;
  35 
  36 public class TestIntegerComparison {
  37     /**
  38      * Tests optimization of signed integer comparison (see BoolNode::Ideal).
  39      * The body of the if statement is unreachable and should not be compiled.
  40      *
  41      * @param c Character (value in the integer range [0, 65535])
  42      */
  43     public static void testSigned(char c) {
  44         // The following addition may overflow. The result is in one
  45         // of the two ranges [IntMax] and [IntMin, IntMin + CharMax - 1].
  46         int result = c + Integer.MAX_VALUE;
  47         // CmpINode has to consider both result ranges instead of only
  48         // the general [IntMin, IntMax] range to be able to prove that
  49         // result is always unequal to CharMax.
  50         if (result == Character.MAX_VALUE) {
  51             // Unreachable
  52             throw new RuntimeException("Should not reach here!");
  53         }
  54     }
  55 
  56     /**
  57      * Tests optimization of unsigned integer comparison (see CmpUNode::Value).
  58      * The body of the if statement is unreachable and should not be compiled.
  59      *
  60      * @param c Character (value in the integer range [0, 65535])
  61      */
  62     public static void testUnsigned(char c) {
  63     /*
  64      * The following if statement consisting of two CmpIs is replaced
  65      * by a CmpU during optimization (see 'IfNode::fold_compares').
  66      *
  67      * The signed (lo < i) and (i < hi) are replaced by the unsigned
  68      * (i - (lo+1) < hi - (lo+1)). In this case the unsigned comparison
  69      * equals (result - 2) < 98 leading to the following CmpUNode:
  70      *
  71      * CmpU (AddI result, -2) 98
  72      *
  73      * With the value of result this is simplified to:
  74      *
  75      * CmpU (AddI c, -(CharMax - IntMin)) 98
  76      *
  77      * The subtraction may underflow. The result is in one of the two
  78      * ranges [IntMin], [IntMax - CharMax + 1]. Both ranges have to be
  79      * considered instead of only the general [IntMin, IntMax] to prove
  80      * that due to the overflow the signed comparison result < 98 is
  81      * always false.
  82      */
  83         int result = c - (Character.MAX_VALUE - Integer.MIN_VALUE) + 2;
  84         if (1 < result && result < 100) {
  85             // Unreachable
  86             throw new RuntimeException("Should not reach here!");
  87         }
  88     }
  89 
  90     /**
  91      * Tests optimizations of signed and unsigned integer comparison.
  92      */
  93     public static void main(String[] args) {
  94         // We use characters to get a limited integer range for free
  95         for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) {
  96             testSigned((char) i);
  97             testUnsigned((char) i);
  98         }
  99     }
 100 }