test/hotspot/jtreg/compiler/c2/aarch64/TestUnsafeVolatileCAE.java
author roland
Mon, 05 Nov 2018 12:53:55 +0100
changeset 52409 87bc444ca642
permissions -rw-r--r--
8209835: Aarch64: elide barriers on all volatile operations Reviewed-by: aph, adinn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52409
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     1
/*
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     2
 * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     4
 *
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     7
 * published by the Free Software Foundation.
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     8
 *
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    13
 * accompanied this code).
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    14
 *
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    18
 *
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    21
 * questions.
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    22
 */
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    23
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    24
package compiler.c2.aarch64;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    25
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    26
import java.lang.reflect.Field;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    27
import jdk.internal.misc.Unsafe;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    28
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    29
class TestUnsafeVolatileCAE
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    30
{
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    31
    public volatile int f_int = 0;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    32
    public volatile Integer f_obj = Integer.valueOf(0);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    33
    public volatile long f_long = 0;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    34
    public volatile byte f_byte = 0;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    35
    public volatile short f_short = 0;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    36
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    37
    public static Unsafe unsafe = Unsafe.getUnsafe();
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    38
    public static Field f_int_field;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    39
    public static Field f_obj_field;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    40
    public static Field f_long_field;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    41
    public static Field f_byte_field;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    42
    public static Field f_short_field;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    43
    public static long f_int_off;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    44
    public static long f_obj_off;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    45
    public static long f_long_off;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    46
    public static long f_byte_off;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    47
    public static long f_short_off;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    48
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    49
    static {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    50
        try {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    51
            f_int_field = TestUnsafeVolatileCAE.class.getField("f_int");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    52
            f_obj_field = TestUnsafeVolatileCAE.class.getField("f_obj");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    53
            f_long_field = TestUnsafeVolatileCAE.class.getField("f_long");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    54
            f_byte_field = TestUnsafeVolatileCAE.class.getField("f_byte");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    55
            f_short_field = TestUnsafeVolatileCAE.class.getField("f_short");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    56
            f_int_off = unsafe.objectFieldOffset(f_int_field);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    57
            f_obj_off = unsafe.objectFieldOffset(f_obj_field);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    58
            f_long_off = unsafe.objectFieldOffset(f_long_field);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    59
            f_byte_off = unsafe.objectFieldOffset(f_byte_field);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    60
            f_short_off = unsafe.objectFieldOffset(f_short_field);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    61
        } catch (Exception e) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    62
            System.out.println("reflection failed " + e);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    63
            e.printStackTrace();
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    64
        }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    65
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    66
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    67
    public static void main(String[] args)
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    68
    {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    69
        final TestUnsafeVolatileCAE t = new TestUnsafeVolatileCAE();
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    70
        for (int i = 0; i < 100_000; i++) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    71
            t.f_int = -1;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    72
            int res = t.testInt(-1, i);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    73
            if (res != -1 || t.f_int != i) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    74
                throw new RuntimeException("bad result!");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    75
            }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    76
        }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    77
        for (int i = 0; i < 100_000; i++) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    78
            t.f_long = -1;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    79
            long res = t.testLong(-1, i);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    80
            if (res != -1 || t.f_long != i) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    81
                throw new RuntimeException("bad result!");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    82
            }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    83
        }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    84
        for (int i = 0; i < 100_000; i++) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    85
            t.f_byte = -1;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    86
            byte i_b = (byte)i;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    87
            byte res = t.testByte((byte)-1, i_b);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    88
            if (res != (byte)-1 || t.f_byte != i_b) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    89
                throw new RuntimeException("bad result!");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    90
            }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    91
        }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    92
        for (int i = 0; i < 100_000; i++) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    93
            t.f_short = -1;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    94
            short i_s = (short)i;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    95
            short res = t.testShort((byte)-1, i_s);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    96
            if (res != (short)-1 || t.f_short != i_s) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    97
                throw new RuntimeException("bad result!");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    98
            }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
    99
        }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   100
        Integer minusOne = Integer.valueOf(-1);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   101
        for (int i = 0; i < 100_000; i++) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   102
            t.f_obj = minusOne;
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   103
            Object res = t.testObj(minusOne, Integer.valueOf(i));
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   104
            if (res != minusOne || t.f_obj != i) {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   105
                throw new RuntimeException("bad result!");
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   106
            }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   107
        }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   108
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   109
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   110
    public int testInt(int x, int i)
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   111
    {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   112
        return unsafe.compareAndExchangeInt(this, f_int_off, x, i);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   113
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   114
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   115
    public Object testObj(Object x, Object o)
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   116
    {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   117
        return unsafe.compareAndExchangeReference(this, f_obj_off, x, o);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   118
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   119
    public long testLong(long x, long i)
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   120
    {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   121
        return unsafe.compareAndExchangeLong(this, f_long_off, x, i);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   122
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   123
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   124
    public byte testByte(byte x, byte i)
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   125
    {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   126
        return unsafe.compareAndExchangeByte(this, f_byte_off, x, i);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   127
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   128
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   129
    public short testShort(short x, short i)
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   130
    {
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   131
        return unsafe.compareAndExchangeShort(this, f_short_off, x, i);
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   132
    }
87bc444ca642 8209835: Aarch64: elide barriers on all volatile operations
roland
parents:
diff changeset
   133
}