1 /*
   2  * Copyright (c) 2015, 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  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.org.objectweb.asm
  31  *          java.base/jdk.internal.org.objectweb.asm.tree
  32  *          jdk.vm.ci/jdk.vm.ci.hotspot
  33  *          jdk.vm.ci/jdk.vm.ci.code
  34  *          jdk.vm.ci/jdk.vm.ci.meta
  35  * @ignore 8139703
  36  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  37  * @build compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  38  * @build sun.hotspot.WhiteBox
  39  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  40  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  41  * @run main/othervm -Xbootclasspath/a:.
  42  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  43  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  44  *                   -XX:CompileCommand=exclude,*::check
  45  *                   -XX:+DoEscapeAnalysis
  46  *                   -Xbatch
  47  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
  48  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  49  * @run main/othervm -Xbootclasspath/a:.
  50  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  51  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  52  *                   -XX:CompileCommand=exclude,*::check
  53  *                   -XX:+DoEscapeAnalysis
  54  *                   -Xbatch
  55  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
  56  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  57  */
  58 
  59 package compiler.jvmci.compilerToVM;
  60 
  61 import java.lang.reflect.Method;
  62 import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
  63 import jdk.vm.ci.meta.ResolvedJavaMethod;
  64 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  65 import jdk.test.lib.Asserts;
  66 
  67 import compiler.jvmci.common.CTVMUtilities;
  68 import compiler.testlibrary.CompilerUtils;
  69 
  70 import sun.hotspot.WhiteBox;
  71 
  72 public class MaterializeVirtualObjectTest {
  73     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  74     private static final Method METHOD;
  75     private static final ResolvedJavaMethod RESOLVED_METHOD;
  76     private static final boolean INVALIDATE = Boolean.getBoolean(
  77             "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
  78 
  79     static {
  80         try {
  81             METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod(
  82                     "testFrame", String.class, boolean.class);
  83         } catch (NoSuchMethodException e) {
  84             throw new Error("Can't get executable for test method", e);
  85         }
  86         RESOLVED_METHOD = CTVMUtilities.getResolvedMethod(METHOD);
  87     }
  88 
  89     public static void main(String[] args) {
  90         int levels[] = CompilerUtils.getAvailableCompilationLevels();
  91         // we need compilation level 4 to use EscapeAnalysis
  92         if (levels.length < 1 || levels[levels.length - 1] != 4) {
  93             System.out.println("INFO: Test needs compilation level 4 to"
  94                     + " be available. Skipping.");
  95         } else {
  96             new MaterializeVirtualObjectTest().test();
  97         }
  98     }
  99 
 100     private static String getName() {
 101         return "CASE: invalidate=" + INVALIDATE;
 102     }
 103 
 104     private void test() {
 105         System.out.println(getName());
 106         Asserts.assertFalse(WB.isMethodCompiled(METHOD), getName()
 107                 + " : method unexpectedly compiled");
 108         /* need to call testFrame at least once to be able to compile it, so
 109            calling with materialize=false, because testFrame is not compiled */
 110         testFrame("someString", /* materialize= */ false);
 111         WB.enqueueMethodForCompilation(METHOD, 4);
 112         Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
 113                 + "Method unexpectedly not compiled");
 114         // calling with materialize=true to materialize compiled testFrame
 115         testFrame("someString", /* materialize= */ true);
 116     }
 117 
 118     private void testFrame(String str, boolean materialize) {
 119         Helper helper = new Helper(str);
 120         check(materialize);
 121         Asserts.assertTrue((helper.string != null) && (this != null)
 122                 && (helper != null), getName() + " : some locals are null");
 123     }
 124 
 125     private void check(boolean materialize) {
 126         // Materialize virtual objects on last invocation
 127         if (materialize) {
 128             HotSpotStackFrameReference hsFrame = CompilerToVMHelper
 129                     .getNextStackFrame(/* topmost frame */ null,
 130                             new ResolvedJavaMethod[]{
 131                                 RESOLVED_METHOD}, /* don't skip any */ 0);
 132             Asserts.assertNotNull(hsFrame, getName() + " : got null frame");
 133             Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
 134                     + "Test method should be compiled");
 135             Asserts.assertTrue(hsFrame.hasVirtualObjects(), getName()
 136                     + ": has no virtual object before materialization");
 137             CompilerToVMHelper.materializeVirtualObjects(hsFrame, INVALIDATE);
 138             Asserts.assertFalse(hsFrame.hasVirtualObjects(), getName()
 139                     + " : has virtual object after materialization");
 140             Asserts.assertEQ(WB.isMethodCompiled(METHOD), !INVALIDATE, getName()
 141                     + " : unexpected compiled status");
 142         }
 143     }
 144 
 145     private class Helper {
 146         public String string;
 147 
 148         public Helper(String s) {
 149             this.string = s;
 150         }
 151     }
 152 }