LLVM  4.0.0
InstIterator.h
Go to the documentation of this file.
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 
19 #ifndef LLVM_IR_INSTITERATOR_H
20 #define LLVM_IR_INSTITERATOR_H
21 
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Function.h"
26 #include <iterator>
27 
28 namespace llvm {
29 
30 // This class implements inst_begin() & inst_end() for
31 // inst_iterator and const_inst_iterator's.
32 //
33 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
34  typedef BB_t BBty;
35  typedef BB_i_t BBIty;
36  typedef BI_t BIty;
37  typedef II_t IIty;
38  BB_t *BBs; // BasicBlocksType
39  BB_i_t BB; // BasicBlocksType::iterator
40  BI_t BI; // BasicBlock::iterator
41 
42 public:
43  typedef std::bidirectional_iterator_tag iterator_category;
44  typedef IIty value_type;
45  typedef signed difference_type;
46  typedef IIty* pointer;
47  typedef IIty& reference;
48 
49  // Default constructor
50  InstIterator() = default;
51 
52  // Copy constructor...
53  template<typename A, typename B, typename C, typename D>
55  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
56 
57  template<typename A, typename B, typename C, typename D>
59  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
60 
61  template<class M> InstIterator(M &m)
62  : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
63  if (BB != BBs->end()) {
64  BI = BB->begin();
65  advanceToNextBB();
66  }
67  }
68 
69  template<class M> InstIterator(M &m, bool)
70  : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
71  }
72 
73  // Accessors to get at the underlying iterators...
74  inline BBIty &getBasicBlockIterator() { return BB; }
75  inline BIty &getInstructionIterator() { return BI; }
76 
77  inline reference operator*() const { return *BI; }
78  inline pointer operator->() const { return &operator*(); }
79 
80  inline bool operator==(const InstIterator &y) const {
81  return BB == y.BB && (BB == BBs->end() || BI == y.BI);
82  }
83  inline bool operator!=(const InstIterator& y) const {
84  return !operator==(y);
85  }
86 
88  ++BI;
89  advanceToNextBB();
90  return *this;
91  }
92  inline InstIterator operator++(int) {
93  InstIterator tmp = *this; ++*this; return tmp;
94  }
95 
97  while (BB == BBs->end() || BI == BB->begin()) {
98  --BB;
99  BI = BB->end();
100  }
101  --BI;
102  return *this;
103  }
104  inline InstIterator operator--(int) {
105  InstIterator tmp = *this; --*this; return tmp;
106  }
107 
108  inline bool atEnd() const { return BB == BBs->end(); }
109 
110 private:
111  inline void advanceToNextBB() {
112  // The only way that the II could be broken is if it is now pointing to
113  // the end() of the current BasicBlock and there are successor BBs.
114  while (BI == BB->end()) {
115  ++BB;
116  if (BB == BBs->end()) break;
117  BI = BB->begin();
118  }
119  }
120 };
121 
122 typedef InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
129 
131 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
133  return inst_range(inst_begin(F), inst_end(F));
134 }
136  return const_inst_iterator(*F);
137 }
139  return const_inst_iterator(*F, true);
140 }
142  return const_inst_range(inst_begin(F), inst_end(F));
143 }
145 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
147  return inst_range(inst_begin(F), inst_end(F));
148 }
150  return const_inst_iterator(F);
151 }
153  return const_inst_iterator(F, true);
154 }
156  return const_inst_range(inst_begin(F), inst_end(F));
157 }
158 
159 } // end namespace llvm
160 
161 #endif // LLVM_IR_INSTITERATOR_H
BasicBlockListType::const_iterator const_iterator
Definition: Function.h:55
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:241
InstIterator operator++(int)
Definition: InstIterator.h:92
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:233
InstIterator & operator++()
Definition: InstIterator.h:87
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
Definition: InstIterator.h:123
bool atEnd() const
Definition: InstIterator.h:108
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:130
InstIterator(InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:58
#define F(x, y, z)
Definition: MD5.cpp:51
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:84
InstIterator & operator--()
Definition: InstIterator.h:96
pointer operator->() const
Definition: InstIterator.h:78
BBIty & getBasicBlockIterator()
Definition: InstIterator.h:74
InstIterator()=default
bool operator==(const InstIterator &y) const
Definition: InstIterator.h:80
InstIterator< const SymbolTableList< BasicBlock >, Function::const_iterator, BasicBlock::const_iterator, const Instruction > const_inst_iterator
Definition: InstIterator.h:126
BIty & getInstructionIterator()
Definition: InstIterator.h:75
InstIterator(M &m, bool)
Definition: InstIterator.h:69
BasicBlockListType::iterator iterator
Definition: Function.h:54
iterator_range< inst_iterator > inst_range
Definition: InstIterator.h:127
iterator_range< const_inst_iterator > const_inst_range
Definition: InstIterator.h:128
bool operator!=(const InstIterator &y) const
Definition: InstIterator.h:83
A range adaptor for a pair of iterators.
InstIterator operator--(int)
Definition: InstIterator.h:104
inst_range instructions(Function *F)
Definition: InstIterator.h:132
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:131
reference operator*() const
Definition: InstIterator.h:77
std::bidirectional_iterator_tag iterator_category
Definition: InstIterator.h:43
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:83
InstIterator(const InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:54