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