Added LLVM copyright header (for lack of a better term).
[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 // This class implements inst_begin() & inst_end() for
26 // inst_iterator and const_inst_iterator's.
27 //
28 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
29 class InstIterator {
30   typedef _BB_t   BBty;
31   typedef _BB_i_t BBIty;
32   typedef _BI_t   BIty;
33   typedef _II_t   IIty;
34   _BB_t  &BBs;      // BasicBlocksType
35   _BB_i_t BB;       // BasicBlocksType::iterator
36   _BI_t   BI;       // BasicBlock::iterator
37 public:
38   typedef std::bidirectional_iterator_tag iterator_category;
39   typedef IIty                            value_type;
40   typedef unsigned                        difference_type;
41   typedef BIty                            pointer;
42   typedef IIty                            reference;
43
44   // Copy constructor...
45   template<typename A, typename B, typename C, typename D>
46   InstIterator(const InstIterator<A,B,C,D> &II)
47     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
48
49   template<typename A, typename B, typename C, typename D>
50   InstIterator(InstIterator<A,B,C,D> &II)
51     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
52   
53   template<class M> InstIterator(M &m) 
54     : BBs(m.getBasicBlockList()), BB(BBs.begin()) {    // begin ctor
55     if (BB != BBs.end()) {
56       BI = BB->begin();
57       advanceToNextBB();
58     }
59   }
60
61   template<class M> InstIterator(M &m, bool) 
62     : BBs(m.getBasicBlockList()), BB(BBs.end()) {    // end ctor
63   }
64
65   // Accessors to get at the underlying iterators...
66   inline BBIty &getBasicBlockIterator()  { return BB; }
67   inline BIty  &getInstructionIterator() { return BI; }
68   
69   inline IIty operator*()  const { return BI; }
70   inline IIty operator->() const { return operator*(); }
71   
72   inline bool operator==(const InstIterator &y) const { 
73     return BB == y.BB && (BB == BBs.end() || BI == y.BI);
74   }
75   inline bool operator!=(const InstIterator& y) const { 
76     return !operator==(y);
77   }
78
79   InstIterator& operator++() { 
80     ++BI;
81     advanceToNextBB();
82     return *this; 
83   }
84   inline InstIterator operator++(int) { 
85     InstIterator tmp = *this; ++*this; return tmp; 
86   }
87     
88   InstIterator& operator--() { 
89     while (BB == BBs.end() || BI == BB->begin()) {
90       --BB;
91       BI = BB->end();
92     }
93     --BI;
94     return *this; 
95   }
96   inline InstIterator  operator--(int) { 
97     InstIterator tmp = *this; --*this; return tmp; 
98   }
99
100   inline bool atEnd() const { return BB == BBs.end(); }
101
102 private:
103   inline void advanceToNextBB() {
104     // The only way that the II could be broken is if it is now pointing to
105     // the end() of the current BasicBlock and there are successor BBs.
106     while (BI == BB->end()) {
107       ++BB;
108       if (BB == BBs.end()) break;
109       BI = BB->begin();
110     }
111   }
112 };
113
114
115 typedef InstIterator<iplist<BasicBlock>,
116                      Function::iterator, BasicBlock::iterator,
117                      Instruction*> inst_iterator;
118 typedef InstIterator<const iplist<BasicBlock>,
119                      Function::const_iterator, 
120                      BasicBlock::const_iterator,
121                      const Instruction*> const_inst_iterator;
122
123 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
124 inline inst_iterator inst_end(Function *F)   { return inst_iterator(*F, true); }
125 inline const_inst_iterator inst_begin(const Function *F) {
126   return const_inst_iterator(*F);
127 }
128 inline const_inst_iterator inst_end(const Function *F) {
129   return const_inst_iterator(*F, true);
130 }
131 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
132 inline inst_iterator inst_end(Function &F)   { return inst_iterator(F, true); }
133 inline const_inst_iterator inst_begin(const Function &F) {
134   return const_inst_iterator(F);
135 }
136 inline const_inst_iterator inst_end(const Function &F) {
137   return const_inst_iterator(F, true);
138 }
139
140 #endif