1 /*
   2  * Copyright (c) 2020, 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 8238385
  26  * @summary CTW: C2 (Shenandoah) compilation fails with "Range check dependent CastII node was not removed"
  27  * @key gc
  28  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  29  * @modules java.base/jdk.internal.misc:+open
  30  *
  31  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
  32  *                   FoldIfAfterExpansion
  33  *
  34  */
  35 
  36 import jdk.internal.misc.Unsafe;
  37 
  38 public class FoldIfAfterExpansion {
  39     private static int[] field1 = new int[100];
  40     private static int[] field2 = new int[100];
  41     private static int[] field3;
  42     private static volatile int barrier;
  43 
  44     static final jdk.internal.misc.Unsafe UNSAFE = Unsafe.getUnsafe();
  45 
  46     public static void main(String[] args) {
  47         for (int i = 0; i < 20_000; i++) {
  48             test(true, 10, false, true);
  49             test(false, 10, false, false);
  50         }
  51     }
  52 
  53     private static Object test(boolean flag, int i, boolean flag2, boolean flag3) {
  54         int[] array;
  55         if (flag) {
  56             barrier = 1;
  57             array = field1;
  58             final int length = array.length;
  59             if (flag2) {
  60                 field3 = array;
  61             }
  62         } else {
  63             barrier = 1;
  64             array = field1;
  65             final int length = array.length;
  66             if (flag2) {
  67                 field3 = array;
  68             }
  69         }
  70 
  71         array = field1;
  72 
  73         if (flag3) {
  74             if (i < 0 || i >= array.length) {
  75                 throw new RuntimeException();
  76             }
  77             long l = (long)i;
  78             l = l * UNSAFE.ARRAY_INT_INDEX_SCALE + UNSAFE.ARRAY_INT_BASE_OFFSET;
  79             UNSAFE.putInt(array, l, i);
  80         } else {
  81             if (i < 0 || i >= array.length) {
  82                 throw new RuntimeException();
  83             }
  84             long l = (long)i;
  85             l = l * UNSAFE.ARRAY_INT_INDEX_SCALE + UNSAFE.ARRAY_INT_BASE_OFFSET;
  86             UNSAFE.putInt(array, l, i);
  87         }
  88 
  89         return array;
  90     }
  91 }