1 /*
   2  * Copyright (c) 2017, 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 8182997 8214898
  27  * @library /test/lib
  28  * @summary Test the handling of Arrays of unloaded value classes.
  29  * @compile -XDemitQtypes -XDenableValueTypes -XDallowFlattenabilityModifiers -XDallowWithFieldOperator TestUnloadedValueTypeArray.java
  30  * @run main/othervm -XX:+EnableValhalla -Xcomp
  31  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test1
  32  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test2
  33  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test3
  34  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test4
  35  *      TestUnloadedValueTypeArray
  36  */
  37 
  38 import jdk.test.lib.Asserts;
  39 
  40 value final class MyValue {
  41     final int foo;
  42 
  43     private MyValue() {
  44         foo = 0x42;
  45     }
  46 }
  47 
  48 value final class MyValue2 {
  49     final int foo;
  50 
  51     private MyValue2() {
  52         foo = 0x42;
  53     }
  54     static MyValue2 make(int n) {
  55         return __WithField(MyValue2.default.foo, n);
  56     }
  57 }
  58 
  59 value final class MyValue3 {
  60     final int foo;
  61 
  62     private MyValue3() {
  63         foo = 0x42;
  64     }
  65     static MyValue3 make(int n) {
  66         return __WithField(MyValue3.default.foo, n);
  67     }
  68 }
  69 
  70 value final class MyValue4 {
  71     final int foo;
  72 
  73     private MyValue4() {
  74         foo = 0x53;
  75     }
  76     static MyValue4 make(int n) {
  77         return __WithField(MyValue4.default.foo, n);
  78     }
  79 }
  80 
  81 public class TestUnloadedValueTypeArray {
  82 
  83     static MyValue[] target() {
  84         return new MyValue[10];
  85     }
  86 
  87     static void test1() {
  88         target();
  89     }
  90 
  91     static int test2(MyValue2[] arr) {
  92         if (arr != null) {
  93             return arr[1].foo;
  94         } else {
  95             return 1234;
  96         }
  97     }
  98 
  99     static void test2_verifier() {
 100         int n = 50000;
 101 
 102         int m = 9999;
 103         for (int i=0; i<n; i++) {
 104             m = test2(null);
 105         }
 106         Asserts.assertEQ(m, 1234);
 107 
 108         MyValue2[] arr = new MyValue2[2];
 109         arr[1] = MyValue2.make(5678);
 110         m = 9999;
 111         for (int i=0; i<n; i++) {
 112             m = test2(arr);
 113         }
 114         Asserts.assertEQ(m, 5678);
 115     }
 116 
 117     static void test3(MyValue3[] arr) {
 118         if (arr != null) {
 119             arr[1] = MyValue3.make(2345);
 120         }
 121     }
 122 
 123     static void test3_verifier() {
 124         int n = 50000;
 125 
 126         for (int i=0; i<n; i++) {
 127             test3(null);
 128         }
 129 
 130         MyValue3[] arr = new MyValue3[2];
 131         for (int i=0; i<n; i++) {
 132             test3(arr);
 133         }
 134         Asserts.assertEQ(arr[1].foo, 2345);
 135     }
 136 
 137     static MyValue4[] test4(boolean b) {
 138         // range check elimination
 139         if (b) {
 140             MyValue4[] arr = new MyValue4[10];
 141             arr[1] = MyValue4.make(2345);
 142             return arr;
 143         } else {
 144             return null;
 145         }
 146     }
 147 
 148     static void test4_verifier() {
 149         int n = 50000;
 150 
 151         for (int i=0; i<n; i++) {
 152             test4(false);
 153         }
 154 
 155         MyValue4[] arr = null;
 156         for (int i=0; i<n; i++) {
 157           arr = test4(true);
 158         }
 159         Asserts.assertEQ(arr[1].foo, 2345);
 160     }
 161 
 162     static public void main(String[] args) {
 163         test1();
 164         test2_verifier();
 165         test3_verifier();
 166         test4_verifier();
 167     }
 168 }