hotspot/test/compiler/loopopts/TestMoveStoresOutOfLoops.java
author roland
Wed, 29 Jul 2015 17:25:04 +0200
changeset 32372 b82e88dcb26c
child 33078 9452eb89c16d
permissions -rw-r--r--
8080289: Intermediate writes in a loop not eliminated by optimizer Summary: Move Stores out of loop (after or before) when possible Reviewed-by: kvn, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32372
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     1
/*
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     4
 *
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     8
 *
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    13
 * accompanied this code).
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    14
 *
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    18
 *
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    21
 * questions.
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    22
 *
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    23
 */
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    24
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    25
/**
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    26
 * @test
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    27
 * @bug 8080289
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    28
 * @summary Sink stores out of loops if possible
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    29
 * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:+PrintCompilation -XX:CompileCommand=dontinline,TestMoveStoresOutOfLoops::test*  TestMoveStoresOutOfLoops
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    30
 *
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    31
 */
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    32
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    33
import java.lang.reflect.*;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    34
import java.util.*;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    35
import java.util.function.*;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    36
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    37
public class TestMoveStoresOutOfLoops {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    38
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    39
    private static long[] array = new long[10];
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    40
    private static long[] array2 = new long[10];
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    41
    private static boolean[] array3 = new boolean[1000];
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    42
    private static byte[] byte_array = new byte[10];
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    43
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    44
    // Array store should be moved out of the loop, value stored
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    45
    // should be 999, the loop should be eliminated
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    46
    static void test_after_1(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    47
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    48
            array[idx] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    49
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    50
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    51
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    52
    // Array store can't be moved out of loop because of following
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    53
    // non loop invariant array access
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    54
    static void test_after_2(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    55
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    56
            array[idx] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    57
            array2[i%10] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    58
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    59
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    60
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    61
    // Array store can't be moved out of loop because of following
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    62
    // use
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    63
    static void test_after_3(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    64
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    65
            array[idx] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    66
            if (array[0] == -1) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    67
                break;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    68
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    69
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    70
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    71
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    72
    // Array store can't be moved out of loop because of preceding
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    73
    // use
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    74
    static void test_after_4(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    75
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    76
            if (array[0] == -2) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    77
                break;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    78
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    79
            array[idx] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    80
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    81
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    82
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    83
    // All array stores should be moved out of the loop, one after
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    84
    // the other
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    85
    static void test_after_5(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    86
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    87
            array[idx] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    88
            array[idx+1] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    89
            array[idx+2] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    90
            array[idx+3] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    91
            array[idx+4] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    92
            array[idx+5] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    93
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    94
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    95
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    96
    // Array store can be moved after the loop but needs to be
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    97
    // cloned on both exit paths
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    98
    static void test_after_6(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
    99
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   100
            array[idx] = i;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   101
            if (array3[i]) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   102
                return;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   103
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   104
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   105
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   106
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   107
    // Optimize out redundant stores
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   108
    static void test_stores_1(int ignored) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   109
        array[0] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   110
        array[1] = 1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   111
        array[2] = 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   112
        array[0] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   113
        array[1] = 1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   114
        array[2] = 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   115
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   116
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   117
    static void test_stores_2(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   118
        array[idx+0] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   119
        array[idx+1] = 1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   120
        array[idx+2] = 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   121
        array[idx+0] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   122
        array[idx+1] = 1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   123
        array[idx+2] = 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   124
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   125
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   126
    static void test_stores_3(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   127
        byte_array[idx+0] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   128
        byte_array[idx+1] = 1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   129
        byte_array[idx+2] = 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   130
        byte_array[idx+0] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   131
        byte_array[idx+1] = 1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   132
        byte_array[idx+2] = 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   133
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   134
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   135
    // Array store can be moved out of the loop before the loop header
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   136
    static void test_before_1(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   137
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   138
            array[idx] = 999;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   139
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   140
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   141
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   142
    // Array store can't be moved out of the loop before the loop
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   143
    // header because there's more than one store on this slice
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   144
    static void test_before_2(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   145
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   146
            array[idx] = 999;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   147
            array[i%2] = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   148
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   149
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   150
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   151
    // Array store can't be moved out of the loop before the loop
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   152
    // header because of use before store
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   153
    static int test_before_3(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   154
        int res = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   155
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   156
            res += array[i%10];
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   157
            array[idx] = 999;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   158
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   159
        return res;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   160
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   161
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   162
    // Array store can't be moved out of the loop before the loop
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   163
    // header because of possible early exit
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   164
    static void test_before_4(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   165
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   166
            if (idx / (i+1) > 0) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   167
                return;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   168
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   169
            array[idx] = 999;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   170
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   171
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   172
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   173
    // Array store can't be moved out of the loop before the loop
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   174
    // header because it doesn't postdominate the loop head
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   175
    static void test_before_5(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   176
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   177
            if (i % 2 == 0) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   178
                array[idx] = 999;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   179
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   180
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   181
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   182
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   183
    // Array store can be moved out of the loop before the loop header
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   184
    static int test_before_6(int idx) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   185
        int res = 0;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   186
        for (int i = 0; i < 1000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   187
            if (i%2 == 1) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   188
                res *= 2;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   189
            } else {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   190
                res++;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   191
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   192
            array[idx] = 999;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   193
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   194
        return res;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   195
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   196
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   197
    final HashMap<String,Method> tests = new HashMap<>();
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   198
    {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   199
        for (Method m : this.getClass().getDeclaredMethods()) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   200
            if (m.getName().matches("test_(before|after|stores)_[0-9]+")) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   201
                assert(Modifier.isStatic(m.getModifiers())) : m;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   202
                tests.put(m.getName(), m);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   203
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   204
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   205
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   206
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   207
    boolean success = true;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   208
    void doTest(String name, Runnable init, Function<String, Boolean> check) throws Exception {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   209
        Method m = tests.get(name);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   210
        for (int i = 0; i < 20000; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   211
            init.run();
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   212
            m.invoke(null, 0);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   213
            success = success && check.apply(name);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   214
            if (!success) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   215
                break;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   216
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   217
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   218
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   219
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   220
    static void array_init() {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   221
        array[0] = -1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   222
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   223
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   224
    static boolean array_check(String name) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   225
        boolean success = true;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   226
        if (array[0] != 999) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   227
            success = false;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   228
            System.out.println(name + " failed: array[0] = " + array[0]);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   229
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   230
        return success;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   231
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   232
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   233
    static void array_init2() {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   234
        for (int i = 0; i < 6; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   235
            array[i] = -1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   236
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   237
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   238
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   239
    static boolean array_check2(String name) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   240
        boolean success = true;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   241
        for (int i = 0; i < 6; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   242
            if (array[i] != 999) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   243
                success = false;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   244
                System.out.println(name + " failed: array[" + i + "] = " + array[i]);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   245
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   246
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   247
        return success;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   248
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   249
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   250
    static void array_init3() {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   251
        for (int i = 0; i < 3; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   252
            array[i] = -1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   253
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   254
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   255
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   256
    static boolean array_check3(String name) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   257
        boolean success = true;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   258
        for (int i = 0; i < 3; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   259
            if (array[i] != i) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   260
                success = false;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   261
                System.out.println(name + " failed: array[" + i + "] = " + array[i]);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   262
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   263
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   264
        return success;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   265
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   266
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   267
    static void array_init4() {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   268
        for (int i = 0; i < 3; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   269
            byte_array[i] = -1;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   270
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   271
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   272
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   273
    static boolean array_check4(String name) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   274
        boolean success = true;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   275
        for (int i = 0; i < 3; i++) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   276
            if (byte_array[i] != i) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   277
                success = false;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   278
                System.out.println(name + " failed: byte_array[" + i + "] = " + byte_array[i]);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   279
            }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   280
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   281
        return success;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   282
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   283
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   284
    static public void main(String[] args) throws Exception {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   285
        TestMoveStoresOutOfLoops test = new TestMoveStoresOutOfLoops();
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   286
        test.doTest("test_after_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   287
        test.doTest("test_after_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   288
        test.doTest("test_after_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   289
        test.doTest("test_after_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   290
        test.doTest("test_after_5", TestMoveStoresOutOfLoops::array_init2, TestMoveStoresOutOfLoops::array_check2);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   291
        test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   292
        array3[999] = true;
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   293
        test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   294
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   295
        test.doTest("test_stores_1", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   296
        test.doTest("test_stores_2", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   297
        test.doTest("test_stores_3", TestMoveStoresOutOfLoops::array_init4, TestMoveStoresOutOfLoops::array_check4);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   298
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   299
        test.doTest("test_before_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   300
        test.doTest("test_before_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   301
        test.doTest("test_before_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   302
        test.doTest("test_before_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   303
        test.doTest("test_before_5", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   304
        test.doTest("test_before_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   305
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   306
        if (!test.success) {
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   307
            throw new RuntimeException("Some tests failed");
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   308
        }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   309
    }
b82e88dcb26c 8080289: Intermediate writes in a loop not eliminated by optimizer
roland
parents:
diff changeset
   310
}