LLVM API Documentation

CFG.h
Go to the documentation of this file.
00001 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines specializations of GraphTraits that allow Function and
00011 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_CFG_H
00016 #define LLVM_SUPPORT_CFG_H
00017 
00018 #include "llvm/ADT/GraphTraits.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/IR/InstrTypes.h"
00021 
00022 namespace llvm {
00023 
00024 //===----------------------------------------------------------------------===//
00025 // BasicBlock pred_iterator definition
00026 //===----------------------------------------------------------------------===//
00027 
00028 template <class Ptr, class USE_iterator> // Predecessor Iterator
00029 class PredIterator : public std::iterator<std::forward_iterator_tag,
00030                                           Ptr, ptrdiff_t, Ptr*, Ptr*> {
00031   typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t, Ptr*,
00032                                                                     Ptr*> super;
00033   typedef PredIterator<Ptr, USE_iterator> Self;
00034   USE_iterator It;
00035 
00036   inline void advancePastNonTerminators() {
00037     // Loop to ignore non terminator uses (for example BlockAddresses).
00038     while (!It.atEnd() && !isa<TerminatorInst>(*It))
00039       ++It;
00040   }
00041 
00042 public:
00043   typedef typename super::pointer pointer;
00044   typedef typename super::reference reference;
00045 
00046   PredIterator() {}
00047   explicit inline PredIterator(Ptr *bb) : It(bb->use_begin()) {
00048     advancePastNonTerminators();
00049   }
00050   inline PredIterator(Ptr *bb, bool) : It(bb->use_end()) {}
00051 
00052   inline bool operator==(const Self& x) const { return It == x.It; }
00053   inline bool operator!=(const Self& x) const { return !operator==(x); }
00054 
00055   inline reference operator*() const {
00056     assert(!It.atEnd() && "pred_iterator out of range!");
00057     return cast<TerminatorInst>(*It)->getParent();
00058   }
00059   inline pointer *operator->() const { return &operator*(); }
00060 
00061   inline Self& operator++() {   // Preincrement
00062     assert(!It.atEnd() && "pred_iterator out of range!");
00063     ++It; advancePastNonTerminators();
00064     return *this;
00065   }
00066 
00067   inline Self operator++(int) { // Postincrement
00068     Self tmp = *this; ++*this; return tmp;
00069   }
00070 
00071   /// getOperandNo - Return the operand number in the predecessor's
00072   /// terminator of the successor.
00073   unsigned getOperandNo() const {
00074     return It.getOperandNo();
00075   }
00076 
00077   /// getUse - Return the operand Use in the predecessor's terminator
00078   /// of the successor.
00079   Use &getUse() const {
00080     return It.getUse();
00081   }
00082 };
00083 
00084 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
00085 typedef PredIterator<const BasicBlock,
00086                      Value::const_use_iterator> const_pred_iterator;
00087 
00088 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
00089 inline const_pred_iterator pred_begin(const BasicBlock *BB) {
00090   return const_pred_iterator(BB);
00091 }
00092 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
00093 inline const_pred_iterator pred_end(const BasicBlock *BB) {
00094   return const_pred_iterator(BB, true);
00095 }
00096 
00097 
00098 
00099 //===----------------------------------------------------------------------===//
00100 // BasicBlock succ_iterator definition
00101 //===----------------------------------------------------------------------===//
00102 
00103 template <class Term_, class BB_>           // Successor Iterator
00104 class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
00105                                           BB_, ptrdiff_t, BB_*, BB_*> {
00106   const Term_ Term;
00107   unsigned idx;
00108   typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t, BB_*,
00109                                                                     BB_*> super;
00110   typedef SuccIterator<Term_, BB_> Self;
00111 
00112   inline bool index_is_valid(int idx) {
00113     return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
00114   }
00115 
00116 public:
00117   typedef typename super::pointer pointer;
00118   typedef typename super::reference reference;
00119   // TODO: This can be random access iterator, only operator[] missing.
00120 
00121   explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
00122   }
00123   inline SuccIterator(Term_ T, bool)                       // end iterator
00124     : Term(T) {
00125     if (Term)
00126       idx = Term->getNumSuccessors();
00127     else
00128       // Term == NULL happens, if a basic block is not fully constructed and
00129       // consequently getTerminator() returns NULL. In this case we construct a
00130       // SuccIterator which describes a basic block that has zero successors.
00131       // Defining SuccIterator for incomplete and malformed CFGs is especially
00132       // useful for debugging.
00133       idx = 0;
00134   }
00135 
00136   inline const Self &operator=(const Self &I) {
00137     assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
00138     idx = I.idx;
00139     return *this;
00140   }
00141 
00142   /// getSuccessorIndex - This is used to interface between code that wants to
00143   /// operate on terminator instructions directly.
00144   unsigned getSuccessorIndex() const { return idx; }
00145 
00146   inline bool operator==(const Self& x) const { return idx == x.idx; }
00147   inline bool operator!=(const Self& x) const { return !operator==(x); }
00148 
00149   inline reference operator*() const { return Term->getSuccessor(idx); }
00150   inline pointer operator->() const { return operator*(); }
00151 
00152   inline Self& operator++() { ++idx; return *this; } // Preincrement
00153 
00154   inline Self operator++(int) { // Postincrement
00155     Self tmp = *this; ++*this; return tmp;
00156   }
00157 
00158   inline Self& operator--() { --idx; return *this; }  // Predecrement
00159   inline Self operator--(int) { // Postdecrement
00160     Self tmp = *this; --*this; return tmp;
00161   }
00162 
00163   inline bool operator<(const Self& x) const {
00164     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
00165     return idx < x.idx;
00166   }
00167 
00168   inline bool operator<=(const Self& x) const {
00169     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
00170     return idx <= x.idx;
00171   }
00172   inline bool operator>=(const Self& x) const {
00173     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
00174     return idx >= x.idx;
00175   }
00176 
00177   inline bool operator>(const Self& x) const {
00178     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
00179     return idx > x.idx;
00180   }
00181 
00182   inline Self& operator+=(int Right) {
00183     unsigned new_idx = idx + Right;
00184     assert(index_is_valid(new_idx) && "Iterator index out of bound");
00185     idx = new_idx;
00186     return *this;
00187   }
00188 
00189   inline Self operator+(int Right) {
00190     Self tmp = *this;
00191     tmp += Right;
00192     return tmp;
00193   }
00194 
00195   inline Self& operator-=(int Right) {
00196     return operator+=(-Right);
00197   }
00198 
00199   inline Self operator-(int Right) {
00200     return operator+(-Right);
00201   }
00202 
00203   inline int operator-(const Self& x) {
00204     assert(Term == x.Term && "Cannot work on iterators of different blocks!");
00205     int distance = idx - x.idx;
00206     return distance;
00207   }
00208 
00209   // This works for read access, however write access is difficult as changes
00210   // to Term are only possible with Term->setSuccessor(idx). Pointers that can
00211   // be modified are not available.
00212   //
00213   // inline pointer operator[](int offset) {
00214   //  Self tmp = *this;
00215   //  tmp += offset;
00216   //  return tmp.operator*();
00217   // }
00218 
00219   /// Get the source BB of this iterator.
00220   inline BB_ *getSource() {
00221     assert(Term && "Source not available, if basic block was malformed");
00222     return Term->getParent();
00223   }
00224 };
00225 
00226 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
00227 typedef SuccIterator<const TerminatorInst*,
00228                      const BasicBlock> succ_const_iterator;
00229 
00230 inline succ_iterator succ_begin(BasicBlock *BB) {
00231   return succ_iterator(BB->getTerminator());
00232 }
00233 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
00234   return succ_const_iterator(BB->getTerminator());
00235 }
00236 inline succ_iterator succ_end(BasicBlock *BB) {
00237   return succ_iterator(BB->getTerminator(), true);
00238 }
00239 inline succ_const_iterator succ_end(const BasicBlock *BB) {
00240   return succ_const_iterator(BB->getTerminator(), true);
00241 }
00242 
00243 template <typename T, typename U> struct isPodLike<SuccIterator<T, U> > {
00244   static const bool value = isPodLike<T>::value;
00245 };
00246 
00247 
00248 
00249 //===--------------------------------------------------------------------===//
00250 // GraphTraits specializations for basic block graphs (CFGs)
00251 //===--------------------------------------------------------------------===//
00252 
00253 // Provide specializations of GraphTraits to be able to treat a function as a
00254 // graph of basic blocks...
00255 
00256 template <> struct GraphTraits<BasicBlock*> {
00257   typedef BasicBlock NodeType;
00258   typedef succ_iterator ChildIteratorType;
00259 
00260   static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
00261   static inline ChildIteratorType child_begin(NodeType *N) {
00262     return succ_begin(N);
00263   }
00264   static inline ChildIteratorType child_end(NodeType *N) {
00265     return succ_end(N);
00266   }
00267 };
00268 
00269 template <> struct GraphTraits<const BasicBlock*> {
00270   typedef const BasicBlock NodeType;
00271   typedef succ_const_iterator ChildIteratorType;
00272 
00273   static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
00274 
00275   static inline ChildIteratorType child_begin(NodeType *N) {
00276     return succ_begin(N);
00277   }
00278   static inline ChildIteratorType child_end(NodeType *N) {
00279     return succ_end(N);
00280   }
00281 };
00282 
00283 // Provide specializations of GraphTraits to be able to treat a function as a
00284 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
00285 // a function is considered to be when traversing the predecessor edges of a BB
00286 // instead of the successor edges.
00287 //
00288 template <> struct GraphTraits<Inverse<BasicBlock*> > {
00289   typedef BasicBlock NodeType;
00290   typedef pred_iterator ChildIteratorType;
00291   static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
00292   static inline ChildIteratorType child_begin(NodeType *N) {
00293     return pred_begin(N);
00294   }
00295   static inline ChildIteratorType child_end(NodeType *N) {
00296     return pred_end(N);
00297   }
00298 };
00299 
00300 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
00301   typedef const BasicBlock NodeType;
00302   typedef const_pred_iterator ChildIteratorType;
00303   static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
00304     return G.Graph;
00305   }
00306   static inline ChildIteratorType child_begin(NodeType *N) {
00307     return pred_begin(N);
00308   }
00309   static inline ChildIteratorType child_end(NodeType *N) {
00310     return pred_end(N);
00311   }
00312 };
00313 
00314 
00315 
00316 //===--------------------------------------------------------------------===//
00317 // GraphTraits specializations for function basic block graphs (CFGs)
00318 //===--------------------------------------------------------------------===//
00319 
00320 // Provide specializations of GraphTraits to be able to treat a function as a
00321 // graph of basic blocks... these are the same as the basic block iterators,
00322 // except that the root node is implicitly the first node of the function.
00323 //
00324 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
00325   static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
00326 
00327   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00328   typedef Function::iterator nodes_iterator;
00329   static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
00330   static nodes_iterator nodes_end  (Function *F) { return F->end(); }
00331   static unsigned       size       (Function *F) { return F->size(); }
00332 };
00333 template <> struct GraphTraits<const Function*> :
00334   public GraphTraits<const BasicBlock*> {
00335   static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
00336 
00337   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00338   typedef Function::const_iterator nodes_iterator;
00339   static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
00340   static nodes_iterator nodes_end  (const Function *F) { return F->end(); }
00341   static unsigned       size       (const Function *F) { return F->size(); }
00342 };
00343 
00344 
00345 // Provide specializations of GraphTraits to be able to treat a function as a
00346 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
00347 // a function is considered to be when traversing the predecessor edges of a BB
00348 // instead of the successor edges.
00349 //
00350 template <> struct GraphTraits<Inverse<Function*> > :
00351   public GraphTraits<Inverse<BasicBlock*> > {
00352   static NodeType *getEntryNode(Inverse<Function*> G) {
00353     return &G.Graph->getEntryBlock();
00354   }
00355 };
00356 template <> struct GraphTraits<Inverse<const Function*> > :
00357   public GraphTraits<Inverse<const BasicBlock*> > {
00358   static NodeType *getEntryNode(Inverse<const Function *> G) {
00359     return &G.Graph->getEntryBlock();
00360   }
00361 };
00362 
00363 } // End llvm namespace
00364 
00365 #endif