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.StableRefFinalTest
 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 StableRefFinalTest {
 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         final Integer field;
 63 
 64         @ForceInline
 65         public Carrier(boolean init) {
 66             field = init ? INTEGER : null;
 67         }
 68     }
 69 
 70     static final Carrier BLANK_CARRIER = new Carrier(false);
 71     static final Carrier INIT_CARRIER = new Carrier(true);
 72 
 73     @Test
 74     @IR(counts = { IRNode.LOAD, ">0" })
 75     @IR(applyIf = {"enable-valhalla", "false"}, failOn = { IRNode.MEMBAR })
 76     // We have barriers with valhalla from the atomic expansion of the LoadFlatNode
 77     // Indeed, since the field is not initialized, it is not known to be constant yet,
 78     // and so, the LoadFlat cannot be expanded non-atomically. We need barriers to synchronize
 79     // the LoadFlat and potential updates to sub-field of the flatten field.
 80     @IR(applyIfAnd = {"UseFieldFlattening", "true", "enable-valhalla", "true"}, counts = { IRNode.MEMBAR, ">0" })
 81     static int testNoFold() {
 82         // Access should not be folded.
 83         // No barriers expected for plain fields.
 84         Integer i = BLANK_CARRIER.field;
 85         return i != null ? i : 0;
 86     }
 87 
 88     @Test
 89     @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR })
 90     static int testFold() {
 91         // Access should be completely folded.
 92         Integer i = INIT_CARRIER.field;
 93         return i != null ? i : 0;
 94     }
 95 
 96     @Test
 97     @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" })
 98     static Carrier testConstructorInit() {
 99         // Only the header+final barrier.
100         return new Carrier(true);
101     }
102 
103 }