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 BlockAddresses).
37 while (!It.atEnd() && !isa<TerminatorInst>(*It))
42 typedef typename super::pointer pointer;
45 explicit inline PredIterator(Ptr *bb) : It(bb->use_begin()) {
46 advancePastNonTerminators();
48 inline PredIterator(Ptr *bb, bool) : It(bb->use_end()) {}
50 inline bool operator==(const Self& x) const { return It == x.It; }
51 inline bool operator!=(const Self& x) const { return !operator==(x); }
53 inline pointer operator*() const {
54 assert(!It.atEnd() && "pred_iterator out of range!");
55 return cast<TerminatorInst>(*It)->getParent();
57 inline pointer *operator->() const { return &operator*(); }
59 inline Self& operator++() { // Preincrement
60 assert(!It.atEnd() && "pred_iterator out of range!");
61 ++It; advancePastNonTerminators();
65 inline Self operator++(int) { // Postincrement
66 Self tmp = *this; ++*this; return tmp;
69 /// getOperandNo - Return the operand number in the predecessor's
70 /// terminator of the successor.
71 unsigned getOperandNo() const {
72 return It.getOperandNo();
75 /// getUse - Return the operand Use in the predecessor's terminator
82 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
83 typedef PredIterator<const BasicBlock,
84 Value::const_use_iterator> const_pred_iterator;
86 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
87 inline const_pred_iterator pred_begin(const BasicBlock *BB) {
88 return const_pred_iterator(BB);
90 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
91 inline const_pred_iterator pred_end(const BasicBlock *BB) {
92 return const_pred_iterator(BB, true);
97 //===----------------------------------------------------------------------===//
98 // BasicBlock succ_iterator definition
99 //===----------------------------------------------------------------------===//
101 template <class Term_, class BB_> // Successor Iterator
102 class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
106 typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t> super;
107 typedef SuccIterator<Term_, BB_> Self;
109 inline bool index_is_valid(int idx) {
110 return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
114 typedef typename super::pointer pointer;
115 // TODO: This can be random access iterator, only operator[] missing.
117 explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
119 inline SuccIterator(Term_ T, bool) // end iterator
122 idx = Term->getNumSuccessors();
124 // Term == NULL happens, if a basic block is not fully constructed and
125 // consequently getTerminator() returns NULL. In this case we construct a
126 // SuccIterator which describes a basic block that has zero successors.
127 // Defining SuccIterator for incomplete and malformed CFGs is especially
128 // useful for debugging.
132 inline const Self &operator=(const Self &I) {
133 assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
138 /// getSuccessorIndex - This is used to interface between code that wants to
139 /// operate on terminator instructions directly.
140 unsigned getSuccessorIndex() const { return idx; }
142 inline bool operator==(const Self& x) const { return idx == x.idx; }
143 inline bool operator!=(const Self& x) const { return !operator==(x); }
145 inline pointer operator*() const { return Term->getSuccessor(idx); }
146 inline pointer operator->() const { return operator*(); }
148 inline Self& operator++() { ++idx; return *this; } // Preincrement
150 inline Self operator++(int) { // Postincrement
151 Self tmp = *this; ++*this; return tmp;
154 inline Self& operator--() { --idx; return *this; } // Predecrement
155 inline Self operator--(int) { // Postdecrement
156 Self tmp = *this; --*this; return tmp;
159 inline bool operator<(const Self& x) const {
160 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
164 inline bool operator<=(const Self& x) const {
165 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
168 inline bool operator>=(const Self& x) const {
169 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
173 inline bool operator>(const Self& x) const {
174 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
178 inline Self& operator+=(int Right) {
179 unsigned new_idx = idx + Right;
180 assert(index_is_valid(new_idx) && "Iterator index out of bound");
185 inline Self operator+(int Right) {
191 inline Self& operator-=(int Right) {
192 return operator+=(-Right);
195 inline Self operator-(int Right) {
196 return operator+(-Right);
199 inline int operator-(const Self& x) {
200 assert(Term == x.Term && "Cannot work on iterators of different blocks!");
201 int distance = idx - x.idx;
205 // This works for read access, however write access is difficult as changes
206 // to Term are only possible with Term->setSuccessor(idx). Pointers that can
207 // be modified are not available.
209 // inline pointer operator[](int offset) {
212 // return tmp.operator*();
215 /// Get the source BB of this iterator.
216 inline BB_ *getSource() {
217 assert(Term && "Source not available, if basic block was malformed");
218 return Term->getParent();
222 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
223 typedef SuccIterator<const TerminatorInst*,
224 const BasicBlock> succ_const_iterator;
226 inline succ_iterator succ_begin(BasicBlock *BB) {
227 return succ_iterator(BB->getTerminator());
229 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
230 return succ_const_iterator(BB->getTerminator());
232 inline succ_iterator succ_end(BasicBlock *BB) {
233 return succ_iterator(BB->getTerminator(), true);
235 inline succ_const_iterator succ_end(const BasicBlock *BB) {
236 return succ_const_iterator(BB->getTerminator(), true);
241 //===--------------------------------------------------------------------===//
242 // GraphTraits specializations for basic block graphs (CFGs)
243 //===--------------------------------------------------------------------===//
245 // Provide specializations of GraphTraits to be able to treat a function as a
246 // graph of basic blocks...
248 template <> struct GraphTraits<BasicBlock*> {
249 typedef BasicBlock NodeType;
250 typedef succ_iterator ChildIteratorType;
252 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
253 static inline ChildIteratorType child_begin(NodeType *N) {
254 return succ_begin(N);
256 static inline ChildIteratorType child_end(NodeType *N) {
261 template <> struct GraphTraits<const BasicBlock*> {
262 typedef const BasicBlock NodeType;
263 typedef succ_const_iterator ChildIteratorType;
265 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
267 static inline ChildIteratorType child_begin(NodeType *N) {
268 return succ_begin(N);
270 static inline ChildIteratorType child_end(NodeType *N) {
275 // Provide specializations of GraphTraits to be able to treat a function as a
276 // graph of basic blocks... and to walk it in inverse order. Inverse order for
277 // a function is considered to be when traversing the predecessor edges of a BB
278 // instead of the successor edges.
280 template <> struct GraphTraits<Inverse<BasicBlock*> > {
281 typedef BasicBlock NodeType;
282 typedef pred_iterator ChildIteratorType;
283 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
284 static inline ChildIteratorType child_begin(NodeType *N) {
285 return pred_begin(N);
287 static inline ChildIteratorType child_end(NodeType *N) {
292 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
293 typedef const BasicBlock NodeType;
294 typedef const_pred_iterator ChildIteratorType;
295 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
298 static inline ChildIteratorType child_begin(NodeType *N) {
299 return pred_begin(N);
301 static inline ChildIteratorType child_end(NodeType *N) {
308 //===--------------------------------------------------------------------===//
309 // GraphTraits specializations for function basic block graphs (CFGs)
310 //===--------------------------------------------------------------------===//
312 // Provide specializations of GraphTraits to be able to treat a function as a
313 // graph of basic blocks... these are the same as the basic block iterators,
314 // except that the root node is implicitly the first node of the function.
316 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
317 static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
319 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
320 typedef Function::iterator nodes_iterator;
321 static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
322 static nodes_iterator nodes_end (Function *F) { return F->end(); }
323 static unsigned size (Function *F) { return F->size(); }
325 template <> struct GraphTraits<const Function*> :
326 public GraphTraits<const BasicBlock*> {
327 static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
329 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
330 typedef Function::const_iterator nodes_iterator;
331 static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
332 static nodes_iterator nodes_end (const Function *F) { return F->end(); }
333 static unsigned size (const Function *F) { return F->size(); }
337 // Provide specializations of GraphTraits to be able to treat a function as a
338 // graph of basic blocks... and to walk it in inverse order. Inverse order for
339 // a function is considered to be when traversing the predecessor edges of a BB
340 // instead of the successor edges.
342 template <> struct GraphTraits<Inverse<Function*> > :
343 public GraphTraits<Inverse<BasicBlock*> > {
344 static NodeType *getEntryNode(Inverse<Function*> G) {
345 return &G.Graph->getEntryBlock();
348 template <> struct GraphTraits<Inverse<const Function*> > :
349 public GraphTraits<Inverse<const BasicBlock*> > {
350 static NodeType *getEntryNode(Inverse<const Function *> G) {
351 return &G.Graph->getEntryBlock();
355 } // End llvm namespace