File: TestUnstableStable.java

package info (click to toggle)
openjdk-21 21.0.9%2B10-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 821,840 kB
  • sloc: java: 5,620,360; xml: 1,643,607; cpp: 1,297,809; ansic: 421,231; asm: 404,850; objc: 21,018; sh: 15,299; javascript: 11,217; python: 6,895; makefile: 2,367; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (165 lines) | stat: -rw-r--r-- 6,435 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8295486
 * @summary Verify that constant folding of field loads observes consistent values during compilation.
 * @library /test/lib /
 * @modules java.base/jdk.internal.vm.annotation
 *          java.base/jdk.internal.misc
 * @build jdk.test.whitebox.WhiteBox
 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 * @run main/bootclasspath/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *                                 -XX:-TieredCompilation -Xbatch -XX:PerMethodRecompilationCutoff=-1
 *                                 -XX:CompileCommand=compileonly,compiler.stable.TestUnstableStable::test*
 *                                 compiler.stable.TestUnstableStable
 */

package compiler.stable;

import compiler.whitebox.CompilerWhiteBoxTest;

import java.lang.reflect.Method;

import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.Stable;
import jdk.test.whitebox.WhiteBox;

public class TestUnstableStable {
    static final Unsafe U = Unsafe.getUnsafe();
    static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
    static final TestUnstableStable HOLDER = new TestUnstableStable();

    @Stable Integer stableField = null;
    static @Stable Integer staticStableField = null;
    static @Stable Integer[] stableArray0 = new Integer[1];
    static @Stable Integer[][] stableArray1 = new Integer[1][1];

    static final Integer finalField = 43;

    static final long FIELD_OFFSET;
    static {
        try {
            FIELD_OFFSET = U.staticFieldOffset(TestUnstableStable.class.getDeclaredField("finalField"));
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("Field not found", e);
        }
    }

    static class Writer implements Runnable {
        public void run() {
            while (true) {
                HOLDER.stableField = null;
                HOLDER.stableField = 42;
                HOLDER.stableField = 43;
                staticStableField = null;
                staticStableField = 42;
                staticStableField = 43;
                stableArray0[0] = null;
                stableArray0[0] = 42;
                stableArray0[0] = 43;
                stableArray1[0] = null;
                Integer[] tmp1 = {null};
                stableArray1[0] = tmp1;
                Integer[] tmp2 = {42};
                stableArray1[0] = tmp2;
                Integer[] tmp3 = {43};
                stableArray1[0] = tmp3;
                stableArray1[0][0] = null;
                stableArray1[0][0] = 42;
                stableArray1[0][0] = 43;
                U.putObject(TestUnstableStable.class, FIELD_OFFSET, null);
                U.putObject(TestUnstableStable.class, FIELD_OFFSET, 42);
                U.putObject(TestUnstableStable.class, FIELD_OFFSET, 43);
            }
        }
    }

    static Object testNonStatic() {
        // Trigger PhaseCCP and LoadNode::Value -> Type::make_constant_from_field
        // which may observe different values of the stable field when invoked twice.
        Integer val = HOLDER.stableField;
        if (val == null) {
            val = null;
        }
        return val;
    }

    static Object testStatic() {
        Integer val = staticStableField;
        if (val == null) {
            val = null;
        }
        return val;
    }

    static Object testArray0() {
        Integer val = stableArray0[0];
        if (val == null) {
            val = null;
        }
        return val;
    }

    static Object testArray1() {
        Integer[] val = stableArray1[0];
        if (val == null) {
            val = null;
        } else {
            return val[0];
        }
        return val;
    }

    static Object testFinal() {
        Integer val = finalField;
        if (val == null) {
            val = null;
        }
        return val;
    }

    public static void main(String[] args) throws Exception {
        Thread t = new Thread(new Writer());
        t.start();
        Method testNonStatic = TestUnstableStable.class.getDeclaredMethod("testNonStatic");
        Method testStatic = TestUnstableStable.class.getDeclaredMethod("testStatic");
        Method testArray0 = TestUnstableStable.class.getDeclaredMethod("testArray0");
        Method testArray1 = TestUnstableStable.class.getDeclaredMethod("testArray1");
        Method testFinal = TestUnstableStable.class.getDeclaredMethod("testFinal");
        testFinal();
        for (int i = 0; i < 1000; ++i) {
            WHITE_BOX.deoptimizeMethod(testNonStatic, false);
            WHITE_BOX.enqueueMethodForCompilation(testNonStatic, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
            WHITE_BOX.deoptimizeMethod(testStatic, false);
            WHITE_BOX.enqueueMethodForCompilation(testStatic, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
            WHITE_BOX.deoptimizeMethod(testArray0, false);
            WHITE_BOX.enqueueMethodForCompilation(testArray0, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
            WHITE_BOX.deoptimizeMethod(testArray1, false);
            WHITE_BOX.enqueueMethodForCompilation(testArray1, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
            WHITE_BOX.deoptimizeMethod(testFinal, false);
            WHITE_BOX.enqueueMethodForCompilation(testFinal, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
        }
    }
}