1 /*
   2  * Copyright (c) 2015, 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  * @bug 8072016
  27  * @summary Infinite deoptimization/recompilation cycles in case of arraycopy with tightly coupled allocation
  28  * @library /testlibrary /../../test/lib /compiler/whitebox
  29  * @build TestArrayCopyNoInitDeopt
  30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  31  * @run main ClassFileInstaller com.oracle.java.testlibrary.Platform
  32  * @run main/othervm -Xmixed -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  33  *                   -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:TypeProfileLevel=020
  34  *                   TestArrayCopyNoInitDeopt
  35  *
  36  */
  37 
  38 
  39 import sun.hotspot.WhiteBox;
  40 import sun.hotspot.code.NMethod;
  41 import com.oracle.java.testlibrary.Platform;
  42 import java.lang.reflect.*;
  43 
  44 public class TestArrayCopyNoInitDeopt {
  45 
  46     public static int[] m1(Object src) {
  47         if (src == null) return null;
  48         int[] dest = new int[10];
  49         try {
  50             System.arraycopy(src, 0, dest, 0, 10);
  51         } catch (ArrayStoreException npe) {
  52         }
  53         return dest;
  54     }
  55 
  56     static Object m2_src(Object src) {
  57         return src;
  58     }
  59 
  60     public static int[] m2(Object src) {
  61         if (src == null) return null;
  62         src = m2_src(src);
  63         int[] dest = new int[10];
  64         try {
  65             System.arraycopy(src, 0, dest, 0, 10);
  66         } catch (ArrayStoreException npe) {
  67         }
  68         return dest;
  69     }
  70 
  71     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  72 
  73     static boolean deoptimize(Method method, Object src_obj) throws Exception {
  74         for (int i = 0; i < 10; i++) {
  75             method.invoke(null, src_obj);
  76             if (!WHITE_BOX.isMethodCompiled(method)) {
  77                 return true;
  78             }
  79         }
  80         return false;
  81     }
  82 
  83     static public void main(String[] args) throws Exception {
  84         if (Platform.isServer()) {
  85             int[] src = new int[10];
  86             Object src_obj = new Object();
  87             Method method_m1 = TestArrayCopyNoInitDeopt.class.getMethod("m1", Object.class);
  88             Method method_m2 = TestArrayCopyNoInitDeopt.class.getMethod("m2", Object.class);
  89 
  90             // Warm up
  91             for (int i = 0; i < 20000; i++) {
  92                 m1(src);
  93             }
  94 
  95             // And make sure m1 is compiled by C2
  96             WHITE_BOX.enqueueMethodForCompilation(method_m1, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
  97 
  98             if (!WHITE_BOX.isMethodCompiled(method_m1)) {
  99                 throw new RuntimeException("m1 not compiled");
 100             }
 101 
 102             // should deoptimize for type check
 103             if (!deoptimize(method_m1, src_obj)) {
 104                 throw new RuntimeException("m1 not deoptimized");
 105             }
 106 
 107             WHITE_BOX.enqueueMethodForCompilation(method_m1, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
 108 
 109             if (!WHITE_BOX.isMethodCompiled(method_m1)) {
 110                 throw new RuntimeException("m1 not recompiled");
 111             }
 112 
 113             if (deoptimize(method_m1, src_obj)) {
 114                 throw new RuntimeException("m1 deoptimized again");
 115             }
 116 
 117             // Same test as above but with speculative types
 118 
 119             // Warm up & make sure we collect type profiling
 120             for (int i = 0; i < 20000; i++) {
 121                 m2(src);
 122             }
 123 
 124             // And make sure m2 is compiled by C2
 125             WHITE_BOX.enqueueMethodForCompilation(method_m2, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
 126 
 127             if (!WHITE_BOX.isMethodCompiled(method_m2)) {
 128                 throw new RuntimeException("m2 not compiled");
 129             }
 130 
 131             // should deoptimize for speculative type check
 132             if (!deoptimize(method_m2, src_obj)) {
 133                 throw new RuntimeException("m2 not deoptimized");
 134             }
 135 
 136             WHITE_BOX.enqueueMethodForCompilation(method_m2, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
 137 
 138             if (!WHITE_BOX.isMethodCompiled(method_m2)) {
 139                 throw new RuntimeException("m2 not recompiled");
 140             }
 141 
 142             // should deoptimize for actual type check
 143             if (!deoptimize(method_m2, src_obj)) {
 144                 throw new RuntimeException("m2 not deoptimized");
 145             }
 146 
 147             WHITE_BOX.enqueueMethodForCompilation(method_m2, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
 148 
 149             if (!WHITE_BOX.isMethodCompiled(method_m2)) {
 150                 throw new RuntimeException("m2 not recompiled");
 151             }
 152 
 153             if (deoptimize(method_m2, src_obj)) {
 154                 throw new RuntimeException("m2 deoptimized again");
 155             }
 156         }
 157     }
 158 }