blob: dbe31db1b1ddbd97c41c6178729daa245ba5a359 [file] [log] [blame]
Andrew Walbran16937d02019-10-22 13:54:20 +01001//===--------------------- SourceMgr.h --------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements class SourceMgr. Class SourceMgr abstracts the input
10/// code sequence (a sequence of MCInst), and assings unique identifiers to
11/// every instruction in the sequence.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_MCA_SOURCEMGR_H
16#define LLVM_MCA_SOURCEMGR_H
17
18#include "llvm/ADT/ArrayRef.h"
19
20namespace llvm {
21namespace mca {
22
23class Instruction;
24
25typedef std::pair<unsigned, const Instruction &> SourceRef;
26
27class SourceMgr {
28 using UniqueInst = std::unique_ptr<Instruction>;
29 ArrayRef<UniqueInst> Sequence;
30 unsigned Current;
31 const unsigned Iterations;
32 static const unsigned DefaultIterations = 100;
33
34public:
35 SourceMgr(ArrayRef<UniqueInst> S, unsigned Iter)
36 : Sequence(S), Current(0), Iterations(Iter ? Iter : DefaultIterations) {}
37
38 unsigned getNumIterations() const { return Iterations; }
39 unsigned size() const { return Sequence.size(); }
40 bool hasNext() const { return Current < (Iterations * Sequence.size()); }
41 void updateNext() { ++Current; }
42
43 SourceRef peekNext() const {
44 assert(hasNext() && "Already at end of sequence!");
45 return SourceRef(Current, *Sequence[Current % Sequence.size()]);
46 }
47
48 using const_iterator = ArrayRef<UniqueInst>::const_iterator;
49 const_iterator begin() const { return Sequence.begin(); }
50 const_iterator end() const { return Sequence.end(); }
51};
52
53} // namespace mca
54} // namespace llvm
55
56#endif // LLVM_MCA_SOURCEMGR_H