1 //===- InstIterator.h - Classes for inst iteration --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // Note that this iterator gets invalidated any time that basic blocks or
15 // instructions are moved around.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_IR_INSTITERATOR_H
20 #define LLVM_IR_INSTITERATOR_H
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
27 // This class implements inst_begin() & inst_end() for
28 // inst_iterator and const_inst_iterator's.
30 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
35 BB_t *BBs; // BasicBlocksType
36 BB_i_t BB; // BasicBlocksType::iterator
37 BI_t BI; // BasicBlock::iterator
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;
45 // Default constructor
48 // Copy constructor...
49 template<typename A, typename B, typename C, typename D>
50 InstIterator(const InstIterator<A,B,C,D> &II)
51 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
53 template<typename A, typename B, typename C, typename D>
54 InstIterator(InstIterator<A,B,C,D> &II)
55 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
57 template<class M> InstIterator(M &m)
58 : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
59 if (BB != BBs->end()) {
65 template<class M> InstIterator(M &m, bool)
66 : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
69 // Accessors to get at the underlying iterators...
70 inline BBIty &getBasicBlockIterator() { return BB; }
71 inline BIty &getInstructionIterator() { return BI; }
73 inline reference operator*() const { return *BI; }
74 inline pointer operator->() const { return &operator*(); }
76 inline bool operator==(const InstIterator &y) const {
77 return BB == y.BB && (BB == BBs->end() || BI == y.BI);
79 inline bool operator!=(const InstIterator& y) const {
80 return !operator==(y);
83 InstIterator& operator++() {
88 inline InstIterator operator++(int) {
89 InstIterator tmp = *this; ++*this; return tmp;
92 InstIterator& operator--() {
93 while (BB == BBs->end() || BI == BB->begin()) {
100 inline InstIterator operator--(int) {
101 InstIterator tmp = *this; --*this; return tmp;
104 inline bool atEnd() const { return BB == BBs->end(); }
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()) {
112 if (BB == BBs->end()) break;
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;
126 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
127 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
128 inline inst_range instructions(Function *F) {
129 return inst_range(inst_begin(F), inst_end(F));
131 inline const_inst_iterator inst_begin(const Function *F) {
132 return const_inst_iterator(*F);
134 inline const_inst_iterator inst_end(const Function *F) {
135 return const_inst_iterator(*F, true);
137 inline const_inst_range instructions(const Function *F) {
138 return const_inst_range(inst_begin(F), inst_end(F));
140 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
141 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
142 inline inst_range instructions(Function &F) {
143 return inst_range(inst_begin(F), inst_end(F));
145 inline const_inst_iterator inst_begin(const Function &F) {
146 return const_inst_iterator(F);
148 inline const_inst_iterator inst_end(const Function &F) {
149 return const_inst_iterator(F, true);
151 inline const_inst_range instructions(const Function &F) {
152 return const_inst_range(inst_begin(F), inst_end(F));
155 } // End llvm namespace