1 //===- InstIterator.h - Classes for inst iteration --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains definitions of two iterators for iterating over the
10 // instructions in a function. This is effectively a wrapper around a two level
11 // iterator that can probably be genericized later.
12 //
13 // Note that this iterator gets invalidated any time that basic blocks or
14 // instructions are moved around.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_IR_INSTITERATOR_H
19 #define LLVM_IR_INSTITERATOR_H
20
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/SymbolTableListTraits.h"
25 #include <iterator>
26
27 namespace llvm {
28
29 // This class implements inst_begin() & inst_end() for
30 // inst_iterator and const_inst_iterator's.
31 //
32 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
33 using BBty = BB_t;
34 using BBIty = BB_i_t;
35 using BIty = BI_t;
36 using IIty = II_t;
37 BB_t *BBs; // BasicBlocksType
38 BB_i_t BB; // BasicBlocksType::iterator
39 BI_t BI; // BasicBlock::iterator
40
41 public:
42 using iterator_category = std::bidirectional_iterator_tag;
43 using value_type = IIty;
44 using difference_type = signed;
45 using pointer = IIty *;
46 using reference = IIty &;
47
48 // Default constructor
49 InstIterator() = default;
50
51 // Copy constructor...
52 template<typename A, typename B, typename C, typename D>
InstIterator(const InstIterator<A,B,C,D> & II)53 InstIterator(const InstIterator<A,B,C,D> &II)
54 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
55
56 template<typename A, typename B, typename C, typename D>
InstIterator(InstIterator<A,B,C,D> & II)57 InstIterator(InstIterator<A,B,C,D> &II)
58 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
59
InstIterator(M & m)60 template<class M> InstIterator(M &m)
61 : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
62 if (BB != BBs->end()) {
63 BI = BB->begin();
64 advanceToNextBB();
65 }
66 }
67
InstIterator(M & m,bool)68 template<class M> InstIterator(M &m, bool)
69 : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
70 }
71
72 // Accessors to get at the underlying iterators...
getBasicBlockIterator()73 inline BBIty &getBasicBlockIterator() { return BB; }
getInstructionIterator()74 inline BIty &getInstructionIterator() { return BI; }
75
76 inline reference operator*() const { return *BI; }
77 inline pointer operator->() const { return &operator*(); }
78
79 inline bool operator==(const InstIterator &y) const {
80 return BB == y.BB && (BB == BBs->end() || BI == y.BI);
81 }
82 inline bool operator!=(const InstIterator& y) const {
83 return !operator==(y);
84 }
85
86 InstIterator& operator++() {
87 ++BI;
88 advanceToNextBB();
89 return *this;
90 }
91 inline InstIterator operator++(int) {
92 InstIterator tmp = *this; ++*this; return tmp;
93 }
94
95 InstIterator& operator--() {
96 while (BB == BBs->end() || BI == BB->begin()) {
97 --BB;
98 BI = BB->end();
99 }
100 --BI;
101 return *this;
102 }
103 inline InstIterator operator--(int) {
104 InstIterator tmp = *this; --*this; return tmp;
105 }
106
107 private:
advanceToNextBB()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 using inst_iterator =
120 InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
121 BasicBlock::iterator, Instruction>;
122 using const_inst_iterator =
123 InstIterator<const SymbolTableList<BasicBlock>,
124 Function::const_iterator, BasicBlock::const_iterator,
125 const Instruction>;
126 using inst_range = iterator_range<inst_iterator>;
127 using const_inst_range = iterator_range<const_inst_iterator>;
128
inst_begin(Function * F)129 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
inst_end(Function * F)130 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
instructions(Function * F)131 inline inst_range instructions(Function *F) {
132 return inst_range(inst_begin(F), inst_end(F));
133 }
inst_begin(const Function * F)134 inline const_inst_iterator inst_begin(const Function *F) {
135 return const_inst_iterator(*F);
136 }
inst_end(const Function * F)137 inline const_inst_iterator inst_end(const Function *F) {
138 return const_inst_iterator(*F, true);
139 }
instructions(const Function * F)140 inline const_inst_range instructions(const Function *F) {
141 return const_inst_range(inst_begin(F), inst_end(F));
142 }
inst_begin(Function & F)143 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
inst_end(Function & F)144 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
instructions(Function & F)145 inline inst_range instructions(Function &F) {
146 return inst_range(inst_begin(F), inst_end(F));
147 }
inst_begin(const Function & F)148 inline const_inst_iterator inst_begin(const Function &F) {
149 return const_inst_iterator(F);
150 }
inst_end(const Function & F)151 inline const_inst_iterator inst_end(const Function &F) {
152 return const_inst_iterator(F, true);
153 }
instructions(const Function & F)154 inline const_inst_range instructions(const Function &F) {
155 return const_inst_range(inst_begin(F), inst_end(F));
156 }
157
158 } // end namespace llvm
159
160 #endif // LLVM_IR_INSTITERATOR_H
161