Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/deoptimization/test07/test07.java
40948 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25*26* @summary converted from VM Testbase jit/deoptimization/test07.27* VM Testbase keywords: [jit, quick]28*29* @library /vmTestbase30* /test/lib31* @run main/othervm jit.deoptimization.test07.test0732*/3334package jit.deoptimization.test07;3536import nsk.share.TestFailure;3738/*39* Simple recursion that should cause hotspot to deoptimize40* used_alot and A.foo and A.bar methods41* Expected result for the test is: 042*43* run with the -XX:TraceDeoptimization to observ the result.44*/4546public class test07 {47public static void main (String[] args) {48A obj = new A();49for (int index = 0; index < 1; index++) {50obj.used_alot();51}52}53}5455class A {56static int result = 0;5758synchronized public int foo(int index, int iter) {59if (index == 0)60return result;61else if (iter <= sIteration / 2) {62result = bar(index) * foo(--index, iter);63} else {64try {65// halfway through the max iterations66// create a B. This should cause the67// used_alot to be deoptimized68// Call b.frame for the remaining part of the recursion69if (b == null)70b = Class.forName("jit.deoptimization.test07.B").newInstance();71result *= ((B)b).foo(index, iter);72} catch (Exception x) {73throw new TestFailure("Class not found: B");74}75}76return result;77}7879// Does nothing but it will let be over written in class C, See Below80synchronized public int bar(int index) {81synchronized (this) {82for (int i=0; i<5; i++)83index--;84}85return 0;86}8788synchronized public void used_alot() {89int result = 1;9091for (int index = 1; index <= sIteration; index++) {92result *= foo(index, index);93}9495if (result != 0) {96throw new TestFailure("Result: " + result);97}98}99100protected Object b = null;101protected static final int sIteration = 1000;102}103104class B extends A {105// Override foo in order to force deoptimization.106// Also creates and instance of class C in the last107// iteration in order to force A.foo to get deoptimized108// otherwise just do something stupid.109synchronized public int foo(int index, int iter) {110// Make sure that this class was created at least111// halfway through the iteration. Simple sanity check112if (iter < sIteration /2) {113System.out.println ("class B create to early");114System.exit(1);115}116117if (iter == sIteration) {118try {119result = ((C)(Class.forName("jit.deoptimization.test07.C").newInstance())).foo(index,iter);120result *= ((C)(Class.forName("jit.deoptimization.test07.C").newInstance())).bar(index);121122} catch (Exception x) {123throw new TestFailure("Class not found: C");124}125} else {126result = bar(index);127if (index != 0)128result += foo(--index, iter);129}130return result;131}132}133134class C extends B {135synchronized public int foo(int index, int iter) {136for (int i=0; i<iter; i++)137result = bar(i);138139try {140result = ((D)(Class.forName("jit.deoptimization.test07.D").newInstance())).bar(index);141} catch (Exception x) {142throw new TestFailure("Class not found: D");143}144return result;145}146}147148class D extends C {149synchronized public int bar(int index) {150return 1;151}152}153154155