changeset 13330:1f4c9729c9f0

add base class for new object nodes, simplification to remove new objects without real usages
author Lukas Stadler <lukas.stadler@jku.at>
date Fri, 13 Dec 2013 22:43:21 +0100
parents 5d47d69d523a
children 51b2999299bc
files graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/AbstractNewArrayNode.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/AbstractNewObjectNode.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/DynamicNewArrayNode.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/NewInstanceNode.java
diffstat 4 files changed, 119 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/AbstractNewArrayNode.java	Fri Dec 13 20:20:11 2013 +0100
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/AbstractNewArrayNode.java	Fri Dec 13 22:43:21 2013 +0100
@@ -22,7 +22,6 @@
  */
 package com.oracle.graal.nodes.java;
 
-import com.oracle.graal.graph.*;
 import com.oracle.graal.graph.spi.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.spi.*;
@@ -31,10 +30,9 @@
 /**
  * The {@code AbstractNewArrayNode} is used for all 1-dimensional array allocations.
  */
-public class AbstractNewArrayNode extends DeoptimizingFixedWithNextNode implements Canonicalizable, Lowerable, ArrayLengthProvider {
+public class AbstractNewArrayNode extends AbstractNewObjectNode implements ArrayLengthProvider {
 
     @Input private ValueNode length;
-    private final boolean fillContents;
 
     @Override
     public ValueNode length() {
@@ -49,16 +47,8 @@
      * @param fillContents determines whether the array elements should be initialized to zero/null.
      */
     protected AbstractNewArrayNode(Stamp stamp, ValueNode length, boolean fillContents) {
-        super(stamp);
+        super(stamp, fillContents);
         this.length = length;
-        this.fillContents = fillContents;
-    }
-
-    /**
-     * @return <code>true</code> if the elements of the array will be initialized.
-     */
-    public boolean fillContents() {
-        return fillContents;
     }
 
     /**
@@ -77,23 +67,11 @@
     }
 
     @Override
-    public Node canonical(CanonicalizerTool tool) {
-        if (usages().isEmpty()) {
-            Stamp stamp = length.stamp();
-            if (stamp instanceof IntegerStamp && ((IntegerStamp) stamp).isPositive()) {
-                return null;
-            }
+    public void simplify(SimplifierTool tool) {
+        Stamp stamp = length.stamp();
+        if (stamp instanceof IntegerStamp && ((IntegerStamp) stamp).isPositive()) {
+            // otherwise, removing the allocation might swallow a NegativeArraySizeException
+            super.simplify(tool);
         }
-        return this;
-    }
-
-    @Override
-    public void lower(LoweringTool tool) {
-        tool.getLowerer().lower(this, tool);
-    }
-
-    @Override
-    public boolean canDeoptimize() {
-        return true;
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/AbstractNewObjectNode.java	Fri Dec 13 22:43:21 2013 +0100
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2009, 2011, 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.nodes.java;
+
+import java.util.*;
+
+import com.oracle.graal.graph.*;
+import com.oracle.graal.graph.spi.*;
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.extended.*;
+import com.oracle.graal.nodes.spi.*;
+import com.oracle.graal.nodes.type.*;
+
+/**
+ * The {@code AbstractNewObjectNode} is the base class for the new instance and new array nodes.
+ */
+public class AbstractNewObjectNode extends DeoptimizingFixedWithNextNode implements Simplifiable, Lowerable {
+
+    private final boolean fillContents;
+
+    /**
+     * Constructs a new AbstractNewObjectNode.
+     * 
+     * @param stamp the stamp of the newly created object
+     * @param fillContents determines if the object's contents should be initialized to zero/null.
+     */
+    protected AbstractNewObjectNode(Stamp stamp, boolean fillContents) {
+        super(stamp);
+        this.fillContents = fillContents;
+    }
+
+    /**
+     * @return <code>true</code> if the object's contents should be initialized to zero/null.
+     */
+    public boolean fillContents() {
+        return fillContents;
+    }
+
+    @Override
+    public void simplify(SimplifierTool tool) {
+        // poor man's escape analysis: check if the object can be trivially removed
+        for (Node usage : usages()) {
+            if (usage instanceof FixedValueAnchorNode) {
+                if (((FixedValueAnchorNode) usage).usages().isNotEmpty()) {
+                    return;
+                }
+            } else if (usage instanceof WriteNode) {
+                if (((WriteNode) usage).object() != this || usage.usages().isNotEmpty()) {
+                    // we would need to fix up the memory graph if the write has usages
+                    return;
+                }
+            } else {
+                return;
+            }
+        }
+        for (Node usage : usages().snapshot()) {
+            List<Node> snapshot = usage.inputs().snapshot();
+            graph().removeFixed((FixedWithNextNode) usage);
+            for (Node input : snapshot) {
+                tool.removeIfUnused(input);
+            }
+        }
+        List<Node> snapshot = inputs().snapshot();
+        graph().removeFixed(this);
+        for (Node input : snapshot) {
+            tool.removeIfUnused(input);
+        }
+    }
+
+    @Override
+    public void lower(LoweringTool tool) {
+        tool.getLowerer().lower(this, tool);
+    }
+
+    @Override
+    public boolean canDeoptimize() {
+        return true;
+    }
+}
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/DynamicNewArrayNode.java	Fri Dec 13 20:20:11 2013 +0100
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/DynamicNewArrayNode.java	Fri Dec 13 22:43:21 2013 +0100
@@ -23,6 +23,7 @@
 package com.oracle.graal.nodes.java;
 
 import java.lang.reflect.*;
+import java.util.*;
 
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.graph.*;
@@ -34,7 +35,7 @@
  * The {@code DynamicNewArrayNode} is used for allocation of arrays when the type is not a
  * compile-time constant.
  */
-public class DynamicNewArrayNode extends AbstractNewArrayNode implements Canonicalizable {
+public class DynamicNewArrayNode extends AbstractNewArrayNode {
 
     @Input private ValueNode elementType;
 
@@ -52,15 +53,20 @@
     }
 
     @Override
-    public Node canonical(CanonicalizerTool tool) {
-        if (elementType.isConstant()) {
+    public void simplify(SimplifierTool tool) {
+        if (isAlive() && elementType.isConstant()) {
             Class<?> elementClass = (Class<?>) elementType.asConstant().asObject();
             if (elementClass != null && !(elementClass.equals(void.class))) {
                 ResolvedJavaType javaType = tool.getMetaAccess().lookupJavaType(elementClass);
-                return graph().add(new NewArrayNode(javaType, length(), fillContents()));
+                NewArrayNode newArray = graph().add(new NewArrayNode(javaType, length(), fillContents()));
+                List<Node> snapshot = inputs().snapshot();
+                graph().replaceFixedWithFixed(this, newArray);
+                for (Node input : snapshot) {
+                    tool.removeIfUnused(input);
+                }
+                tool.addToWorkList(newArray);
             }
         }
-        return this;
     }
 
     @NodeIntrinsic
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/NewInstanceNode.java	Fri Dec 13 20:20:11 2013 +0100
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/NewInstanceNode.java	Fri Dec 13 22:43:21 2013 +0100
@@ -26,7 +26,6 @@
 
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.graph.*;
-import com.oracle.graal.graph.spi.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.spi.*;
 import com.oracle.graal.nodes.type.*;
@@ -36,10 +35,9 @@
  * The {@code NewInstanceNode} represents the allocation of an instance class object.
  */
 @NodeInfo(nameTemplate = "New {p#instanceClass/s}")
-public class NewInstanceNode extends DeoptimizingFixedWithNextNode implements Canonicalizable, Lowerable, VirtualizableAllocation {
+public class NewInstanceNode extends AbstractNewObjectNode implements VirtualizableAllocation {
 
     private final ResolvedJavaType instanceClass;
-    private final boolean fillContents;
 
     /**
      * Constructs a NewInstanceNode.
@@ -49,10 +47,9 @@
      *            zero/null.
      */
     public NewInstanceNode(ResolvedJavaType type, boolean fillContents) {
-        super(StampFactory.exactNonNull(type));
+        super(StampFactory.exactNonNull(type), fillContents);
         assert !type.isArray();
         this.instanceClass = type;
-        this.fillContents = fillContents;
     }
 
     /**
@@ -64,27 +61,6 @@
         return instanceClass;
     }
 
-    /**
-     * @return <code>true</code> if the fields of the new object will be initialized.
-     */
-    public boolean fillContents() {
-        return fillContents;
-    }
-
-    @Override
-    public Node canonical(CanonicalizerTool tool) {
-        if (usages().isEmpty()) {
-            return null;
-        } else {
-            return this;
-        }
-    }
-
-    @Override
-    public void lower(LoweringTool tool) {
-        tool.getLowerer().lower(this, tool);
-    }
-
     @Override
     public void virtualize(VirtualizerTool tool) {
         /*
@@ -107,9 +83,4 @@
     protected ConstantNode defaultFieldValue(ResolvedJavaField field) {
         return ConstantNode.defaultForKind(field.getType().getKind(), graph());
     }
-
-    @Override
-    public boolean canDeoptimize() {
-        return true;
-    }
 }