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 | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 94 | void RegisterFile::allocatePhysRegs(IndexPlusCostPairTy Entry, |
| 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 | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 109 | void RegisterFile::freePhysRegs(IndexPlusCostPairTy Entry, |
| 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 | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 124 | void RegisterFile::addRegisterWrite(WriteState &WS, |
| 125 | MutableArrayRef<unsigned> UsedPhysRegs, |
| 126 | bool ShouldAllocatePhysRegs) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 127 | unsigned RegID = WS.getRegisterID(); |
| 128 | assert(RegID && "Adding an invalid register definition?"); |
| 129 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 130 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 131 | Mapping.first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 132 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 133 | RegisterMappings[*I].first = &WS; |
| 134 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 135 | // No physical registers are allocated for instructions that are optimized in |
| 136 | // hardware. For example, zero-latency data-dependency breaking instructions |
| 137 | // don't consume physical registers. |
| 138 | if (ShouldAllocatePhysRegs) |
| 139 | allocatePhysRegs(Mapping.second, UsedPhysRegs); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 140 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 141 | // If this is a partial update, then we are done. |
| 142 | if (!WS.fullyUpdatesSuperRegs()) |
| 143 | return; |
| 144 | |
| 145 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 146 | RegisterMappings[*I].first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 149 | void RegisterFile::removeRegisterWrite(const WriteState &WS, |
| 150 | MutableArrayRef<unsigned> FreedPhysRegs, |
| 151 | bool ShouldFreePhysRegs) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 152 | unsigned RegID = WS.getRegisterID(); |
| 153 | bool ShouldInvalidateSuperRegs = WS.fullyUpdatesSuperRegs(); |
| 154 | |
| 155 | assert(RegID != 0 && "Invalidating an already invalid register?"); |
| 156 | assert(WS.getCyclesLeft() != -512 && |
| 157 | "Invalidating a write of unknown cycles!"); |
| 158 | assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 159 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 160 | if (!Mapping.first) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 161 | return; |
| 162 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 163 | if (ShouldFreePhysRegs) |
| 164 | freePhysRegs(Mapping.second, FreedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 165 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 166 | if (Mapping.first == &WS) |
| 167 | Mapping.first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 168 | |
| 169 | for (MCSubRegIterator 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 | if (!ShouldInvalidateSuperRegs) |
| 174 | return; |
| 175 | |
| 176 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 177 | if (RegisterMappings[*I].first == &WS) |
| 178 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 181 | void RegisterFile::collectWrites(SmallVectorImpl<WriteState *> &Writes, |
| 182 | unsigned RegID) const { |
| 183 | assert(RegID && RegID < RegisterMappings.size()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 184 | WriteState *WS = RegisterMappings[RegID].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 185 | if (WS) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 186 | LLVM_DEBUG(dbgs() << "Found a dependent use of RegID=" << RegID << '\n'); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 187 | Writes.push_back(WS); |
| 188 | } |
| 189 | |
| 190 | // Handle potential partial register updates. |
| 191 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 192 | WS = RegisterMappings[*I].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 193 | if (WS && std::find(Writes.begin(), Writes.end(), WS) == Writes.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 194 | LLVM_DEBUG(dbgs() << "Found a dependent use of subReg " << *I |
| 195 | << " (part of " << RegID << ")\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 196 | Writes.push_back(WS); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 201 | unsigned RegisterFile::isAvailable(ArrayRef<unsigned> Regs) const { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 202 | SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 203 | |
| 204 | // Find how many new mappings must be created for each register file. |
| 205 | for (const unsigned RegID : Regs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 206 | const IndexPlusCostPairTy &Entry = RegisterMappings[RegID].second; |
| 207 | if (Entry.first) |
| 208 | NumPhysRegs[Entry.first] += Entry.second; |
| 209 | NumPhysRegs[0] += Entry.second; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 212 | unsigned Response = 0; |
| 213 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 214 | unsigned NumRegs = NumPhysRegs[I]; |
| 215 | if (!NumRegs) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 216 | continue; |
| 217 | |
| 218 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 219 | if (!RMT.TotalMappings) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 220 | // The register file has an unbounded number of microarchitectural |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 221 | // registers. |
| 222 | continue; |
| 223 | } |
| 224 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 225 | if (RMT.TotalMappings < NumRegs) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 226 | // The current register file is too small. This may occur if the number of |
| 227 | // microarchitectural registers in register file #0 was changed by the |
| 228 | // users via flag -reg-file-size. Alternatively, the scheduling model |
| 229 | // specified a too small number of registers for this register file. |
| 230 | report_fatal_error( |
| 231 | "Not enough microarchitectural registers in the register file"); |
| 232 | } |
| 233 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 234 | if (RMT.TotalMappings < (RMT.NumUsedMappings + NumRegs)) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 235 | Response |= (1U << I); |
| 236 | } |
| 237 | |
| 238 | return Response; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | #ifndef NDEBUG |
| 242 | void RegisterFile::dump() const { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 243 | for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) { |
| 244 | const RegisterMapping &RM = RegisterMappings[I]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 245 | dbgs() << MRI.getName(I) << ", " << I << ", Map=" << RM.second.first |
| 246 | << ", "; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 247 | if (RM.first) |
| 248 | RM.first->dump(); |
| 249 | else |
| 250 | dbgs() << "(null)\n"; |
| 251 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 252 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 253 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
| 254 | dbgs() << "Register File #" << I; |
| 255 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 256 | dbgs() << "\n TotalMappings: " << RMT.TotalMappings |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 257 | << "\n NumUsedMappings: " << RMT.NumUsedMappings << '\n'; |
| 258 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 259 | } |
| 260 | #endif |
| 261 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 262 | void DispatchUnit::notifyInstructionDispatched(const InstRef &IR, |
Andrea Di Biagio | 94fafdf | 2018-03-24 16:05:36 +0000 | [diff] [blame] | 263 | ArrayRef<unsigned> UsedRegs) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 264 | LLVM_DEBUG(dbgs() << "[E] Instruction Dispatched: " << IR << '\n'); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 265 | Owner->notifyInstructionEvent(HWInstructionDispatchedEvent(IR, UsedRegs)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 268 | void DispatchUnit::notifyInstructionRetired(const InstRef &IR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 269 | LLVM_DEBUG(dbgs() << "[E] Instruction Retired: " << IR << '\n'); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 270 | SmallVector<unsigned, 4> FreedRegs(RAT->getNumRegisterFiles()); |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 271 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
| 272 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 273 | for (const std::unique_ptr<WriteState> &WS : IR.getInstruction()->getDefs()) |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 274 | RAT->removeRegisterWrite(*WS.get(), FreedRegs, !Desc.isZeroLatency()); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 275 | Owner->notifyInstructionEvent(HWInstructionRetiredEvent(IR, FreedRegs)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 278 | bool DispatchUnit::checkRAT(const InstRef &IR) { |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 279 | SmallVector<unsigned, 4> RegDefs; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 280 | for (const std::unique_ptr<WriteState> &RegDef : |
| 281 | IR.getInstruction()->getDefs()) |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 282 | RegDefs.emplace_back(RegDef->getRegisterID()); |
| 283 | |
| 284 | unsigned RegisterMask = RAT->isAvailable(RegDefs); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 285 | // A mask with all zeroes means: register files are available. |
| 286 | if (RegisterMask) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 287 | Owner->notifyStallEvent(HWStallEvent(HWStallEvent::RegisterFileStall, IR)); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | return true; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 294 | bool DispatchUnit::checkRCU(const InstRef &IR) { |
| 295 | const unsigned NumMicroOps = IR.getInstruction()->getDesc().NumMicroOps; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 296 | if (RCU->isAvailable(NumMicroOps)) |
| 297 | return true; |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 298 | Owner->notifyStallEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 299 | HWStallEvent(HWStallEvent::RetireControlUnitStall, IR)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 303 | bool DispatchUnit::checkScheduler(const InstRef &IR) { |
| 304 | return SC->canBeDispatched(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 307 | void DispatchUnit::updateRAWDependencies(ReadState &RS, |
| 308 | const MCSubtargetInfo &STI) { |
| 309 | SmallVector<WriteState *, 4> DependentWrites; |
| 310 | |
| 311 | collectWrites(DependentWrites, RS.getRegisterID()); |
| 312 | RS.setDependentWrites(DependentWrites.size()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 313 | LLVM_DEBUG(dbgs() << "Found " << DependentWrites.size() |
| 314 | << " dependent writes\n"); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 315 | // We know that this read depends on all the writes in DependentWrites. |
| 316 | // For each write, check if we have ReadAdvance information, and use it |
| 317 | // to figure out in how many cycles this read becomes available. |
| 318 | const ReadDescriptor &RD = RS.getDescriptor(); |
| 319 | if (!RD.HasReadAdvanceEntries) { |
| 320 | for (WriteState *WS : DependentWrites) |
| 321 | WS->addUser(&RS, /* ReadAdvance */ 0); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | const MCSchedModel &SM = STI.getSchedModel(); |
| 326 | const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); |
| 327 | for (WriteState *WS : DependentWrites) { |
| 328 | unsigned WriteResID = WS->getWriteResourceID(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 329 | int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 330 | WS->addUser(&RS, ReadAdvance); |
| 331 | } |
| 332 | // Prepare the set for another round. |
| 333 | DependentWrites.clear(); |
| 334 | } |
| 335 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 336 | void DispatchUnit::dispatch(InstRef IR, const MCSubtargetInfo &STI) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 337 | assert(!CarryOver && "Cannot dispatch another instruction!"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 338 | Instruction &IS = *IR.getInstruction(); |
| 339 | const InstrDesc &Desc = IS.getDesc(); |
| 340 | const unsigned NumMicroOps = Desc.NumMicroOps; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 341 | if (NumMicroOps > DispatchWidth) { |
| 342 | assert(AvailableEntries == DispatchWidth); |
| 343 | AvailableEntries = 0; |
| 344 | CarryOver = NumMicroOps - DispatchWidth; |
| 345 | } else { |
| 346 | assert(AvailableEntries >= NumMicroOps); |
| 347 | AvailableEntries -= NumMicroOps; |
| 348 | } |
| 349 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 350 | // A dependency-breaking instruction doesn't have to wait on the register |
| 351 | // input operands, and it is often optimized at register renaming stage. |
| 352 | // Update RAW dependencies if this instruction is not a dependency-breaking |
| 353 | // instruction. A dependency-breaking instruction is a zero-latency |
| 354 | // instruction that doesn't consume hardware resources. |
| 355 | // An example of dependency-breaking instruction on X86 is a zero-idiom XOR. |
| 356 | if (!Desc.isZeroLatency()) |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 357 | for (std::unique_ptr<ReadState> &RS : IS.getUses()) |
Andrea Di Biagio | db66efc | 2018-04-25 09:38:58 +0000 | [diff] [blame] | 358 | updateRAWDependencies(*RS, STI); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 359 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 360 | // By default, a dependency-breaking zero-latency instruction is expected to |
| 361 | // be optimized at register renaming stage. That means, no physical register |
| 362 | // is allocated to the instruction. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 363 | SmallVector<unsigned, 4> RegisterFiles(RAT->getNumRegisterFiles()); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 364 | for (std::unique_ptr<WriteState> &WS : IS.getDefs()) |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 365 | RAT->addRegisterWrite(*WS, RegisterFiles, !Desc.isZeroLatency()); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 366 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 367 | // Reserve slots in the RCU, and notify the instruction that it has been |
| 368 | // dispatched to the schedulers for execution. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 369 | IS.dispatch(RCU->reserveSlot(IR, NumMicroOps)); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 370 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 371 | // Notify listeners of the "instruction dispatched" event. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 372 | notifyInstructionDispatched(IR, RegisterFiles); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 373 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 374 | // Now move the instruction into the scheduler's queue. |
| 375 | // The scheduler is responsible for checking if this is a zero-latency |
| 376 | // instruction that doesn't consume pipeline/scheduler resources. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 377 | SC->scheduleInstruction(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | #ifndef NDEBUG |
| 381 | void DispatchUnit::dump() const { |
| 382 | RAT->dump(); |
| 383 | RCU->dump(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 384 | } |
| 385 | #endif |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 386 | } // namespace mca |