changeset 5452:ce4dafd906d0

Make it possible in SuperBlock to duplicate the whole loop (with the loopbegin)
author Gilles Duboscq <duboscq@ssw.jku.at>
date Wed, 30 May 2012 18:13:35 +0200
parents a899a40a7ddf
children 071f24ba116e
files graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/loop/LoopTransformUtil.java graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/loop/SuperBlock.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeBitMap.java
diffstat 3 files changed, 49 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/loop/LoopTransformUtil.java	Tue May 29 16:49:20 2012 +0200
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/loop/LoopTransformUtil.java	Wed May 30 18:13:35 2012 +0200
@@ -35,7 +35,7 @@
     }
 
     public static void peel(Loop loop, SuperBlock wholeLoop) {
-        SuperBlock peel = wholeLoop.duplicate(); // duplicates the nodes, merges early exits
+        SuperBlock peel = wholeLoop.duplicate(true); // duplicates the nodes, merges early exits
 
         peel.insertBefore(loop.loopBegin().forwardEnd()); // connects peeled part's CFG
 
@@ -43,7 +43,7 @@
         resolver.wholeLoop(wholeLoop).peeled(peel); // block (comming from the loop) was peeled into peel
         resolver.resolve();
 
-        peel.finish();
+        peel.finishDuplication();
     }
 
     public static SuperBlock wholeLoop(Loop loop) {
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/loop/SuperBlock.java	Tue May 29 16:49:20 2012 +0200
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/loop/SuperBlock.java	Wed May 30 18:13:35 2012 +0200
@@ -38,7 +38,8 @@
     protected List<BeginNode> earlyExits;
     protected Map<Node, Node> duplicationMapping;
     protected SuperBlock original;
-    protected NodeBitMap loopNodes;
+    protected NodeBitMap allNodes;
+    protected NodeBitMap allNodesExcludeLoopPhi;
 
     public SuperBlock(BeginNode entry, BeginNode exit, List<BeginNode> blocks, List<BeginNode> earlyExits) {
         this.entry = entry;
@@ -57,31 +58,45 @@
         return entry;
     }
 
-    public NodeBitMap loopNodes() {
-        if (loopNodes == null) {
-            loopNodes = computeNodes();
+    public NodeBitMap nodes() {
+        if (allNodes == null) {
+            allNodes = computeNodes();
         }
-        return loopNodes;
+        return allNodes;
+    }
+
+    private NodeBitMap nodesExcludeLoopPhi() {
+        if (allNodesExcludeLoopPhi == null) {
+            allNodesExcludeLoopPhi = nodes().copy();
+            if (entry instanceof LoopBeginNode) {
+                for (PhiNode phi : ((LoopBeginNode) entry).phis()) {
+                    allNodesExcludeLoopPhi.clear(phi);
+                }
+            }
+        }
+        return allNodesExcludeLoopPhi;
     }
 
     public SuperBlock duplicate() {
-        NodeBitMap nodes = loopNodes();
+        return duplicate(false);
+    }
+
+    public SuperBlock duplicate(boolean excludeLoop) {
+        NodeBitMap nodes = nodes();
         Map<Node, Node> replacements = new HashMap<>();
         StructuredGraph graph = (StructuredGraph) entry.graph();
-        BeginNode newEntry = graph.add(new BeginNode());
-        BeginNode newExit = null;
+        if (excludeLoop || (entry instanceof MergeNode && !(entry instanceof LoopBeginNode))) {
+            replacements.put(entry, graph.add(new BeginNode())); // no merge/loop begin
+        }
         List<BeginNode> newEarlyExits = new ArrayList<>(earlyExits.size());
-        if (!(exit instanceof MergeNode)) {
-            newExit = graph.add(new BeginNode());
-            replacements.put(exit, newExit);
-        }
-        replacements.put(entry, newEntry); // no merge/loop begin
         for (BeginNode earlyExit : earlyExits) {
             BeginNode newEarlyExit = graph.add(new BeginNode());
             newEarlyExits.add(newEarlyExit);
             replacements.put(earlyExit, newEarlyExit);
         }
-        if (exit instanceof LoopBeginNode) {
+        if (exit instanceof LoopBeginNode && excludeLoop) {
+            assert entry == exit;
+            nodes = nodesExcludeLoopPhi();
             for (LoopEndNode end : ((LoopBeginNode) exit).loopEnds()) {
                 if (nodes.isMarked(end)) {
                     replacements.put(end, graph.add(new EndNode()));
@@ -89,8 +104,14 @@
             }
         }
         Map<Node, Node> duplicates = graph.addDuplicates(nodes, replacements);
-        if (exit instanceof MergeNode) {
+        BeginNode newExit;
+        if (excludeLoop || (exit instanceof MergeNode && !(exit instanceof LoopBeginNode))) {
             newExit = mergeExits(replacements, duplicates);
+        } else if (exit != entry) {
+            newExit = graph.add(new BeginNode());
+            replacements.put(exit, newExit);
+        } else {
+            newExit = (BeginNode) duplicates.get(exit);
         }
 
         List<BeginNode> newBlocks = new ArrayList<>(blocks.size());
@@ -105,7 +126,7 @@
         for (Entry<Node, Node> e : replacements.entrySet()) {
             duplicates.put(e.getKey(), e.getValue());
         }
-        SuperBlock superBlock = new SuperBlock(newEntry, newExit, newBlocks, newEarlyExits);
+        SuperBlock superBlock = new SuperBlock((BeginNode) duplicates.get(entry), newExit, newBlocks, newEarlyExits);
         superBlock.duplicationMapping = duplicates;
         superBlock.original = this;
         return superBlock;
@@ -163,7 +184,7 @@
         return newExit;
     }
 
-    public void finish() {
+    public void finishDuplication() {
         if (original != null) {
             mergeEarlyExits((StructuredGraph) entry.graph(), original.earlyExits, duplicationMapping);
         }
@@ -376,12 +397,6 @@
             }
         }
 
-        if (entry instanceof LoopBeginNode) {
-            for (PhiNode phi : ((LoopBeginNode) entry).phis()) {
-                nodes.clear(phi);
-            }
-        }
-
         return nodes;
     }
 
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeBitMap.java	Tue May 29 16:49:20 2012 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeBitMap.java	Wed May 30 18:13:35 2012 +0200
@@ -36,9 +36,13 @@
     }
 
     public NodeBitMap(Graph graph, boolean autoGrow) {
+        this(graph, autoGrow, new BitMap(graph.nodeIdCount()));
+    }
+
+    private NodeBitMap(Graph graph, boolean autoGrow, BitMap bits) {
         this.graph = graph;
         this.autoGrow = autoGrow;
-        bitMap = new BitMap(graph.nodeIdCount());
+        bitMap = bits;
     }
 
     public Graph graph() {
@@ -178,4 +182,8 @@
     public int cardinality() {
         return bitMap.cardinality();
     }
+
+    public NodeBitMap copy() {
+        return new NodeBitMap(graph, autoGrow, bitMap.copy());
+    }
 }