1 /*
  2  * Copyright (c) 2026, Oracle and/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 8377541
 27  * @summary Test that membars are eliminated when loading from a stable array.
 28  * @library /test/lib /
 29  * @modules java.base/jdk.internal.misc

 30  * @modules java.base/jdk.internal.vm.annotation
 31  * @run driver ${test.main.class}
 32  */
 33 
 34 package compiler.stable;
 35 
 36 import java.util.Objects;

 37 
 38 import jdk.internal.misc.Unsafe;

 39 import jdk.internal.vm.annotation.Stable;
 40 

 41 import compiler.lib.ir_framework.*;
 42 
 43 public class TestStableArrayMembars {
 44 
 45     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 46 


 47     public static void main(String[] args) {
 48         TestFramework tf = new TestFramework();
 49         tf.addTestClassesToBootClassPath();
 50         tf.addFlags( "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");







 51         tf.start();
 52     }
 53 
 54     static final class LazyIntArray {
 55         @Stable
 56         private final Integer[] arr;
 57 
 58         LazyIntArray() {
 59             this.arr = new Integer[10];
 60         }
 61 
 62         @ForceInline
 63         Integer get(int idx) {
 64             Integer i = contentsAcquire(offsetFor(idx));
 65             return i == null ? slowPath(arr, idx) : i;
 66         }
 67 
 68         @ForceInline
 69         private Integer contentsAcquire(long offset) {
 70             return (Integer) UNSAFE.getReferenceAcquire(arr, offset);


 71         }
 72 
 73         @ForceInline
 74         private static long offsetFor(long index) {
 75             return Unsafe.ARRAY_OBJECT_BASE_OFFSET + Unsafe.ARRAY_OBJECT_INDEX_SCALE * index;
 76         }
 77 
 78         static Integer slowPath(final Integer[] array, final int index) {
 79             final long offset = offsetFor(index);
 80             final Integer t = array[index];
 81             if (t == null) {
 82                 final Integer newValue = Integer.valueOf(42);
 83                 Objects.requireNonNull(newValue);
 84                 set(array, index, newValue);
 85 
 86                 return newValue;
 87             }
 88             return t;
 89         }
 90 
 91         static void set(Integer[] array, int index, Integer newValue) {
 92             if (array[index] == null) {
 93                 UNSAFE.putReferenceRelease(array, Unsafe.ARRAY_OBJECT_BASE_OFFSET + Unsafe.ARRAY_OBJECT_INDEX_SCALE * (long) index, newValue);




 94             }
 95         }
 96     }
 97 
 98     static final LazyIntArray la = new LazyIntArray();
 99 
100     @Test
101     @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR })















102     static Integer test() {
103         return la.get(0);
104     }





105 }
--- EOF ---