Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestArrayCopyAsLoadsStores.java
41152 views
/*1* Copyright (c) 2015, 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 691252126* @summary small array copy as loads/stores27* @library /28*29* @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement30* -XX:CompileCommand=dontinline,compiler.arraycopy.TestArrayCopyAsLoadsStores::m*31* -XX:TypeProfileLevel=20032* compiler.arraycopy.TestArrayCopyAsLoadsStores33* @run main/othervm -ea -XX:-BackgroundCompilation -XX:-UseOnStackReplacement34* -XX:CompileCommand=dontinline,compiler.arraycopy.TestArrayCopyAsLoadsStores::m*35* -XX:TypeProfileLevel=20036* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressArrayCopyMacroNode37* compiler.arraycopy.TestArrayCopyAsLoadsStores38*/3940package compiler.arraycopy;4142import java.util.Arrays;4344public class TestArrayCopyAsLoadsStores extends TestArrayCopyUtils {4546// array clone should be compiled as loads/stores47@Args(src=ArraySrc.SMALL)48static A[] m1() throws CloneNotSupportedException {49return (A[])small_a_src.clone();50}5152@Args(src=ArraySrc.SMALL)53static int[] m2() throws CloneNotSupportedException {54return (int[])small_int_src.clone();55}5657// new array allocation should be optimized out58@Args(src=ArraySrc.SMALL)59static int m3() throws CloneNotSupportedException {60int[] array_clone = (int[])small_int_src.clone();61return array_clone[0] + array_clone[1] + array_clone[2] +62array_clone[3] + array_clone[4];63}6465// should not be compiled as loads/stores66@Args(src=ArraySrc.LARGE)67static int[] m4() throws CloneNotSupportedException {68return (int[])large_int_src.clone();69}7071// check that array of length 0 is handled correctly72@Args(src=ArraySrc.ZERO)73static int[] m5() throws CloneNotSupportedException {74return (int[])zero_int_src.clone();75}7677// array copy should be compiled as loads/stores78@Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW)79static void m6(int[] src, int[] dest) {80System.arraycopy(src, 0, dest, 0, 5);81}8283// array copy should not be compiled as loads/stores84@Args(src=ArraySrc.LARGE, dst=ArrayDst.NEW)85static void m7(int[] src, int[] dest) {86System.arraycopy(src, 0, dest, 0, 10);87}8889// array copy should be compiled as loads/stores90@Args(src=ArraySrc.SMALL)91static A[] m8(A[] src) {92src[0] = src[0]; // force null check93A[] dest = new A[5];94System.arraycopy(src, 0, dest, 0, 5);95return dest;96}9798// array copy should not be compiled as loads/stores: we would99// need to emit GC barriers100@Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW)101static void m9(A[] src, A[] dest) {102System.arraycopy(src, 0, dest, 0, 5);103}104105// overlapping array regions: copy backward106@Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC)107static void m10(int[] src, int[] dest) {108System.arraycopy(src, 0, dest, 1, 4);109}110111static boolean m10_check(int[] src, int[] dest) {112boolean failure = false;113for (int i = 0; i < 5; i++) {114int j = Math.max(i - 1, 0);115if (dest[i] != src[j]) {116System.out.println("Test m10 failed for " + i + " src[" + j +"]=" + src[j] + ", dest[" + i + "]=" + dest[i]);117failure = true;118}119}120return failure;121}122123// overlapping array regions: copy forward124@Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC)125static void m11(int[] src, int[] dest) {126System.arraycopy(src, 1, dest, 0, 4);127}128129static boolean m11_check(int[] src, int[] dest) {130boolean failure = false;131for (int i = 0; i < 5; i++) {132int j = Math.min(i + 1, 4);133if (dest[i] != src[j]) {134System.out.println("Test m11 failed for " + i + " src[" + j +"]=" + src[j] + ", dest[" + i + "]=" + dest[i]);135failure = true;136}137}138return failure;139}140141// overlapping array region with unknown src/dest offsets: compiled code must include both forward and backward copies142@Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC, extra_args={0,1})143static void m12(int[] src, int[] dest, int srcPos, int destPos) {144System.arraycopy(src, srcPos, dest, destPos, 4);145}146147static boolean m12_check(int[] src, int[] dest) {148boolean failure = false;149for (int i = 0; i < 5; i++) {150int j = Math.max(i - 1, 0);151if (dest[i] != src[j]) {152System.out.println("Test m10 failed for " + i + " src[" + j +"]=" + src[j] + ", dest[" + i + "]=" + dest[i]);153failure = true;154}155}156return failure;157}158159// Array allocation and copy should optimize out160@Args(src=ArraySrc.SMALL)161static int m13(int[] src) {162int[] dest = new int[5];163System.arraycopy(src, 0, dest, 0, 5);164return dest[0] + dest[1] + dest[2] + dest[3] + dest[4];165}166167// Check that copy of length 0 is handled correctly168@Args(src=ArraySrc.ZERO, dst=ArrayDst.NEW)169static void m14(int[] src, int[] dest) {170System.arraycopy(src, 0, dest, 0, 0);171}172173// copyOf should compile to loads/stores174@Args(src=ArraySrc.SMALL)175static A[] m15() {176return Arrays.copyOf(small_a_src, 5, A[].class);177}178179static Object[] helper16(int i) {180Object[] arr = null;181if ((i%2) == 0) {182arr = small_a_src;183} else {184arr = small_object_src;185}186return arr;187}188189// CopyOf may need subtype check190@Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0})191static A[] m16(A[] unused_src, int i) {192Object[] arr = helper16(i);193return Arrays.copyOf(arr, 5, A[].class);194}195196static Object[] helper17_1(int i) {197Object[] arr = null;198if ((i%2) == 0) {199arr = small_a_src;200} else {201arr = small_object_src;202}203return arr;204}205206static A[] helper17_2(Object[] arr) {207return Arrays.copyOf(arr, 5, A[].class);208}209210// CopyOf may leverage type speculation211@Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0})212static A[] m17(A[] unused_src, int i) {213Object[] arr = helper17_1(i);214return helper17_2(arr);215}216217static Object[] helper18_1(int i) {218Object[] arr = null;219if ((i%2) == 0) {220arr = small_a_src;221} else {222arr = small_object_src;223}224return arr;225}226227static Object[] helper18_2(Object[] arr) {228return Arrays.copyOf(arr, 5, Object[].class);229}230231// CopyOf should not attempt to use type speculation if it's not needed232@Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0})233static Object[] m18(A[] unused_src, int i) {234Object[] arr = helper18_1(i);235return helper18_2(arr);236}237238static Object[] helper19(int i) {239Object[] arr = null;240if ((i%2) == 0) {241arr = small_a_src;242} else {243arr = small_object_src;244}245return arr;246}247248// CopyOf may need subtype check. Test is run to make type check249// fail and cause deoptimization. Next compilation should not250// compile as loads/stores because the first compilation251// deoptimized.252@Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0})253static A[] m19(A[] unused_src, int i) {254Object[] arr = helper19(i);255return Arrays.copyOf(arr, 5, A[].class);256}257258// copyOf for large array should not compile to loads/stores259@Args(src=ArraySrc.LARGE)260static A[] m20() {261return Arrays.copyOf(large_a_src, 10, A[].class);262}263264// check zero length copyOf is handled correctly265@Args(src=ArraySrc.ZERO)266static A[] m21() {267return Arrays.copyOf(zero_a_src, 0, A[].class);268}269270// Run with srcPos=0 for a 1st compile, then with incorrect value271// of srcPos to cause deoptimization, then with srcPos=0 for a 2nd272// compile. The 2nd compile shouldn't turn arraycopy into273// loads/stores because input arguments are no longer known to be274// valid.275@Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW, extra_args={0})276static void m22(int[] src, int[] dest, int srcPos) {277System.arraycopy(src, srcPos, dest, 0, 5);278}279280// copyOfRange should compile to loads/stores281@Args(src=ArraySrc.SMALL)282static A[] m23() {283return Arrays.copyOfRange(small_a_src, 1, 4, A[].class);284}285286static boolean m23_check(A[] src, A[] dest) {287boolean failure = false;288for (int i = 0; i < 3; i++) {289if (src[i+1] != dest[i]) {290System.out.println("Test m23 failed for " + i + " src[" + (i+1) +"]=" + dest[i] + ", dest[" + i + "]=" + dest[i]);291failure = true;292}293}294return failure;295}296297// array copy should be compiled as loads/stores. Invoke then with298// incompatible array type to verify we don't allow a forbidden299// arraycopy to happen.300@Args(src=ArraySrc.SMALL)301static A[] m24(Object[] src) {302src[0] = src[0]; // force null check303A[] dest = new A[5];304System.arraycopy(src, 0, dest, 0, 5);305return dest;306}307308// overlapping array region with unknown src/dest offsets but309// length 1: compiled code doesn't need both forward and backward310// copies311@Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC, extra_args={0,1})312static void m25(int[] src, int[] dest, int srcPos, int destPos) {313System.arraycopy(src, srcPos, dest, destPos, 1);314}315316static boolean m25_check(int[] src, int[] dest) {317boolean failure = false;318if (dest[1] != src[0]) {319System.out.println("Test m10 failed for src[0]=" + src[0] + ", dest[1]=" + dest[1]);320return true;321}322return false;323}324325public static void main(String[] args) throws Exception {326TestArrayCopyAsLoadsStores test = new TestArrayCopyAsLoadsStores();327328test.doTest("m1");329test.doTest("m2");330test.doTest("m3");331test.doTest("m4");332test.doTest("m5");333test.doTest("m6");334test.doTest("m7");335test.doTest("m8");336test.doTest("m9");337test.doTest("m10");338test.doTest("m11");339test.doTest("m12");340test.doTest("m13");341test.doTest("m14");342test.doTest("m15");343344// make both branches of the If appear taken345for (int i = 0; i < 20000; i++) {346helper16(i);347}348349test.doTest("m16");350351// load class B so type check in m17 would not be simple comparison352B b = new B();353// make both branches of the If appear taken354for (int i = 0; i < 20000; i++) {355helper17_1(i);356}357358test.doTest("m17");359360// make both branches of the If appear taken361for (int i = 0; i < 20000; i++) {362helper18_1(i);363}364test.doTest("m18");365366// make both branches of the If appear taken367for (int i = 0; i < 20000; i++) {368helper19(i);369}370371// Compile372for (int i = 0; i < 20000; i++) {373m19(null, 0);374}375376// force deopt377boolean m19_exception = false;378for (int i = 0; i < 10; i++) {379try {380m19(null, 1);381} catch(ArrayStoreException ase) {382m19_exception = true;383}384}385386if (!m19_exception) {387System.out.println("Test m19: exception wasn't thrown");388test.success = false;389}390391test.doTest("m19");392393test.doTest("m20");394test.doTest("m21");395396// Compile397int[] dst = new int[small_int_src.length];398for (int i = 0; i < 20000; i++) {399m22(small_int_src, dst, 0);400}401402// force deopt403for (int i = 0; i < 10; i++) {404try {405m22(small_int_src, dst, 5);406} catch(ArrayIndexOutOfBoundsException aioobe) {}407}408409test.doTest("m22");410test.doTest("m23");411412test.doTest("m24");413boolean m24_exception = false;414try {415m24(small_object_src);416} catch(ArrayStoreException ase) {417m24_exception = true;418}419420if (!m24_exception) {421System.out.println("Test m24: exception wasn't thrown");422test.success = false;423}424425test.doTest("m25");426427if (!test.success) {428throw new RuntimeException("some tests failed");429}430}431}432433434