1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. 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 8214344
  27  * @summary LoadVector from a known element of a stable array shouldn't attempt to constant fold
  28  * @modules java.base/jdk.internal.vm.annotation
  29  *
  30  * @run main/bootclasspath/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation -XX:-UseOnStackReplacement LoadVectorFromStableArray
  31  *                                 
  32  */
  33 
  34 import jdk.internal.vm.annotation.Stable;
  35 
  36 public class LoadVectorFromStableArray {
  37     public static void main(String[] args) {
  38         byte[] byte_array = new byte[100];
  39         for (int i = 0; i < 20_000; i++) {
  40             test_helper(0, 0);
  41             test_helper(42, 0);
  42             test_helper2(0, 20, byte_array, byte_array);
  43         }
  44         for (int i = 0; i < 20_000; i++) {
  45             test(20, true);
  46             test(20, false);
  47         }
  48     }
  49 
  50     static @Stable byte[] stable_array1 = new byte[100];
  51     static @Stable byte[] stable_array2 = new byte[100];
  52     
  53     private static void test(int stop, boolean flag) {
  54         int start = 0;
  55         byte[] byte_array1 = stable_array1;
  56         byte[] byte_array2 = flag ? stable_array1 : stable_array2;
  57         int k = 2;
  58         for (; k < 4; k *= 2);
  59         int i = test_helper(k, stop);
  60         // Loop in this method is unrolled and vectorized, then, upper
  61         // bound is found to be constant and small. A single iteration
  62         // of the main loop is executed. That iteration attempts to
  63         // load a vector element from the array at a constant offset.
  64         test_helper2(start, i, byte_array1, byte_array2);
  65     }
  66 
  67 
  68     private static void test_helper2(int start, int stop, byte[] byte_array1, byte[] byte_array2) {
  69         for (int j = start; j < stop; j++) {
  70             byte b = byte_array1[j+3];
  71             byte_array2[j+3] = b;
  72         }
  73     }
  74 
  75     private static int test_helper(int k, int stop) {
  76         int i = 0;
  77         if (k == 42) {
  78             i = stop;
  79         } else {
  80             i = 5;
  81         }
  82         return i;
  83     }
  84 }