1 /*
 2  * Copyright (c) 2024, Red Hat, Inc. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 8325372
27  * @summary fusion of heap stable test causes GetAndSet node to be removed
28  * @requires vm.gc.Shenandoah
29  * @modules java.base/jdk.internal.misc:+open
30  *
31  * @run main/othervm -XX:+UseShenandoahGC -XX:-BackgroundCompilation TestUnsafeLoadStoreMergedHeapStableTests
32  */
33 
34 import jdk.internal.misc.Unsafe;
35 
36 import java.lang.reflect.Field;
37 
38 public class TestUnsafeLoadStoreMergedHeapStableTests {
39 
40     static final jdk.internal.misc.Unsafe UNSAFE = Unsafe.getUnsafe();
41     static long F_OFFSET;
42 
43     static class A {
44         Object f;
45     }
46 
47     static {
48         try {
49             Field fField = A.class.getDeclaredField("f");
50             F_OFFSET = UNSAFE.objectFieldOffset(fField);
51         } catch (Exception e) {
52             throw new RuntimeException(e);
53         }
54     }
55 
56     static Object testHelper(boolean flag, Object o, long offset, Object x) {
57         if (flag) {
58             return UNSAFE.getAndSetObject(o, offset, x);
59         }
60         return null;
61     }
62 
63     static Object field;
64 
65 
66     static Object test1(boolean flag, Object o, long offset) {
67         return testHelper(flag, null, offset, field);
68     }
69 
70     static Object test2(Object o, long offset) {
71         return UNSAFE.getAndSetObject(o, offset, field);
72     }
73 
74     static public void main(String[] args) {
75         A a = new A();
76         for (int i = 0; i < 20_000; i++) {
77             testHelper(true, a, F_OFFSET, null);
78             test1(false, a, F_OFFSET);
79             test2(a, F_OFFSET);
80         }
81     }
82 }