LLVM 20.0.0git
InstIterator.h
Go to the documentation of this file.
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
22#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Function.h"
25#include <iterator>
26
27namespace llvm {
28
29// This class implements inst_begin() & inst_end() for
30// inst_iterator and const_inst_iterator's.
31//
32template <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
41public:
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>
54 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
55
56 template<typename A, typename B, typename C, typename D>
58 : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
59
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
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...
73 inline BBIty &getBasicBlockIterator() { return BB; }
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
87 ++BI;
88 advanceToNextBB();
89 return *this;
90 }
92 InstIterator tmp = *this; ++*this; return tmp;
93 }
94
96 while (BB == BBs->end() || BI == BB->begin()) {
97 --BB;
98 BI = BB->end();
99 }
100 --BI;
101 return *this;
102 }
104 InstIterator tmp = *this; --*this; return tmp;
105 }
106
107 inline bool atEnd() const { return BB == BBs->end(); }
108
109private:
110 inline void advanceToNextBB() {
111 // The only way that the II could be broken is if it is now pointing to
112 // the end() of the current BasicBlock and there are successor BBs.
113 while (BI == BB->end()) {
114 ++BB;
115 if (BB == BBs->end()) break;
116 BI = BB->begin();
117 }
118 }
119};
120
127 const Instruction>;
130
132inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
134 return inst_range(inst_begin(F), inst_end(F));
135}
137 return const_inst_iterator(*F);
138}
140 return const_inst_iterator(*F, true);
141}
144}
146inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
148 return inst_range(inst_begin(F), inst_end(F));
149}
151 return const_inst_iterator(F);
152}
154 return const_inst_iterator(F, true);
155}
158}
159
160} // end namespace llvm
161
162#endif // LLVM_IR_INSTITERATOR_H
Expand Atomic instructions
#define F(x, y, z)
Definition: MD5.cpp:55
uint64_t IntrinsicInst * II
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:178
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
BasicBlockListType::iterator iterator
Definition: Function.h:69
BasicBlockListType::const_iterator const_iterator
Definition: Function.h:70
InstIterator & operator++()
Definition: InstIterator.h:86
reference operator*() const
Definition: InstIterator.h:76
InstIterator & operator--()
Definition: InstIterator.h:95
InstIterator(const InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:53
bool operator!=(const InstIterator &y) const
Definition: InstIterator.h:82
InstIterator(M &m, bool)
Definition: InstIterator.h:68
bool operator==(const InstIterator &y) const
Definition: InstIterator.h:79
InstIterator operator++(int)
Definition: InstIterator.h:91
std::bidirectional_iterator_tag iterator_category
Definition: InstIterator.h:42
InstIterator(InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:57
InstIterator operator--(int)
Definition: InstIterator.h:103
BIty & getInstructionIterator()
Definition: InstIterator.h:74
BBIty & getBasicBlockIterator()
Definition: InstIterator.h:73
bool atEnd() const
Definition: InstIterator.h:107
pointer operator->() const
Definition: InstIterator.h:77
InstIterator()=default
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< inst_iterator > inst_range
Definition: InstIterator.h:128
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
Definition: InstIterator.h:123
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
iterator_range< const_inst_iterator > const_inst_range
Definition: InstIterator.h:129
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
InstIterator< const SymbolTableList< BasicBlock >, Function::const_iterator, BasicBlock::const_iterator, const Instruction > const_inst_iterator
Definition: InstIterator.h:127