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