blob: 127135882460e04e60c17209bc03c5591d45a743 [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;
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000030 const 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
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000038 unsigned getNumIterations() const { return Iterations; }
39 unsigned size() const { return Sequence.size(); }
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000040 bool hasNext() const { return Current < (Iterations * Sequence.size()); }
41 void updateNext() { ++Current; }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000042
Matt Davis21a8d322018-05-07 18:29:15 +000043 const SourceRef peekNext() const {
Andrea Di Biagio492816d2018-08-16 15:43:09 +000044 assert(hasNext() && "Already at end of sequence!");
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000045 return SourceRef(Current, Sequence[Current % Sequence.size()]);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000046 }
47
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000048 using const_iterator = llvm::ArrayRef<llvm::MCInst>::const_iterator;
49 const_iterator begin() const { return Sequence.begin(); }
50 const_iterator end() const { return Sequence.end(); }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000051};
Andrea Di Biagio84d00512018-10-26 10:48:04 +000052
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000053} // namespace mca
54
55#endif