InstIterator.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //===- InstIterator.h - Classes for inst iteration --------------*- 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 contains definitions of two iterators for iterating over the
  11. // instructions in a function. This is effectively a wrapper around a two level
  12. // iterator that can probably be genericized later.
  13. //
  14. // Note that this iterator gets invalidated any time that basic blocks or
  15. // instructions are moved around.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_INSTITERATOR_H
  19. #define LLVM_IR_INSTITERATOR_H
  20. #include "llvm/IR/BasicBlock.h"
  21. #include "llvm/IR/Function.h"
  22. namespace llvm {
  23. // This class implements inst_begin() & inst_end() for
  24. // inst_iterator and const_inst_iterator's.
  25. //
  26. template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
  27. typedef BB_t BBty;
  28. typedef BB_i_t BBIty;
  29. typedef BI_t BIty;
  30. typedef II_t IIty;
  31. BB_t *BBs; // BasicBlocksType
  32. BB_i_t BB; // BasicBlocksType::iterator
  33. BI_t BI; // BasicBlock::iterator
  34. public:
  35. typedef std::bidirectional_iterator_tag iterator_category;
  36. typedef IIty value_type;
  37. typedef signed difference_type;
  38. typedef IIty* pointer;
  39. typedef IIty& reference;
  40. // Default constructor
  41. InstIterator() {}
  42. // Copy constructor...
  43. template<typename A, typename B, typename C, typename D>
  44. InstIterator(const InstIterator<A,B,C,D> &II)
  45. : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
  46. template<typename A, typename B, typename C, typename D>
  47. InstIterator(InstIterator<A,B,C,D> &II)
  48. : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
  49. template<class M> InstIterator(M &m)
  50. : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
  51. if (BB != BBs->end()) {
  52. BI = BB->begin();
  53. advanceToNextBB();
  54. }
  55. }
  56. template<class M> InstIterator(M &m, bool)
  57. : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
  58. }
  59. // Accessors to get at the underlying iterators...
  60. inline BBIty &getBasicBlockIterator() { return BB; }
  61. inline BIty &getInstructionIterator() { return BI; }
  62. inline reference operator*() const { return *BI; }
  63. inline pointer operator->() const { return &operator*(); }
  64. inline bool operator==(const InstIterator &y) const {
  65. return BB == y.BB && (BB == BBs->end() || BI == y.BI);
  66. }
  67. inline bool operator!=(const InstIterator& y) const {
  68. return !operator==(y);
  69. }
  70. InstIterator& operator++() {
  71. ++BI;
  72. advanceToNextBB();
  73. return *this;
  74. }
  75. inline InstIterator operator++(int) {
  76. InstIterator tmp = *this; ++*this; return tmp;
  77. }
  78. InstIterator& operator--() {
  79. while (BB == BBs->end() || BI == BB->begin()) {
  80. --BB;
  81. BI = BB->end();
  82. }
  83. --BI;
  84. return *this;
  85. }
  86. inline InstIterator operator--(int) {
  87. InstIterator tmp = *this; --*this; return tmp;
  88. }
  89. inline bool atEnd() const { return BB == BBs->end(); }
  90. private:
  91. inline void advanceToNextBB() {
  92. // The only way that the II could be broken is if it is now pointing to
  93. // the end() of the current BasicBlock and there are successor BBs.
  94. while (BI == BB->end()) {
  95. ++BB;
  96. if (BB == BBs->end()) break;
  97. BI = BB->begin();
  98. }
  99. }
  100. };
  101. typedef InstIterator<iplist<BasicBlock>,
  102. Function::iterator, BasicBlock::iterator,
  103. Instruction> inst_iterator;
  104. typedef InstIterator<const iplist<BasicBlock>,
  105. Function::const_iterator,
  106. BasicBlock::const_iterator,
  107. const Instruction> const_inst_iterator;
  108. inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
  109. inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
  110. inline iterator_range<inst_iterator> inst_range(Function *F) {
  111. return iterator_range<inst_iterator>(inst_begin(F), inst_end(F));
  112. }
  113. inline const_inst_iterator inst_begin(const Function *F) {
  114. return const_inst_iterator(*F);
  115. }
  116. inline const_inst_iterator inst_end(const Function *F) {
  117. return const_inst_iterator(*F, true);
  118. }
  119. inline iterator_range<const_inst_iterator> inst_range(const Function *F) {
  120. return iterator_range<const_inst_iterator>(inst_begin(F), inst_end(F));
  121. }
  122. inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
  123. inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
  124. inline iterator_range<inst_iterator> inst_range(Function &F) {
  125. return iterator_range<inst_iterator>(inst_begin(F), inst_end(F));
  126. }
  127. inline const_inst_iterator inst_begin(const Function &F) {
  128. return const_inst_iterator(F);
  129. }
  130. inline const_inst_iterator inst_end(const Function &F) {
  131. return const_inst_iterator(F, true);
  132. }
  133. inline iterator_range<const_inst_iterator> inst_range(const Function &F) {
  134. return iterator_range<const_inst_iterator>(inst_begin(F), inst_end(F));
  135. }
  136. } // End llvm namespace
  137. #endif