Blame SOURCES/jdk8172850-rh1640127-01-register_allocator_crash.patch

9fd28f
9fd28f
# HG changeset patch
9fd28f
# User thartmann
9fd28f
# Date 1539594027 -7200
9fd28f
# Node ID e044997c2edaeae97866394a7f8e2ddebbd41392
9fd28f
# Parent  99212080341058548d449a22d1381e79353ec5b5
9fd28f
8172850: Anti-dependency on membar causes crash in register allocator due to invalid instruction scheduling
9fd28f
Summary: Regression test and additional asserts. The problem is fixed by 8087341.
9fd28f
Reviewed-by: kvn
9fd28f
9fd28f
diff -r 992120803410 -r e044997c2eda src/share/vm/opto/cfgnode.cpp
9fd28f
--- openjdk/hotspot/src/share/vm/opto/cfgnode.cpp	Mon Oct 22 05:26:38 2018 -0400
9fd28f
+++ openjdk/hotspot/src/share/vm/opto/cfgnode.cpp	Mon Oct 15 11:00:27 2018 +0200
9fd28f
@@ -2016,6 +2016,7 @@
9fd28f
   uint ideal_reg = _type->ideal_reg();
9fd28f
   assert( ideal_reg != Node::NotAMachineReg, "invalid type at Phi" );
9fd28f
   if( ideal_reg == 0 ) return RegMask::Empty;
9fd28f
+  assert(ideal_reg != Op_RegFlags, "flags register is not spillable");
9fd28f
   return *(Compile::current()->matcher()->idealreg2spillmask[ideal_reg]);
9fd28f
 }
9fd28f
 
9fd28f
diff -r 992120803410 -r e044997c2eda src/share/vm/opto/coalesce.cpp
9fd28f
--- openjdk/hotspot/src/share/vm/opto/coalesce.cpp	Mon Oct 22 05:26:38 2018 -0400
9fd28f
+++ openjdk/hotspot/src/share/vm/opto/coalesce.cpp	Mon Oct 15 11:00:27 2018 +0200
9fd28f
@@ -292,7 +292,14 @@
9fd28f
               // Copy any flags as well
9fd28f
               _phc.clone_projs(pred, pred->end_idx(), m, copy, _phc._lrg_map);
9fd28f
             } else {
9fd28f
-              const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()];
9fd28f
+              int ireg = m->ideal_reg();
9fd28f
+              if (ireg == 0 || ireg == Op_RegFlags) {
9fd28f
+                assert(false, err_msg("attempted to spill a non-spillable item: %d: %s, ireg = %d",
9fd28f
+                                      m->_idx, m->Name(), ireg));
9fd28f
+                C->record_method_not_compilable("attempted to spill a non-spillable item");
9fd28f
+                return;
9fd28f
+              }
9fd28f
+              const RegMask *rm = C->matcher()->idealreg2spillmask[ireg];
9fd28f
               copy = new (C) MachSpillCopyNode(m, *rm, *rm);
9fd28f
               // Find a good place to insert.  Kinda tricky, use a subroutine
9fd28f
               insert_copy_with_overlap(pred,copy,phi_name,src_name);
9fd28f
@@ -326,7 +333,14 @@
9fd28f
               b->insert_node(copy, l++);
9fd28f
               l += _phc.clone_projs(b, l, m, copy, _phc._lrg_map);
9fd28f
             } else {
9fd28f
-              const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()];
9fd28f
+              int ireg = m->ideal_reg();
9fd28f
+              if (ireg == 0 || ireg == Op_RegFlags) {
9fd28f
+                assert(false, err_msg("attempted to spill a non-spillable item: %d: %s, ireg = %d",
9fd28f
+                                      m->_idx, m->Name(), ireg));
9fd28f
+                C->record_method_not_compilable("attempted to spill a non-spillable item");
9fd28f
+                return;
9fd28f
+              }
9fd28f
+              const RegMask *rm = C->matcher()->idealreg2spillmask[ireg];
9fd28f
               copy = new (C) MachSpillCopyNode(m, *rm, *rm);
9fd28f
               // Insert the copy in the basic block, just before us
9fd28f
               b->insert_node(copy, l++);
9fd28f
@@ -373,7 +387,14 @@
9fd28f
               if( k < b->_num_succs )
9fd28f
                 continue;     // Live out; do not pre-split
9fd28f
               // Split the lrg at this use
9fd28f
-              const RegMask *rm = C->matcher()->idealreg2spillmask[inp->ideal_reg()];
9fd28f
+              int ireg = inp->ideal_reg();
9fd28f
+              if (ireg == 0 || ireg == Op_RegFlags) {
9fd28f
+                assert(false, err_msg("attempted to spill a non-spillable item: %d: %s, ireg = %d",
9fd28f
+                                      inp->_idx, inp->Name(), ireg));
9fd28f
+                C->record_method_not_compilable("attempted to spill a non-spillable item");
9fd28f
+                return;
9fd28f
+              }
9fd28f
+              const RegMask *rm = C->matcher()->idealreg2spillmask[ireg];
9fd28f
               Node *copy = new (C) MachSpillCopyNode( inp, *rm, *rm );
9fd28f
               // Insert the copy in the use-def chain
9fd28f
               n->set_req(inpidx, copy );
9fd28f
diff -r 992120803410 -r e044997c2eda src/share/vm/opto/machnode.cpp
9fd28f
--- openjdk/hotspot/src/share/vm/opto/machnode.cpp	Mon Oct 22 05:26:38 2018 -0400
9fd28f
+++ openjdk/hotspot/src/share/vm/opto/machnode.cpp	Mon Oct 15 11:00:27 2018 +0200
9fd28f
@@ -619,6 +619,7 @@
9fd28f
   }
9fd28f
 
9fd28f
   // Values outside the domain represent debug info
9fd28f
+  assert(in(idx)->ideal_reg() != Op_RegFlags, "flags register is not spillable");
9fd28f
   return *Compile::current()->matcher()->idealreg2spillmask[in(idx)->ideal_reg()];
9fd28f
 }
9fd28f
 
9fd28f
diff -r 992120803410 -r e044997c2eda src/share/vm/opto/matcher.cpp
9fd28f
--- openjdk/hotspot/src/share/vm/opto/matcher.cpp	Mon Oct 22 05:26:38 2018 -0400
9fd28f
+++ openjdk/hotspot/src/share/vm/opto/matcher.cpp	Mon Oct 15 11:00:27 2018 +0200
9fd28f
@@ -95,6 +95,7 @@
9fd28f
   idealreg2spillmask  [Op_VecD] = NULL;
9fd28f
   idealreg2spillmask  [Op_VecX] = NULL;
9fd28f
   idealreg2spillmask  [Op_VecY] = NULL;
9fd28f
+  idealreg2spillmask  [Op_RegFlags] = NULL;
9fd28f
 
9fd28f
   idealreg2debugmask  [Op_RegI] = NULL;
9fd28f
   idealreg2debugmask  [Op_RegN] = NULL;
9fd28f
@@ -106,6 +107,7 @@
9fd28f
   idealreg2debugmask  [Op_VecD] = NULL;
9fd28f
   idealreg2debugmask  [Op_VecX] = NULL;
9fd28f
   idealreg2debugmask  [Op_VecY] = NULL;
9fd28f
+  idealreg2debugmask  [Op_RegFlags] = NULL;
9fd28f
 
9fd28f
   idealreg2mhdebugmask[Op_RegI] = NULL;
9fd28f
   idealreg2mhdebugmask[Op_RegN] = NULL;
9fd28f
@@ -117,6 +119,7 @@
9fd28f
   idealreg2mhdebugmask[Op_VecD] = NULL;
9fd28f
   idealreg2mhdebugmask[Op_VecX] = NULL;
9fd28f
   idealreg2mhdebugmask[Op_VecY] = NULL;
9fd28f
+  idealreg2mhdebugmask[Op_RegFlags] = NULL;
9fd28f
 
9fd28f
   debug_only(_mem_node = NULL;)   // Ideal memory node consumed by mach node
9fd28f
 }
9fd28f
diff -r 992120803410 -r e044997c2eda test/compiler/gcbarriers/TestMembarDependencies.java
9fd28f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
9fd28f
+++ openjdk/hotspot/test/compiler/gcbarriers/TestMembarDependencies.java	Mon Oct 15 11:00:27 2018 +0200
9fd28f
@@ -0,0 +1,98 @@
9fd28f
+/*
9fd28f
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
9fd28f
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fd28f
+ *
9fd28f
+ * This code is free software; you can redistribute it and/or modify it
9fd28f
+ * under the terms of the GNU General Public License version 2 only, as
9fd28f
+ * published by the Free Software Foundation.
9fd28f
+ *
9fd28f
+ * This code is distributed in the hope that it will be useful, but WITHOUT
9fd28f
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fd28f
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fd28f
+ * version 2 for more details (a copy is included in the LICENSE file that
9fd28f
+ * accompanied this code).
9fd28f
+ *
9fd28f
+ * You should have received a copy of the GNU General Public License version
9fd28f
+ * 2 along with this work; if not, write to the Free Software Foundation,
9fd28f
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fd28f
+ *
9fd28f
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fd28f
+ * or visit www.oracle.com if you need additional information or have any
9fd28f
+ * questions.
9fd28f
+ */
9fd28f
+
9fd28f
+/*
9fd28f
+ * @test TestMembarDependencies
9fd28f
+ * @bug 8172850
9fd28f
+ * @summary Tests correct scheduling of memory loads around MembarVolatile emitted by GC barriers.
9fd28f
+ * @library /testlibrary
9fd28f
+ * @run driver compiler.membars.TestMembarDependencies
9fd28f
+ */
9fd28f
+
9fd28f
+package compiler.membars;
9fd28f
+
9fd28f
+import com.oracle.java.testlibrary.*;
9fd28f
+
9fd28f
+public class TestMembarDependencies {
9fd28f
+    private static TestMembarDependencies f1;
9fd28f
+    private static TestMembarDependencies f2;
9fd28f
+
9fd28f
+    public static void main(String args[]) throws Throwable {
9fd28f
+        if (args.length == 0) {
9fd28f
+            // For debugging, add "-XX:+TraceOptoPipelining"
9fd28f
+            OutputAnalyzer oa = ProcessTools.executeTestJvm("-XX:+IgnoreUnrecognizedVMOptions",
9fd28f
+                "-XX:-TieredCompilation", "-XX:-BackgroundCompilation", "-XX:+PrintOpto",
9fd28f
+                "-XX:CompileCommand=compileonly,compiler.membars.TestMembarDependencies::test*",
9fd28f
+                "-XX:CompileCommand=dontinline,compiler.membars.TestMembarDependencies::test_m1",
9fd28f
+                TestMembarDependencies.class.getName(), "run");
9fd28f
+            // C2 should not crash or bail out from compilation
9fd28f
+            oa.shouldHaveExitValue(0);
9fd28f
+            oa.shouldNotMatch("Bailout: Recompile without subsuming loads");
9fd28f
+            System.out.println(oa.getOutput());
9fd28f
+        } else {
9fd28f
+            f2 = new TestMembarDependencies();
9fd28f
+            // Trigger compilation of test1 and test2
9fd28f
+            for (int i = 0; i < 10_000; ++i) {
9fd28f
+              f2.test1(f2);
9fd28f
+              f2.test2(f2);
9fd28f
+            }
9fd28f
+        }
9fd28f
+    }
9fd28f
+
9fd28f
+    public void test_m1() { }
9fd28f
+    public void test_m2() { }
9fd28f
+
9fd28f
+    public void test1(TestMembarDependencies obj) {
9fd28f
+        // Try/catch/finally is used to create a CFG block without a test + jmpCon
9fd28f
+        // allowing GCM to schedule the testN_mem_reg0 instruction into that block.
9fd28f
+        try {
9fd28f
+            // Method call defines memory state that is then
9fd28f
+            // used by subsequent instructions/blocks (see below).
9fd28f
+            test_m1();
9fd28f
+        } catch (Exception e) {
9fd28f
+
9fd28f
+        } finally {
9fd28f
+            // Oop write to field emits a GC post-barrier with a MembarVolatile
9fd28f
+            // which has a wide memory effect (kills all memory). This creates an
9fd28f
+            // anti-dependency on all surrounding memory loads.
9fd28f
+            f1 = obj;
9fd28f
+        }
9fd28f
+        // The empty method m2 is inlined but the null check of f2 remains. It is encoded
9fd28f
+        // as CmpN(LoadN(MEM), NULL) where MEM is the memory after the call to test_m1().
9fd28f
+        // This is matched to testN_mem_reg0 on x86 which is scheduled before the barrier
9fd28f
+        // in the try/catch block due to the anti-dependency on the MembarVolatile.
9fd28f
+        // C2 crashes in the register allocator when trying to spill the flag register
9fd28f
+        // to keep the result of the testN instruction live from the try/catch block
9fd28f
+        // until it is here.
9fd28f
+        f2.test_m2();
9fd28f
+    }
9fd28f
+
9fd28f
+    public void test2(TestMembarDependencies obj) {
9fd28f
+        // Same as test1 but without try/catch/finally.
9fd28f
+        // This causes C2 to bail out in block local scheduling because testN_mem_reg0 is
9fd28f
+        // scheduled into a block that already contains another test + jmpCon instruction.
9fd28f
+        test_m1();
9fd28f
+        f1 = obj;
9fd28f
+        f2.test_m2();
9fd28f
+    }
9fd28f
+}
9fd28f