1 //===-- llvm/Support/InstIterator.h - Classes for inst iteration -*- C++ -*--=//
3 // This file contains definitions of two iterators for iterating over the
4 // instructions in a function. This is effectively a wrapper around a two level
5 // iterator that can probably be genericized later.
7 // Note that this iterator gets invalidated any time that basic blocks or
8 // instructions are moved around.
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_INST_ITERATOR_H
13 #define LLVM_INST_ITERATOR_H
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Function.h"
18 // This class is implements inst_begin() & inst_end() for
19 // inst_iterator and const_inst_iterator's.
21 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
24 typedef _BB_i_t BBIty;
27 _BB_t &BBs; // BasicBlocksType
28 _BB_i_t BB; // BasicBlocksType::iterator
29 _BI_t BI; // BasicBlock::iterator
31 typedef std::bidirectional_iterator_tag iterator_category;
32 typedef IIty value_type;
33 typedef unsigned difference_type;
35 typedef IIty reference;
37 // Copy constructor...
38 template<typename A, typename B, typename C, typename D>
39 InstIterator(const InstIterator<A,B,C,D> &II)
40 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
42 template<typename A, typename B, typename C, typename D>
43 InstIterator(InstIterator<A,B,C,D> &II)
44 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
46 template<class M> InstIterator(M &m)
47 : BBs(m.getBasicBlockList()), BB(BBs.begin()) { // begin ctor
48 if (BB != BBs.end()) {
54 template<class M> InstIterator(M &m, bool)
55 : BBs(m.getBasicBlockList()), BB(BBs.end()) { // end ctor
58 // Accessors to get at the underlying iterators...
59 inline BBIty &getBasicBlockIterator() { return BB; }
60 inline BIty &getInstructionIterator() { return BI; }
62 inline IIty operator*() const { return &*BI; }
63 inline IIty operator->() const { return operator*(); }
65 inline bool operator==(const InstIterator &y) const {
66 return BB == y.BB && (BB == BBs.end() || BI == y.BI);
68 inline bool operator!=(const InstIterator& y) const {
69 return !operator==(y);
72 InstIterator& operator++() {
77 inline InstIterator operator++(int) {
78 InstIterator tmp = *this; ++*this; return tmp;
81 InstIterator& operator--() {
82 while (BB == BBs.end() || BI == BB->begin()) {
89 inline InstIterator operator--(int) {
90 InstIterator tmp = *this; --*this; return tmp;
93 inline bool atEnd() const { return BB == BBs.end(); }
96 inline void advanceToNextBB() {
97 // The only way that the II could be broken is if it is now pointing to
98 // the end() of the current BasicBlock and there are successor BBs.
99 while (BI == BB->end()) {
101 if (BB == BBs.end()) break;
108 typedef InstIterator<iplist<BasicBlock>,
109 Function::iterator, BasicBlock::iterator,
110 Instruction*> inst_iterator;
111 typedef InstIterator<const iplist<BasicBlock>,
112 Function::const_iterator,
113 BasicBlock::const_iterator,
114 const Instruction*> const_inst_iterator;
116 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
117 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
118 inline const_inst_iterator inst_begin(const Function *F) {
119 return const_inst_iterator(*F);
121 inline const_inst_iterator inst_end(const Function *F) {
122 return const_inst_iterator(*F, true);
124 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
125 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
126 inline const_inst_iterator inst_begin(const Function &F) {
127 return const_inst_iterator(F);
129 inline const_inst_iterator inst_end(const Function &F) {
130 return const_inst_iterator(F, true);