Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:01

0001 //===- InstIterator.h - Classes for inst iteration --------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains definitions of two iterators for iterating over the
0010 // instructions in a function.  This is effectively a wrapper around a two level
0011 // iterator that can probably be genericized later.
0012 //
0013 // Note that this iterator gets invalidated any time that basic blocks or
0014 // instructions are moved around.
0015 //
0016 //===----------------------------------------------------------------------===//
0017 
0018 #ifndef LLVM_IR_INSTITERATOR_H
0019 #define LLVM_IR_INSTITERATOR_H
0020 
0021 #include "llvm/ADT/iterator_range.h"
0022 #include "llvm/IR/BasicBlock.h"
0023 #include "llvm/IR/Function.h"
0024 #include "llvm/IR/SymbolTableListTraits.h"
0025 #include <iterator>
0026 
0027 namespace llvm {
0028 
0029 // This class implements inst_begin() & inst_end() for
0030 // inst_iterator and const_inst_iterator's.
0031 //
0032 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
0033   using BBty = BB_t;
0034   using BBIty = BB_i_t;
0035   using BIty = BI_t;
0036   using IIty = II_t;
0037   BB_t *BBs; // BasicBlocksType
0038   BB_i_t BB; // BasicBlocksType::iterator
0039   BI_t BI;   // BasicBlock::iterator
0040 
0041 public:
0042   using iterator_category = std::bidirectional_iterator_tag;
0043   using value_type = IIty;
0044   using difference_type = signed;
0045   using pointer = IIty *;
0046   using reference = IIty &;
0047 
0048   // Default constructor
0049   InstIterator() = default;
0050 
0051   // Copy constructor...
0052   template<typename A, typename B, typename C, typename D>
0053   InstIterator(const InstIterator<A,B,C,D> &II)
0054     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
0055 
0056   template<typename A, typename B, typename C, typename D>
0057   InstIterator(InstIterator<A,B,C,D> &II)
0058     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
0059 
0060   template<class M> InstIterator(M &m)
0061     : BBs(&m.getBasicBlockList()), BB(BBs->begin()) {    // begin ctor
0062     if (BB != BBs->end()) {
0063       BI = BB->begin();
0064       advanceToNextBB();
0065     }
0066   }
0067 
0068   template<class M> InstIterator(M &m, bool)
0069     : BBs(&m.getBasicBlockList()), BB(BBs->end()) {    // end ctor
0070   }
0071 
0072   // Accessors to get at the underlying iterators...
0073   inline BBIty &getBasicBlockIterator()  { return BB; }
0074   inline BIty  &getInstructionIterator() { return BI; }
0075 
0076   inline reference operator*()  const { return *BI; }
0077   inline pointer operator->() const { return &operator*(); }
0078 
0079   inline bool operator==(const InstIterator &y) const {
0080     return BB == y.BB && (BB == BBs->end() || BI == y.BI);
0081   }
0082   inline bool operator!=(const InstIterator& y) const {
0083     return !operator==(y);
0084   }
0085 
0086   InstIterator& operator++() {
0087     ++BI;
0088     advanceToNextBB();
0089     return *this;
0090   }
0091   inline InstIterator operator++(int) {
0092     InstIterator tmp = *this; ++*this; return tmp;
0093   }
0094 
0095   InstIterator& operator--() {
0096     while (BB == BBs->end() || BI == BB->begin()) {
0097       --BB;
0098       BI = BB->end();
0099     }
0100     --BI;
0101     return *this;
0102   }
0103   inline InstIterator operator--(int) {
0104     InstIterator tmp = *this; --*this; return tmp;
0105   }
0106 
0107 private:
0108   inline void advanceToNextBB() {
0109     // The only way that the II could be broken is if it is now pointing to
0110     // the end() of the current BasicBlock and there are successor BBs.
0111     while (BI == BB->end()) {
0112       ++BB;
0113       if (BB == BBs->end()) break;
0114       BI = BB->begin();
0115     }
0116   }
0117 };
0118 
0119 using inst_iterator =
0120     InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
0121                  BasicBlock::iterator, Instruction>;
0122 using const_inst_iterator =
0123     InstIterator<const SymbolTableList<BasicBlock>,
0124                  Function::const_iterator, BasicBlock::const_iterator,
0125                  const Instruction>;
0126 using inst_range = iterator_range<inst_iterator>;
0127 using const_inst_range = iterator_range<const_inst_iterator>;
0128 
0129 inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
0130 inline inst_iterator inst_end(Function *F)   { return inst_iterator(*F, true); }
0131 inline inst_range instructions(Function *F) {
0132   return inst_range(inst_begin(F), inst_end(F));
0133 }
0134 inline const_inst_iterator inst_begin(const Function *F) {
0135   return const_inst_iterator(*F);
0136 }
0137 inline const_inst_iterator inst_end(const Function *F) {
0138   return const_inst_iterator(*F, true);
0139 }
0140 inline const_inst_range instructions(const Function *F) {
0141   return const_inst_range(inst_begin(F), inst_end(F));
0142 }
0143 inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
0144 inline inst_iterator inst_end(Function &F)   { return inst_iterator(F, true); }
0145 inline inst_range instructions(Function &F) {
0146   return inst_range(inst_begin(F), inst_end(F));
0147 }
0148 inline const_inst_iterator inst_begin(const Function &F) {
0149   return const_inst_iterator(F);
0150 }
0151 inline const_inst_iterator inst_end(const Function &F) {
0152   return const_inst_iterator(F, true);
0153 }
0154 inline const_inst_range instructions(const Function &F) {
0155   return const_inst_range(inst_begin(F), inst_end(F));
0156 }
0157 
0158 } // end namespace llvm
0159 
0160 #endif // LLVM_IR_INSTITERATOR_H