1 /*
2  * Copyright (c) 2021, Red Hat, Inc. 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  * @test
26  * @bug 8261812
27  * @summary C2 compilation fails with assert(!had_error) failed: bad dominance
28  *
29  * @run main/othervm -XX:-BackgroundCompilation TestValAtSafepointOverflowsInt
30  *
31  */
32 
33 public class TestValAtSafepointOverflowsInt {
34     private static volatile int volatileField;
35 
main(String[] args)36     public static void main(String[] args) {
37         for (int i = 0; i < 20_000; i++) {
38             testByte(true, false);
39             testByte(false, false);
40             testShort(true, false);
41             testShort(false, false);
42             testChar(true, false);
43             testChar(false, false);
44         }
45         testByte(true, true);
46         testShort(true, true);
47         testChar(true, true);
48     }
49 
testByte(boolean flag, boolean flag2)50     private static Object testByte(boolean flag, boolean flag2) {
51         int i;
52         // loop to delay constant folding
53         for (i = 0; i < 9; i++) {
54         }
55         C obj = new C();
56         if (flag) {
57             obj.byteField = (byte)(1 << i);
58         } else {
59             obj.byteField = (byte)(1 << (i+1));
60         }
61         // Phi for byte here for uncommon trap in never taken path below
62         // Phi inputs don't fit in a byte. Phi transfomed to top.
63         if (flag2) {
64             return obj;
65         }
66         return null;
67     }
68 
testShort(boolean flag, boolean flag2)69     private static Object testShort(boolean flag, boolean flag2) {
70         int i;
71         for (i = 0; i < 17; i++) {
72         }
73         C obj = new C();
74         if (flag) {
75             obj.shortField = (short)(1 << i);
76         } else {
77             obj.shortField = (short)(1 << (i+1));
78         }
79         if (flag2) {
80             return obj;
81         }
82         return null;
83     }
84 
testChar(boolean flag, boolean flag2)85     private static Object testChar(boolean flag, boolean flag2) {
86         int i;
87         for (i = 0; i < 17; i++) {
88         }
89         C obj = new C();
90         if (flag) {
91             obj.charField = (char)(1 << i);
92         } else {
93             obj.charField = (char)(1 << (i+1));
94         }
95         if (flag2) {
96             return obj;
97         }
98         return null;
99     }
100 
101 
102     static class C {
103         byte byteField;
104         short shortField;
105         char charField;
106     }
107 
108 }
109