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