changeset 8531:0d91be9af2bc

Write Barrier refactoring
author Christos Kotselidis <christos.kotselidis@oracle.com>
date Wed, 27 Mar 2013 17:03:01 +0100
parents ebf95eed9eef
children 1f958e760261
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/ArrayWriteBarrier.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/FieldWriteBarrier.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/G1WriteBarrierPost.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/G1WriteBarrierPre.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/SerialWriteBarrierPost.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/WriteBarrierPost.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/WriteBarrierPre.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ArrayCopySnippets.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/WriteBarrierSnippets.java
diffstat 10 files changed, 291 insertions(+), 236 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java	Wed Mar 27 14:03:03 2013 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java	Wed Mar 27 17:03:01 2013 +0100
@@ -611,18 +611,12 @@
             FixedWithNextNode first = memoryWrite;
 
             if (field.getKind() == Kind.Object && !memoryWrite.value().objectStamp().alwaysNull()) {
-                if (config.useG1GC) {
-                    WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(memoryWrite.object(), null, memoryWrite.location(), true));
-                    WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(memoryWrite.object(), memoryWrite.value(), memoryWrite.location(), false));
-                    graph.addBeforeFixed(memoryWrite, writeBarrierPre);
-                    graph.addAfterFixed(memoryWrite, writeBarrierPost);
-                    first = writeBarrierPre;
-                    last = writeBarrierPost;
-                } else {
-                    FieldWriteBarrier writeBarrier = graph.add(new FieldWriteBarrier(memoryWrite.object()));
-                    graph.addAfterFixed(memoryWrite, writeBarrier);
-                    last = writeBarrier;
-                }
+                WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(memoryWrite.object(), null, memoryWrite.location(), true));
+                WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(memoryWrite.object(), memoryWrite.value(), memoryWrite.location(), false));
+                graph.addBeforeFixed(memoryWrite, writeBarrierPre);
+                graph.addAfterFixed(memoryWrite, writeBarrierPost);
+                first = writeBarrierPre;
+                last = writeBarrierPost;
             }
             if (storeField.isVolatile()) {
                 MembarNode preMembar = graph.add(new MembarNode(JMM_PRE_VOLATILE_WRITE));
@@ -637,25 +631,14 @@
             LocationNode location = IndexedLocationNode.create(LocationNode.ANY_LOCATION, cas.expected().kind(), cas.displacement(), cas.offset(), graph, 1);
             if (expected.kind() == Kind.Object && !cas.newValue().objectStamp().alwaysNull()) {
                 ResolvedJavaType type = cas.object().objectStamp().type();
+                WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(cas.object(), null, location, true));
+                graph.addBeforeFixed(cas, writeBarrierPre);
                 if (type != null && !type.isArray() && !MetaUtil.isJavaLangObject(type)) {
-                    // Use a field write barrier since it's not an array store
-                    if (config.useG1GC) {
-                        WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(cas.object(), null, location, true));
-                        WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(cas.object(), cas.newValue(), location, false));
-                        graph.addBeforeFixed(cas, writeBarrierPre);
-                        graph.addAfterFixed(cas, writeBarrierPost);
-                    } else {
-                        graph.addAfterFixed(cas, graph.add(new FieldWriteBarrier(cas.object())));
-                    }
+                    WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(cas.object(), cas.newValue(), location, false));
+                    graph.addAfterFixed(cas, writeBarrierPost);
                 } else {
-                    // This may be an array store so use an array write barrier
-                    if (config.useG1GC) {
-                        WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(cas.object(), null, location, true));
-                        graph.addBeforeFixed(cas, writeBarrierPre);
-                        graph.addAfterFixed(cas, graph.add(new WriteBarrierPost(cas.object(), cas.newValue(), location, true)));
-                    } else {
-                        graph.addAfterFixed(cas, graph.add(new ArrayWriteBarrier(cas.object(), location)));
-                    }
+                    WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(cas.object(), cas.newValue(), location, true));
+                    graph.addAfterFixed(cas, writeBarrierPost);
                 }
             }
         } else if (n instanceof LoadIndexedNode) {
@@ -699,14 +682,10 @@
             graph.replaceFixedWithFixed(storeIndexed, memoryWrite);
 
             if (elementKind == Kind.Object && !value.objectStamp().alwaysNull()) {
-                if (config.useG1GC) {
-                    WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(array, null, arrayLocation, true));
-                    graph.addBeforeFixed(memoryWrite, writeBarrierPre);
-                    WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(array, value, arrayLocation, true));
-                    graph.addAfterFixed(memoryWrite, writeBarrierPost);
-                } else {
-                    graph.addAfterFixed(memoryWrite, graph.add(new ArrayWriteBarrier(array, arrayLocation)));
-                }
+                WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(array, null, arrayLocation, true));
+                graph.addBeforeFixed(memoryWrite, writeBarrierPre);
+                WriteBarrierPost writeBarrierPost = graph.add(new WriteBarrierPost(array, value, arrayLocation, true));
+                graph.addAfterFixed(memoryWrite, writeBarrierPost);
             }
         } else if (n instanceof UnsafeLoadNode) {
             UnsafeLoadNode load = (UnsafeLoadNode) n;
@@ -726,25 +705,12 @@
             graph.replaceFixedWithFixed(store, write);
             if (write.value().kind() == Kind.Object && !write.value().objectStamp().alwaysNull()) {
                 ResolvedJavaType type = object.objectStamp().type();
-                // WriteBarrier writeBarrier;
+                WriteBarrierPre writeBarrierPre = new WriteBarrierPre(object, null, location, true);
+                graph.addBeforeFixed(write, graph.add(writeBarrierPre));
                 if (type != null && !type.isArray() && !MetaUtil.isJavaLangObject(type)) {
-                    // Use a field write barrier since it's not an array store
-                    if (config.useG1GC) {
-                        WriteBarrierPre writeBarrierPre = new WriteBarrierPre(object, null, location, true);
-                        graph.addBeforeFixed(write, graph.add(writeBarrierPre));
-                        graph.addAfterFixed(write, graph.add(new WriteBarrierPost(object, write.value(), location, false)));
-                    } else {
-                        graph.addAfterFixed(write, graph.add(new FieldWriteBarrier(object)));
-                    }
+                    graph.addAfterFixed(write, graph.add(new WriteBarrierPost(object, write.value(), location, false)));
                 } else {
-                    // This may be an array store so use an array write barrier
-                    if (config.useG1GC) {
-                        WriteBarrierPre writeBarrierPre = graph.add(new WriteBarrierPre(object, null, location, true));
-                        graph.addBeforeFixed(write, writeBarrierPre);
-                        graph.addAfterFixed(write, graph.add(new WriteBarrierPost(object, write.value(), location, true)));
-                    } else {
-                        graph.addAfterFixed(write, graph.add(new ArrayWriteBarrier(object, location)));
-                    }
+                    graph.addAfterFixed(write, graph.add(new WriteBarrierPost(object, write.value(), location, true)));
                 }
             }
         } else if (n instanceof LoadHubNode) {
@@ -777,14 +743,12 @@
             monitorSnippets.lower((MonitorEnterNode) n, tool);
         } else if (n instanceof MonitorExitNode) {
             monitorSnippets.lower((MonitorExitNode) n, tool);
-        } else if (n instanceof FieldWriteBarrier) {
-            writeBarrierSnippets.lower((FieldWriteBarrier) n, tool);
-        } else if (n instanceof ArrayWriteBarrier) {
-            writeBarrierSnippets.lower((ArrayWriteBarrier) n, tool);
-        } else if (n instanceof WriteBarrierPre) {
-            writeBarrierSnippets.lower((WriteBarrierPre) n, tool);
-        } else if (n instanceof WriteBarrierPost) {
-            writeBarrierSnippets.lower((WriteBarrierPost) n, tool);
+        } else if (n instanceof SerialWriteBarrierPost) {
+            writeBarrierSnippets.lower((SerialWriteBarrierPost) n, tool);
+        } else if (n instanceof G1WriteBarrierPre) {
+            writeBarrierSnippets.lower((G1WriteBarrierPre) n, tool);
+        } else if (n instanceof G1WriteBarrierPost) {
+            writeBarrierSnippets.lower((G1WriteBarrierPost) n, tool);
         } else if (n instanceof TLABAllocateNode) {
             newObjectSnippets.lower((TLABAllocateNode) n, tool);
         } else if (n instanceof InitializeObjectNode) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/ArrayWriteBarrier.java	Wed Mar 27 14:03:03 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-/*
- * Copyright (c) 2012, Oracle and/or its affiliates. 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.
- */
-package com.oracle.graal.hotspot.nodes;
-
-import com.oracle.graal.nodes.*;
-import com.oracle.graal.nodes.extended.*;
-import com.oracle.graal.nodes.spi.*;
-import com.oracle.graal.nodes.type.*;
-
-public final class ArrayWriteBarrier extends FixedWithNextNode implements Lowerable {
-
-    @Input private ValueNode object;
-    @Input private LocationNode location;
-
-    public ValueNode getObject() {
-        return object;
-    }
-
-    public LocationNode getLocation() {
-        return location;
-    }
-
-    public ArrayWriteBarrier(ValueNode object, LocationNode location) {
-        super(StampFactory.forVoid());
-        this.object = object;
-        this.location = location;
-    }
-
-    public void lower(LoweringTool generator) {
-        generator.getRuntime().lower(this, generator);
-    }
-}
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/FieldWriteBarrier.java	Wed Mar 27 14:03:03 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2012, Oracle and/or its affiliates. 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.
- */
-package com.oracle.graal.hotspot.nodes;
-
-import com.oracle.graal.nodes.*;
-import com.oracle.graal.nodes.spi.*;
-import com.oracle.graal.nodes.type.*;
-
-public final class FieldWriteBarrier extends FixedWithNextNode implements Lowerable {
-
-    @Input private ValueNode object;
-
-    public ValueNode getObject() {
-        return object;
-    }
-
-    public FieldWriteBarrier(ValueNode object) {
-        super(StampFactory.forVoid());
-        this.object = object;
-    }
-
-    public void lower(LoweringTool generator) {
-        generator.getRuntime().lower(this, generator);
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/G1WriteBarrierPost.java	Wed Mar 27 17:03:01 2013 +0100
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. 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.
+ */
+
+package com.oracle.graal.hotspot.nodes;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.extended.*;
+import com.oracle.graal.nodes.spi.*;
+
+public class G1WriteBarrierPost extends WriteBarrierPost implements Lowerable {
+
+    @Input private ValueNode object;
+    @Input private ValueNode value;
+    @Input private LocationNode location;
+    private final boolean precise;
+
+    @Override
+    public ValueNode getObject() {
+        return object;
+    }
+
+    @Override
+    public ValueNode getValue() {
+        return value;
+    }
+
+    @Override
+    public LocationNode getLocation() {
+        return location;
+    }
+
+    @Override
+    public boolean usePrecise() {
+        return precise;
+    }
+
+    public G1WriteBarrierPost(ValueNode object, ValueNode value, LocationNode location, boolean precise) {
+        this.object = object;
+        this.value = value;
+        this.location = location;
+        this.precise = precise;
+    }
+
+    @Override
+    public void lower(LoweringTool generator) {
+        generator.getRuntime().lower(this, generator);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/G1WriteBarrierPre.java	Wed Mar 27 17:03:01 2013 +0100
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. 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.
+ */
+
+package com.oracle.graal.hotspot.nodes;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.extended.*;
+import com.oracle.graal.nodes.spi.*;
+
+public final class G1WriteBarrierPre extends WriteBarrierPre implements Lowerable {
+
+    @Input private ValueNode object;
+    @Input private LocationNode location;
+    @Input private ValueNode expectedObject;
+    private final boolean doLoad;
+
+    @Override
+    public ValueNode getObject() {
+        return object;
+    }
+
+    @Override
+    public ValueNode getExpectedObject() {
+        return expectedObject;
+    }
+
+    @Override
+    public boolean doLoad() {
+        return doLoad;
+    }
+
+    @Override
+    public LocationNode getLocation() {
+        return location;
+    }
+
+    public G1WriteBarrierPre(ValueNode object, ValueNode expectedObject, LocationNode location, boolean doLoad) {
+        this.object = object;
+        this.doLoad = doLoad;
+        this.location = location;
+        this.expectedObject = expectedObject;
+    }
+
+    public void lower(LoweringTool generator) {
+        generator.getRuntime().lower(this, generator);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/SerialWriteBarrierPost.java	Wed Mar 27 17:03:01 2013 +0100
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. 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.
+ */
+
+package com.oracle.graal.hotspot.nodes;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.extended.*;
+import com.oracle.graal.nodes.spi.*;
+
+public class SerialWriteBarrierPost extends WriteBarrierPost implements Lowerable {
+
+    @Input private ValueNode object;
+    @Input private LocationNode location;
+    private final boolean usePrecise;
+
+    public SerialWriteBarrierPost(ValueNode object, LocationNode location, boolean usePrecise) {
+        this.object = object;
+        this.location = location;
+        this.usePrecise = usePrecise;
+    }
+
+    @Override
+    public ValueNode getObject() {
+        return object;
+    }
+
+    @Override
+    public LocationNode getLocation() {
+        return location;
+    }
+
+    @Override
+    public boolean usePrecise() {
+        return usePrecise;
+    }
+
+    @Override
+    public void lower(LoweringTool generator) {
+        generator.getRuntime().lower(this, generator);
+    }
+
+}
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/WriteBarrierPost.java	Wed Mar 27 14:03:03 2013 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/WriteBarrierPost.java	Wed Mar 27 17:03:01 2013 +0100
@@ -23,18 +23,18 @@
 package com.oracle.graal.hotspot.nodes;
 
 import static com.oracle.graal.hotspot.replacements.HotSpotSnippetUtils.*;
+
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.extended.*;
 import com.oracle.graal.nodes.spi.*;
-import com.oracle.graal.nodes.type.*;
 
-public final class WriteBarrierPost extends FixedWithNextNode implements Lowerable {
+public class WriteBarrierPost extends WriteBarrier implements Lowerable {
 
     @Input private ValueNode object;
     @Input private ValueNode value;
+    @Input private ValueNode index;
     @Input private LocationNode location;
-    @Input private ValueNode length;
     private final boolean precise;
 
     public ValueNode getObject() {
@@ -45,10 +45,6 @@
         return value;
     }
 
-    public ValueNode getLength() {
-        return length;
-    }
-
     public LocationNode getLocation() {
         return location;
     }
@@ -57,39 +53,45 @@
         return precise;
     }
 
+    public WriteBarrierPost() {
+        this.precise = false;
+    }
+
+    public WriteBarrierPost(ValueNode object, ValueNode value, ValueNode index) {
+        this.object = object;
+        this.value = value;
+        this.index = index;
+        this.precise = true;
+        this.location = null;
+    }
+
     public WriteBarrierPost(ValueNode object, ValueNode value, LocationNode location, boolean precise) {
-        super(StampFactory.forVoid());
         this.object = object;
         this.value = value;
         this.location = location;
         this.precise = precise;
-        this.length = null;
-
-    }
-
-    public WriteBarrierPost(ValueNode array, ValueNode value, ValueNode index, ValueNode length) {
-        super(StampFactory.forVoid());
-        this.object = array;
-        this.location = IndexedLocationNode.create(LocationNode.getArrayLocation(Kind.Object), Kind.Object, arrayBaseOffset(Kind.Object), index, array.graph(), arrayIndexScale(Kind.Object));
-        this.length = length;
-        this.value = value;
-        this.precise = true;
     }
 
     @Override
     public void lower(LoweringTool generator) {
-        if (getLength() == null) {
-            generator.getRuntime().lower(this, generator);
-        } else {
-            StructuredGraph graph = (StructuredGraph) this.graph();
+        StructuredGraph graph = (StructuredGraph) this.graph();
+        if (location == null) { // Come from array copy intrinsic
+            LocationNode arrayLocation = IndexedLocationNode.create(LocationNode.getArrayLocation(Kind.Object), Kind.Object, arrayBaseOffset(Kind.Object), index, graph, arrayIndexScale(Kind.Object));
             if (useG1GC()) {
-                graph.replaceFixedWithFixed(this, graph().add(new WriteBarrierPost(getObject(), getValue(), getLocation(), usePrecise())));
+                graph.replaceFixedWithFixed(this, graph().add(new G1WriteBarrierPost(object, value, arrayLocation, true)));
             } else {
-                graph.replaceFixedWithFixed(this, graph().add(new ArrayWriteBarrier(getObject(), getLocation())));
+                graph.replaceFixedWithFixed(this, graph().add(new SerialWriteBarrierPost(object, arrayLocation, true)));
+            }
+        } else { // Normal WriteBarrier
+            assert location != null;
+            if (useG1GC()) {
+                graph.replaceFixedWithFixed(this, graph().add(new G1WriteBarrierPost(object, value, location, precise)));
+            } else {
+                graph.replaceFixedWithFixed(this, graph().add(new SerialWriteBarrierPost(object, location, precise)));
             }
         }
     }
 
     @NodeIntrinsic
-    public static native void arrayCopyWriteBarrier(Object array, Object value, int index, int length);
+    public static native void arrayCopyWriteBarrier(Object array, Object value, int index);
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/WriteBarrierPre.java	Wed Mar 27 14:03:03 2013 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nodes/WriteBarrierPre.java	Wed Mar 27 17:03:01 2013 +0100
@@ -22,12 +22,14 @@
  */
 package com.oracle.graal.hotspot.nodes;
 
+import static com.oracle.graal.hotspot.replacements.HotSpotSnippetUtils.*;
+
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.extended.*;
 import com.oracle.graal.nodes.spi.*;
 import com.oracle.graal.nodes.type.*;
 
-public final class WriteBarrierPre extends FixedWithNextNode implements Lowerable {
+public class WriteBarrierPre extends WriteBarrier implements Lowerable {
 
     @Input private ValueNode object;
     @Input private LocationNode location;
@@ -50,16 +52,25 @@
         return location;
     }
 
+    public WriteBarrierPre() {
+        this.doLoad = false;
+    }
+
     public WriteBarrierPre(ValueNode object, ValueNode expectedObject, LocationNode location, boolean doLoad) {
-        super(StampFactory.forVoid());
         this.object = object;
         this.doLoad = doLoad;
         this.location = location;
         this.expectedObject = expectedObject;
     }
 
+    @Override
     public void lower(LoweringTool generator) {
-        generator.getRuntime().lower(this, generator);
+        StructuredGraph graph = (StructuredGraph) this.graph();
+        if (useG1GC()) {
+            graph.replaceFixedWithFixed(this, graph().add(new G1WriteBarrierPre(getObject(), getExpectedObject(), getLocation(), doLoad())));
+        } else {
+            graph.removeFixed(this);
+        }
     }
 
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ArrayCopySnippets.java	Wed Mar 27 14:03:03 2013 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ArrayCopySnippets.java	Wed Mar 27 17:03:01 2013 +0100
@@ -37,7 +37,7 @@
 import com.oracle.graal.nodes.java.*;
 import com.oracle.graal.phases.*;
 import com.oracle.graal.replacements.*;
-import com.oracle.graal.replacements.Snippet.*;
+import com.oracle.graal.replacements.Snippet.ConstantParameter;
 import com.oracle.graal.replacements.nodes.*;
 import com.oracle.graal.word.*;
 
@@ -250,7 +250,7 @@
             for (long i = start; i >= 0; i -= scale) {
                 Object a = UnsafeLoadNode.load(src, header, i + (long) srcPos * scale, Kind.Object);
                 DirectObjectStoreNode.storeObject(dest, header, i + (long) destPos * scale, a);
-                WriteBarrierPost.arrayCopyWriteBarrier(dest, a, j, length);
+                WriteBarrierPost.arrayCopyWriteBarrier(dest, a, j);
                 j--;
             }
         } else {
@@ -259,7 +259,7 @@
             for (long i = 0; i < end; i += scale) {
                 Object a = UnsafeLoadNode.load(src, header, i + (long) srcPos * scale, Kind.Object);
                 DirectObjectStoreNode.storeObject(dest, header, i + (long) destPos * scale, a);
-                WriteBarrierPost.arrayCopyWriteBarrier(dest, a, j, length);
+                WriteBarrierPost.arrayCopyWriteBarrier(dest, a, j);
                 j++;
             }
         }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/WriteBarrierSnippets.java	Wed Mar 27 14:03:03 2013 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/WriteBarrierSnippets.java	Wed Mar 27 17:03:01 2013 +0100
@@ -40,7 +40,7 @@
 public class WriteBarrierSnippets implements Snippets {
 
     @Snippet
-    public static void g1PreWriteBarrier(@Parameter("object") Object obj, @Parameter("expectedObject") Object expobj, @Parameter("location") Object location,
+    public static void g1WriteBarrierPre(@Parameter("object") Object obj, @Parameter("expectedObject") Object expobj, @Parameter("location") Object location,
                     @ConstantParameter("doLoad") boolean doLoad) {
         Word thread = thread();
         Object object = FixedValueAnchorNode.getObject(obj);
@@ -48,11 +48,9 @@
         Word field = (Word) Word.fromArray(object, location);
         Word previousOop = (Word) Word.fromObject(expectedObject);
         byte markingValue = thread.readByte(HotSpotSnippetUtils.g1SATBQueueMarkingOffset());
-
         Word bufferAddress = thread.readWord(HotSpotSnippetUtils.g1SATBQueueBufferOffset());
         Word indexAddress = thread.add(HotSpotSnippetUtils.g1SATBQueueIndexOffset());
         Word indexValue = indexAddress.readWord(0);
-
         if (markingValue != (byte) 0) {
             if (doLoad) {
                 previousOop = field.readWord(0);
@@ -65,14 +63,13 @@
                     indexAddress.writeWord(0, nextIndex);
                 } else {
                     WriteBarrierPreStubCall.call(previousOop);
-
                 }
             }
         }
     }
 
     @Snippet
-    public static void g1PostWriteBarrier(@Parameter("object") Object obj, @Parameter("value") Object value, @Parameter("location") Object location, @ConstantParameter("usePrecise") boolean usePrecise) {
+    public static void g1WriteBarrierPost(@Parameter("object") Object obj, @Parameter("value") Object value, @Parameter("location") Object location, @ConstantParameter("usePrecise") boolean usePrecise) {
         Word thread = thread();
         Object object = FixedValueAnchorNode.getObject(obj);
         Object wrObject = FixedValueAnchorNode.getObject(value);
@@ -99,7 +96,6 @@
             cardBase = cardBase.add(Word.unsigned(cardTableStart()));
         }
         Word cardAddress = cardBase.add(displacement);
-
         if (xorResult.notEqual(Word.zero())) {
             if (writtenValue.notEqual(Word.zero())) {
                 byte cardByte = cardAddress.readByte(0);
@@ -119,24 +115,14 @@
     }
 
     @Snippet
-    public static void serialFieldWriteBarrier(@Parameter("object") Object obj) {
+    public static void serialWriteBarrierPost(@Parameter("object") Object obj, @Parameter("location") Object location, @ConstantParameter("usePrecise") boolean usePrecise) {
         Object object = FixedValueAnchorNode.getObject(obj);
-        Pointer oop = Word.fromObject(object);
-        Word base = (Word) oop.unsignedShiftRight(cardTableShift());
-        long startAddress = cardTableStart();
-        int displacement = 0;
-        if (((int) startAddress) == startAddress) {
-            displacement = (int) startAddress;
+        Pointer oop;
+        if (usePrecise) {
+            oop = Word.fromArray(object, location);
         } else {
-            base = base.add(Word.unsigned(cardTableStart()));
+            oop = Word.fromObject(object);
         }
-        base.writeByte(displacement, (byte) 0);
-    }
-
-    @Snippet
-    public static void serialArrayWriteBarrier(@Parameter("object") Object obj, @Parameter("location") Object location) {
-        Object object = FixedValueAnchorNode.getObject(obj);
-        Pointer oop = Word.fromArray(object, location);
         Word base = (Word) oop.unsignedShiftRight(cardTableShift());
         long startAddress = cardTableStart();
         int displacement = 0;
@@ -150,40 +136,30 @@
 
     public static class Templates extends AbstractTemplates<WriteBarrierSnippets> {
 
-        private final ResolvedJavaMethod serialFieldWriteBarrier;
-        private final ResolvedJavaMethod serialArrayWriteBarrier;
-        private final ResolvedJavaMethod g1PreWriteBarrier;
-        private final ResolvedJavaMethod g1PostWriteBarrier;
+        private final ResolvedJavaMethod serialWriteBarrierPost;
+        private final ResolvedJavaMethod g1WriteBarrierPre;
+        private final ResolvedJavaMethod g1WriteBarrierPost;
 
         public Templates(CodeCacheProvider runtime, Assumptions assumptions, TargetDescription target) {
             super(runtime, assumptions, target, WriteBarrierSnippets.class);
-            serialFieldWriteBarrier = snippet("serialFieldWriteBarrier", Object.class);
-            serialArrayWriteBarrier = snippet("serialArrayWriteBarrier", Object.class, Object.class);
-            g1PreWriteBarrier = snippet("g1PreWriteBarrier", Object.class, Object.class, Object.class, boolean.class);
-            g1PostWriteBarrier = snippet("g1PostWriteBarrier", Object.class, Object.class, Object.class, boolean.class);
+            serialWriteBarrierPost = snippet("serialWriteBarrierPost", Object.class, Object.class, boolean.class);
+            g1WriteBarrierPre = snippet("g1WriteBarrierPre", Object.class, Object.class, Object.class, boolean.class);
+            g1WriteBarrierPost = snippet("g1WriteBarrierPost", Object.class, Object.class, Object.class, boolean.class);
         }
 
-        public void lower(ArrayWriteBarrier arrayWriteBarrier, @SuppressWarnings("unused") LoweringTool tool) {
-            ResolvedJavaMethod method = serialArrayWriteBarrier;
+        public void lower(SerialWriteBarrierPost fieldWriteBarrier, @SuppressWarnings("unused") LoweringTool tool) {
+            ResolvedJavaMethod method = serialWriteBarrierPost;
             Key key = new Key(method);
-            Arguments arguments = new Arguments();
-            arguments.add("object", arrayWriteBarrier.getObject());
-            arguments.add("location", arrayWriteBarrier.getLocation());
-            SnippetTemplate template = cache.get(key, assumptions);
-            template.instantiate(runtime, arrayWriteBarrier, DEFAULT_REPLACER, arguments);
-        }
-
-        public void lower(FieldWriteBarrier fieldWriteBarrier, @SuppressWarnings("unused") LoweringTool tool) {
-            ResolvedJavaMethod method = serialFieldWriteBarrier;
-            Key key = new Key(method);
+            key.add("usePrecise", fieldWriteBarrier.usePrecise());
             Arguments arguments = new Arguments();
             arguments.add("object", fieldWriteBarrier.getObject());
+            arguments.add("location", fieldWriteBarrier.getLocation());
             SnippetTemplate template = cache.get(key, assumptions);
             template.instantiate(runtime, fieldWriteBarrier, DEFAULT_REPLACER, arguments);
         }
 
-        public void lower(WriteBarrierPre writeBarrierPre, @SuppressWarnings("unused") LoweringTool tool) {
-            ResolvedJavaMethod method = g1PreWriteBarrier;
+        public void lower(G1WriteBarrierPre writeBarrierPre, @SuppressWarnings("unused") LoweringTool tool) {
+            ResolvedJavaMethod method = g1WriteBarrierPre;
             Key key = new Key(method);
             key.add("doLoad", writeBarrierPre.doLoad());
             Arguments arguments = new Arguments();
@@ -194,8 +170,8 @@
             template.instantiate(runtime, writeBarrierPre, DEFAULT_REPLACER, arguments);
         }
 
-        public void lower(WriteBarrierPost writeBarrierPost, @SuppressWarnings("unused") LoweringTool tool) {
-            ResolvedJavaMethod method = g1PostWriteBarrier;
+        public void lower(G1WriteBarrierPost writeBarrierPost, @SuppressWarnings("unused") LoweringTool tool) {
+            ResolvedJavaMethod method = g1WriteBarrierPost;
             Key key = new Key(method);
             key.add("usePrecise", writeBarrierPost.usePrecise());
             Arguments arguments = new Arguments();