changeset 19609:b964772c43bd

Changes to the node list iterators to make more values loop invariant.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Wed, 25 Feb 2015 18:14:35 +0100
parents 3349fe56e6e9
children 4f8226c98a02
files graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Edges.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Position.java graal/com.oracle.graal.printer/src/com/oracle/graal/printer/BinaryGraphPrinter.java graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/EdgesTest.java graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/EdgesSubstitutions.java
diffstat 6 files changed, 103 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Edges.java	Wed Feb 25 17:06:15 2015 +0100
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Edges.java	Wed Feb 25 18:14:35 2015 +0100
@@ -61,20 +61,20 @@
         }
     }
 
-    private static Node getNode(Node node, long offset) {
+    private static Node getNodeUnsafe(Node node, long offset) {
         return (Node) unsafe.getObject(node, offset);
     }
 
     @SuppressWarnings("unchecked")
-    private static NodeList<Node> getNodeList(Node node, long offset) {
+    private static NodeList<Node> getNodeListUnsafe(Node node, long offset) {
         return (NodeList<Node>) unsafe.getObject(node, offset);
     }
 
-    private static void putNode(Node node, long offset, Node value) {
+    private static void putNodeUnsafe(Node node, long offset, Node value) {
         unsafe.putObject(node, offset, value);
     }
 
-    private static void putNodeList(Node node, long offset, NodeList<?> value) {
+    private static void putNodeListUnsafe(Node node, long offset, NodeList<?> value) {
         unsafe.putObject(node, offset, value);
     }
 
@@ -93,9 +93,8 @@
      * @param index the index of a non-list the edge (must be less than {@link #getDirectCount()})
      * @return the Node at the other edge of the requested edge
      */
-    public Node getNode(Node node, int index) {
-        assert index >= 0 && index < directCount;
-        return getNode(node, offsets[index]);
+    public static Node getNode(Node node, long[] offsets, int index) {
+        return getNodeUnsafe(node, offsets[index]);
     }
 
     /**
@@ -106,9 +105,8 @@
      *            {@link #getDirectCount()})
      * @return the {@link NodeList} at the other edge of the requested edge
      */
-    public NodeList<Node> getNodeList(Node node, int index) {
-        assert index >= directCount && index < getCount();
-        return getNodeList(node, offsets[index]);
+    public static NodeList<Node> getNodeList(Node node, long[] offsets, int index) {
+        return getNodeListUnsafe(node, offsets[index]);
     }
 
     /**
@@ -119,18 +117,22 @@
      * @param node the node whose edges are to be cleared
      */
     public void clear(Node node) {
+        final long[] curOffsets = this.offsets;
+        final Type curType = this.type;
         int index = 0;
-        while (index < getDirectCount()) {
-            initializeNode(node, index++, null);
+        int curDirectCount = getDirectCount();
+        while (index < curDirectCount) {
+            initializeNode(node, curOffsets, index++, null);
         }
-        while (index < getCount()) {
-            NodeList<Node> list = getNodeList(node, index);
+        int curCount = getCount();
+        while (index < curCount) {
+            NodeList<Node> list = getNodeList(node, curOffsets, index);
             if (list != null) {
                 int size = list.initialSize;
-                NodeList<Node> newList = type == Edges.Type.Inputs ? new NodeInputList<>(node, size) : new NodeSuccessorList<>(node, size);
+                NodeList<Node> newList = curType == Edges.Type.Inputs ? new NodeInputList<>(node, size) : new NodeSuccessorList<>(node, size);
 
                 // replacing with a new list object is the expected behavior!
-                initializeList(node, index, newList);
+                initializeList(node, curOffsets, index, newList);
             }
             index++;
         }
@@ -145,12 +147,14 @@
      */
     public void initializeLists(Node node, Node prototype) {
         int index = getDirectCount();
+        final long[] curOffsets = this.offsets;
+        final Edges.Type curType = this.type;
         while (index < getCount()) {
-            NodeList<Node> list = getNodeList(prototype, index);
+            NodeList<Node> list = getNodeList(prototype, curOffsets, index);
             if (list != null) {
                 int size = list.initialSize;
-                NodeList<Node> newList = type == Edges.Type.Inputs ? new NodeInputList<>(node, size) : new NodeSuccessorList<>(node, size);
-                initializeList(node, index, newList);
+                NodeList<Node> newList = curType == Edges.Type.Inputs ? new NodeInputList<>(node, size) : new NodeSuccessorList<>(node, size);
+                initializeList(node, curOffsets, index, newList);
             }
             index++;
         }
@@ -167,16 +171,20 @@
         assert fromNode != toNode;
         assert fromNode.getNodeClass().getClazz() == toNode.getNodeClass().getClazz();
         int index = 0;
-        while (index < getDirectCount()) {
-            initializeNode(toNode, index, getNode(fromNode, index));
+        final long[] curOffsets = this.offsets;
+        final Type curType = this.type;
+        int curDirectCount = getDirectCount();
+        while (index < curDirectCount) {
+            initializeNode(toNode, curOffsets, index, getNode(fromNode, curOffsets, index));
             index++;
         }
-        while (index < getCount()) {
-            NodeList<Node> list = getNodeList(toNode, index);
-            NodeList<Node> fromList = getNodeList(fromNode, index);
+        int curCount = getCount();
+        while (index < curCount) {
+            NodeList<Node> list = getNodeList(toNode, curOffsets, index);
+            NodeList<Node> fromList = getNodeList(fromNode, curOffsets, index);
             if (list == null || list == fromList) {
-                list = type == Edges.Type.Inputs ? new NodeInputList<>(toNode, fromList) : new NodeSuccessorList<>(toNode, fromList);
-                initializeList(toNode, index, list);
+                list = curType == Edges.Type.Inputs ? new NodeInputList<>(toNode, fromList) : new NodeSuccessorList<>(toNode, fromList);
+                initializeList(toNode, curOffsets, index, list);
             } else {
                 list.copy(fromList);
             }
@@ -195,17 +203,20 @@
      */
     public boolean replaceFirst(Node node, Node key, Node replacement) {
         int index = 0;
-        while (index < getDirectCount()) {
-            Node edge = getNode(node, index);
+        final long[] curOffsets = this.getOffsets();
+        int curDirectCount = getDirectCount();
+        while (index < curDirectCount) {
+            Node edge = getNode(node, curOffsets, index);
             if (edge == key) {
                 assert replacement == null || getType(index).isAssignableFrom(replacement.getClass()) : "Can not assign " + replacement.getClass() + " to " + getType(index) + " in " + node;
-                initializeNode(node, index, replacement);
+                initializeNode(node, curOffsets, index, replacement);
                 return true;
             }
             index++;
         }
-        while (index < getCount()) {
-            NodeList<Node> list = getNodeList(node, index);
+        int curCount = getCount();
+        while (index < curCount) {
+            NodeList<Node> list = getNodeList(node, curOffsets, index);
             if (list != null) {
                 if (list.replaceFirst(key, replacement)) {
                     return true;
@@ -229,13 +240,12 @@
      * @param index the index of the edge (between 0 and {@link #getCount()})
      * @param value the node to be written to the edge
      */
-    public void initializeNode(Node node, int index, Node value) {
-        putNode(node, offsets[index], value);
+    public static void initializeNode(Node node, long[] offsets, int index, Node value) {
+        putNodeUnsafe(node, offsets[index], value);
     }
 
-    public void initializeList(Node node, int index, NodeList<Node> value) {
-        assert index >= directCount;
-        putNodeList(node, offsets[index], value);
+    public static void initializeList(Node node, long[] offsets, int index, NodeList<Node> value) {
+        putNodeListUnsafe(node, offsets[index], value);
     }
 
     /**
@@ -248,21 +258,22 @@
      */
     public void setNode(Node node, int index, Node value) {
         assert index < directCount;
-        Node old = getNode(node, offsets[index]);
-        putNode(node, offsets[index], value);
+        Node old = getNodeUnsafe(node, offsets[index]);
+        putNodeUnsafe(node, offsets[index], value);
         update(node, old, value);
     }
 
     protected abstract void update(Node node, Node oldValue, Node newValue);
 
     public boolean contains(Node node, Node value) {
+        final long[] curOffsets = this.offsets;
         for (int i = 0; i < directCount; i++) {
-            if (getNode(node, i) == value) {
+            if (getNode(node, curOffsets, i) == value) {
                 return true;
             }
         }
         for (int i = directCount; i < getCount(); i++) {
-            NodeList<?> curList = getNodeList(node, i);
+            NodeList<?> curList = getNodeList(node, curOffsets, i);
             if (curList != null && curList.contains(value)) {
                 return true;
             }
@@ -276,15 +287,16 @@
     public boolean areEqualIn(Node node, Node other) {
         assert node.getNodeClass().getClazz() == other.getNodeClass().getClazz();
         int index = 0;
+        final long[] curOffsets = this.offsets;
         while (index < directCount) {
-            if (getNode(other, index) != getNode(node, index)) {
+            if (getNode(other, curOffsets, index) != getNode(node, curOffsets, index)) {
                 return false;
             }
             index++;
         }
         while (index < getCount()) {
-            NodeList<Node> list = getNodeList(other, index);
-            if (!Objects.equals(list, getNodeList(node, index))) {
+            NodeList<Node> list = getNodeList(other, curOffsets, index);
+            if (!Objects.equals(list, getNodeList(node, curOffsets, index))) {
                 return false;
             }
             index++;
@@ -306,6 +318,9 @@
         NodeList<Node> list;
         protected boolean needsForward;
         protected Node nextElement;
+        protected final int directCount;
+        protected final int count;
+        protected final long[] offsets;
 
         /**
          * Creates an iterator that will iterate over some given edges in a given node.
@@ -316,14 +331,17 @@
             index = NOT_ITERABLE;
             subIndex = 0;
             needsForward = true;
+            this.directCount = edges.getDirectCount();
+            this.offsets = edges.getOffsets();
+            this.count = edges.getCount();
         }
 
         void forward() {
             needsForward = false;
-            if (index < edges.getDirectCount()) {
+            if (index < directCount) {
                 index++;
-                while (index < edges.getDirectCount()) {
-                    nextElement = edges.getNode(node, index);
+                while (index < directCount) {
+                    nextElement = Edges.getNode(node, offsets, index);
                     if (nextElement != null) {
                         return;
                     }
@@ -340,7 +358,7 @@
         private void forwardNodeList() {
             do {
                 if (subIndex == 0) {
-                    list = edges.getNodeList(node, index);
+                    list = Edges.getNodeList(node, offsets, index);
                 }
                 if (list != null) {
                     while (subIndex < list.size()) {
@@ -361,7 +379,7 @@
                 forward();
             }
             needsForward = true;
-            if (index < edges.getCount()) {
+            if (index < count) {
                 return nextElement;
             }
             throw new NoSuchElementException();
@@ -385,7 +403,7 @@
                 forward();
             }
             needsForward = true;
-            if (index < edges.getDirectCount()) {
+            if (index < directCount) {
                 return new Position(edges, index, NOT_ITERABLE);
             } else {
                 return new Position(edges, index, subIndex);
@@ -399,6 +417,7 @@
     }
 
     private static class AllEdgesIterator extends EdgesIterator {
+
         AllEdgesIterator(Node node, Edges edges) {
             super(node, edges);
         }
@@ -406,10 +425,10 @@
         @Override
         void forward() {
             needsForward = false;
-            if (index < edges.getDirectCount()) {
+            if (index < directCount) {
                 index++;
                 if (index < edges.getDirectCount()) {
-                    nextElement = edges.getNode(node, index);
+                    nextElement = Edges.getNode(node, edges.getOffsets(), index);
                     return;
                 }
             } else {
@@ -417,7 +436,7 @@
             }
             while (index < edges.getCount()) {
                 if (subIndex == 0) {
-                    list = edges.getNodeList(node, index);
+                    list = Edges.getNodeList(node, edges.getOffsets(), index);
                 }
                 if (list != null) {
                     if (subIndex < list.size()) {
@@ -468,6 +487,9 @@
         }
     }
 
+    static int cnt1;
+    static int cnt2;
+
     public NodeClassIterable getIterable(final Node node) {
         return new NodeClassIterable() {
 
@@ -498,8 +520,9 @@
     public void accept(Node node, BiConsumer<Node, Node> consumer) {
         int index = 0;
         int curDirectCount = this.directCount;
+        final long[] curOffsets = this.offsets;
         while (index < curDirectCount) {
-            Node curNode = getNode(node, index);
+            Node curNode = getNode(node, curOffsets, index);
             if (curNode != null) {
                 consumer.accept(node, curNode);
             }
@@ -507,7 +530,7 @@
         }
         int count = getCount();
         while (index < count) {
-            NodeList<Node> list = getNodeList(node, index);
+            NodeList<Node> list = getNodeList(node, curOffsets, index);
             if (list != null) {
                 for (int i = 0; i < list.size(); ++i) {
                     Node curNode = list.get(i);
@@ -519,4 +542,8 @@
             index++;
         }
     }
+
+    public long[] getOffsets() {
+        return this.offsets;
+    }
 }
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java	Wed Feb 25 17:06:15 2015 +0100
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java	Wed Feb 25 18:14:35 2015 +0100
@@ -571,8 +571,9 @@
         int index = 0;
         Type curType = edges.type();
         int directCount = edges.getDirectCount();
+        final long[] curOffsets = edges.getOffsets();
         while (index < directCount) {
-            Node edge = edges.getNode(node, index);
+            Node edge = Edges.getNode(node, curOffsets, index);
             if (edge != null) {
                 Node newEdge = duplicationReplacement.replacement(edge, curType);
                 if (curType == Edges.Type.Inputs) {
@@ -581,15 +582,15 @@
                     node.updatePredecessor(null, newEdge);
                 }
                 assert assertUpdateValid(node, edges, index, newEdge);
-                edges.initializeNode(node, index, newEdge);
+                Edges.initializeNode(node, curOffsets, index, newEdge);
             }
             index++;
         }
 
         while (index < edges.getCount()) {
-            NodeList<Node> list = edges.getNodeList(node, index);
+            NodeList<Node> list = Edges.getNodeList(node, curOffsets, index);
             if (list != null) {
-                edges.initializeList(node, index, updateEdgeListCopy(node, list, duplicationReplacement, curType));
+                Edges.initializeList(node, curOffsets, index, updateEdgeListCopy(node, list, duplicationReplacement, curType));
             }
             index++;
         }
@@ -645,9 +646,10 @@
 
     private void initNullEdgeLists(Node node, Edges.Type type) {
         Edges edges = getEdges(type);
+        final long[] curOffsets = edges.getOffsets();
         for (int inputPos = edges.getDirectCount(); inputPos < edges.getCount(); inputPos++) {
-            if (edges.getNodeList(node, inputPos) == null) {
-                edges.initializeList(node, inputPos, type == Edges.Type.Inputs ? new NodeInputList<>(node) : new NodeSuccessorList<>(node));
+            if (Edges.getNodeList(node, curOffsets, inputPos) == null) {
+                Edges.initializeList(node, curOffsets, inputPos, type == Edges.Type.Inputs ? new NodeInputList<>(node) : new NodeSuccessorList<>(node));
             }
         }
     }
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Position.java	Wed Feb 25 17:06:15 2015 +0100
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Position.java	Wed Feb 25 18:14:35 2015 +0100
@@ -53,9 +53,9 @@
 
     public Node get(Node node) {
         if (index < edges.getDirectCount()) {
-            return edges.getNode(node, index);
+            return Edges.getNode(node, edges.getOffsets(), index);
         } else {
-            return edges.getNodeList(node, index).get(subIndex);
+            return Edges.getNodeList(node, edges.getOffsets(), index).get(subIndex);
         }
     }
 
@@ -75,15 +75,15 @@
         if (index < edges.getDirectCount()) {
             edges.setNode(node, index, value);
         } else {
-            edges.getNodeList(node, index).set(subIndex, value);
+            Edges.getNodeList(node, edges.getOffsets(), index).set(subIndex, value);
         }
     }
 
     public void initialize(Node node, Node value) {
         if (index < edges.getDirectCount()) {
-            edges.initializeNode(node, index, value);
+            Edges.initializeNode(node, edges.getOffsets(), index, value);
         } else {
-            edges.getNodeList(node, index).initialize(subIndex, value);
+            Edges.getNodeList(node, edges.getOffsets(), index).initialize(subIndex, value);
         }
     }
 
--- a/graal/com.oracle.graal.printer/src/com/oracle/graal/printer/BinaryGraphPrinter.java	Wed Feb 25 17:06:15 2015 +0100
+++ b/graal/com.oracle.graal.printer/src/com/oracle/graal/printer/BinaryGraphPrinter.java	Wed Feb 25 18:14:35 2015 +0100
@@ -441,11 +441,12 @@
     private void writeEdges(Node node, Edges.Type type) throws IOException {
         NodeClass<?> nodeClass = node.getNodeClass();
         Edges edges = nodeClass.getEdges(type);
+        final long[] curOffsets = edges.getOffsets();
         for (int i = 0; i < edges.getDirectCount(); i++) {
-            writeNodeRef(edges.getNode(node, i));
+            writeNodeRef(Edges.getNode(node, curOffsets, i));
         }
         for (int i = edges.getDirectCount(); i < edges.getCount(); i++) {
-            NodeList<Node> list = edges.getNodeList(node, i);
+            NodeList<Node> list = Edges.getNodeList(node, curOffsets, i);
             if (list == null) {
                 writeShort((char) 0);
             } else {
--- a/graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/EdgesTest.java	Wed Feb 25 17:06:15 2015 +0100
+++ b/graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/EdgesTest.java	Wed Feb 25 18:14:35 2015 +0100
@@ -78,20 +78,20 @@
 
     /**
      * Checks that there are no checkcasts in the compiled version of
-     * {@link Edges#getNode(Node, int)}.
+     * {@link Edges#getNode(Node, long[], int)}.
      */
     @Test
     public void test0() {
-        testMethod(getMethod("getNode", Node.class, int.class), inputs, node, 0);
+        testMethod(getMethod("getNode", Node.class, long[].class, int.class), inputs, node, 0);
     }
 
     /**
      * Checks that there are no checkcasts in the compiled version of
-     * {@link Edges#getNodeList(Node, int)}.
+     * {@link Edges#getNodeList(Node, long[], int)}.
      */
     @Test
     public void test1() {
-        testMethod(getMethod("getNodeList", Node.class, int.class), inputs, node, 2);
+        testMethod(getMethod("getNodeList", Node.class, long[].class, int.class), inputs, node, 2);
     }
 
     /**
--- a/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/EdgesSubstitutions.java	Wed Feb 25 17:06:15 2015 +0100
+++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/EdgesSubstitutions.java	Wed Feb 25 18:14:35 2015 +0100
@@ -40,22 +40,12 @@
 public class EdgesSubstitutions {
 
     @MethodSubstitution
-    private static Node getNode(Node node, long offset) {
+    private static Node getNodeUnsafe(Node node, long offset) {
         return PiNode.piCast(UnsafeLoadNode.load(node, offset, Kind.Object, LocationIdentity.ANY_LOCATION), Node.class);
     }
 
     @MethodSubstitution
-    private static NodeList<?> getNodeList(Node node, long offset) {
+    private static NodeList<?> getNodeListUnsafe(Node node, long offset) {
         return PiNode.piCast(UnsafeLoadNode.load(node, offset, Kind.Object, LocationIdentity.ANY_LOCATION), NodeList.class);
     }
-
-    @MethodSubstitution
-    private static void putNode(Node node, long offset, Node value) {
-        UnsafeStoreNode.store(node, offset, value, Kind.Object, LocationIdentity.ANY_LOCATION);
-    }
-
-    @MethodSubstitution
-    private static void putNodeList(Node node, long offset, NodeList<?> value) {
-        UnsafeStoreNode.store(node, offset, value, Kind.Object, LocationIdentity.ANY_LOCATION);
-    }
 }