File: TestPartialPeelAtUnsignedTestsNegativeLimit.java

package info (click to toggle)
openjdk-25 25~32ea-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,280 kB
  • sloc: java: 5,584,902; cpp: 1,333,941; xml: 1,321,242; ansic: 487,993; asm: 404,003; objc: 21,088; sh: 15,102; javascript: 13,265; python: 8,319; makefile: 2,515; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (327 lines) | stat: -rw-r--r-- 11,956 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (c) 2024, 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 id=Xbatch
 * @bug 8332920
 * @summary Tests partial peeling at unsigned tests with limit being negative in exit tests "i >u limit".
 * @run main/othervm -Xbatch -XX:-TieredCompilation
 *                   -XX:CompileOnly=*TestPartialPeel*::original*,*TestPartialPeel*::test*
 *                   compiler.loopopts.TestPartialPeelAtUnsignedTestsNegativeLimit
 */

/*
 * @test id=Xcomp-run-inline
 * @bug 8332920
 * @summary Tests partial peeling at unsigned tests with limit being negative in exit tests "i >u limit".
 * @run main/othervm -Xcomp -XX:-TieredCompilation
 *                   -XX:CompileOnly=*TestPartialPeel*::original*,*TestPartialPeel*::run*,*TestPartialPeel*::test*
 *                   -XX:CompileCommand=inline,*TestPartialPeelAtUnsignedTestsNegativeLimit::test*
 *                   -XX:CompileCommand=dontinline,*TestPartialPeelAtUnsignedTestsNegativeLimit::check
 *                   compiler.loopopts.TestPartialPeelAtUnsignedTestsNegativeLimit
 */

/*
 * @test id=Xcomp-compile-test
 * @bug 8332920
 * @summary Tests partial peeling at unsigned tests with limit being negative in exit tests "i >u limit".
 * @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=*TestPartialPeel*::original*,*TestPartialPeel*::test*
 *                   compiler.loopopts.TestPartialPeelAtUnsignedTestsNegativeLimit
 */

/*
 * @test id=vanilla
 * @bug 8332920
 * @requires vm.flavor == "server" & (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4)
 * @summary Tests partial peeling at unsigned tests with limit being negative in exit tests "i >u limit".
 *          Only run this test with C2 since it is time-consuming and only tests a C2 issue.
 * @run main compiler.loopopts.TestPartialPeelAtUnsignedTestsNegativeLimit
 */

package compiler.loopopts;

import java.util.Random;

import static java.lang.Integer.*;

public class TestPartialPeelAtUnsignedTestsNegativeLimit {
    static int iFld = 10000;
    static int iterations = 0;
    static int iFld2;
    static boolean flag;
    final static Random RANDOM = new Random();

    public static void main(String[] args) {
        compareUnsigned(3, 3); // Load Integer class for -Xcomp
        for (int i = 0; i < 2; i++) {
            if (!originalTest()) {
                throw new RuntimeException("originalTest() failed");
            }
        }

        for (int i = 0; i < 2000; i++) {
            // For profiling
            iFld = -1;
            originalTestVariation1();

            // Actual run
            iFld = MAX_VALUE - 100_000;
            if (!originalTestVariation1()) {
                throw new RuntimeException("originalTestVariation1() failed");
            }
        }

        for (int i = 0; i < 2000; ++i) {
            // For profiling
            iFld = MAX_VALUE;
            originalTestVariation2();

            // Actual run
            iFld = MIN_VALUE + 100000;
            if (!originalTestVariation2()) {
                throw new RuntimeException("originalTestVariation2() failed");
            }
        }

        runWhileLTIncr();
        runWhileLTDecr();
    }

    // Originally reported simplified regression test with 2 variations (see below).
    public static boolean originalTest() {
        for (int i = MAX_VALUE - 50_000; compareUnsigned(i, -1) < 0; i++) {
            if (compareUnsigned(MIN_VALUE, i) < 0) {
                return true;
            }
        }
        return false;
    }

    public static boolean originalTestVariation1() {
        int a = 0;
        for (int i = iFld; compareUnsigned(i, -1) < 0; ++i) { // i <u -1

            if (i >= Integer.MIN_VALUE + 1 && i <= 100) { // Transformed to unsigned test.
                return true;
            }
            a *= 23;
        }
        return false;
    }

    public static boolean originalTestVariation2() {
        int a = 0;
        for (int i = iFld; compareUnsigned(i, -1000) < 0; i--) { // i <u -1
            if (compareUnsigned(MAX_VALUE - 20, i) > 0) {
                return true;
            }
            a = i;
        }
        System.out.println(a);
        return false;
    }


    public static void testWhileLTIncr(int init, int limit) {
        int i = init;
        while (true) {
            // <Peeled Section>

            // Found as loop head in ciTypeFlow, but both paths inside loop -> head not cloned.
            // As a result, this head has the safepoint as backedge instead of the loop exit test
            // and we cannot create a counted loop (yet). We first need to partial peel.
            if (flag) {
            }

            iFld2++;

            // Loop exit test i >=u limit (i.e. "while (i <u limit)") to partial peel with.
            // insert_cmpi_loop_exit() changes this exit condition into a signed and an unsigned test:
            //   i >= limit && i >=u limit
            // where the signed condition can be used as proper loop exit condition for a counted loop
            // (we cannot use an unsigned counted loop exit condition).
            //
            // After Partial Peeling, we have:
            //   if (i >= limit) goto Exit
            // Loop:
            //   if (i >=u limit) goto Exit
            //   ...
            //   i++;
            //   if (i >= limit) goto Exit
            //   goto Loop
            // Exit:
            //   ...
            //
            // If init = MAX_VALUE and limit = MIN_VALUE:
            //   i >= limit
            //   MAX_VALUE >= MIN_VALUE
            // which is true where
            //   i >=u limit
            //   MAX_VALUE >=u MIN_VALUE
            //   MAX_VALUE >=u (uint)(MAX_INT + 1)
            // is false and we wrongly never enter the loop even though we should have.
            // This results in a wrong execution.
            if (compareUnsigned(i, limit) >= 0) {
                return;
            }
            // <-- Partial Peeling CUT -->
            // Safepoint
            // <Unpeeled Section>
            iterations++;
            i++;
        }
    }

    // Same as testWhileLTIncr() but with decrement instead.
    public static void testWhileLTDecr(int init, int limit) {
        int i = init;
        while (true) {
            if (flag) {
            }

            // Loop exit test.
            if (compareUnsigned(i, limit) >= 0) { // While (i <u limit)
                return;
            }

            iterations++;
            i--;
        }
    }

    public static void runWhileLTIncr() {
        // Currently works:
        testWhileLTIncr(MAX_VALUE, -1);
        check(MIN_VALUE); // MAX_VALUE + 1 iterations
        testWhileLTIncr(-1, 1);
        check(0);
        testWhileLTIncr(0, 0);
        check(0);
        checkIncrWithRandom(0, 0); // Sanity check this method.
        flag = !flag; // Change profiling
        testWhileLTIncr(MAX_VALUE - 2000, MAX_VALUE);
        check(2000);
        testWhileLTIncr(MAX_VALUE - 1990, MAX_VALUE);
        check(1990);
        testWhileLTIncr(MAX_VALUE - 1, MAX_VALUE);
        check(1);
        testWhileLTIncr(MIN_VALUE, MIN_VALUE + 2000);
        check(2000);
        testWhileLTIncr(MIN_VALUE, MIN_VALUE + 1990);
        check(1990);
        testWhileLTIncr(MIN_VALUE, MIN_VALUE + 1);
        check(1);

        flag = !flag;
        // Overflow currently does not work with negative limit and is fixed with patch:
        testWhileLTIncr(MAX_VALUE, MIN_VALUE);
        check(1);
        testWhileLTIncr(MAX_VALUE - 2000, MIN_VALUE);
        check(2001);
        testWhileLTIncr(MAX_VALUE, MIN_VALUE + 2000);
        check(2001);
        testWhileLTIncr(MAX_VALUE - 2000, MIN_VALUE + 2000);
        check(4001);

        // Random values
        int init = RANDOM.nextInt(0, MAX_VALUE);
        int limit = RANDOM.nextInt(MIN_VALUE, 0);
        testWhileLTIncr(init, limit);
        checkIncrWithRandom(init, limit);
    }

    public static void runWhileLTDecr() {
        // Currently works:
        testWhileLTDecr(1, -1);
        check(2);
        testWhileLTDecr(-1, 1);
        check(0);
        testWhileLTDecr(0, 0);
        check(0);
        checkDecrWithRandom(0, 0); // Sanity check this method.
        flag = !flag;
        testWhileLTDecr(MAX_VALUE, MIN_VALUE);
        check(MIN_VALUE); // MAX_VALUE + 1 iterations
        testWhileLTDecr(MAX_VALUE, -1);
        check(MIN_VALUE); // MAX_VALUE + 1 iterations
        testWhileLTDecr(MAX_VALUE, MIN_VALUE);
        check(MIN_VALUE); // MAX_VALUE + 1 iterations
        testWhileLTDecr(MIN_VALUE, 0);
        check(0);
        testWhileLTDecr(MIN_VALUE, 1);
        check(0);
        flag = !flag;

        // Underflow currently does not work with negative limit and is fixed with patch:
        testWhileLTDecr(MIN_VALUE, -1);
        check(MIN_VALUE + 1); // MAX_VALUE + 2 iterations
        testWhileLTDecr(MIN_VALUE, -2000);
        check(MIN_VALUE + 1); // MAX_VALUE + 2 iterations
        testWhileLTDecr(MIN_VALUE, MIN_VALUE + 1);
        check(MIN_VALUE + 1); // MAX_VALUE + 2 iterations
        testWhileLTDecr(MIN_VALUE + 2000, -1);
        check(MIN_VALUE + 2001); // MAX_VALUE + 2002 iterations
        testWhileLTDecr(MIN_VALUE + 2000, -2000);
        check(MIN_VALUE + 2001); // MAX_VALUE + 2002 iterations
        testWhileLTDecr(MIN_VALUE + 2000, MIN_VALUE + 2001);
        check(MIN_VALUE + 2001); // MAX_VALUE + 2002 iterations

        // Random values
        int r1 = RANDOM.nextInt(MIN_VALUE, 0);
        int r2 = RANDOM.nextInt(MIN_VALUE, 0);
        int init = Math.min(r1, r2);
        int limit = Math.max(r1, r2);
        testWhileLTDecr(init, limit);
        checkDecrWithRandom(init, limit);
    }

    static void check(int expectedIterations) {
        if (expectedIterations != iterations) {
            throw new RuntimeException("Expected " + expectedIterations + " iterations but only got " + iterations);
        }
        iterations = 0; // Reset
    }

    static void checkIncrWithRandom(long init, long limit) {
        long expectedIterations = ((long)(MAX_VALUE) - init) + (limit - (long)MIN_VALUE) + 1;
        if ((int)expectedIterations != iterations) {
            String error = "Expected %d iterations but only got %d, init: %d, limit: %d"
                            .formatted(expectedIterations, iterations, init, limit);
            throw new RuntimeException(error);
        }
        iterations = 0; // Reset
    }

    static void checkDecrWithRandom(long init, long limit) {
        long expectedIterations = init + MIN_VALUE + MAX_VALUE + 2;
        if (init == limit) {
            expectedIterations = 0;
        }
        if ((int)expectedIterations != iterations) {
            String error = "Expected %d iterations but only got %d, init: %d, limit: %d"
                    .formatted(expectedIterations, iterations, init, limit);
            throw new RuntimeException(error);
        }
        iterations = 0; // Reset
    }
}