1 /*
   2  * Copyright (c) 2015, 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 8086046
  27  * @summary load bypasses arraycopy that sets the value after the ArrayCopyNode is expanded
  28  * @run main/othervm -XX:-BackgroundCompilation  -XX:-UseOnStackReplacement -XX:CompileCommand=dontinline,TestLoadBypassArrayCopy::test_helper -XX:-TieredCompilation TestLoadBypassArrayCopy
  29  *
  30  */
  31 
  32 public class TestLoadBypassArrayCopy {
  33 
  34     static long i;
  35     static boolean test_helper() {
  36         i++;
  37         if ((i%10) == 0) {
  38             return false;
  39         }
  40         return true;
  41     }
  42 
  43     static int test(int[] src, int len, boolean flag) {
  44         int[] dest = new int[10];
  45         int res = 0;
  46         while (test_helper()) {
  47             System.arraycopy(src, 0, dest, 0, len);
  48             // predicate moved out of loop so control of following
  49             // load is not the ArrayCopyNode. Otherwise, if the memory
  50             // of the load is changed and the control is set to the
  51             // ArrayCopyNode the graph is unschedulable and the test
  52             // doesn't fail.
  53             if (flag) {
  54             }
  55             // The memory of this load shouldn't bypass the arraycopy
  56             res = dest[0];
  57         }
  58         return res;
  59     }
  60     
  61     static public void main(String[] args) {
  62         int[] src = new int[10];
  63         src[0] = 0x42;
  64         for (int i = 0; i < 20000; i++) {
  65             int res = test(src, 10, false);
  66             if (res != src[0]) {
  67                 throw new RuntimeException("test failed");
  68             }
  69         }
  70     }
  71 }