blob: 4a55bdba5b41028df958b23633723f59fe72a8ca [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
Fangrui Song5a8fd652018-10-30 15:56:08 +000021namespace llvm {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000022namespace mca {
23
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +000024class Instruction;
25
26typedef std::pair<unsigned, const Instruction &> SourceRef;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000027
28class SourceMgr {
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +000029 using UniqueInst = std::unique_ptr<Instruction>;
30 llvm::ArrayRef<UniqueInst> Sequence;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000031 unsigned Current;
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000032 const unsigned Iterations;
Andrea Di Biagio074cef32018-04-10 12:50:03 +000033 static const unsigned DefaultIterations = 100;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000034
35public:
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +000036 SourceMgr(llvm::ArrayRef<UniqueInst> S, unsigned Iter)
37 : Sequence(S), Current(0), Iterations(Iter ? Iter : DefaultIterations) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000038
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000039 unsigned getNumIterations() const { return Iterations; }
40 unsigned size() const { return Sequence.size(); }
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000041 bool hasNext() const { return Current < (Iterations * Sequence.size()); }
42 void updateNext() { ++Current; }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000043
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +000044 SourceRef peekNext() const {
Andrea Di Biagio492816d2018-08-16 15:43:09 +000045 assert(hasNext() && "Already at end of sequence!");
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +000046 return SourceRef(Current, *Sequence[Current % Sequence.size()]);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000047 }
48
Andrea Di Biagiodf4d65d2018-10-29 13:29:22 +000049 using const_iterator = llvm::ArrayRef<UniqueInst>::const_iterator;
Andrea Di Biagio7be45b0f2018-10-24 15:06:27 +000050 const_iterator begin() const { return Sequence.begin(); }
51 const_iterator end() const { return Sequence.end(); }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000052};
Andrea Di Biagio84d00512018-10-26 10:48:04 +000053
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000054} // namespace mca
Fangrui Song5a8fd652018-10-30 15:56:08 +000055} // namespace llvm
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000056
57#endif