hotspot/test/compiler/profiling/UnsafeAccess.java
author vlivanov
Wed, 04 May 2016 23:08:43 +0300
changeset 38188 e76af590ea2f
parent 38131 7bb27ec1a3d8
child 40059 c2304140ed64
permissions -rw-r--r--
8155751: Some tests miss othervm for main/bootclasspath mode Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38131
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     1
/*
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     4
 *
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     8
 *
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    13
 * accompanied this code).
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    14
 *
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    18
 *
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    21
 * questions.
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    22
 */
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    23
/*
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    24
 * @test
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    25
 * @bug 8134918
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    26
 * @modules java.base/jdk.internal.misc
38188
e76af590ea2f 8155751: Some tests miss othervm for main/bootclasspath mode
vlivanov
parents: 38131
diff changeset
    27
 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation -Xbatch
e76af590ea2f 8155751: Some tests miss othervm for main/bootclasspath mode
vlivanov
parents: 38131
diff changeset
    28
 *                                 -XX:CompileCommand=dontinline,UnsafeAccess::test*
e76af590ea2f 8155751: Some tests miss othervm for main/bootclasspath mode
vlivanov
parents: 38131
diff changeset
    29
 *                                 UnsafeAccess
38131
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    30
 */
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    31
import jdk.internal.misc.Unsafe;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    32
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    33
public class UnsafeAccess {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    34
    private static final Unsafe U = Unsafe.getUnsafe();
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    35
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    36
    static Class cls = Object.class;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    37
    static long off = U.ARRAY_OBJECT_BASE_OFFSET;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    38
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    39
    static Object testUnsafeAccess(Object o, boolean isObjArray) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    40
        if (o != null && cls.isInstance(o)) { // speculates "o" type to int[]
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    41
            return helperUnsafeAccess(o, isObjArray);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    42
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    43
        return null;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    44
    }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    45
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    46
    static Object helperUnsafeAccess(Object o, boolean isObjArray) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    47
        if (isObjArray) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    48
            U.putObject(o, off, new Object());
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    49
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    50
        return o;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    51
    }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    52
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    53
    static Object testUnsafeLoadStore(Object o, boolean isObjArray) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    54
        if (o != null && cls.isInstance(o)) { // speculates "o" type to int[]
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    55
            return helperUnsafeLoadStore(o, isObjArray);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    56
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    57
        return null;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    58
    }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    59
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    60
    static Object helperUnsafeLoadStore(Object o, boolean isObjArray) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    61
        if (isObjArray) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    62
            Object o1 = U.getObject(o, off);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    63
            U.compareAndSwapObject(o, off, o1, new Object());
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    64
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    65
        return o;
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    66
    }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    67
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    68
    public static void main(String[] args) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    69
        Object[] objArray = new Object[10];
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    70
        int[]    intArray = new    int[10];
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    71
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    72
        for (int i = 0; i < 20_000; i++) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    73
            helperUnsafeAccess(objArray, true);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    74
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    75
        for (int i = 0; i < 20_000; i++) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    76
            testUnsafeAccess(intArray, false);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    77
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    78
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    79
        for (int i = 0; i < 20_000; i++) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    80
            helperUnsafeLoadStore(objArray, true);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    81
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    82
        for (int i = 0; i < 20_000; i++) {
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    83
            testUnsafeLoadStore(intArray, false);
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    84
        }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    85
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    86
        System.out.println("TEST PASSED");
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    87
    }
7bb27ec1a3d8 8134918: C2: Type speculation produces mismatched unsafe accesses
vlivanov
parents:
diff changeset
    88
}