Home | History | Annotate | Download | only in Support
      1 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines specializations of GraphTraits that allow Function and
     11 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_SUPPORT_CFG_H
     16 #define LLVM_SUPPORT_CFG_H
     17 
     18 #include "llvm/ADT/GraphTraits.h"
     19 #include "llvm/Function.h"
     20 #include "llvm/InstrTypes.h"
     21 
     22 namespace llvm {
     23 
     24 //===----------------------------------------------------------------------===//
     25 // BasicBlock pred_iterator definition
     26 //===----------------------------------------------------------------------===//
     27 
     28 template <class Ptr, class USE_iterator> // Predecessor Iterator
     29 class PredIterator : public std::iterator<std::forward_iterator_tag,
     30                                           Ptr, ptrdiff_t> {
     31   typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t> super;
     32   typedef PredIterator<Ptr, USE_iterator> Self;
     33   USE_iterator It;
     34 
     35   inline void advancePastNonTerminators() {
     36     // Loop to ignore non terminator uses (for example BlockAddresses).
     37     while (!It.atEnd() && !isa<TerminatorInst>(*It))
     38       ++It;
     39   }
     40 
     41 public:
     42   typedef typename super::pointer pointer;
     43 
     44   PredIterator() {}
     45   explicit inline PredIterator(Ptr *bb) : It(bb->use_begin()) {
     46     advancePastNonTerminators();
     47   }
     48   inline PredIterator(Ptr *bb, bool) : It(bb->use_end()) {}
     49 
     50   inline bool operator==(const Self& x) const { return It == x.It; }
     51   inline bool operator!=(const Self& x) const { return !operator==(x); }
     52 
     53   inline pointer operator*() const {
     54     assert(!It.atEnd() && "pred_iterator out of range!");
     55     return cast<TerminatorInst>(*It)->getParent();
     56   }
     57   inline pointer *operator->() const { return &operator*(); }
     58 
     59   inline Self& operator++() {   // Preincrement
     60     assert(!It.atEnd() && "pred_iterator out of range!");
     61     ++It; advancePastNonTerminators();
     62     return *this;
     63   }
     64 
     65   inline Self operator++(int) { // Postincrement
     66     Self tmp = *this; ++*this; return tmp;
     67   }
     68 
     69   /// getOperandNo - Return the operand number in the predecessor's
     70   /// terminator of the successor.
     71   unsigned getOperandNo() const {
     72     return It.getOperandNo();
     73   }
     74 };
     75 
     76 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
     77 typedef PredIterator<const BasicBlock,
     78                      Value::const_use_iterator> const_pred_iterator;
     79 
     80 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
     81 inline const_pred_iterator pred_begin(const BasicBlock *BB) {
     82   return const_pred_iterator(BB);
     83 }
     84 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
     85 inline const_pred_iterator pred_end(const BasicBlock *BB) {
     86   return const_pred_iterator(BB, true);
     87 }
     88 
     89 
     90 
     91 //===----------------------------------------------------------------------===//
     92 // BasicBlock succ_iterator definition
     93 //===----------------------------------------------------------------------===//
     94 
     95 template <class Term_, class BB_>           // Successor Iterator
     96 class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
     97                                           BB_, ptrdiff_t> {
     98   const Term_ Term;
     99   unsigned idx;
    100   typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t> super;
    101   typedef SuccIterator<Term_, BB_> Self;
    102 
    103   inline bool index_is_valid(int idx) {
    104     return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
    105   }
    106 
    107 public:
    108   typedef typename super::pointer pointer;
    109   // TODO: This can be random access iterator, only operator[] missing.
    110 
    111   explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
    112   }
    113   inline SuccIterator(Term_ T, bool)                       // end iterator
    114     : Term(T) {
    115     if (Term)
    116       idx = Term->getNumSuccessors();
    117     else
    118       // Term == NULL happens, if a basic block is not fully constructed and
    119       // consequently getTerminator() returns NULL. In this case we construct a
    120       // SuccIterator which describes a basic block that has zero successors.
    121       // Defining SuccIterator for incomplete and malformed CFGs is especially
    122       // useful for debugging.
    123       idx = 0;
    124   }
    125 
    126   inline const Self &operator=(const Self &I) {
    127     assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
    128     idx = I.idx;
    129     return *this;
    130   }
    131 
    132   /// getSuccessorIndex - This is used to interface between code that wants to
    133   /// operate on terminator instructions directly.
    134   unsigned getSuccessorIndex() const { return idx; }
    135 
    136   inline bool operator==(const Self& x) const { return idx == x.idx; }
    137   inline bool operator!=(const Self& x) const { return !operator==(x); }
    138 
    139   inline pointer operator*() const { return Term->getSuccessor(idx); }
    140   inline pointer operator->() const { return operator*(); }
    141 
    142   inline Self& operator++() { ++idx; return *this; } // Preincrement
    143 
    144   inline Self operator++(int) { // Postincrement
    145     Self tmp = *this; ++*this; return tmp;
    146   }
    147 
    148   inline Self& operator--() { --idx; return *this; }  // Predecrement
    149   inline Self operator--(int) { // Postdecrement
    150     Self tmp = *this; --*this; return tmp;
    151   }
    152 
    153   inline bool operator<(const Self& x) const {
    154     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
    155     return idx < x.idx;
    156   }
    157 
    158   inline bool operator<=(const Self& x) const {
    159     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
    160     return idx <= x.idx;
    161   }
    162   inline bool operator>=(const Self& x) const {
    163     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
    164     return idx >= x.idx;
    165   }
    166 
    167   inline bool operator>(const Self& x) const {
    168     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
    169     return idx > x.idx;
    170   }
    171 
    172   inline Self& operator+=(int Right) {
    173     unsigned new_idx = idx + Right;
    174     assert(index_is_valid(new_idx) && "Iterator index out of bound");
    175     idx = new_idx;
    176     return *this;
    177   }
    178 
    179   inline Self operator+(int Right) {
    180     Self tmp = *this;
    181     tmp += Right;
    182     return tmp;
    183   }
    184 
    185   inline Self& operator-=(int Right) {
    186     return operator+=(-Right);
    187   }
    188 
    189   inline Self operator-(int Right) {
    190     return operator+(-Right);
    191   }
    192 
    193   inline int operator-(const Self& x) {
    194     assert(Term == x.Term && "Cannot work on iterators of different blocks!");
    195     int distance = idx - x.idx;
    196     return distance;
    197   }
    198 
    199   // This works for read access, however write access is difficult as changes
    200   // to Term are only possible with Term->setSuccessor(idx). Pointers that can
    201   // be modified are not available.
    202   //
    203   // inline pointer operator[](int offset) {
    204   //  Self tmp = *this;
    205   //  tmp += offset;
    206   //  return tmp.operator*();
    207   // }
    208 
    209   /// Get the source BB of this iterator.
    210   inline BB_ *getSource() {
    211     assert(Term && "Source not available, if basic block was malformed");
    212     return Term->getParent();
    213   }
    214 };
    215 
    216 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
    217 typedef SuccIterator<const TerminatorInst*,
    218                      const BasicBlock> succ_const_iterator;
    219 
    220 inline succ_iterator succ_begin(BasicBlock *BB) {
    221   return succ_iterator(BB->getTerminator());
    222 }
    223 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
    224   return succ_const_iterator(BB->getTerminator());
    225 }
    226 inline succ_iterator succ_end(BasicBlock *BB) {
    227   return succ_iterator(BB->getTerminator(), true);
    228 }
    229 inline succ_const_iterator succ_end(const BasicBlock *BB) {
    230   return succ_const_iterator(BB->getTerminator(), true);
    231 }
    232 
    233 
    234 
    235 //===--------------------------------------------------------------------===//
    236 // GraphTraits specializations for basic block graphs (CFGs)
    237 //===--------------------------------------------------------------------===//
    238 
    239 // Provide specializations of GraphTraits to be able to treat a function as a
    240 // graph of basic blocks...
    241 
    242 template <> struct GraphTraits<BasicBlock*> {
    243   typedef BasicBlock NodeType;
    244   typedef succ_iterator ChildIteratorType;
    245 
    246   static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
    247   static inline ChildIteratorType child_begin(NodeType *N) {
    248     return succ_begin(N);
    249   }
    250   static inline ChildIteratorType child_end(NodeType *N) {
    251     return succ_end(N);
    252   }
    253 };
    254 
    255 template <> struct GraphTraits<const BasicBlock*> {
    256   typedef const BasicBlock NodeType;
    257   typedef succ_const_iterator ChildIteratorType;
    258 
    259   static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
    260 
    261   static inline ChildIteratorType child_begin(NodeType *N) {
    262     return succ_begin(N);
    263   }
    264   static inline ChildIteratorType child_end(NodeType *N) {
    265     return succ_end(N);
    266   }
    267 };
    268 
    269 // Provide specializations of GraphTraits to be able to treat a function as a
    270 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
    271 // a function is considered to be when traversing the predecessor edges of a BB
    272 // instead of the successor edges.
    273 //
    274 template <> struct GraphTraits<Inverse<BasicBlock*> > {
    275   typedef BasicBlock NodeType;
    276   typedef pred_iterator ChildIteratorType;
    277   static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
    278   static inline ChildIteratorType child_begin(NodeType *N) {
    279     return pred_begin(N);
    280   }
    281   static inline ChildIteratorType child_end(NodeType *N) {
    282     return pred_end(N);
    283   }
    284 };
    285 
    286 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
    287   typedef const BasicBlock NodeType;
    288   typedef const_pred_iterator ChildIteratorType;
    289   static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
    290     return G.Graph;
    291   }
    292   static inline ChildIteratorType child_begin(NodeType *N) {
    293     return pred_begin(N);
    294   }
    295   static inline ChildIteratorType child_end(NodeType *N) {
    296     return pred_end(N);
    297   }
    298 };
    299 
    300 
    301 
    302 //===--------------------------------------------------------------------===//
    303 // GraphTraits specializations for function basic block graphs (CFGs)
    304 //===--------------------------------------------------------------------===//
    305 
    306 // Provide specializations of GraphTraits to be able to treat a function as a
    307 // graph of basic blocks... these are the same as the basic block iterators,
    308 // except that the root node is implicitly the first node of the function.
    309 //
    310 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
    311   static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
    312 
    313   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
    314   typedef Function::iterator nodes_iterator;
    315   static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
    316   static nodes_iterator nodes_end  (Function *F) { return F->end(); }
    317 };
    318 template <> struct GraphTraits<const Function*> :
    319   public GraphTraits<const BasicBlock*> {
    320   static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
    321 
    322   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
    323   typedef Function::const_iterator nodes_iterator;
    324   static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
    325   static nodes_iterator nodes_end  (const Function *F) { return F->end(); }
    326 };
    327 
    328 
    329 // Provide specializations of GraphTraits to be able to treat a function as a
    330 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
    331 // a function is considered to be when traversing the predecessor edges of a BB
    332 // instead of the successor edges.
    333 //
    334 template <> struct GraphTraits<Inverse<Function*> > :
    335   public GraphTraits<Inverse<BasicBlock*> > {
    336   static NodeType *getEntryNode(Inverse<Function*> G) {
    337     return &G.Graph->getEntryBlock();
    338   }
    339 };
    340 template <> struct GraphTraits<Inverse<const Function*> > :
    341   public GraphTraits<Inverse<const BasicBlock*> > {
    342   static NodeType *getEntryNode(Inverse<const Function *> G) {
    343     return &G.Graph->getEntryBlock();
    344   }
    345 };
    346 
    347 } // End llvm namespace
    348 
    349 #endif
    350