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 | |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/ArrayRef.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace mca { |
| 24 | |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 25 | typedef std::pair<unsigned, const llvm::MCInst &> SourceRef; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 26 | |
| 27 | class SourceMgr { |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 28 | llvm::ArrayRef<llvm::MCInst> Sequence; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 29 | unsigned Current; |
| 30 | unsigned Iterations; |
Andrea Di Biagio | 074cef3 | 2018-04-10 12:50:03 +0000 | [diff] [blame] | 31 | static const unsigned DefaultIterations = 100; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 32 | |
| 33 | public: |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 34 | SourceMgr(llvm::ArrayRef<llvm::MCInst> MCInstSequence, unsigned NumIterations) |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 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(); } |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 41 | llvm::ArrayRef<llvm::MCInst> getSequence() const { return Sequence; } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 42 | |
Matt Davis | 73dd5f9 | 2018-07-12 23:19:30 +0000 | [diff] [blame] | 43 | bool hasNext() const { return Current < (Iterations * size()); } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 44 | void updateNext() { Current++; } |
| 45 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 46 | const SourceRef peekNext() const { |
Andrea Di Biagio | 492816d | 2018-08-16 15:43:09 +0000 | [diff] [blame] | 47 | assert(hasNext() && "Already at end of sequence!"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 48 | unsigned Index = getCurrentInstructionIndex(); |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 49 | return SourceRef(Current, Sequence[Index]); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | unsigned getCurrentInstructionIndex() const { |
| 53 | return Current % Sequence.size(); |
| 54 | } |
| 55 | |
| 56 | const llvm::MCInst &getMCInstFromIndex(unsigned Index) const { |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame^] | 57 | return Sequence[Index % size()]; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool isEmpty() const { return size() == 0; } |
| 61 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 62 | } // namespace mca |
| 63 | |
| 64 | #endif |