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 template<class M> InstIterator(M &m)
38 : BBs(m.getBasicBlocks()), BB(BBs.begin()) { // begin ctor
39 if (BB != BBs.end()) {
45 template<class M> InstIterator(M &m, bool)
46 : BBs(m.getBasicBlocks()), BB(BBs.end()) { // end ctor
49 // Accessors to get at the underlying iterators...
50 inline BBIty &getBasicBlockIterator() { return BB; }
51 inline BIty &getInstructionIterator() { return BI; }
53 inline IIty operator*() const { return *BI; }
54 inline IIty operator->() const { return operator*(); }
56 inline bool operator==(const InstIterator &y) const {
57 return BB == y.BB && (BB == BBs.end() || BI == y.BI);
59 inline bool operator!=(const InstIterator& y) const {
60 return !operator==(y);
63 InstIterator& operator++() {
68 inline InstIterator operator++(int) {
69 InstIterator tmp = *this; ++*this; return tmp;
72 InstIterator& operator--() {
73 while (BB == BBs.end() || BI == (*BB)->begin()) {
80 inline InstIterator operator--(int) {
81 InstIterator tmp = *this; --*this; return tmp;
84 inline bool atEnd() const { return BB == BBs.end(); }
87 inline void advanceToNextBB() {
88 // The only way that the II could be broken is if it is now pointing to
89 // the end() of the current BasicBlock and there are successor BBs.
90 while (BI == (*BB)->end()) {
92 if (BB == BBs.end()) break;
99 typedef InstIterator<ValueHolder<BasicBlock, Function, Function>,
100 Function::iterator, BasicBlock::iterator,
101 Instruction*> inst_iterator;
102 typedef InstIterator<const ValueHolder<BasicBlock, Function, Function>,
103 Function::const_iterator,
104 BasicBlock::const_iterator,
105 const Instruction*> const_inst_iterator;
107 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
108 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
109 inline const_inst_iterator inst_begin(const Function *F) {
110 return const_inst_iterator(*F);
112 inline const_inst_iterator inst_end(const Function *F) {
113 return const_inst_iterator(*F, true);