1 /*
2 * Copyright Amazon.com Inc. or its affiliates. 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 8333791
27 * @requires os.arch=="aarch64" | os.arch=="riscv64" | os.arch=="x86_64" | os.arch=="amd64"
28 * @requires vm.gc.Parallel
29 * @requires vm.compiler2.enabled
30 * @summary Check stable field folding and barriers
31 * @modules java.base/jdk.internal.vm.annotation
32 * @library /test/lib /
33 * @run driver compiler.c2.irTests.stable.StableRefArrayTest
34 */
35
36 package compiler.c2.irTests.stable;
37
38 import compiler.lib.ir_framework.*;
39 import jdk.test.lib.Asserts;
40
41 import jdk.internal.vm.annotation.Stable;
42
43 public class StableRefArrayTest {
44
45 public static void main(String[] args) {
46 TestFramework tf = new TestFramework();
47 tf.addTestClassesToBootClassPath();
48 tf.addFlags(
49 "-XX:+UnlockExperimentalVMOptions",
50 "-XX:CompileThreshold=100",
51 "-XX:-TieredCompilation",
52 "-XX:+UseParallelGC"
53 );
54 tf.start();
55 }
56
57 static final Integer[] EMPTY_INTEGER = new Integer[] { null };
58 static final Integer[] FULL_INTEGER = new Integer[] { 42 };
59
60 static class Carrier {
61 @Stable
62 Integer[] field;
63
64 @ForceInline
65 public Carrier(int initLevel) {
66 switch (initLevel) {
67 case 0:
68 // Do nothing.
69 break;
70 case 1:
71 field = EMPTY_INTEGER;
72 break;
73 case 2:
74 field = FULL_INTEGER;
75 break;
76 default:
77 throw new IllegalStateException("Unknown level");
78 }
79 }
80
81 @ForceInline
82 public void initEmpty() {
83 field = EMPTY_INTEGER;
84 }
85
86 @ForceInline
87 public void initFull() {
88 field = FULL_INTEGER;
89 }
90
91 }
92
93 static final Carrier BLANK_CARRIER = new Carrier(0);
94 static final Carrier INIT_EMPTY_CARRIER = new Carrier(1);
95 static final Carrier INIT_FULL_CARRIER = new Carrier(2);
96
97 @Test
98 @IR(counts = { IRNode.LOAD, ">0" })
99 @IR(failOn = { IRNode.MEMBAR })
100 static int testNoFold() {
101 // Access should not be folded.
102 // No barriers expected for plain fields.
103 Integer[] is = BLANK_CARRIER.field;
104 if (is != null) {
105 Integer i = is[0];
106 if (i != null) {
107 return i;
108 }
109 }
110 return 0;
111 }
112
113 @Test
114 @IR(counts = { IRNode.LOAD, ">0" })
115 @IR(failOn = { IRNode.MEMBAR })
116 static int testPartialFold() {
117 // Access should not be folded.
118 // No barriers expected for plain fields.
119 Integer[] is = INIT_EMPTY_CARRIER.field;
120 if (is != null) {
121 Integer i = is[0];
122 if (i != null) {
123 return i;
124 }
125 }
126 return 0;
127 }
128
129
130 @Test
131 @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR })
132 static int testFold() {
133 // Access should be completely folded.
134 Integer[] is = INIT_FULL_CARRIER.field;
135 if (is != null) {
136 Integer i = is[0];
137 if (i != null) {
138 return i;
139 }
140 }
141 return 0;
142 }
143
144 @Test
145 @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
146 static Carrier testConstructorBlankInit() {
147 // Only the header barrier.
148 return new Carrier(0);
149 }
150
151 @Test
152 @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
153 static Carrier testConstructorEmptyInit() {
154 // Only the header barrier.
155 return new Carrier(1);
156 }
157
158 @Test
159 @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
160 static Carrier testConstructorFullInit() {
161 // Only the header barrier.
162 return new Carrier(2);
163 }
164
165 @Test
166 @IR(failOn = { IRNode.MEMBAR })
167 static void testMethodEmptyInit() {
168 // Reference inits do not have membars.
169 INIT_EMPTY_CARRIER.initEmpty();
170 }
171
172 @Test
173 @IR(failOn = { IRNode.MEMBAR })
174 static void testMethodFullInit() {
175 // Reference inits do not have membars.
176 INIT_FULL_CARRIER.initFull();
177 }
178
179 }