|
1 /* |
|
2 * Copyright (c) 2017, 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 package org.graalvm.compiler.hotspot.test; |
|
24 |
|
25 import java.lang.reflect.Array; |
|
26 import java.util.ArrayList; |
|
27 |
|
28 import org.graalvm.compiler.api.directives.GraalDirectives; |
|
29 import org.graalvm.compiler.core.test.GraalCompilerTest; |
|
30 import org.junit.Test; |
|
31 import org.junit.runner.RunWith; |
|
32 import org.junit.runners.Parameterized; |
|
33 import org.junit.runners.Parameterized.Parameters; |
|
34 |
|
35 @RunWith(Parameterized.class) |
|
36 public class ArrayNewInstanceTest extends GraalCompilerTest { |
|
37 |
|
38 @Parameters(name = "{index}: class {0} length {1}") |
|
39 public static Iterable<Object[]> data() { |
|
40 ArrayList<Object[]> parameters = new ArrayList<>(); |
|
41 Class<?>[] classesToTest = new Class<?>[]{ |
|
42 byte.class, |
|
43 boolean.class, |
|
44 short.class, |
|
45 char.class, |
|
46 int.class, |
|
47 long.class, |
|
48 Void.class, |
|
49 ArrayNewInstanceTest.class |
|
50 }; |
|
51 for (Class<?> clazz : classesToTest) { |
|
52 // Negative sizes always deopt |
|
53 parameters.add(new Object[]{clazz, -1, true}); |
|
54 parameters.add(new Object[]{clazz, 0, false}); |
|
55 parameters.add(new Object[]{clazz, 42, false}); |
|
56 } |
|
57 // The void type always throws an exception where graal deopts |
|
58 parameters.add(new Object[]{void.class, -1, true}); |
|
59 parameters.add(new Object[]{void.class, 0, true}); |
|
60 parameters.add(new Object[]{void.class, 42, true}); |
|
61 return parameters; |
|
62 } |
|
63 |
|
64 private final Class<?> type; |
|
65 private final int length; |
|
66 private final boolean shouldDeopt; |
|
67 private final DeoptimizationBox box = new DeoptimizationBox(); |
|
68 |
|
69 public ArrayNewInstanceTest(Class<?> type, int length, boolean shouldDeopt) { |
|
70 super(); |
|
71 this.type = type; |
|
72 this.length = length; |
|
73 this.shouldDeopt = shouldDeopt; |
|
74 } |
|
75 |
|
76 public static Object newArray(Class<?> klass, int length, DeoptimizationBox box) { |
|
77 Object result = Array.newInstance(klass, length); |
|
78 box.inCompiledCode = GraalDirectives.inCompiledCode(); |
|
79 return result; |
|
80 } |
|
81 |
|
82 @Test |
|
83 public void testNewArray() { |
|
84 test("newArray", type, length, box); |
|
85 assertTrue(box.inCompiledCode != shouldDeopt); |
|
86 } |
|
87 |
|
88 public static Object newArrayInLoop(Class<?> klass, int length, int iterations, DeoptimizationBox box) { |
|
89 Object o = null; |
|
90 for (int i = 0; i < iterations; i++) { |
|
91 o = Array.newInstance(klass, length); |
|
92 } |
|
93 box.inCompiledCode = GraalDirectives.inCompiledCode(); |
|
94 return o; |
|
95 } |
|
96 |
|
97 @Test |
|
98 public void testNewArrayInLoop() { |
|
99 test("newArrayInLoop", type, length, 2, box); |
|
100 assertTrue(box.inCompiledCode != shouldDeopt); |
|
101 } |
|
102 |
|
103 private static class DeoptimizationBox { |
|
104 volatile boolean inCompiledCode = false; |
|
105 } |
|
106 |
|
107 } |