1 //===--------------------- Instruction.cpp ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines abstractions used by the Pipeline to model register reads,
10 // register writes and instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MCA/Instruction.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
21 void WriteState::writeStartEvent(unsigned IID
, MCPhysReg RegID
,
26 DependentWriteCyclesLeft
= Cycles
;
27 DependentWrite
= nullptr;
30 void ReadState::writeStartEvent(unsigned IID
, MCPhysReg RegID
,
32 assert(DependentWrites
);
33 assert(CyclesLeft
== UNKNOWN_CYCLES
);
35 // This read may be dependent on more than one write. This typically occurs
36 // when a definition is the result of multiple writes where at least one
37 // write does a partial register update.
38 // The HW is forced to do some extra bookkeeping to track of all the
39 // dependent writes, and implement a merging scheme for the partial writes.
41 if (TotalCycles
< Cycles
) {
48 if (!DependentWrites
) {
49 CyclesLeft
= TotalCycles
;
50 IsReady
= !CyclesLeft
;
54 void WriteState::onInstructionIssued(unsigned IID
) {
55 assert(CyclesLeft
== UNKNOWN_CYCLES
);
56 // Update the number of cycles left based on the WriteDescriptor info.
57 CyclesLeft
= getLatency();
59 // Now that the time left before write-back is known, notify
61 for (const std::pair
<ReadState
*, int> &User
: Users
) {
62 ReadState
*RS
= User
.first
;
63 unsigned ReadCycles
= std::max(0, CyclesLeft
- User
.second
);
64 RS
->writeStartEvent(IID
, RegisterID
, ReadCycles
);
67 // Notify any writes that are in a false dependency with this write.
69 PartialWrite
->writeStartEvent(IID
, RegisterID
, CyclesLeft
);
72 void WriteState::addUser(unsigned IID
, ReadState
*User
, int ReadAdvance
) {
73 // If CyclesLeft is different than -1, then we don't need to
74 // update the list of users. We can just notify the user with
75 // the actual number of cycles left (which may be zero).
76 if (CyclesLeft
!= UNKNOWN_CYCLES
) {
77 unsigned ReadCycles
= std::max(0, CyclesLeft
- ReadAdvance
);
78 User
->writeStartEvent(IID
, RegisterID
, ReadCycles
);
82 Users
.emplace_back(User
, ReadAdvance
);
85 void WriteState::addUser(unsigned IID
, WriteState
*User
) {
86 if (CyclesLeft
!= UNKNOWN_CYCLES
) {
87 User
->writeStartEvent(IID
, RegisterID
, std::max(0, CyclesLeft
));
91 assert(!PartialWrite
&& "PartialWrite already set!");
93 User
->setDependentWrite(this);
96 void WriteState::cycleEvent() {
97 // Note: CyclesLeft can be a negative number. It is an error to
98 // make it an unsigned quantity because users of this write may
99 // specify a negative ReadAdvance.
100 if (CyclesLeft
!= UNKNOWN_CYCLES
)
103 if (DependentWriteCyclesLeft
)
104 DependentWriteCyclesLeft
--;
107 void ReadState::cycleEvent() {
108 // Update the total number of cycles.
109 if (DependentWrites
&& TotalCycles
) {
114 // Bail out immediately if we don't know how many cycles are left.
115 if (CyclesLeft
== UNKNOWN_CYCLES
)
120 IsReady
= !CyclesLeft
;
125 void WriteState::dump() const {
126 dbgs() << "{ OpIdx=" << WD
->OpIndex
<< ", Lat=" << getLatency() << ", RegID "
127 << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }";
131 const CriticalDependency
&Instruction::computeCriticalRegDep() {
132 if (CriticalRegDep
.Cycles
)
133 return CriticalRegDep
;
135 unsigned MaxLatency
= 0;
136 for (const WriteState
&WS
: getDefs()) {
137 const CriticalDependency
&WriteCRD
= WS
.getCriticalRegDep();
138 if (WriteCRD
.Cycles
> MaxLatency
)
139 CriticalRegDep
= WriteCRD
;
142 for (const ReadState
&RS
: getUses()) {
143 const CriticalDependency
&ReadCRD
= RS
.getCriticalRegDep();
144 if (ReadCRD
.Cycles
> MaxLatency
)
145 CriticalRegDep
= ReadCRD
;
148 return CriticalRegDep
;
151 void Instruction::reset() {
152 // Note that this won't clear read/write descriptors
153 // or other non-trivial fields
155 CyclesLeft
= UNKNOWN_CYCLES
;
156 clearOptimizableMove();
159 CriticalResourceMask
= 0;
160 IsEliminated
= false;
163 void Instruction::dispatch(unsigned RCUToken
) {
164 assert(Stage
== IS_INVALID
);
165 Stage
= IS_DISPATCHED
;
166 RCUTokenID
= RCUToken
;
168 // Check if input operands are already available.
169 if (updateDispatched())
173 void Instruction::execute(unsigned IID
) {
174 assert(Stage
== IS_READY
);
175 Stage
= IS_EXECUTING
;
177 // Set the cycles left before the write-back stage.
178 CyclesLeft
= getLatency();
180 for (WriteState
&WS
: getDefs())
181 WS
.onInstructionIssued(IID
);
183 // Transition to the "executed" stage if this is a zero-latency instruction.
188 void Instruction::forceExecuted() {
189 assert(Stage
== IS_READY
&& "Invalid internal state!");
194 bool Instruction::updatePending() {
195 assert(isPending() && "Unexpected instruction stage found!");
197 if (!all_of(getUses(), [](const ReadState
&Use
) { return Use
.isReady(); }))
200 // A partial register write cannot complete before a dependent write.
201 if (!all_of(getDefs(), [](const WriteState
&Def
) { return Def
.isReady(); }))
208 bool Instruction::updateDispatched() {
209 assert(isDispatched() && "Unexpected instruction stage found!");
211 if (!all_of(getUses(), [](const ReadState
&Use
) {
212 return Use
.isPending() || Use
.isReady();
216 // A partial register write cannot complete before a dependent write.
217 if (!all_of(getDefs(),
218 [](const WriteState
&Def
) { return !Def
.getDependentWrite(); }))
225 void Instruction::update() {
232 void Instruction::cycleEvent() {
236 if (isDispatched() || isPending()) {
237 for (ReadState
&Use
: getUses())
240 for (WriteState
&Def
: getDefs())
247 assert(isExecuting() && "Instruction not in-flight?");
248 assert(CyclesLeft
&& "Instruction already executed?");
249 for (WriteState
&Def
: getDefs())