Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/deoptimization/test06/test06.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/test06.27* VM Testbase keywords: [jit, quick]28*29* @library /vmTestbase30* /test/lib31* @run main/othervm jit.deoptimization.test06.test0632*/3334package jit.deoptimization.test06;3536import nsk.share.TestFailure;3738/*39* Simple recursion that should causes 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 test06 {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;5758public 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.test06.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 Below80public int bar(int index) {81synchronized (this) {82for (int i=0; i<5; i++)83index--;84}85return 0;86}8788public 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.109public 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) {113throw new TestFailure("class B create to early");114}115116if (iter == sIteration) {117try {118result = ((C)(Class.forName("jit.deoptimization.test06.C").newInstance())).foo(index,iter);119result *= ((C)(Class.forName("jit.deoptimization.test06.C").newInstance())).bar(index);120121} catch (Exception x) {122throw new TestFailure("Class not found: C");123}124} else {125result = bar(index);126if (index != 0)127result += foo(--index, iter);128}129return result;130}131}132133class C extends B {134public int foo(int index, int iter) {135for (int i=0; i<iter; i++)136result = bar(i);137138try {139result = ((D)(Class.forName("jit.deoptimization.test06.D").newInstance())).bar(index);140} catch (Exception x) {141throw new TestFailure("Class not found: D");142}143return result;144}145}146147class D extends C {148public int bar(int index) {149return 1;150}151}152153154