changeset 2961:0966a5a904ad

Created variable part in NodeArray.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 15 Jun 2011 11:55:47 +0200
parents 49a8b14e9d24
children ef7ceaf48b6f
files graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/ir/Phi.java graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/Graph.java graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/Node.java graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/NodeArray.java
diffstat 4 files changed, 97 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/ir/Phi.java	Tue Jun 14 16:41:27 2011 +0200
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/ir/Phi.java	Wed Jun 15 11:55:47 2011 +0200
@@ -37,16 +37,13 @@
     private static final int INPUT_COUNT = 1;
     private static final int INPUT_MERGE = 0;
 
-    private final int maxValues;
-
     private static final int SUCCESSOR_COUNT = 0;
 
-    private int usedInputCount;
     private boolean isDead;
 
     @Override
     protected int inputCount() {
-        return super.inputCount() + INPUT_COUNT + maxValues;
+        return super.inputCount() + INPUT_COUNT;
     }
 
     @Override
@@ -61,24 +58,12 @@
         return (Merge) inputs().get(super.inputCount() + INPUT_MERGE);
     }
 
-    public Value setMerge(Value n) {
-        return (Merge) inputs().set(super.inputCount() + INPUT_MERGE, n);
+    public void setMerge(Value n) {
+        inputs().set(super.inputCount() + INPUT_MERGE, n);
     }
 
-    /**
-     * Create a new Phi for the specified join block and local variable (or operand stack) slot.
-     * @param kind the type of the variable
-     * @param merge the join point
-     * @param graph
-     */
     public Phi(CiKind kind, Merge merge, Graph graph) {
-        this(kind, merge, DEFAULT_MAX_VALUES, graph);
-    }
-
-    public Phi(CiKind kind, Merge merge, int maxValues, Graph graph) {
-        super(kind, INPUT_COUNT + maxValues, SUCCESSOR_COUNT, graph);
-        this.maxValues = maxValues;
-        usedInputCount = 0;
+        super(kind, INPUT_COUNT, SUCCESSOR_COUNT, graph);
         setMerge(merge);
     }
 
@@ -101,7 +86,7 @@
      * @return the number of inputs in this phi
      */
     public int valueCount() {
-        return usedInputCount;
+        return inputs().variablePart().size();
     }
 
     @Override
@@ -145,35 +130,17 @@
     }
 
     public Phi addInput(Node y) {
-        assert !this.isDeleted() && !y.isDeleted();
-        Phi phi = this;
-        if (usedInputCount == maxValues) {
-            phi = new Phi(kind, merge(), maxValues * 2, graph());
-            for (int i = 0; i < valueCount(); ++i) {
-                phi.addInput(valueAt(i));
-            }
-            phi.addInput(y);
-            this.replace(phi);
-        } else {
-            setValueAt(usedInputCount++, y);
-        }
-        return phi;
+        inputs().variablePart().add(y);
+        return this;
     }
 
     public void removeInput(int index) {
-        assert index < valueCount() : "index: " + index + ", valueCount: " + valueCount() + "@phi " + id();
-        setValueAt(index, Node.Null);
-        for (int i = index + 1; i < valueCount(); ++i) {
-            setValueAt(i - 1, valueAt(i));
-        }
-        setValueAt(valueCount() - 1, Node.Null);
-        usedInputCount--;
+        inputs().variablePart().remove(index);
     }
 
     @Override
     public Node copy(Graph into) {
-        Phi x = new Phi(kind, null, maxValues, into);
-        x.usedInputCount = usedInputCount;
+        Phi x = new Phi(kind, null, into);
         x.isDead = isDead;
         return x;
     }
--- a/graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/Graph.java	Tue Jun 14 16:41:27 2011 +0200
+++ b/graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/Graph.java	Wed Jun 15 11:55:47 2011 +0200
@@ -171,7 +171,7 @@
                 if (target == null) {
                     target = newNodes.get(input);
                 }
-                node.inputs().set(i, target);
+                node.inputs().setOrExpand(i, target);
             }
         }
         for (Entry<Node, Node> entry : replacements.entrySet()) {
@@ -180,7 +180,7 @@
             for (int i = 0; i < oldNode.inputs().size(); i++) {
                 Node input = oldNode.inputs().get(i);
                 if (newNodes.containsKey(input)) {
-                    node.inputs().set(i, newNodes.get(input));
+                    node.inputs().setOrExpand(i, newNodes.get(input));
                 }
             }
         }
--- a/graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/Node.java	Tue Jun 14 16:41:27 2011 +0200
+++ b/graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/Node.java	Wed Jun 15 11:55:47 2011 +0200
@@ -103,7 +103,7 @@
         int z = 0;
         for (Node predecessor : predecessors) {
             int predIndex = predecessorsIndex.get(z);
-            predecessor.successors.nodes[predIndex] = other;
+            predecessor.successors.silentSet(predIndex, other);
             ++z;
         }
         if (other != null) {
--- a/graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/NodeArray.java	Tue Jun 14 16:41:27 2011 +0200
+++ b/graal/com.oracle.max.graal.graph/src/com/oracle/max/graal/graph/NodeArray.java	Wed Jun 15 11:55:47 2011 +0200
@@ -26,16 +26,17 @@
 import java.util.Arrays;
 import java.util.Iterator;
 
-import org.omg.PortableInterceptor.SUCCESSFUL;
-
 public class NodeArray extends AbstractList<Node> {
 
     private final Node node;
-    final Node[] nodes;
+    private Node[] nodes;
+    private final int fixedLength;
+    private int variableLength;
 
     public NodeArray(Node node, int length) {
         this.node = node;
         this.nodes = new Node[length];
+        this.fixedLength = length;
     }
 
     @Override
@@ -52,14 +53,81 @@
         nodes[index] = node;
         return result;
     }
+    
+    public AbstractList<Node> variablePart() {
+        return new AbstractList<Node>() {
+
+            @Override
+            public Node get(int index) {
+                checkIndex(index);
+                return NodeArray.this.get(fixedLength + index);
+            }
+
+            @Override
+            public int size() {
+                return variableLength;
+            }
+
+            public Node set(int index, Node element) {
+                checkIndex(index);
+                return NodeArray.this.set(fixedLength + index, element);
+            }
+
+            public void add(int index, Node element) {
+                variableLength++;
+                checkIndex(index);
+                NodeArray.this.ensureSize();
+                for (int i=size() - 1; i > index; i--) {
+                    NodeArray.this.nodes[fixedLength + i] = NodeArray.this.nodes[fixedLength + i-1];
+                }
+                set(index, element);
+            }
+            
+            private void checkIndex(int index) {
+                if (index < 0 || index >= size()) {
+                    throw new IndexOutOfBoundsException();
+                }
+            }
+            
+            @Override
+            public Node remove(int index) {
+                checkIndex(index);
+                Node n = get(index);
+                set(index, Node.Null);
+                for (int i=index; i < size() - 1; i++) {
+                    NodeArray.this.nodes[fixedLength + i] = NodeArray.this.nodes[fixedLength + i + 1];
+                }
+                variableLength--;
+                return n;
+            }
+        };
+    }
+
+    private void ensureSize() {
+        if (size() > nodes.length) {
+            nodes = Arrays.copyOf(nodes, (nodes.length + 1)*2);
+        }
+    }
+    
+    public void setOrExpand(int index, Node node) {
+        if (index < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        
+        while (index >= size()) {
+            variablePart().add(Node.Null);
+        }
+        
+        set(index, node);
+    }
 
     @Override
     public Node set(int index, Node node) {
         assert !self().isDeleted() : "trying to set input/successor of deleted node: " + self().shortName();
         assert node == Node.Null || node.graph == self().graph : "node is from different graph: (this=" + self() + ") and (node=" + node + ")";
         assert node == Node.Null || node.id() != Node.DeletedID : "inserted node must not be deleted";
-        Node old = nodes[index];
-
+        
+        Node old = get(index);
         if (old != node) {
             silentSet(index, node);
             if (self().inputs == this) {
@@ -96,9 +164,16 @@
             set(i, other.get(i));
         }
     }
+    
+    private void checkIndex(int index) {
+        if (index < 0 || index >= size()) {
+            throw new IndexOutOfBoundsException();
+        }
+    }
 
     @Override
     public Node get(int index) {
+        checkIndex(index);
         return nodes[index];
     }
 
@@ -108,7 +183,7 @@
     }
 
     boolean replaceFirstOccurrence(Node toReplace, Node replacement) {
-        for (int i = 0; i < nodes.length; i++) {
+        for (int i = 0; i < size(); i++) {
             if (nodes[i] == toReplace) {
                 nodes[i] = replacement;
                 return true;
@@ -123,7 +198,7 @@
 
     public int replace(Node toReplace, Node replacement) {
         int result = 0;
-        for (int i = 0; i < nodes.length; i++) {
+        for (int i = 0; i < size(); i++) {
             if (nodes[i] == toReplace) {
                 set(i, replacement);
                 result++;
@@ -138,7 +213,7 @@
 
     int silentReplace(Node toReplace, Node replacement) {
         int result = 0;
-        for (int i = 0; i < nodes.length; i++) {
+        for (int i = 0; i < size(); i++) {
             if (nodes[i] == toReplace) {
                 silentSet(i, replacement);
                 result++;
@@ -169,11 +244,11 @@
 
     @Override
     public int size() {
-        return nodes.length;
+        return fixedLength + variableLength;
     }
 
     public void clearAll() {
-        for (int i = 0; i < nodes.length; i++) {
+        for (int i = 0; i < size(); i++) {
             set(i, Node.Null);
         }
     }