2003-09-30 20:37:50 +02:00
|
|
|
//===- llvm/Support/InstIterator.h - Classes for inst iteration -*- C++ -*-===//
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-12 22:02:53 +01:00
|
|
|
//
|
|
|
|
// This file contains definitions of two iterators for iterating over the
|
2002-04-07 22:49:59 +02:00
|
|
|
// instructions in a function. This is effectively a wrapper around a two level
|
2002-02-12 22:02:53 +01: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 06:09:50 +02:00
|
|
|
#ifndef LLVM_SUPPORT_INSTITERATOR_H
|
|
|
|
#define LLVM_SUPPORT_INSTITERATOR_H
|
2002-02-12 22:02:53 +01:00
|
|
|
|
|
|
|
#include "llvm/BasicBlock.h"
|
2002-04-07 22:49:59 +02:00
|
|
|
#include "llvm/Function.h"
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-09-24 06:09:50 +02:00
|
|
|
// This class implements inst_begin() & inst_end() for
|
2002-02-12 22:02:53 +01: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;
|
2004-04-27 17:13:33 +02:00
|
|
|
_BB_t *BBs; // BasicBlocksType
|
2002-02-12 22:02:53 +01:00
|
|
|
_BB_i_t BB; // BasicBlocksType::iterator
|
|
|
|
_BI_t BI; // BasicBlock::iterator
|
|
|
|
public:
|
|
|
|
typedef std::bidirectional_iterator_tag iterator_category;
|
|
|
|
typedef IIty value_type;
|
2004-04-27 17:13:33 +02:00
|
|
|
typedef signed difference_type;
|
|
|
|
typedef IIty* pointer;
|
|
|
|
typedef IIty& reference;
|
|
|
|
|
|
|
|
// Default constructor
|
|
|
|
InstIterator() {}
|
2002-07-25 17:00:45 +02: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) {}
|
2005-04-21 22:48:15 +02:00
|
|
|
|
|
|
|
template<class M> InstIterator(M &m)
|
2004-04-27 17:13:33 +02:00
|
|
|
: BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
|
|
|
|
if (BB != BBs->end()) {
|
2002-06-25 18:13:24 +02:00
|
|
|
BI = BB->begin();
|
2002-02-12 22:02:53 +01:00
|
|
|
advanceToNextBB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:48:15 +02:00
|
|
|
template<class M> InstIterator(M &m, bool)
|
2004-04-27 17:13:33 +02:00
|
|
|
: BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors to get at the underlying iterators...
|
|
|
|
inline BBIty &getBasicBlockIterator() { return BB; }
|
|
|
|
inline BIty &getInstructionIterator() { return BI; }
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2004-04-27 17:13:33 +02:00
|
|
|
inline reference operator*() const { return *BI; }
|
|
|
|
inline pointer operator->() const { return &operator*(); }
|
2005-04-21 22:48:15 +02:00
|
|
|
|
|
|
|
inline bool operator==(const InstIterator &y) const {
|
2004-04-27 17:13:33 +02:00
|
|
|
return BB == y.BB && (BB == BBs->end() || BI == y.BI);
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
inline bool operator!=(const InstIterator& y) const {
|
2002-02-12 22:02:53 +01:00
|
|
|
return !operator==(y);
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:48:15 +02:00
|
|
|
InstIterator& operator++() {
|
2002-02-12 22:02:53 +01:00
|
|
|
++BI;
|
|
|
|
advanceToNextBB();
|
2005-04-21 22:48:15 +02:00
|
|
|
return *this;
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
inline InstIterator operator++(int) {
|
|
|
|
InstIterator tmp = *this; ++*this; return tmp;
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
|
|
|
|
InstIterator& operator--() {
|
2004-04-27 17:13:33 +02:00
|
|
|
while (BB == BBs->end() || BI == BB->begin()) {
|
2002-02-12 22:02:53 +01:00
|
|
|
--BB;
|
2002-06-25 18:13:24 +02:00
|
|
|
BI = BB->end();
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
|
|
|
--BI;
|
2005-04-21 22:48:15 +02:00
|
|
|
return *this;
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
inline InstIterator operator--(int) {
|
|
|
|
InstIterator tmp = *this; --*this; return tmp;
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
|
|
|
|
2004-04-27 17:13:33 +02:00
|
|
|
inline bool atEnd() const { return BB == BBs->end(); }
|
2002-02-12 22:02:53 +01:00
|
|
|
|
|
|
|
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 18:13:24 +02:00
|
|
|
while (BI == BB->end()) {
|
2002-02-12 22:02:53 +01:00
|
|
|
++BB;
|
2004-04-27 17:13:33 +02:00
|
|
|
if (BB == BBs->end()) break;
|
2002-06-25 18:13:24 +02:00
|
|
|
BI = BB->begin();
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-06-25 18:13:24 +02:00
|
|
|
typedef InstIterator<iplist<BasicBlock>,
|
2002-04-07 22:49:59 +02:00
|
|
|
Function::iterator, BasicBlock::iterator,
|
2004-04-27 17:13:33 +02:00
|
|
|
Instruction> inst_iterator;
|
2002-06-25 18:13:24 +02:00
|
|
|
typedef InstIterator<const iplist<BasicBlock>,
|
2005-04-21 22:48:15 +02:00
|
|
|
Function::const_iterator,
|
2002-02-12 22:02:53 +01:00
|
|
|
BasicBlock::const_iterator,
|
2004-04-27 17:13:33 +02:00
|
|
|
const Instruction> const_inst_iterator;
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2002-04-07 22:49:59 +02: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 22:02:53 +01:00
|
|
|
}
|
2002-04-07 22:49:59 +02:00
|
|
|
inline const_inst_iterator inst_end(const Function *F) {
|
|
|
|
return const_inst_iterator(*F, true);
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
2002-06-25 18:13:24 +02: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 22:02:53 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-02-12 22:02:53 +01:00
|
|
|
#endif
|