1 /*
  2  * Copyright (c) 2023, Arm Limited. All rights reserved.
  3  * Copyright (c) 2023, 2024, 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.ImplicitlyConstructible;
 33 import jdk.internal.vm.annotation.LooselyConsistentValue;
 34 import jdk.internal.vm.annotation.NullRestricted;
 35 
 36 /*
 37  * @test
 38  * @bug 8311219
 39  * @summary VM option "InlineFieldMaxFlatSize" does not work well.
 40  * @library /test/lib /
 41  * @enablePreview
 42  * @modules java.base/jdk.internal.value
 43  *          java.base/jdk.internal.vm.annotation
 44  * @run main/othervm -XX:-TieredCompilation
 45  *                   -XX:InlineFieldMaxFlatSize=0
 46  *                   compiler.valhalla.inlinetypes.TestInlineFieldNonFlattened
 47  */
 48 
 49 public class TestInlineFieldNonFlattened {
 50     static class MyClass {
 51         @NullRestricted
 52         public final MyValue v1 = new MyValue(5);
 53 
 54         public MyValue v2;
 55 
 56         public MyClass(MyValue v) {
 57             v2 = v;
 58         }
 59     }
 60 
 61     @ImplicitlyConstructible
 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:InlineFieldMaxFlatSize=0")
101                      .start();
102     }
103 }
104