1 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines specializations of GraphTraits that allow Function and
11 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CFG_H
16 #define LLVM_SUPPORT_CFG_H
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/Function.h"
20 #include "llvm/InstrTypes.h"
24 //===----------------------------------------------------------------------===//
25 // BasicBlock pred_iterator definition
26 //===----------------------------------------------------------------------===//
28 template <class _Ptr, class _USE_iterator> // Predecessor Iterator
29 class PredIterator : public std::iterator<std::forward_iterator_tag,
31 typedef std::iterator<std::forward_iterator_tag, _Ptr, ptrdiff_t> super;
34 typedef PredIterator<_Ptr,_USE_iterator> _Self;
35 typedef typename super::pointer pointer;
37 inline void advancePastNonTerminators() {
38 // Loop to ignore non terminator uses (for example PHI nodes)...
39 while (!It.atEnd() && !isa<TerminatorInst>(*It))
43 inline PredIterator(_Ptr *bb) : It(bb->use_begin()) {
44 advancePastNonTerminators();
46 inline PredIterator(_Ptr *bb, bool) : It(bb->use_end()) {}
48 inline bool operator==(const _Self& x) const { return It == x.It; }
49 inline bool operator!=(const _Self& x) const { return !operator==(x); }
51 inline pointer operator*() const {
52 assert(!It.atEnd() && "pred_iterator out of range!");
53 return cast<TerminatorInst>(*It)->getParent();
55 inline pointer *operator->() const { return &(operator*()); }
57 inline _Self& operator++() { // Preincrement
58 assert(!It.atEnd() && "pred_iterator out of range!");
59 ++It; advancePastNonTerminators();
63 inline _Self operator++(int) { // Postincrement
64 _Self tmp = *this; ++*this; return tmp;
68 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
69 typedef PredIterator<const BasicBlock,
70 Value::use_const_iterator> pred_const_iterator;
72 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
73 inline pred_const_iterator pred_begin(const BasicBlock *BB) {
74 return pred_const_iterator(BB);
76 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
77 inline pred_const_iterator pred_end(const BasicBlock *BB) {
78 return pred_const_iterator(BB, true);
83 //===----------------------------------------------------------------------===//
84 // BasicBlock succ_iterator definition
85 //===----------------------------------------------------------------------===//
87 template <class Term_, class BB_> // Successor Iterator
88 class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
92 typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t> super;
94 typedef SuccIterator<Term_, BB_> _Self;
95 typedef typename super::pointer pointer;
96 // TODO: This can be random access iterator, only operator[] missing.
98 inline SuccIterator(Term_ T) : Term(T), idx(0) { // begin iterator
99 assert(T && "getTerminator returned null!");
101 inline SuccIterator(Term_ T, bool) // end iterator
102 : Term(T), idx(Term->getNumSuccessors()) {
103 assert(T && "getTerminator returned null!");
106 inline const _Self &operator=(const _Self &I) {
107 assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
112 inline bool index_is_valid (int idx) {
113 return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
116 /// getSuccessorIndex - This is used to interface between code that wants to
117 /// operate on terminator instructions directly.
118 unsigned getSuccessorIndex() const { return idx; }
120 inline bool operator==(const _Self& x) const { return idx == x.idx; }
121 inline bool operator!=(const _Self& x) const { return !operator==(x); }
123 inline pointer operator*() const { return Term->getSuccessor(idx); }
124 inline pointer operator->() const { return operator*(); }
126 inline _Self& operator++() { ++idx; return *this; } // Preincrement
128 inline _Self operator++(int) { // Postincrement
129 _Self tmp = *this; ++*this; return tmp;
132 inline _Self& operator--() { --idx; return *this; } // Predecrement
133 inline _Self operator--(int) { // Postdecrement
134 _Self tmp = *this; --*this; return tmp;
137 inline bool operator<(const _Self& x) const {
138 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
142 inline bool operator<=(const _Self& x) const {
143 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
146 inline bool operator>=(const _Self& x) const {
147 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
151 inline bool operator>(const _Self& x) const {
153 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
156 inline _Self& operator+=(int Right) {
157 unsigned new_idx = idx + Right;
158 assert(index_is_valid(new_idx) && "Iterator index out of bound");
163 inline _Self operator+(int Right) {
169 inline _Self& operator-=(int Right) {
170 return operator+=(-Right);
173 inline _Self operator-(int Right) {
174 return operator+(-Right);
177 inline int operator-(const _Self& x) {
178 assert(Term == x.Term && "Cannot work on iterators of different blocks!");
179 int distance = idx - x.idx;
183 // This works for read access, however write access is difficult as changes
184 // to Term are only possible with Term->setSuccessor(idx). Pointers that can
185 // be modified are not available.
187 // inline pointer operator[](int offset) {
188 // _Self tmp = *this;
190 // return tmp.operator*();
194 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
195 typedef SuccIterator<const TerminatorInst*,
196 const BasicBlock> succ_const_iterator;
198 inline succ_iterator succ_begin(BasicBlock *BB) {
199 return succ_iterator(BB->getTerminator());
201 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
202 return succ_const_iterator(BB->getTerminator());
204 inline succ_iterator succ_end(BasicBlock *BB) {
205 return succ_iterator(BB->getTerminator(), true);
207 inline succ_const_iterator succ_end(const BasicBlock *BB) {
208 return succ_const_iterator(BB->getTerminator(), true);
213 //===--------------------------------------------------------------------===//
214 // GraphTraits specializations for basic block graphs (CFGs)
215 //===--------------------------------------------------------------------===//
217 // Provide specializations of GraphTraits to be able to treat a function as a
218 // graph of basic blocks...
220 template <> struct GraphTraits<BasicBlock*> {
221 typedef BasicBlock NodeType;
222 typedef succ_iterator ChildIteratorType;
224 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
225 static inline ChildIteratorType child_begin(NodeType *N) {
226 return succ_begin(N);
228 static inline ChildIteratorType child_end(NodeType *N) {
233 template <> struct GraphTraits<const BasicBlock*> {
234 typedef const BasicBlock NodeType;
235 typedef succ_const_iterator ChildIteratorType;
237 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
239 static inline ChildIteratorType child_begin(NodeType *N) {
240 return succ_begin(N);
242 static inline ChildIteratorType child_end(NodeType *N) {
247 // Provide specializations of GraphTraits to be able to treat a function as a
248 // graph of basic blocks... and to walk it in inverse order. Inverse order for
249 // a function is considered to be when traversing the predecessor edges of a BB
250 // instead of the successor edges.
252 template <> struct GraphTraits<Inverse<BasicBlock*> > {
253 typedef BasicBlock NodeType;
254 typedef pred_iterator ChildIteratorType;
255 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
256 static inline ChildIteratorType child_begin(NodeType *N) {
257 return pred_begin(N);
259 static inline ChildIteratorType child_end(NodeType *N) {
264 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
265 typedef const BasicBlock NodeType;
266 typedef pred_const_iterator ChildIteratorType;
267 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
270 static inline ChildIteratorType child_begin(NodeType *N) {
271 return pred_begin(N);
273 static inline ChildIteratorType child_end(NodeType *N) {
280 //===--------------------------------------------------------------------===//
281 // GraphTraits specializations for function basic block graphs (CFGs)
282 //===--------------------------------------------------------------------===//
284 // Provide specializations of GraphTraits to be able to treat a function as a
285 // graph of basic blocks... these are the same as the basic block iterators,
286 // except that the root node is implicitly the first node of the function.
288 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
289 static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
291 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
292 typedef Function::iterator nodes_iterator;
293 static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
294 static nodes_iterator nodes_end (Function *F) { return F->end(); }
296 template <> struct GraphTraits<const Function*> :
297 public GraphTraits<const BasicBlock*> {
298 static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
300 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
301 typedef Function::const_iterator nodes_iterator;
302 static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
303 static nodes_iterator nodes_end (const Function *F) { return F->end(); }
307 // Provide specializations of GraphTraits to be able to treat a function as a
308 // graph of basic blocks... and to walk it in inverse order. Inverse order for
309 // a function is considered to be when traversing the predecessor edges of a BB
310 // instead of the successor edges.
312 template <> struct GraphTraits<Inverse<Function*> > :
313 public GraphTraits<Inverse<BasicBlock*> > {
314 static NodeType *getEntryNode(Inverse<Function*> G) {
315 return &G.Graph->getEntryBlock();
318 template <> struct GraphTraits<Inverse<const Function*> > :
319 public GraphTraits<Inverse<const BasicBlock*> > {
320 static NodeType *getEntryNode(Inverse<const Function *> G) {
321 return &G.Graph->getEntryBlock();
325 } // End llvm namespace