File: TestMoveStoresOutOfLoops.java

package info (click to toggle)
openjdk-11 11.0.24%2B8-2~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 780,008 kB
  • sloc: java: 5,197,087; xml: 1,192,267; cpp: 1,138,204; ansic: 462,088; javascript: 162,551; sh: 16,713; objc: 13,719; python: 4,757; asm: 3,570; makefile: 2,943; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (336 lines) | stat: -rw-r--r-- 11,128 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 8080289 8189067
 * @summary Move stores out of loops if possible
 *
 * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation
 *      -XX:CompileCommand=dontinline,compiler.loopopts.TestMoveStoresOutOfLoops::test*
 *      compiler.loopopts.TestMoveStoresOutOfLoops
 */

package compiler.loopopts;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.function.Function;

public class TestMoveStoresOutOfLoops {

    private static long[] array = new long[10];
    private static long[] array2 = new long[10];
    private static boolean[] array3 = new boolean[1000];
    private static int[] array4 = new int[1000];
    private static byte[] byte_array = new byte[10];

    // Array store should be moved out of the loop, value stored
    // should be 999, the loop should be eliminated
    static void test_after_1(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = i;
        }
    }

    // Array store can't be moved out of loop because of following
    // non loop invariant array access
    static void test_after_2(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = i;
            array2[i%10] = i;
        }
    }

    // Array store can't be moved out of loop because of following
    // use
    static void test_after_3(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = i;
            if (array[0] == -1) {
                break;
            }
        }
    }

    // Array store can't be moved out of loop because of preceding
    // use
    static void test_after_4(int idx) {
        for (int i = 0; i < 1000; i++) {
            if (array[0] == -2) {
                break;
            }
            array[idx] = i;
        }
    }

    // All array stores should be moved out of the loop, one after
    // the other
    static void test_after_5(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = i;
            array[idx+1] = i;
            array[idx+2] = i;
            array[idx+3] = i;
            array[idx+4] = i;
            array[idx+5] = i;
        }
    }

    // Array store can be moved after the loop but needs to be
    // cloned on both exit paths
    static void test_after_6(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = i;
            if (array3[i]) {
                return;
            }
        }
    }

    // Array store can be moved out of the inner loop
    static void test_after_7(int idx) {
        for (int i = 0; i < 1000; i++) {
            for (int j = 0; j <= 42; j++) {
                array4[i] = j;
            }
        }
    }

    // Optimize out redundant stores
    static void test_stores_1(int ignored) {
        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
    }

    static void test_stores_2(int idx) {
        array[idx+0] = 0;
        array[idx+1] = 1;
        array[idx+2] = 2;
        array[idx+0] = 0;
        array[idx+1] = 1;
        array[idx+2] = 2;
    }

    static void test_stores_3(int idx) {
        byte_array[idx+0] = 0;
        byte_array[idx+1] = 1;
        byte_array[idx+2] = 2;
        byte_array[idx+0] = 0;
        byte_array[idx+1] = 1;
        byte_array[idx+2] = 2;
    }

    // Array store can be moved out of the loop before the loop header
    static void test_before_1(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = 999;
        }
    }

    // Array store can't be moved out of the loop before the loop
    // header because there's more than one store on this slice
    static void test_before_2(int idx) {
        for (int i = 0; i < 1000; i++) {
            array[idx] = 999;
            array[i%2] = 0;
        }
    }

    // Array store can't be moved out of the loop before the loop
    // header because of use before store
    static int test_before_3(int idx) {
        int res = 0;
        for (int i = 0; i < 1000; i++) {
            res += array[i%10];
            array[idx] = 999;
        }
        return res;
    }

    // Array store can't be moved out of the loop before the loop
    // header because of possible early exit
    static void test_before_4(int idx) {
        for (int i = 0; i < 1000; i++) {
            if (idx / (i+1) > 0) {
                return;
            }
            array[idx] = 999;
        }
    }

    // Array store can't be moved out of the loop before the loop
    // header because it doesn't postdominate the loop head
    static void test_before_5(int idx) {
        for (int i = 0; i < 1000; i++) {
            if (i % 2 == 0) {
                array[idx] = 999;
            }
        }
    }

    // Array store can be moved out of the loop before the loop header
    static int test_before_6(int idx) {
        int res = 0;
        for (int i = 0; i < 1000; i++) {
            if (i%2 == 1) {
                res *= 2;
            } else {
                res++;
            }
            array[idx] = 999;
        }
        return res;
    }

    final HashMap<String,Method> tests = new HashMap<>();
    {
        for (Method m : this.getClass().getDeclaredMethods()) {
            if (m.getName().matches("test_(before|after|stores)_[0-9]+")) {
                assert(Modifier.isStatic(m.getModifiers())) : m;
                tests.put(m.getName(), m);
            }
        }
    }

    boolean success = true;
    void doTest(String name, Runnable init, Function<String, Boolean> check) throws Exception {
        Method m = tests.get(name);
        for (int i = 0; i < 20000; i++) {
            init.run();
            m.invoke(null, 0);
            success = success && check.apply(name);
            if (!success) {
                break;
            }
        }
    }

    static void array_init() {
        array[0] = -1;
    }

    static boolean array_check(String name) {
        boolean success = true;
        if (array[0] != 999) {
            success = false;
            System.out.println(name + " failed: array[0] = " + array[0]);
        }
        return success;
    }

    static void array_init2() {
        for (int i = 0; i < 6; i++) {
            array[i] = -1;
        }
    }

    static boolean array_check2(String name) {
        boolean success = true;
        for (int i = 0; i < 6; i++) {
            if (array[i] != 999) {
                success = false;
                System.out.println(name + " failed: array[" + i + "] = " + array[i]);
            }
        }
        return success;
    }

    static void array_init3() {
        for (int i = 0; i < 3; i++) {
            array[i] = -1;
        }
    }

    static boolean array_check3(String name) {
        boolean success = true;
        for (int i = 0; i < 3; i++) {
            if (array[i] != i) {
                success = false;
                System.out.println(name + " failed: array[" + i + "] = " + array[i]);
            }
        }
        return success;
    }

    static void array_init4() {
        for (int i = 0; i < 3; i++) {
            byte_array[i] = -1;
        }
    }

    static boolean array_check4(String name) {
        boolean success = true;
        for (int i = 0; i < 3; i++) {
            if (byte_array[i] != i) {
                success = false;
                System.out.println(name + " failed: byte_array[" + i + "] = " + byte_array[i]);
            }
        }
        return success;
    }

    static boolean array_check5(String name) {
        boolean success = true;
        for (int i = 0; i < 1000; i++) {
            if (array4[i] != 42) {
                success = false;
                System.out.println(name + " failed: array[" + i + "] = " + array4[i]);
            }
        }
        return success;
    }

    static public void main(String[] args) throws Exception {
        TestMoveStoresOutOfLoops test = new TestMoveStoresOutOfLoops();
        test.doTest("test_after_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_after_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_after_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_after_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_after_5", TestMoveStoresOutOfLoops::array_init2, TestMoveStoresOutOfLoops::array_check2);
        test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        array3[999] = true;
        test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_after_7", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check5);

        test.doTest("test_stores_1", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
        test.doTest("test_stores_2", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
        test.doTest("test_stores_3", TestMoveStoresOutOfLoops::array_init4, TestMoveStoresOutOfLoops::array_check4);

        test.doTest("test_before_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_before_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_before_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_before_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_before_5", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
        test.doTest("test_before_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);

        if (!test.success) {
            throw new RuntimeException("Some tests failed");
        }
    }
}