Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 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 *> InstRef; |
| 25 | |
| 26 | class SourceMgr { |
| 27 | using InstVec = std::vector<std::unique_ptr<const llvm::MCInst>>; |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame^] | 28 | const InstVec &Sequence; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 29 | unsigned Current; |
| 30 | unsigned Iterations; |
| 31 | static const unsigned DefaultIterations = 70; |
| 32 | |
| 33 | public: |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame^] | 34 | SourceMgr(const InstVec &MCInstSequence, unsigned NumIterations) |
| 35 | : Sequence(MCInstSequence), Current(0), |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 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; } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 42 | |
| 43 | bool hasNext() { return Current < (Iterations * size()); } |
| 44 | void updateNext() { Current++; } |
| 45 | |
| 46 | const InstRef peekNext() const { |
| 47 | unsigned Index = getCurrentInstructionIndex(); |
| 48 | return InstRef(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 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 61 | } // namespace mca |
| 62 | |
| 63 | #endif |