1 //===--------------------- RegisterFile.cpp ---------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// 10 /// This file defines a register mapping file class. This class is responsible 11 /// for managing hardware register files and the tracking of data dependencies 12 /// between registers. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/MCA/HardwareUnits/RegisterFile.h" 17 #include "llvm/MCA/Instruction.h" 18 #include "llvm/Support/Debug.h" 19 20 #define DEBUG_TYPE "llvm-mca" 21 22 namespace llvm { 23 namespace mca { 24 25 const unsigned WriteRef::INVALID_IID = std::numeric_limits<unsigned>::max(); 26 27 WriteRef::WriteRef(unsigned SourceIndex, WriteState *WS) 28 : IID(SourceIndex), WriteBackCycle(), WriteResID(), RegisterID(), 29 Write(WS) {} 30 31 void WriteRef::commit() { 32 assert(Write && Write->isExecuted() && "Cannot commit before write back!"); 33 Write = nullptr; 34 } 35 36 void WriteRef::notifyExecuted(unsigned Cycle) { 37 assert(Write && Write->isExecuted() && "Not executed!"); 38 WriteBackCycle = Cycle; 39 } 40 41 bool WriteRef::hasKnownWriteBackCycle() const { 42 return isValid() && (!Write || Write->isExecuted()); 43 } 44 45 bool WriteRef::isWriteZero() const { 46 assert(isValid() && "Invalid null WriteState found!"); 47 return getWriteState()->isWriteZero(); 48 } 49 50 unsigned WriteRef::getWriteResourceID() const { 51 if (Write) 52 return Write->getWriteResourceID(); 53 return WriteResID; 54 } 55 56 MCPhysReg WriteRef::getRegisterID() const { 57 if (Write) 58 return Write->getRegisterID(); 59 return RegisterID; 60 } 61 62 RegisterFile::RegisterFile(const MCSchedModel &SM, const MCRegisterInfo &mri, 63 unsigned NumRegs) 64 : MRI(mri), 65 RegisterMappings(mri.getNumRegs(), {WriteRef(), RegisterRenamingInfo()}), 66 ZeroRegisters(mri.getNumRegs(), false), CurrentCycle() { 67 initialize(SM, NumRegs); 68 } 69 70 void RegisterFile::initialize(const MCSchedModel &SM, unsigned NumRegs) { 71 // Create a default register file that "sees" all the machine registers 72 // declared by the target. The number of physical registers in the default 73 // register file is set equal to `NumRegs`. A value of zero for `NumRegs` 74 // means: this register file has an unbounded number of physical registers. 75 RegisterFiles.emplace_back(NumRegs); 76 if (!SM.hasExtraProcessorInfo()) 77 return; 78 79 // For each user defined register file, allocate a RegisterMappingTracker 80 // object. The size of every register file, as well as the mapping between 81 // register files and register classes is specified via tablegen. 82 const MCExtraProcessorInfo &Info = SM.getExtraProcessorInfo(); 83 84 // Skip invalid register file at index 0. 85 for (unsigned I = 1, E = Info.NumRegisterFiles; I < E; ++I) { 86 const MCRegisterFileDesc &RF = Info.RegisterFiles[I]; 87 assert(RF.NumPhysRegs && "Invalid PRF with zero physical registers!"); 88 89 // The cost of a register definition is equivalent to the number of 90 // physical registers that are allocated at register renaming stage. 91 unsigned Length = RF.NumRegisterCostEntries; 92 const MCRegisterCostEntry *FirstElt = 93 &Info.RegisterCostTable[RF.RegisterCostEntryIdx]; 94 addRegisterFile(RF, ArrayRef<MCRegisterCostEntry>(FirstElt, Length)); 95 } 96 } 97 98 void RegisterFile::cycleStart() { 99 for (RegisterMappingTracker &RMT : RegisterFiles) 100 RMT.NumMoveEliminated = 0; 101 } 102 103 void RegisterFile::onInstructionExecuted(Instruction *IS) { 104 assert(IS && IS->isExecuted() && "Unexpected internal state found!"); 105 for (WriteState &WS : IS->getDefs()) { 106 if (WS.isEliminated()) 107 return; 108 109 MCPhysReg RegID = WS.getRegisterID(); 110 assert(RegID != 0 && "A write of an invalid register?"); 111 assert(WS.getCyclesLeft() != UNKNOWN_CYCLES && 112 "The number of cycles should be known at this point!"); 113 assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); 114 115 MCPhysReg RenameAs = RegisterMappings[RegID].second.RenameAs; 116 if (RenameAs && RenameAs != RegID) 117 RegID = RenameAs; 118 119 WriteRef &WR = RegisterMappings[RegID].first; 120 if (WR.getWriteState() == &WS) 121 WR.notifyExecuted(CurrentCycle); 122 123 for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 124 WriteRef &OtherWR = RegisterMappings[*I].first; 125 if (OtherWR.getWriteState() == &WS) 126 OtherWR.notifyExecuted(CurrentCycle); 127 } 128 129 if (!WS.clearsSuperRegisters()) 130 continue; 131 132 for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) { 133 WriteRef &OtherWR = RegisterMappings[*I].first; 134 if (OtherWR.getWriteState() == &WS) 135 OtherWR.notifyExecuted(CurrentCycle); 136 } 137 } 138 } 139 140 void RegisterFile::addRegisterFile(const MCRegisterFileDesc &RF, 141 ArrayRef<MCRegisterCostEntry> Entries) { 142 // A default register file is always allocated at index #0. That register file 143 // is mainly used to count the total number of mappings created by all 144 // register files at runtime. Users can limit the number of available physical 145 // registers in register file #0 through the command line flag 146 // `-register-file-size`. 147 unsigned RegisterFileIndex = RegisterFiles.size(); 148 RegisterFiles.emplace_back(RF.NumPhysRegs, RF.MaxMovesEliminatedPerCycle, 149 RF.AllowZeroMoveEliminationOnly); 150 151 // Special case where there is no register class identifier in the set. 152 // An empty set of register classes means: this register file contains all 153 // the physical registers specified by the target. 154 // We optimistically assume that a register can be renamed at the cost of a 155 // single physical register. The constructor of RegisterFile ensures that 156 // a RegisterMapping exists for each logical register defined by the Target. 157 if (Entries.empty()) 158 return; 159 160 // Now update the cost of individual registers. 161 for (const MCRegisterCostEntry &RCE : Entries) { 162 const MCRegisterClass &RC = MRI.getRegClass(RCE.RegisterClassID); 163 for (const MCPhysReg Reg : RC) { 164 RegisterRenamingInfo &Entry = RegisterMappings[Reg].second; 165 IndexPlusCostPairTy &IPC = Entry.IndexPlusCost; 166 if (IPC.first && IPC.first != RegisterFileIndex) { 167 // The only register file that is allowed to overlap is the default 168 // register file at index #0. The analysis is inaccurate if register 169 // files overlap. 170 errs() << "warning: register " << MRI.getName(Reg) 171 << " defined in multiple register files."; 172 } 173 IPC = std::make_pair(RegisterFileIndex, RCE.Cost); 174 Entry.RenameAs = Reg; 175 Entry.AllowMoveElimination = RCE.AllowMoveElimination; 176 177 // Assume the same cost for each sub-register. 178 for (MCSubRegIterator I(Reg, &MRI); I.isValid(); ++I) { 179 RegisterRenamingInfo &OtherEntry = RegisterMappings[*I].second; 180 if (!OtherEntry.IndexPlusCost.first && 181 (!OtherEntry.RenameAs || 182 MRI.isSuperRegister(*I, OtherEntry.RenameAs))) { 183 OtherEntry.IndexPlusCost = IPC; 184 OtherEntry.RenameAs = Reg; 185 } 186 } 187 } 188 } 189 } 190 191 void RegisterFile::allocatePhysRegs(const RegisterRenamingInfo &Entry, 192 MutableArrayRef<unsigned> UsedPhysRegs) { 193 unsigned RegisterFileIndex = Entry.IndexPlusCost.first; 194 unsigned Cost = Entry.IndexPlusCost.second; 195 if (RegisterFileIndex) { 196 RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; 197 RMT.NumUsedPhysRegs += Cost; 198 UsedPhysRegs[RegisterFileIndex] += Cost; 199 } 200 201 // Now update the default register mapping tracker. 202 RegisterFiles[0].NumUsedPhysRegs += Cost; 203 UsedPhysRegs[0] += Cost; 204 } 205 206 void RegisterFile::freePhysRegs(const RegisterRenamingInfo &Entry, 207 MutableArrayRef<unsigned> FreedPhysRegs) { 208 unsigned RegisterFileIndex = Entry.IndexPlusCost.first; 209 unsigned Cost = Entry.IndexPlusCost.second; 210 if (RegisterFileIndex) { 211 RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; 212 RMT.NumUsedPhysRegs -= Cost; 213 FreedPhysRegs[RegisterFileIndex] += Cost; 214 } 215 216 // Now update the default register mapping tracker. 217 RegisterFiles[0].NumUsedPhysRegs -= Cost; 218 FreedPhysRegs[0] += Cost; 219 } 220 221 void RegisterFile::addRegisterWrite(WriteRef Write, 222 MutableArrayRef<unsigned> UsedPhysRegs) { 223 WriteState &WS = *Write.getWriteState(); 224 MCPhysReg RegID = WS.getRegisterID(); 225 assert(RegID && "Adding an invalid register definition?"); 226 227 LLVM_DEBUG({ 228 dbgs() << "RegisterFile: addRegisterWrite [ " << Write.getSourceIndex() 229 << ", " << MRI.getName(RegID) << "]\n"; 230 }); 231 232 // If RenameAs is equal to RegID, then RegID is subject to register renaming 233 // and false dependencies on RegID are all eliminated. 234 235 // If RenameAs references the invalid register, then we optimistically assume 236 // that it can be renamed. In the absence of tablegen descriptors for register 237 // files, RenameAs is always set to the invalid register ID. In all other 238 // cases, RenameAs must be either equal to RegID, or it must reference a 239 // super-register of RegID. 240 241 // If RenameAs is a super-register of RegID, then a write to RegID has always 242 // a false dependency on RenameAs. The only exception is for when the write 243 // implicitly clears the upper portion of the underlying register. 244 // If a write clears its super-registers, then it is renamed as `RenameAs`. 245 bool IsWriteZero = WS.isWriteZero(); 246 bool IsEliminated = WS.isEliminated(); 247 bool ShouldAllocatePhysRegs = !IsWriteZero && !IsEliminated; 248 const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 249 WS.setPRF(RRI.IndexPlusCost.first); 250 251 if (RRI.RenameAs && RRI.RenameAs != RegID) { 252 RegID = RRI.RenameAs; 253 WriteRef &OtherWrite = RegisterMappings[RegID].first; 254 255 if (!WS.clearsSuperRegisters()) { 256 // The processor keeps the definition of `RegID` together with register 257 // `RenameAs`. Since this partial write is not renamed, no physical 258 // register is allocated. 259 ShouldAllocatePhysRegs = false; 260 261 WriteState *OtherWS = OtherWrite.getWriteState(); 262 if (OtherWS && (OtherWrite.getSourceIndex() != Write.getSourceIndex())) { 263 // This partial write has a false dependency on RenameAs. 264 assert(!IsEliminated && "Unexpected partial update!"); 265 OtherWS->addUser(OtherWrite.getSourceIndex(), &WS); 266 } 267 } 268 } 269 270 // Update zero registers. 271 MCPhysReg ZeroRegisterID = 272 WS.clearsSuperRegisters() ? RegID : WS.getRegisterID(); 273 ZeroRegisters.setBitVal(ZeroRegisterID, IsWriteZero); 274 for (MCSubRegIterator I(ZeroRegisterID, &MRI); I.isValid(); ++I) 275 ZeroRegisters.setBitVal(*I, IsWriteZero); 276 277 // If this is move has been eliminated, then the call to tryEliminateMove 278 // should have already updated all the register mappings. 279 if (!IsEliminated) { 280 // Update the mapping for register RegID including its sub-registers. 281 RegisterMappings[RegID].first = Write; 282 RegisterMappings[RegID].second.AliasRegID = 0U; 283 for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 284 RegisterMappings[*I].first = Write; 285 RegisterMappings[*I].second.AliasRegID = 0U; 286 } 287 288 // No physical registers are allocated for instructions that are optimized 289 // in hardware. For example, zero-latency data-dependency breaking 290 // instructions don't consume physical registers. 291 if (ShouldAllocatePhysRegs) 292 allocatePhysRegs(RegisterMappings[RegID].second, UsedPhysRegs); 293 } 294 295 if (!WS.clearsSuperRegisters()) 296 return; 297 298 for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) { 299 if (!IsEliminated) { 300 RegisterMappings[*I].first = Write; 301 RegisterMappings[*I].second.AliasRegID = 0U; 302 } 303 304 ZeroRegisters.setBitVal(*I, IsWriteZero); 305 } 306 } 307 308 void RegisterFile::removeRegisterWrite( 309 const WriteState &WS, MutableArrayRef<unsigned> FreedPhysRegs) { 310 // Early exit if this write was eliminated. A write eliminated at register 311 // renaming stage generates an alias, and it is not added to the PRF. 312 if (WS.isEliminated()) 313 return; 314 315 MCPhysReg RegID = WS.getRegisterID(); 316 317 assert(RegID != 0 && "Invalidating an already invalid register?"); 318 assert(WS.getCyclesLeft() != UNKNOWN_CYCLES && 319 "Invalidating a write of unknown cycles!"); 320 assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); 321 322 bool ShouldFreePhysRegs = !WS.isWriteZero(); 323 MCPhysReg RenameAs = RegisterMappings[RegID].second.RenameAs; 324 if (RenameAs && RenameAs != RegID) { 325 RegID = RenameAs; 326 327 if (!WS.clearsSuperRegisters()) { 328 // Keep the definition of `RegID` together with register `RenameAs`. 329 ShouldFreePhysRegs = false; 330 } 331 } 332 333 if (ShouldFreePhysRegs) 334 freePhysRegs(RegisterMappings[RegID].second, FreedPhysRegs); 335 336 WriteRef &WR = RegisterMappings[RegID].first; 337 if (WR.getWriteState() == &WS) 338 WR.commit(); 339 340 for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 341 WriteRef &OtherWR = RegisterMappings[*I].first; 342 if (OtherWR.getWriteState() == &WS) 343 OtherWR.commit(); 344 } 345 346 if (!WS.clearsSuperRegisters()) 347 return; 348 349 for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) { 350 WriteRef &OtherWR = RegisterMappings[*I].first; 351 if (OtherWR.getWriteState() == &WS) 352 OtherWR.commit(); 353 } 354 } 355 356 bool RegisterFile::tryEliminateMove(WriteState &WS, ReadState &RS) { 357 const RegisterMapping &RMFrom = RegisterMappings[RS.getRegisterID()]; 358 const RegisterMapping &RMTo = RegisterMappings[WS.getRegisterID()]; 359 360 // From and To must be owned by the same PRF. 361 const RegisterRenamingInfo &RRIFrom = RMFrom.second; 362 const RegisterRenamingInfo &RRITo = RMTo.second; 363 unsigned RegisterFileIndex = RRIFrom.IndexPlusCost.first; 364 if (RegisterFileIndex != RRITo.IndexPlusCost.first) 365 return false; 366 367 // We only allow move elimination for writes that update a full physical 368 // register. On X86, move elimination is possible with 32-bit general purpose 369 // registers because writes to those registers are not partial writes. If a 370 // register move is a partial write, then we conservatively assume that move 371 // elimination fails, since it would either trigger a partial update, or the 372 // issue of a merge opcode. 373 // 374 // Note that this constraint may be lifted in future. For example, we could 375 // make this model more flexible, and let users customize the set of registers 376 // (i.e. register classes) that allow move elimination. 377 // 378 // For now, we assume that there is a strong correlation between registers 379 // that allow move elimination, and how those same registers are renamed in 380 // hardware. 381 if (RRITo.RenameAs && RRITo.RenameAs != WS.getRegisterID()) { 382 // Early exit if the PRF doesn't support move elimination for this register. 383 if (!RegisterMappings[RRITo.RenameAs].second.AllowMoveElimination) 384 return false; 385 if (!WS.clearsSuperRegisters()) 386 return false; 387 } 388 389 RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; 390 if (RMT.MaxMoveEliminatedPerCycle && 391 RMT.NumMoveEliminated == RMT.MaxMoveEliminatedPerCycle) 392 return false; 393 394 bool IsZeroMove = ZeroRegisters[RS.getRegisterID()]; 395 if (RMT.AllowZeroMoveEliminationOnly && !IsZeroMove) 396 return false; 397 398 // Construct an alias. 399 MCPhysReg AliasedReg = 400 RRIFrom.RenameAs ? RRIFrom.RenameAs : RS.getRegisterID(); 401 MCPhysReg AliasReg = RRITo.RenameAs ? RRITo.RenameAs : WS.getRegisterID(); 402 403 const RegisterRenamingInfo &RMAlias = RegisterMappings[AliasedReg].second; 404 if (RMAlias.AliasRegID) 405 AliasedReg = RMAlias.AliasRegID; 406 407 RegisterMappings[AliasReg].second.AliasRegID = AliasedReg; 408 for (MCSubRegIterator I(AliasReg, &MRI); I.isValid(); ++I) 409 RegisterMappings[*I].second.AliasRegID = AliasedReg; 410 411 if (IsZeroMove) { 412 WS.setWriteZero(); 413 RS.setReadZero(); 414 } 415 WS.setEliminated(); 416 RMT.NumMoveEliminated++; 417 418 return true; 419 } 420 421 unsigned WriteRef::getWriteBackCycle() const { 422 assert(hasKnownWriteBackCycle() && "Instruction not executed!"); 423 assert((!Write || Write->getCyclesLeft() <= 0) && 424 "Inconsistent state found!"); 425 return WriteBackCycle; 426 } 427 428 unsigned RegisterFile::getElapsedCyclesFromWriteBack(const WriteRef &WR) const { 429 assert(WR.hasKnownWriteBackCycle() && "Write hasn't been committed yet!"); 430 return CurrentCycle - WR.getWriteBackCycle(); 431 } 432 433 void RegisterFile::collectWrites( 434 const MCSubtargetInfo &STI, const ReadState &RS, 435 SmallVectorImpl<WriteRef> &Writes, 436 SmallVectorImpl<WriteRef> &CommittedWrites) const { 437 const ReadDescriptor &RD = RS.getDescriptor(); 438 const MCSchedModel &SM = STI.getSchedModel(); 439 const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); 440 MCPhysReg RegID = RS.getRegisterID(); 441 assert(RegID && RegID < RegisterMappings.size()); 442 LLVM_DEBUG(dbgs() << "RegisterFile: collecting writes for register " 443 << MRI.getName(RegID) << '\n'); 444 445 // Check if this is an alias. 446 const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 447 if (RRI.AliasRegID) 448 RegID = RRI.AliasRegID; 449 450 const WriteRef &WR = RegisterMappings[RegID].first; 451 if (WR.getWriteState()) { 452 Writes.push_back(WR); 453 } else if (WR.hasKnownWriteBackCycle()) { 454 unsigned WriteResID = WR.getWriteResourceID(); 455 int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); 456 if (ReadAdvance < 0) { 457 unsigned Elapsed = getElapsedCyclesFromWriteBack(WR); 458 if (Elapsed < static_cast<unsigned>(-ReadAdvance)) 459 CommittedWrites.push_back(WR); 460 } 461 } 462 463 // Handle potential partial register updates. 464 for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 465 const WriteRef &WR = RegisterMappings[*I].first; 466 if (WR.getWriteState()) { 467 Writes.push_back(WR); 468 } else if (WR.hasKnownWriteBackCycle()) { 469 unsigned WriteResID = WR.getWriteResourceID(); 470 int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); 471 if (ReadAdvance < 0) { 472 unsigned Elapsed = getElapsedCyclesFromWriteBack(WR); 473 if (Elapsed < static_cast<unsigned>(-ReadAdvance)) 474 CommittedWrites.push_back(WR); 475 } 476 } 477 } 478 479 // Remove duplicate entries and resize the input vector. 480 if (Writes.size() > 1) { 481 sort(Writes, [](const WriteRef &Lhs, const WriteRef &Rhs) { 482 return Lhs.getWriteState() < Rhs.getWriteState(); 483 }); 484 auto It = std::unique(Writes.begin(), Writes.end()); 485 Writes.resize(std::distance(Writes.begin(), It)); 486 } 487 488 LLVM_DEBUG({ 489 for (const WriteRef &WR : Writes) { 490 const WriteState &WS = *WR.getWriteState(); 491 dbgs() << "[PRF] Found a dependent use of Register " 492 << MRI.getName(WS.getRegisterID()) << " (defined by instruction #" 493 << WR.getSourceIndex() << ")\n"; 494 } 495 }); 496 } 497 498 void RegisterFile::addRegisterRead(ReadState &RS, 499 const MCSubtargetInfo &STI) const { 500 MCPhysReg RegID = RS.getRegisterID(); 501 const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 502 RS.setPRF(RRI.IndexPlusCost.first); 503 if (RS.isIndependentFromDef()) 504 return; 505 506 if (ZeroRegisters[RS.getRegisterID()]) 507 RS.setReadZero(); 508 509 SmallVector<WriteRef, 4> DependentWrites; 510 SmallVector<WriteRef, 4> CompletedWrites; 511 collectWrites(STI, RS, DependentWrites, CompletedWrites); 512 RS.setDependentWrites(DependentWrites.size() + CompletedWrites.size()); 513 514 // We know that this read depends on all the writes in DependentWrites. 515 // For each write, check if we have ReadAdvance information, and use it 516 // to figure out in how many cycles this read will be available. 517 const ReadDescriptor &RD = RS.getDescriptor(); 518 const MCSchedModel &SM = STI.getSchedModel(); 519 const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); 520 for (WriteRef &WR : DependentWrites) { 521 unsigned WriteResID = WR.getWriteResourceID(); 522 WriteState &WS = *WR.getWriteState(); 523 int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); 524 WS.addUser(WR.getSourceIndex(), &RS, ReadAdvance); 525 } 526 527 for (WriteRef &WR : CompletedWrites) { 528 unsigned WriteResID = WR.getWriteResourceID(); 529 assert(WR.hasKnownWriteBackCycle() && "Invalid write!"); 530 assert(STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID) < 0); 531 unsigned ReadAdvance = static_cast<unsigned>( 532 -STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID)); 533 unsigned Elapsed = getElapsedCyclesFromWriteBack(WR); 534 assert(Elapsed < ReadAdvance && "Should not have been added to the set!"); 535 RS.writeStartEvent(WR.getSourceIndex(), WR.getRegisterID(), 536 ReadAdvance - Elapsed); 537 } 538 } 539 540 unsigned RegisterFile::isAvailable(ArrayRef<MCPhysReg> Regs) const { 541 SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles()); 542 543 // Find how many new mappings must be created for each register file. 544 for (const MCPhysReg RegID : Regs) { 545 const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 546 const IndexPlusCostPairTy &Entry = RRI.IndexPlusCost; 547 if (Entry.first) 548 NumPhysRegs[Entry.first] += Entry.second; 549 NumPhysRegs[0] += Entry.second; 550 } 551 552 unsigned Response = 0; 553 for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { 554 unsigned NumRegs = NumPhysRegs[I]; 555 if (!NumRegs) 556 continue; 557 558 const RegisterMappingTracker &RMT = RegisterFiles[I]; 559 if (!RMT.NumPhysRegs) { 560 // The register file has an unbounded number of microarchitectural 561 // registers. 562 continue; 563 } 564 565 if (RMT.NumPhysRegs < NumRegs) { 566 // The current register file is too small. This may occur if the number of 567 // microarchitectural registers in register file #0 was changed by the 568 // users via flag -reg-file-size. Alternatively, the scheduling model 569 // specified a too small number of registers for this register file. 570 LLVM_DEBUG(dbgs() << "Not enough registers in the register file.\n"); 571 572 // FIXME: Normalize the instruction register count to match the 573 // NumPhysRegs value. This is a highly unusual case, and is not expected 574 // to occur. This normalization is hiding an inconsistency in either the 575 // scheduling model or in the value that the user might have specified 576 // for NumPhysRegs. 577 NumRegs = RMT.NumPhysRegs; 578 } 579 580 if (RMT.NumPhysRegs < (RMT.NumUsedPhysRegs + NumRegs)) 581 Response |= (1U << I); 582 } 583 584 return Response; 585 } 586 587 #ifndef NDEBUG 588 void WriteRef::dump() const { 589 dbgs() << "IID=" << getSourceIndex() << ' '; 590 if (isValid()) 591 getWriteState()->dump(); 592 else 593 dbgs() << "(null)"; 594 } 595 596 void RegisterFile::dump() const { 597 for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) { 598 const RegisterMapping &RM = RegisterMappings[I]; 599 const RegisterRenamingInfo &RRI = RM.second; 600 if (ZeroRegisters[I]) { 601 dbgs() << MRI.getName(I) << ", " << I 602 << ", PRF=" << RRI.IndexPlusCost.first 603 << ", Cost=" << RRI.IndexPlusCost.second 604 << ", RenameAs=" << RRI.RenameAs << ", IsZero=" << ZeroRegisters[I] 605 << ","; 606 RM.first.dump(); 607 dbgs() << '\n'; 608 } 609 } 610 611 for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { 612 dbgs() << "Register File #" << I; 613 const RegisterMappingTracker &RMT = RegisterFiles[I]; 614 dbgs() << "\n TotalMappings: " << RMT.NumPhysRegs 615 << "\n NumUsedMappings: " << RMT.NumUsedPhysRegs << '\n'; 616 } 617 } 618 #endif 619 620 } // namespace mca 621 } // namespace llvm 622