Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestEliminateArrayCopy.java
41152 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8076188 8246153 824822626* @summary arraycopy to non escaping destination may be eliminated27* @library /28*29* @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement30* -XX:CompileCommand=dontinline,compiler.arraycopy.TestEliminateArrayCopy*::m*31* compiler.arraycopy.TestEliminateArrayCopy32* @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement33* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode34* -XX:CompileCommand=dontinline,compiler.arraycopy.TestEliminateArrayCopy*::m*35* compiler.arraycopy.TestEliminateArrayCopy36* @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement37* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode38* -XX:-ReduceInitialCardMarks -XX:-ReduceBulkZeroing39* -XX:CompileCommand=dontinline,compiler.arraycopy.TestEliminateArrayCopy*::m*40* compiler.arraycopy.TestEliminateArrayCopy41*/4243package compiler.arraycopy;4445public class TestEliminateArrayCopy {4647static class CloneTests extends TestInstanceCloneUtils {48// object allocation and ArrayCopyNode should be eliminated49static void m1(E src) throws CloneNotSupportedException {50src.clone();51}5253// both object allocations and ArrayCopyNode should be eliminated54static void m2(Object dummy) throws CloneNotSupportedException {55E src = new E(false);56src.clone();57}5859// object allocation and ArrayCopyNode should be eliminated. Fields should be loaded from src.60static int m3(E src) throws CloneNotSupportedException {61E dest = (E)src.clone();62return dest.i1 + dest.i2 + dest.i3 + dest.i4 + dest.i5 +63dest.i6 + dest.i7 + dest.i8 + dest.i9;64}65}6667static class ArrayCopyTests extends TestArrayCopyUtils {6869// object allocation and ArrayCopyNode should be eliminated.70@Args(src=ArraySrc.LARGE)71static int m1() throws CloneNotSupportedException {72int[] array_clone = (int[])large_int_src.clone();73return array_clone[0] + array_clone[1] + array_clone[2] +74array_clone[3] + array_clone[4] + array_clone[5] +75array_clone[6] + array_clone[7] + array_clone[8] +76array_clone[9];77}7879// object allocation and ArrayCopyNode should be eliminated.80@Args(src=ArraySrc.LARGE)81static int m2() {82int[] dest = new int[10];83System.arraycopy(large_int_src, 0, dest, 0, 10);84return dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +85dest[5] + dest[6] + dest[7] + dest[8] + dest[9];86}8788// object allocations and ArrayCopyNodes should be eliminated.89@Args(src=ArraySrc.LARGE)90static int m3() {91int[] dest1 = new int[10];92System.arraycopy(large_int_src, 0, dest1, 0, 10);9394int[] dest2 = new int[10];95System.arraycopy(dest1, 0, dest2, 0, 10);9697return dest2[0] + dest2[1] + dest2[2] + dest2[3] + dest2[4] +98dest2[5] + dest2[6] + dest2[7] + dest2[8] + dest2[9];99}100101static class m4_class {102Object f;103}104105static void m4_helper() {}106107// allocations eliminated and arraycopy optimized out108@Args(src=ArraySrc.LARGE)109static int m4() {110int[] dest = new int[10];111m4_class o = new m4_class();112o.f = dest;113m4_helper();114System.arraycopy(large_int_src, 0, o.f, 0, 10);115return dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +116dest[5] + dest[6] + dest[7] + dest[8] + dest[9];117}118119static void m5_helper() {}120121// Small copy cannot be converted to loads/stores because122// allocation is not close enough to arraycopy but arraycopy123// itself can be eliminated124@Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW)125static void m5(A[] src, A[] dest) {126A[] temp = new A[5];127m5_helper();128System.arraycopy(src, 0, temp, 0, 5);129dest[0] = temp[0];130dest[1] = temp[1];131dest[2] = temp[2];132dest[3] = temp[3];133dest[4] = temp[4];134}135136// object allocation and ArrayCopyNode should be eliminated.137@Args(src=ArraySrc.LARGE)138static int m6(int [] src) {139int res = src[0] + src[1] + src[2] + src[3] + src[4] +140src[5] + src[6] + src[7] + src[8] + src[9];141142int[] dest = new int[10];143144System.arraycopy(src, 0, dest, 0, 10);145146res += dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +147dest[5] + dest[6] + dest[7] + dest[8] + dest[9];148return res/2;149}150151@Args(src=ArraySrc.LARGE)152static int m7() {153int[] dest = new int[10];154dest[0] = large_int_src[8];155dest[1] = large_int_src[9];156System.arraycopy(large_int_src, 0, dest, 2, 8);157return dest[0] + dest[1] + dest[2] + dest[3] + dest[4] +158dest[5] + dest[6] + dest[7] + dest[8] + dest[9];159}160}161162// test that OptimizePtrCompare still works163static final Object[] m1_array = new Object[10];164static boolean m1_array_null_element = false;165static void m1(int i) {166Object[] array_clone = (Object[])m1_array.clone();167if (array_clone[i] == null) {168m1_array_null_element = true;169}170}171172static public void main(String[] args) throws Exception {173CloneTests clone_tests = new CloneTests();174175clone_tests.doTest(clone_tests.e, "m1");176clone_tests.doTest(null, "m2");177clone_tests.doTest(clone_tests.e, "m3");178179ArrayCopyTests ac_tests = new ArrayCopyTests();180181ac_tests.doTest("m1");182ac_tests.doTest("m2");183ac_tests.doTest("m3");184ac_tests.doTest("m4");185ac_tests.doTest("m5");186ac_tests.doTest("m6");187ac_tests.doTest("m7");188189if (!clone_tests.success || !ac_tests.success) {190throw new RuntimeException("some tests failed");191}192193// Make sure both branches of the if in m1() appear taken194for (int i = 0; i < 7000; i++) {195m1(0);196}197m1_array[0] = new Object();198for (int i = 0; i < 20000; i++) {199m1(0);200}201m1_array_null_element = false;202m1(0);203if (m1_array_null_element) {204throw new RuntimeException("OptimizePtrCompare test failed");205}206}207}208209210