blob: f0f4de5a5fe00a14ea12acb3df64e0a84f88c65e [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- Instruction.cpp ----------------------*- 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//
10// This file defines abstractions used by the Backend to model register reads,
11// register writes and instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Instruction.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/raw_ostream.h"
18
19namespace mca {
20
21using namespace llvm;
22
23void ReadState::writeStartEvent(unsigned Cycles) {
24 assert(DependentWrites);
25 assert(CyclesLeft == UNKNOWN_CYCLES);
26
27 // This read may be dependent on more than one write. This typically occurs
28 // when a definition is the result of multiple writes where at least one
29 // write does a partial register update.
30 // The HW is forced to do some extra bookkeeping to track of all the
31 // dependent writes, and implement a merging scheme for the partial writes.
32 --DependentWrites;
33 TotalCycles = std::max(TotalCycles, Cycles);
34
35 if (!DependentWrites)
36 CyclesLeft = TotalCycles;
37}
38
39void WriteState::onInstructionIssued() {
40 assert(CyclesLeft == UNKNOWN_CYCLES);
41 // Update the number of cycles left based on the WriteDescriptor info.
42 CyclesLeft = WD.Latency;
43
Andrea Di Biagio757600b2018-06-05 17:12:02 +000044 // Now that the time left before write-back is known, notify
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000045 // all the users.
46 for (const std::pair<ReadState *, int> &User : Users) {
47 ReadState *RS = User.first;
48 unsigned ReadCycles = std::max(0, CyclesLeft - User.second);
49 RS->writeStartEvent(ReadCycles);
50 }
51}
52
53void WriteState::addUser(ReadState *User, int ReadAdvance) {
54 // If CyclesLeft is different than -1, then we don't need to
55 // update the list of users. We can just notify the user with
56 // the actual number of cycles left (which may be zero).
57 if (CyclesLeft != UNKNOWN_CYCLES) {
58 unsigned ReadCycles = std::max(0, CyclesLeft - ReadAdvance);
59 User->writeStartEvent(ReadCycles);
60 return;
61 }
62
63 std::pair<ReadState *, int> NewPair(User, ReadAdvance);
64 Users.insert(NewPair);
65}
66
67void WriteState::cycleEvent() {
68 // Note: CyclesLeft can be a negative number. It is an error to
69 // make it an unsigned quantity because users of this write may
70 // specify a negative ReadAdvance.
71 if (CyclesLeft != UNKNOWN_CYCLES)
72 CyclesLeft--;
73}
74
75void ReadState::cycleEvent() {
Andrea Di Biagio757600b2018-06-05 17:12:02 +000076 // Update the total number of cycles.
77 if (DependentWrites && TotalCycles) {
78 --TotalCycles;
79 return;
80 }
81
82 // Bail out immediately if we don't know how many cycles are left.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000083 if (CyclesLeft == UNKNOWN_CYCLES)
84 return;
85
Andrea Di Biagio757600b2018-06-05 17:12:02 +000086 if (CyclesLeft)
87 --CyclesLeft;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000088}
89
90#ifndef NDEBUG
91void WriteState::dump() const {
92 dbgs() << "{ OpIdx=" << WD.OpIndex << ", Lat=" << WD.Latency << ", RegID "
93 << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }\n";
94}
95#endif
96
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +000097void Instruction::dispatch(unsigned RCUToken) {
Andrea Di Biagio35622482018-03-22 10:19:20 +000098 assert(Stage == IS_INVALID);
99 Stage = IS_AVAILABLE;
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000100 RCUTokenID = RCUToken;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000101
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000102 // Check if input operands are already available.
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000103 update();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000104}
105
106void Instruction::execute() {
107 assert(Stage == IS_READY);
108 Stage = IS_EXECUTING;
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000109
110 // Set the cycles left before the write-back stage.
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +0000111 CyclesLeft = Desc.MaxLatency;
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000112
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000113 for (UniqueDef &Def : Defs)
114 Def->onInstructionIssued();
Andrea Di Biagio09ea09e2018-03-22 11:39:34 +0000115
116 // Transition to the "executed" stage if this is a zero-latency instruction.
Andrea Di Biagio35622482018-03-22 10:19:20 +0000117 if (!CyclesLeft)
118 Stage = IS_EXECUTED;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000119}
120
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000121void Instruction::update() {
122 if (!isDispatched())
123 return;
124
125 if (llvm::all_of(Uses, [](const UniqueUse &Use) { return Use->isReady(); }))
126 Stage = IS_READY;
127}
128
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000129void Instruction::cycleEvent() {
Andrea Di Biagio35622482018-03-22 10:19:20 +0000130 if (isReady())
131 return;
132
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000133 if (isDispatched()) {
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000134 for (UniqueUse &Use : Uses)
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000135 Use->cycleEvent();
Andrea Di Biagio0a837ef2018-03-29 14:26:56 +0000136 update();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000137 return;
138 }
Andrea Di Biagio35622482018-03-22 10:19:20 +0000139
140 assert(isExecuting() && "Instruction not in-flight?");
141 assert(CyclesLeft && "Instruction already executed?");
142 for (UniqueDef &Def : Defs)
143 Def->cycleEvent();
144 CyclesLeft--;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000145 if (!CyclesLeft)
146 Stage = IS_EXECUTED;
147}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000148} // namespace mca