blob: d76957051071f48d6dd2449250de02004627c871 [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
19#include "llvm/MC/MCInst.h"
20#include <vector>
21
22namespace mca {
23
24typedef std::pair<unsigned, const llvm::MCInst *> InstRef;
25
26class SourceMgr {
27 using InstVec = std::vector<std::unique_ptr<const llvm::MCInst>>;
Andrea Di Biagioc6590122018-04-09 16:39:52 +000028 const InstVec &Sequence;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000029 unsigned Current;
30 unsigned Iterations;
31 static const unsigned DefaultIterations = 70;
32
33public:
Andrea Di Biagioc6590122018-04-09 16:39:52 +000034 SourceMgr(const InstVec &MCInstSequence, unsigned NumIterations)
35 : 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(); }
41 const InstVec &getSequence() const { return Sequence; }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000042
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 Biagio3a6b0922018-03-08 13:05:02 +000061} // namespace mca
62
63#endif