Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- Dispatch.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 | /// \file |
| 10 | /// |
| 11 | /// This file implements methods declared by class RegisterFile, DispatchUnit |
| 12 | /// and RetireControlUnit. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "Dispatch.h" |
| 17 | #include "Backend.h" |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 18 | #include "HWEventListener.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 19 | #include "Scheduler.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "llvm-mca" |
| 25 | |
| 26 | namespace mca { |
| 27 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 28 | void RegisterFile::addRegisterFile(ArrayRef<unsigned> RegisterClasses, |
| 29 | unsigned NumTemps) { |
| 30 | unsigned RegisterFileIndex = RegisterFiles.size(); |
| 31 | assert(RegisterFileIndex < 32 && "Too many register files!"); |
| 32 | RegisterFiles.emplace_back(NumTemps); |
| 33 | |
| 34 | // Special case where there are no register classes specified. |
| 35 | // An empty register class set means *all* registers. |
| 36 | if (RegisterClasses.empty()) { |
| 37 | for (std::pair<WriteState *, unsigned> &Mapping : RegisterMappings) |
| 38 | Mapping.second |= 1U << RegisterFileIndex; |
| 39 | } else { |
| 40 | for (const unsigned RegClassIndex : RegisterClasses) { |
| 41 | const MCRegisterClass &RC = MRI.getRegClass(RegClassIndex); |
| 42 | for (const MCPhysReg Reg : RC) |
| 43 | RegisterMappings[Reg].second |= 1U << RegisterFileIndex; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void RegisterFile::createNewMappings(unsigned RegisterFileMask) { |
| 49 | assert(RegisterFileMask && "RegisterFileMask cannot be zero!"); |
| 50 | // Notify each register file that contains RegID. |
| 51 | do { |
| 52 | unsigned NextRegisterFile = llvm::PowerOf2Floor(RegisterFileMask); |
| 53 | unsigned RegisterFileIndex = llvm::countTrailingZeros(NextRegisterFile); |
| 54 | RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; |
| 55 | RMT.NumUsedMappings++; |
| 56 | RMT.MaxUsedMappings = std::max(RMT.MaxUsedMappings, RMT.NumUsedMappings); |
| 57 | RMT.TotalMappingsCreated++; |
| 58 | RegisterFileMask ^= NextRegisterFile; |
| 59 | } while (RegisterFileMask); |
| 60 | } |
| 61 | |
| 62 | void RegisterFile::removeMappings(unsigned RegisterFileMask) { |
| 63 | assert(RegisterFileMask && "RegisterFileMask cannot be zero!"); |
| 64 | // Notify each register file that contains RegID. |
| 65 | do { |
| 66 | unsigned NextRegisterFile = llvm::PowerOf2Floor(RegisterFileMask); |
| 67 | unsigned RegisterFileIndex = llvm::countTrailingZeros(NextRegisterFile); |
| 68 | RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; |
| 69 | assert(RMT.NumUsedMappings); |
| 70 | RMT.NumUsedMappings--; |
| 71 | RegisterFileMask ^= NextRegisterFile; |
| 72 | } while (RegisterFileMask); |
| 73 | } |
| 74 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 75 | void RegisterFile::addRegisterMapping(WriteState &WS) { |
| 76 | unsigned RegID = WS.getRegisterID(); |
| 77 | assert(RegID && "Adding an invalid register definition?"); |
| 78 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 79 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 80 | Mapping.first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 81 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 82 | RegisterMappings[*I].first = &WS; |
| 83 | |
| 84 | createNewMappings(Mapping.second); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 85 | // If this is a partial update, then we are done. |
| 86 | if (!WS.fullyUpdatesSuperRegs()) |
| 87 | return; |
| 88 | |
| 89 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 90 | RegisterMappings[*I].first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void RegisterFile::invalidateRegisterMapping(const WriteState &WS) { |
| 94 | unsigned RegID = WS.getRegisterID(); |
| 95 | bool ShouldInvalidateSuperRegs = WS.fullyUpdatesSuperRegs(); |
| 96 | |
| 97 | assert(RegID != 0 && "Invalidating an already invalid register?"); |
| 98 | assert(WS.getCyclesLeft() != -512 && |
| 99 | "Invalidating a write of unknown cycles!"); |
| 100 | assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 101 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 102 | if (!Mapping.first) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 103 | return; |
| 104 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 105 | removeMappings(Mapping.second); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 106 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 107 | if (Mapping.first == &WS) |
| 108 | Mapping.first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 109 | |
| 110 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 111 | if (RegisterMappings[*I].first == &WS) |
| 112 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 113 | |
| 114 | if (!ShouldInvalidateSuperRegs) |
| 115 | return; |
| 116 | |
| 117 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 118 | if (RegisterMappings[*I].first == &WS) |
| 119 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 122 | void RegisterFile::collectWrites(SmallVectorImpl<WriteState *> &Writes, |
| 123 | unsigned RegID) const { |
| 124 | assert(RegID && RegID < RegisterMappings.size()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 125 | WriteState *WS = RegisterMappings[RegID].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 126 | if (WS) { |
| 127 | DEBUG(dbgs() << "Found a dependent use of RegID=" << RegID << '\n'); |
| 128 | Writes.push_back(WS); |
| 129 | } |
| 130 | |
| 131 | // Handle potential partial register updates. |
| 132 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 133 | WS = RegisterMappings[*I].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 134 | if (WS && std::find(Writes.begin(), Writes.end(), WS) == Writes.end()) { |
| 135 | DEBUG(dbgs() << "Found a dependent use of subReg " << *I << " (part of " |
| 136 | << RegID << ")\n"); |
| 137 | Writes.push_back(WS); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 142 | unsigned RegisterFile::isAvailable(ArrayRef<unsigned> Regs) const { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 143 | SmallVector<unsigned, 4> NumTemporaries(getNumRegisterFiles()); |
| 144 | |
| 145 | // Find how many new mappings must be created for each register file. |
| 146 | for (const unsigned RegID : Regs) { |
| 147 | unsigned RegisterFileMask = RegisterMappings[RegID].second; |
| 148 | do { |
| 149 | unsigned NextRegisterFileID = llvm::PowerOf2Floor(RegisterFileMask); |
| 150 | NumTemporaries[llvm::countTrailingZeros(NextRegisterFileID)]++; |
| 151 | RegisterFileMask ^= NextRegisterFileID; |
| 152 | } while (RegisterFileMask); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 155 | unsigned Response = 0; |
| 156 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
| 157 | unsigned Temporaries = NumTemporaries[I]; |
| 158 | if (!Temporaries) |
| 159 | continue; |
| 160 | |
| 161 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 162 | if (!RMT.TotalMappings) { |
| 163 | // The register file has an unbound number of microarchitectural |
| 164 | // registers. |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if (RMT.TotalMappings < Temporaries) { |
| 169 | // The current register file is too small. This may occur if the number of |
| 170 | // microarchitectural registers in register file #0 was changed by the |
| 171 | // users via flag -reg-file-size. Alternatively, the scheduling model |
| 172 | // specified a too small number of registers for this register file. |
| 173 | report_fatal_error( |
| 174 | "Not enough microarchitectural registers in the register file"); |
| 175 | } |
| 176 | |
| 177 | if (RMT.TotalMappings < RMT.NumUsedMappings + Temporaries) |
| 178 | Response |= (1U << I); |
| 179 | } |
| 180 | |
| 181 | return Response; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | #ifndef NDEBUG |
| 185 | void RegisterFile::dump() const { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 186 | for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) { |
| 187 | const RegisterMapping &RM = RegisterMappings[I]; |
| 188 | dbgs() << MRI.getName(I) << ", " << I << ", Map=" << RM.second << ", "; |
| 189 | if (RM.first) |
| 190 | RM.first->dump(); |
| 191 | else |
| 192 | dbgs() << "(null)\n"; |
| 193 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 194 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 195 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
| 196 | dbgs() << "Register File #" << I; |
| 197 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 198 | dbgs() << "\n TotalMappings: " << RMT.TotalMappings |
| 199 | << "\n TotalMappingsCreated: " << RMT.TotalMappingsCreated |
| 200 | << "\n MaxUsedMappings: " << RMT.MaxUsedMappings |
| 201 | << "\n NumUsedMappings: " << RMT.NumUsedMappings << '\n'; |
| 202 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 203 | } |
| 204 | #endif |
| 205 | |
| 206 | // Reserves a number of slots, and returns a new token. |
| 207 | unsigned RetireControlUnit::reserveSlot(unsigned Index, unsigned NumMicroOps) { |
| 208 | assert(isAvailable(NumMicroOps)); |
| 209 | unsigned NormalizedQuantity = |
| 210 | std::min(NumMicroOps, static_cast<unsigned>(Queue.size())); |
| 211 | // Zero latency instructions may have zero mOps. Artificially bump this |
| 212 | // value to 1. Although zero latency instructions don't consume scheduler |
| 213 | // resources, they still consume one slot in the retire queue. |
| 214 | NormalizedQuantity = std::max(NormalizedQuantity, 1U); |
| 215 | unsigned TokenID = NextAvailableSlotIdx; |
| 216 | Queue[NextAvailableSlotIdx] = {Index, NormalizedQuantity, false}; |
| 217 | NextAvailableSlotIdx += NormalizedQuantity; |
| 218 | NextAvailableSlotIdx %= Queue.size(); |
| 219 | AvailableSlots -= NormalizedQuantity; |
| 220 | return TokenID; |
| 221 | } |
| 222 | |
| 223 | void DispatchUnit::notifyInstructionDispatched(unsigned Index) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 224 | DEBUG(dbgs() << "[E] Instruction Dispatched: " << Index << '\n'); |
| 225 | Owner->notifyInstructionEvent( |
| 226 | HWInstructionEvent(HWInstructionEvent::Dispatched, Index)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void DispatchUnit::notifyInstructionRetired(unsigned Index) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 230 | DEBUG(dbgs() << "[E] Instruction Retired: " << Index << '\n'); |
| 231 | Owner->notifyInstructionEvent( |
| 232 | HWInstructionEvent(HWInstructionEvent::Retired, Index)); |
| 233 | |
| 234 | const Instruction &IS = Owner->getInstruction(Index); |
Andrea Di Biagio | 43e8f7d | 2018-03-21 12:49:07 +0000 | [diff] [blame^] | 235 | for (const std::unique_ptr<WriteState> &WS : IS.getDefs()) |
| 236 | RAT->invalidateRegisterMapping(*WS.get()); |
| 237 | |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 238 | Owner->eraseInstruction(Index); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void RetireControlUnit::cycleEvent() { |
| 242 | if (isEmpty()) |
| 243 | return; |
| 244 | |
| 245 | unsigned NumRetired = 0; |
| 246 | while (!isEmpty()) { |
| 247 | if (MaxRetirePerCycle != 0 && NumRetired == MaxRetirePerCycle) |
| 248 | break; |
| 249 | RUToken &Current = Queue[CurrentInstructionSlotIdx]; |
| 250 | assert(Current.NumSlots && "Reserved zero slots?"); |
| 251 | if (!Current.Executed) |
| 252 | break; |
| 253 | Owner->notifyInstructionRetired(Current.Index); |
| 254 | CurrentInstructionSlotIdx += Current.NumSlots; |
| 255 | CurrentInstructionSlotIdx %= Queue.size(); |
| 256 | AvailableSlots += Current.NumSlots; |
| 257 | NumRetired++; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void RetireControlUnit::onInstructionExecuted(unsigned TokenID) { |
| 262 | assert(Queue.size() > TokenID); |
| 263 | assert(Queue[TokenID].Executed == false && Queue[TokenID].Index != ~0U); |
| 264 | Queue[TokenID].Executed = true; |
| 265 | } |
| 266 | |
| 267 | #ifndef NDEBUG |
| 268 | void RetireControlUnit::dump() const { |
| 269 | dbgs() << "Retire Unit: { Total Slots=" << Queue.size() |
| 270 | << ", Available Slots=" << AvailableSlots << " }\n"; |
| 271 | } |
| 272 | #endif |
| 273 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 274 | bool DispatchUnit::checkRAT(unsigned Index, const Instruction &Instr) { |
| 275 | const InstrDesc &Desc = Instr.getDesc(); |
| 276 | unsigned NumWrites = Desc.Writes.size(); |
| 277 | unsigned RegisterMask = RAT->isAvailable(NumWrites); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 278 | // A mask with all zeroes means: register files are available. |
| 279 | if (RegisterMask) { |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 280 | Owner->notifyStallEvent( |
| 281 | HWStallEvent(HWStallEvent::RegisterFileStall, Index)); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | |
| 285 | return true; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 288 | bool DispatchUnit::checkRCU(unsigned Index, const InstrDesc &Desc) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 289 | unsigned NumMicroOps = Desc.NumMicroOps; |
| 290 | if (RCU->isAvailable(NumMicroOps)) |
| 291 | return true; |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 292 | Owner->notifyStallEvent( |
| 293 | HWStallEvent(HWStallEvent::RetireControlUnitStall, Index)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 297 | bool DispatchUnit::checkScheduler(unsigned Index, const InstrDesc &Desc) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 298 | // If this is a zero-latency instruction, then it bypasses |
| 299 | // the scheduler. |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 300 | HWStallEvent::GenericEventType Type = HWStallEvent::Invalid; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 301 | switch (SC->canBeDispatched(Desc)) { |
| 302 | case Scheduler::HWS_AVAILABLE: |
| 303 | return true; |
| 304 | case Scheduler::HWS_QUEUE_UNAVAILABLE: |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 305 | Type = HWStallEvent::SchedulerQueueFull; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 306 | break; |
| 307 | case Scheduler::HWS_LD_QUEUE_UNAVAILABLE: |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 308 | Type = HWStallEvent::LoadQueueFull; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 309 | break; |
| 310 | case Scheduler::HWS_ST_QUEUE_UNAVAILABLE: |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 311 | Type = HWStallEvent::StoreQueueFull; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 312 | break; |
| 313 | case Scheduler::HWS_DISPATCH_GROUP_RESTRICTION: |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 314 | Type = HWStallEvent::DispatchGroupStall; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 317 | Owner->notifyStallEvent(HWStallEvent(Type, Index)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 321 | void DispatchUnit::updateRAWDependencies(ReadState &RS, |
| 322 | const MCSubtargetInfo &STI) { |
| 323 | SmallVector<WriteState *, 4> DependentWrites; |
| 324 | |
| 325 | collectWrites(DependentWrites, RS.getRegisterID()); |
| 326 | RS.setDependentWrites(DependentWrites.size()); |
| 327 | DEBUG(dbgs() << "Found " << DependentWrites.size() << " dependent writes\n"); |
| 328 | // We know that this read depends on all the writes in DependentWrites. |
| 329 | // For each write, check if we have ReadAdvance information, and use it |
| 330 | // to figure out in how many cycles this read becomes available. |
| 331 | const ReadDescriptor &RD = RS.getDescriptor(); |
| 332 | if (!RD.HasReadAdvanceEntries) { |
| 333 | for (WriteState *WS : DependentWrites) |
| 334 | WS->addUser(&RS, /* ReadAdvance */ 0); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | const MCSchedModel &SM = STI.getSchedModel(); |
| 339 | const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); |
| 340 | for (WriteState *WS : DependentWrites) { |
| 341 | unsigned WriteResID = WS->getWriteResourceID(); |
| 342 | int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.OpIndex, WriteResID); |
| 343 | WS->addUser(&RS, ReadAdvance); |
| 344 | } |
| 345 | // Prepare the set for another round. |
| 346 | DependentWrites.clear(); |
| 347 | } |
| 348 | |
| 349 | unsigned DispatchUnit::dispatch(unsigned IID, Instruction *NewInst, |
| 350 | const MCSubtargetInfo &STI) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 351 | assert(!CarryOver && "Cannot dispatch another instruction!"); |
| 352 | unsigned NumMicroOps = NewInst->getDesc().NumMicroOps; |
| 353 | if (NumMicroOps > DispatchWidth) { |
| 354 | assert(AvailableEntries == DispatchWidth); |
| 355 | AvailableEntries = 0; |
| 356 | CarryOver = NumMicroOps - DispatchWidth; |
| 357 | } else { |
| 358 | assert(AvailableEntries >= NumMicroOps); |
| 359 | AvailableEntries -= NumMicroOps; |
| 360 | } |
| 361 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 362 | // Update RAW dependencies. |
| 363 | for (std::unique_ptr<ReadState> &RS : NewInst->getUses()) |
| 364 | updateRAWDependencies(*RS, STI); |
| 365 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 366 | // Allocate new mappings. |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 367 | for (std::unique_ptr<WriteState> &WS : NewInst->getDefs()) |
Andrea Di Biagio | 43e8f7d | 2018-03-21 12:49:07 +0000 | [diff] [blame^] | 368 | RAT->addRegisterMapping(*WS); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 369 | |
| 370 | // Set the cycles left before the write-back stage. |
| 371 | const InstrDesc &D = NewInst->getDesc(); |
| 372 | NewInst->setCyclesLeft(D.MaxLatency); |
| 373 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 374 | // Reserve slots in the RCU. |
| 375 | unsigned RCUTokenID = RCU->reserveSlot(IID, NumMicroOps); |
Andrea Di Biagio | 373c38a | 2018-03-08 20:21:55 +0000 | [diff] [blame] | 376 | NewInst->setRCUTokenID(RCUTokenID); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 377 | notifyInstructionDispatched(IID); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 378 | |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 379 | SC->scheduleInstruction(IID, *NewInst); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 380 | return RCUTokenID; |
| 381 | } |
| 382 | |
| 383 | #ifndef NDEBUG |
| 384 | void DispatchUnit::dump() const { |
| 385 | RAT->dump(); |
| 386 | RCU->dump(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 387 | } |
| 388 | #endif |
| 389 | |
| 390 | } // namespace mca |