1/*
2 * Copyright (c) 2016, 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 8130847 8156760
27 * @summary Eliminated instance/array written to by an array copy variant must be correctly initialized when reallocated at a deopt
28 * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
29 *                   compiler.arraycopy.TestEliminatedArrayCopyDeopt
30 * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
31 *                   -XX:+IgnoreUnrecognizedVMOptions -XX:-ReduceInitialCardMarks
32 *                   compiler.arraycopy.TestEliminatedArrayCopyDeopt
33 */
34
35// Test that if an ArrayCopy node is eliminated because it doesn't
36// escape, then the correct field/array element values are captured so
37// on a deoptimization, when the object/array is reallocated, it is
38// correctly initialized
39
40package compiler.arraycopy;
41
42public class TestEliminatedArrayCopyDeopt {
43
44    static class A implements Cloneable {
45        int f0;
46        int f1;
47        int f2;
48        int f3;
49        int f4;
50        int f5;
51        int f6;
52        int f7;
53        int f8;
54        int f9;
55        int f10;
56        int f11;
57        int f12;
58        int f13;
59        int f14;
60        int f15;
61
62        public Object clone() throws CloneNotSupportedException {
63            return super.clone();
64        }
65    }
66
67    // Clone
68    static boolean m1(A a, boolean flag) throws CloneNotSupportedException {
69        A c = (A)a.clone();
70        if (flag) {
71            // never taken branch that causes the deoptimization
72            if (c.f0 != 0x42) {
73                return false;
74            }
75        }
76        return true;
77    }
78
79    // Array clone
80    static int[] m2_src = null;
81    static boolean m2(boolean flag) throws CloneNotSupportedException {
82        int[] src  = new int[10];
83        m2_src = src;
84        for (int i = 0; i < src.length; i++) {
85            src[i] = 0x42+i;
86        }
87        int[] c = (int[])src.clone();
88        if (flag) {
89            for (int i = 0; i < c.length; i++) {
90                if (c[i] != src[i]) {
91                    return false;
92                }
93            }
94        }
95        return true;
96    }
97
98    // Array copy
99    static boolean m3(int[] src, boolean flag) {
100        int[] dst = new int[10];
101        System.arraycopy(src, 0, dst, 0, 10);
102        if (flag) {
103            for (int i = 0; i < dst.length; i++) {
104                if (dst[i] != src[i]) {
105                    return false;
106                }
107            }
108        }
109        return true;
110    }
111
112    // Array copy of subrange
113    static boolean m4(int[] src, boolean flag) {
114        int[] dst = new int[10];
115        dst[0] = 0x42;
116        dst[1] = 0x42 - 1;
117        dst[2] = 0x42 - 2;
118        dst[8] = 0x42 - 8;
119        dst[9] = 0x42 - 9;
120        int src_off = 2;
121        int dst_off = 3;
122        int len = 5;
123        System.arraycopy(src, src_off, dst, dst_off, len);
124        if (flag) {
125            for (int i = 0; i < dst.length; i++) {
126                if (i >= dst_off &&  i < dst_off + len) {
127                    if (dst[i] != src[i - dst_off + src_off]) {
128                        return false;
129                    }
130                } else {
131                    if (dst[i] != 0x42-i) {
132                        return false;
133                    }
134                }
135            }
136        }
137        return true;
138    }
139
140    // Array copy with Phi
141    static boolean m5(int[] src, boolean flag1, boolean flag2) {
142        int[] dst = new int[10];
143        if (flag1) {
144            System.arraycopy(src, 0, dst, 0, 10);
145        }
146        if (flag2) {
147            for (int i = 0; i < dst.length; i++) {
148                if (dst[i] != src[i]) {
149                    return false;
150                }
151            }
152        }
153        return true;
154    }
155
156    static public void main(String[] args) throws Exception {
157        boolean success = true;
158        A a = new A();
159        a.f0 = 0x42;
160        for (int i = 0; i < 20000; i++) {
161            m1(a, false);
162        }
163        if (!m1(a, true)) {
164            System.out.println("m1 failed");
165            success = false;
166        }
167
168        for (int i = 0; i < 20000; i++) {
169            m2(false);
170        }
171        if (!m2(true)) {
172            System.out.println("m2 failed");
173            success = false;
174        }
175
176        int[] src = new int[10];
177        for (int i = 0; i < src.length; i++) {
178            src[i] = 0x42+i;
179        }
180
181        for (int i = 0; i < 20000; i++) {
182            m3(src, false);
183        }
184        if (!m3(src, true)) {
185            System.out.println("m3 failed");
186            success = false;
187        }
188
189        for (int i = 0; i < 20000; i++) {
190            m4(src, false);
191        }
192        if (!m4(src, true)) {
193            System.out.println("m4 failed");
194            success = false;
195        }
196
197        for (int i = 0; i < 20000; i++) {
198            m5(src, i%2 == 0, false);
199        }
200        if (!m5(src, true, true)) {
201            System.out.println("m4 failed");
202            success = false;
203        }
204
205        if (!success) {
206            throw new RuntimeException("Test failed");
207        }
208    }
209}
210