LLVM  10.0.0svn
InstIterator.h
Go to the documentation of this file.
1 //===- InstIterator.h - Classes for inst iteration --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains definitions of two iterators for iterating over the
10 // instructions in a function. This is effectively a wrapper around a two level
11 // iterator that can probably be genericized later.
12 //
13 // Note that this iterator gets invalidated any time that basic blocks or
14 // instructions are moved around.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_IR_INSTITERATOR_H
19 #define LLVM_IR_INSTITERATOR_H
20 
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
25 #include <iterator>
26 
27 namespace llvm {
28 
29 // This class implements inst_begin() & inst_end() for
30 // inst_iterator and const_inst_iterator's.
31 //
32 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
33  using BBty = BB_t;
34  using BBIty = BB_i_t;
35  using BIty = BI_t;
36  using IIty = II_t;
37  BB_t *BBs; // BasicBlocksType
38  BB_i_t BB; // BasicBlocksType::iterator
39  BI_t BI; // BasicBlock::iterator
40 
41 public:
42  using iterator_category = std::bidirectional_iterator_tag;
43  using value_type = IIty;
44  using difference_type = signed;
45  using pointer = IIty *;
46  using reference = IIty &;
47 
48  // Default constructor
49  InstIterator() = default;
50 
51  // Copy constructor...
52  template<typename A, typename B, typename C, typename D>
54  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
55 
56  template<typename A, typename B, typename C, typename D>
58  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
59 
60  template<class M> InstIterator(M &m)
61  : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
62  if (BB != BBs->end()) {
63  BI = BB->begin();
64  advanceToNextBB();
65  }
66  }
67 
68  template<class M> InstIterator(M &m, bool)
69  : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
70  }
71 
72  // Accessors to get at the underlying iterators...
73  inline BBIty &getBasicBlockIterator() { return BB; }
74  inline BIty &getInstructionIterator() { return BI; }
75 
76  inline reference operator*() const { return *BI; }
77  inline pointer operator->() const { return &operator*(); }
78 
79  inline bool operator==(const InstIterator &y) const {
80  return BB == y.BB && (BB == BBs->end() || BI == y.BI);
81  }
82  inline bool operator!=(const InstIterator& y) const {
83  return !operator==(y);
84  }
85 
87  ++BI;
88  advanceToNextBB();
89  return *this;
90  }
91  inline InstIterator operator++(int) {
92  InstIterator tmp = *this; ++*this; return tmp;
93  }
94 
96  while (BB == BBs->end() || BI == BB->begin()) {
97  --BB;
98  BI = BB->end();
99  }
100  --BI;
101  return *this;
102  }
103  inline InstIterator operator--(int) {
104  InstIterator tmp = *this; --*this; return tmp;
105  }
106 
107  inline bool atEnd() const { return BB == BBs->end(); }
108 
109 private:
110  inline void advanceToNextBB() {
111  // The only way that the II could be broken is if it is now pointing to
112  // the end() of the current BasicBlock and there are successor BBs.
113  while (BI == BB->end()) {
114  ++BB;
115  if (BB == BBs->end()) break;
116  BI = BB->begin();
117  }
118  }
119 };
120 
121 using inst_iterator =
124 using const_inst_iterator =
127  const Instruction>;
130 
132 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
134  return inst_range(inst_begin(F), inst_end(F));
135 }
137  return const_inst_iterator(*F);
138 }
140  return const_inst_iterator(*F, true);
141 }
143  return const_inst_range(inst_begin(F), inst_end(F));
144 }
146 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
148  return inst_range(inst_begin(F), inst_end(F));
149 }
151  return const_inst_iterator(F);
152 }
154  return const_inst_iterator(F, true);
155 }
157  return const_inst_range(inst_begin(F), inst_end(F));
158 }
159 
160 } // end namespace llvm
161 
162 #endif // LLVM_IR_INSTITERATOR_H
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool operator!=(const InstIterator &y) const
Definition: InstIterator.h:82
InstIterator operator++(int)
Definition: InstIterator.h:91
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool operator==(const InstIterator &y) const
Definition: InstIterator.h:79
InstIterator & operator++()
Definition: InstIterator.h:86
F(f)
reference operator*() const
Definition: InstIterator.h:76
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:90
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
Definition: InstIterator.h:123
InstIterator(InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:57
BasicBlockListType::iterator iterator
Definition: Function.h:64
InstIterator & operator--()
Definition: InstIterator.h:95
BasicBlockListType::const_iterator const_iterator
Definition: Function.h:65
bool atEnd() const
Definition: InstIterator.h:107
BBIty & getBasicBlockIterator()
Definition: InstIterator.h:73
InstIterator()=default
InstIterator< const SymbolTableList< BasicBlock >, Function::const_iterator, BasicBlock::const_iterator, const Instruction > const_inst_iterator
Definition: InstIterator.h:127
BIty & getInstructionIterator()
Definition: InstIterator.h:74
InstIterator(M &m, bool)
Definition: InstIterator.h:68
std::bidirectional_iterator_tag iterator_category
Definition: InstIterator.h:42
A range adaptor for a pair of iterators.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:89
iterator_range< inst_iterator > inst_range
Definition: InstIterator.h:128
InstIterator operator--(int)
Definition: InstIterator.h:103
iterator_range< const_inst_iterator > const_inst_range
Definition: InstIterator.h:129
inst_range instructions(Function *F)
Definition: InstIterator.h:133
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
InstIterator(const InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:53
pointer operator->() const
Definition: InstIterator.h:77