llvm.org GIT mirror llvm / cc179d3
* Pull BasicBlock::pred_* and BasicBlock::succ_* out of BasicBlock.h and into llvm/Support/CFG.h git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1749 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 18 years ago
1 changed file(s) with 141 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
1010 #include "Support/GraphTraits.h"
1111 #include "llvm/Method.h"
1212 #include "llvm/BasicBlock.h"
13 #include "llvm/InstrTypes.h"
14 #include
15
16 //===--------------------------------------------------------------------===//
17 // BasicBlock pred_iterator definition
18 //===--------------------------------------------------------------------===//
19
20 template // Predecessor Iterator
21 class PredIterator : public std::bidirectional_iterator<_Ptr, ptrdiff_t> {
22 _Ptr *BB;
23 _USE_iterator It;
24 public:
25 typedef PredIterator<_Ptr,_USE_iterator> _Self;
26
27 inline void advancePastConstants() {
28 // TODO: This is bad
29 // Loop to ignore constant pool references
30 while (It != BB->use_end() && !isa(*It))
31 ++It;
32 }
33
34 inline PredIterator(_Ptr *bb) : BB(bb), It(bb->use_begin()) {
35 advancePastConstants();
36 }
37 inline PredIterator(_Ptr *bb, bool) : BB(bb), It(bb->use_end()) {}
38
39 inline bool operator==(const _Self& x) const { return It == x.It; }
40 inline bool operator!=(const _Self& x) const { return !operator==(x); }
41
42 inline pointer operator*() const {
43 assert(It != BB->use_end() && "pred_iterator out of range!");
44 return cast(*It)->getParent();
45 }
46 inline pointer *operator->() const { return &(operator*()); }
47
48 inline _Self& operator++() { // Preincrement
49 assert(It != BB->use_end() && "pred_iterator out of range!");
50 ++It; advancePastConstants();
51 return *this;
52 }
53
54 inline _Self operator++(int) { // Postincrement
55 _Self tmp = *this; ++*this; return tmp;
56 }
57
58 inline _Self& operator--() { --It; return *this; } // Predecrement
59 inline _Self operator--(int) { // Postdecrement
60 _Self tmp = *this; --*this; return tmp;
61 }
62 };
63
64 typedef PredIterator pred_iterator;
65 typedef PredIterator
66 Value::use_const_iterator> pred_const_iterator;
67
68 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
69 inline pred_const_iterator pred_begin(const BasicBlock *BB) {
70 return pred_const_iterator(BB);
71 }
72 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
73 inline pred_const_iterator pred_end(const BasicBlock *BB) {
74 return pred_const_iterator(BB, true);
75 }
76
77
78
79 //===--------------------------------------------------------------------===//
80 // BasicBlock succ_iterator definition
81 //===--------------------------------------------------------------------===//
82
83 template // Successor Iterator
84 class SuccIterator : public std::bidirectional_iterator<_BB, ptrdiff_t> {
85 const _Term Term;
86 unsigned idx;
87 public:
88 typedef SuccIterator<_Term, _BB> _Self;
89 // TODO: This can be random access iterator, need operator+ and stuff tho
90
91 inline SuccIterator(_Term T) : Term(T), idx(0) { // begin iterator
92 assert(T && "getTerminator returned null!");
93 }
94 inline SuccIterator(_Term T, bool) // end iterator
95 : Term(T), idx(Term->getNumSuccessors()) {
96 assert(T && "getTerminator returned null!");
97 }
98
99 inline bool operator==(const _Self& x) const { return idx == x.idx; }
100 inline bool operator!=(const _Self& x) const { return !operator==(x); }
101
102 inline pointer operator*() const { return Term->getSuccessor(idx); }
103 inline pointer operator->() const { return operator*(); }
104
105 inline _Self& operator++() { ++idx; return *this; } // Preincrement
106 inline _Self operator++(int) { // Postincrement
107 _Self tmp = *this; ++*this; return tmp;
108 }
109
110 inline _Self& operator--() { --idx; return *this; } // Predecrement
111 inline _Self operator--(int) { // Postdecrement
112 _Self tmp = *this; --*this; return tmp;
113 }
114 };
115
116 typedef SuccIterator succ_iterator;
117 typedef SuccIterator
118 const BasicBlock> succ_const_iterator;
119
120 inline succ_iterator succ_begin(BasicBlock *BB) {
121 return succ_iterator(BB->getTerminator());
122 }
123 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
124 return succ_const_iterator(BB->getTerminator());
125 }
126 inline succ_iterator succ_end(BasicBlock *BB) {
127 return succ_iterator(BB->getTerminator(), true);
128 }
129 inline succ_const_iterator succ_end(const BasicBlock *BB) {
130 return succ_const_iterator(BB->getTerminator(), true);
131 }
132
133
13134
14135 //===--------------------------------------------------------------------===//
15136 // GraphTraits specializations for basic block graphs (CFGs)
20141
21142 template <> struct GraphTraits {
22143 typedef BasicBlock NodeType;
23 typedef BasicBlock::succ_iterator ChildIteratorType;
144 typedef succ_iterator ChildIteratorType;
24145
25146 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
26147 static inline ChildIteratorType child_begin(NodeType *N) {
27 return N->succ_begin();
28 }
29 static inline ChildIteratorType child_end(NodeType *N) {
30 return N->succ_end();
148 return succ_begin(N);
149 }
150 static inline ChildIteratorType child_end(NodeType *N) {
151 return succ_end(N);
31152 }
32153 };
33154
34155 template <> struct GraphTraits {
35156 typedef const BasicBlock NodeType;
36 typedef BasicBlock::succ_const_iterator ChildIteratorType;
157 typedef succ_const_iterator ChildIteratorType;
37158
38159 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
39160
40161 static inline ChildIteratorType child_begin(NodeType *N) {
41 return N->succ_begin();
42 }
43 static inline ChildIteratorType child_end(NodeType *N) {
44 return N->succ_end();
162 return succ_begin(N);
163 }
164 static inline ChildIteratorType child_end(NodeType *N) {
165 return succ_end(N);
45166 }
46167 };
47168
52173 //
53174 template <> struct GraphTraits > {
54175 typedef BasicBlock NodeType;
55 typedef BasicBlock::pred_iterator ChildIteratorType;
176 typedef pred_iterator ChildIteratorType;
56177 static NodeType *getEntryNode(Inverse G) { return G.Graph; }
57178 static inline ChildIteratorType child_begin(NodeType *N) {
58 return N->pred_begin();
59 }
60 static inline ChildIteratorType child_end(NodeType *N) {
61 return N->pred_end();
179 return pred_begin(N);
180 }
181 static inline ChildIteratorType child_end(NodeType *N) {
182 return pred_end(N);
62183 }
63184 };
64185
65186 template <> struct GraphTraits > {
66187 typedef const BasicBlock NodeType;
67 typedef BasicBlock::pred_const_iterator ChildIteratorType;
188 typedef pred_const_iterator ChildIteratorType;
68189 static NodeType *getEntryNode(Inverse G) {
69190 return G.Graph;
70191 }
71192 static inline ChildIteratorType child_begin(NodeType *N) {
72 return N->pred_begin();
73 }
74 static inline ChildIteratorType child_end(NodeType *N) {
75 return N->pred_end();
193 return pred_begin(N);
194 }
195 static inline ChildIteratorType child_end(NodeType *N) {
196 return pred_end(N);
76197 }
77198 };
78199