1 //===- RegisterScavenging.cpp - Machine register scavenging ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// \file 11 /// This file implements the machine register scavenger. It can provide 12 /// information, such as unused registers, at any point in a machine basic 13 /// block. It also provides a mechanism to make registers available by evicting 14 /// them to spill slots. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/CodeGen/RegisterScavenging.h" 19 20 #include "llvm/ADT/BitVector.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/CodeGen/MachineBasicBlock.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineFunction.h" 26 #include "llvm/CodeGen/MachineInstr.h" 27 #include "llvm/CodeGen/MachineOperand.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/MC/MCRegisterInfo.h" 30 #include "llvm/PassSupport.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include "llvm/Target/TargetFrameLowering.h" 35 #include "llvm/Target/TargetInstrInfo.h" 36 #include "llvm/Target/TargetRegisterInfo.h" 37 #include "llvm/Target/TargetSubtargetInfo.h" 38 #include <cassert> 39 #include <iterator> 40 #include <limits> 41 #include <string> 42 43 using namespace llvm; 44 45 #define DEBUG_TYPE "reg-scavenging" 46 47 STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged"); 48 49 void RegScavenger::setRegUsed(unsigned Reg, LaneBitmask LaneMask) { 50 LiveUnits.addRegMasked(Reg, LaneMask); 51 } 52 53 void RegScavenger::init(MachineBasicBlock &MBB) { 54 MachineFunction &MF = *MBB.getParent(); 55 TII = MF.getSubtarget().getInstrInfo(); 56 TRI = MF.getSubtarget().getRegisterInfo(); 57 MRI = &MF.getRegInfo(); 58 LiveUnits.init(*TRI); 59 60 assert((NumRegUnits == 0 || NumRegUnits == TRI->getNumRegUnits()) && 61 "Target changed?"); 62 63 // Self-initialize. 64 if (!this->MBB) { 65 NumRegUnits = TRI->getNumRegUnits(); 66 KillRegUnits.resize(NumRegUnits); 67 DefRegUnits.resize(NumRegUnits); 68 TmpRegUnits.resize(NumRegUnits); 69 } 70 this->MBB = &MBB; 71 72 for (ScavengedInfo &SI : Scavenged) { 73 SI.Reg = 0; 74 SI.Restore = nullptr; 75 } 76 77 Tracking = false; 78 } 79 80 void RegScavenger::enterBasicBlock(MachineBasicBlock &MBB) { 81 init(MBB); 82 LiveUnits.addLiveIns(MBB); 83 } 84 85 void RegScavenger::enterBasicBlockEnd(MachineBasicBlock &MBB) { 86 init(MBB); 87 LiveUnits.addLiveOuts(MBB); 88 89 // Move internal iterator at the last instruction of the block. 90 if (MBB.begin() != MBB.end()) { 91 MBBI = std::prev(MBB.end()); 92 Tracking = true; 93 } 94 } 95 96 void RegScavenger::addRegUnits(BitVector &BV, unsigned Reg) { 97 for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) 98 BV.set(*RUI); 99 } 100 101 void RegScavenger::removeRegUnits(BitVector &BV, unsigned Reg) { 102 for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) 103 BV.reset(*RUI); 104 } 105 106 void RegScavenger::determineKillsAndDefs() { 107 assert(Tracking && "Must be tracking to determine kills and defs"); 108 109 MachineInstr &MI = *MBBI; 110 assert(!MI.isDebugValue() && "Debug values have no kills or defs"); 111 112 // Find out which registers are early clobbered, killed, defined, and marked 113 // def-dead in this instruction. 114 KillRegUnits.reset(); 115 DefRegUnits.reset(); 116 for (const MachineOperand &MO : MI.operands()) { 117 if (MO.isRegMask()) { 118 TmpRegUnits.clear(); 119 for (unsigned RU = 0, RUEnd = TRI->getNumRegUnits(); RU != RUEnd; ++RU) { 120 for (MCRegUnitRootIterator RURI(RU, TRI); RURI.isValid(); ++RURI) { 121 if (MO.clobbersPhysReg(*RURI)) { 122 TmpRegUnits.set(RU); 123 break; 124 } 125 } 126 } 127 128 // Apply the mask. 129 KillRegUnits |= TmpRegUnits; 130 } 131 if (!MO.isReg()) 132 continue; 133 unsigned Reg = MO.getReg(); 134 if (!TargetRegisterInfo::isPhysicalRegister(Reg) || isReserved(Reg)) 135 continue; 136 137 if (MO.isUse()) { 138 // Ignore undef uses. 139 if (MO.isUndef()) 140 continue; 141 if (MO.isKill()) 142 addRegUnits(KillRegUnits, Reg); 143 } else { 144 assert(MO.isDef()); 145 if (MO.isDead()) 146 addRegUnits(KillRegUnits, Reg); 147 else 148 addRegUnits(DefRegUnits, Reg); 149 } 150 } 151 } 152 153 void RegScavenger::unprocess() { 154 assert(Tracking && "Cannot unprocess because we're not tracking"); 155 156 MachineInstr &MI = *MBBI; 157 if (!MI.isDebugValue()) { 158 determineKillsAndDefs(); 159 160 // Commit the changes. 161 setUsed(KillRegUnits); 162 setUnused(DefRegUnits); 163 } 164 165 if (MBBI == MBB->begin()) { 166 MBBI = MachineBasicBlock::iterator(nullptr); 167 Tracking = false; 168 } else 169 --MBBI; 170 } 171 172 void RegScavenger::forward() { 173 // Move ptr forward. 174 if (!Tracking) { 175 MBBI = MBB->begin(); 176 Tracking = true; 177 } else { 178 assert(MBBI != MBB->end() && "Already past the end of the basic block!"); 179 MBBI = std::next(MBBI); 180 } 181 assert(MBBI != MBB->end() && "Already at the end of the basic block!"); 182 183 MachineInstr &MI = *MBBI; 184 185 for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(), 186 IE = Scavenged.end(); I != IE; ++I) { 187 if (I->Restore != &MI) 188 continue; 189 190 I->Reg = 0; 191 I->Restore = nullptr; 192 } 193 194 if (MI.isDebugValue()) 195 return; 196 197 determineKillsAndDefs(); 198 199 // Verify uses and defs. 200 #ifndef NDEBUG 201 for (const MachineOperand &MO : MI.operands()) { 202 if (!MO.isReg()) 203 continue; 204 unsigned Reg = MO.getReg(); 205 if (!TargetRegisterInfo::isPhysicalRegister(Reg) || isReserved(Reg)) 206 continue; 207 if (MO.isUse()) { 208 if (MO.isUndef()) 209 continue; 210 if (!isRegUsed(Reg)) { 211 // Check if it's partial live: e.g. 212 // D0 = insert_subreg D0<undef>, S0 213 // ... D0 214 // The problem is the insert_subreg could be eliminated. The use of 215 // D0 is using a partially undef value. This is not *incorrect* since 216 // S1 is can be freely clobbered. 217 // Ideally we would like a way to model this, but leaving the 218 // insert_subreg around causes both correctness and performance issues. 219 bool SubUsed = false; 220 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) 221 if (isRegUsed(*SubRegs)) { 222 SubUsed = true; 223 break; 224 } 225 bool SuperUsed = false; 226 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) { 227 if (isRegUsed(*SR)) { 228 SuperUsed = true; 229 break; 230 } 231 } 232 if (!SubUsed && !SuperUsed) { 233 MBB->getParent()->verify(nullptr, "In Register Scavenger"); 234 llvm_unreachable("Using an undefined register!"); 235 } 236 (void)SubUsed; 237 (void)SuperUsed; 238 } 239 } else { 240 assert(MO.isDef()); 241 #if 0 242 // FIXME: Enable this once we've figured out how to correctly transfer 243 // implicit kills during codegen passes like the coalescer. 244 assert((KillRegs.test(Reg) || isUnused(Reg) || 245 isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) && 246 "Re-defining a live register!"); 247 #endif 248 } 249 } 250 #endif // NDEBUG 251 252 // Commit the changes. 253 setUnused(KillRegUnits); 254 setUsed(DefRegUnits); 255 } 256 257 void RegScavenger::backward() { 258 assert(Tracking && "Must be tracking to determine kills and defs"); 259 260 const MachineInstr &MI = *MBBI; 261 LiveUnits.stepBackward(MI); 262 263 if (MBBI == MBB->begin()) { 264 MBBI = MachineBasicBlock::iterator(nullptr); 265 Tracking = false; 266 } else 267 --MBBI; 268 } 269 270 bool RegScavenger::isRegUsed(unsigned Reg, bool includeReserved) const { 271 if (isReserved(Reg)) 272 return includeReserved; 273 return !LiveUnits.available(Reg); 274 } 275 276 unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const { 277 for (unsigned Reg : *RC) { 278 if (!isRegUsed(Reg)) { 279 DEBUG(dbgs() << "Scavenger found unused reg: " << TRI->getName(Reg) << 280 "\n"); 281 return Reg; 282 } 283 } 284 return 0; 285 } 286 287 BitVector RegScavenger::getRegsAvailable(const TargetRegisterClass *RC) { 288 BitVector Mask(TRI->getNumRegs()); 289 for (unsigned Reg : *RC) 290 if (!isRegUsed(Reg)) 291 Mask.set(Reg); 292 return Mask; 293 } 294 295 unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI, 296 BitVector &Candidates, 297 unsigned InstrLimit, 298 MachineBasicBlock::iterator &UseMI) { 299 int Survivor = Candidates.find_first(); 300 assert(Survivor > 0 && "No candidates for scavenging"); 301 302 MachineBasicBlock::iterator ME = MBB->getFirstTerminator(); 303 assert(StartMI != ME && "MI already at terminator"); 304 MachineBasicBlock::iterator RestorePointMI = StartMI; 305 MachineBasicBlock::iterator MI = StartMI; 306 307 bool inVirtLiveRange = false; 308 for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) { 309 if (MI->isDebugValue()) { 310 ++InstrLimit; // Don't count debug instructions 311 continue; 312 } 313 bool isVirtKillInsn = false; 314 bool isVirtDefInsn = false; 315 // Remove any candidates touched by instruction. 316 for (const MachineOperand &MO : MI->operands()) { 317 if (MO.isRegMask()) 318 Candidates.clearBitsNotInMask(MO.getRegMask()); 319 if (!MO.isReg() || MO.isUndef() || !MO.getReg()) 320 continue; 321 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) { 322 if (MO.isDef()) 323 isVirtDefInsn = true; 324 else if (MO.isKill()) 325 isVirtKillInsn = true; 326 continue; 327 } 328 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI) 329 Candidates.reset(*AI); 330 } 331 // If we're not in a virtual reg's live range, this is a valid 332 // restore point. 333 if (!inVirtLiveRange) RestorePointMI = MI; 334 335 // Update whether we're in the live range of a virtual register 336 if (isVirtKillInsn) inVirtLiveRange = false; 337 if (isVirtDefInsn) inVirtLiveRange = true; 338 339 // Was our survivor untouched by this instruction? 340 if (Candidates.test(Survivor)) 341 continue; 342 343 // All candidates gone? 344 if (Candidates.none()) 345 break; 346 347 Survivor = Candidates.find_first(); 348 } 349 // If we ran off the end, that's where we want to restore. 350 if (MI == ME) RestorePointMI = ME; 351 assert(RestorePointMI != StartMI && 352 "No available scavenger restore location!"); 353 354 // We ran out of candidates, so stop the search. 355 UseMI = RestorePointMI; 356 return Survivor; 357 } 358 359 static unsigned getFrameIndexOperandNum(MachineInstr &MI) { 360 unsigned i = 0; 361 while (!MI.getOperand(i).isFI()) { 362 ++i; 363 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 364 } 365 return i; 366 } 367 368 unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC, 369 MachineBasicBlock::iterator I, 370 int SPAdj) { 371 MachineInstr &MI = *I; 372 const MachineFunction &MF = *MI.getParent()->getParent(); 373 // Consider all allocatable registers in the register class initially 374 BitVector Candidates = TRI->getAllocatableSet(MF, RC); 375 376 // Exclude all the registers being used by the instruction. 377 for (const MachineOperand &MO : MI.operands()) { 378 if (MO.isReg() && MO.getReg() != 0 && !(MO.isUse() && MO.isUndef()) && 379 !TargetRegisterInfo::isVirtualRegister(MO.getReg())) 380 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI) 381 Candidates.reset(*AI); 382 } 383 384 // Try to find a register that's unused if there is one, as then we won't 385 // have to spill. 386 BitVector Available = getRegsAvailable(RC); 387 Available &= Candidates; 388 if (Available.any()) 389 Candidates = Available; 390 391 // Find the register whose use is furthest away. 392 MachineBasicBlock::iterator UseMI; 393 unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI); 394 395 // If we found an unused register there is no reason to spill it. 396 if (!isRegUsed(SReg)) { 397 DEBUG(dbgs() << "Scavenged register: " << TRI->getName(SReg) << "\n"); 398 return SReg; 399 } 400 401 // Find an available scavenging slot with size and alignment matching 402 // the requirements of the class RC. 403 const MachineFrameInfo &MFI = MF.getFrameInfo(); 404 unsigned NeedSize = TRI->getSpillSize(*RC); 405 unsigned NeedAlign = TRI->getSpillAlignment(*RC); 406 407 unsigned SI = Scavenged.size(), Diff = std::numeric_limits<unsigned>::max(); 408 int FIB = MFI.getObjectIndexBegin(), FIE = MFI.getObjectIndexEnd(); 409 for (unsigned I = 0; I < Scavenged.size(); ++I) { 410 if (Scavenged[I].Reg != 0) 411 continue; 412 // Verify that this slot is valid for this register. 413 int FI = Scavenged[I].FrameIndex; 414 if (FI < FIB || FI >= FIE) 415 continue; 416 unsigned S = MFI.getObjectSize(FI); 417 unsigned A = MFI.getObjectAlignment(FI); 418 if (NeedSize > S || NeedAlign > A) 419 continue; 420 // Avoid wasting slots with large size and/or large alignment. Pick one 421 // that is the best fit for this register class (in street metric). 422 // Picking a larger slot than necessary could happen if a slot for a 423 // larger register is reserved before a slot for a smaller one. When 424 // trying to spill a smaller register, the large slot would be found 425 // first, thus making it impossible to spill the larger register later. 426 unsigned D = (S-NeedSize) + (A-NeedAlign); 427 if (D < Diff) { 428 SI = I; 429 Diff = D; 430 } 431 } 432 433 if (SI == Scavenged.size()) { 434 // We need to scavenge a register but have no spill slot, the target 435 // must know how to do it (if not, we'll assert below). 436 Scavenged.push_back(ScavengedInfo(FIE)); 437 } 438 439 // Avoid infinite regress 440 Scavenged[SI].Reg = SReg; 441 442 // If the target knows how to save/restore the register, let it do so; 443 // otherwise, use the emergency stack spill slot. 444 if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) { 445 // Spill the scavenged register before I. 446 int FI = Scavenged[SI].FrameIndex; 447 if (FI < FIB || FI >= FIE) { 448 std::string Msg = std::string("Error while trying to spill ") + 449 TRI->getName(SReg) + " from class " + TRI->getRegClassName(RC) + 450 ": Cannot scavenge register without an emergency spill slot!"; 451 report_fatal_error(Msg.c_str()); 452 } 453 TII->storeRegToStackSlot(*MBB, I, SReg, true, Scavenged[SI].FrameIndex, 454 RC, TRI); 455 MachineBasicBlock::iterator II = std::prev(I); 456 457 unsigned FIOperandNum = getFrameIndexOperandNum(*II); 458 TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this); 459 460 // Restore the scavenged register before its use (or first terminator). 461 TII->loadRegFromStackSlot(*MBB, UseMI, SReg, Scavenged[SI].FrameIndex, 462 RC, TRI); 463 II = std::prev(UseMI); 464 465 FIOperandNum = getFrameIndexOperandNum(*II); 466 TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this); 467 } 468 469 Scavenged[SI].Restore = &*std::prev(UseMI); 470 471 // Doing this here leads to infinite regress. 472 // Scavenged[SI].Reg = SReg; 473 474 DEBUG(dbgs() << "Scavenged register (with spill): " << TRI->getName(SReg) << 475 "\n"); 476 477 return SReg; 478 } 479 480 void llvm::scavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger &RS) { 481 // FIXME: Iterating over the instruction stream is unnecessary. We can simply 482 // iterate over the vreg use list, which at this point only contains machine 483 // operands for which eliminateFrameIndex need a new scratch reg. 484 485 // Run through the instructions and find any virtual registers. 486 MachineRegisterInfo &MRI = MF.getRegInfo(); 487 for (MachineBasicBlock &MBB : MF) { 488 RS.enterBasicBlock(MBB); 489 490 int SPAdj = 0; 491 492 // The instruction stream may change in the loop, so check MBB.end() 493 // directly. 494 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) { 495 // We might end up here again with a NULL iterator if we scavenged a 496 // register for which we inserted spill code for definition by what was 497 // originally the first instruction in MBB. 498 if (I == MachineBasicBlock::iterator(nullptr)) 499 I = MBB.begin(); 500 501 const MachineInstr &MI = *I; 502 MachineBasicBlock::iterator J = std::next(I); 503 MachineBasicBlock::iterator P = 504 I == MBB.begin() ? MachineBasicBlock::iterator(nullptr) 505 : std::prev(I); 506 507 // RS should process this instruction before we might scavenge at this 508 // location. This is because we might be replacing a virtual register 509 // defined by this instruction, and if so, registers killed by this 510 // instruction are available, and defined registers are not. 511 RS.forward(I); 512 513 for (const MachineOperand &MO : MI.operands()) { 514 if (!MO.isReg()) 515 continue; 516 unsigned Reg = MO.getReg(); 517 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 518 continue; 519 520 // When we first encounter a new virtual register, it 521 // must be a definition. 522 assert(MO.isDef() && "frame index virtual missing def!"); 523 // Scavenge a new scratch register 524 const TargetRegisterClass *RC = MRI.getRegClass(Reg); 525 unsigned ScratchReg = RS.scavengeRegister(RC, J, SPAdj); 526 527 ++NumScavengedRegs; 528 529 // Replace this reference to the virtual register with the 530 // scratch register. 531 assert(ScratchReg && "Missing scratch register!"); 532 MRI.replaceRegWith(Reg, ScratchReg); 533 534 // Because this instruction was processed by the RS before this 535 // register was allocated, make sure that the RS now records the 536 // register as being used. 537 RS.setRegUsed(ScratchReg); 538 } 539 540 // If the scavenger needed to use one of its spill slots, the 541 // spill code will have been inserted in between I and J. This is a 542 // problem because we need the spill code before I: Move I to just 543 // prior to J. 544 if (I != std::prev(J)) { 545 MBB.splice(J, &MBB, I); 546 547 // Before we move I, we need to prepare the RS to visit I again. 548 // Specifically, RS will assert if it sees uses of registers that 549 // it believes are undefined. Because we have already processed 550 // register kills in I, when it visits I again, it will believe that 551 // those registers are undefined. To avoid this situation, unprocess 552 // the instruction I. 553 assert(RS.getCurrentPosition() == I && 554 "The register scavenger has an unexpected position"); 555 I = P; 556 RS.unprocess(P); 557 } else 558 ++I; 559 } 560 } 561 562 MRI.clearVirtRegs(); 563 MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs); 564 } 565 566 namespace { 567 /// This class runs register scavenging independ of the PrologEpilogInserter. 568 /// This is used in for testing. 569 class ScavengerTest : public MachineFunctionPass { 570 public: 571 static char ID; 572 ScavengerTest() : MachineFunctionPass(ID) {} 573 bool runOnMachineFunction(MachineFunction &MF) { 574 const TargetSubtargetInfo &STI = MF.getSubtarget(); 575 const TargetFrameLowering &TFL = *STI.getFrameLowering(); 576 577 RegScavenger RS; 578 // Let's hope that calling those outside of PrologEpilogueInserter works 579 // well enough to initialize the scavenger with some emergency spillslots 580 // for the target. 581 BitVector SavedRegs; 582 TFL.determineCalleeSaves(MF, SavedRegs, &RS); 583 TFL.processFunctionBeforeFrameFinalized(MF, &RS); 584 585 // Let's scavenge the current function 586 scavengeFrameVirtualRegs(MF, RS); 587 return true; 588 } 589 }; 590 char ScavengerTest::ID; 591 592 } // end anonymous namespace 593 594 INITIALIZE_PASS(ScavengerTest, "scavenger-test", 595 "Scavenge virtual registers inside basic blocks", false, false) 596