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