8166836: Elimination of clone's ArrayCopyNode may make compilation fail silently
authorroland
Wed, 28 Sep 2016 11:17:51 +0200
changeset 41699 fcefeb90b468
parent 41698 a3f113541801
child 41700 ba9120f9206b
child 41701 908cd3b6bddc
8166836: Elimination of clone's ArrayCopyNode may make compilation fail silently Reviewed-by: vlivanov
hotspot/src/share/vm/opto/macro.cpp
hotspot/test/compiler/arraycopy/TestEliminatedCloneBadMemEdge.java
--- a/hotspot/src/share/vm/opto/macro.cpp	Thu Sep 29 22:37:05 2016 -0700
+++ b/hotspot/src/share/vm/opto/macro.cpp	Wed Sep 28 11:17:51 2016 +0200
@@ -439,12 +439,6 @@
     Node* adr = _igvn.transform(new AddPNode(base, base, MakeConX(offset)));
     const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset);
     Node* m = ac->in(TypeFunc::Memory);
-    while (m->is_MergeMem()) {
-      m = m->as_MergeMem()->memory_at(C->get_alias_index(adr_type));
-      if (m->is_Proj() && m->in(0)->is_MemBar()) {
-        m = m->in(0)->in(TypeFunc::Memory);
-      }
-    }
     res = LoadNode::make(_igvn, ctl, m, adr, adr_type, type, bt, MemNode::unordered, LoadNode::Pinned);
   } else {
     if (ac->modifies(offset, offset, &_igvn, true)) {
@@ -978,6 +972,17 @@
   return true;
 }
 
+static void disconnect_projections(MultiNode* n, PhaseIterGVN& igvn) {
+  Node* ctl_proj = n->proj_out(TypeFunc::Control);
+  Node* mem_proj = n->proj_out(TypeFunc::Memory);
+  if (ctl_proj != NULL) {
+    igvn.replace_node(ctl_proj, n->in(0));
+  }
+  if (mem_proj != NULL) {
+    igvn.replace_node(mem_proj, n->in(TypeFunc::Memory));
+  }
+}
+
 // Process users of eliminated allocation.
 void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc) {
   Node* res = alloc->result_cast();
@@ -1008,13 +1013,13 @@
             // Disconnect ArrayCopy node
             ArrayCopyNode* ac = n->as_ArrayCopy();
             assert(ac->is_clonebasic(), "unexpected array copy kind");
-            Node* ctl_proj = ac->proj_out(TypeFunc::Control);
-            Node* mem_proj = ac->proj_out(TypeFunc::Memory);
-            if (ctl_proj != NULL) {
-              _igvn.replace_node(ctl_proj, n->in(0));
-            }
-            if (mem_proj != NULL) {
-              _igvn.replace_node(mem_proj, n->in(TypeFunc::Memory));
+            Node* membar_after = ac->proj_out(TypeFunc::Control)->unique_ctrl_out();
+            disconnect_projections(ac, _igvn);
+            assert(alloc->in(0)->is_Proj() && alloc->in(0)->in(0)->Opcode() == Op_MemBarCPUOrder, "mem barrier expected before allocation");
+            Node* membar_before = alloc->in(0)->in(0);
+            disconnect_projections(membar_before->as_MemBar(), _igvn);
+            if (membar_after->is_MemBar()) {
+              disconnect_projections(membar_after->as_MemBar(), _igvn);
             }
           } else {
             eliminate_card_mark(n);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/compiler/arraycopy/TestEliminatedCloneBadMemEdge.java	Wed Sep 28 11:17:51 2016 +0200
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2016, Red Hat, Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8166836
+ * @summary Elimination of clone's ArrayCopyNode may make compilation fail silently
+ * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:CompileCommand=dontinline,TestEliminatedCloneBadMemEdge::not_inlined TestEliminatedCloneBadMemEdge
+ *
+ */
+
+public class TestEliminatedCloneBadMemEdge implements Cloneable {
+
+    int f1;
+    int f2;
+    int f3;
+    int f4;
+    int f5;
+    int f6;
+    int f7;
+    int f8;
+    int f9;
+
+    static void not_inlined() {}
+
+    static void test(TestEliminatedCloneBadMemEdge o1) throws CloneNotSupportedException {
+        TestEliminatedCloneBadMemEdge o2 = (TestEliminatedCloneBadMemEdge)o1.clone();
+        not_inlined();
+        o2.f1 = 0x42;
+    }
+
+    static public void main(String[] args) throws CloneNotSupportedException {
+        TestEliminatedCloneBadMemEdge o1 = new TestEliminatedCloneBadMemEdge();
+        for (int i = 0; i < 20000; i++) {
+            test(o1);
+        }
+    }
+}