* Allow access to DSNode iterator as DSNode::iterator/begin/end
authorChris Lattner <sabre@nondot.org>
Sun, 31 Mar 2002 07:11:20 +0000 (07:11 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 31 Mar 2002 07:11:20 +0000 (07:11 +0000)
* Add debugging "dump" method to DSNode
* Fix bugs in DSNode iterator

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

include/llvm/Analysis/DSGraphTraits.h
include/llvm/Analysis/DataStructure.h
include/llvm/Analysis/DataStructure/DSGraphTraits.h
include/llvm/Analysis/DataStructure/DataStructure.h
include/llvm/Analysis/DataStructureGraph.h

index 67989697504e531d4788f1c7f5410543474cb014..3b6ff744c769e0dbb25bde43659e774b29370658 100644 (file)
 
 #include "Support/GraphTraits.h"
 #include "llvm/Analysis/DataStructure.h"
-#include "llvm/Value.h"  // FIXME: Move cast/dyn_cast out to Support
 
 class DSNodeIterator : public std::forward_iterator<DSNode, ptrdiff_t> {
+  friend class DSNode;
   DSNode * const Node;
   unsigned Link;
   unsigned LinkIdx;
   
   typedef DSNodeIterator _Self;
-public:
-  DSNodeIterator(DSNode *N) : Node(N), Link(0), LinkIdx(0) {}  // begin iterator
+
+  DSNodeIterator(DSNode *N) : Node(N), Link(0), LinkIdx(0) {   // begin iterator
+    unsigned NumLinks = Node->getNumOutgoingLinks();
+    while (Link < NumLinks && Node->getOutgoingLink(Link).empty())
+      ++Link;
+  }
   DSNodeIterator(DSNode *N, bool)       // Create end iterator
     : Node(N), Link(N->getNumOutgoingLinks()), LinkIdx(0) {
   }
+public:
 
   bool operator==(const _Self& x) const {
     return Link == x.Link && LinkIdx == x.LinkIdx;
@@ -39,7 +44,10 @@ public:
     if (LinkIdx < Node->getOutgoingLink(Link).size()-1)
       ++LinkIdx;
     else {
-      ++Link;
+      unsigned NumLinks = Node->getNumOutgoingLinks();
+      do {
+        ++Link;
+      } while (Link < NumLinks && Node->getOutgoingLink(Link).empty());
       LinkIdx = 0;
     }
     return *this;
@@ -52,16 +60,15 @@ public:
 
 template <> struct GraphTraits<DSNode*> {
   typedef DSNode NodeType;
-  typedef DSNodeIterator ChildIteratorType;
+  typedef DSNode::iterator ChildIteratorType;
 
   static NodeType *getEntryNode(DSNode *N) { return N; }
-  static ChildIteratorType child_begin(NodeType *N) { 
-    return DSNodeIterator(N);
-  }
-  static ChildIteratorType child_end(NodeType *N) { 
-    return DSNodeIterator(N, true);
-  }
+  static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
+  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
 };
 
+// Provide iterators for DSNode...
+inline DSNode::iterator DSNode::begin() { return DSNodeIterator(this); }
+inline DSNode::iterator DSNode::end()   { return DSNodeIterator(this, false); }
 
 #endif
index 64cd3fc566a5066558c3228f5c47983836b98b64..edb0ac5d10903ba88da4a175aff63890bc2724af 100644 (file)
@@ -19,6 +19,7 @@ class FunctionRepBuilder;
 class GlobalValue;
 class FunctionDSGraph;
 class DataStructure;
+class DSNodeIterator;
 
 // FIXME: move this somewhere private
 unsigned countPointerFields(const Type *Ty);
@@ -126,6 +127,10 @@ public:
     assert(Referrers.empty() && "Referrers to dead node exist!");
   }
 
+  typedef DSNodeIterator iterator;
+  inline iterator begin();   // Defined in DataStructureGraph.h
+  inline iterator end();
+
   unsigned getNumLinks() const { return FieldLinks.size(); }
   PointerValSet &getLink(unsigned i) {
     assert(i < getNumLinks() && "Field links access out of range...");
@@ -163,6 +168,7 @@ public:
   }
 
   void print(std::ostream &O) const;
+  void dump() const;
 
   virtual std::string getCaption() const = 0;
   virtual const std::vector<PointerValSet> *getAuxLinks() const {
index 67989697504e531d4788f1c7f5410543474cb014..3b6ff744c769e0dbb25bde43659e774b29370658 100644 (file)
 
 #include "Support/GraphTraits.h"
 #include "llvm/Analysis/DataStructure.h"
-#include "llvm/Value.h"  // FIXME: Move cast/dyn_cast out to Support
 
 class DSNodeIterator : public std::forward_iterator<DSNode, ptrdiff_t> {
+  friend class DSNode;
   DSNode * const Node;
   unsigned Link;
   unsigned LinkIdx;
   
   typedef DSNodeIterator _Self;
-public:
-  DSNodeIterator(DSNode *N) : Node(N), Link(0), LinkIdx(0) {}  // begin iterator
+
+  DSNodeIterator(DSNode *N) : Node(N), Link(0), LinkIdx(0) {   // begin iterator
+    unsigned NumLinks = Node->getNumOutgoingLinks();
+    while (Link < NumLinks && Node->getOutgoingLink(Link).empty())
+      ++Link;
+  }
   DSNodeIterator(DSNode *N, bool)       // Create end iterator
     : Node(N), Link(N->getNumOutgoingLinks()), LinkIdx(0) {
   }
+public:
 
   bool operator==(const _Self& x) const {
     return Link == x.Link && LinkIdx == x.LinkIdx;
@@ -39,7 +44,10 @@ public:
     if (LinkIdx < Node->getOutgoingLink(Link).size()-1)
       ++LinkIdx;
     else {
-      ++Link;
+      unsigned NumLinks = Node->getNumOutgoingLinks();
+      do {
+        ++Link;
+      } while (Link < NumLinks && Node->getOutgoingLink(Link).empty());
       LinkIdx = 0;
     }
     return *this;
@@ -52,16 +60,15 @@ public:
 
 template <> struct GraphTraits<DSNode*> {
   typedef DSNode NodeType;
-  typedef DSNodeIterator ChildIteratorType;
+  typedef DSNode::iterator ChildIteratorType;
 
   static NodeType *getEntryNode(DSNode *N) { return N; }
-  static ChildIteratorType child_begin(NodeType *N) { 
-    return DSNodeIterator(N);
-  }
-  static ChildIteratorType child_end(NodeType *N) { 
-    return DSNodeIterator(N, true);
-  }
+  static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
+  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
 };
 
+// Provide iterators for DSNode...
+inline DSNode::iterator DSNode::begin() { return DSNodeIterator(this); }
+inline DSNode::iterator DSNode::end()   { return DSNodeIterator(this, false); }
 
 #endif
index 64cd3fc566a5066558c3228f5c47983836b98b64..edb0ac5d10903ba88da4a175aff63890bc2724af 100644 (file)
@@ -19,6 +19,7 @@ class FunctionRepBuilder;
 class GlobalValue;
 class FunctionDSGraph;
 class DataStructure;
+class DSNodeIterator;
 
 // FIXME: move this somewhere private
 unsigned countPointerFields(const Type *Ty);
@@ -126,6 +127,10 @@ public:
     assert(Referrers.empty() && "Referrers to dead node exist!");
   }
 
+  typedef DSNodeIterator iterator;
+  inline iterator begin();   // Defined in DataStructureGraph.h
+  inline iterator end();
+
   unsigned getNumLinks() const { return FieldLinks.size(); }
   PointerValSet &getLink(unsigned i) {
     assert(i < getNumLinks() && "Field links access out of range...");
@@ -163,6 +168,7 @@ public:
   }
 
   void print(std::ostream &O) const;
+  void dump() const;
 
   virtual std::string getCaption() const = 0;
   virtual const std::vector<PointerValSet> *getAuxLinks() const {
index 67989697504e531d4788f1c7f5410543474cb014..3b6ff744c769e0dbb25bde43659e774b29370658 100644 (file)
 
 #include "Support/GraphTraits.h"
 #include "llvm/Analysis/DataStructure.h"
-#include "llvm/Value.h"  // FIXME: Move cast/dyn_cast out to Support
 
 class DSNodeIterator : public std::forward_iterator<DSNode, ptrdiff_t> {
+  friend class DSNode;
   DSNode * const Node;
   unsigned Link;
   unsigned LinkIdx;
   
   typedef DSNodeIterator _Self;
-public:
-  DSNodeIterator(DSNode *N) : Node(N), Link(0), LinkIdx(0) {}  // begin iterator
+
+  DSNodeIterator(DSNode *N) : Node(N), Link(0), LinkIdx(0) {   // begin iterator
+    unsigned NumLinks = Node->getNumOutgoingLinks();
+    while (Link < NumLinks && Node->getOutgoingLink(Link).empty())
+      ++Link;
+  }
   DSNodeIterator(DSNode *N, bool)       // Create end iterator
     : Node(N), Link(N->getNumOutgoingLinks()), LinkIdx(0) {
   }
+public:
 
   bool operator==(const _Self& x) const {
     return Link == x.Link && LinkIdx == x.LinkIdx;
@@ -39,7 +44,10 @@ public:
     if (LinkIdx < Node->getOutgoingLink(Link).size()-1)
       ++LinkIdx;
     else {
-      ++Link;
+      unsigned NumLinks = Node->getNumOutgoingLinks();
+      do {
+        ++Link;
+      } while (Link < NumLinks && Node->getOutgoingLink(Link).empty());
       LinkIdx = 0;
     }
     return *this;
@@ -52,16 +60,15 @@ public:
 
 template <> struct GraphTraits<DSNode*> {
   typedef DSNode NodeType;
-  typedef DSNodeIterator ChildIteratorType;
+  typedef DSNode::iterator ChildIteratorType;
 
   static NodeType *getEntryNode(DSNode *N) { return N; }
-  static ChildIteratorType child_begin(NodeType *N) { 
-    return DSNodeIterator(N);
-  }
-  static ChildIteratorType child_end(NodeType *N) { 
-    return DSNodeIterator(N, true);
-  }
+  static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
+  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
 };
 
+// Provide iterators for DSNode...
+inline DSNode::iterator DSNode::begin() { return DSNodeIterator(this); }
+inline DSNode::iterator DSNode::end()   { return DSNodeIterator(this, false); }
 
 #endif