blob: 894128363605958bd5c1918686b868f760ec5094 [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- 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 Biagio01b9fd62018-10-22 15:36:15 +000019#include "llvm/ADT/ArrayRef.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000020#include "llvm/MC/MCInst.h"
21#include <vector>
22
23namespace mca {
24
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000025typedef std::pair<unsigned, const llvm::MCInst &> SourceRef;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000026
27class SourceMgr {
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000028 llvm::ArrayRef<llvm::MCInst> Sequence;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000029 unsigned Current;
30 unsigned Iterations;
Andrea Di Biagio074cef32018-04-10 12:50:03 +000031 static const unsigned DefaultIterations = 100;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000032
33public:
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000034 SourceMgr(llvm::ArrayRef<llvm::MCInst> MCInstSequence, unsigned NumIterations)
Andrea Di Biagioc6590122018-04-09 16:39:52 +000035 : Sequence(MCInstSequence), Current(0),
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000036 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 Biagio01b9fd62018-10-22 15:36:15 +000041 llvm::ArrayRef<llvm::MCInst> getSequence() const { return Sequence; }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000042
Matt Davis73dd5f92018-07-12 23:19:30 +000043 bool hasNext() const { return Current < (Iterations * size()); }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000044 void updateNext() { Current++; }
45
Matt Davis21a8d322018-05-07 18:29:15 +000046 const SourceRef peekNext() const {
Andrea Di Biagio492816d2018-08-16 15:43:09 +000047 assert(hasNext() && "Already at end of sequence!");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000048 unsigned Index = getCurrentInstructionIndex();
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000049 return SourceRef(Current, Sequence[Index]);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000050 }
51
52 unsigned getCurrentInstructionIndex() const {
53 return Current % Sequence.size();
54 }
55
56 const llvm::MCInst &getMCInstFromIndex(unsigned Index) const {
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000057 return Sequence[Index % size()];
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000058 }
59
60 bool isEmpty() const { return size() == 0; }
61};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000062} // namespace mca
63
64#endif