Use iterative do-while loop instead of recursive DFSPass calls to
authorDevang Patel <dpatel@apple.com>
Thu, 7 Sep 2006 23:22:37 +0000 (23:22 +0000)
committerDevang Patel <dpatel@apple.com>
Thu, 7 Sep 2006 23:22:37 +0000 (23:22 +0000)
reduce amount of stack space used at runtime.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30167 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/PostDominators.cpp

index ec7f7c75466434803f4fca9e2861539e838a777c..b0b2374317dbd2716890fc2a8d570329ab2ba1aa 100644 (file)
@@ -28,23 +28,36 @@ D("postidom", "Immediate Post-Dominators Construction", true);
 
 unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
                                           unsigned N) {
-  VInfo.Semi = ++N;
-  VInfo.Label = V;
-  
-  Vertex.push_back(V);        // Vertex[n] = V;
-                              //Info[V].Ancestor = 0;     // Ancestor[n] = 0
-                              //Child[V] = 0;             // Child[v] = 0
-  VInfo.Size = 1;             // Size[v] = 1
-  
-  // For PostDominators, we want to walk predecessors rather than successors
-  // as we do in forward Dominators.
-  for (pred_iterator PI = pred_begin(V), PE = pred_end(V); PI != PE; ++PI) {
-    InfoRec &SuccVInfo = Info[*PI];
-    if (SuccVInfo.Semi == 0) {
-      SuccVInfo.Parent = V;
-      N = DFSPass(*PI, SuccVInfo, N);
+
+  std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
+  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;
+
+    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.
+    for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB); 
+        PI != PE; ++PI) {
+      InfoRec &SuccVInfo = Info[*PI];
+      if (SuccVInfo.Semi == 0) {
+       SuccVInfo.Parent = currentBB;
+
+       workStack.push_back(std::make_pair(*PI, &SuccVInfo));   
+      }
     }
-  }
+  } while (!workStack.empty());
   return N;
 }