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