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.StableRefPlainTest
 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 StableRefPlainTest {
 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         Integer field;
 62 
 63         @ForceInline
 64         public Carrier(boolean init) {
 65             if (init) {
 66                 field = INTEGER;
 67             }
 68         }
 69 
 70         @ForceInline
 71         public void init() {
 72             field = INTEGER;
 73         }
 74     }
 75 
 76     static final Carrier BLANK_CARRIER = new Carrier(false);
 77     static final Carrier INIT_CARRIER = new Carrier(true);
 78 
 79     @Test
 80     @IR(counts = { IRNode.LOAD, ">0" })
 81     @IR(failOn = { IRNode.MEMBAR })
 82     static int testNoFold() {
 83         // Access should not be folded.
 84         // No barriers expected for plain fields.
 85         Integer i = BLANK_CARRIER.field;
 86         return i != null ? i : 0;
 87     }
 88 
 89     @Test
 90     @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR })
 91     static int testFold() {
 92         // Access should be completely folded.
 93         Integer i = INIT_CARRIER.field;
 94         return i != null ? i : 0;
 95     }
 96 
 97     @Test
 98     @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
 99     static Carrier testConstructorBlankInit() {
100         // Only the header barrier.
101         return new Carrier(false);
102     }
103 
104     @Test
105     @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
106     static Carrier testConstructorFullInit() {
107         // Only the header barrier.
108         return new Carrier(true);
109     }
110 
111     @Test
112     @IR(failOn = { IRNode.MEMBAR })
113     static void testMethodInit() {
114         // Reference inits do not have membars.
115         INIT_CARRIER.init();
116     }
117 
118 }