hotspot/test/compiler/integerArithmetic/TestIntegerComparison.java
author alanb
Tue, 03 May 2016 09:11:31 +0100
changeset 37827 09fa0c82a5f7
parent 27699 9913b19c0948
child 40059 c2304140ed64
permissions -rw-r--r--
8154956: Module system implementation refresh (4/2016) Reviewed-by: alanb, mchung, chegar, redestad Contributed-by: alan.bateman@oracle.com, erik.joelsson@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26165
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     1
/*
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     4
 *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     8
 *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    13
 * accompanied this code).
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    14
 *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    18
 *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    21
 * questions.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    22
 */
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    23
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    24
/*
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    25
 * @test TestIntegerComparison
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    26
 * @bug 8043284 8042786
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    27
 * @summary "Tests optimizations of signed and unsigned integer comparison."
26916
8a1deee4f9b4 8038098: [TESTBUG] remove explicit set build flavor from hotspot/test/compiler/* tests
vlivanov
parents: 26165
diff changeset
    28
 * @run main/othervm -Xcomp -XX:CompileOnly=TestIntegerComparison::testSigned,TestIntegerComparison::testUnsigned TestIntegerComparison
26165
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    29
 */
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    30
public class TestIntegerComparison {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    31
  /**
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    32
   * Tests optimization of signed integer comparison (see BoolNode::Ideal).
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    33
   * The body of the if statement is unreachable and should not be compiled.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    34
   * @param c Character (value in the integer range [0, 65535])
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    35
   */
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    36
  public static void testSigned(char c) {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    37
    // The following addition may overflow. The result is in one
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    38
    // of the two ranges [IntMax] and [IntMin, IntMin + CharMax - 1].
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    39
    int result = c + Integer.MAX_VALUE;
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    40
    // CmpINode has to consider both result ranges instead of only
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    41
    // the general [IntMin, IntMax] range to be able to prove that
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    42
    // result is always unequal to CharMax.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    43
    if (result == Character.MAX_VALUE) {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    44
      // Unreachable
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    45
      throw new RuntimeException("Should not reach here!");
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    46
    }
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    47
  }
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    48
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    49
  /**
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    50
   * Tests optimization of unsigned integer comparison (see CmpUNode::Value).
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    51
   * The body of the if statement is unreachable and should not be compiled.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    52
   * @param c Character (value in the integer range [0, 65535])
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    53
   */
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    54
  public static void testUnsigned(char c) {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    55
    /*
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    56
     * The following if statement consisting of two CmpIs is replaced
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    57
     * by a CmpU during optimization (see 'IfNode::fold_compares').
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    58
     *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    59
     * The signed (lo < i) and (i < hi) are replaced by the unsigned
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    60
     * (i - (lo+1) < hi - (lo+1)). In this case the unsigned comparison
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    61
     * equals (result - 2) < 98 leading to the following CmpUNode:
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    62
     *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    63
     * CmpU (AddI result, -2) 98
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    64
     *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    65
     * With the value of result this is simplified to:
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    66
     *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    67
     * CmpU (AddI c, -(CharMax - IntMin)) 98
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    68
     *
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    69
     * The subtraction may underflow. The result is in one of the two
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    70
     * ranges [IntMin], [IntMax - CharMax + 1]. Both ranges have to be
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    71
     * considered instead of only the general [IntMin, IntMax] to prove
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    72
     * that due to the overflow the signed comparison result < 98 is
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    73
     * always false.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    74
     */
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    75
    int result = c - (Character.MAX_VALUE - Integer.MIN_VALUE) + 2;
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    76
    if (1 < result && result < 100) {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    77
      // Unreachable
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    78
      throw new RuntimeException("Should not reach here!");
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    79
    }
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    80
  }
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    81
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    82
  /**
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    83
   * Tests optimizations of signed and unsigned integer comparison.
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    84
   */
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    85
  public static void main(String[] args) {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    86
    // We use characters to get a limited integer range for free
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    87
    for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) {
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    88
      testSigned((char) i);
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    89
      testUnsigned((char) i);
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    90
    }
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    91
  }
1b175b39dfc1 8043284: Optimize signed integer comparison
thartmann
parents:
diff changeset
    92
}