Extricate the "reverse" support from the depth-first iterator. This is really
authorChris Lattner <sabre@nondot.org>
Mon, 13 Oct 2003 15:45:33 +0000 (15:45 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 13 Oct 2003 15:45:33 +0000 (15:45 +0000)
a crappy form of post-order traversal which really does not belong here.  While
we are at it, improve documentation and use a vector instead of a stack.

This improves the post dominator analysis pass by ~5%, and probably also helps
other passes as well.

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

include/Support/DepthFirstIterator.h
include/llvm/ADT/DepthFirstIterator.h

index 967a0e145ac2ba8c1c284643a5241b3c54831033..10ef3f378c86621a24c4abc1624a05e13d330c04 100644 (file)
@@ -1,7 +1,13 @@
 //===- Support/DepthFirstIterator.h - Depth First iterator ------*- C++ -*-===//
 //
 // This file builds on the Support/GraphTraits.h file to build generic depth
-// first graph iterator.
+// first graph iterator.  This file exposes the following functions/types:
+//
+// df_begin/df_end/df_iterator
+//   * Normal depth-first iteration - visit a node and then all of its children.
+//
+// idf_begin/idf_end/idf_iterator
+//   * Depth-first iteration on the 'inverse' graph.
 //
 //===----------------------------------------------------------------------===//
 
@@ -10,7 +16,7 @@
 
 #include "Support/GraphTraits.h"
 #include "Support/iterator"
-#include <stack>
+#include <vector>
 #include <set>
 
 // Generic Depth First Iterator
@@ -24,28 +30,11 @@ class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
   std::set<NodeType *> Visited;    // All of the blocks visited so far...
   // VisitStack - Used to maintain the ordering.  Top = current block
   // First element is node pointer, second is the 'next child' to visit
-  std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
-  const bool Reverse;         // Iterate over children before self?
+  std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
 private:
-  void reverseEnterNode() {
-    std::pair<NodeType *, ChildItTy> &Top = VisitStack.top();
-    NodeType *Node = Top.first;
-    ChildItTy &It  = Top.second;
-    for (; It != GT::child_end(Node); ++It) {
-      NodeType *Child = *It;
-      if (!Visited.count(Child)) {
-       Visited.insert(Child);
-       VisitStack.push(std::make_pair(Child, GT::child_begin(Child)));
-       reverseEnterNode();
-       return;
-      }
-    }
-  }
-
-  inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) {
+  inline df_iterator(NodeType *Node) {
     Visited.insert(Node);
-    VisitStack.push(std::make_pair(Node, GT::child_begin(Node)));
-    if (Reverse) reverseEnterNode();
+    VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
   }
   inline df_iterator() { /* End is when stack is empty */ }
 
@@ -54,19 +43,20 @@ public:
   typedef df_iterator<GraphT, GT> _Self;
 
   // Provide static begin and end methods as our public "constructors"
-  static inline _Self begin(GraphT G, bool Reverse = false) {
-    return _Self(GT::getEntryNode(G), Reverse);
+  static inline _Self begin(GraphT G) {
+    return _Self(GT::getEntryNode(G));
   }
   static inline _Self end(GraphT G) { return _Self(); }
 
 
   inline bool operator==(const _Self& x) const { 
-    return VisitStack == x.VisitStack;
+    return VisitStack.size() == x.VisitStack.size() &&
+           VisitStack == x.VisitStack;
   }
   inline bool operator!=(const _Self& x) const { return !operator==(x); }
 
   inline pointer operator*() const { 
-    return VisitStack.top().first;
+    return VisitStack.back().first;
   }
 
   // This is a nonstandard operator-> that dereferences the pointer an extra
@@ -76,31 +66,24 @@ public:
   inline NodeType *operator->() const { return operator*(); }
 
   inline _Self& operator++() {   // Preincrement
-    if (Reverse) {               // Reverse Depth First Iterator
-      if (VisitStack.top().second == GT::child_end(VisitStack.top().first))
-       VisitStack.pop();
-      if (!VisitStack.empty())
-       reverseEnterNode();
-    } else {                     // Normal Depth First Iterator
-      do {
-       std::pair<NodeType *, ChildItTy> &Top = VisitStack.top();
-       NodeType *Node = Top.first;
-       ChildItTy &It  = Top.second;
-
-       while (It != GT::child_end(Node)) {
-         NodeType *Next = *It++;
-         if (!Visited.count(Next)) {  // Has our next sibling been visited?
-           // No, do it now.
-           Visited.insert(Next);
-           VisitStack.push(std::make_pair(Next, GT::child_begin(Next)));
-           return *this;
-         }
-       }
-       
-       // Oops, ran out of successors... go up a level on the stack.
-       VisitStack.pop();
-      } while (!VisitStack.empty());
-    }
+    do {
+      std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
+      NodeType *Node = Top.first;
+      ChildItTy &It  = Top.second;
+      
+      while (It != GT::child_end(Node)) {
+        NodeType *Next = *It++;
+        if (!Visited.count(Next)) {  // Has our next sibling been visited?
+          // No, do it now.
+          Visited.insert(Next);
+          VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
+          return *this;
+        }
+      }
+      
+      // Oops, ran out of successors... go up a level on the stack.
+      VisitStack.pop_back();
+    } while (!VisitStack.empty());
     return *this; 
   }
 
@@ -121,8 +104,8 @@ public:
 // Provide global constructors that automatically figure out correct types...
 //
 template <class T>
-df_iterator<T> df_begin(T G, bool Reverse = false) {
-  return df_iterator<T>::begin(G, Reverse);
+df_iterator<T> df_begin(T G) {
+  return df_iterator<T>::begin(G);
 }
 
 template <class T>
@@ -137,8 +120,8 @@ struct idf_iterator : public df_iterator<Inverse<T> > {
 };
 
 template <class T>
-idf_iterator<T> idf_begin(T G, bool Reverse = false) {
-  return idf_iterator<T>::begin(G, Reverse);
+idf_iterator<T> idf_begin(T G) {
+  return idf_iterator<T>::begin(G);
 }
 
 template <class T>
index 967a0e145ac2ba8c1c284643a5241b3c54831033..10ef3f378c86621a24c4abc1624a05e13d330c04 100644 (file)
@@ -1,7 +1,13 @@
 //===- Support/DepthFirstIterator.h - Depth First iterator ------*- C++ -*-===//
 //
 // This file builds on the Support/GraphTraits.h file to build generic depth
-// first graph iterator.
+// first graph iterator.  This file exposes the following functions/types:
+//
+// df_begin/df_end/df_iterator
+//   * Normal depth-first iteration - visit a node and then all of its children.
+//
+// idf_begin/idf_end/idf_iterator
+//   * Depth-first iteration on the 'inverse' graph.
 //
 //===----------------------------------------------------------------------===//
 
@@ -10,7 +16,7 @@
 
 #include "Support/GraphTraits.h"
 #include "Support/iterator"
-#include <stack>
+#include <vector>
 #include <set>
 
 // Generic Depth First Iterator
@@ -24,28 +30,11 @@ class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
   std::set<NodeType *> Visited;    // All of the blocks visited so far...
   // VisitStack - Used to maintain the ordering.  Top = current block
   // First element is node pointer, second is the 'next child' to visit
-  std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
-  const bool Reverse;         // Iterate over children before self?
+  std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
 private:
-  void reverseEnterNode() {
-    std::pair<NodeType *, ChildItTy> &Top = VisitStack.top();
-    NodeType *Node = Top.first;
-    ChildItTy &It  = Top.second;
-    for (; It != GT::child_end(Node); ++It) {
-      NodeType *Child = *It;
-      if (!Visited.count(Child)) {
-       Visited.insert(Child);
-       VisitStack.push(std::make_pair(Child, GT::child_begin(Child)));
-       reverseEnterNode();
-       return;
-      }
-    }
-  }
-
-  inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) {
+  inline df_iterator(NodeType *Node) {
     Visited.insert(Node);
-    VisitStack.push(std::make_pair(Node, GT::child_begin(Node)));
-    if (Reverse) reverseEnterNode();
+    VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
   }
   inline df_iterator() { /* End is when stack is empty */ }
 
@@ -54,19 +43,20 @@ public:
   typedef df_iterator<GraphT, GT> _Self;
 
   // Provide static begin and end methods as our public "constructors"
-  static inline _Self begin(GraphT G, bool Reverse = false) {
-    return _Self(GT::getEntryNode(G), Reverse);
+  static inline _Self begin(GraphT G) {
+    return _Self(GT::getEntryNode(G));
   }
   static inline _Self end(GraphT G) { return _Self(); }
 
 
   inline bool operator==(const _Self& x) const { 
-    return VisitStack == x.VisitStack;
+    return VisitStack.size() == x.VisitStack.size() &&
+           VisitStack == x.VisitStack;
   }
   inline bool operator!=(const _Self& x) const { return !operator==(x); }
 
   inline pointer operator*() const { 
-    return VisitStack.top().first;
+    return VisitStack.back().first;
   }
 
   // This is a nonstandard operator-> that dereferences the pointer an extra
@@ -76,31 +66,24 @@ public:
   inline NodeType *operator->() const { return operator*(); }
 
   inline _Self& operator++() {   // Preincrement
-    if (Reverse) {               // Reverse Depth First Iterator
-      if (VisitStack.top().second == GT::child_end(VisitStack.top().first))
-       VisitStack.pop();
-      if (!VisitStack.empty())
-       reverseEnterNode();
-    } else {                     // Normal Depth First Iterator
-      do {
-       std::pair<NodeType *, ChildItTy> &Top = VisitStack.top();
-       NodeType *Node = Top.first;
-       ChildItTy &It  = Top.second;
-
-       while (It != GT::child_end(Node)) {
-         NodeType *Next = *It++;
-         if (!Visited.count(Next)) {  // Has our next sibling been visited?
-           // No, do it now.
-           Visited.insert(Next);
-           VisitStack.push(std::make_pair(Next, GT::child_begin(Next)));
-           return *this;
-         }
-       }
-       
-       // Oops, ran out of successors... go up a level on the stack.
-       VisitStack.pop();
-      } while (!VisitStack.empty());
-    }
+    do {
+      std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
+      NodeType *Node = Top.first;
+      ChildItTy &It  = Top.second;
+      
+      while (It != GT::child_end(Node)) {
+        NodeType *Next = *It++;
+        if (!Visited.count(Next)) {  // Has our next sibling been visited?
+          // No, do it now.
+          Visited.insert(Next);
+          VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
+          return *this;
+        }
+      }
+      
+      // Oops, ran out of successors... go up a level on the stack.
+      VisitStack.pop_back();
+    } while (!VisitStack.empty());
     return *this; 
   }
 
@@ -121,8 +104,8 @@ public:
 // Provide global constructors that automatically figure out correct types...
 //
 template <class T>
-df_iterator<T> df_begin(T G, bool Reverse = false) {
-  return df_iterator<T>::begin(G, Reverse);
+df_iterator<T> df_begin(T G) {
+  return df_iterator<T>::begin(G);
 }
 
 template <class T>
@@ -137,8 +120,8 @@ struct idf_iterator : public df_iterator<Inverse<T> > {
 };
 
 template <class T>
-idf_iterator<T> idf_begin(T G, bool Reverse = false) {
-  return idf_iterator<T>::begin(G, Reverse);
+idf_iterator<T> idf_begin(T G) {
+  return idf_iterator<T>::begin(G);
 }
 
 template <class T>