hotspot/test/compiler/c1/NullCheckTest.java
author thartmann
Fri, 14 Jul 2017 11:55:58 +0200
changeset 46663 715d5f268f62
parent 41705 332239c052cc
permissions -rw-r--r--
8184271: Time related C1 intrinsics produce inconsistent results when floating around Summary: C1 intrinsics for System.nanoTime(), System.currentTimeMillis() and JVM.counterTime() should be pinned. Reviewed-by: kvn, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     1
/*
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     4
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     8
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    13
 * accompanied this code).
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    14
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    18
 *
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    21
 * questions.
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    22
 */
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    23
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    24
/**
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    25
 * @test
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    26
 * @bug 6478991
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    27
 * @summary C1 NullCheckEliminator yields incorrect exceptions
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    28
 *
40059
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.c1.NullCheckTest::test
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    31
 *      -XX:CompileCommand=compileonly,compiler.c1.NullCheckTest::inlined
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    32
 *      compiler.c1.NullCheckTest
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    33
 */
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    34
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    35
package compiler.c1;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    36
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    37
public class NullCheckTest {
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    38
    static class A {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    39
        int f;
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    40
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    41
        public final void inlined(A a) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    42
            // This cast is intended to fail.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    43
            B b = ((B) a);
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    44
        }
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    45
    }
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    46
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    47
    static class B extends A {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    48
    }
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    49
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    50
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    51
    private static void test(A a1, A a2) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    52
        // Inlined call must do a null check on a1.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    53
        // However, the exlipcit NullCheck instruction is eliminated and
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    54
        // the null check is folded into the field load below, so the
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    55
        // exception in the inlined method is thrown before the null check
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    56
        // and the NullPointerException is not thrown.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    57
        a1.inlined(a2);
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    58
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    59
        int x = a1.f;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    60
    }
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    61
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    62
    public static void main(String[] args) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    63
        // load classes
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    64
        new B();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    65
        try {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    66
                test(null, new A());
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    67
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    68
                throw new InternalError("FAILURE: no exception");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    69
        } catch (NullPointerException ex) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    70
                System.out.println("CORRECT: NullPointerException");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    71
        } catch (ClassCastException ex) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    72
                System.out.println("FAILURE: ClassCastException");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    73
                throw ex;
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    74
        }
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 27699
diff changeset
    75
    }
10015
ddfb84d0d0c1 6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
diff changeset
    76
}