1
//===- llvm/Support/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_SUPPORT_INSTITERATOR_H
20
#define LLVM_SUPPORT_INSTITERATOR_H
22
#include "llvm/BasicBlock.h"
23
#include "llvm/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>
33
typedef _BB_i_t BBIty;
36
_BB_t *BBs; // BasicBlocksType
37
_BB_i_t BB; // BasicBlocksType::iterator
38
_BI_t BI; // BasicBlock::iterator
40
typedef std::bidirectional_iterator_tag iterator_category;
41
typedef IIty value_type;
42
typedef signed difference_type;
43
typedef IIty* pointer;
44
typedef IIty& reference;
46
// Default constructor
49
// Copy constructor...
50
template<typename A, typename B, typename C, typename D>
51
InstIterator(const InstIterator<A,B,C,D> &II)
52
: BBs(II.BBs), BB(II.BB), BI(II.BI) {}
54
template<typename A, typename B, typename C, typename D>
55
InstIterator(InstIterator<A,B,C,D> &II)
56
: BBs(II.BBs), BB(II.BB), BI(II.BI) {}
58
template<class M> InstIterator(M &m)
59
: BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
60
if (BB != BBs->end()) {
66
template<class M> InstIterator(M &m, bool)
67
: BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
70
// Accessors to get at the underlying iterators...
71
inline BBIty &getBasicBlockIterator() { return BB; }
72
inline BIty &getInstructionIterator() { return BI; }
74
inline reference operator*() const { return *BI; }
75
inline pointer operator->() const { return &operator*(); }
77
inline bool operator==(const InstIterator &y) const {
78
return BB == y.BB && (BB == BBs->end() || BI == y.BI);
80
inline bool operator!=(const InstIterator& y) const {
81
return !operator==(y);
84
InstIterator& operator++() {
89
inline InstIterator operator++(int) {
90
InstIterator tmp = *this; ++*this; return tmp;
93
InstIterator& operator--() {
94
while (BB == BBs->end() || BI == BB->begin()) {
101
inline InstIterator operator--(int) {
102
InstIterator tmp = *this; --*this; return tmp;
105
inline bool atEnd() const { return BB == BBs->end(); }
108
inline void advanceToNextBB() {
109
// The only way that the II could be broken is if it is now pointing to
110
// the end() of the current BasicBlock and there are successor BBs.
111
while (BI == BB->end()) {
113
if (BB == BBs->end()) break;
120
typedef InstIterator<iplist<BasicBlock>,
121
Function::iterator, BasicBlock::iterator,
122
Instruction> inst_iterator;
123
typedef InstIterator<const iplist<BasicBlock>,
124
Function::const_iterator,
125
BasicBlock::const_iterator,
126
const Instruction> const_inst_iterator;
128
inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
129
inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
130
inline const_inst_iterator inst_begin(const Function *F) {
131
return const_inst_iterator(*F);
133
inline const_inst_iterator inst_end(const Function *F) {
134
return const_inst_iterator(*F, true);
136
inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
137
inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
138
inline const_inst_iterator inst_begin(const Function &F) {
139
return const_inst_iterator(F);
141
inline const_inst_iterator inst_end(const Function &F) {
142
return const_inst_iterator(F, true);
145
} // End llvm namespace