31c8b660d67ce08e94d2b1d3022314b01f794ea1
[oota-llvm.git] / include / llvm / Support / InstIterator.h
1 //===- llvm/Support/InstIterator.h - Classes for inst iteration -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 // Note that this iterator gets invalidated any time that basic blocks or
15 // instructions are moved around.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_SUPPORT_INSTITERATOR_H
20 #define LLVM_SUPPORT_INSTITERATOR_H
21
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Function.h"
24
25 namespace llvm {
26
27 // This class implements inst_begin() & inst_end() for
28 // inst_iterator and const_inst_iterator's.
29 //
30 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
31 class InstIterator {
32   typedef _BB_t   BBty;
33   typedef _BB_i_t BBIty;
34   typedef _BI_t   BIty;
35   typedef _II_t   IIty;
36   _BB_t  &BBs;      // BasicBlocksType
37   _BB_i_t BB;       // BasicBlocksType::iterator
38   _BI_t   BI;       // BasicBlock::iterator
39 public:
40   typedef std::bidirectional_iterator_tag iterator_category;
41   typedef IIty                            value_type;
42   typedef unsigned                        difference_type;
43   typedef BIty                            pointer;
44   typedef IIty                            reference;
45
46   // Copy constructor...
47   template<typename A, typename B, typename C, typename D>
48   InstIterator(const InstIterator<A,B,C,D> &II)
49     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
50
51   template<typename A, typename B, typename C, typename D>
52   InstIterator(InstIterator<A,B,C,D> &II)
53     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
54   
55   template<class M> InstIterator(M &m) 
56     : BBs(m.getBasicBlockList()), BB(BBs.begin()) {    // begin ctor
57     if (BB != BBs.end()) {
58       BI = BB->begin();
59       advanceToNextBB();
60     }
61   }
62
63   template<class M> InstIterator(M &m, bool) 
64     : BBs(m.getBasicBlockList()), BB(BBs.end()) {    // end ctor
65   }
66
67   // Accessors to get at the underlying iterators...
68   inline BBIty &getBasicBlockIterator()  { return BB; }
69   inline BIty  &getInstructionIterator() { return BI; }
70   
71   inline IIty operator*()  const { return BI; }
72   inline IIty operator->() const { return operator*(); }
73   
74   inline bool operator==(const InstIterator &y) const { 
75     return BB == y.BB && (BB == BBs.end() || BI == y.BI);
76   }
77   inline bool operator!=(const InstIterator& y) const { 
78     return !operator==(y);
79   }
80
81   InstIterator& operator++() { 
82     ++BI;
83     advanceToNextBB();
84     return *this; 
85   }
86   inline InstIterator operator++(int) { 
87     InstIterator tmp = *this; ++*this; return tmp; 
88   }
89     
90   InstIterator& operator--() { 
91     while (BB == BBs.end() || BI == BB->begin()) {
92       --BB;
93       BI = BB->end();
94     }
95     --BI;
96     return *this; 
97   }
98   inline InstIterator  operator--(int) { 
99     InstIterator tmp = *this; --*this; return tmp; 
100   }
101
102   inline bool atEnd() const { return BB == BBs.end(); }
103
104 private:
105   inline void advanceToNextBB() {
106     // The only way that the II could be broken is if it is now pointing to
107     // the end() of the current BasicBlock and there are successor BBs.
108     while (BI == BB->end()) {
109       ++BB;
110       if (BB == BBs.end()) break;
111       BI = BB->begin();
112     }
113   }
114 };
115
116
117 typedef InstIterator<iplist<BasicBlock>,
118                      Function::iterator, BasicBlock::iterator,
119                      Instruction*> inst_iterator;
120 typedef InstIterator<const iplist<BasicBlock>,
121                      Function::const_iterator, 
122                      BasicBlock::const_iterator,
123                      const Instruction*> const_inst_iterator;
124
125 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
126 inline inst_iterator inst_end(Function *F)   { return inst_iterator(*F, true); }
127 inline const_inst_iterator inst_begin(const Function *F) {
128   return const_inst_iterator(*F);
129 }
130 inline const_inst_iterator inst_end(const Function *F) {
131   return const_inst_iterator(*F, true);
132 }
133 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
134 inline inst_iterator inst_end(Function &F)   { return inst_iterator(F, true); }
135 inline const_inst_iterator inst_begin(const Function &F) {
136   return const_inst_iterator(F);
137 }
138 inline const_inst_iterator inst_end(const Function &F) {
139   return const_inst_iterator(F, true);
140 }
141
142 } // End llvm namespace
143
144 #endif