1 /*
   2  * Copyright (c) 2019, 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  * @run main/othervm -XX:+EnableValhalla TestArrayAccessDeopt
  29  */
  30 
  31 import java.io.File;
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import jdk.test.lib.process.ProcessTools;
  35 
  36 final inline class MyValue1 {
  37     public final int x = 0;
  38 }
  39 
  40 public class TestArrayAccessDeopt {
  41 
  42     public static void test1(Object[] va, Object vt) {
  43         va[0] = vt;
  44     }
  45 
  46     public static void test2(Object[] va, MyValue1? vt) {
  47         va[0] = vt;
  48     }
  49 
  50     public static void test3(MyValue1?[] va, Object vt) {
  51         va[0] = (MyValue1?)vt;
  52     }
  53 
  54     public static void test4(MyValue1?[] va, MyValue1? vt) {
  55         va[0] = vt;
  56     }
  57 
  58     public static void test5(Object[] va, MyValue1 vt) {
  59         va[0] = vt;
  60     }
  61 
  62     public static void test6(MyValue1[] va, Object vt) {
  63         va[0] = (MyValue1)vt;
  64     }
  65 
  66     public static void test7(MyValue1[] va, MyValue1 vt) {
  67         va[0] = vt;
  68     }
  69 
  70     public static void test8(MyValue1?[] va, MyValue1 vt) {
  71         va[0] = vt;
  72     }
  73 
  74     public static void test9(MyValue1[] va, MyValue1? vt) {
  75         va[0] = (MyValue1)vt;
  76     }
  77 
  78     public static void test10(Object[] va) {
  79         va[0] = null;
  80     }
  81 
  82     public static void test11(MyValue1?[] va) {
  83         va[0] = null;
  84     }
  85 
  86     static public void main(String[] args) throws Exception {
  87         if (args.length == 0) {
  88             // Run test in new VM instance
  89             String[] arg = {"-XX:+EnableValhalla", "-XX:CompileCommand=quiet", "-XX:CompileCommand=compileonly,TestArrayAccessDeopt::test*",
  90                             "-XX:+TraceDeoptimization",  "-Xbatch", "-XX:-MonomorphicArrayCheck", "TestArrayAccessDeopt", "run"};
  91             OutputAnalyzer oa = ProcessTools.executeTestJvm(arg);
  92             String output = oa.getOutput();
  93             oa.shouldNotContain("Uncommon trap occurred");
  94         } else {
  95             MyValue1[] va = new MyValue1[1];
  96             MyValue1?[] vaB = new MyValue1?[1];
  97             MyValue1 vt = new MyValue1();
  98             for (int i = 0; i < 10_000; ++i) {
  99                 test1(va, vt);
 100                 test1(vaB, vt);
 101                 test1(vaB, null);
 102                 test2(va, vt);
 103                 test2(vaB, vt);
 104                 test2(vaB, null);
 105                 test3(va, vt);
 106                 test3(vaB, vt);
 107                 test3(vaB, null);
 108                 test4(va, vt);
 109                 test4(vaB, vt);
 110                 test4(vaB, null);
 111                 test5(va, vt);
 112                 test5(vaB, vt);
 113                 test6(va, vt);
 114                 try {
 115                     test6(va, null);
 116                 } catch (NullPointerException npe) {
 117                     // Expected
 118                 }
 119                 test7(va, vt);
 120                 test8(va, vt);
 121                 test8(vaB, vt);
 122                 test9(va, vt);
 123                 try {
 124                     test9(va, null);
 125                 } catch (NullPointerException npe) {
 126                     // Expected
 127                 }
 128                 test10(vaB);
 129                 test11(vaB);
 130             }
 131         }
 132     }
 133 }