1 /*
2  * Copyright (c) 2017, 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 TestMembarDependencies
26  * @bug 8172850
27  * @summary Tests correct scheduling of memory loads around MembarVolatile emitted by GC barriers.
28  * @library /testlibrary
29  * @run driver compiler.membars.TestMembarDependencies
30  */
31 
32 package compiler.membars;
33 
34 import com.oracle.java.testlibrary.*;
35 
36 public class TestMembarDependencies {
37     private static TestMembarDependencies f1;
38     private static TestMembarDependencies f2;
39 
main(String args[])40     public static void main(String args[]) throws Throwable {
41         if (args.length == 0) {
42             // For debugging, add "-XX:+TraceOptoPipelining"
43             OutputAnalyzer oa = ProcessTools.executeTestJvm("-XX:+IgnoreUnrecognizedVMOptions",
44                 "-XX:-TieredCompilation", "-XX:-BackgroundCompilation", "-XX:+PrintOpto",
45                 "-XX:CompileCommand=compileonly,compiler.membars.TestMembarDependencies::test*",
46                 "-XX:CompileCommand=dontinline,compiler.membars.TestMembarDependencies::test_m1",
47                 TestMembarDependencies.class.getName(), "run");
48             // C2 should not crash or bail out from compilation
49             oa.shouldHaveExitValue(0);
50             oa.shouldNotMatch("Bailout: Recompile without subsuming loads");
51             System.out.println(oa.getOutput());
52         } else {
53             f2 = new TestMembarDependencies();
54             // Trigger compilation of test1 and test2
55             for (int i = 0; i < 10_000; ++i) {
56               f2.test1(f2);
57               f2.test2(f2);
58             }
59         }
60     }
61 
test_m1()62     public void test_m1() { }
test_m2()63     public void test_m2() { }
64 
test1(TestMembarDependencies obj)65     public void test1(TestMembarDependencies obj) {
66         // Try/catch/finally is used to create a CFG block without a test + jmpCon
67         // allowing GCM to schedule the testN_mem_reg0 instruction into that block.
68         try {
69             // Method call defines memory state that is then
70             // used by subsequent instructions/blocks (see below).
71             test_m1();
72         } catch (Exception e) {
73 
74         } finally {
75             // Oop write to field emits a GC post-barrier with a MembarVolatile
76             // which has a wide memory effect (kills all memory). This creates an
77             // anti-dependency on all surrounding memory loads.
78             f1 = obj;
79         }
80         // The empty method m2 is inlined but the null check of f2 remains. It is encoded
81         // as CmpN(LoadN(MEM), NULL) where MEM is the memory after the call to test_m1().
82         // This is matched to testN_mem_reg0 on x86 which is scheduled before the barrier
83         // in the try/catch block due to the anti-dependency on the MembarVolatile.
84         // C2 crashes in the register allocator when trying to spill the flag register
85         // to keep the result of the testN instruction live from the try/catch block
86         // until it is here.
87         f2.test_m2();
88     }
89 
test2(TestMembarDependencies obj)90     public void test2(TestMembarDependencies obj) {
91         // Same as test1 but without try/catch/finally.
92         // This causes C2 to bail out in block local scheduling because testN_mem_reg0 is
93         // scheduled into a block that already contains another test + jmpCon instruction.
94         test_m1();
95         f1 = obj;
96         f2.test_m2();
97     }
98 }
99