File: TypeSpeculation.java

package info (click to toggle)
openjdk-8-jre-dcevm 8u112-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 65,396 kB
  • sloc: cpp: 570,249; java: 257,943; xml: 16,359; ansic: 9,543; asm: 2,774; sh: 2,403; makefile: 2,391
file content (563 lines) | stat: -rw-r--r-- 14,181 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 * Copyright (c) 2013, 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 8024070
 * @summary Test that type speculation doesn't cause incorrect execution
 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation TypeSpeculation
 *
 */

public class TypeSpeculation {

    interface I {
    }

    static class A {
        int m() {
            return 1;
        }
    }

    static class B extends A implements I {
        int m() {
            return 2;
        }
    }

    static class C extends B {
        int m() {
            return 3;
        }
    }

    static int test1_invokevirtual(A a) {
        return a.m();
    }

    static int test1_1(A a) {
        return test1_invokevirtual(a);
    }

    static boolean test1() {
        A a = new A();
        B b = new B();
        C c = new C();

        // pollute profile at test1_invokevirtual to make sure the
        // compiler cannot rely on it
        for (int i = 0; i < 5000; i++) {
            test1_invokevirtual(a);
            test1_invokevirtual(b);
            test1_invokevirtual(c);
        }

        // profiling + speculation should make test1_invokevirtual
        // inline A.m() with a guard
        for (int i = 0; i < 20000; i++) {
            int res = test1_1(b);
            if (res != b.m()) {
                System.out.println("test1 failed with class B");
                return false;
            }
        }
        // check that the guard works as expected by passing a
        // different type
        int res = test1_1(a);
        if (res != a.m()) {
            System.out.println("test1 failed with class A");
            return false;
        }
        return true;
    }

    static int test2_invokevirtual(A a) {
        return a.m();
    }

    static int test2_1(A a, boolean t) {
        A aa;
        if (t) {
            aa = (B)a;
        } else {
            aa = a;
        }
        // if a of type B is passed to test2_1, the static type of aa
        // here is no better than A but the profiled type is B so this
        // should inline
        return test2_invokevirtual(aa);
    }

    static boolean test2() {
        A a = new A();
        B b = new B();
        C c = new C();

        // pollute profile at test2_invokevirtual to make sure the
        // compiler cannot rely on it
        for (int i = 0; i < 5000; i++) {
            test2_invokevirtual(a);
            test2_invokevirtual(b);
            test2_invokevirtual(c);
        }

        // profiling + speculation should make test2_invokevirtual
        // inline A.m() with a guard
        for (int i = 0; i < 20000; i++) {
            int res = test2_1(b, (i % 2) == 0);
            if (res != b.m()) {
                System.out.println("test2 failed with class B");
                return false;
            }
        }
        // check that the guard works as expected by passing a
        // different type
        int res = test2_1(a, false);
        if (res != a.m()) {
            System.out.println("test2 failed with class A");
            return false;
        }
        return true;
    }

    static int test3_invokevirtual(A a) {
        return a.m();
    }

    static void test3_2(A a) {
    }

    static int test3_1(A a, int i) {
        if (i == 0) {
            return 0;
        }
        // If we come here and a is of type B but parameter profiling
        // is polluted, both branches of the if below should have
        // profiling that tell us and inlining of the virtual call
        // should happen
        if (i == 1) {
            test3_2(a);
        } else {
            test3_2(a);
        }
        return test3_invokevirtual(a);
    }

    static boolean test3() {
        A a = new A();
        B b = new B();
        C c = new C();

        // pollute profile at test3_invokevirtual and test3_1 to make
        // sure the compiler cannot rely on it
        for (int i = 0; i < 3000; i++) {
            test3_invokevirtual(a);
            test3_invokevirtual(b);
            test3_invokevirtual(c);
            test3_1(a, 0);
            test3_1(b, 0);
        }

        // profiling + speculation should make test3_invokevirtual
        // inline A.m() with a guard
        for (int i = 0; i < 20000; i++) {
            int res = test3_1(b, (i % 2) + 1);
            if (res != b.m()) {
                System.out.println("test3 failed with class B");
                return false;
            }
        }
        // check that the guard works as expected by passing a
        // different type
        int res = test3_1(a, 1);
        if (res != a.m()) {
            System.out.println("test3 failed with class A");
            return false;
        }
        return true;
    }

    // Mix 2 incompatible profiled types
    static int test4_invokevirtual(A a) {
        return a.m();
    }

    static void test4_2(A a) {
    }

    static int test4_1(A a, boolean b) {
        if (b) {
            test4_2(a);
        } else {
            test4_2(a);
        }
        // shouldn't inline
        return test4_invokevirtual(a);
    }

    static boolean test4() {
        A a = new A();
        B b = new B();
        C c = new C();

        // pollute profile at test3_invokevirtual and test3_1 to make
        // sure the compiler cannot rely on it
        for (int i = 0; i < 3000; i++) {
            test4_invokevirtual(a);
            test4_invokevirtual(b);
            test4_invokevirtual(c);
        }

        for (int i = 0; i < 20000; i++) {
            if ((i % 2) == 0) {
                int res = test4_1(a, true);
                if (res != a.m()) {
                    System.out.println("test4 failed with class A");
                    return false;
                }
            } else {
                int res = test4_1(b, false);
                if (res != b.m()) {
                    System.out.println("test4 failed with class B");
                    return false;
                }
            }
        }
        return true;
    }

    // Mix one profiled type with an incompatible type
    static int test5_invokevirtual(A a) {
        return a.m();
    }

    static void test5_2(A a) {
    }

    static int test5_1(A a, boolean b) {
        if (b) {
            test5_2(a);
        } else {
            A aa = (B)a;
        }
        // shouldn't inline
        return test5_invokevirtual(a);
    }

    static boolean test5() {
        A a = new A();
        B b = new B();
        C c = new C();

        // pollute profile at test3_invokevirtual and test3_1 to make
        // sure the compiler cannot rely on it
        for (int i = 0; i < 3000; i++) {
            test5_invokevirtual(a);
            test5_invokevirtual(b);
            test5_invokevirtual(c);
        }

        for (int i = 0; i < 20000; i++) {
            if ((i % 2) == 0) {
                int res = test5_1(a, true);
                if (res != a.m()) {
                    System.out.println("test5 failed with class A");
                    return false;
                }
            } else {
                int res = test5_1(b, false);
                if (res != b.m()) {
                    System.out.println("test5 failed with class B");
                    return false;
                }
            }
        }
        return true;
    }

    // Mix incompatible profiled types
    static void test6_2(Object o) {
    }

    static Object test6_1(Object o, boolean b) {
        if (b) {
            test6_2(o);
        } else {
            test6_2(o);
        }
        return o;
    }

    static boolean test6() {
        A a = new A();
        A[] aa = new A[10];

        for (int i = 0; i < 20000; i++) {
            if ((i % 2) == 0) {
                test6_1(a, true);
            } else {
                test6_1(aa, false);
            }
        }
        return true;
    }

    // Mix a profiled type with an incompatible type
    static void test7_2(Object o) {
    }

    static Object test7_1(Object o, boolean b) {
        if (b) {
            test7_2(o);
        } else {
            Object oo = (A[])o;
        }
        return o;
    }

    static boolean test7() {
        A a = new A();
        A[] aa = new A[10];

        for (int i = 0; i < 20000; i++) {
            if ((i % 2) == 0) {
                test7_1(a, true);
            } else {
                test7_1(aa, false);
            }
        }
        return true;
    }

    // Mix a profiled type with an interface
    static void test8_2(Object o) {
    }

    static I test8_1(Object o) {
        test8_2(o);
        return (I)o;
    }

    static boolean test8() {
        A a = new A();
        B b = new B();
        C c = new C();

        for (int i = 0; i < 20000; i++) {
            test8_1(b);
        }
        return true;
    }

    // Mix a profiled type with a constant
    static void test9_2(Object o) {
    }

    static Object test9_1(Object o, boolean b) {
        Object oo;
        if (b) {
            test9_2(o);
            oo = o;
        } else {
            oo = "some string";
        }
        return oo;
    }

    static boolean test9() {
        A a = new A();

        for (int i = 0; i < 20000; i++) {
            if ((i % 2) == 0) {
                test9_1(a, true);
            } else {
                test9_1(a, false);
            }
        }
        return true;
    }

    // java/lang/Object:AnyNull:exact *,iid=top
    // meets
    // stable:bottom[int:max..0]:NotNull *
    static void test10_4(Object o) {
    }

    static void test10_3(Object o, boolean b) {
        if (b) {
            test10_4(o);
        }
    }

    static void test10_2(Object o, boolean b1, boolean b2) {
        if (b1) {
            test10_3(o, b2);
        }
    }

    static void test10_1(B[] b, boolean b1, boolean b2) {
        test10_2(b, b1, b2);
    }

    static boolean test10() {
        Object o = new Object();
        A[] a = new A[10];
        B[] b = new B[10];
        B[] c = new C[10];
        for (int i = 0; i < 20000; i++) {
            test10_1(b, false, false);
            test10_1(c, false, false);
            test10_2(a, true, false);
            test10_3(o, true);
        }
        return true;
    }

    // stable:TypeSpeculation$B:TopPTR *,iid=top[int:max..0]:TopPTR *,iid=top
    // meets
    // java/lang/Object:AnyNull:exact *,iid=top
    static void test11_3(Object o) {
    }

    static void test11_2(Object o, boolean b) {
        if (b) {
            test11_3(o);
        }
    }

    static void test11_1(B[] b, boolean bb) {
        test11_2(b, bb);
    }

    static boolean test11() {
        Object o = new Object();
        B[] b = new B[10];
        B[] c = new C[10];
        for (int i = 0; i < 20000; i++) {
            test11_1(b, false);
            test11_1(c, false);
            test11_2(o, true);
        }
        return true;
    }

    // TypeSpeculation$I *
    // meets
    // java/lang/Object:AnyNull *,iid=top
    static void test12_3(Object o) {
    }

    static void test12_2(Object o, boolean b) {
        if (b) {
            test12_3(o);
        }
    }

    static void test12_1(I i, boolean b) {
        test12_2(i, b);
    }

    static boolean test12() {
        Object o = new Object();
        B b = new B();
        C c = new C();
        for (int i = 0; i < 20000; i++) {
            test12_1(b, false);
            test12_1(c, false);
            test12_2(o, true);
        }
        return true;
    }

    // stable:bottom[int:max..0]:NotNull *
    // meets
    // stable:TypeSpeculation$A:TopPTR *,iid=top[int:max..0]:AnyNull:exact *,iid=top
    static Object test13_3(Object o, boolean b) {
        Object oo;
        if (b) {
            oo = o;
        } else {
            oo = new A[10];
        }
        return oo;
    }

    static void test13_2(Object o, boolean b1, boolean b2) {
        if (b1) {
            test13_3(o, b2);
        }
    }

    static void test13_1(B[] b, boolean b1, boolean b2) {
        test13_2(b, b1, b2);
    }

    static boolean test13() {
        A[] a = new A[10];
        B[] b = new B[10];
        B[] c = new C[10];
        for (int i = 0; i < 20000; i++) {
            test13_1(b, false, false);
            test13_1(c, false, false);
            test13_2(a, true, (i%2) == 0);
        }
        return true;
    }

    static public void main(String[] args) {
        boolean success = true;

        success = test1() && success;

        success = test2() && success;

        success = test3() && success;

        success = test4() && success;

        success = test5() && success;

        success = test6() && success;

        success = test7() && success;

        success = test8() && success;

        success = test9() && success;

        success = test10() && success;

        success = test11() && success;

        success = test12() && success;

        success = test13() && success;

        if (success) {
            System.out.println("TEST PASSED");
        } else {
            throw new RuntimeException("TEST FAILED: erroneous bound check elimination");
        }
    }
}