Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- 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 | // |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 10 | // This file defines abstractions used by the Pipeline to model register reads, |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 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 | |
| 19 | namespace mca { |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | void 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 | |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 35 | if (!DependentWrites) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 36 | CyclesLeft = TotalCycles; |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 37 | IsReady = !CyclesLeft; |
| 38 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void WriteState::onInstructionIssued() { |
| 42 | assert(CyclesLeft == UNKNOWN_CYCLES); |
| 43 | // Update the number of cycles left based on the WriteDescriptor info. |
Andrea Di Biagio | bb25e27 | 2018-07-06 13:46:10 +0000 | [diff] [blame] | 44 | CyclesLeft = getLatency(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 45 | |
Andrea Di Biagio | 757600b | 2018-06-05 17:12:02 +0000 | [diff] [blame] | 46 | // Now that the time left before write-back is known, notify |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 47 | // all the users. |
| 48 | for (const std::pair<ReadState *, int> &User : Users) { |
| 49 | ReadState *RS = User.first; |
| 50 | unsigned ReadCycles = std::max(0, CyclesLeft - User.second); |
| 51 | RS->writeStartEvent(ReadCycles); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void WriteState::addUser(ReadState *User, int ReadAdvance) { |
| 56 | // If CyclesLeft is different than -1, then we don't need to |
| 57 | // update the list of users. We can just notify the user with |
| 58 | // the actual number of cycles left (which may be zero). |
| 59 | if (CyclesLeft != UNKNOWN_CYCLES) { |
| 60 | unsigned ReadCycles = std::max(0, CyclesLeft - ReadAdvance); |
| 61 | User->writeStartEvent(ReadCycles); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | std::pair<ReadState *, int> NewPair(User, ReadAdvance); |
| 66 | Users.insert(NewPair); |
| 67 | } |
| 68 | |
| 69 | void WriteState::cycleEvent() { |
| 70 | // Note: CyclesLeft can be a negative number. It is an error to |
| 71 | // make it an unsigned quantity because users of this write may |
| 72 | // specify a negative ReadAdvance. |
| 73 | if (CyclesLeft != UNKNOWN_CYCLES) |
| 74 | CyclesLeft--; |
| 75 | } |
| 76 | |
| 77 | void ReadState::cycleEvent() { |
Andrea Di Biagio | 757600b | 2018-06-05 17:12:02 +0000 | [diff] [blame] | 78 | // Update the total number of cycles. |
| 79 | if (DependentWrites && TotalCycles) { |
| 80 | --TotalCycles; |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Bail out immediately if we don't know how many cycles are left. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 85 | if (CyclesLeft == UNKNOWN_CYCLES) |
| 86 | return; |
| 87 | |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 88 | if (CyclesLeft) { |
Andrea Di Biagio | 757600b | 2018-06-05 17:12:02 +0000 | [diff] [blame] | 89 | --CyclesLeft; |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 90 | IsReady = !CyclesLeft; |
| 91 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | #ifndef NDEBUG |
| 95 | void WriteState::dump() const { |
Andrea Di Biagio | bb25e27 | 2018-07-06 13:46:10 +0000 | [diff] [blame] | 96 | dbgs() << "{ OpIdx=" << WD.OpIndex << ", Lat=" << getLatency() << ", RegID " |
Andrea Di Biagio | fa2d16f | 2018-07-05 16:13:49 +0000 | [diff] [blame] | 97 | << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 98 | } |
Andrea Di Biagio | 877f9a7 | 2018-06-28 15:50:26 +0000 | [diff] [blame] | 99 | |
| 100 | void WriteRef::dump() const { |
Andrea Di Biagio | fa2d16f | 2018-07-05 16:13:49 +0000 | [diff] [blame] | 101 | dbgs() << "IID=" << getSourceIndex() << ' '; |
Andrea Di Biagio | 877f9a7 | 2018-06-28 15:50:26 +0000 | [diff] [blame] | 102 | if (isValid()) |
| 103 | getWriteState()->dump(); |
| 104 | else |
| 105 | dbgs() << "(null)"; |
| 106 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 109 | void Instruction::dispatch(unsigned RCUToken) { |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 110 | assert(Stage == IS_INVALID); |
| 111 | Stage = IS_AVAILABLE; |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 112 | RCUTokenID = RCUToken; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 113 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 114 | // Check if input operands are already available. |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 115 | update(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void Instruction::execute() { |
| 119 | assert(Stage == IS_READY); |
| 120 | Stage = IS_EXECUTING; |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 121 | |
| 122 | // Set the cycles left before the write-back stage. |
Andrea Di Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 123 | CyclesLeft = Desc.MaxLatency; |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 124 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 125 | for (UniqueDef &Def : Defs) |
| 126 | Def->onInstructionIssued(); |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 127 | |
| 128 | // Transition to the "executed" stage if this is a zero-latency instruction. |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 129 | if (!CyclesLeft) |
| 130 | Stage = IS_EXECUTED; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 133 | void Instruction::update() { |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 134 | assert(isDispatched() && "Unexpected instruction stage found!"); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 135 | if (llvm::all_of(Uses, [](const UniqueUse &Use) { return Use->isReady(); })) |
| 136 | Stage = IS_READY; |
| 137 | } |
| 138 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 139 | void Instruction::cycleEvent() { |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 140 | if (isReady()) |
| 141 | return; |
| 142 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 143 | if (isDispatched()) { |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 144 | bool IsReady = true; |
| 145 | for (UniqueUse &Use : Uses) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 146 | Use->cycleEvent(); |
Andrea Di Biagio | eb1bef6 | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 147 | IsReady &= Use->isReady(); |
| 148 | } |
| 149 | |
| 150 | if (IsReady) |
| 151 | Stage = IS_READY; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 152 | return; |
| 153 | } |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 154 | |
| 155 | assert(isExecuting() && "Instruction not in-flight?"); |
| 156 | assert(CyclesLeft && "Instruction already executed?"); |
| 157 | for (UniqueDef &Def : Defs) |
| 158 | Def->cycleEvent(); |
| 159 | CyclesLeft--; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 160 | if (!CyclesLeft) |
| 161 | Stage = IS_EXECUTED; |
| 162 | } |
Andrea Di Biagio | 877f9a7 | 2018-06-28 15:50:26 +0000 | [diff] [blame] | 163 | |
| 164 | const unsigned WriteRef::INVALID_IID = std::numeric_limits<unsigned>::max(); |
| 165 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 166 | } // namespace mca |