1 /*
 2  * Copyright (c) 2022, 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  * @summary Test scalarization in returns with unloaded return types.
27  * @library /test/lib /compiler/whitebox /
28  * @enablePreview
29  * @build jdk.test.whitebox.WhiteBox
30  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
31  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
32  *                   -Xbatch -XX:CompileCommand=dontinline,*::test*
33  *                   TestUnloadedReturnTypes
34  */
35 
36 import java.lang.reflect.Method;
37 
38 import jdk.test.whitebox.WhiteBox;
39 
40 value class MyValue1 {
41     int x;
42 
43     public MyValue1(int x) {
44         this.x = x;
45     }
46 }
47 
48 class MyClass {
49 
50     static MyValue1 test(boolean b) {
51         return b ? new MyValue1(42) : null;
52     }
53 }
54 
55 public class TestUnloadedReturnTypes {
56     public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
57 
58     static Object res = null;
59 
60     public static void test(boolean b) {
61         res = MyClass.test(b);
62     }
63 
64     public static void main(String[] args) throws Exception {
65         // C1 compile caller method
66         Method m = TestUnloadedReturnTypes.class.getMethod("test", boolean.class);
67         WHITE_BOX.enqueueMethodForCompilation(m, 3);
68 
69         // Make sure the callee method is C2 compiled
70         for (int i = 0; i < 100_000; ++i) {
71             MyClass.test((i % 2) == 0);
72         }
73 
74         test(true);
75         if (((MyValue1)res).x != 42) {
76             throw new RuntimeException("Test failed");
77         }
78 
79         test(false);
80         if (res != null) {
81             throw new RuntimeException("Test failed");
82         }
83     }
84 }