[llvm-commits] CVS: llvm/lib/Analysis/PostDominators.cpp

Devang Patel dpatel at apple.com
Wed Sep 27 10:18:20 PDT 2006



Changes in directory llvm/lib/Analysis:

PostDominators.cpp updated: 1.60 -> 1.61
---
Log message:

Fix DFS walk.
Fix http://llvm.org/bugs/show_bug.cgi?id=923


---
Diffs of the changes:  (+27 -14)

 PostDominators.cpp |   41 +++++++++++++++++++++++++++--------------
 1 files changed, 27 insertions(+), 14 deletions(-)


Index: llvm/lib/Analysis/PostDominators.cpp
diff -u llvm/lib/Analysis/PostDominators.cpp:1.60 llvm/lib/Analysis/PostDominators.cpp:1.61
--- llvm/lib/Analysis/PostDominators.cpp:1.60	Thu Sep  7 18:29:19 2006
+++ llvm/lib/Analysis/PostDominators.cpp	Wed Sep 27 12:18:05 2006
@@ -28,36 +28,49 @@
 
 unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
                                           unsigned N) {
-
   std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
+  std::set<BasicBlock *> visited;
   workStack.push_back(std::make_pair(V, &VInfo));
 
   do {
     BasicBlock *currentBB = workStack.back().first; 
     InfoRec *currentVInfo = workStack.back().second;
-    workStack.pop_back();
 
-    currentVInfo->Semi = ++N;
-    currentVInfo->Label = currentBB;
+    // Visit each block only once.
+    if (visited.count(currentBB) == 0) {
 
-    Vertex.push_back(currentBB);  // Vertex[n] = current;
-                                  // Info[currentBB].Ancestor = 0;     
-                                  // Ancestor[n] = 0
-                                  // Child[currentBB] = 0;
-    currentVInfo->Size = 1;       // Size[currentBB] = 1
+      visited.insert(currentBB);
+      currentVInfo->Semi = ++N;
+      currentVInfo->Label = currentBB;
+      
+      Vertex.push_back(currentBB);  // Vertex[n] = current;
+      // Info[currentBB].Ancestor = 0;     
+      // Ancestor[n] = 0
+      // Child[currentBB] = 0;
+      currentVInfo->Size = 1;       // Size[currentBB] = 1
+    }
 
-    // For PostDominators, we want to walk predecessors rather than successors
-    // as we do in forward Dominators.
+    // Visit children
+    bool visitChild = false;
     for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB); 
-         PI != PE; ++PI) {
+         PI != PE && !visitChild; ++PI) {
       InfoRec &SuccVInfo = Info[*PI];
       if (SuccVInfo.Semi == 0) {
         SuccVInfo.Parent = currentBB;
-
-        workStack.push_back(std::make_pair(*PI, &SuccVInfo));   
+        if (visited.count (*PI) == 0) {
+          workStack.push_back(std::make_pair(*PI, &SuccVInfo));   
+          visitChild = true;
+        }
       }
     }
+
+    // If all children are visited or if this block has no child then pop this
+    // block out of workStack.
+    if (!visitChild)
+      workStack.pop_back();
+
   } while (!workStack.empty());
+
   return N;
 }
 






More information about the llvm-commits mailing list