1/*
2 * Copyright (c) 2011, 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 7044738
27 * @summary Loop unroll optimization causes incorrect result
28 *
29 * @run main/othervm -Xbatch compiler.loopopts.Test7044738
30 */
31
32package compiler.loopopts;
33
34public class Test7044738 {
35
36    private static final int INITSIZE = 10000;
37    public int d[] = {1, 2, 3, 4};
38    public int i, size;
39
40    private static int iter = 5;
41
42    boolean done() {
43        return (--iter > 0);
44    }
45
46    public static void main(String args[]) {
47        Test7044738 t = new Test7044738();
48        t.test();
49    }
50
51    int test() {
52
53        while (done()) {
54            size = INITSIZE;
55
56            for (i = 0; i < size; i++) {
57                d[0] = d[1]; // 2
58                d[1] = d[2]; // 3
59                d[2] = d[3]; // 4
60                d[3] = d[0]; // 2
61
62                d[0] = d[1]; // 3
63                d[1] = d[2]; // 4
64                d[2] = d[3]; // 2
65                d[3] = d[0]; // 3
66
67                d[0] = d[1]; // 4
68                d[1] = d[2]; // 2
69                d[2] = d[3]; // 3
70                d[3] = d[0]; // 4
71
72                d[0] = d[1]; // 2
73                d[1] = d[2]; // 3
74                d[2] = d[3]; // 4
75                d[3] = d[0]; // 2
76
77                d[0] = d[1]; // 3
78                d[1] = d[2]; // 4
79                d[2] = d[3]; // 2
80                d[3] = d[0]; // 3
81
82                d[0] = d[1]; // 4
83                d[1] = d[2]; // 2
84                d[2] = d[3]; // 3
85                d[3] = d[0]; // 4
86
87                d[0] = d[1]; // 2
88                d[1] = d[2]; // 3
89                d[2] = d[3]; // 4
90                d[3] = d[0]; // 2
91
92                d[0] = d[1]; // 3
93                d[1] = d[2]; // 4
94                d[2] = d[3]; // 2
95                d[3] = d[0]; // 3
96            }
97
98            // try to defeat dead code elimination
99            if (d[0] == d[1]) {
100                System.out.println("test failed: iter=" + iter + "  i=" + i + " d[] = { " + d[0] + ", " + d[1] + ", " + d[2] + ", " + d[3] + " } ");
101                System.exit(97);
102            }
103        }
104        return d[3];
105    }
106}
107