Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
12 KiB

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