Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest.java
41153 views
/*1* Copyright (c) 2015, 2021, 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* @bug 813642126*27* @requires vm.jvmci & vm.compMode == "Xmixed"28* @requires vm.opt.final.EliminateAllocations == true29*30* @comment no "-Xcomp -XX:-TieredCompilation" combination allowed until JDK-8140018 is resolved31* @requires vm.opt.TieredCompilation == null | vm.opt.TieredCompilation == true32*33* @library / /test/lib34* @library ../common/patches35* @modules java.base/jdk.internal.misc36* @modules java.base/jdk.internal.org.objectweb.asm37* java.base/jdk.internal.org.objectweb.asm.tree38* jdk.internal.vm.ci/jdk.vm.ci.hotspot39* jdk.internal.vm.ci/jdk.vm.ci.code40* jdk.internal.vm.ci/jdk.vm.ci.code.stack41* jdk.internal.vm.ci/jdk.vm.ci.meta42*43* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox44* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox45* @run main/othervm -Xbatch -Xbootclasspath/a:.46* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI47* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI48* -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check49* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame50* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame251* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse52* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame353* -XX:+DoEscapeAnalysis -XX:-UseCounterDecay54* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true55* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false56* compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest57* @run main/othervm -Xbatch -Xbootclasspath/a:.58* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI59* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI60* -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check61* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame62* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame263* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse64* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame365* -XX:+DoEscapeAnalysis -XX:-UseCounterDecay66* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false67* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false68* compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest69* @run main/othervm -Xbatch -Xbootclasspath/a:.70* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI71* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI72* -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check73* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame74* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame275* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse76* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame377* -XX:+DoEscapeAnalysis -XX:-UseCounterDecay78* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true79* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true80* compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest81* @run main/othervm -Xbatch -Xbootclasspath/a:.82* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI83* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI84* -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check85* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame86* -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame287* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse88* -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame389* -XX:+DoEscapeAnalysis -XX:-UseCounterDecay90* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false91* -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true92* compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest93*/9495package compiler.jvmci.compilerToVM;9697import compiler.jvmci.common.CTVMUtilities;98import compiler.testlibrary.CompilerUtils;99import compiler.whitebox.CompilerWhiteBoxTest;100import jdk.test.lib.Asserts;101import jdk.vm.ci.code.stack.InspectedFrame;102import jdk.vm.ci.hotspot.CompilerToVMHelper;103import jdk.vm.ci.hotspot.HotSpotStackFrameReference;104import jdk.vm.ci.meta.ResolvedJavaMethod;105import jtreg.SkippedException;106import sun.hotspot.WhiteBox;107108import java.lang.reflect.Method;109110public class MaterializeVirtualObjectTest {111private static final WhiteBox WB;112private static final boolean INVALIDATE;113private static final int COMPILE_THRESHOLD;114private static final Method MATERIALIZED_METHOD;115private static final Method NOT_MATERIALIZED_METHOD;116private static final Method FRAME3_METHOD;117private static final ResolvedJavaMethod MATERIALIZED_RESOLVED;118private static final ResolvedJavaMethod NOT_MATERIALIZED_RESOLVED;119private static final ResolvedJavaMethod FRAME2_RESOLVED;120private static final ResolvedJavaMethod FRAME3_RESOLVED;121private static final boolean MATERIALIZE_FIRST;122123static {124Method method1;125Method method2;126WB = WhiteBox.getWhiteBox();127try {128method1 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame",129String.class, int.class);130method2 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame2",131String.class, int.class);132FRAME3_METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame3",133Helper.class, int.class);134} catch (NoSuchMethodException e) {135throw new Error("Can't get executable for test method", e);136}137ResolvedJavaMethod resolved1;138resolved1 = CTVMUtilities.getResolvedMethod(method1);139FRAME2_RESOLVED = CTVMUtilities.getResolvedMethod(method2);140FRAME3_RESOLVED = CTVMUtilities.getResolvedMethod(FRAME3_METHOD);141INVALIDATE = Boolean.getBoolean(142"compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");143COMPILE_THRESHOLD = CompilerWhiteBoxTest.THRESHOLD;144MATERIALIZE_FIRST = Boolean.getBoolean(145"compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst");146MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? resolved1 : FRAME2_RESOLVED;147NOT_MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? FRAME2_RESOLVED : resolved1;148MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method1 : method2;149NOT_MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method2 : method1;150}151152public static void main(String[] args) {153int levels[] = CompilerUtils.getAvailableCompilationLevels();154// we need compilation level 4 to use EscapeAnalysis155if (levels.length < 1 || levels[levels.length - 1] != 4) {156throw new SkippedException("Test needs compilation level 4");157}158159new MaterializeVirtualObjectTest().test();160}161162private static String getName() {163return "CASE: invalidate=" + INVALIDATE + ", materializedMethod="164+ (MATERIALIZE_FIRST ? "testFrame" : "testFrame2")165+ ", notMaterializedMethod="166+ (MATERIALIZE_FIRST ? "testFrame2" : "testFrame");167}168169private void test() {170System.out.println(getName());171Asserts.assertFalse(WB.isMethodCompiled(MATERIALIZED_METHOD),172getName() + " : materialized method is compiled");173Asserts.assertFalse(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),174getName() + " : not materialized method is compiled");175for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {176testFrame("someString", i);177}178Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()179+ " : materialized method not compiled");180Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),181getName() + " : not materialized method not compiled");182testFrame("someString", /* materialize */ CompilerWhiteBoxTest.THRESHOLD);183184// run second test types185for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {186testFrame("someString", i);187}188Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()189+ " : materialized method not compiled");190Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),191getName() + " : not materialized method not compiled");192testFrame("someString", /* materialize */ CompilerWhiteBoxTest.THRESHOLD + 1);193}194195private void testFrame(String str, int iteration) {196Helper helper = new Helper(str);197testFrame2(str, iteration);198Asserts.assertTrue((helper.string != null) && (this != null)199&& (helper != null), String.format("%s : some locals are null", getName()));200}201202private void testFrame2(String str, int iteration) {203Helper helper = new Helper(str);204Helper helper2 = new Helper("bar");205testFrame3(helper, iteration);206Asserts.assertTrue((helper.string != null) && (this != null) && helper.string == str207&& (helper != null), String.format("%s : some locals are null", getName()));208Asserts.assertTrue((helper2.string != null) && (this != null)209&& (helper2 != null), String.format("%s : some locals are null", getName()));210}211212private void testFrame3(Helper outerHelper, int iteration) {213Helper innerHelper = new Helper("foo");214recurse(2, iteration);215Asserts.assertTrue((innerHelper.string != null) && (this != null)216&& (innerHelper != null), String.format("%s : some locals are null", getName()));217Asserts.assertTrue((outerHelper.string != null) && (this != null)218&& (outerHelper != null), String.format("%s : some locals are null", getName()));219}220221private void recurse(int depth, int iteration) {222if (depth == 0) {223check(iteration);224} else {225Integer s = new Integer(depth);226recurse(depth - 1, iteration);227Asserts.assertEQ(s.intValue(), depth,228String.format("different values: %s != %s", s.intValue(), depth));229}230}231232private void checkStructure(boolean materialize) {233boolean[] framesSeen = new boolean[2];234Object[] helpers = new Object[1];235CompilerToVMHelper.iterateFrames(236new ResolvedJavaMethod[] {FRAME3_RESOLVED},237null, /* any */2380,239f -> {240if (!framesSeen[1]) {241Asserts.assertTrue(f.isMethod(FRAME3_RESOLVED),242"Expected testFrame3 first");243framesSeen[1] = true;244Asserts.assertTrue(f.getLocal(0) != null, "this should not be null");245Asserts.assertTrue(f.getLocal(1) != null, "outerHelper should not be null");246Asserts.assertTrue(f.getLocal(3) != null, "innerHelper should not be null");247Asserts.assertEQ(((Helper) f.getLocal(3)).string, "foo", "innerHelper.string should be foo");248helpers[0] = f.getLocal(1);249if (materialize) {250f.materializeVirtualObjects(false);251}252return null; //continue253} else {254Asserts.assertFalse(framesSeen[0], "frame3 can not have been seen");255Asserts.assertTrue(f.isMethod(FRAME2_RESOLVED),256"Expected testFrame2 second");257framesSeen[0] = true;258Asserts.assertTrue(f.getLocal(0) != null, "this should not be null");259Asserts.assertTrue(f.getLocal(1) != null, "str should not be null");260Asserts.assertTrue(f.getLocal(3) != null, "helper should not be null");261Asserts.assertTrue(f.getLocal(4) != null, "helper2 should not be null");262Asserts.assertEQ(((Helper) f.getLocal(3)).string, f.getLocal(1), "helper.string should be the same as str");263Asserts.assertEQ(((Helper) f.getLocal(4)).string, "bar", "helper2.string should be foo");264if (!materialize) {265Asserts.assertEQ(f.getLocal(3), helpers[0], "helper should be the same as frame3's outerHelper");266}267return f; // stop268}269});270Asserts.assertTrue(framesSeen[1], "frame3 should have been seen");271Asserts.assertTrue(framesSeen[0], "frame2 should have been seen");272}273274private void check(int iteration) {275// Materialize virtual objects on last invocation276if (iteration == COMPILE_THRESHOLD) {277// get frames and check not-null278HotSpotStackFrameReference materialized = CompilerToVMHelper.iterateFrames(279new ResolvedJavaMethod[] {MATERIALIZED_RESOLVED},280null /* any */,2810,282f -> (HotSpotStackFrameReference) f);283Asserts.assertNotNull(materialized, getName()284+ " : got null frame for materialized method");285Asserts.assertTrue(materialized.isMethod(MATERIALIZED_RESOLVED),286"Expected materialized method but got " + materialized);287InspectedFrame notMaterialized = CompilerToVMHelper.iterateFrames(288new ResolvedJavaMethod[] {NOT_MATERIALIZED_RESOLVED},289null /* any */,2900,291f -> f);292Asserts.assertNE(materialized, notMaterialized,293"Got same frame pointer for both tested frames");294Asserts.assertTrue(notMaterialized.isMethod(NOT_MATERIALIZED_RESOLVED),295"Expected notMaterialized method but got " + notMaterialized);296Asserts.assertNotNull(notMaterialized, getName()297+ " : got null frame for not materialized method");298Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()299+ " : materialized method not compiled");300Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),301getName() + " : not materialized method not compiled");302// check that frames has virtual objects before materialization stage303Asserts.assertTrue(materialized.hasVirtualObjects(), getName()304+ ": materialized frame has no virtual object before materialization");305Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()306+ ": notMaterialized frame has no virtual object before materialization");307// materialize308CompilerToVMHelper.materializeVirtualObjects(materialized, INVALIDATE);309// check that only not materialized frame has virtual objects310Asserts.assertFalse(materialized.hasVirtualObjects(), getName()311+ " : materialized has virtual object after materialization");312Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()313+ " : notMaterialized has no virtual object after materialization");314// check that materialized frame was deoptimized in case invalidate=true315Asserts.assertEQ(WB.isMethodCompiled(MATERIALIZED_METHOD), !INVALIDATE, getName()316+ " : materialized method has unexpected compiled status");317// check that not materialized frame wasn't deoptimized318Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD), getName()319+ " : not materialized method has unexpected compiled status");320} else if (iteration == COMPILE_THRESHOLD + 1) {321checkStructure(false);322checkStructure(true);323}324}325326private class Helper {327public String string;328329public Helper(String s) {330this.string = s;331}332}333}334335336