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
InstIterator()46 InstIterator() {}
47
48 // Copy constructor...
49 template<typename A, typename B, typename C, typename D>
InstIterator(const InstIterator<A,B,C,D> & II)50 InstIterator(const InstIterator<A,B,C,D> &II)
51 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
52
53 template<typename A, typename B, typename C, typename D>
InstIterator(InstIterator<A,B,C,D> & II)54 InstIterator(InstIterator<A,B,C,D> &II)
55 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
56
InstIterator(M & m)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
InstIterator(M & m,bool)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...
getBasicBlockIterator()70 inline BBIty &getBasicBlockIterator() { return BB; }
getInstructionIterator()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
83 InstIterator& operator++() {
84 ++BI;
85 advanceToNextBB();
86 return *this;
87 }
88 inline InstIterator operator++(int) {
89 InstIterator tmp = *this; ++*this; return tmp;
90 }
91
92 InstIterator& operator--() {
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
atEnd()104 inline bool atEnd() const { return BB == BBs->end(); }
105
106 private:
advanceToNextBB()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 typedef InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
119 BasicBlock::iterator, Instruction> inst_iterator;
120 typedef InstIterator<const SymbolTableList<BasicBlock>,
121 Function::const_iterator, BasicBlock::const_iterator,
122 const Instruction> const_inst_iterator;
123 typedef iterator_range<inst_iterator> inst_range;
124 typedef iterator_range<const_inst_iterator> const_inst_range;
125
inst_begin(Function * F)126 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
inst_end(Function * F)127 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
instructions(Function * F)128 inline inst_range instructions(Function *F) {
129 return inst_range(inst_begin(F), inst_end(F));
130 }
inst_begin(const Function * F)131 inline const_inst_iterator inst_begin(const Function *F) {
132 return const_inst_iterator(*F);
133 }
inst_end(const Function * F)134 inline const_inst_iterator inst_end(const Function *F) {
135 return const_inst_iterator(*F, true);
136 }
instructions(const Function * F)137 inline const_inst_range instructions(const Function *F) {
138 return const_inst_range(inst_begin(F), inst_end(F));
139 }
inst_begin(Function & F)140 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
inst_end(Function & F)141 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
instructions(Function & F)142 inline inst_range instructions(Function &F) {
143 return inst_range(inst_begin(F), inst_end(F));
144 }
inst_begin(const Function & F)145 inline const_inst_iterator inst_begin(const Function &F) {
146 return const_inst_iterator(F);
147 }
inst_end(const Function & F)148 inline const_inst_iterator inst_end(const Function &F) {
149 return const_inst_iterator(F, true);
150 }
instructions(const Function & F)151 inline const_inst_range instructions(const Function &F) {
152 return const_inst_range(inst_begin(F), inst_end(F));
153 }
154
155 } // End llvm namespace
156
157 #endif
158