LCOV - code coverage report
Current view: top level - include/llvm/IR - InstIterator.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 32 37 86.5 %
Date: 2017-09-14 15:23:50 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- 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_IR_INSTITERATOR_H
      20             : #define LLVM_IR_INSTITERATOR_H
      21             : 
      22             : #include "llvm/ADT/iterator_range.h"
      23             : #include "llvm/IR/BasicBlock.h"
      24             : #include "llvm/IR/Function.h"
      25             : #include "llvm/IR/SymbolTableListTraits.h"
      26             : #include <iterator>
      27             : 
      28             : namespace llvm {
      29             : 
      30             : // This class implements inst_begin() & inst_end() for
      31             : // inst_iterator and const_inst_iterator's.
      32             : //
      33             : template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
      34             :   using BBty = BB_t;
      35             :   using BBIty = BB_i_t;
      36             :   using BIty = BI_t;
      37             :   using IIty = II_t;
      38             :   BB_t *BBs; // BasicBlocksType
      39             :   BB_i_t BB; // BasicBlocksType::iterator
      40             :   BI_t BI;   // BasicBlock::iterator
      41             : 
      42             : public:
      43             :   using iterator_category = std::bidirectional_iterator_tag;
      44             :   using value_type = IIty;
      45             :   using difference_type = signed;
      46             :   using pointer = IIty *;
      47             :   using reference = IIty &;
      48             : 
      49             :   // Default constructor
      50             :   InstIterator() = default;
      51             : 
      52             :   // Copy constructor...
      53             :   template<typename A, typename B, typename C, typename D>
      54             :   InstIterator(const InstIterator<A,B,C,D> &II)
      55             :     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
      56             : 
      57             :   template<typename A, typename B, typename C, typename D>
      58       31057 :   InstIterator(InstIterator<A,B,C,D> &II)
      59     1069628 :     : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
      60             : 
      61      861205 :   template<class M> InstIterator(M &m)
      62     2583615 :     : BBs(&m.getBasicBlockList()), BB(BBs->begin()) {    // begin ctor
      63     1722410 :     if (BB != BBs->end()) {
      64     2567970 :       BI = BB->begin();
      65             :       advanceToNextBB();
      66             :     }
      67      861205 :   }
      68             : 
      69      861637 :   template<class M> InstIterator(M &m, bool)
      70     2167284 :     : BBs(&m.getBasicBlockList()), BB(BBs->end()) {    // end ctor
      71             :   }
      72             : 
      73             :   // Accessors to get at the underlying iterators...
      74             :   inline BBIty &getBasicBlockIterator()  { return BB; }
      75             :   inline BIty  &getInstructionIterator() { return BI; }
      76             : 
      77    42213666 :   inline reference operator*()  const { return *BI; }
      78        2623 :   inline pointer operator->() const { return &operator*(); }
      79             : 
      80             :   inline bool operator==(const InstIterator &y) const {
      81    44758491 :     return BB == y.BB && (BB == BBs->end() || BI == y.BI);
      82             :   }
      83             :   inline bool operator!=(const InstIterator& y) const {
      84             :     return !operator==(y);
      85             :   }
      86             : 
      87    21056809 :   InstIterator& operator++() {
      88    21056809 :     ++BI;
      89    21056809 :     advanceToNextBB();
      90    21056809 :     return *this;
      91             :   }
      92             :   inline InstIterator operator++(int) {
      93        6191 :     InstIterator tmp = *this; ++*this; return tmp;
      94             :   }
      95             : 
      96             :   InstIterator& operator--() {
      97         108 :     while (BB == BBs->end() || BI == BB->begin()) {
      98           0 :       --BB;
      99           0 :       BI = BB->end();
     100             :     }
     101          36 :     --BI;
     102             :     return *this;
     103             :   }
     104             :   inline InstIterator operator--(int) {
     105             :     InstIterator tmp = *this; --*this; return tmp;
     106             :   }
     107             : 
     108             :   inline bool atEnd() const { return BB == BBs->end(); }
     109             : 
     110             : private:
     111             :   inline void advanceToNextBB() {
     112             :     // The only way that the II could be broken is if it is now pointing to
     113             :     // the end() of the current BasicBlock and there are successor BBs.
     114    93252552 :     while (BI == BB->end()) {
     115     4450012 :       ++BB;
     116     4450012 :       if (BB == BBs->end()) break;
     117     4201017 :       BI = BB->begin();
     118             :     }
     119             :   }
     120             : };
     121             : 
     122             : using inst_iterator =
     123             :     InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
     124             :                  BasicBlock::iterator, Instruction>;
     125             : using const_inst_iterator =
     126             :     InstIterator<const SymbolTableList<BasicBlock>,
     127             :                  Function::const_iterator, BasicBlock::const_iterator,
     128             :                  const Instruction>;
     129             : using inst_range = iterator_range<inst_iterator>;
     130             : using const_inst_range = iterator_range<const_inst_iterator>;
     131             : 
     132       34452 : inline inst_iterator inst_begin(Function *F) { return inst_iterator(*F); }
     133       35023 : inline inst_iterator inst_end(Function *F)   { return inst_iterator(*F, true); }
     134             : inline inst_range instructions(Function *F) {
     135      101556 :   return inst_range(inst_begin(F), inst_end(F));
     136             : }
     137             : inline const_inst_iterator inst_begin(const Function *F) {
     138      175977 :   return const_inst_iterator(*F);
     139             : }
     140             : inline const_inst_iterator inst_end(const Function *F) {
     141      175977 :   return const_inst_iterator(*F, true);
     142             : }
     143             : inline const_inst_range instructions(const Function *F) {
     144         147 :   return const_inst_range(inst_begin(F), inst_end(F));
     145             : }
     146      650776 : inline inst_iterator inst_begin(Function &F) { return inst_iterator(F); }
     147      650776 : inline inst_iterator inst_end(Function &F)   { return inst_iterator(F, true); }
     148             : inline inst_range instructions(Function &F) {
     149     1152429 :   return inst_range(inst_begin(F), inst_end(F));
     150             : }
     151             : inline const_inst_iterator inst_begin(const Function &F) {
     152           0 :   return const_inst_iterator(F);
     153             : }
     154             : inline const_inst_iterator inst_end(const Function &F) {
     155           0 :   return const_inst_iterator(F, true);
     156             : }
     157             : inline const_inst_range instructions(const Function &F) {
     158           0 :   return const_inst_range(inst_begin(F), inst_end(F));
     159             : }
     160             : 
     161             : } // end namespace llvm
     162             : 
     163             : #endif // LLVM_IR_INSTITERATOR_H

Generated by: LCOV version 1.13