1 /*
  2  * Copyright (c) 2019, 2025, 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 package compiler.valhalla.inlinetypes;
 25 
 26 import java.lang.invoke.*;
 27 import java.lang.reflect.Method;
 28 
 29 import jdk.internal.value.ValueClass;
 30 import jdk.internal.vm.annotation.LooselyConsistentValue;
 31 import jdk.internal.vm.annotation.NullRestricted;
 32 import jdk.internal.vm.annotation.Strict;
 33 
 34 import jdk.test.lib.Asserts;
 35 
 36 import jdk.test.whitebox.WhiteBox;
 37 
 38 /**
 39  * @test TestDeoptimizationWhenBuffering
 40  * @summary Test correct execution after deoptimizing from inline type specific runtime calls.
 41  * @library /testlibrary /test/lib /compiler/whitebox /
 42  * @enablePreview
 43  * @modules java.base/jdk.internal.value
 44  *          java.base/jdk.internal.vm.annotation
 45  * @build org.openjdk.asmtools.* org.openjdk.asmtools.jasm.*
 46  * @build jdk.test.whitebox.WhiteBox
 47  * @enablePreview
 48  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 49  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 50  *                   -XX:+DeoptimizeALot -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 51  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering C1
 52  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 53  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch
 54  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 55  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 56  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch -XX:-MonomorphicArrayCheck -XX:-AlwaysIncrementalInline
 57  *                   -XX:-InlineTypePassFieldsAsArgs -XX:-InlineTypeReturnedAsFields -XX:+UseArrayFlattening
 58  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 59  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 60  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 61  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch -XX:-MonomorphicArrayCheck -XX:+AlwaysIncrementalInline
 62  *                   -XX:-InlineTypePassFieldsAsArgs -XX:-InlineTypeReturnedAsFields -XX:+UseArrayFlattening
 63  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 64  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 65  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 66  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch -XX:-MonomorphicArrayCheck -XX:-AlwaysIncrementalInline
 67  *                   -XX:+InlineTypePassFieldsAsArgs -XX:+InlineTypeReturnedAsFields -XX:+UseArrayFlattening
 68  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 69  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 70  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 71  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch -XX:-MonomorphicArrayCheck -XX:+AlwaysIncrementalInline
 72  *                   -XX:+InlineTypePassFieldsAsArgs -XX:+InlineTypeReturnedAsFields -XX:+UseArrayFlattening
 73  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 74  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 75  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 76  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch -XX:-MonomorphicArrayCheck -XX:-AlwaysIncrementalInline
 77  *                   -XX:+InlineTypePassFieldsAsArgs -XX:+InlineTypeReturnedAsFields -XX:+UseArrayFlattening -XX:-UseFieldFlattening
 78  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 79  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 80  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 81  *                   -XX:+DeoptimizeALot -XX:-UseTLAB -Xbatch -XX:-MonomorphicArrayCheck -XX:+AlwaysIncrementalInline
 82  *                   -XX:+InlineTypePassFieldsAsArgs -XX:+InlineTypeReturnedAsFields -XX:+UseArrayFlattening -XX:-UseFieldFlattening
 83  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.*::test*
 84  *                   compiler.valhalla.inlinetypes.TestDeoptimizationWhenBuffering
 85  */
 86 
 87 public class TestDeoptimizationWhenBuffering {
 88     static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 89     static final int COMP_LEVEL_FULL_OPTIMIZATION = 4; // C2 or JVMCI
 90 
 91     @LooselyConsistentValue
 92     static value class MyValue1 {
 93         static int cnt = 0;
 94         int x;
 95         @Strict
 96         @NullRestricted
 97         MyValue2 vtField1;
 98         MyValue2 vtField2;
 99 
100         public MyValue1() {
101             cnt++;
102             x = cnt;
103             vtField1 = new MyValue2();
104             vtField2 = new MyValue2();
105         }
106 
107         public MyValue1(int x, MyValue2 vtField1, MyValue2 vtField2) {
108             this.x = x;
109             this.vtField1 = vtField1;
110             this.vtField2 = vtField2;
111         }
112 
113         public int hash() {
114             return x + vtField1.x + vtField2.x;
115         }
116 
117         public MyValue1 testWithField(int x) {
118             return new MyValue1(x, vtField1, vtField2);
119         }
120 
121         public static MyValue1 makeDefault() {
122             return new MyValue1(0, MyValue2.makeDefault(), null);
123         }
124 
125         public static final MyValue1 DEFAULT = new MyValue1(0, new MyValue2(0), new MyValue2(0));
126     }
127 
128     @LooselyConsistentValue
129     static value class MyValue2 {
130         static int cnt = 0;
131         int x;
132 
133         public MyValue2() {
134             cnt++;
135             x = cnt;
136         }
137 
138         public MyValue2(int x) {
139             this.x = x;
140         }
141 
142         public static MyValue2 makeDefault() {
143             return new MyValue2(0);
144         }
145     }
146 
147     static {
148         try {
149             Class<?> clazz = TestDeoptimizationWhenBuffering.class;
150             MethodHandles.Lookup lookup = MethodHandles.lookup();
151 
152             MethodType mt = MethodType.methodType(MyValue1.class);
153             test9_mh = lookup.findStatic(clazz, "test9Callee", mt);
154             test10_mh = lookup.findStatic(clazz, "test10Callee", mt);
155         } catch (NoSuchMethodException | IllegalAccessException e) {
156             e.printStackTrace();
157             throw new RuntimeException("Method handle lookup failed");
158         }
159     }
160 
161     MyValue1 test1() {
162         return new MyValue1();
163     }
164 
165     @Strict
166     @NullRestricted
167     static MyValue1 vtField1 = MyValue1.DEFAULT;
168 
169     MyValue1 test2() {
170         vtField1 = new MyValue1();
171         return vtField1;
172     }
173 
174     public int test3Callee(MyValue1 vt) {
175         return vt.hash();
176     }
177 
178     int test3() {
179         MyValue1 vt = new MyValue1();
180         return test3Callee(vt);
181     }
182 
183     static MyValue1[] vtArray = (MyValue1[])ValueClass.newNullRestrictedNonAtomicArray(MyValue1.class, 1, MyValue1.DEFAULT);
184 
185     MyValue1 test4() {
186         vtArray[0] = new MyValue1();
187         return vtArray[0];
188     }
189 
190     Object test5(Object[] array) {
191         array[0] = new MyValue1();
192         return array[0];
193     }
194 
195     boolean test6(Object obj) {
196         MyValue1 vt = new MyValue1();
197         return vt == obj;
198     }
199 
200     Object test7(Object[] obj) {
201         return obj[0];
202     }
203 
204     MyValue1 test8(MyValue1[] obj) {
205         return obj[0];
206     }
207 
208     static final MethodHandle test9_mh;
209 
210     public static MyValue1 test9Callee() {
211         return new MyValue1();
212     }
213 
214     MyValue1 test9() throws Throwable {
215         return (MyValue1)test9_mh.invokeExact();
216     }
217 
218     static final MethodHandle test10_mh;
219     @Strict
220     @NullRestricted
221     static final MyValue1 test10Field = new MyValue1();
222     static int test10Counter = 0;
223 
224     public static MyValue1 test10Callee() {
225         test10Counter++;
226         return test10Field;
227     }
228 
229     Object test10() throws Throwable {
230         return test10_mh.invoke();
231     }
232 
233     MyValue1 test11(MyValue1 vt) {
234         return vt.testWithField(42);
235     }
236 
237     MyValue1 vtField2;
238 
239     MyValue1 test12() {
240         vtField2 = new MyValue1();
241         return vtField2;
242     }
243 
244     public static void main(String[] args) throws Throwable {
245         if (args.length > 0) {
246             // Compile callees with C1 only, to exercise deoptimization while buffering at method entry
247             Asserts.assertEQ(args[0], "C1", "unsupported mode");
248             Method m = MyValue1.class.getMethod("testWithField", int.class);
249             WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_FULL_OPTIMIZATION, false);
250             m = TestDeoptimizationWhenBuffering.class.getMethod("test3Callee", MyValue1.class);
251             WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_FULL_OPTIMIZATION, false);
252             m = TestDeoptimizationWhenBuffering.class.getMethod("test9Callee");
253             WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_FULL_OPTIMIZATION, false);
254             m = TestDeoptimizationWhenBuffering.class.getMethod("test10Callee");
255             WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_FULL_OPTIMIZATION, false);
256         }
257 
258         MyValue1[] va = (MyValue1[])ValueClass.newNullRestrictedNonAtomicArray(MyValue1.class, 3, MyValue1.DEFAULT);
259         va[0] = new MyValue1();
260         Object[] oa = new Object[3];
261         oa[0] = va[0];
262         TestDeoptimizationWhenBuffering t = new TestDeoptimizationWhenBuffering();
263         for (int i = 0; i < 100_000; ++i) {
264             // Check counters to make sure that we don't accidentially reexecute calls when deoptimizing
265             int expected = MyValue1.cnt + MyValue2.cnt + MyValue2.cnt;
266             Asserts.assertEQ(t.test1().hash(), expected + 4);
267             vtField1 = MyValue1.makeDefault();
268             Asserts.assertEQ(t.test2().hash(), expected + 9);
269             Asserts.assertEQ(vtField1.hash(), expected + 9);
270             Asserts.assertEQ(t.test3(), expected + 14);
271             Asserts.assertEQ(t.test4().hash(), expected + 19);
272             Asserts.assertEQ(((MyValue1)t.test5(vtArray)).hash(), expected + 24);
273             Asserts.assertEQ(t.test6(vtField1), false);
274             Asserts.assertEQ(t.test7(((i % 2) == 0) ? va : oa), va[0]);
275             Asserts.assertEQ(t.test8(va), va[0]);
276             Asserts.assertEQ(t.test8(va), va[0]);
277             Asserts.assertEQ(t.test9().hash(), expected + 34);
278             int count = test10Counter;
279             Asserts.assertEQ(((MyValue1)t.test10()).hash(), test10Field.hash());
280             Asserts.assertEQ(t.test10Counter, count + 1);
281             Asserts.assertEQ(t.test11(va[0]).hash(), va[0].testWithField(42).hash());
282             t.vtField2 = MyValue1.makeDefault();
283             Asserts.assertEQ(t.test12().hash(), expected + 39);
284             Asserts.assertEQ(t.vtField2.hash(), expected + 39);
285         }
286     }
287 }