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;
32 typedef PredIterator<Ptr, USE_iterator> Self;
35 inline void advancePastNonTerminators() {
36 // Loop to ignore non terminator uses (for example PHI nodes).
37 while (!It.atEnd() && !isa<TerminatorInst>(*It))
42 typedef typename super::pointer pointer;
44 explicit inline PredIterator(Ptr *bb) : It(bb->use_begin()) {
45 advancePastNonTerminators();
47 inline PredIterator(Ptr *bb, bool) : It(bb->use_end()) {}
49 inline bool operator==(const Self& x) const { return It == x.It; }
50 inline bool operator!=(const Self& x) const { return !operator==(x); }
52 inline pointer operator*() const {
53 assert(!It.atEnd() && "pred_iterator out of range!");
54 return cast<TerminatorInst>(*It)->getParent();
56 inline pointer *operator->() const { return &operator*(); }
58 inline Self& operator++() { // Preincrement
59 assert(!It.atEnd() && "pred_iterator out of range!");
60 ++It; advancePastNonTerminators();
64 inline Self operator++(int) { // Postincrement
65 Self tmp = *this; ++*this; return tmp;
69 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
70 typedef PredIterator<const BasicBlock,
71 Value::const_use_iterator> const_pred_iterator;
73 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
74 inline const_pred_iterator pred_begin(const BasicBlock *BB) {
75 return const_pred_iterator(BB);
77 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
78 inline const_pred_iterator pred_end(const BasicBlock *BB) {
79 return const_pred_iterator(BB, true);
84 //===----------------------------------------------------------------------===//
85 // BasicBlock succ_iterator definition
86 //===----------------------------------------------------------------------===//
88 template <class Term_, class BB_> // Successor Iterator
89 class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
93 typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t> super;
94 typedef SuccIterator<Term_, BB_> Self;
96 inline bool index_is_valid(int idx) {
97 return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
101 typedef typename super::pointer pointer;
102 // TODO: This can be random access iterator, only operator[] missing.
104 explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
105 assert(T && "getTerminator returned null!");
107 inline SuccIterator(Term_ T, bool) // end iterator
108 : Term(T), idx(Term->getNumSuccessors()) {
109 assert(T && "getTerminator returned null!");
112 inline const Self &operator=(const Self &I) {
113 assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
118 /// getSuccessorIndex - This is used to interface between code that wants to
119 /// operate on terminator instructions directly.
120 unsigned getSuccessorIndex() const { return idx; }
122 inline bool operator==(const Self& x) const { return idx == x.idx; }
123 inline bool operator!=(const Self& x) const { return !operator==(x); }
125 inline pointer operator*() const { return Term->getSuccessor(idx); }
126 inline pointer operator->() const { return operator*(); }
128 inline Self& operator++() { ++idx; return *this; } // Preincrement
130 inline Self operator++(int) { // Postincrement
131 Self tmp = *this; ++*this; return tmp;
134 inline Self& operator--() { --idx; return *this; } // Predecrement
135 inline Self operator--(int) { // Postdecrement
136 Self tmp = *this; --*this; return tmp;
139 inline bool operator<(const Self& x) const {
140 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
144 inline bool operator<=(const Self& x) const {
145 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
148 inline bool operator>=(const Self& x) const {
149 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
153 inline bool operator>(const Self& x) const {
154 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
158 inline Self& operator+=(int Right) {
159 unsigned new_idx = idx + Right;
160 assert(index_is_valid(new_idx) && "Iterator index out of bound");
165 inline Self operator+(int Right) {
171 inline Self& operator-=(int Right) {
172 return operator+=(-Right);
175 inline Self operator-(int Right) {
176 return operator+(-Right);
179 inline int operator-(const Self& x) {
180 assert(Term == x.Term && "Cannot work on iterators of different blocks!");
181 int distance = idx - x.idx;
185 // This works for read access, however write access is difficult as changes
186 // to Term are only possible with Term->setSuccessor(idx). Pointers that can
187 // be modified are not available.
189 // inline pointer operator[](int offset) {
192 // return tmp.operator*();
195 /// Get the source BB of this iterator.
196 inline BB_ *getSource() {
197 return Term->getParent();
201 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
202 typedef SuccIterator<const TerminatorInst*,
203 const BasicBlock> succ_const_iterator;
205 inline succ_iterator succ_begin(BasicBlock *BB) {
206 return succ_iterator(BB->getTerminator());
208 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
209 return succ_const_iterator(BB->getTerminator());
211 inline succ_iterator succ_end(BasicBlock *BB) {
212 return succ_iterator(BB->getTerminator(), true);
214 inline succ_const_iterator succ_end(const BasicBlock *BB) {
215 return succ_const_iterator(BB->getTerminator(), true);
220 //===--------------------------------------------------------------------===//
221 // GraphTraits specializations for basic block graphs (CFGs)
222 //===--------------------------------------------------------------------===//
224 // Provide specializations of GraphTraits to be able to treat a function as a
225 // graph of basic blocks...
227 template <> struct GraphTraits<BasicBlock*> {
228 typedef BasicBlock NodeType;
229 typedef succ_iterator ChildIteratorType;
231 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
232 static inline ChildIteratorType child_begin(NodeType *N) {
233 return succ_begin(N);
235 static inline ChildIteratorType child_end(NodeType *N) {
240 template <> struct GraphTraits<const BasicBlock*> {
241 typedef const BasicBlock NodeType;
242 typedef succ_const_iterator ChildIteratorType;
244 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
246 static inline ChildIteratorType child_begin(NodeType *N) {
247 return succ_begin(N);
249 static inline ChildIteratorType child_end(NodeType *N) {
254 // Provide specializations of GraphTraits to be able to treat a function as a
255 // graph of basic blocks... and to walk it in inverse order. Inverse order for
256 // a function is considered to be when traversing the predecessor edges of a BB
257 // instead of the successor edges.
259 template <> struct GraphTraits<Inverse<BasicBlock*> > {
260 typedef BasicBlock NodeType;
261 typedef pred_iterator ChildIteratorType;
262 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
263 static inline ChildIteratorType child_begin(NodeType *N) {
264 return pred_begin(N);
266 static inline ChildIteratorType child_end(NodeType *N) {
271 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
272 typedef const BasicBlock NodeType;
273 typedef const_pred_iterator ChildIteratorType;
274 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
277 static inline ChildIteratorType child_begin(NodeType *N) {
278 return pred_begin(N);
280 static inline ChildIteratorType child_end(NodeType *N) {
287 //===--------------------------------------------------------------------===//
288 // GraphTraits specializations for function basic block graphs (CFGs)
289 //===--------------------------------------------------------------------===//
291 // Provide specializations of GraphTraits to be able to treat a function as a
292 // graph of basic blocks... these are the same as the basic block iterators,
293 // except that the root node is implicitly the first node of the function.
295 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
296 static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
298 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
299 typedef Function::iterator nodes_iterator;
300 static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
301 static nodes_iterator nodes_end (Function *F) { return F->end(); }
303 template <> struct GraphTraits<const Function*> :
304 public GraphTraits<const BasicBlock*> {
305 static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
307 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
308 typedef Function::const_iterator nodes_iterator;
309 static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
310 static nodes_iterator nodes_end (const Function *F) { return F->end(); }
314 // Provide specializations of GraphTraits to be able to treat a function as a
315 // graph of basic blocks... and to walk it in inverse order. Inverse order for
316 // a function is considered to be when traversing the predecessor edges of a BB
317 // instead of the successor edges.
319 template <> struct GraphTraits<Inverse<Function*> > :
320 public GraphTraits<Inverse<BasicBlock*> > {
321 static NodeType *getEntryNode(Inverse<Function*> G) {
322 return &G.Graph->getEntryBlock();
325 template <> struct GraphTraits<Inverse<const Function*> > :
326 public GraphTraits<Inverse<const BasicBlock*> > {
327 static NodeType *getEntryNode(Inverse<const Function *> G) {
328 return &G.Graph->getEntryBlock();
332 } // End llvm namespace