Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestEliminatedArrayCopyDeopt.java
41152 views
/*1* Copyright (c) 2016, 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 8130847 815676026* @summary Eliminated instance/array written to by an array copy variant must be correctly initialized when reallocated at a deopt27* @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement28* compiler.arraycopy.TestEliminatedArrayCopyDeopt29* @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement30* -XX:+IgnoreUnrecognizedVMOptions -XX:-ReduceInitialCardMarks31* compiler.arraycopy.TestEliminatedArrayCopyDeopt32*/3334// Test that if an ArrayCopy node is eliminated because it doesn't35// escape, then the correct field/array element values are captured so36// on a deoptimization, when the object/array is reallocated, it is37// correctly initialized3839package compiler.arraycopy;4041public class TestEliminatedArrayCopyDeopt {4243static class A implements Cloneable {44int f0;45int f1;46int f2;47int f3;48int f4;49int f5;50int f6;51int f7;52int f8;53int f9;54int f10;55int f11;56int f12;57int f13;58int f14;59int f15;6061public Object clone() throws CloneNotSupportedException {62return super.clone();63}64}6566// Clone67static boolean m1(A a, boolean flag) throws CloneNotSupportedException {68A c = (A)a.clone();69if (flag) {70// never taken branch that causes the deoptimization71if (c.f0 != 0x42) {72return false;73}74}75return true;76}7778// Array clone79static int[] m2_src = null;80static boolean m2(boolean flag) throws CloneNotSupportedException {81int[] src = new int[10];82m2_src = src;83for (int i = 0; i < src.length; i++) {84src[i] = 0x42+i;85}86int[] c = (int[])src.clone();87if (flag) {88for (int i = 0; i < c.length; i++) {89if (c[i] != src[i]) {90return false;91}92}93}94return true;95}9697// Array copy98static boolean m3(int[] src, boolean flag) {99int[] dst = new int[10];100System.arraycopy(src, 0, dst, 0, 10);101if (flag) {102for (int i = 0; i < dst.length; i++) {103if (dst[i] != src[i]) {104return false;105}106}107}108return true;109}110111// Array copy of subrange112static boolean m4(int[] src, boolean flag) {113int[] dst = new int[10];114dst[0] = 0x42;115dst[1] = 0x42 - 1;116dst[2] = 0x42 - 2;117dst[8] = 0x42 - 8;118dst[9] = 0x42 - 9;119int src_off = 2;120int dst_off = 3;121int len = 5;122System.arraycopy(src, src_off, dst, dst_off, len);123if (flag) {124for (int i = 0; i < dst.length; i++) {125if (i >= dst_off && i < dst_off + len) {126if (dst[i] != src[i - dst_off + src_off]) {127return false;128}129} else {130if (dst[i] != 0x42-i) {131return false;132}133}134}135}136return true;137}138139// Array copy with Phi140static boolean m5(int[] src, boolean flag1, boolean flag2) {141int[] dst = new int[10];142if (flag1) {143System.arraycopy(src, 0, dst, 0, 10);144}145if (flag2) {146for (int i = 0; i < dst.length; i++) {147if (dst[i] != src[i]) {148return false;149}150}151}152return true;153}154155static public void main(String[] args) throws Exception {156boolean success = true;157A a = new A();158a.f0 = 0x42;159for (int i = 0; i < 20000; i++) {160m1(a, false);161}162if (!m1(a, true)) {163System.out.println("m1 failed");164success = false;165}166167for (int i = 0; i < 20000; i++) {168m2(false);169}170if (!m2(true)) {171System.out.println("m2 failed");172success = false;173}174175int[] src = new int[10];176for (int i = 0; i < src.length; i++) {177src[i] = 0x42+i;178}179180for (int i = 0; i < 20000; i++) {181m3(src, false);182}183if (!m3(src, true)) {184System.out.println("m3 failed");185success = false;186}187188for (int i = 0; i < 20000; i++) {189m4(src, false);190}191if (!m4(src, true)) {192System.out.println("m4 failed");193success = false;194}195196for (int i = 0; i < 20000; i++) {197m5(src, i%2 == 0, false);198}199if (!m5(src, true, true)) {200System.out.println("m4 failed");201success = false;202}203204if (!success) {205throw new RuntimeException("Test failed");206}207}208}209210211