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/test04.
  28  * VM Testbase keywords: [jit, quick]
  29  *
  30  * @library /vmTestbase
  31  *          /test/lib
  32  * @run driver jdk.test.lib.FileInstaller . .
  33  * @build jit.deoptimization.test04.test04
  34  * @run driver ExecDriver --java jit.deoptimization.test04.test04
  35  */
  36 
  37 package jit.deoptimization.test04;
  38 
  39 import nsk.share.TestFailure;
  40 
  41 /*
  42  *
  43  */
  44 
  45 class test04 {
  46         public static void main (String[] args) {
  47                 A obj = new A();
  48 
  49                 int result = -1;
  50                 for (int index = 0; index < 1; index++) {
  51                         result += obj.used_alot();
  52                 }
  53 
  54                 if (result != 1) {
  55                         throw new TestFailure("result : " + result + " must equal 1");
  56                 }
  57         }
  58 }
  59 
  60 
  61 class A {
  62         protected int count;
  63         public int foo(int index) {
  64                 int rv = ++count;
  65                 if (index < A.sIndex / 2)
  66                         rv = index;
  67                 else {
  68                         try {
  69                                 rv = ((A)Class.forName("jit.deoptimization.test04.B").newInstance()).foo(index);
  70                         }
  71                         catch(Exception e) {
  72                         }
  73                 }
  74                 return rv;
  75         }
  76 
  77         public int used_alot() {
  78                 int result = 1;
  79                 for (int i = 0; i < A.sIndex; i++) {
  80                         result = foo(i);
  81                 }
  82                 return result;
  83         }
  84 
  85         protected static final int sIndex = 25000;
  86 }
  87 
  88 class B extends A {
  89         public int foo(int index) {
  90                 int rv = 0;
  91                 int qrtr = A.sIndex / 4;
  92                 if (index > 3 * qrtr) {
  93                         try {
  94                                 if (index < A.sIndex - qrtr)
  95                                         rv = ((B)Class.forName("jit.deoptimization.test04.C").newInstance()).foo(index);
  96                                 else
  97                                         rv = ((B)Class.forName("jit.deoptimization.test04.D").newInstance()).foo(index);
  98                         }
  99                         catch(Exception e) {
 100                         }
 101                 }
 102                 else {
 103                         rv = 1;
 104                 }
 105                 return rv;
 106         }
 107 }
 108 
 109 class C extends B {
 110         public C () {
 111                 --this.count;
 112         }
 113 
 114         public int foo(int index) {
 115                 int j = count;
 116                 for (int i=0; i<500; i++)
 117                         j += used_alot();
 118                 return j;
 119         }
 120 
 121         public int used_alot() {
 122                 int i=1;
 123                 int j=4;
 124                 int k=i+j;
 125                 byte ba[] = new byte[1000];
 126                 int ia[] = new int[1000];
 127                 return this.count + (ba.length + i + j - k - ia.length);
 128         }
 129 
 130         protected int count = 1;
 131 }
 132 
 133 class D extends C {
 134         public D () {
 135                 super();
 136 
 137                 this.count+=2;
 138         }
 139 
 140         public int foo(int index) {
 141                 return super.foo(index);
 142         }
 143 
 144         public synchronized int used_alot() {
 145                 count += (--count);
 146                 return 0;
 147         }
 148 }