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.StableRefFinalTest
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 StableRefFinalTest {
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 INTEGER = 42;
58 
59     static class Carrier {
60         @Stable
61         final Integer field;
62 
63         @ForceInline
64         public Carrier(boolean init) {
65             field = init ? INTEGER : null;
66         }
67     }
68 
69     static final Carrier BLANK_CARRIER = new Carrier(false);
70     static final Carrier INIT_CARRIER = new Carrier(true);
71 
72     @Test
73     @IR(counts = { IRNode.LOAD, ">0" })
74     @IR(failOn = { IRNode.MEMBAR })
75     static int testNoFold() {
76         // Access should not be folded.
77         // No barriers expected for plain fields.
78         Integer i = BLANK_CARRIER.field;
79         return i != null ? i : 0;
80     }
81 
82     @Test
83     @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR })
84     static int testFold() {
85         // Access should be completely folded.
86         Integer i = INIT_CARRIER.field;
87         return i != null ? i : 0;
88     }
89 
90     @Test
91     @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
92     static Carrier testConstructorInit() {
93         // Only the header+final barrier.
94         return new Carrier(true);
95     }
96 
97 }