LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InstIterator.h
Go to the documentation of this file.
1 //===- llvm/Support/InstIterator.h - Classes for inst iteration -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/IR/BasicBlock.h"
23 #include "llvm/IR/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 signed difference_type;
43  typedef IIty* pointer;
44  typedef IIty& reference;
45 
46  // Default constructor
48 
49  // Copy constructor...
50  template<typename A, typename B, typename C, typename D>
52  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
53 
54  template<typename A, typename B, typename C, typename D>
56  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
57 
58  template<class M> InstIterator(M &m)
59  : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
60  if (BB != BBs->end()) {
61  BI = BB->begin();
62  advanceToNextBB();
63  }
64  }
65 
66  template<class M> InstIterator(M &m, bool)
67  : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
68  }
69 
70  // Accessors to get at the underlying iterators...
71  inline BBIty &getBasicBlockIterator() { return BB; }
72  inline BIty &getInstructionIterator() { return BI; }
73 
74  inline reference operator*() const { return *BI; }
75  inline pointer operator->() const { return &operator*(); }
76 
77  inline bool operator==(const InstIterator &y) const {
78  return BB == y.BB && (BB == BBs->end() || BI == y.BI);
79  }
80  inline bool operator!=(const InstIterator& y) const {
81  return !operator==(y);
82  }
83 
85  ++BI;
86  advanceToNextBB();
87  return *this;
88  }
89  inline InstIterator operator++(int) {
90  InstIterator tmp = *this; ++*this; return tmp;
91  }
92 
94  while (BB == BBs->end() || BI == BB->begin()) {
95  --BB;
96  BI = BB->end();
97  }
98  --BI;
99  return *this;
100  }
101  inline InstIterator operator--(int) {
102  InstIterator tmp = *this; --*this; return tmp;
103  }
104 
105  inline bool atEnd() const { return BB == BBs->end(); }
106 
107 private:
108  inline void advanceToNextBB() {
109  // The only way that the II could be broken is if it is now pointing to
110  // the end() of the current BasicBlock and there are successor BBs.
111  while (BI == BB->end()) {
112  ++BB;
113  if (BB == BBs->end()) break;
114  BI = BB->begin();
115  }
116  }
117 };
118 
119 
120 typedef InstIterator<iplist<BasicBlock>,
127 
129 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
131  return const_inst_iterator(*F);
132 }
134  return const_inst_iterator(*F, true);
135 }
137 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
139  return const_inst_iterator(F);
140 }
142  return const_inst_iterator(F, true);
143 }
144 
145 } // End llvm namespace
146 
147 #endif
BBIty & getBasicBlockIterator()
Definition: InstIterator.h:71
BasicBlockListType::const_iterator const_iterator
Definition: Function.h:78
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:181
bool operator==(const InstIterator &y) const
Definition: InstIterator.h:77
BIty & getInstructionIterator()
Definition: InstIterator.h:72
bool atEnd() const
Definition: InstIterator.h:105
InstIterator & operator++()
Definition: InstIterator.h:84
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:173
InstIterator & operator--()
Definition: InstIterator.h:93
F(f)
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:128
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:100
bool operator!=(const InstIterator &y) const
Definition: InstIterator.h:80
InstIterator< const iplist< BasicBlock >, Function::const_iterator, BasicBlock::const_iterator, const Instruction > const_inst_iterator
Definition: InstIterator.h:126
InstIterator(M &m, bool)
Definition: InstIterator.h:66
pointer operator->() const
Definition: InstIterator.h:75
BasicBlockListType::iterator iterator
Definition: Function.h:77
std::bidirectional_iterator_tag iterator_category
Definition: InstIterator.h:40
reference operator*() const
Definition: InstIterator.h:74
InstIterator< iplist< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
Definition: InstIterator.h:122
InstIterator(const InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:51
InstIterator operator--(int)
Definition: InstIterator.h:101
InstIterator operator++(int)
Definition: InstIterator.h:89
InstIterator(InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:55
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:129
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:99