1 /*
2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3 * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /*
26 * @test
27 * @bug 8333791
28 * @requires os.arch=="aarch64" | os.arch=="riscv64" | os.arch=="x86_64" | os.arch=="amd64"
29 * @requires vm.gc.Parallel
30 * @requires vm.compiler2.enabled
31 * @summary Check stable field folding and barriers
32 * @modules java.base/jdk.internal.vm.annotation
33 * @library /test/lib /
34 * @run driver compiler.c2.irTests.stable.StableRefPlainTest
35 */
36
37 package compiler.c2.irTests.stable;
38
39 import compiler.lib.ir_framework.*;
40 import jdk.test.lib.Asserts;
41
42 import jdk.internal.vm.annotation.Stable;
43
44 public class StableRefPlainTest {
45
46 public static void main(String[] args) {
47 TestFramework tf = new TestFramework();
48 tf.addTestClassesToBootClassPath();
49 tf.addFlags(
50 "-XX:+UnlockExperimentalVMOptions",
51 "-XX:CompileThreshold=100",
52 "-XX:-TieredCompilation",
53 "-XX:+UseParallelGC"
54 );
55 tf.start();
56 }
57
58 static final Integer INTEGER = 42;
59
60 static class Carrier {
61 @Stable
62 Integer field;
63
64 @ForceInline
65 public Carrier(boolean init) {
66 if (init) {
67 field = INTEGER;
68 }
69 }
70
71 @ForceInline
72 public void init() {
73 field = INTEGER;
74 }
75 }
76
77 static final Carrier BLANK_CARRIER = new Carrier(false);
78 static final Carrier INIT_CARRIER = new Carrier(true);
79
80 @Test
81 @IR(counts = { IRNode.LOAD, ">0" })
82 @IR(applyIf = {"enable-valhalla", "false"}, failOn = { IRNode.MEMBAR })
83 // We have barriers with valhalla from the atomic expansion of the LoadFlatNode
84 // Indeed, since the field is not initialized, it is not known to be constant yet,
85 // and so, the LoadFlat cannot be expanded non-atomically. We need barriers to synchronize
86 // the LoadFlat and potential updates to sub-field of the flatten field.
87 @IR(applyIfAnd = {"UseFieldFlattening", "true", "enable-valhalla", "true"}, counts = { IRNode.MEMBAR, ">0" })
88 static int testNoFold() {
89 // Access should not be folded.
90 Integer i = BLANK_CARRIER.field;
91 return i != null ? i : 0;
92 }
93
94 @Test
95 @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR })
96 static int testFold() {
97 // Access should be completely folded.
98 Integer i = INIT_CARRIER.field;
99 return i != null ? i : 0;
100 }
101
102 @Test
103 @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
104 static Carrier testConstructorBlankInit() {
105 // Only the header barrier.
106 return new Carrier(false);
107 }
108
109 @Test
110 @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
111 static Carrier testConstructorFullInit() {
112 // Only the header barrier.
113 return new Carrier(true);
114 }
115
116 @Test
117 @IR(applyIf = {"enable-valhalla", "false"}, failOn = { IRNode.MEMBAR })
118 // We have barriers from the atomic expansion of the StoreFlatNode. Store is not eliminated with
119 // or without Valhalla, but Valhalla's StoreFlat require barriers.
120 @IR(applyIfAnd = {"UseFieldFlattening", "true", "enable-valhalla", "true"}, counts = { IRNode.MEMBAR, ">0" })
121 static void testMethodInit() {
122 INIT_CARRIER.init();
123 }
124
125 }