LLVM  8.0.1
MachineInstrBundleIterator.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/MachineInstrBundleIterator.h ----------------*- 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 // Defines an iterator class that bundles MachineInstr.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLEITERATOR_H
15 #define LLVM_CODEGEN_MACHINEINSTRBUNDLEITERATOR_H
16 
17 #include "llvm/ADT/ilist.h"
18 #include "llvm/ADT/simple_ilist.h"
19 #include <cassert>
20 #include <iterator>
21 #include <type_traits>
22 
23 namespace llvm {
24 
25 template <class T, bool IsReverse> struct MachineInstrBundleIteratorTraits;
26 template <class T> struct MachineInstrBundleIteratorTraits<T, false> {
31 };
32 template <class T> struct MachineInstrBundleIteratorTraits<T, true> {
37 };
38 template <class T> struct MachineInstrBundleIteratorTraits<const T, false> {
43 };
44 template <class T> struct MachineInstrBundleIteratorTraits<const T, true> {
49 };
50 
51 template <bool IsReverse> struct MachineInstrBundleIteratorHelper;
53  /// Get the beginning of the current bundle.
54  template <class Iterator> static Iterator getBundleBegin(Iterator I) {
55  if (!I.isEnd())
56  while (I->isBundledWithPred())
57  --I;
58  return I;
59  }
60 
61  /// Get the final node of the current bundle.
62  template <class Iterator> static Iterator getBundleFinal(Iterator I) {
63  if (!I.isEnd())
64  while (I->isBundledWithSucc())
65  ++I;
66  return I;
67  }
68 
69  /// Increment forward ilist iterator.
70  template <class Iterator> static void increment(Iterator &I) {
71  I = std::next(getBundleFinal(I));
72  }
73 
74  /// Decrement forward ilist iterator.
75  template <class Iterator> static void decrement(Iterator &I) {
76  I = getBundleBegin(std::prev(I));
77  }
78 };
79 
81  /// Get the beginning of the current bundle.
82  template <class Iterator> static Iterator getBundleBegin(Iterator I) {
84  I.getReverse())
85  .getReverse();
86  }
87 
88  /// Get the final node of the current bundle.
89  template <class Iterator> static Iterator getBundleFinal(Iterator I) {
91  I.getReverse())
92  .getReverse();
93  }
94 
95  /// Increment reverse ilist iterator.
96  template <class Iterator> static void increment(Iterator &I) {
97  I = getBundleBegin(std::next(I));
98  }
99 
100  /// Decrement reverse ilist iterator.
101  template <class Iterator> static void decrement(Iterator &I) {
102  I = std::prev(getBundleFinal(I));
103  }
104 };
105 
106 /// MachineBasicBlock iterator that automatically skips over MIs that are
107 /// inside bundles (i.e. walk top level MIs only).
108 template <typename Ty, bool IsReverse = false>
111  using instr_iterator = typename Traits::instr_iterator;
112 
113  instr_iterator MII;
114 
115 public:
116  using value_type = typename instr_iterator::value_type;
117  using difference_type = typename instr_iterator::difference_type;
118  using pointer = typename instr_iterator::pointer;
119  using reference = typename instr_iterator::reference;
120  using const_pointer = typename instr_iterator::const_pointer;
121  using const_reference = typename instr_iterator::const_reference;
122  using iterator_category = std::bidirectional_iterator_tag;
123 
124 private:
125  using nonconst_instr_iterator = typename Traits::nonconst_instr_iterator;
126  using const_instr_iterator = typename Traits::const_instr_iterator;
127  using nonconst_iterator =
128  MachineInstrBundleIterator<typename nonconst_instr_iterator::value_type,
129  IsReverse>;
131 
132 public:
133  MachineInstrBundleIterator(instr_iterator MI) : MII(MI) {
134  assert((!MI.getNodePtr() || MI.isEnd() || !MI->isBundledWithPred()) &&
135  "It's not legal to initialize MachineInstrBundleIterator with a "
136  "bundled MI");
137  }
138 
140  assert(!MI.isBundledWithPred() && "It's not legal to initialize "
141  "MachineInstrBundleIterator with a "
142  "bundled MI");
143  }
144 
146  // FIXME: This conversion should be explicit.
147  assert((!MI || !MI->isBundledWithPred()) && "It's not legal to initialize "
148  "MachineInstrBundleIterator "
149  "with a bundled MI");
150  }
151 
152  // Template allows conversion from const to nonconst.
153  template <class OtherTy>
156  typename std::enable_if<std::is_convertible<OtherTy *, Ty *>::value,
157  void *>::type = nullptr)
158  : MII(I.getInstrIterator()) {}
159 
160  MachineInstrBundleIterator() : MII(nullptr) {}
161 
162  /// Explicit conversion between forward/reverse iterators.
163  ///
164  /// Translate between forward and reverse iterators without changing range
165  /// boundaries. The resulting iterator will dereference (and have a handle)
166  /// to the previous node, which is somewhat unexpected; but converting the
167  /// two endpoints in a range will give the same range in reverse.
168  ///
169  /// This matches std::reverse_iterator conversions.
172  : MachineInstrBundleIterator(++I.getReverse()) {}
173 
174  /// Get the bundle iterator for the given instruction's bundle.
177  }
178 
179  reference operator*() const { return *MII; }
180  pointer operator->() const { return &operator*(); }
181 
182  /// Check for null.
183  bool isValid() const { return MII.getNodePtr(); }
184 
186  const MachineInstrBundleIterator &R) {
187  return L.MII == R.MII;
188  }
190  const const_instr_iterator &R) {
191  return L.MII == R; // Avoid assertion about validity of R.
192  }
193  friend bool operator==(const const_instr_iterator &L,
194  const MachineInstrBundleIterator &R) {
195  return L == R.MII; // Avoid assertion about validity of L.
196  }
198  const nonconst_instr_iterator &R) {
199  return L.MII == R; // Avoid assertion about validity of R.
200  }
201  friend bool operator==(const nonconst_instr_iterator &L,
202  const MachineInstrBundleIterator &R) {
203  return L == R.MII; // Avoid assertion about validity of L.
204  }
206  return L == const_instr_iterator(R); // Avoid assertion about validity of R.
207  }
209  return const_instr_iterator(L) == R; // Avoid assertion about validity of L.
210  }
212  const_reference R) {
213  return L == &R; // Avoid assertion about validity of R.
214  }
216  const MachineInstrBundleIterator &R) {
217  return &L == R; // Avoid assertion about validity of L.
218  }
219 
221  const MachineInstrBundleIterator &R) {
222  return !(L == R);
223  }
225  const const_instr_iterator &R) {
226  return !(L == R);
227  }
228  friend bool operator!=(const const_instr_iterator &L,
229  const MachineInstrBundleIterator &R) {
230  return !(L == R);
231  }
233  const nonconst_instr_iterator &R) {
234  return !(L == R);
235  }
236  friend bool operator!=(const nonconst_instr_iterator &L,
237  const MachineInstrBundleIterator &R) {
238  return !(L == R);
239  }
241  return !(L == R);
242  }
244  return !(L == R);
245  }
247  const_reference R) {
248  return !(L == R);
249  }
251  const MachineInstrBundleIterator &R) {
252  return !(L == R);
253  }
254 
255  // Increment and decrement operators...
257  this->decrement(MII);
258  return *this;
259  }
261  this->increment(MII);
262  return *this;
263  }
265  MachineInstrBundleIterator Temp = *this;
266  --*this;
267  return Temp;
268  }
270  MachineInstrBundleIterator Temp = *this;
271  ++*this;
272  return Temp;
273  }
274 
275  instr_iterator getInstrIterator() const { return MII; }
276 
277  nonconst_iterator getNonConstIterator() const { return MII.getNonConst(); }
278 
279  /// Get a reverse iterator to the same node.
280  ///
281  /// Gives a reverse iterator that will dereference (and have a handle) to the
282  /// same node. Converting the endpoint iterators in a range will give a
283  /// different range; for range operations, use the explicit conversions.
284  reverse_iterator getReverse() const { return MII.getReverse(); }
285 };
286 
287 } // end namespace llvm
288 
289 #endif // LLVM_CODEGEN_MACHINEINSTRBUNDLEITERATOR_H
MachineInstrBundleIterator & operator--()
static void decrement(Iterator &I)
Decrement reverse ilist iterator.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
typename list_type::reverse_iterator nonconst_instr_iterator
friend bool operator!=(const MachineInstrBundleIterator &L, const nonconst_instr_iterator &R)
MachineInstrBundleIterator(const MachineInstrBundleIterator< Ty, !IsReverse > &I)
Explicit conversion between forward/reverse iterators.
block Block Frequency true
friend bool operator!=(const_reference L, const MachineInstrBundleIterator &R)
static Iterator getBundleFinal(Iterator I)
Get the final node of the current bundle.
typename list_type::const_reverse_iterator instr_iterator
friend bool operator==(const MachineInstrBundleIterator &L, const_pointer R)
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
friend bool operator!=(const MachineInstrBundleIterator &L, const_reference R)
static void increment(Iterator &I)
Increment forward ilist iterator.
friend bool operator==(const MachineInstrBundleIterator &L, const const_instr_iterator &R)
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2091
friend bool operator==(const_pointer L, const MachineInstrBundleIterator &R)
A simple intrusive list implementation.
Definition: simple_ilist.h:79
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
friend bool operator==(const MachineInstrBundleIterator &L, const_reference R)
friend bool operator!=(const const_instr_iterator &L, const MachineInstrBundleIterator &R)
friend bool operator!=(const_pointer L, const MachineInstrBundleIterator &R)
friend bool operator!=(const MachineInstrBundleIterator &L, const_pointer R)
typename list_type::const_reverse_iterator const_instr_iterator
static MachineInstrBundleIterator getAtBundleBegin(instr_iterator MI)
Get the bundle iterator for the given instruction&#39;s bundle.
friend bool operator!=(const nonconst_instr_iterator &L, const MachineInstrBundleIterator &R)
static Iterator getBundleFinal(Iterator I)
Get the final node of the current bundle.
Iterator for intrusive lists based on ilist_node.
MachineInstrBundleIterator operator++(int)
friend bool operator==(const_reference L, const MachineInstrBundleIterator &R)
MachineInstrBundleIterator & operator++()
static Iterator getBundleBegin(Iterator I)
Get the beginning of the current bundle.
typename list_type::const_reverse_iterator const_instr_iterator
friend bool operator==(const MachineInstrBundleIterator &L, const MachineInstrBundleIterator &R)
#define I(x, y, z)
Definition: MD5.cpp:58
MachineInstrBundleIterator(const MachineInstrBundleIterator< OtherTy, IsReverse > &I, typename std::enable_if< std::is_convertible< OtherTy *, Ty *>::value, void *>::type=nullptr)
static void increment(Iterator &I)
Increment reverse ilist iterator.
friend bool operator==(const const_instr_iterator &L, const MachineInstrBundleIterator &R)
MachineInstrBundleIterator operator--(int)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void decrement(Iterator &I)
Decrement forward ilist iterator.
aarch64 promote const
nonconst_iterator getNonConstIterator() const
friend bool operator==(const MachineInstrBundleIterator &L, const nonconst_instr_iterator &R)
friend bool operator!=(const MachineInstrBundleIterator &L, const MachineInstrBundleIterator &R)
IRTranslator LLVM IR MI
friend bool operator!=(const MachineInstrBundleIterator &L, const const_instr_iterator &R)
friend bool operator==(const nonconst_instr_iterator &L, const MachineInstrBundleIterator &R)
static Iterator getBundleBegin(Iterator I)
Get the beginning of the current bundle.