1 /*
  2  * Copyright (c) 2019, 2024, 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  * @summary Verify that certain array accesses do not trigger deoptimization.
 27  * @library /test/lib
 28  * @enablePreview
 29  * @modules java.base/jdk.internal.value
 30  *          java.base/jdk.internal.vm.annotation
 31  * @run main TestArrayAccessDeopt
 32  */
 33 
 34 import java.io.File;
 35 import java.util.Objects;
 36 
 37 import jdk.test.lib.Asserts;
 38 import jdk.test.lib.process.OutputAnalyzer;
 39 import jdk.test.lib.process.ProcessTools;
 40 
 41 import jdk.internal.value.ValueClass;
 42 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 43 import jdk.internal.vm.annotation.LooselyConsistentValue;
 44 import jdk.internal.vm.annotation.NullRestricted;
 45 
 46 @ImplicitlyConstructible
 47 @LooselyConsistentValue
 48 value class MyValue1 {
 49     public int x = 0;
 50 }
 51 
 52 public class TestArrayAccessDeopt {
 53 
 54     public static void test1(Object[] va, Object vt) {
 55         va[0] = vt;
 56     }
 57 
 58     public static void test2(Object[] va, MyValue1 vt) {
 59         va[0] = vt;
 60     }
 61 
 62     public static void test3(MyValue1[] va, Object vt) {
 63         va[0] = (MyValue1)vt;
 64     }
 65 
 66     public static void test4(MyValue1[] va, MyValue1 vt) {
 67         va[0] = vt;
 68     }
 69 
 70     public static void test5(Object[] va, MyValue1 vt) {
 71         va[0] = vt;
 72     }
 73 
 74     public static void test6(MyValue1[] va, Object vt) {
 75         va[0] = (MyValue1)Objects.requireNonNull(vt);
 76     }
 77 
 78     public static void test7(MyValue1[] va, MyValue1 vt) {
 79         va[0] = vt;
 80     }
 81 
 82     public static void test8(MyValue1[] va, MyValue1 vt) {
 83         va[0] = vt;
 84     }
 85 
 86     public static void test9(MyValue1[] va, MyValue1 vt) {
 87         va[0] = Objects.requireNonNull(vt);
 88     }
 89 
 90     public static void test10(Object[] va) {
 91         va[0] = null;
 92     }
 93 
 94     public static void test11(MyValue1[] va) {
 95         va[0] = null;
 96     }
 97 
 98     static public void main(String[] args) throws Exception {
 99         if (args.length == 0) {
100             // Run test in new VM instance
101             String[] arg = {"--enable-preview", "--add-exports", "java.base/jdk.internal.vm.annotation=ALL-UNNAMED", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED",
102                             "-XX:CompileCommand=quiet", "-XX:CompileCommand=compileonly,TestArrayAccessDeopt::test*", "-XX:-UseArrayLoadStoreProfile",
103                             "-XX:+TraceDeoptimization", "-Xbatch", "-XX:-MonomorphicArrayCheck", "-Xmixed", "-XX:+ProfileInterpreter", "TestArrayAccessDeopt", "run"};
104             OutputAnalyzer oa = ProcessTools.executeTestJava(arg);
105             String output = oa.getOutput();
106             oa.shouldNotContain("UNCOMMON TRAP");
107         } else {
108             MyValue1[] va = (MyValue1[])ValueClass.newNullRestrictedArray(MyValue1.class, 1);
109             MyValue1[] vaB = new MyValue1[1];
110             MyValue1 vt = new MyValue1();
111             for (int i = 0; i < 10_000; ++i) {
112                 test1(va, vt);
113                 test1(vaB, vt);
114                 test1(vaB, null);
115                 test2(va, vt);
116                 test2(vaB, vt);
117                 test2(vaB, null);
118                 test3(va, vt);
119                 test3(vaB, vt);
120                 test3(vaB, null);
121                 test4(va, vt);
122                 test4(vaB, vt);
123                 test4(vaB, null);
124                 test5(va, vt);
125                 test5(vaB, vt);
126                 test6(va, vt);
127                 try {
128                     test6(va, null);
129                     throw new RuntimeException("NullPointerException expected");
130                 } catch (NullPointerException npe) {
131                     // Expected
132                 }
133                 test7(va, vt);
134                 test8(va, vt);
135                 test8(vaB, vt);
136                 test9(va, vt);
137                 try {
138                     test9(va, null);
139                     throw new RuntimeException("NullPointerException expected");
140                 } catch (NullPointerException npe) {
141                     // Expected
142                 }
143                 test10(vaB);
144                 test11(vaB);
145             }
146         }
147     }
148 }