LLVM API Documentation

InstIterator.h
Go to the documentation of this file.
00001 //===- InstIterator.h - Classes for inst iteration --------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains definitions of two iterators for iterating over the
00011 // instructions in a function.  This is effectively a wrapper around a two level
00012 // iterator that can probably be genericized later.
00013 //
00014 // Note that this iterator gets invalidated any time that basic blocks or
00015 // instructions are moved around.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_IR_INSTITERATOR_H
00020 #define LLVM_IR_INSTITERATOR_H
00021 
00022 #include "llvm/IR/BasicBlock.h"
00023 #include "llvm/IR/Function.h"
00024 
00025 namespace llvm {
00026 
00027 // This class implements inst_begin() & inst_end() for
00028 // inst_iterator and const_inst_iterator's.
00029 //
00030 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
00031 class InstIterator {
00032   typedef _BB_t   BBty;
00033   typedef _BB_i_t BBIty;
00034   typedef _BI_t   BIty;
00035   typedef _II_t   IIty;
00036   _BB_t  *BBs;      // BasicBlocksType
00037   _BB_i_t BB;       // BasicBlocksType::iterator
00038   _BI_t   BI;       // BasicBlock::iterator
00039 public:
00040   typedef std::bidirectional_iterator_tag iterator_category;
00041   typedef IIty                            value_type;
00042   typedef signed                        difference_type;
00043   typedef IIty*                           pointer;
00044   typedef IIty&                           reference;
00045 
00046   // Default constructor
00047   InstIterator() {}
00048 
00049   // Copy constructor...
00050   template<typename A, typename B, typename C, typename D>
00051   InstIterator(const InstIterator<A,B,C,D> &II)
00052     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
00053 
00054   template<typename A, typename B, typename C, typename D>
00055   InstIterator(InstIterator<A,B,C,D> &II)
00056     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
00057 
00058   template<class M> InstIterator(M &m)
00059     : BBs(&m.getBasicBlockList()), BB(BBs->begin()) {    // begin ctor
00060     if (BB != BBs->end()) {
00061       BI = BB->begin();
00062       advanceToNextBB();
00063     }
00064   }
00065 
00066   template<class M> InstIterator(M &m, bool)
00067     : BBs(&m.getBasicBlockList()), BB(BBs->end()) {    // end ctor
00068   }
00069 
00070   // Accessors to get at the underlying iterators...
00071   inline BBIty &getBasicBlockIterator()  { return BB; }
00072   inline BIty  &getInstructionIterator() { return BI; }
00073 
00074   inline reference operator*()  const { return *BI; }
00075   inline pointer operator->() const { return &operator*(); }
00076 
00077   inline bool operator==(const InstIterator &y) const {
00078     return BB == y.BB && (BB == BBs->end() || BI == y.BI);
00079   }
00080   inline bool operator!=(const InstIterator& y) const {
00081     return !operator==(y);
00082   }
00083 
00084   InstIterator& operator++() {
00085     ++BI;
00086     advanceToNextBB();
00087     return *this;
00088   }
00089   inline InstIterator operator++(int) {
00090     InstIterator tmp = *this; ++*this; return tmp;
00091   }
00092 
00093   InstIterator& operator--() {
00094     while (BB == BBs->end() || BI == BB->begin()) {
00095       --BB;
00096       BI = BB->end();
00097     }
00098     --BI;
00099     return *this;
00100   }
00101   inline InstIterator  operator--(int) {
00102     InstIterator tmp = *this; --*this; return tmp;
00103   }
00104 
00105   inline bool atEnd() const { return BB == BBs->end(); }
00106 
00107 private:
00108   inline void advanceToNextBB() {
00109     // The only way that the II could be broken is if it is now pointing to
00110     // the end() of the current BasicBlock and there are successor BBs.
00111     while (BI == BB->end()) {
00112       ++BB;
00113       if (BB == BBs->end()) break;
00114       BI = BB->begin();
00115     }
00116   }
00117 };
00118 
00119 
00120 typedef InstIterator<iplist<BasicBlock>,
00121                      Function::iterator, BasicBlock::iterator,
00122                      Instruction> inst_iterator;
00123 typedef InstIterator<const iplist<BasicBlock>,
00124                      Function::const_iterator,
00125                      BasicBlock::const_iterator,
00126                      const Instruction> const_inst_iterator;
00127 
00128 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
00129 inline inst_iterator inst_end(Function *F)   { return inst_iterator(*F, true); }
00130 inline const_inst_iterator inst_begin(const Function *F) {
00131   return const_inst_iterator(*F);
00132 }
00133 inline const_inst_iterator inst_end(const Function *F) {
00134   return const_inst_iterator(*F, true);
00135 }
00136 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
00137 inline inst_iterator inst_end(Function &F)   { return inst_iterator(F, true); }
00138 inline const_inst_iterator inst_begin(const Function &F) {
00139   return const_inst_iterator(F);
00140 }
00141 inline const_inst_iterator inst_end(const Function &F) {
00142   return const_inst_iterator(F, true);
00143 }
00144 
00145 } // End llvm namespace
00146 
00147 #endif