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/ADT/iterator_range.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/SymbolTableListTraits.h"
30 // This class implements inst_begin() & inst_end() for
31 // inst_iterator and const_inst_iterator's.
33 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
38 BB_t *BBs; // BasicBlocksType
39 BB_i_t BB; // BasicBlocksType::iterator
40 BI_t BI; // BasicBlock::iterator
43 typedef std::bidirectional_iterator_tag iterator_category;
44 typedef IIty value_type;
45 typedef signed difference_type;
46 typedef IIty* pointer;
47 typedef IIty& reference;
49 // Default constructor
50 InstIterator() = default;
52 // Copy constructor...
53 template<typename A, typename B, typename C, typename D>
54 InstIterator(const InstIterator<A,B,C,D> &II)
55 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
57 template<typename A, typename B, typename C, typename D>
58 InstIterator(InstIterator<A,B,C,D> &II)
59 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
61 template<class M> InstIterator(M &m)
62 : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
63 if (BB != BBs->end()) {
69 template<class M> InstIterator(M &m, bool)
70 : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
73 // Accessors to get at the underlying iterators...
74 inline BBIty &getBasicBlockIterator() { return BB; }
75 inline BIty &getInstructionIterator() { return BI; }
77 inline reference operator*() const { return *BI; }
78 inline pointer operator->() const { return &operator*(); }
80 inline bool operator==(const InstIterator &y) const {
81 return BB == y.BB && (BB == BBs->end() || BI == y.BI);
83 inline bool operator!=(const InstIterator& y) const {
84 return !operator==(y);
87 InstIterator& operator++() {
92 inline InstIterator operator++(int) {
93 InstIterator tmp = *this; ++*this; return tmp;
96 InstIterator& operator--() {
97 while (BB == BBs->end() || BI == BB->begin()) {
104 inline InstIterator operator--(int) {
105 InstIterator tmp = *this; --*this; return tmp;
108 inline bool atEnd() const { return BB == BBs->end(); }
111 inline void advanceToNextBB() {
112 // The only way that the II could be broken is if it is now pointing to
113 // the end() of the current BasicBlock and there are successor BBs.
114 while (BI == BB->end()) {
116 if (BB == BBs->end()) break;
122 typedef InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
123 BasicBlock::iterator, Instruction> inst_iterator;
124 typedef InstIterator<const SymbolTableList<BasicBlock>,
125 Function::const_iterator, BasicBlock::const_iterator,
126 const Instruction> const_inst_iterator;
127 typedef iterator_range<inst_iterator> inst_range;
128 typedef iterator_range<const_inst_iterator> const_inst_range;
130 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
131 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
132 inline inst_range instructions(Function *F) {
133 return inst_range(inst_begin(F), inst_end(F));
135 inline const_inst_iterator inst_begin(const Function *F) {
136 return const_inst_iterator(*F);
138 inline const_inst_iterator inst_end(const Function *F) {
139 return const_inst_iterator(*F, true);
141 inline const_inst_range instructions(const Function *F) {
142 return const_inst_range(inst_begin(F), inst_end(F));
144 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
145 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
146 inline inst_range instructions(Function &F) {
147 return inst_range(inst_begin(F), inst_end(F));
149 inline const_inst_iterator inst_begin(const Function &F) {
150 return const_inst_iterator(F);
152 inline const_inst_iterator inst_end(const Function &F) {
153 return const_inst_iterator(F, true);
155 inline const_inst_range instructions(const Function &F) {
156 return const_inst_range(inst_begin(F), inst_end(F));
159 } // end namespace llvm
161 #endif // LLVM_IR_INSTITERATOR_H