[llvm-commits] [llvm] r93363 - /llvm/trunk/include/llvm/Support/CFG.h

Tobias Grosser grosser at fim.uni-passau.de
Wed Jan 13 14:21:28 PST 2010


Author: grosser
Date: Wed Jan 13 16:21:28 2010
New Revision: 93363

URL: http://llvm.org/viewvc/llvm-project?rev=93363&view=rev
Log:
Extend SuccIterator

Implement most of the missing methods to make SuccIterator random access.
operator[] is still missing.

Modified:
    llvm/trunk/include/llvm/Support/CFG.h

Modified: llvm/trunk/include/llvm/Support/CFG.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CFG.h?rev=93363&r1=93362&r2=93363&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/CFG.h (original)
+++ llvm/trunk/include/llvm/Support/CFG.h Wed Jan 13 16:21:28 2010
@@ -93,7 +93,7 @@
 public:
   typedef SuccIterator<Term_, BB_> _Self;
   typedef typename super::pointer pointer;
-  // TODO: This can be random access iterator, need operator+ and stuff tho
+  // TODO: This can be random access iterator, only operator[] missing.
 
   inline SuccIterator(Term_ T) : Term(T), idx(0) {         // begin iterator
     assert(T && "getTerminator returned null!");
@@ -109,6 +109,10 @@
     return *this;
   }
 
+  inline bool index_is_valid (int idx) {
+    return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
+  }
+
   /// getSuccessorIndex - This is used to interface between code that wants to
   /// operate on terminator instructions directly.
   unsigned getSuccessorIndex() const { return idx; }
@@ -120,6 +124,7 @@
   inline pointer operator->() const { return operator*(); }
 
   inline _Self& operator++() { ++idx; return *this; } // Preincrement
+
   inline _Self operator++(int) { // Postincrement
     _Self tmp = *this; ++*this; return tmp;
   }
@@ -128,6 +133,62 @@
   inline _Self operator--(int) { // Postdecrement
     _Self tmp = *this; --*this; return tmp;
   }
+
+  inline bool operator<(const _Self& x) const {
+    assert(Term == x.Term && "Cannot compare iterators of different blocks!");
+    return idx < x.idx;
+  }
+
+  inline bool operator<=(const _Self& x) const {
+    assert(Term == x.Term && "Cannot compare iterators of different blocks!");
+    return idx <= x.idx;
+  }
+  inline bool operator>=(const _Self& x) const {
+    assert(Term == x.Term && "Cannot compare iterators of different blocks!");
+    return idx >= x.idx;
+  }
+
+  inline bool operator>(const _Self& x) const {
+    return idx > x.idx;
+    assert(Term == x.Term && "Cannot compare iterators of different blocks!");
+  }
+
+  inline _Self& operator+=(int Right) {
+    unsigned new_idx = idx + Right;
+    assert(index_is_valid(new_idx) && "Iterator index out of bound");
+    idx = new_idx;
+    return *this;
+  }
+
+  inline _Self operator+(int Right) {
+    _Self tmp = *this;
+    tmp += Right;
+    return tmp;
+  }
+
+  inline _Self& operator-=(int Right) {
+    return operator+=(-Right);
+  }
+
+  inline _Self operator-(int Right) {
+    return operator+(-Right);
+  }
+
+  inline int operator-(const _Self& x) {
+    assert(Term == x.Term && "Cannot work on iterators of different blocks!");
+    int distance = idx - x.idx;
+    return distance;
+  }
+
+  // This works for read access, however write access is difficult as changes
+  // to Term are only possible with Term->setSuccessor(idx). Pointers that can
+  // be modified are not available.
+  //
+  // inline pointer operator[](int offset) {
+  //  _Self tmp = *this;
+  //  tmp += offset;
+  //  return tmp.operator*();
+  // }
 };
 
 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;





More information about the llvm-commits mailing list