1 /*
  2  * Copyright (c) 2021, 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  * @test
 26  * @bug 8260034 8260225 8260283 8261037 8261874 8262128 8262831
 27  * @summary Generated inline type tests.
 28  * @run main/othervm -Xbatch
 29  *                   compiler.valhalla.inlinetypes.TestGenerated
 30  * @run main/othervm -Xbatch -XX:FlatArrayElementMaxSize=0
 31  *                   compiler.valhalla.inlinetypes.TestGenerated
 32  */
 33 
 34 package compiler.valhalla.inlinetypes;
 35 
 36 primitive class EmptyValue {
 37 
 38 }
 39 
 40 primitive class MyValue1 {
 41     int x = 42;
 42     int[] array = new int[1];
 43 }
 44 
 45 primitive class MyValue2 {
 46     int[] a = new int[1];
 47     int[] b = new int[6];
 48     int[] c = new int[5];
 49 }
 50 
 51 primitive class MyValue3 {
 52     int[] intArray = new int[1];
 53     float[] floatArray = new float[1];
 54 }
 55 
 56 primitive class MyValue4 {
 57     short b = 2;
 58     int c = 8;
 59 }
 60 
 61 class MyValue4Wrapper {
 62     public MyValue4.ref val;
 63 
 64     public MyValue4Wrapper(MyValue4 val) {
 65         this.val = val;
 66     }
 67 }
 68 
 69 primitive class MyValue5 {
 70     int b = 2;
 71 }
 72 
 73 public class TestGenerated {
 74     EmptyValue f1 = new EmptyValue();
 75     EmptyValue f2 = new EmptyValue();
 76 
 77     void test1(EmptyValue[] array) {
 78         for (int i = 0; i < 10; ++i) {
 79             f1 = array[0];
 80             f2 = array[0];
 81         }
 82     }
 83 
 84     MyValue1 test2(MyValue1[] array) {
 85         MyValue1 res = MyValue1.default;
 86         for (int i = 0; i < array.length; ++i) {
 87             res = array[i];
 88         }
 89         for (int i = 0; i < 1000; ++i) {
 90 
 91         }
 92         return res;
 93     }
 94 
 95     void test3(MyValue1[] array) {
 96         for (int i = 0; i < array.length; ++i) {
 97             array[i] = MyValue1.default;
 98         }
 99         for (int i = 0; i < 1000; ++i) {
100 
101         }
102     }
103 
104     void test4(MyValue1[] array) {
105         array[0].array[0] = 0;
106     }
107 
108     int test5(MyValue1[] array) {
109         return array[0].array[0];
110     }
111 
112     long f3;
113     MyValue1 f4 = new MyValue1();
114 
115     void test6() {
116         f3 = 123L;
117         int res = f4.x;
118         if (res != 42) {
119             throw new RuntimeException("test6 failed");
120         }
121     }
122 
123     MyValue2 f5;
124 
125     void test7(boolean b) {
126         MyValue2[] array1 = {new MyValue2(), new MyValue2(), new MyValue2(),
127                              new MyValue2(), new MyValue2(), new MyValue2()};
128         MyValue2 h = new MyValue2();
129         MyValue2 n = new MyValue2();
130         int[] array2 = new int[1];
131 
132         for (int i = 0; i < 10; ++i) {
133           for (int j = 0; j < 10; ++j) {
134             array1[0] = array1[0];
135             if (i == 1) {
136               h = h;
137               array2[0] *= 42;
138             }
139           }
140         }
141         if (b) {
142           f5 = n;
143         }
144     }
145 
146     boolean test8(MyValue1[] array) {
147         return array[0].array == array[0].array;
148     }
149 
150     void test9(boolean b) {
151         MyValue1[] array = { new MyValue1() };
152         if (b) {
153             for (int i = 0; i < 10; ++i) {
154                 if (array != array) {
155                     array = null;
156                 }
157             }
158         }
159     }
160 
161     int[] f6 = new int[1];
162 
163     void test10(MyValue3[] array) {
164         float[] floatArray = array[0].floatArray;
165         if (f6 == f6) {
166             f6 = array[0].intArray;
167         }
168     }
169 
170     void test11(MyValue3[] array) {
171         float[] floatArray = array[0].floatArray;
172         if (array[0].intArray[0] != 42) {
173             throw new RuntimeException("test11 failed");
174         }
175     }
176 
177     MyValue4[] d = {new MyValue4()};
178     MyValue4 e;
179     byte f;
180     byte test12() {
181         MyValue4 i = new MyValue4();
182         for (int j = 0; j < 6; ++j) {
183             MyValue4[] k = {};
184             if (i.b < 101) {
185                 i = e;
186             }
187             for (int l = 0; l < 9; ++l) {
188                 MyValue4 m = new MyValue4();
189                 i = m;
190             }
191         }
192         if (d[0].c > 1) {
193             for (int n = 0; n < 7; ++n) {
194             }
195         }
196         return f;
197     }
198 
199     int test13_iField;
200     MyValue5 test13_c;
201     MyValue5 test13_t;
202 
203     void test13(MyValue5[] array) {
204         for (int i = 0; i < 10; ++i) {
205             for (int j = 0; j < 10; ++j) {
206                 test13_iField = 6;
207             }
208             for (int j = 0; j < 2; ++j) {
209                 test13_iField += array[0].b;
210             }
211             MyValue5[] array2 = {new MyValue5()};
212             test13_c = array[0];
213             array2[0] = test13_t;
214         }
215     }
216 
217     void test14(boolean b, MyValue4 val) {
218         for (int i = 0; i < 10; ++i) {
219             if (b) {
220                 val = MyValue4.default;
221             }
222             MyValue4[] array = new MyValue4[1];
223             array[0] = val;
224 
225             for (int j = 0; j < 5; ++j) {
226                 for (int k = 0; k < 5; ++k) {
227                 }
228             }
229         }
230     }
231 
232     void test15() {
233         MyValue4 val = new MyValue4();
234         for (int i = 0; i < 10; ++i) {
235             for (int j = 0; j < 10; ++j) {
236                 MyValue4[] array = new MyValue4[1];
237                 for (int k = 0; k < 10; ++k) {
238                     array[0] = val;
239                     val = array[0];
240                 }
241             }
242         }
243     }
244 
245     void test16() {
246         MyValue4 val = MyValue4.default;
247         for (int i = 0; i < 10; ++i) {
248             for (int j = 0; j < 10; ++j) {
249                 val = (new MyValue4Wrapper(val)).val;
250                 for (int k = 0; k < 10; ++k) {
251                 }
252             }
253         }
254     }
255 
256     public static void main(String[] args) {
257         TestGenerated t = new TestGenerated();
258         EmptyValue[] array1 = { new EmptyValue() };
259         MyValue1[] array2 = new MyValue1[10];
260         MyValue1[] array3 = { new MyValue1() };
261         MyValue3[] array4 = { new MyValue3() };
262         MyValue5[] array5 = { new MyValue5() };
263         array4[0].intArray[0] = 42;
264 
265         for (int i = 0; i < 50_000; ++i) {
266             t.test1(array1);
267             t.test2(array2);
268             t.test3(array2);
269             t.test4(array3);
270             t.test5(array3);
271             t.test6();
272             t.test7(false);
273             t.test8(array3);
274             t.test9(true);
275             t.test10(array4);
276             t.test11(array4);
277             t.test12();
278             t.test13(array5);
279             t.test14(false, MyValue4.default);
280             t.test15();
281             t.test16();
282         }
283     }
284 }