< prev index next >

test/hotspot/jtreg/compiler/loopopts/TestMoveStoresOutOfLoops.java

Print this page




   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 8080289
  27  * @summary Move stores out of loops if possible
  28  *
  29  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation
  30  *      -XX:CompileCommand=dontinline,compiler.loopopts.TestMoveStoresOutOfLoops::test*
  31  *      compiler.loopopts.TestMoveStoresOutOfLoops
  32  */
  33 
  34 package compiler.loopopts;
  35 
  36 import java.lang.reflect.Method;
  37 import java.lang.reflect.Modifier;
  38 import java.util.HashMap;
  39 import java.util.function.Function;
  40 
  41 public class TestMoveStoresOutOfLoops {
  42 
  43     private static long[] array = new long[10];
  44     private static long[] array2 = new long[10];
  45     private static boolean[] array3 = new boolean[1000];

  46     private static byte[] byte_array = new byte[10];
  47 
  48     // Array store should be moved out of the loop, value stored
  49     // should be 999, the loop should be eliminated
  50     static void test_after_1(int idx) {
  51         for (int i = 0; i < 1000; i++) {
  52             array[idx] = i;
  53         }
  54     }
  55 
  56     // Array store can't be moved out of loop because of following
  57     // non loop invariant array access
  58     static void test_after_2(int idx) {
  59         for (int i = 0; i < 1000; i++) {
  60             array[idx] = i;
  61             array2[i%10] = i;
  62         }
  63     }
  64 
  65     // Array store can't be moved out of loop because of following


  91             array[idx] = i;
  92             array[idx+1] = i;
  93             array[idx+2] = i;
  94             array[idx+3] = i;
  95             array[idx+4] = i;
  96             array[idx+5] = i;
  97         }
  98     }
  99 
 100     // Array store can be moved after the loop but needs to be
 101     // cloned on both exit paths
 102     static void test_after_6(int idx) {
 103         for (int i = 0; i < 1000; i++) {
 104             array[idx] = i;
 105             if (array3[i]) {
 106                 return;
 107             }
 108         }
 109     }
 110 









 111     // Optimize out redundant stores
 112     static void test_stores_1(int ignored) {
 113         array[0] = 0;
 114         array[1] = 1;
 115         array[2] = 2;
 116         array[0] = 0;
 117         array[1] = 1;
 118         array[2] = 2;
 119     }
 120 
 121     static void test_stores_2(int idx) {
 122         array[idx+0] = 0;
 123         array[idx+1] = 1;
 124         array[idx+2] = 2;
 125         array[idx+0] = 0;
 126         array[idx+1] = 1;
 127         array[idx+2] = 2;
 128     }
 129 
 130     static void test_stores_3(int idx) {


 268         return success;
 269     }
 270 
 271     static void array_init4() {
 272         for (int i = 0; i < 3; i++) {
 273             byte_array[i] = -1;
 274         }
 275     }
 276 
 277     static boolean array_check4(String name) {
 278         boolean success = true;
 279         for (int i = 0; i < 3; i++) {
 280             if (byte_array[i] != i) {
 281                 success = false;
 282                 System.out.println(name + " failed: byte_array[" + i + "] = " + byte_array[i]);
 283             }
 284         }
 285         return success;
 286     }
 287 











 288     static public void main(String[] args) throws Exception {
 289         TestMoveStoresOutOfLoops test = new TestMoveStoresOutOfLoops();
 290         test.doTest("test_after_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 291         test.doTest("test_after_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 292         test.doTest("test_after_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 293         test.doTest("test_after_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 294         test.doTest("test_after_5", TestMoveStoresOutOfLoops::array_init2, TestMoveStoresOutOfLoops::array_check2);
 295         test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 296         array3[999] = true;
 297         test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);

 298 
 299         test.doTest("test_stores_1", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
 300         test.doTest("test_stores_2", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
 301         test.doTest("test_stores_3", TestMoveStoresOutOfLoops::array_init4, TestMoveStoresOutOfLoops::array_check4);
 302 
 303         test.doTest("test_before_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 304         test.doTest("test_before_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 305         test.doTest("test_before_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 306         test.doTest("test_before_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 307         test.doTest("test_before_5", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 308         test.doTest("test_before_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 309 
 310         if (!test.success) {
 311             throw new RuntimeException("Some tests failed");
 312         }
 313     }
 314 }


   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 8080289 8189067
  27  * @summary Move stores out of loops if possible
  28  *
  29  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation
  30  *      -XX:CompileCommand=dontinline,compiler.loopopts.TestMoveStoresOutOfLoops::test*
  31  *      compiler.loopopts.TestMoveStoresOutOfLoops
  32  */
  33 
  34 package compiler.loopopts;
  35 
  36 import java.lang.reflect.Method;
  37 import java.lang.reflect.Modifier;
  38 import java.util.HashMap;
  39 import java.util.function.Function;
  40 
  41 public class TestMoveStoresOutOfLoops {
  42 
  43     private static long[] array = new long[10];
  44     private static long[] array2 = new long[10];
  45     private static boolean[] array3 = new boolean[1000];
  46     private static int[] array4 = new int[1000];
  47     private static byte[] byte_array = new byte[10];
  48 
  49     // Array store should be moved out of the loop, value stored
  50     // should be 999, the loop should be eliminated
  51     static void test_after_1(int idx) {
  52         for (int i = 0; i < 1000; i++) {
  53             array[idx] = i;
  54         }
  55     }
  56 
  57     // Array store can't be moved out of loop because of following
  58     // non loop invariant array access
  59     static void test_after_2(int idx) {
  60         for (int i = 0; i < 1000; i++) {
  61             array[idx] = i;
  62             array2[i%10] = i;
  63         }
  64     }
  65 
  66     // Array store can't be moved out of loop because of following


  92             array[idx] = i;
  93             array[idx+1] = i;
  94             array[idx+2] = i;
  95             array[idx+3] = i;
  96             array[idx+4] = i;
  97             array[idx+5] = i;
  98         }
  99     }
 100 
 101     // Array store can be moved after the loop but needs to be
 102     // cloned on both exit paths
 103     static void test_after_6(int idx) {
 104         for (int i = 0; i < 1000; i++) {
 105             array[idx] = i;
 106             if (array3[i]) {
 107                 return;
 108             }
 109         }
 110     }
 111 
 112     // Array store can be moved out of the inner loop
 113     static void test_after_7(int idx) {
 114         for (int i = 0; i < 1000; i++) {
 115             for (int j = 0; j <= 42; j++) {
 116                 array4[i] = j;
 117             }
 118         }
 119     }
 120 
 121     // Optimize out redundant stores
 122     static void test_stores_1(int ignored) {
 123         array[0] = 0;
 124         array[1] = 1;
 125         array[2] = 2;
 126         array[0] = 0;
 127         array[1] = 1;
 128         array[2] = 2;
 129     }
 130 
 131     static void test_stores_2(int idx) {
 132         array[idx+0] = 0;
 133         array[idx+1] = 1;
 134         array[idx+2] = 2;
 135         array[idx+0] = 0;
 136         array[idx+1] = 1;
 137         array[idx+2] = 2;
 138     }
 139 
 140     static void test_stores_3(int idx) {


 278         return success;
 279     }
 280 
 281     static void array_init4() {
 282         for (int i = 0; i < 3; i++) {
 283             byte_array[i] = -1;
 284         }
 285     }
 286 
 287     static boolean array_check4(String name) {
 288         boolean success = true;
 289         for (int i = 0; i < 3; i++) {
 290             if (byte_array[i] != i) {
 291                 success = false;
 292                 System.out.println(name + " failed: byte_array[" + i + "] = " + byte_array[i]);
 293             }
 294         }
 295         return success;
 296     }
 297 
 298     static boolean array_check5(String name) {
 299         boolean success = true;
 300         for (int i = 0; i < 1000; i++) {
 301             if (array4[i] != 42) {
 302                 success = false;
 303                 System.out.println(name + " failed: array[" + i + "] = " + array4[i]);
 304             }
 305         }
 306         return success;
 307     }
 308 
 309     static public void main(String[] args) throws Exception {
 310         TestMoveStoresOutOfLoops test = new TestMoveStoresOutOfLoops();
 311         test.doTest("test_after_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 312         test.doTest("test_after_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 313         test.doTest("test_after_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 314         test.doTest("test_after_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 315         test.doTest("test_after_5", TestMoveStoresOutOfLoops::array_init2, TestMoveStoresOutOfLoops::array_check2);
 316         test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 317         array3[999] = true;
 318         test.doTest("test_after_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 319         test.doTest("test_after_7", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check5);
 320 
 321         test.doTest("test_stores_1", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
 322         test.doTest("test_stores_2", TestMoveStoresOutOfLoops::array_init3, TestMoveStoresOutOfLoops::array_check3);
 323         test.doTest("test_stores_3", TestMoveStoresOutOfLoops::array_init4, TestMoveStoresOutOfLoops::array_check4);
 324 
 325         test.doTest("test_before_1", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 326         test.doTest("test_before_2", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 327         test.doTest("test_before_3", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 328         test.doTest("test_before_4", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 329         test.doTest("test_before_5", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 330         test.doTest("test_before_6", TestMoveStoresOutOfLoops::array_init, TestMoveStoresOutOfLoops::array_check);
 331 
 332         if (!test.success) {
 333             throw new RuntimeException("Some tests failed");
 334         }
 335     }
 336 }
< prev index next >