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 | // |
| 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 | |
| 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 | |
| 35 | if (!DependentWrites) |
| 36 | CyclesLeft = TotalCycles; |
| 37 | } |
| 38 | |
| 39 | void WriteState::onInstructionIssued() { |
| 40 | assert(CyclesLeft == UNKNOWN_CYCLES); |
| 41 | // Update the number of cycles left based on the WriteDescriptor info. |
| 42 | CyclesLeft = WD.Latency; |
| 43 | |
| 44 | // Now that the time left before write-back is know, notify |
| 45 | // 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 | |
| 53 | void 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 | |
| 67 | void 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 | |
| 75 | void ReadState::cycleEvent() { |
| 76 | // If CyclesLeft is unknown, then bail out immediately. |
| 77 | if (CyclesLeft == UNKNOWN_CYCLES) |
| 78 | return; |
| 79 | |
| 80 | // If there are still dependent writes, or we reached cycle zero, |
| 81 | // then just exit. |
| 82 | if (DependentWrites || CyclesLeft == 0) |
| 83 | return; |
| 84 | |
| 85 | CyclesLeft--; |
| 86 | } |
| 87 | |
| 88 | #ifndef NDEBUG |
| 89 | void WriteState::dump() const { |
| 90 | dbgs() << "{ OpIdx=" << WD.OpIndex << ", Lat=" << WD.Latency << ", RegID " |
| 91 | << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }\n"; |
| 92 | } |
| 93 | #endif |
| 94 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 95 | void Instruction::dispatch(unsigned RCUToken) { |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 96 | assert(Stage == IS_INVALID); |
| 97 | Stage = IS_AVAILABLE; |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 98 | RCUTokenID = RCUToken; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 99 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 100 | // Check if input operands are already available. |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 101 | update(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void Instruction::execute() { |
| 105 | assert(Stage == IS_READY); |
| 106 | Stage = IS_EXECUTING; |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 107 | |
| 108 | // Set the cycles left before the write-back stage. |
Andrea Di Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 109 | CyclesLeft = Desc.MaxLatency; |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 110 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 111 | for (UniqueDef &Def : Defs) |
| 112 | Def->onInstructionIssued(); |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 113 | |
| 114 | // 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] | 115 | if (!CyclesLeft) |
| 116 | Stage = IS_EXECUTED; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 119 | void Instruction::update() { |
| 120 | if (!isDispatched()) |
| 121 | return; |
| 122 | |
| 123 | if (llvm::all_of(Uses, [](const UniqueUse &Use) { return Use->isReady(); })) |
| 124 | Stage = IS_READY; |
| 125 | } |
| 126 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 127 | void Instruction::cycleEvent() { |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 128 | if (isReady()) |
| 129 | return; |
| 130 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 131 | if (isDispatched()) { |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 132 | for (UniqueUse &Use : Uses) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 133 | Use->cycleEvent(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 134 | update(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 137 | |
| 138 | assert(isExecuting() && "Instruction not in-flight?"); |
| 139 | assert(CyclesLeft && "Instruction already executed?"); |
| 140 | for (UniqueDef &Def : Defs) |
| 141 | Def->cycleEvent(); |
| 142 | CyclesLeft--; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 143 | if (!CyclesLeft) |
| 144 | Stage = IS_EXECUTED; |
| 145 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 146 | } // namespace mca |