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 package compiler.valhalla.inlinetypes;
26 
27 import jdk.test.lib.Asserts;
28 
29 /*
30  * @test
31  * @summary Test value numbering behaves correctly with flat fields.
32  * @library /testlibrary /test/lib
33  * @enablePreview
34  * @modules java.base/jdk.internal.value
35  *          java.base/jdk.internal.vm.annotation
36  * @run main/othervm -Xcomp -XX:TieredStopAtLevel=1 -ea
37  *                   -XX:CompileCommand=compileonly,compiler.valhalla.inlinetypes.TestC1ValueNumbering::*
38  *                   compiler.valhalla.inlinetypes.TestC1ValueNumbering
39  */
40 
41 import jdk.internal.vm.annotation.ImplicitlyConstructible;
42 import jdk.internal.vm.annotation.LooselyConsistentValue;
43 import jdk.internal.vm.annotation.NullRestricted;
44 
45 public class TestC1ValueNumbering {
46     @ImplicitlyConstructible
47     @LooselyConsistentValue
48     static value class Point {
49         int x;
50         int y;
51 
52         public Point() {
53             x = 0;
54             y = 0;
55         }
56 
57         public Point(int x, int y) {
58             this.x = x;
59             this.y = y;
60         }
61     }
62 
63     @NullRestricted
64     Point p;
65 
66     // Notes on test 1:
67     // 1 - asserts are important create several basic blocks (asserts create branches)
68     // 2 - local variables x, y must be read in the same block as the putfield
69     static void test1() {
70         Point p = new Point(4,5);
71         TestC1ValueNumbering test = new TestC1ValueNumbering();
72         assert test.p.x == 0;
73         assert test.p.y == 0;
74         test.p = p;
75         int x = test.p.x;
76         int y = test.p.y;
77         Asserts.assertEQ(x, 4, "Bad field value");
78         Asserts.assertEQ(y, 5, "Bad field value");
79     }
80 
81     public static void main(String[] args) {
82         for (int i = 0; i < 10; i++) {
83             test1();
84         }
85     }
86 }