LLVM 22.0.0git
IncrementalSourceMgr.h
Go to the documentation of this file.
1//===---------------- IncrementalSourceMgr.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 contains IncrementalSourceMgr, an implementation of SourceMgr
10/// that allows users to add new instructions incrementally / dynamically.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MCA_INCREMENTALSOURCEMGR_H
15#define LLVM_MCA_INCREMENTALSOURCEMGR_H
16
17#include "llvm/MCA/SourceMgr.h"
19#include <deque>
20
21namespace llvm {
22namespace mca {
23
24/// An implementation of \a SourceMgr that allows users to add new instructions
25/// incrementally / dynamically.
26/// Note that this SourceMgr takes ownership of all \a mca::Instruction.
28 /// Owner of all mca::Instruction instances. Note that we use std::deque here
29 /// to have a better throughput, in comparison to std::vector or
30 /// llvm::SmallVector, as they usually pay a higher re-allocation cost when
31 /// there is a large number of instructions.
32 std::deque<UniqueInst> InstStorage;
33
34 /// Instructions that are ready to be used. Each of them is a pointer of an
35 /// \a UniqueInst inside InstStorage.
36 std::deque<Instruction *> Staging;
37
38 /// Current instruction index.
39 unsigned TotalCounter = 0U;
40
41 /// End-of-stream flag.
42 bool EOS = false;
43
44 /// Called when an instruction is no longer needed.
45 using InstFreedCallback = std::function<void(Instruction *)>;
46 InstFreedCallback InstFreedCB;
47
48public:
50
51 // Explicitly non-copyable.
54
55 void clear();
56
57 /// Set a callback that is invoked when a mca::Instruction is
58 /// no longer needed. This is usually used for recycling the
59 /// instruction.
60 void setOnInstFreedCallback(InstFreedCallback CB) { InstFreedCB = CB; }
61
63 llvm_unreachable("Not applicable");
64 }
65
66 bool hasNext() const override { return !Staging.empty(); }
67 bool isEnd() const override { return EOS; }
68
69 SourceRef peekNext() const override {
70 assert(hasNext());
71 return SourceRef(TotalCounter, *Staging.front());
72 }
73
74 /// Add a new instruction.
75 void addInst(UniqueInst &&Inst) {
76 InstStorage.emplace_back(std::move(Inst));
77 Staging.push_back(InstStorage.back().get());
78 }
79
80 /// Add a recycled instruction.
81 void addRecycledInst(Instruction *Inst) { Staging.push_back(Inst); }
82
83 void updateNext() override;
84
85 /// Mark the end of instruction stream.
86 void endOfStream() { EOS = true; }
87
88#ifndef NDEBUG
89 /// Print statistic about instruction recycling stats.
90 void printStatistic(raw_ostream &OS);
91#endif
92};
93
94} // end namespace mca
95} // end namespace llvm
96
97#endif // LLVM_MCA_INCREMENTALSOURCEMGR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
This file contains abstract class SourceMgr and the default implementation, CircularSourceMgr.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
void setOnInstFreedCallback(InstFreedCallback CB)
Set a callback that is invoked when a mca::Instruction is no longer needed.
void addRecycledInst(Instruction *Inst)
Add a recycled instruction.
bool isEnd() const override
Whether the instruction stream has eneded.
void addInst(UniqueInst &&Inst)
Add a new instruction.
ArrayRef< UniqueInst > getInstructions() const override
Provides a fixed range of UniqueInst to iterate.
SourceRef peekNext() const override
The next SourceRef.
bool hasNext() const override
Whether there is any SourceRef to inspect / peek next.
void endOfStream()
Mark the end of instruction stream.
IncrementalSourceMgr & operator=(const IncrementalSourceMgr &)=delete
IncrementalSourceMgr(const IncrementalSourceMgr &)=delete
An instruction propagated through the simulated instruction pipeline.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< unsigned, const Instruction & > SourceRef
Definition SourceMgr.h:25
This is an optimization pass for GlobalISel generic memory operations.
Abstracting the input code sequence (a sequence of MCInst) and assigning unique identifiers to every ...
Definition SourceMgr.h:29
std::unique_ptr< Instruction > UniqueInst
Definition SourceMgr.h:30