1 /*
2  * Copyright (c) 2015, 2020, 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 8076188 8246153 8248226
27  * @summary arraycopy to non escaping destination may be eliminated
28  * @library /
29  *
30  * @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
31  *                   -XX:CompileCommand=dontinline,compiler.arraycopy.TestEliminateArrayCopy*::m*
32  *                   compiler.arraycopy.TestEliminateArrayCopy
33  * @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
34  *                   -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode
35  *                   -XX:CompileCommand=dontinline,compiler.arraycopy.TestEliminateArrayCopy*::m*
36  *                   compiler.arraycopy.TestEliminateArrayCopy
37  * @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
38  *                   -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode
39  *                   -XX:-ReduceInitialCardMarks -XX:-ReduceBulkZeroing
40  *                   -XX:CompileCommand=dontinline,compiler.arraycopy.TestEliminateArrayCopy*::m*
41  *                   compiler.arraycopy.TestEliminateArrayCopy
42  */
43 
44 package compiler.arraycopy;
45 
46 public class TestEliminateArrayCopy {
47 
48     static class CloneTests extends TestInstanceCloneUtils {
49         // object allocation and ArrayCopyNode should be eliminated
m1(E src)50         static void m1(E src) throws CloneNotSupportedException {
51             src.clone();
52         }
53 
54         // both object allocations and ArrayCopyNode should be eliminated
m2(Object dummy)55         static void m2(Object dummy) throws CloneNotSupportedException {
56             E src = new E(false);
57             src.clone();
58         }
59 
60         // object allocation and ArrayCopyNode should be eliminated. Fields should be loaded from src.
m3(E src)61         static int m3(E src) throws CloneNotSupportedException {
62             E dest = (E)src.clone();
63             return dest.i1 + dest.i2 + dest.i3 + dest.i4 + dest.i5 +
64                 dest.i6 + dest.i7 + dest.i8 + dest.i9;
65         }
66     }
67 
68     static class ArrayCopyTests extends TestArrayCopyUtils {
69 
70         // object allocation and ArrayCopyNode should be eliminated.
71         @Args(src=ArraySrc.LARGE)
m1()72         static int m1() throws CloneNotSupportedException {
73             int[] array_clone = (int[])large_int_src.clone();
74             return array_clone[0] + array_clone[1] + array_clone[2] +
75                 array_clone[3] + array_clone[4] + array_clone[5] +
76                 array_clone[6] + array_clone[7] + array_clone[8] +
77                 array_clone[9];
78         }
79 
80         // object allocation and ArrayCopyNode should be eliminated.
81         @Args(src=ArraySrc.LARGE)
m2()82         static int m2() {
83             int[] dest = new int[10];
84             System.arraycopy(large_int_src, 0, dest, 0, 10);
85             return dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +
86                 dest[5] + dest[6] + dest[7] + dest[8] + dest[9];
87         }
88 
89         // object allocations and ArrayCopyNodes should be eliminated.
90         @Args(src=ArraySrc.LARGE)
m3()91         static int m3() {
92             int[] dest1 = new int[10];
93             System.arraycopy(large_int_src, 0, dest1, 0, 10);
94 
95             int[] dest2 = new int[10];
96             System.arraycopy(dest1, 0, dest2, 0, 10);
97 
98             return dest2[0] + dest2[1] + dest2[2] + dest2[3] + dest2[4] +
99                 dest2[5] + dest2[6] + dest2[7] + dest2[8] + dest2[9];
100         }
101 
102         static class m4_class {
103             Object f;
104         }
105 
m4_helper()106         static void m4_helper() {}
107 
108         // allocations eliminated and arraycopy optimized out
109         @Args(src=ArraySrc.LARGE)
m4()110         static int m4() {
111             int[] dest = new int[10];
112             m4_class o = new m4_class();
113             o.f = dest;
114             m4_helper();
115             System.arraycopy(large_int_src, 0, o.f, 0, 10);
116             return dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +
117                 dest[5] + dest[6] + dest[7] + dest[8] + dest[9];
118         }
119 
m5_helper()120         static void m5_helper() {}
121 
122         // Small copy cannot be converted to loads/stores because
123         // allocation is not close enough to arraycopy but arraycopy
124         // itself can be eliminated
125         @Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW)
m5(A[] src, A[] dest)126         static void m5(A[] src, A[] dest) {
127             A[] temp = new A[5];
128             m5_helper();
129             System.arraycopy(src, 0, temp, 0, 5);
130             dest[0] = temp[0];
131             dest[1] = temp[1];
132             dest[2] = temp[2];
133             dest[3] = temp[3];
134             dest[4] = temp[4];
135         }
136 
137         // object allocation and ArrayCopyNode should be eliminated.
138         @Args(src=ArraySrc.LARGE)
m6(int [] src)139         static int m6(int [] src) {
140             int res = src[0] + src[1] + src[2] + src[3] + src[4] +
141                 src[5] + src[6] + src[7] + src[8] + src[9];
142 
143             int[] dest = new int[10];
144 
145             System.arraycopy(src, 0, dest, 0, 10);
146 
147             res += dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +
148                 dest[5] + dest[6] + dest[7] + dest[8] + dest[9];
149             return res/2;
150         }
151 
152         @Args(src=ArraySrc.LARGE)
m7()153         static int m7() {
154             int[] dest = new int[10];
155             dest[0] = large_int_src[8];
156             dest[1] = large_int_src[9];
157             System.arraycopy(large_int_src, 0, dest, 2, 8);
158             return dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +
159                 dest[5] + dest[6] + dest[7] + dest[8] + dest[9];
160         }
161     }
162 
163     // test that OptimizePtrCompare still works
164     static final Object[] m1_array = new Object[10];
165     static boolean m1_array_null_element = false;
m1(int i)166     static void m1(int i) {
167         Object[] array_clone = (Object[])m1_array.clone();
168         if (array_clone[i] == null) {
169             m1_array_null_element = true;
170         }
171     }
172 
main(String[] args)173     static public void main(String[] args) throws Exception {
174         CloneTests clone_tests = new CloneTests();
175 
176         clone_tests.doTest(clone_tests.e, "m1");
177         clone_tests.doTest(null, "m2");
178         clone_tests.doTest(clone_tests.e, "m3");
179 
180         ArrayCopyTests ac_tests = new ArrayCopyTests();
181 
182         ac_tests.doTest("m1");
183         ac_tests.doTest("m2");
184         ac_tests.doTest("m3");
185         ac_tests.doTest("m4");
186         ac_tests.doTest("m5");
187         ac_tests.doTest("m6");
188         ac_tests.doTest("m7");
189 
190         if (!clone_tests.success || !ac_tests.success) {
191             throw new RuntimeException("some tests failed");
192         }
193 
194         // Make sure both branches of the if in m1() appear taken
195         for (int i = 0; i < 7000; i++) {
196             m1(0);
197         }
198         m1_array[0] = new Object();
199         for (int i = 0; i < 20000; i++) {
200             m1(0);
201         }
202         m1_array_null_element = false;
203         m1(0);
204         if (m1_array_null_element) {
205             throw new RuntimeException("OptimizePtrCompare test failed");
206         }
207     }
208 }
209