1 /*
   2  * Copyright (c) 2008, 2018, 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  *
  27  * @summary converted from VM Testbase jit/deoptimization/test06.
  28  * VM Testbase keywords: [jit, quick]
  29  *
  30  * @library /vmTestbase
  31  *          /test/lib
  32  * @run driver jdk.test.lib.FileInstaller . .
  33  * @build jit.deoptimization.test06.test06
  34  * @run driver ExecDriver --java jit.deoptimization.test06.test06
  35  */
  36 
  37 package jit.deoptimization.test06;
  38 
  39 import nsk.share.TestFailure;
  40 
  41 /*
  42  *      Simple recursion that should causes hotspot to deoptimize
  43  *      used_alot and A.foo and A.bar methods
  44  *      Expected result for the test is: 0
  45  *
  46  *      run with the -XX:TraceDeoptimization to observ the result.
  47  */
  48 
  49 class test06 {
  50   public static void main (String[] args) {
  51     A obj = new A();
  52     for (int index = 0; index < 1; index++) {
  53       obj.used_alot();
  54     }
  55   }
  56 }
  57 
  58 class A {
  59         static int result = 0;
  60 
  61         public int foo(int index, int iter) {
  62                 if (index == 0)
  63                         return result;
  64                 else if (iter <= sIteration / 2) {
  65                         result = bar(index) * foo(--index, iter);
  66                 } else {
  67                         try {
  68                                 // halfway through the max iterations
  69                                 // create a B. This should cause the
  70                                 // used_alot to be deoptimized
  71                                 // Call b.frame for the remaining part of the recursion
  72                                 if (b == null)
  73                                         b = Class.forName("jit.deoptimization.test06.B").newInstance();
  74                                 result *= ((B)b).foo(index, iter);
  75                         } catch (Exception x) {
  76                                 throw new TestFailure("Class not found: B");
  77                         }
  78                 }
  79                 return result;
  80         }
  81 
  82         // Does nothing but it will let be over written in class C, See Below
  83         public int bar(int index) {
  84                 synchronized (this) {
  85                         for (int i=0; i<5; i++)
  86                                 index--;
  87                 }
  88                 return 0;
  89         }
  90 
  91         public void used_alot() {
  92                 int result = 1;
  93 
  94                 for (int index = 1; index <= sIteration; index++) {
  95                         result *= foo(index, index);
  96                 }
  97 
  98                 if (result != 0) {
  99                         throw new TestFailure("Result: " + result);
 100                 }
 101         }
 102 
 103         protected Object b = null;
 104         protected static final int sIteration = 1000;
 105 }
 106 
 107 class B extends A {
 108         //      Override foo in order to force deoptimization.
 109         //      Also creates and instance of class C in the last
 110         //      iteration in order to force A.foo to get deoptimized
 111         //      otherwise just do something stupid.
 112   public int foo(int index, int iter) {
 113                 //      Make sure that this class was created at least
 114                 //      halfway through the iteration. Simple sanity check
 115         if (iter < sIteration /2) {
 116                 throw new TestFailure("class B create to early");
 117         }
 118 
 119                 if (iter == sIteration) {
 120                         try {
 121                                 result = ((C)(Class.forName("jit.deoptimization.test06.C").newInstance())).foo(index,iter);
 122                                 result *= ((C)(Class.forName("jit.deoptimization.test06.C").newInstance())).bar(index);
 123 
 124                         } catch (Exception x) {
 125                                 throw new TestFailure("Class not found: C");
 126                         }
 127                 } else {
 128                         result = bar(index);
 129                         if (index != 0)
 130                                 result += foo(--index, iter);
 131                 }
 132         return result;
 133   }
 134 }
 135 
 136 class C extends B {
 137   public int foo(int index, int iter) {
 138         for (int i=0; i<iter; i++)
 139                 result = bar(i);
 140 
 141                 try {
 142                         result = ((D)(Class.forName("jit.deoptimization.test06.D").newInstance())).bar(index);
 143                 } catch (Exception x) {
 144                         throw new TestFailure("Class not found: D");
 145                 }
 146                 return result;
 147         }
 148 }
 149 
 150 class D extends C {
 151   public int bar(int index) {
 152                 return 1;
 153         }
 154 }