Home | History | Annotate | Download | only in llvm-mca
      1 //===--------------------- SourceMgr.h --------------------------*- 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 /// \file
     10 /// This file implements class SourceMgr. Class SourceMgr abstracts the input
     11 /// code sequence (a sequence of MCInst), and assings unique identifiers to
     12 /// every instruction in the sequence.
     13 ///
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_TOOLS_LLVM_MCA_SOURCEMGR_H
     17 #define LLVM_TOOLS_LLVM_MCA_SOURCEMGR_H
     18 
     19 #include "llvm/MC/MCInst.h"
     20 #include <vector>
     21 
     22 namespace mca {
     23 
     24 typedef std::pair<unsigned, const llvm::MCInst *> SourceRef;
     25 
     26 class SourceMgr {
     27   using InstVec = std::vector<std::unique_ptr<const llvm::MCInst>>;
     28   const InstVec &Sequence;
     29   unsigned Current;
     30   unsigned Iterations;
     31   static const unsigned DefaultIterations = 100;
     32 
     33 public:
     34   SourceMgr(const InstVec &MCInstSequence, unsigned NumIterations)
     35       : Sequence(MCInstSequence), Current(0),
     36         Iterations(NumIterations ? NumIterations : DefaultIterations) {}
     37 
     38   unsigned getCurrentIteration() const { return Current / Sequence.size(); }
     39   unsigned getNumIterations() const { return Iterations; }
     40   unsigned size() const { return Sequence.size(); }
     41   const InstVec &getSequence() const { return Sequence; }
     42 
     43   bool hasNext() const { return Current < (Iterations * size()); }
     44   void updateNext() { Current++; }
     45 
     46   const SourceRef peekNext() const {
     47     unsigned Index = getCurrentInstructionIndex();
     48     return SourceRef(Current, Sequence[Index].get());
     49   }
     50 
     51   unsigned getCurrentInstructionIndex() const {
     52     return Current % Sequence.size();
     53   }
     54 
     55   const llvm::MCInst &getMCInstFromIndex(unsigned Index) const {
     56     return *Sequence[Index % size()];
     57   }
     58 
     59   bool isEmpty() const { return size() == 0; }
     60 };
     61 } // namespace mca
     62 
     63 #endif
     64