1 /*
  2  * Copyright (c) 2019, 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 package compiler.valhalla.inlinetypes;
 25 
 26 import compiler.lib.ir_framework.CompLevel;
 27 import compiler.lib.ir_framework.Run;
 28 import compiler.lib.ir_framework.Scenario;
 29 import compiler.lib.ir_framework.Test;
 30 import jdk.test.lib.Asserts;
 31 
 32 import static compiler.valhalla.inlinetypes.InlineTypes.rI;
 33 import static compiler.valhalla.inlinetypes.InlineTypes.rL;
 34 
 35 /*
 36  * @test
 37  * @key randomness
 38  * @summary Various tests that are specific for C1.
 39  * @library /test/lib /
 40  * @requires (os.simpleArch == "x64" | os.simpleArch == "aarch64")
 41  * @run driver/timeout=300 compiler.valhalla.inlinetypes.TestC1
 42  */
 43 
 44 public class TestC1 {
 45     public static void main(String[] args) {
 46         final Scenario[] scenarios = {
 47                 // C1 only
 48                 new Scenario(0,
 49                              "-XX:TieredStopAtLevel=1", "-XX:+TieredCompilation"),
 50                 // C2 only. (Make sure the tests are correctly written)
 51                 new Scenario(1,
 52                              "-XX:TieredStopAtLevel=4", "-XX:-TieredCompilation"),
 53                 // interpreter only
 54                 new Scenario(2,
 55                              "-Xint"),
 56                 // Xcomp Only C1.
 57                 new Scenario(3,
 58                              "-XX:TieredStopAtLevel=1", "-XX:+TieredCompilation", "-Xcomp"),
 59                 // Xcomp Only C2.
 60                 new Scenario(4,
 61                              "-XX:TieredStopAtLevel=4", "-XX:-TieredCompilation", "-Xcomp")
 62         };
 63 
 64         InlineTypes.getFramework()
 65                    .addScenarios(scenarios)
 66                    .addHelperClasses(MyValue1.class,
 67                                      MyValue2.class,
 68                                      MyValue2Inline.class,
 69                                      MyValue3.class,
 70                                      MyValue3Inline.class)
 71                    .start();
 72     }
 73 
 74     // JDK-8229799
 75     @Test(compLevel = CompLevel.C1_SIMPLE)
 76     public long test1(Object a, Object b, long n) {
 77         long r;
 78         n += (a == b) ? 0x5678123456781234L : 0x1234567812345678L;
 79         n -= 1;
 80         return n;
 81     }
 82 
 83     @Run(test = "test1")
 84     public void test1_verifier() {
 85         MyValue1 v1 = MyValue1.createWithFieldsInline(rI, rL);
 86         MyValue1 v2 = MyValue1.createWithFieldsInline(rI, rL+1);
 87         long r1 = test1(v1, v1, 1);
 88         long r2 = test1(v1, v2, 1);
 89         Asserts.assertEQ(r1, 0x5678123456781234L);
 90         Asserts.assertEQ(r2, 0x1234567812345678L);
 91     }
 92 
 93     static primitive class SimpleValue2 {
 94         final int value;
 95         SimpleValue2(int value) {
 96             this.value = value;
 97         }
 98     }
 99 
100     // JDK-8231961
101     // Test that the value numbering optimization does not remove
102     // the second load from the buffered array element.
103     @Test(compLevel = CompLevel.C1_SIMPLE)
104     public int test2(SimpleValue2[] array) {
105         return array[0].value + array[0].value;
106     }
107 
108     @Run(test = "test2")
109     public void test2_verifier() {
110         SimpleValue2[] array = new SimpleValue2[1];
111         array[0] = new SimpleValue2(rI);
112         int result = test2(array);
113         Asserts.assertEQ(result, 2*rI);
114     }
115 
116 
117     // Tests below (3 to 8) check the behavior of the C1 optimization to access
118     // sub-elements of a flattened array without copying the element first
119 
120     // Test access to a null array
121     @Test(compLevel = CompLevel.C1_SIMPLE)
122     public int test3(MyValue2[] array, int index) {
123         return array[index].x;
124     }
125 
126     @Run(test = "test3")
127     public void test3_verifier() {
128         NullPointerException npe = null;
129         try {
130             test3(null, 0);
131         } catch(NullPointerException e) {
132             npe = e;
133         }
134         Asserts.assertNE(npe, null);
135     }
136 
137     // Test out of bound accesses
138     @Test(compLevel = CompLevel.C1_SIMPLE)
139     public int test4(MyValue2[] array, int index) {
140         return array[index].x;
141     }
142 
143     @Run(test = "test4")
144     public void test4_verifier() {
145         MyValue2[] array = new MyValue2[2];
146         ArrayIndexOutOfBoundsException aioob = null;
147         try {
148             test3(array, -1);
149         } catch(ArrayIndexOutOfBoundsException e) {
150             aioob = e;
151         }
152         Asserts.assertNE(aioob, null);
153         aioob = null;
154         try {
155             test3(array, 2);
156         } catch(ArrayIndexOutOfBoundsException e) {
157             aioob = e;
158         }
159         Asserts.assertNE(aioob, null);
160     }
161 
162     // Test 1st level sub-element access to primitive field
163     @Test(compLevel = CompLevel.C1_SIMPLE)
164     public int test5(MyValue2[] array, int index) {
165         return array[index].x;
166     }
167 
168     @Run(test = "test5")
169     public void test5_verifier() {
170         MyValue2[] array = new MyValue2[2];
171         MyValue2 v = new MyValue2(1,(byte)2, new MyValue2Inline(5.0d, 345L));
172         array[1] = v;
173         int x = test5(array, 1);
174         Asserts.assertEQ(x, 1);
175     }
176 
177     // Test 1st level sub-element access to flattened field
178     @Test(compLevel = CompLevel.C1_SIMPLE)
179     public MyValue2Inline test6(MyValue2[] array, int index) {
180         return array[index].v;
181     }
182 
183     @Run(test = "test6")
184     public void test6_verifier() {
185         MyValue2[] array = new MyValue2[2];
186         MyValue2Inline vi = new MyValue2Inline(3.5d, 678L);
187         MyValue2 v = new MyValue2(1,(byte)2, vi);
188         array[0] = v;
189         MyValue2Inline vi2 = test6(array, 0);
190         Asserts.assertEQ(vi, vi2);
191     }
192 
193     // Test 1st level sub-element access to non-flattened field
194     static primitive class Big {
195         long l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18,l19 ;
196 
197         Big(long n) {
198             l0 = n++; l1 = n++; l2 = n++; l3 = n++; l4 = n++; l5 = n++; l6 = n++; l7 = n++; l8 = n++;
199             l9 = n++; l10 = n++; l11 = n++; l12 = n++; l13 = n++; l14 = n++; l15 = n++; l16= n++;
200             l17 = n++; l18 = n++; l19 = n++;
201         }
202 
203         void check(long n, int i) {
204             Asserts.assertEQ(l0, n); n += i;
205             Asserts.assertEQ(l1, n); n += i;
206             Asserts.assertEQ(l2, n); n += i;
207             Asserts.assertEQ(l3, n); n += i;
208             Asserts.assertEQ(l4, n); n += i;
209             Asserts.assertEQ(l5, n); n += i;
210             Asserts.assertEQ(l6, n); n += i;
211             Asserts.assertEQ(l7, n); n += i;
212             Asserts.assertEQ(l8, n); n += i;
213             Asserts.assertEQ(l9, n); n += i;
214             Asserts.assertEQ(l10, n); n += i;
215             Asserts.assertEQ(l11, n); n += i;
216             Asserts.assertEQ(l12, n); n += i;
217             Asserts.assertEQ(l13, n); n += i;
218             Asserts.assertEQ(l14, n); n += i;
219             Asserts.assertEQ(l15, n); n += i;
220             Asserts.assertEQ(l16, n); n += i;
221             Asserts.assertEQ(l17, n); n += i;
222             Asserts.assertEQ(l18, n); n += i;
223             Asserts.assertEQ(l19, n);
224         }
225     }
226 
227     static primitive class TestValue {
228         int i;
229         Big big;
230 
231         TestValue(int n) {
232             i = n;
233             big = new Big(n);
234         }
235     }
236 
237     @Test(compLevel = CompLevel.C1_SIMPLE)
238     public Big test7(TestValue[] array, int index) {
239         return array[index].big;
240     }
241 
242     @Run(test = "test7")
243     public void test7_verifier() {
244         TestValue[] array = new TestValue[7];
245         Big b0 = test7(array, 3);
246         b0.check(0, 0);
247         TestValue tv = new TestValue(9);
248         array[5] = tv;
249         Big b1 = test7(array, 5);
250         b1.check(9, 1);
251     }
252 
253     // Test 2nd level sub-element access to primitive field
254     @Test(compLevel = CompLevel.C1_SIMPLE)
255     public byte test8(MyValue1[] array, int index) {
256         return array[index].v2.y;
257     }
258 
259     @Run(test = "test8")
260     public void test8_verifier() {
261         MyValue1[] array = new MyValue1[23];
262         MyValue2 mv2a = MyValue2.createWithFieldsInline(7, 63L, 8.9d);
263         MyValue2 mv2b = MyValue2.createWithFieldsInline(11, 69L, 17.3d);
264         MyValue1 mv1 = new MyValue1(1, 2L, (short)3, 4, null, mv2a, mv2b, mv2a, mv2b, 'z');
265         array[19] = mv1;
266         byte b = test8(array, 19);
267         Asserts.assertEQ(b, (byte)11);
268     }
269 
270 
271     // Test optimizations for arrays of empty types
272     // (read/write are not performed, pre-allocated instance is used for reads)
273     // Most tests check that error conditions are still correctly handled
274     // (OOB, null pointer)
275     static primitive class EmptyType {}
276 
277     @Test(compLevel = CompLevel.C1_SIMPLE)
278     public EmptyType test9() {
279         EmptyType[] array = new EmptyType[10];
280         return array[4];
281     }
282 
283     @Run(test = "test9")
284     public void test9_verifier() {
285         EmptyType et = test9();
286         Asserts.assertEQ(et, EmptyType.default);
287     }
288 
289     @Test(compLevel = CompLevel.C1_SIMPLE)
290     public EmptyType test10(EmptyType[] array) {
291         return array[0];
292     }
293 
294     @Run(test = "test10")
295     public void test10_verifier() {
296         EmptyType[] array = new EmptyType[16];
297         EmptyType et = test10(array);
298         Asserts.assertEQ(et, EmptyType.default);
299     }
300 
301     @Test(compLevel = CompLevel.C1_SIMPLE)
302     public EmptyType test11(EmptyType[] array, int index) {
303         return array[index];
304     }
305 
306     @Run(test = "test11")
307     public void test11_verifier() {
308         Exception e = null;
309         EmptyType[] array = new EmptyType[10];
310         try {
311             EmptyType et = test11(array, 11);
312         } catch (ArrayIndexOutOfBoundsException ex) {
313             e = ex;
314         }
315         Asserts.assertNotNull(e);
316         e = null;
317         try {
318             EmptyType et = test11(array, -1);
319         } catch (ArrayIndexOutOfBoundsException ex) {
320             e = ex;
321         }
322         Asserts.assertNotNull(e);
323         e = null;
324         try {
325             EmptyType et = test11(null, 1);
326         } catch (NullPointerException ex) {
327             e = ex;
328         }
329         Asserts.assertNotNull(e);
330     }
331 
332     @Test(compLevel = CompLevel.C1_SIMPLE)
333     public void test12(EmptyType[] array, int index, EmptyType value) {
334         array[index] = value;
335     }
336 
337     @Run(test = "test12")
338     public void test12_verifier() {
339         EmptyType[] array = new EmptyType[16];
340         test12(array, 2, EmptyType.default);
341         Exception e = null;
342         try {
343             test12(null, 2, EmptyType.default);
344         } catch(NullPointerException ex) {
345             e = ex;
346         }
347         Asserts.assertNotNull(e);
348         e = null;
349         try {
350             test12(array, 17, EmptyType.default);
351         } catch(ArrayIndexOutOfBoundsException ex) {
352             e = ex;
353         }
354         Asserts.assertNotNull(e);
355         e = null;
356         try {
357             test12(array, -8, EmptyType.default);
358         } catch(ArrayIndexOutOfBoundsException ex) {
359             e = ex;
360         }
361         Asserts.assertNotNull(e);
362     }
363 }