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