Avoid recursion in assignDFSNumber(). Move def from ET-Forest.h
authorDevang Patel <dpatel@apple.com>
Thu, 14 Sep 2006 01:27:42 +0000 (01:27 +0000)
committerDevang Patel <dpatel@apple.com>
Thu, 14 Sep 2006 01:27:42 +0000 (01:27 +0000)
to Dominators.h

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

include/llvm/Analysis/ET-Forest.h
lib/VMCore/Dominators.cpp

index b05776a9e0f1b3aaccf24d200e7abd69839c5e28..ef2b2155c729e3f5582ab9d249c6dbb98015653d 100644 (file)
@@ -250,16 +250,7 @@ public:
     return this->Below(other);
   }
 
-  void assignDFSNumber(int &num) {
-    DFSNumIn = num++;
-    
-    if (Son) {
-      Son->assignDFSNumber(num);
-      for (ETNode *son = Son->Right; son != Son; son = son->Right)
-        son->assignDFSNumber(num);
-    }
-    DFSNumOut = num++;
-  }
+  void assignDFSNumber(int &);
   
   bool hasFather() const {
     return Father != NULL;
index 9f7e5d9365d11e702866b6472423146f312e7d00..fd193b8d7aa402b39dced6aa7212a4f00c01a8ed 100644 (file)
@@ -890,6 +890,39 @@ void ETForest::calculate(const ImmediateDominators &ID) {
   updateDFSNumbers ();
 }
 
+// Walk ETNode and its children using DFS algorithm and assign
+// DFSNumIn and DFSNumOut numbers for each node. 
+void ETNode::assignDFSNumber(int &num) {
+
+    std::vector<ETNode *> DFSInStack;
+    std::set<ETNode *> visited;
+
+    DFSInStack.push_back(this);
+
+    visited.insert(this);
+
+    while(!DFSInStack.empty()) {
+      ETNode *Parent = DFSInStack.back();
+      DFSInStack.pop_back();
+      Parent->DFSNumIn = num++;
+      Parent->DFSNumOut = Parent->DFSNumIn + 1;
+
+      ETNode *son = Parent->Son;
+      if (son && visited.count(son) == 0) {
+
+        DFSInStack.push_back(son);
+        son->DFSNumIn = Parent->DFSNumIn + 1;
+        visited.insert(son);
+        
+        for (ETNode *s = son->Right; s != son; s = s->Right) {
+          DFSInStack.push_back(s);
+          s->DFSNumIn = Parent->DFSNumIn + 1;
+          visited.insert(s);
+        }
+      }
+    }
+}
+
 //===----------------------------------------------------------------------===//
 // ETForestBase Implementation
 //===----------------------------------------------------------------------===//