CFG.h 13 KB

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