2003-09-30 18:37:50 +00:00
|
|
|
//===- llvm/Support/InstIterator.h - Classes for inst iteration -*- C++ -*-===//
|
2002-02-12 21:02:53 +00:00
|
|
|
//
|
|
|
|
// This file contains definitions of two iterators for iterating over the
|
2002-04-07 20:49:59 +00:00
|
|
|
// instructions in a function. This is effectively a wrapper around a two level
|
2002-02-12 21:02:53 +00:00
|
|
|
// iterator that can probably be genericized later.
|
|
|
|
//
|
|
|
|
// Note that this iterator gets invalidated any time that basic blocks or
|
|
|
|
// instructions are moved around.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-24 04:09:50 +00:00
|
|
|
#ifndef LLVM_SUPPORT_INSTITERATOR_H
|
|
|
|
#define LLVM_SUPPORT_INSTITERATOR_H
|
2002-02-12 21:02:53 +00:00
|
|
|
|
|
|
|
#include "llvm/BasicBlock.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-02-12 21:02:53 +00:00
|
|
|
|
2003-09-24 04:09:50 +00:00
|
|
|
// This class implements inst_begin() & inst_end() for
|
2002-02-12 21:02:53 +00:00
|
|
|
// inst_iterator and const_inst_iterator's.
|
|
|
|
//
|
|
|
|
template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
|
|
|
|
class InstIterator {
|
|
|
|
typedef _BB_t BBty;
|
|
|
|
typedef _BB_i_t BBIty;
|
|
|
|
typedef _BI_t BIty;
|
|
|
|
typedef _II_t IIty;
|
|
|
|
_BB_t &BBs; // BasicBlocksType
|
|
|
|
_BB_i_t BB; // BasicBlocksType::iterator
|
|
|
|
_BI_t BI; // BasicBlock::iterator
|
|
|
|
public:
|
|
|
|
typedef std::bidirectional_iterator_tag iterator_category;
|
|
|
|
typedef IIty value_type;
|
|
|
|
typedef unsigned difference_type;
|
|
|
|
typedef BIty pointer;
|
|
|
|
typedef IIty reference;
|
2002-07-25 15:00:45 +00:00
|
|
|
|
|
|
|
// Copy constructor...
|
|
|
|
template<typename A, typename B, typename C, typename D>
|
|
|
|
InstIterator(const InstIterator<A,B,C,D> &II)
|
|
|
|
: BBs(II.BBs), BB(II.BB), BI(II.BI) {}
|
|
|
|
|
|
|
|
template<typename A, typename B, typename C, typename D>
|
|
|
|
InstIterator(InstIterator<A,B,C,D> &II)
|
|
|
|
: BBs(II.BBs), BB(II.BB), BI(II.BI) {}
|
2002-02-12 21:02:53 +00:00
|
|
|
|
|
|
|
template<class M> InstIterator(M &m)
|
2002-06-25 16:13:24 +00:00
|
|
|
: BBs(m.getBasicBlockList()), BB(BBs.begin()) { // begin ctor
|
2002-02-12 21:02:53 +00:00
|
|
|
if (BB != BBs.end()) {
|
2002-06-25 16:13:24 +00:00
|
|
|
BI = BB->begin();
|
2002-02-12 21:02:53 +00:00
|
|
|
advanceToNextBB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class M> InstIterator(M &m, bool)
|
2002-06-25 16:13:24 +00:00
|
|
|
: BBs(m.getBasicBlockList()), BB(BBs.end()) { // end ctor
|
2002-02-12 21:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors to get at the underlying iterators...
|
|
|
|
inline BBIty &getBasicBlockIterator() { return BB; }
|
|
|
|
inline BIty &getInstructionIterator() { return BI; }
|
|
|
|
|
2003-04-23 16:35:30 +00:00
|
|
|
inline IIty operator*() const { return BI; }
|
2002-02-12 21:02:53 +00:00
|
|
|
inline IIty operator->() const { return operator*(); }
|
|
|
|
|
|
|
|
inline bool operator==(const InstIterator &y) const {
|
|
|
|
return BB == y.BB && (BB == BBs.end() || BI == y.BI);
|
|
|
|
}
|
|
|
|
inline bool operator!=(const InstIterator& y) const {
|
|
|
|
return !operator==(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
InstIterator& operator++() {
|
|
|
|
++BI;
|
|
|
|
advanceToNextBB();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline InstIterator operator++(int) {
|
|
|
|
InstIterator tmp = *this; ++*this; return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstIterator& operator--() {
|
2002-06-25 16:13:24 +00:00
|
|
|
while (BB == BBs.end() || BI == BB->begin()) {
|
2002-02-12 21:02:53 +00:00
|
|
|
--BB;
|
2002-06-25 16:13:24 +00:00
|
|
|
BI = BB->end();
|
2002-02-12 21:02:53 +00:00
|
|
|
}
|
|
|
|
--BI;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline InstIterator operator--(int) {
|
|
|
|
InstIterator tmp = *this; --*this; return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool atEnd() const { return BB == BBs.end(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline void advanceToNextBB() {
|
|
|
|
// The only way that the II could be broken is if it is now pointing to
|
|
|
|
// the end() of the current BasicBlock and there are successor BBs.
|
2002-06-25 16:13:24 +00:00
|
|
|
while (BI == BB->end()) {
|
2002-02-12 21:02:53 +00:00
|
|
|
++BB;
|
|
|
|
if (BB == BBs.end()) break;
|
2002-06-25 16:13:24 +00:00
|
|
|
BI = BB->begin();
|
2002-02-12 21:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
typedef InstIterator<iplist<BasicBlock>,
|
2002-04-07 20:49:59 +00:00
|
|
|
Function::iterator, BasicBlock::iterator,
|
|
|
|
Instruction*> inst_iterator;
|
2002-06-25 16:13:24 +00:00
|
|
|
typedef InstIterator<const iplist<BasicBlock>,
|
2002-04-07 20:49:59 +00:00
|
|
|
Function::const_iterator,
|
2002-02-12 21:02:53 +00:00
|
|
|
BasicBlock::const_iterator,
|
|
|
|
const Instruction*> const_inst_iterator;
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
|
|
|
|
inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
|
|
|
|
inline const_inst_iterator inst_begin(const Function *F) {
|
|
|
|
return const_inst_iterator(*F);
|
2002-02-12 21:02:53 +00:00
|
|
|
}
|
2002-04-07 20:49:59 +00:00
|
|
|
inline const_inst_iterator inst_end(const Function *F) {
|
|
|
|
return const_inst_iterator(*F, true);
|
2002-02-12 21:02:53 +00:00
|
|
|
}
|
2002-06-25 16:13:24 +00:00
|
|
|
inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
|
|
|
|
inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
|
|
|
|
inline const_inst_iterator inst_begin(const Function &F) {
|
|
|
|
return const_inst_iterator(F);
|
|
|
|
}
|
|
|
|
inline const_inst_iterator inst_end(const Function &F) {
|
|
|
|
return const_inst_iterator(F, true);
|
|
|
|
}
|
2002-02-12 21:02:53 +00:00
|
|
|
|
|
|
|
#endif
|