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 | /// |
Matt Davis | 6aa5dcd | 2018-05-01 23:04:01 +0000 | [diff] [blame] | 11 | /// This file implements methods declared by class RegisterFile and |
| 12 | /// DispatchUnit. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 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 | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 28 | void RegisterFile::initialize(const MCSchedModel &SM, unsigned NumRegs) { |
| 29 | // Create a default register file that "sees" all the machine registers |
| 30 | // declared by the target. The number of physical registers in the default |
| 31 | // register file is set equal to `NumRegs`. A value of zero for `NumRegs` |
| 32 | // means: this register file has an unbounded number of physical registers. |
| 33 | addRegisterFile({} /* all registers */, NumRegs); |
| 34 | if (!SM.hasExtraProcessorInfo()) |
| 35 | return; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 36 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 37 | // For each user defined register file, allocate a RegisterMappingTracker |
| 38 | // object. The size of every register file, as well as the mapping between |
| 39 | // register files and register classes is specified via tablegen. |
| 40 | const MCExtraProcessorInfo &Info = SM.getExtraProcessorInfo(); |
| 41 | for (unsigned I = 0, E = Info.NumRegisterFiles; I < E; ++I) { |
| 42 | const MCRegisterFileDesc &RF = Info.RegisterFiles[I]; |
| 43 | // Skip invalid register files with zero physical registers. |
| 44 | unsigned Length = RF.NumRegisterCostEntries; |
| 45 | if (!RF.NumPhysRegs) |
| 46 | continue; |
| 47 | // The cost of a register definition is equivalent to the number of |
| 48 | // physical registers that are allocated at register renaming stage. |
| 49 | const MCRegisterCostEntry *FirstElt = |
| 50 | &Info.RegisterCostTable[RF.RegisterCostEntryIdx]; |
| 51 | addRegisterFile(ArrayRef<MCRegisterCostEntry>(FirstElt, Length), |
| 52 | RF.NumPhysRegs); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void RegisterFile::addRegisterFile(ArrayRef<MCRegisterCostEntry> Entries, |
| 57 | unsigned NumPhysRegs) { |
| 58 | // A default register file is always allocated at index #0. That register file |
| 59 | // is mainly used to count the total number of mappings created by all |
| 60 | // register files at runtime. Users can limit the number of available physical |
| 61 | // registers in register file #0 through the command line flag |
| 62 | // `-register-file-size`. |
| 63 | unsigned RegisterFileIndex = RegisterFiles.size(); |
| 64 | RegisterFiles.emplace_back(NumPhysRegs); |
| 65 | |
| 66 | // Special case where there is no register class identifier in the set. |
| 67 | // An empty set of register classes means: this register file contains all |
| 68 | // the physical registers specified by the target. |
| 69 | if (Entries.empty()) { |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 70 | for (std::pair<WriteState *, IndexPlusCostPairTy> &Mapping : |
| 71 | RegisterMappings) |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 72 | Mapping.second = std::make_pair(RegisterFileIndex, 1U); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Now update the cost of individual registers. |
| 77 | for (const MCRegisterCostEntry &RCE : Entries) { |
| 78 | const MCRegisterClass &RC = MRI.getRegClass(RCE.RegisterClassID); |
| 79 | for (const MCPhysReg Reg : RC) { |
| 80 | IndexPlusCostPairTy &Entry = RegisterMappings[Reg].second; |
| 81 | if (Entry.first) { |
| 82 | // The only register file that is allowed to overlap is the default |
| 83 | // register file at index #0. The analysis is inaccurate if register |
| 84 | // files overlap. |
| 85 | errs() << "warning: register " << MRI.getName(Reg) |
| 86 | << " defined in multiple register files."; |
| 87 | } |
| 88 | Entry.first = RegisterFileIndex; |
| 89 | Entry.second = RCE.Cost; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 94 | void RegisterFile::createNewMappings(IndexPlusCostPairTy Entry, |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 95 | MutableArrayRef<unsigned> UsedPhysRegs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 96 | unsigned RegisterFileIndex = Entry.first; |
| 97 | unsigned Cost = Entry.second; |
| 98 | if (RegisterFileIndex) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 99 | RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 100 | RMT.NumUsedMappings += Cost; |
| 101 | UsedPhysRegs[RegisterFileIndex] += Cost; |
| 102 | } |
| 103 | |
| 104 | // Now update the default register mapping tracker. |
| 105 | RegisterFiles[0].NumUsedMappings += Cost; |
| 106 | UsedPhysRegs[0] += Cost; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 109 | void RegisterFile::removeMappings(IndexPlusCostPairTy Entry, |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 110 | MutableArrayRef<unsigned> FreedPhysRegs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 111 | unsigned RegisterFileIndex = Entry.first; |
| 112 | unsigned Cost = Entry.second; |
| 113 | if (RegisterFileIndex) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 114 | RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 115 | RMT.NumUsedMappings -= Cost; |
| 116 | FreedPhysRegs[RegisterFileIndex] += Cost; |
| 117 | } |
| 118 | |
| 119 | // Now update the default register mapping tracker. |
| 120 | RegisterFiles[0].NumUsedMappings -= Cost; |
| 121 | FreedPhysRegs[0] += Cost; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 124 | void RegisterFile::addRegisterMapping(WriteState &WS, |
| 125 | MutableArrayRef<unsigned> UsedPhysRegs) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 126 | unsigned RegID = WS.getRegisterID(); |
| 127 | assert(RegID && "Adding an invalid register definition?"); |
| 128 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 129 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 130 | Mapping.first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 131 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 132 | RegisterMappings[*I].first = &WS; |
| 133 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 134 | createNewMappings(Mapping.second, UsedPhysRegs); |
| 135 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 136 | // If this is a partial update, then we are done. |
| 137 | if (!WS.fullyUpdatesSuperRegs()) |
| 138 | return; |
| 139 | |
| 140 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 141 | RegisterMappings[*I].first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 144 | void RegisterFile::invalidateRegisterMapping( |
| 145 | const WriteState &WS, MutableArrayRef<unsigned> FreedPhysRegs) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 146 | unsigned RegID = WS.getRegisterID(); |
| 147 | bool ShouldInvalidateSuperRegs = WS.fullyUpdatesSuperRegs(); |
| 148 | |
| 149 | assert(RegID != 0 && "Invalidating an already invalid register?"); |
| 150 | assert(WS.getCyclesLeft() != -512 && |
| 151 | "Invalidating a write of unknown cycles!"); |
| 152 | assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 153 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 154 | if (!Mapping.first) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 157 | removeMappings(Mapping.second, FreedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 158 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 159 | if (Mapping.first == &WS) |
| 160 | Mapping.first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 161 | |
| 162 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 163 | if (RegisterMappings[*I].first == &WS) |
| 164 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 165 | |
| 166 | if (!ShouldInvalidateSuperRegs) |
| 167 | return; |
| 168 | |
| 169 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 170 | if (RegisterMappings[*I].first == &WS) |
| 171 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 174 | void RegisterFile::collectWrites(SmallVectorImpl<WriteState *> &Writes, |
| 175 | unsigned RegID) const { |
| 176 | assert(RegID && RegID < RegisterMappings.size()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 177 | WriteState *WS = RegisterMappings[RegID].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 178 | if (WS) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 179 | LLVM_DEBUG(dbgs() << "Found a dependent use of RegID=" << RegID << '\n'); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 180 | Writes.push_back(WS); |
| 181 | } |
| 182 | |
| 183 | // Handle potential partial register updates. |
| 184 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 185 | WS = RegisterMappings[*I].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 186 | if (WS && std::find(Writes.begin(), Writes.end(), WS) == Writes.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 187 | LLVM_DEBUG(dbgs() << "Found a dependent use of subReg " << *I |
| 188 | << " (part of " << RegID << ")\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 189 | Writes.push_back(WS); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 194 | unsigned RegisterFile::isAvailable(ArrayRef<unsigned> Regs) const { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 195 | SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 196 | |
| 197 | // Find how many new mappings must be created for each register file. |
| 198 | for (const unsigned RegID : Regs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 199 | const IndexPlusCostPairTy &Entry = RegisterMappings[RegID].second; |
| 200 | if (Entry.first) |
| 201 | NumPhysRegs[Entry.first] += Entry.second; |
| 202 | NumPhysRegs[0] += Entry.second; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 205 | unsigned Response = 0; |
| 206 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 207 | unsigned NumRegs = NumPhysRegs[I]; |
| 208 | if (!NumRegs) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 209 | continue; |
| 210 | |
| 211 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 212 | if (!RMT.TotalMappings) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 213 | // The register file has an unbounded number of microarchitectural |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 214 | // registers. |
| 215 | continue; |
| 216 | } |
| 217 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 218 | if (RMT.TotalMappings < NumRegs) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 219 | // The current register file is too small. This may occur if the number of |
| 220 | // microarchitectural registers in register file #0 was changed by the |
| 221 | // users via flag -reg-file-size. Alternatively, the scheduling model |
| 222 | // specified a too small number of registers for this register file. |
| 223 | report_fatal_error( |
| 224 | "Not enough microarchitectural registers in the register file"); |
| 225 | } |
| 226 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 227 | if (RMT.TotalMappings < (RMT.NumUsedMappings + NumRegs)) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 228 | Response |= (1U << I); |
| 229 | } |
| 230 | |
| 231 | return Response; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | #ifndef NDEBUG |
| 235 | void RegisterFile::dump() const { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 236 | for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) { |
| 237 | const RegisterMapping &RM = RegisterMappings[I]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 238 | dbgs() << MRI.getName(I) << ", " << I << ", Map=" << RM.second.first |
| 239 | << ", "; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 240 | if (RM.first) |
| 241 | RM.first->dump(); |
| 242 | else |
| 243 | dbgs() << "(null)\n"; |
| 244 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 245 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 246 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
| 247 | dbgs() << "Register File #" << I; |
| 248 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 249 | dbgs() << "\n TotalMappings: " << RMT.TotalMappings |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 250 | << "\n NumUsedMappings: " << RMT.NumUsedMappings << '\n'; |
| 251 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 252 | } |
| 253 | #endif |
| 254 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 255 | void DispatchUnit::notifyInstructionDispatched(const InstRef &IR, |
Andrea Di Biagio | 94fafdf | 2018-03-24 16:05:36 +0000 | [diff] [blame] | 256 | ArrayRef<unsigned> UsedRegs) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 257 | LLVM_DEBUG(dbgs() << "[E] Instruction Dispatched: " << IR << '\n'); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 258 | Owner->notifyInstructionEvent(HWInstructionDispatchedEvent(IR, UsedRegs)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 261 | void DispatchUnit::notifyInstructionRetired(const InstRef &IR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 262 | LLVM_DEBUG(dbgs() << "[E] Instruction Retired: " << IR << '\n'); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 263 | SmallVector<unsigned, 4> FreedRegs(RAT->getNumRegisterFiles()); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 264 | for (const std::unique_ptr<WriteState> &WS : IR.getInstruction()->getDefs()) |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 265 | RAT->invalidateRegisterMapping(*WS.get(), FreedRegs); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 266 | Owner->notifyInstructionEvent(HWInstructionRetiredEvent(IR, FreedRegs)); |
| 267 | Owner->eraseInstruction(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 270 | bool DispatchUnit::checkRAT(const InstRef &IR) { |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 271 | SmallVector<unsigned, 4> RegDefs; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 272 | for (const std::unique_ptr<WriteState> &RegDef : |
| 273 | IR.getInstruction()->getDefs()) |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 274 | RegDefs.emplace_back(RegDef->getRegisterID()); |
| 275 | |
| 276 | unsigned RegisterMask = RAT->isAvailable(RegDefs); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 277 | // A mask with all zeroes means: register files are available. |
| 278 | if (RegisterMask) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 279 | Owner->notifyStallEvent(HWStallEvent(HWStallEvent::RegisterFileStall, IR)); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
| 283 | return true; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 286 | bool DispatchUnit::checkRCU(const InstRef &IR) { |
| 287 | const unsigned NumMicroOps = IR.getInstruction()->getDesc().NumMicroOps; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 288 | if (RCU->isAvailable(NumMicroOps)) |
| 289 | return true; |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 290 | Owner->notifyStallEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 291 | HWStallEvent(HWStallEvent::RetireControlUnitStall, IR)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 292 | return false; |
| 293 | } |
| 294 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 295 | bool DispatchUnit::checkScheduler(const InstRef &IR) { |
| 296 | return SC->canBeDispatched(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 299 | void DispatchUnit::updateRAWDependencies(ReadState &RS, |
| 300 | const MCSubtargetInfo &STI) { |
| 301 | SmallVector<WriteState *, 4> DependentWrites; |
| 302 | |
| 303 | collectWrites(DependentWrites, RS.getRegisterID()); |
| 304 | RS.setDependentWrites(DependentWrites.size()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 305 | LLVM_DEBUG(dbgs() << "Found " << DependentWrites.size() |
| 306 | << " dependent writes\n"); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 307 | // We know that this read depends on all the writes in DependentWrites. |
| 308 | // For each write, check if we have ReadAdvance information, and use it |
| 309 | // to figure out in how many cycles this read becomes available. |
| 310 | const ReadDescriptor &RD = RS.getDescriptor(); |
| 311 | if (!RD.HasReadAdvanceEntries) { |
| 312 | for (WriteState *WS : DependentWrites) |
| 313 | WS->addUser(&RS, /* ReadAdvance */ 0); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | const MCSchedModel &SM = STI.getSchedModel(); |
| 318 | const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); |
| 319 | for (WriteState *WS : DependentWrites) { |
| 320 | unsigned WriteResID = WS->getWriteResourceID(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 321 | int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 322 | WS->addUser(&RS, ReadAdvance); |
| 323 | } |
| 324 | // Prepare the set for another round. |
| 325 | DependentWrites.clear(); |
| 326 | } |
| 327 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 328 | void DispatchUnit::dispatch(InstRef IR, const MCSubtargetInfo &STI) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 329 | assert(!CarryOver && "Cannot dispatch another instruction!"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 330 | Instruction &IS = *IR.getInstruction(); |
| 331 | const InstrDesc &Desc = IS.getDesc(); |
| 332 | const unsigned NumMicroOps = Desc.NumMicroOps; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 333 | if (NumMicroOps > DispatchWidth) { |
| 334 | assert(AvailableEntries == DispatchWidth); |
| 335 | AvailableEntries = 0; |
| 336 | CarryOver = NumMicroOps - DispatchWidth; |
| 337 | } else { |
| 338 | assert(AvailableEntries >= NumMicroOps); |
| 339 | AvailableEntries -= NumMicroOps; |
| 340 | } |
| 341 | |
Andrea Di Biagio | db66efc | 2018-04-25 09:38:58 +0000 | [diff] [blame] | 342 | // Update RAW dependencies if this instruction is not a zero-latency |
| 343 | // instruction. The assumption is that a zero-latency instruction doesn't |
| 344 | // require to be issued to the scheduler for execution. More importantly, it |
| 345 | // doesn't have to wait on the register input operands. |
Andrea Di Biagio | e047d35 | 2018-04-30 15:55:04 +0000 | [diff] [blame] | 346 | if (Desc.MaxLatency || !Desc.Resources.empty()) |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 347 | for (std::unique_ptr<ReadState> &RS : IS.getUses()) |
Andrea Di Biagio | db66efc | 2018-04-25 09:38:58 +0000 | [diff] [blame] | 348 | updateRAWDependencies(*RS, STI); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 349 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 350 | // Allocate new mappings. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 351 | SmallVector<unsigned, 4> RegisterFiles(RAT->getNumRegisterFiles()); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 352 | for (std::unique_ptr<WriteState> &WS : IS.getDefs()) |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 353 | RAT->addRegisterMapping(*WS, RegisterFiles); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 354 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 355 | // Reserve slots in the RCU, and notify the instruction that it has been |
| 356 | // dispatched to the schedulers for execution. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 357 | IS.dispatch(RCU->reserveSlot(IR, NumMicroOps)); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 358 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 359 | // Notify listeners of the "instruction dispatched" event. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 360 | notifyInstructionDispatched(IR, RegisterFiles); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 361 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 362 | // Now move the instruction into the scheduler's queue. |
| 363 | // The scheduler is responsible for checking if this is a zero-latency |
| 364 | // instruction that doesn't consume pipeline/scheduler resources. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 365 | SC->scheduleInstruction(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | #ifndef NDEBUG |
| 369 | void DispatchUnit::dump() const { |
| 370 | RAT->dump(); |
| 371 | RCU->dump(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 372 | } |
| 373 | #endif |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 374 | } // namespace mca |