1 /*
2 * Copyright (c) 2023, Arm Limited. All rights reserved.
3 * Copyright (c) 2023, 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 package compiler.valhalla.inlinetypes;
26
27 import compiler.lib.ir_framework.*;
28 import java.util.Random;
29
30 import jdk.test.lib.Utils;
31
32 import jdk.internal.vm.annotation.LooselyConsistentValue;
33 import jdk.internal.vm.annotation.NullRestricted;
34
35 /*
36 * @test
37 * @bug 8311219
38 * @summary VM option "UseFieldFlattening" does not work well.
39 * @library /test/lib /
40 * @enablePreview
41 * @modules java.base/jdk.internal.value
42 * java.base/jdk.internal.vm.annotation
43 * @run main/othervm -XX:-TieredCompilation
44 * -XX:-UseFieldFlattening
45 * compiler.valhalla.inlinetypes.TestInlineFieldNonFlattened
46 */
47
48 public class TestInlineFieldNonFlattened {
49 static class MyClass {
50 @NullRestricted
51 public final MyValue v1;
52
53 public MyValue v2;
54
55 public MyClass(MyValue v) {
56 v2 = v;
57 v1 = new MyValue(5);
58 super();
59 }
60 }
61
62 @LooselyConsistentValue
63 static value class MyValue {
64 public int field;
65
66 public MyValue(int f) {
67 field = f;
68 }
69 }
70
71 private static final Random RD = Utils.getRandomInstance();
72
73 static MyClass c;
74
75 static {
76 c = new MyClass(new MyValue(RD.nextInt(100)));
77 }
78
79 static int f;
80
81 @Test
82 @IR(counts = {IRNode.LOAD_N, "2"})
83 public static void testNonFlattenedField() {
84 f = c.v2.field;
85 }
86
87 @Test
88 @IR(counts = {IRNode.LOAD_N, "2"})
89 public static void testNonFlattenedFinalField() {
90 f = c.v1.field;
91 }
92
93 public static void main(String[] args) {
94 TestFramework testFramework = new TestFramework();
95 testFramework.setDefaultWarmup(10000)
96 .addFlags("--enable-preview",
97 "--add-exports", "java.base/jdk.internal.vm.annotation=ALL-UNNAMED",
98 "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED",
99 "-XX:-TieredCompilation",
100 "-XX:-UseFieldFlattening")
101 .start();
102 }
103 }
104