1 /*
   2  * Copyright (c) 2015, 2016, 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 vm.jvmci
  28  *         & (vm.compMode != "Xcomp" | vm.opt.TieredCompilation == null | vm.opt.TieredCompilation == true)
  29  * @summary no "-Xcomp -XX:-TieredCompilation" combination allowed until JDK-8140018 is resolved
  30  * @library / /test/lib
  31  * @library ../common/patches
  32  * @modules java.base/jdk.internal.misc
  33  * @modules java.base/jdk.internal.org.objectweb.asm
  34  *          java.base/jdk.internal.org.objectweb.asm.tree
  35  *          jdk.internal.vm.ci/jdk.vm.ci.hotspot
  36  *          jdk.internal.vm.ci/jdk.vm.ci.code
  37  *          jdk.internal.vm.ci/jdk.vm.ci.meta
  38  *
  39  * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
  40  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  41  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  42  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
  43  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  44  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  45  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
  46  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
  47  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
  48  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
  49  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
  50  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true
  51  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
  52  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  53  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
  54  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  55  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  56  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
  57  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
  58  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
  59  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
  60  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
  61  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false
  62  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
  63  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  64  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
  65  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  66  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  67  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
  68  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
  69  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
  70  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
  71  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
  72  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true
  73  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
  74  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  75  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
  76  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  77  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  78  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
  79  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
  80  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
  81  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
  82  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
  83  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false
  84  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
  85  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  86  */
  87 
  88 package compiler.jvmci.compilerToVM;
  89 
  90 import compiler.jvmci.common.CTVMUtilities;
  91 import compiler.testlibrary.CompilerUtils;
  92 import compiler.whitebox.CompilerWhiteBoxTest;
  93 import jdk.test.lib.Asserts;
  94 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  95 import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
  96 import jdk.vm.ci.meta.ResolvedJavaMethod;
  97 import sun.hotspot.WhiteBox;
  98 
  99 import java.lang.reflect.Method;
 100 
 101 public class MaterializeVirtualObjectTest {
 102     private static final WhiteBox WB;
 103     private static final boolean INVALIDATE;
 104     private static final int COMPILE_THRESHOLD;
 105     private static final Method MATERIALIZED_METHOD;
 106     private static final Method NOT_MATERIALIZED_METHOD;
 107     private static final ResolvedJavaMethod MATERIALIZED_RESOLVED;
 108     private static final ResolvedJavaMethod NOT_MATERIALIZED_RESOLVED;
 109     private static final boolean MATERIALIZE_FIRST;
 110 
 111     static {
 112         Method method1;
 113         Method method2;
 114         WB = WhiteBox.getWhiteBox();
 115         try {
 116             method1 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame",
 117                     String.class, int.class);
 118             method2 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame2",
 119                     String.class, int.class);
 120         } catch (NoSuchMethodException e) {
 121             throw new Error("Can't get executable for test method", e);
 122         }
 123         ResolvedJavaMethod resolved1;
 124         ResolvedJavaMethod resolved2;
 125         resolved1 = CTVMUtilities.getResolvedMethod(method1);
 126         resolved2 = CTVMUtilities.getResolvedMethod(method2);
 127         INVALIDATE = Boolean.getBoolean(
 128                 "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
 129         COMPILE_THRESHOLD = WB.getBooleanVMFlag("TieredCompilation")
 130                 ? CompilerWhiteBoxTest.THRESHOLD
 131                 : CompilerWhiteBoxTest.THRESHOLD * 2;
 132         MATERIALIZE_FIRST = Boolean.getBoolean(
 133                 "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst");
 134         MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? resolved1 : resolved2;
 135         NOT_MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? resolved2 : resolved1;
 136         MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method1 : method2;
 137         NOT_MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method2 : method1;
 138     }
 139 
 140     public static void main(String[] args) {
 141         int levels[] = CompilerUtils.getAvailableCompilationLevels();
 142         // we need compilation level 4 to use EscapeAnalysis
 143         if (levels.length < 1 || levels[levels.length - 1] != 4) {
 144             System.out.println("INFO: Test needs compilation level 4 to"
 145                     + " be available. Skipping.");
 146         } else {
 147             new MaterializeVirtualObjectTest().test();
 148         }
 149     }
 150 
 151     private static String getName() {
 152         return "CASE: invalidate=" + INVALIDATE + ", materializedMethod="
 153                 + (MATERIALIZE_FIRST ? "testFrame" : "testFrame2")
 154                 + ", notMaterializedMethod="
 155                 + (MATERIALIZE_FIRST ? "testFrame2" : "testFrame");
 156     }
 157 
 158     private void test() {
 159         System.out.println(getName());
 160         Asserts.assertFalse(WB.isMethodCompiled(MATERIALIZED_METHOD),
 161                 getName() + " : materialized method is compiled");
 162         Asserts.assertFalse(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
 163                 getName() + " : not materialized method is compiled");
 164         for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {
 165             testFrame("someString", i);
 166         }
 167         Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()
 168                 + " : materialized method not compiled");
 169         Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
 170                 getName() + " : not materialized method not compiled");
 171         testFrame("someString", /* materialize */ CompilerWhiteBoxTest.THRESHOLD);
 172     }
 173 
 174     private void testFrame(String str, int iteration) {
 175         Helper helper = new Helper(str);
 176         testFrame2(str, iteration);
 177         Asserts.assertTrue((helper.string != null) && (this != null)
 178                 && (helper != null), String.format("%s : some locals are null", getName()));
 179      }
 180 
 181     private void testFrame2(String str, int iteration) {
 182         Helper helper = new Helper(str);
 183         recurse(2, iteration);
 184         Asserts.assertTrue((helper.string != null) && (this != null)
 185                 && (helper != null), String.format("%s : some locals are null", getName()));
 186     }
 187 
 188     private void recurse(int depth, int iteration) {
 189         if (depth == 0) {
 190             check(iteration);
 191         } else {
 192             Integer s = new Integer(depth);
 193             recurse(depth - 1, iteration);
 194             Asserts.assertEQ(s.intValue(), depth,
 195                     String.format("different values: %s != %s", s.intValue(), depth));
 196         }
 197     }
 198 
 199     private void check(int iteration) {
 200         // Materialize virtual objects on last invocation
 201         if (iteration == COMPILE_THRESHOLD) {
 202             // get frames and check not-null
 203             HotSpotStackFrameReference materialized = CompilerToVMHelper.getNextStackFrame(
 204                     /* topmost frame */ null, new ResolvedJavaMethod[]{MATERIALIZED_RESOLVED},
 205                     /* don't skip any */ 0);
 206             Asserts.assertNotNull(materialized, getName()
 207                     + " : got null frame for materialized method");
 208             HotSpotStackFrameReference notMaterialized = CompilerToVMHelper.getNextStackFrame(
 209                     /* topmost frame */ null, new ResolvedJavaMethod[]{NOT_MATERIALIZED_RESOLVED},
 210                     /* don't skip any */ 0);
 211             Asserts.assertNE(materialized, notMaterialized,
 212                     "Got same frame pointer for both tested frames");
 213             Asserts.assertNotNull(notMaterialized, getName()
 214                     + " : got null frame for not materialized method");
 215             // check that frames has virtual objects before materialization stage
 216             Asserts.assertTrue(materialized.hasVirtualObjects(), getName()
 217                     + ": materialized frame has no virtual object before materialization");
 218             Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
 219                     + ": notMaterialized frame has no virtual object before materialization");
 220             // materialize
 221             CompilerToVMHelper.materializeVirtualObjects(materialized, INVALIDATE);
 222             // check that only not materialized frame has virtual objects
 223             Asserts.assertFalse(materialized.hasVirtualObjects(), getName()
 224                     + " : materialized has virtual object after materialization");
 225             Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
 226                     + " : notMaterialized has no virtual object after materialization");
 227             // check that materialized frame was deoptimized in case invalidate=true
 228             Asserts.assertEQ(WB.isMethodCompiled(MATERIALIZED_METHOD), !INVALIDATE, getName()
 229                     + " : materialized method has unexpected compiled status");
 230             // check that not materialized frame wasn't deoptimized
 231             Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD), getName()
 232                     + " : not materialized method has unexpected compiled status");
 233         }
 234     }
 235 
 236     private class Helper {
 237         public String string;
 238 
 239         public Helper(String s) {
 240             this.string = s;
 241         }
 242     }
 243 }