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