MachineInstrBundleIterator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/MachineInstrBundleIterator.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. //
  14. // Defines an iterator class that bundles MachineInstr.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLEITERATOR_H
  18. #define LLVM_CODEGEN_MACHINEINSTRBUNDLEITERATOR_H
  19. #include "llvm/ADT/ilist.h"
  20. #include "llvm/ADT/simple_ilist.h"
  21. #include <cassert>
  22. #include <iterator>
  23. #include <type_traits>
  24. namespace llvm {
  25. template <class T, bool IsReverse> struct MachineInstrBundleIteratorTraits;
  26. template <class T> struct MachineInstrBundleIteratorTraits<T, false> {
  27. using list_type = simple_ilist<T, ilist_sentinel_tracking<true>>;
  28. using instr_iterator = typename list_type::iterator;
  29. using nonconst_instr_iterator = typename list_type::iterator;
  30. using const_instr_iterator = typename list_type::const_iterator;
  31. };
  32. template <class T> struct MachineInstrBundleIteratorTraits<T, true> {
  33. using list_type = simple_ilist<T, ilist_sentinel_tracking<true>>;
  34. using instr_iterator = typename list_type::reverse_iterator;
  35. using nonconst_instr_iterator = typename list_type::reverse_iterator;
  36. using const_instr_iterator = typename list_type::const_reverse_iterator;
  37. };
  38. template <class T> struct MachineInstrBundleIteratorTraits<const T, false> {
  39. using list_type = simple_ilist<T, ilist_sentinel_tracking<true>>;
  40. using instr_iterator = typename list_type::const_iterator;
  41. using nonconst_instr_iterator = typename list_type::iterator;
  42. using const_instr_iterator = typename list_type::const_iterator;
  43. };
  44. template <class T> struct MachineInstrBundleIteratorTraits<const T, true> {
  45. using list_type = simple_ilist<T, ilist_sentinel_tracking<true>>;
  46. using instr_iterator = typename list_type::const_reverse_iterator;
  47. using nonconst_instr_iterator = typename list_type::reverse_iterator;
  48. using const_instr_iterator = typename list_type::const_reverse_iterator;
  49. };
  50. template <bool IsReverse> struct MachineInstrBundleIteratorHelper;
  51. template <> struct MachineInstrBundleIteratorHelper<false> {
  52. /// Get the beginning of the current bundle.
  53. template <class Iterator> static Iterator getBundleBegin(Iterator I) {
  54. if (!I.isEnd())
  55. while (I->isBundledWithPred())
  56. --I;
  57. return I;
  58. }
  59. /// Get the final node of the current bundle.
  60. template <class Iterator> static Iterator getBundleFinal(Iterator I) {
  61. if (!I.isEnd())
  62. while (I->isBundledWithSucc())
  63. ++I;
  64. return I;
  65. }
  66. /// Increment forward ilist iterator.
  67. template <class Iterator> static void increment(Iterator &I) {
  68. I = std::next(getBundleFinal(I));
  69. }
  70. /// Decrement forward ilist iterator.
  71. template <class Iterator> static void decrement(Iterator &I) {
  72. I = getBundleBegin(std::prev(I));
  73. }
  74. };
  75. template <> struct MachineInstrBundleIteratorHelper<true> {
  76. /// Get the beginning of the current bundle.
  77. template <class Iterator> static Iterator getBundleBegin(Iterator I) {
  78. return MachineInstrBundleIteratorHelper<false>::getBundleBegin(
  79. I.getReverse())
  80. .getReverse();
  81. }
  82. /// Get the final node of the current bundle.
  83. template <class Iterator> static Iterator getBundleFinal(Iterator I) {
  84. return MachineInstrBundleIteratorHelper<false>::getBundleFinal(
  85. I.getReverse())
  86. .getReverse();
  87. }
  88. /// Increment reverse ilist iterator.
  89. template <class Iterator> static void increment(Iterator &I) {
  90. I = getBundleBegin(std::next(I));
  91. }
  92. /// Decrement reverse ilist iterator.
  93. template <class Iterator> static void decrement(Iterator &I) {
  94. I = std::prev(getBundleFinal(I));
  95. }
  96. };
  97. /// MachineBasicBlock iterator that automatically skips over MIs that are
  98. /// inside bundles (i.e. walk top level MIs only).
  99. template <typename Ty, bool IsReverse = false>
  100. class MachineInstrBundleIterator : MachineInstrBundleIteratorHelper<IsReverse> {
  101. using Traits = MachineInstrBundleIteratorTraits<Ty, IsReverse>;
  102. using instr_iterator = typename Traits::instr_iterator;
  103. instr_iterator MII;
  104. public:
  105. using value_type = typename instr_iterator::value_type;
  106. using difference_type = typename instr_iterator::difference_type;
  107. using pointer = typename instr_iterator::pointer;
  108. using reference = typename instr_iterator::reference;
  109. using const_pointer = typename instr_iterator::const_pointer;
  110. using const_reference = typename instr_iterator::const_reference;
  111. using iterator_category = std::bidirectional_iterator_tag;
  112. private:
  113. using nonconst_instr_iterator = typename Traits::nonconst_instr_iterator;
  114. using const_instr_iterator = typename Traits::const_instr_iterator;
  115. using nonconst_iterator =
  116. MachineInstrBundleIterator<typename nonconst_instr_iterator::value_type,
  117. IsReverse>;
  118. using reverse_iterator = MachineInstrBundleIterator<Ty, !IsReverse>;
  119. public:
  120. MachineInstrBundleIterator(instr_iterator MI) : MII(MI) {
  121. assert((!MI.getNodePtr() || MI.isEnd() || !MI->isBundledWithPred()) &&
  122. "It's not legal to initialize MachineInstrBundleIterator with a "
  123. "bundled MI");
  124. }
  125. MachineInstrBundleIterator(reference MI) : MII(MI) {
  126. assert(!MI.isBundledWithPred() && "It's not legal to initialize "
  127. "MachineInstrBundleIterator with a "
  128. "bundled MI");
  129. }
  130. MachineInstrBundleIterator(pointer MI) : MII(MI) {
  131. // FIXME: This conversion should be explicit.
  132. assert((!MI || !MI->isBundledWithPred()) && "It's not legal to initialize "
  133. "MachineInstrBundleIterator "
  134. "with a bundled MI");
  135. }
  136. // Template allows conversion from const to nonconst.
  137. template <class OtherTy>
  138. MachineInstrBundleIterator(
  139. const MachineInstrBundleIterator<OtherTy, IsReverse> &I,
  140. std::enable_if_t<std::is_convertible<OtherTy *, Ty *>::value, void *> =
  141. nullptr)
  142. : MII(I.getInstrIterator()) {}
  143. MachineInstrBundleIterator() : MII(nullptr) {}
  144. /// Explicit conversion between forward/reverse iterators.
  145. ///
  146. /// Translate between forward and reverse iterators without changing range
  147. /// boundaries. The resulting iterator will dereference (and have a handle)
  148. /// to the previous node, which is somewhat unexpected; but converting the
  149. /// two endpoints in a range will give the same range in reverse.
  150. ///
  151. /// This matches std::reverse_iterator conversions.
  152. explicit MachineInstrBundleIterator(
  153. const MachineInstrBundleIterator<Ty, !IsReverse> &I)
  154. : MachineInstrBundleIterator(++I.getReverse()) {}
  155. /// Get the bundle iterator for the given instruction's bundle.
  156. static MachineInstrBundleIterator getAtBundleBegin(instr_iterator MI) {
  157. return MachineInstrBundleIteratorHelper<IsReverse>::getBundleBegin(MI);
  158. }
  159. reference operator*() const { return *MII; }
  160. pointer operator->() const { return &operator*(); }
  161. /// Check for null.
  162. bool isValid() const { return MII.getNodePtr(); }
  163. friend bool operator==(const MachineInstrBundleIterator &L,
  164. const MachineInstrBundleIterator &R) {
  165. return L.MII == R.MII;
  166. }
  167. friend bool operator==(const MachineInstrBundleIterator &L,
  168. const const_instr_iterator &R) {
  169. return L.MII == R; // Avoid assertion about validity of R.
  170. }
  171. friend bool operator==(const const_instr_iterator &L,
  172. const MachineInstrBundleIterator &R) {
  173. return L == R.MII; // Avoid assertion about validity of L.
  174. }
  175. friend bool operator==(const MachineInstrBundleIterator &L,
  176. const nonconst_instr_iterator &R) {
  177. return L.MII == R; // Avoid assertion about validity of R.
  178. }
  179. friend bool operator==(const nonconst_instr_iterator &L,
  180. const MachineInstrBundleIterator &R) {
  181. return L == R.MII; // Avoid assertion about validity of L.
  182. }
  183. friend bool operator==(const MachineInstrBundleIterator &L, const_pointer R) {
  184. return L == const_instr_iterator(R); // Avoid assertion about validity of R.
  185. }
  186. friend bool operator==(const_pointer L, const MachineInstrBundleIterator &R) {
  187. return const_instr_iterator(L) == R; // Avoid assertion about validity of L.
  188. }
  189. friend bool operator==(const MachineInstrBundleIterator &L,
  190. const_reference R) {
  191. return L == &R; // Avoid assertion about validity of R.
  192. }
  193. friend bool operator==(const_reference L,
  194. const MachineInstrBundleIterator &R) {
  195. return &L == R; // Avoid assertion about validity of L.
  196. }
  197. friend bool operator!=(const MachineInstrBundleIterator &L,
  198. const MachineInstrBundleIterator &R) {
  199. return !(L == R);
  200. }
  201. friend bool operator!=(const MachineInstrBundleIterator &L,
  202. const const_instr_iterator &R) {
  203. return !(L == R);
  204. }
  205. friend bool operator!=(const const_instr_iterator &L,
  206. const MachineInstrBundleIterator &R) {
  207. return !(L == R);
  208. }
  209. friend bool operator!=(const MachineInstrBundleIterator &L,
  210. const nonconst_instr_iterator &R) {
  211. return !(L == R);
  212. }
  213. friend bool operator!=(const nonconst_instr_iterator &L,
  214. const MachineInstrBundleIterator &R) {
  215. return !(L == R);
  216. }
  217. friend bool operator!=(const MachineInstrBundleIterator &L, const_pointer R) {
  218. return !(L == R);
  219. }
  220. friend bool operator!=(const_pointer L, const MachineInstrBundleIterator &R) {
  221. return !(L == R);
  222. }
  223. friend bool operator!=(const MachineInstrBundleIterator &L,
  224. const_reference R) {
  225. return !(L == R);
  226. }
  227. friend bool operator!=(const_reference L,
  228. const MachineInstrBundleIterator &R) {
  229. return !(L == R);
  230. }
  231. // Increment and decrement operators...
  232. MachineInstrBundleIterator &operator--() {
  233. this->decrement(MII);
  234. return *this;
  235. }
  236. MachineInstrBundleIterator &operator++() {
  237. this->increment(MII);
  238. return *this;
  239. }
  240. MachineInstrBundleIterator operator--(int) {
  241. MachineInstrBundleIterator Temp = *this;
  242. --*this;
  243. return Temp;
  244. }
  245. MachineInstrBundleIterator operator++(int) {
  246. MachineInstrBundleIterator Temp = *this;
  247. ++*this;
  248. return Temp;
  249. }
  250. instr_iterator getInstrIterator() const { return MII; }
  251. nonconst_iterator getNonConstIterator() const { return MII.getNonConst(); }
  252. /// Get a reverse iterator to the same node.
  253. ///
  254. /// Gives a reverse iterator that will dereference (and have a handle) to the
  255. /// same node. Converting the endpoint iterators in a range will give a
  256. /// different range; for range operations, use the explicit conversions.
  257. reverse_iterator getReverse() const { return MII.getReverse(); }
  258. };
  259. } // end namespace llvm
  260. #endif // LLVM_CODEGEN_MACHINEINSTRBUNDLEITERATOR_H
  261. #ifdef __GNUC__
  262. #pragma GCC diagnostic pop
  263. #endif