CFG.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CFG.h ----------------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// This file provides various utilities for inspecting and working with the
  16. /// control flow graph in LLVM IR. This includes generic facilities for
  17. /// iterating successors and predecessors of basic blocks, the successors of
  18. /// specific terminator instructions, etc. It also defines specializations of
  19. /// GraphTraits that allow Function and BasicBlock graphs to be treated as
  20. /// proper graphs for generic algorithms.
  21. ///
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_IR_CFG_H
  24. #define LLVM_IR_CFG_H
  25. #include "llvm/ADT/GraphTraits.h"
  26. #include "llvm/ADT/iterator.h"
  27. #include "llvm/ADT/iterator_range.h"
  28. #include "llvm/IR/BasicBlock.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/Value.h"
  31. #include <cassert>
  32. #include <cstddef>
  33. #include <iterator>
  34. namespace llvm {
  35. class Instruction;
  36. class Use;
  37. //===----------------------------------------------------------------------===//
  38. // BasicBlock pred_iterator definition
  39. //===----------------------------------------------------------------------===//
  40. template <class Ptr, class USE_iterator> // Predecessor Iterator
  41. class PredIterator {
  42. public:
  43. using iterator_category = std::forward_iterator_tag;
  44. using value_type = Ptr;
  45. using difference_type = std::ptrdiff_t;
  46. using pointer = Ptr *;
  47. using reference = Ptr *;
  48. protected:
  49. using Self = PredIterator<Ptr, USE_iterator>;
  50. USE_iterator It;
  51. inline void advancePastNonTerminators() {
  52. // Loop to ignore non-terminator uses (for example BlockAddresses).
  53. while (!It.atEnd()) {
  54. if (auto *Inst = dyn_cast<Instruction>(*It))
  55. if (Inst->isTerminator())
  56. break;
  57. ++It;
  58. }
  59. }
  60. public:
  61. PredIterator() = default;
  62. explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
  63. advancePastNonTerminators();
  64. }
  65. inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
  66. inline bool operator==(const Self& x) const { return It == x.It; }
  67. inline bool operator!=(const Self& x) const { return !operator==(x); }
  68. inline reference operator*() const {
  69. assert(!It.atEnd() && "pred_iterator out of range!");
  70. return cast<Instruction>(*It)->getParent();
  71. }
  72. inline pointer *operator->() const { return &operator*(); }
  73. inline Self& operator++() { // Preincrement
  74. assert(!It.atEnd() && "pred_iterator out of range!");
  75. ++It; advancePastNonTerminators();
  76. return *this;
  77. }
  78. inline Self operator++(int) { // Postincrement
  79. Self tmp = *this; ++*this; return tmp;
  80. }
  81. /// getOperandNo - Return the operand number in the predecessor's
  82. /// terminator of the successor.
  83. unsigned getOperandNo() const {
  84. return It.getOperandNo();
  85. }
  86. /// getUse - Return the operand Use in the predecessor's terminator
  87. /// of the successor.
  88. Use &getUse() const {
  89. return It.getUse();
  90. }
  91. };
  92. using pred_iterator = PredIterator<BasicBlock, Value::user_iterator>;
  93. using const_pred_iterator =
  94. PredIterator<const BasicBlock, Value::const_user_iterator>;
  95. using pred_range = iterator_range<pred_iterator>;
  96. using const_pred_range = iterator_range<const_pred_iterator>;
  97. inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
  98. inline const_pred_iterator pred_begin(const BasicBlock *BB) {
  99. return const_pred_iterator(BB);
  100. }
  101. inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
  102. inline const_pred_iterator pred_end(const BasicBlock *BB) {
  103. return const_pred_iterator(BB, true);
  104. }
  105. inline bool pred_empty(const BasicBlock *BB) {
  106. return pred_begin(BB) == pred_end(BB);
  107. }
  108. /// Get the number of predecessors of \p BB. This is a linear time operation.
  109. /// Use \ref BasicBlock::hasNPredecessors() or hasNPredecessorsOrMore if able.
  110. inline unsigned pred_size(const BasicBlock *BB) {
  111. return std::distance(pred_begin(BB), pred_end(BB));
  112. }
  113. inline pred_range predecessors(BasicBlock *BB) {
  114. return pred_range(pred_begin(BB), pred_end(BB));
  115. }
  116. inline const_pred_range predecessors(const BasicBlock *BB) {
  117. return const_pred_range(pred_begin(BB), pred_end(BB));
  118. }
  119. //===----------------------------------------------------------------------===//
  120. // Instruction and BasicBlock succ_iterator helpers
  121. //===----------------------------------------------------------------------===//
  122. template <class InstructionT, class BlockT>
  123. class SuccIterator
  124. : public iterator_facade_base<SuccIterator<InstructionT, BlockT>,
  125. std::random_access_iterator_tag, BlockT, int,
  126. BlockT *, BlockT *> {
  127. public:
  128. using difference_type = int;
  129. using pointer = BlockT *;
  130. using reference = BlockT *;
  131. private:
  132. InstructionT *Inst;
  133. int Idx;
  134. using Self = SuccIterator<InstructionT, BlockT>;
  135. inline bool index_is_valid(int Idx) {
  136. // Note that we specially support the index of zero being valid even in the
  137. // face of a null instruction.
  138. return Idx >= 0 && (Idx == 0 || Idx <= (int)Inst->getNumSuccessors());
  139. }
  140. /// Proxy object to allow write access in operator[]
  141. class SuccessorProxy {
  142. Self It;
  143. public:
  144. explicit SuccessorProxy(const Self &It) : It(It) {}
  145. SuccessorProxy(const SuccessorProxy &) = default;
  146. SuccessorProxy &operator=(SuccessorProxy RHS) {
  147. *this = reference(RHS);
  148. return *this;
  149. }
  150. SuccessorProxy &operator=(reference RHS) {
  151. It.Inst->setSuccessor(It.Idx, RHS);
  152. return *this;
  153. }
  154. operator reference() const { return *It; }
  155. };
  156. public:
  157. // begin iterator
  158. explicit inline SuccIterator(InstructionT *Inst) : Inst(Inst), Idx(0) {}
  159. // end iterator
  160. inline SuccIterator(InstructionT *Inst, bool) : Inst(Inst) {
  161. if (Inst)
  162. Idx = Inst->getNumSuccessors();
  163. else
  164. // Inst == NULL happens, if a basic block is not fully constructed and
  165. // consequently getTerminator() returns NULL. In this case we construct
  166. // a SuccIterator which describes a basic block that has zero
  167. // successors.
  168. // Defining SuccIterator for incomplete and malformed CFGs is especially
  169. // useful for debugging.
  170. Idx = 0;
  171. }
  172. /// This is used to interface between code that wants to
  173. /// operate on terminator instructions directly.
  174. int getSuccessorIndex() const { return Idx; }
  175. inline bool operator==(const Self &x) const { return Idx == x.Idx; }
  176. inline BlockT *operator*() const { return Inst->getSuccessor(Idx); }
  177. // We use the basic block pointer directly for operator->.
  178. inline BlockT *operator->() const { return operator*(); }
  179. inline bool operator<(const Self &RHS) const {
  180. assert(Inst == RHS.Inst && "Cannot compare iterators of different blocks!");
  181. return Idx < RHS.Idx;
  182. }
  183. int operator-(const Self &RHS) const {
  184. assert(Inst == RHS.Inst && "Cannot compare iterators of different blocks!");
  185. return Idx - RHS.Idx;
  186. }
  187. inline Self &operator+=(int RHS) {
  188. int NewIdx = Idx + RHS;
  189. assert(index_is_valid(NewIdx) && "Iterator index out of bound");
  190. Idx = NewIdx;
  191. return *this;
  192. }
  193. inline Self &operator-=(int RHS) { return operator+=(-RHS); }
  194. // Specially implement the [] operation using a proxy object to support
  195. // assignment.
  196. inline SuccessorProxy operator[](int Offset) {
  197. Self TmpIt = *this;
  198. TmpIt += Offset;
  199. return SuccessorProxy(TmpIt);
  200. }
  201. /// Get the source BlockT of this iterator.
  202. inline BlockT *getSource() {
  203. assert(Inst && "Source not available, if basic block was malformed");
  204. return Inst->getParent();
  205. }
  206. };
  207. using succ_iterator = SuccIterator<Instruction, BasicBlock>;
  208. using const_succ_iterator = SuccIterator<const Instruction, const BasicBlock>;
  209. using succ_range = iterator_range<succ_iterator>;
  210. using const_succ_range = iterator_range<const_succ_iterator>;
  211. inline succ_iterator succ_begin(Instruction *I) { return succ_iterator(I); }
  212. inline const_succ_iterator succ_begin(const Instruction *I) {
  213. return const_succ_iterator(I);
  214. }
  215. inline succ_iterator succ_end(Instruction *I) { return succ_iterator(I, true); }
  216. inline const_succ_iterator succ_end(const Instruction *I) {
  217. return const_succ_iterator(I, true);
  218. }
  219. inline bool succ_empty(const Instruction *I) {
  220. return succ_begin(I) == succ_end(I);
  221. }
  222. inline unsigned succ_size(const Instruction *I) {
  223. return std::distance(succ_begin(I), succ_end(I));
  224. }
  225. inline succ_range successors(Instruction *I) {
  226. return succ_range(succ_begin(I), succ_end(I));
  227. }
  228. inline const_succ_range successors(const Instruction *I) {
  229. return const_succ_range(succ_begin(I), succ_end(I));
  230. }
  231. inline succ_iterator succ_begin(BasicBlock *BB) {
  232. return succ_iterator(BB->getTerminator());
  233. }
  234. inline const_succ_iterator succ_begin(const BasicBlock *BB) {
  235. return const_succ_iterator(BB->getTerminator());
  236. }
  237. inline succ_iterator succ_end(BasicBlock *BB) {
  238. return succ_iterator(BB->getTerminator(), true);
  239. }
  240. inline const_succ_iterator succ_end(const BasicBlock *BB) {
  241. return const_succ_iterator(BB->getTerminator(), true);
  242. }
  243. inline bool succ_empty(const BasicBlock *BB) {
  244. return succ_begin(BB) == succ_end(BB);
  245. }
  246. inline unsigned succ_size(const BasicBlock *BB) {
  247. return std::distance(succ_begin(BB), succ_end(BB));
  248. }
  249. inline succ_range successors(BasicBlock *BB) {
  250. return succ_range(succ_begin(BB), succ_end(BB));
  251. }
  252. inline const_succ_range successors(const BasicBlock *BB) {
  253. return const_succ_range(succ_begin(BB), succ_end(BB));
  254. }
  255. //===--------------------------------------------------------------------===//
  256. // GraphTraits specializations for basic block graphs (CFGs)
  257. //===--------------------------------------------------------------------===//
  258. // Provide specializations of GraphTraits to be able to treat a function as a
  259. // graph of basic blocks...
  260. template <> struct GraphTraits<BasicBlock*> {
  261. using NodeRef = BasicBlock *;
  262. using ChildIteratorType = succ_iterator;
  263. static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
  264. static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
  265. static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
  266. };
  267. template <> struct GraphTraits<const BasicBlock*> {
  268. using NodeRef = const BasicBlock *;
  269. using ChildIteratorType = const_succ_iterator;
  270. static NodeRef getEntryNode(const BasicBlock *BB) { return BB; }
  271. static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
  272. static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
  273. };
  274. // Provide specializations of GraphTraits to be able to treat a function as a
  275. // graph of basic blocks... and to walk it in inverse order. Inverse order for
  276. // a function is considered to be when traversing the predecessor edges of a BB
  277. // instead of the successor edges.
  278. //
  279. template <> struct GraphTraits<Inverse<BasicBlock*>> {
  280. using NodeRef = BasicBlock *;
  281. using ChildIteratorType = pred_iterator;
  282. static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
  283. static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
  284. static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
  285. };
  286. template <> struct GraphTraits<Inverse<const BasicBlock*>> {
  287. using NodeRef = const BasicBlock *;
  288. using ChildIteratorType = const_pred_iterator;
  289. static NodeRef getEntryNode(Inverse<const BasicBlock *> G) { return G.Graph; }
  290. static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
  291. static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
  292. };
  293. //===--------------------------------------------------------------------===//
  294. // GraphTraits specializations for function basic block graphs (CFGs)
  295. //===--------------------------------------------------------------------===//
  296. // Provide specializations of GraphTraits to be able to treat a function as a
  297. // graph of basic blocks... these are the same as the basic block iterators,
  298. // except that the root node is implicitly the first node of the function.
  299. //
  300. template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
  301. static NodeRef getEntryNode(Function *F) { return &F->getEntryBlock(); }
  302. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  303. using nodes_iterator = pointer_iterator<Function::iterator>;
  304. static nodes_iterator nodes_begin(Function *F) {
  305. return nodes_iterator(F->begin());
  306. }
  307. static nodes_iterator nodes_end(Function *F) {
  308. return nodes_iterator(F->end());
  309. }
  310. static size_t size(Function *F) { return F->size(); }
  311. };
  312. template <> struct GraphTraits<const Function*> :
  313. public GraphTraits<const BasicBlock*> {
  314. static NodeRef getEntryNode(const Function *F) { return &F->getEntryBlock(); }
  315. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  316. using nodes_iterator = pointer_iterator<Function::const_iterator>;
  317. static nodes_iterator nodes_begin(const Function *F) {
  318. return nodes_iterator(F->begin());
  319. }
  320. static nodes_iterator nodes_end(const Function *F) {
  321. return nodes_iterator(F->end());
  322. }
  323. static size_t size(const Function *F) { return F->size(); }
  324. };
  325. // Provide specializations of GraphTraits to be able to treat a function as a
  326. // graph of basic blocks... and to walk it in inverse order. Inverse order for
  327. // a function is considered to be when traversing the predecessor edges of a BB
  328. // instead of the successor edges.
  329. //
  330. template <> struct GraphTraits<Inverse<Function*>> :
  331. public GraphTraits<Inverse<BasicBlock*>> {
  332. static NodeRef getEntryNode(Inverse<Function *> G) {
  333. return &G.Graph->getEntryBlock();
  334. }
  335. };
  336. template <> struct GraphTraits<Inverse<const Function*>> :
  337. public GraphTraits<Inverse<const BasicBlock*>> {
  338. static NodeRef getEntryNode(Inverse<const Function *> G) {
  339. return &G.Graph->getEntryBlock();
  340. }
  341. };
  342. } // end namespace llvm
  343. #endif // LLVM_IR_CFG_H
  344. #ifdef __GNUC__
  345. #pragma GCC diagnostic pop
  346. #endif