1 /*
  2  * Copyright (c) 2021, 2024, 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 8270995
 27  * @summary Membars of non-escaping value class buffer allocations should be removed.
 28  * @library /test/lib /
 29  * @enablePreview
 30  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
 31  *                   -XX:-TieredCompilation -XX:-ReduceInitialCardMarks
 32  *                   -XX:+AlwaysIncrementalInline -Xbatch -XX:CompileCommand=compileonly,*TestUnexpectedMemBar::test*
 33  *                   -XX:+StressIGVN -XX:+StressGCM -XX:+StressLCM -XX:StressSeed=851121348
 34  *                   compiler.valhalla.inlinetypes.TestUnexpectedMemBar
 35  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
 36  *                   -XX:-TieredCompilation -XX:-ReduceInitialCardMarks -XX:+AlwaysIncrementalInline
 37  *                   -Xbatch -XX:CompileCommand=compileonly,*TestUnexpectedMemBar::test*
 38  *                   -XX:+StressIGVN -XX:+StressGCM -XX:+StressLCM
 39  *                   compiler.valhalla.inlinetypes.TestUnexpectedMemBar
 40  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
 41  *                   -Xbatch -XX:CompileCommand=compileonly,*TestUnexpectedMemBar::test*
 42  *                   -XX:+StressIGVN -XX:+StressGCM -XX:+StressLCM
 43  *                   compiler.valhalla.inlinetypes.TestUnexpectedMemBar
 44  */
 45 
 46 package compiler.valhalla.inlinetypes;
 47 
 48 import jdk.test.lib.Asserts;
 49 
 50 value class MyValue1 {
 51     int a = 0;
 52     int b = 0;
 53     int c = 0;
 54     int d = 0;
 55     int e = 0;
 56 
 57     Integer i;
 58     int[] array;
 59 
 60     public MyValue1(Integer i, int[] array) {
 61         this.i = i;
 62         this.array = array;
 63     }
 64 }
 65 
 66 value class MyValue2 {
 67     int a = 0;
 68     int b = 0;
 69     int c = 0;
 70     int d = 0;
 71     int e = 0;
 72 
 73     NonValueClass obj;
 74     int[] array;
 75 
 76     public MyValue2(NonValueClass obj, int[] array) {
 77         this.obj = obj;
 78         this.array = array;
 79     }
 80 }
 81 
 82 public class TestUnexpectedMemBar {
 83 
 84     public static int test1(Integer i) {
 85         int[] array = new int[1];
 86         MyValue1 vt = new MyValue1(i, array);
 87         vt = new MyValue1(vt.i, vt.array);
 88         return vt.i + vt.array[0];
 89     }
 90 
 91     public static int test2(Integer i) {
 92         int[] array = {i};
 93         MyValue1 vt = new MyValue1(i, array);
 94         vt = new MyValue1(vt.i, vt.array);
 95         return vt.i + vt.array[0];
 96     }
 97 
 98     public static int test3(NonValueClass obj) {
 99         int[] array = new int[1];
100         MyValue2 vt = new MyValue2(obj, array);
101         vt = new MyValue2(vt.obj, vt.array);
102         return vt.obj.x + vt.array[0];
103     }
104 
105     public static int test4(NonValueClass obj) {
106         int[] array = {obj.x};
107         MyValue2 vt = new MyValue2(obj, array);
108         vt = new MyValue2(vt.obj, vt.array);
109         return vt.obj.x + vt.array[0];
110     }
111 
112     public static void main(String[] args) {
113         for (int i = 0; i < 100_000; ++i) {
114             int res = test1(i);
115             Asserts.assertEquals(res, i, "test1 failed");
116             res = test2(i);
117             Asserts.assertEquals(res, 2*i, "test2 failed");
118             res = test3(new NonValueClass(i));
119             Asserts.assertEquals(res, i, "test3 failed");
120             res = test4(new NonValueClass(i));
121             Asserts.assertEquals(res, 2*i, "test4 failed");
122         }
123     }
124 }