1 //===- RegAllocFast.cpp - A fast register allocator for debug code --------===// 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 // 9 /// \file This register allocator allocates registers to a basic block at a 10 /// time, attempting to keep values in registers and reusing registers as 11 /// appropriate. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/IndexedMap.h" 18 #include "llvm/ADT/MapVector.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/SparseSet.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/MachineFunctionPass.h" 27 #include "llvm/CodeGen/MachineInstr.h" 28 #include "llvm/CodeGen/MachineInstrBuilder.h" 29 #include "llvm/CodeGen/MachineOperand.h" 30 #include "llvm/CodeGen/MachineRegisterInfo.h" 31 #include "llvm/CodeGen/RegAllocCommon.h" 32 #include "llvm/CodeGen/RegAllocRegistry.h" 33 #include "llvm/CodeGen/RegisterClassInfo.h" 34 #include "llvm/CodeGen/TargetInstrInfo.h" 35 #include "llvm/CodeGen/TargetOpcodes.h" 36 #include "llvm/CodeGen/TargetRegisterInfo.h" 37 #include "llvm/CodeGen/TargetSubtargetInfo.h" 38 #include "llvm/InitializePasses.h" 39 #include "llvm/MC/MCRegisterInfo.h" 40 #include "llvm/Pass.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/raw_ostream.h" 44 #include <cassert> 45 #include <tuple> 46 #include <vector> 47 48 using namespace llvm; 49 50 #define DEBUG_TYPE "regalloc" 51 52 STATISTIC(NumStores, "Number of stores added"); 53 STATISTIC(NumLoads , "Number of loads added"); 54 STATISTIC(NumCoalesced, "Number of copies coalesced"); 55 56 // FIXME: Remove this switch when all testcases are fixed! 57 static cl::opt<bool> IgnoreMissingDefs("rafast-ignore-missing-defs", 58 cl::Hidden); 59 60 static RegisterRegAlloc 61 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator); 62 63 namespace { 64 65 class RegAllocFast : public MachineFunctionPass { 66 public: 67 static char ID; 68 69 RegAllocFast(const RegClassFilterFunc F = allocateAllRegClasses, 70 bool ClearVirtRegs_ = true) : 71 MachineFunctionPass(ID), 72 ShouldAllocateClass(F), 73 StackSlotForVirtReg(-1), 74 ClearVirtRegs(ClearVirtRegs_) { 75 } 76 77 private: 78 MachineFrameInfo *MFI; 79 MachineRegisterInfo *MRI; 80 const TargetRegisterInfo *TRI; 81 const TargetInstrInfo *TII; 82 RegisterClassInfo RegClassInfo; 83 const RegClassFilterFunc ShouldAllocateClass; 84 85 /// Basic block currently being allocated. 86 MachineBasicBlock *MBB; 87 88 /// Maps virtual regs to the frame index where these values are spilled. 89 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg; 90 91 bool ClearVirtRegs; 92 93 /// Everything we know about a live virtual register. 94 struct LiveReg { 95 MachineInstr *LastUse = nullptr; ///< Last instr to use reg. 96 Register VirtReg; ///< Virtual register number. 97 MCPhysReg PhysReg = 0; ///< Currently held here. 98 bool LiveOut = false; ///< Register is possibly live out. 99 bool Reloaded = false; ///< Register was reloaded. 100 bool Error = false; ///< Could not allocate. 101 102 explicit LiveReg(Register VirtReg) : VirtReg(VirtReg) {} 103 104 unsigned getSparseSetIndex() const { 105 return Register::virtReg2Index(VirtReg); 106 } 107 }; 108 109 using LiveRegMap = SparseSet<LiveReg>; 110 /// This map contains entries for each virtual register that is currently 111 /// available in a physical register. 112 LiveRegMap LiveVirtRegs; 113 114 /// Stores assigned virtual registers present in the bundle MI. 115 DenseMap<Register, MCPhysReg> BundleVirtRegsMap; 116 117 DenseMap<unsigned, SmallVector<MachineOperand *, 2>> LiveDbgValueMap; 118 /// List of DBG_VALUE that we encountered without the vreg being assigned 119 /// because they were placed after the last use of the vreg. 120 DenseMap<unsigned, SmallVector<MachineInstr *, 1>> DanglingDbgValues; 121 122 /// Has a bit set for every virtual register for which it was determined 123 /// that it is alive across blocks. 124 BitVector MayLiveAcrossBlocks; 125 126 /// State of a register unit. 127 enum RegUnitState { 128 /// A free register is not currently in use and can be allocated 129 /// immediately without checking aliases. 130 regFree, 131 132 /// A pre-assigned register has been assigned before register allocation 133 /// (e.g., setting up a call parameter). 134 regPreAssigned, 135 136 /// Used temporarily in reloadAtBegin() to mark register units that are 137 /// live-in to the basic block. 138 regLiveIn, 139 140 /// A register state may also be a virtual register number, indication 141 /// that the physical register is currently allocated to a virtual 142 /// register. In that case, LiveVirtRegs contains the inverse mapping. 143 }; 144 145 /// Maps each physical register to a RegUnitState enum or virtual register. 146 std::vector<unsigned> RegUnitStates; 147 148 SmallVector<MachineInstr *, 32> Coalesced; 149 150 using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>; 151 /// Set of register units that are used in the current instruction, and so 152 /// cannot be allocated. 153 RegUnitSet UsedInInstr; 154 RegUnitSet PhysRegUses; 155 SmallVector<uint16_t, 8> DefOperandIndexes; 156 // Register masks attached to the current instruction. 157 SmallVector<const uint32_t *> RegMasks; 158 159 void setPhysRegState(MCPhysReg PhysReg, unsigned NewState); 160 bool isPhysRegFree(MCPhysReg PhysReg) const; 161 162 /// Mark a physreg as used in this instruction. 163 void markRegUsedInInstr(MCPhysReg PhysReg) { 164 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) 165 UsedInInstr.insert(*Units); 166 } 167 168 // Check if physreg is clobbered by instruction's regmask(s). 169 bool isClobberedByRegMasks(MCPhysReg PhysReg) const { 170 return llvm::any_of(RegMasks, [PhysReg](const uint32_t *Mask) { 171 return MachineOperand::clobbersPhysReg(Mask, PhysReg); 172 }); 173 } 174 175 /// Check if a physreg or any of its aliases are used in this instruction. 176 bool isRegUsedInInstr(MCPhysReg PhysReg, bool LookAtPhysRegUses) const { 177 if (LookAtPhysRegUses && isClobberedByRegMasks(PhysReg)) 178 return true; 179 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 180 if (UsedInInstr.count(*Units)) 181 return true; 182 if (LookAtPhysRegUses && PhysRegUses.count(*Units)) 183 return true; 184 } 185 return false; 186 } 187 188 /// Mark physical register as being used in a register use operand. 189 /// This is only used by the special livethrough handling code. 190 void markPhysRegUsedInInstr(MCPhysReg PhysReg) { 191 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) 192 PhysRegUses.insert(*Units); 193 } 194 195 /// Remove mark of physical register being used in the instruction. 196 void unmarkRegUsedInInstr(MCPhysReg PhysReg) { 197 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) 198 UsedInInstr.erase(*Units); 199 } 200 201 enum : unsigned { 202 spillClean = 50, 203 spillDirty = 100, 204 spillPrefBonus = 20, 205 spillImpossible = ~0u 206 }; 207 208 public: 209 StringRef getPassName() const override { return "Fast Register Allocator"; } 210 211 void getAnalysisUsage(AnalysisUsage &AU) const override { 212 AU.setPreservesCFG(); 213 MachineFunctionPass::getAnalysisUsage(AU); 214 } 215 216 MachineFunctionProperties getRequiredProperties() const override { 217 return MachineFunctionProperties().set( 218 MachineFunctionProperties::Property::NoPHIs); 219 } 220 221 MachineFunctionProperties getSetProperties() const override { 222 if (ClearVirtRegs) { 223 return MachineFunctionProperties().set( 224 MachineFunctionProperties::Property::NoVRegs); 225 } 226 227 return MachineFunctionProperties(); 228 } 229 230 MachineFunctionProperties getClearedProperties() const override { 231 return MachineFunctionProperties().set( 232 MachineFunctionProperties::Property::IsSSA); 233 } 234 235 private: 236 bool runOnMachineFunction(MachineFunction &MF) override; 237 238 void allocateBasicBlock(MachineBasicBlock &MBB); 239 240 void addRegClassDefCounts(std::vector<unsigned> &RegClassDefCounts, 241 Register Reg) const; 242 243 void allocateInstruction(MachineInstr &MI); 244 void handleDebugValue(MachineInstr &MI); 245 void handleBundle(MachineInstr &MI); 246 247 bool usePhysReg(MachineInstr &MI, MCPhysReg PhysReg); 248 bool definePhysReg(MachineInstr &MI, MCPhysReg PhysReg); 249 bool displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg); 250 void freePhysReg(MCPhysReg PhysReg); 251 252 unsigned calcSpillCost(MCPhysReg PhysReg) const; 253 254 LiveRegMap::iterator findLiveVirtReg(Register VirtReg) { 255 return LiveVirtRegs.find(Register::virtReg2Index(VirtReg)); 256 } 257 258 LiveRegMap::const_iterator findLiveVirtReg(Register VirtReg) const { 259 return LiveVirtRegs.find(Register::virtReg2Index(VirtReg)); 260 } 261 262 void assignVirtToPhysReg(MachineInstr &MI, LiveReg &, MCPhysReg PhysReg); 263 void allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint, 264 bool LookAtPhysRegUses = false); 265 void allocVirtRegUndef(MachineOperand &MO); 266 void assignDanglingDebugValues(MachineInstr &Def, Register VirtReg, 267 MCPhysReg Reg); 268 void defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum, 269 Register VirtReg); 270 void defineVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg, 271 bool LookAtPhysRegUses = false); 272 void useVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg); 273 274 MachineBasicBlock::iterator 275 getMBBBeginInsertionPoint(MachineBasicBlock &MBB, 276 SmallSet<Register, 2> &PrologLiveIns) const; 277 278 void reloadAtBegin(MachineBasicBlock &MBB); 279 void setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg); 280 281 Register traceCopies(Register VirtReg) const; 282 Register traceCopyChain(Register Reg) const; 283 284 int getStackSpaceFor(Register VirtReg); 285 void spill(MachineBasicBlock::iterator Before, Register VirtReg, 286 MCPhysReg AssignedReg, bool Kill, bool LiveOut); 287 void reload(MachineBasicBlock::iterator Before, Register VirtReg, 288 MCPhysReg PhysReg); 289 290 bool mayLiveOut(Register VirtReg); 291 bool mayLiveIn(Register VirtReg); 292 293 void dumpState() const; 294 }; 295 296 } // end anonymous namespace 297 298 char RegAllocFast::ID = 0; 299 300 INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false, 301 false) 302 303 void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) { 304 for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) 305 RegUnitStates[*UI] = NewState; 306 } 307 308 bool RegAllocFast::isPhysRegFree(MCPhysReg PhysReg) const { 309 for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) { 310 if (RegUnitStates[*UI] != regFree) 311 return false; 312 } 313 return true; 314 } 315 316 /// This allocates space for the specified virtual register to be held on the 317 /// stack. 318 int RegAllocFast::getStackSpaceFor(Register VirtReg) { 319 // Find the location Reg would belong... 320 int SS = StackSlotForVirtReg[VirtReg]; 321 // Already has space allocated? 322 if (SS != -1) 323 return SS; 324 325 // Allocate a new stack object for this spill location... 326 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); 327 unsigned Size = TRI->getSpillSize(RC); 328 Align Alignment = TRI->getSpillAlign(RC); 329 int FrameIdx = MFI->CreateSpillStackObject(Size, Alignment); 330 331 // Assign the slot. 332 StackSlotForVirtReg[VirtReg] = FrameIdx; 333 return FrameIdx; 334 } 335 336 static bool dominates(MachineBasicBlock &MBB, 337 MachineBasicBlock::const_iterator A, 338 MachineBasicBlock::const_iterator B) { 339 auto MBBEnd = MBB.end(); 340 if (B == MBBEnd) 341 return true; 342 343 MachineBasicBlock::const_iterator I = MBB.begin(); 344 for (; &*I != A && &*I != B; ++I) 345 ; 346 347 return &*I == A; 348 } 349 350 /// Returns false if \p VirtReg is known to not live out of the current block. 351 bool RegAllocFast::mayLiveOut(Register VirtReg) { 352 if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) { 353 // Cannot be live-out if there are no successors. 354 return !MBB->succ_empty(); 355 } 356 357 const MachineInstr *SelfLoopDef = nullptr; 358 359 // If this block loops back to itself, it is necessary to check whether the 360 // use comes after the def. 361 if (MBB->isSuccessor(MBB)) { 362 SelfLoopDef = MRI->getUniqueVRegDef(VirtReg); 363 if (!SelfLoopDef) { 364 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg)); 365 return true; 366 } 367 } 368 369 // See if the first \p Limit uses of the register are all in the current 370 // block. 371 static const unsigned Limit = 8; 372 unsigned C = 0; 373 for (const MachineInstr &UseInst : MRI->use_nodbg_instructions(VirtReg)) { 374 if (UseInst.getParent() != MBB || ++C >= Limit) { 375 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg)); 376 // Cannot be live-out if there are no successors. 377 return !MBB->succ_empty(); 378 } 379 380 if (SelfLoopDef) { 381 // Try to handle some simple cases to avoid spilling and reloading every 382 // value inside a self looping block. 383 if (SelfLoopDef == &UseInst || 384 !dominates(*MBB, SelfLoopDef->getIterator(), UseInst.getIterator())) { 385 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg)); 386 return true; 387 } 388 } 389 } 390 391 return false; 392 } 393 394 /// Returns false if \p VirtReg is known to not be live into the current block. 395 bool RegAllocFast::mayLiveIn(Register VirtReg) { 396 if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) 397 return !MBB->pred_empty(); 398 399 // See if the first \p Limit def of the register are all in the current block. 400 static const unsigned Limit = 8; 401 unsigned C = 0; 402 for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) { 403 if (DefInst.getParent() != MBB || ++C >= Limit) { 404 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg)); 405 return !MBB->pred_empty(); 406 } 407 } 408 409 return false; 410 } 411 412 /// Insert spill instruction for \p AssignedReg before \p Before. Update 413 /// DBG_VALUEs with \p VirtReg operands with the stack slot. 414 void RegAllocFast::spill(MachineBasicBlock::iterator Before, Register VirtReg, 415 MCPhysReg AssignedReg, bool Kill, bool LiveOut) { 416 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI) 417 << " in " << printReg(AssignedReg, TRI)); 418 int FI = getStackSpaceFor(VirtReg); 419 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n'); 420 421 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); 422 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI); 423 ++NumStores; 424 425 MachineBasicBlock::iterator FirstTerm = MBB->getFirstTerminator(); 426 427 // When we spill a virtual register, we will have spill instructions behind 428 // every definition of it, meaning we can switch all the DBG_VALUEs over 429 // to just reference the stack slot. 430 SmallVectorImpl<MachineOperand *> &LRIDbgOperands = LiveDbgValueMap[VirtReg]; 431 SmallMapVector<MachineInstr *, SmallVector<const MachineOperand *>, 2> 432 SpilledOperandsMap; 433 for (MachineOperand *MO : LRIDbgOperands) 434 SpilledOperandsMap[MO->getParent()].push_back(MO); 435 for (auto MISpilledOperands : SpilledOperandsMap) { 436 MachineInstr &DBG = *MISpilledOperands.first; 437 MachineInstr *NewDV = buildDbgValueForSpill( 438 *MBB, Before, *MISpilledOperands.first, FI, MISpilledOperands.second); 439 assert(NewDV->getParent() == MBB && "dangling parent pointer"); 440 (void)NewDV; 441 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV); 442 443 if (LiveOut) { 444 // We need to insert a DBG_VALUE at the end of the block if the spill slot 445 // is live out, but there is another use of the value after the 446 // spill. This will allow LiveDebugValues to see the correct live out 447 // value to propagate to the successors. 448 MachineInstr *ClonedDV = MBB->getParent()->CloneMachineInstr(NewDV); 449 MBB->insert(FirstTerm, ClonedDV); 450 LLVM_DEBUG(dbgs() << "Cloning debug info due to live out spill\n"); 451 } 452 453 // Rewrite unassigned dbg_values to use the stack slot. 454 // TODO We can potentially do this for list debug values as well if we know 455 // how the dbg_values are getting unassigned. 456 if (DBG.isNonListDebugValue()) { 457 MachineOperand &MO = DBG.getDebugOperand(0); 458 if (MO.isReg() && MO.getReg() == 0) { 459 updateDbgValueForSpill(DBG, FI, 0); 460 } 461 } 462 } 463 // Now this register is spilled there is should not be any DBG_VALUE 464 // pointing to this register because they are all pointing to spilled value 465 // now. 466 LRIDbgOperands.clear(); 467 } 468 469 /// Insert reload instruction for \p PhysReg before \p Before. 470 void RegAllocFast::reload(MachineBasicBlock::iterator Before, Register VirtReg, 471 MCPhysReg PhysReg) { 472 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into " 473 << printReg(PhysReg, TRI) << '\n'); 474 int FI = getStackSpaceFor(VirtReg); 475 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); 476 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI); 477 ++NumLoads; 478 } 479 480 /// Get basic block begin insertion point. 481 /// This is not just MBB.begin() because surprisingly we have EH_LABEL 482 /// instructions marking the begin of a basic block. This means we must insert 483 /// new instructions after such labels... 484 MachineBasicBlock::iterator 485 RegAllocFast::getMBBBeginInsertionPoint( 486 MachineBasicBlock &MBB, SmallSet<Register, 2> &PrologLiveIns) const { 487 MachineBasicBlock::iterator I = MBB.begin(); 488 while (I != MBB.end()) { 489 if (I->isLabel()) { 490 ++I; 491 continue; 492 } 493 494 // Most reloads should be inserted after prolog instructions. 495 if (!TII->isBasicBlockPrologue(*I)) 496 break; 497 498 // However if a prolog instruction reads a register that needs to be 499 // reloaded, the reload should be inserted before the prolog. 500 for (MachineOperand &MO : I->operands()) { 501 if (MO.isReg()) 502 PrologLiveIns.insert(MO.getReg()); 503 } 504 505 ++I; 506 } 507 508 return I; 509 } 510 511 /// Reload all currently assigned virtual registers. 512 void RegAllocFast::reloadAtBegin(MachineBasicBlock &MBB) { 513 if (LiveVirtRegs.empty()) 514 return; 515 516 for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) { 517 MCPhysReg Reg = P.PhysReg; 518 // Set state to live-in. This possibly overrides mappings to virtual 519 // registers but we don't care anymore at this point. 520 setPhysRegState(Reg, regLiveIn); 521 } 522 523 524 SmallSet<Register, 2> PrologLiveIns; 525 526 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order 527 // of spilling here is deterministic, if arbitrary. 528 MachineBasicBlock::iterator InsertBefore 529 = getMBBBeginInsertionPoint(MBB, PrologLiveIns); 530 for (const LiveReg &LR : LiveVirtRegs) { 531 MCPhysReg PhysReg = LR.PhysReg; 532 if (PhysReg == 0) 533 continue; 534 535 MCRegister FirstUnit = *MCRegUnitIterator(PhysReg, TRI); 536 if (RegUnitStates[FirstUnit] == regLiveIn) 537 continue; 538 539 assert((&MBB != &MBB.getParent()->front() || IgnoreMissingDefs) && 540 "no reload in start block. Missing vreg def?"); 541 542 if (PrologLiveIns.count(PhysReg)) { 543 // FIXME: Theoretically this should use an insert point skipping labels 544 // but I'm not sure how labels should interact with prolog instruction 545 // that need reloads. 546 reload(MBB.begin(), LR.VirtReg, PhysReg); 547 } else 548 reload(InsertBefore, LR.VirtReg, PhysReg); 549 } 550 LiveVirtRegs.clear(); 551 } 552 553 /// Handle the direct use of a physical register. Check that the register is 554 /// not used by a virtreg. Kill the physreg, marking it free. This may add 555 /// implicit kills to MO->getParent() and invalidate MO. 556 bool RegAllocFast::usePhysReg(MachineInstr &MI, MCPhysReg Reg) { 557 assert(Register::isPhysicalRegister(Reg) && "expected physreg"); 558 bool displacedAny = displacePhysReg(MI, Reg); 559 setPhysRegState(Reg, regPreAssigned); 560 markRegUsedInInstr(Reg); 561 return displacedAny; 562 } 563 564 bool RegAllocFast::definePhysReg(MachineInstr &MI, MCPhysReg Reg) { 565 bool displacedAny = displacePhysReg(MI, Reg); 566 setPhysRegState(Reg, regPreAssigned); 567 return displacedAny; 568 } 569 570 /// Mark PhysReg as reserved or free after spilling any virtregs. This is very 571 /// similar to defineVirtReg except the physreg is reserved instead of 572 /// allocated. 573 bool RegAllocFast::displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg) { 574 bool displacedAny = false; 575 576 for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) { 577 unsigned Unit = *UI; 578 switch (unsigned VirtReg = RegUnitStates[Unit]) { 579 default: { 580 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); 581 assert(LRI != LiveVirtRegs.end() && "datastructures in sync"); 582 MachineBasicBlock::iterator ReloadBefore = 583 std::next((MachineBasicBlock::iterator)MI.getIterator()); 584 reload(ReloadBefore, VirtReg, LRI->PhysReg); 585 586 setPhysRegState(LRI->PhysReg, regFree); 587 LRI->PhysReg = 0; 588 LRI->Reloaded = true; 589 displacedAny = true; 590 break; 591 } 592 case regPreAssigned: 593 RegUnitStates[Unit] = regFree; 594 displacedAny = true; 595 break; 596 case regFree: 597 break; 598 } 599 } 600 return displacedAny; 601 } 602 603 void RegAllocFast::freePhysReg(MCPhysReg PhysReg) { 604 LLVM_DEBUG(dbgs() << "Freeing " << printReg(PhysReg, TRI) << ':'); 605 606 MCRegister FirstUnit = *MCRegUnitIterator(PhysReg, TRI); 607 switch (unsigned VirtReg = RegUnitStates[FirstUnit]) { 608 case regFree: 609 LLVM_DEBUG(dbgs() << '\n'); 610 return; 611 case regPreAssigned: 612 LLVM_DEBUG(dbgs() << '\n'); 613 setPhysRegState(PhysReg, regFree); 614 return; 615 default: { 616 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); 617 assert(LRI != LiveVirtRegs.end()); 618 LLVM_DEBUG(dbgs() << ' ' << printReg(LRI->VirtReg, TRI) << '\n'); 619 setPhysRegState(LRI->PhysReg, regFree); 620 LRI->PhysReg = 0; 621 } 622 return; 623 } 624 } 625 626 /// Return the cost of spilling clearing out PhysReg and aliases so it is free 627 /// for allocation. Returns 0 when PhysReg is free or disabled with all aliases 628 /// disabled - it can be allocated directly. 629 /// \returns spillImpossible when PhysReg or an alias can't be spilled. 630 unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const { 631 for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) { 632 switch (unsigned VirtReg = RegUnitStates[*UI]) { 633 case regFree: 634 break; 635 case regPreAssigned: 636 LLVM_DEBUG(dbgs() << "Cannot spill pre-assigned " 637 << printReg(PhysReg, TRI) << '\n'); 638 return spillImpossible; 639 default: { 640 bool SureSpill = StackSlotForVirtReg[VirtReg] != -1 || 641 findLiveVirtReg(VirtReg)->LiveOut; 642 return SureSpill ? spillClean : spillDirty; 643 } 644 } 645 } 646 return 0; 647 } 648 649 void RegAllocFast::assignDanglingDebugValues(MachineInstr &Definition, 650 Register VirtReg, MCPhysReg Reg) { 651 auto UDBGValIter = DanglingDbgValues.find(VirtReg); 652 if (UDBGValIter == DanglingDbgValues.end()) 653 return; 654 655 SmallVectorImpl<MachineInstr*> &Dangling = UDBGValIter->second; 656 for (MachineInstr *DbgValue : Dangling) { 657 assert(DbgValue->isDebugValue()); 658 if (!DbgValue->hasDebugOperandForReg(VirtReg)) 659 continue; 660 661 // Test whether the physreg survives from the definition to the DBG_VALUE. 662 MCPhysReg SetToReg = Reg; 663 unsigned Limit = 20; 664 for (MachineBasicBlock::iterator I = std::next(Definition.getIterator()), 665 E = DbgValue->getIterator(); I != E; ++I) { 666 if (I->modifiesRegister(Reg, TRI) || --Limit == 0) { 667 LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue 668 << '\n'); 669 SetToReg = 0; 670 break; 671 } 672 } 673 for (MachineOperand &MO : DbgValue->getDebugOperandsForReg(VirtReg)) { 674 MO.setReg(SetToReg); 675 if (SetToReg != 0) 676 MO.setIsRenamable(); 677 } 678 } 679 Dangling.clear(); 680 } 681 682 /// This method updates local state so that we know that PhysReg is the 683 /// proper container for VirtReg now. The physical register must not be used 684 /// for anything else when this is called. 685 void RegAllocFast::assignVirtToPhysReg(MachineInstr &AtMI, LiveReg &LR, 686 MCPhysReg PhysReg) { 687 Register VirtReg = LR.VirtReg; 688 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to " 689 << printReg(PhysReg, TRI) << '\n'); 690 assert(LR.PhysReg == 0 && "Already assigned a physreg"); 691 assert(PhysReg != 0 && "Trying to assign no register"); 692 LR.PhysReg = PhysReg; 693 setPhysRegState(PhysReg, VirtReg); 694 695 assignDanglingDebugValues(AtMI, VirtReg, PhysReg); 696 } 697 698 static bool isCoalescable(const MachineInstr &MI) { 699 return MI.isFullCopy(); 700 } 701 702 Register RegAllocFast::traceCopyChain(Register Reg) const { 703 static const unsigned ChainLengthLimit = 3; 704 unsigned C = 0; 705 do { 706 if (Reg.isPhysical()) 707 return Reg; 708 assert(Reg.isVirtual()); 709 710 MachineInstr *VRegDef = MRI->getUniqueVRegDef(Reg); 711 if (!VRegDef || !isCoalescable(*VRegDef)) 712 return 0; 713 Reg = VRegDef->getOperand(1).getReg(); 714 } while (++C <= ChainLengthLimit); 715 return 0; 716 } 717 718 /// Check if any of \p VirtReg's definitions is a copy. If it is follow the 719 /// chain of copies to check whether we reach a physical register we can 720 /// coalesce with. 721 Register RegAllocFast::traceCopies(Register VirtReg) const { 722 static const unsigned DefLimit = 3; 723 unsigned C = 0; 724 for (const MachineInstr &MI : MRI->def_instructions(VirtReg)) { 725 if (isCoalescable(MI)) { 726 Register Reg = MI.getOperand(1).getReg(); 727 Reg = traceCopyChain(Reg); 728 if (Reg.isValid()) 729 return Reg; 730 } 731 732 if (++C >= DefLimit) 733 break; 734 } 735 return Register(); 736 } 737 738 /// Allocates a physical register for VirtReg. 739 void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, 740 Register Hint0, bool LookAtPhysRegUses) { 741 const Register VirtReg = LR.VirtReg; 742 assert(LR.PhysReg == 0); 743 744 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); 745 LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg) 746 << " in class " << TRI->getRegClassName(&RC) 747 << " with hint " << printReg(Hint0, TRI) << '\n'); 748 749 // Take hint when possible. 750 if (Hint0.isPhysical() && MRI->isAllocatable(Hint0) && RC.contains(Hint0) && 751 !isRegUsedInInstr(Hint0, LookAtPhysRegUses)) { 752 // Take hint if the register is currently free. 753 if (isPhysRegFree(Hint0)) { 754 LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI) 755 << '\n'); 756 assignVirtToPhysReg(MI, LR, Hint0); 757 return; 758 } else { 759 LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint0, TRI) 760 << " occupied\n"); 761 } 762 } else { 763 Hint0 = Register(); 764 } 765 766 767 // Try other hint. 768 Register Hint1 = traceCopies(VirtReg); 769 if (Hint1.isPhysical() && MRI->isAllocatable(Hint1) && RC.contains(Hint1) && 770 !isRegUsedInInstr(Hint1, LookAtPhysRegUses)) { 771 // Take hint if the register is currently free. 772 if (isPhysRegFree(Hint1)) { 773 LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI) 774 << '\n'); 775 assignVirtToPhysReg(MI, LR, Hint1); 776 return; 777 } else { 778 LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint1, TRI) 779 << " occupied\n"); 780 } 781 } else { 782 Hint1 = Register(); 783 } 784 785 MCPhysReg BestReg = 0; 786 unsigned BestCost = spillImpossible; 787 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC); 788 for (MCPhysReg PhysReg : AllocationOrder) { 789 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' '); 790 if (isRegUsedInInstr(PhysReg, LookAtPhysRegUses)) { 791 LLVM_DEBUG(dbgs() << "already used in instr.\n"); 792 continue; 793 } 794 795 unsigned Cost = calcSpillCost(PhysReg); 796 LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n'); 797 // Immediate take a register with cost 0. 798 if (Cost == 0) { 799 assignVirtToPhysReg(MI, LR, PhysReg); 800 return; 801 } 802 803 if (PhysReg == Hint0 || PhysReg == Hint1) 804 Cost -= spillPrefBonus; 805 806 if (Cost < BestCost) { 807 BestReg = PhysReg; 808 BestCost = Cost; 809 } 810 } 811 812 if (!BestReg) { 813 // Nothing we can do: Report an error and keep going with an invalid 814 // allocation. 815 if (MI.isInlineAsm()) 816 MI.emitError("inline assembly requires more registers than available"); 817 else 818 MI.emitError("ran out of registers during register allocation"); 819 820 LR.Error = true; 821 LR.PhysReg = 0; 822 return; 823 } 824 825 displacePhysReg(MI, BestReg); 826 assignVirtToPhysReg(MI, LR, BestReg); 827 } 828 829 void RegAllocFast::allocVirtRegUndef(MachineOperand &MO) { 830 assert(MO.isUndef() && "expected undef use"); 831 Register VirtReg = MO.getReg(); 832 assert(Register::isVirtualRegister(VirtReg) && "Expected virtreg"); 833 834 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg); 835 MCPhysReg PhysReg; 836 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) { 837 PhysReg = LRI->PhysReg; 838 } else { 839 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); 840 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC); 841 assert(!AllocationOrder.empty() && "Allocation order must not be empty"); 842 PhysReg = AllocationOrder[0]; 843 } 844 845 unsigned SubRegIdx = MO.getSubReg(); 846 if (SubRegIdx != 0) { 847 PhysReg = TRI->getSubReg(PhysReg, SubRegIdx); 848 MO.setSubReg(0); 849 } 850 MO.setReg(PhysReg); 851 MO.setIsRenamable(true); 852 } 853 854 /// Variation of defineVirtReg() with special handling for livethrough regs 855 /// (tied or earlyclobber) that may interfere with preassigned uses. 856 void RegAllocFast::defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum, 857 Register VirtReg) { 858 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); 859 if (LRI != LiveVirtRegs.end()) { 860 MCPhysReg PrevReg = LRI->PhysReg; 861 if (PrevReg != 0 && isRegUsedInInstr(PrevReg, true)) { 862 LLVM_DEBUG(dbgs() << "Need new assignment for " << printReg(PrevReg, TRI) 863 << " (tied/earlyclobber resolution)\n"); 864 freePhysReg(PrevReg); 865 LRI->PhysReg = 0; 866 allocVirtReg(MI, *LRI, 0, true); 867 MachineBasicBlock::iterator InsertBefore = 868 std::next((MachineBasicBlock::iterator)MI.getIterator()); 869 LLVM_DEBUG(dbgs() << "Copy " << printReg(LRI->PhysReg, TRI) << " to " 870 << printReg(PrevReg, TRI) << '\n'); 871 BuildMI(*MBB, InsertBefore, MI.getDebugLoc(), 872 TII->get(TargetOpcode::COPY), PrevReg) 873 .addReg(LRI->PhysReg, llvm::RegState::Kill); 874 } 875 MachineOperand &MO = MI.getOperand(OpNum); 876 if (MO.getSubReg() && !MO.isUndef()) { 877 LRI->LastUse = &MI; 878 } 879 } 880 return defineVirtReg(MI, OpNum, VirtReg, true); 881 } 882 883 /// Allocates a register for VirtReg definition. Typically the register is 884 /// already assigned from a use of the virtreg, however we still need to 885 /// perform an allocation if: 886 /// - It is a dead definition without any uses. 887 /// - The value is live out and all uses are in different basic blocks. 888 void RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum, 889 Register VirtReg, bool LookAtPhysRegUses) { 890 assert(VirtReg.isVirtual() && "Not a virtual register"); 891 MachineOperand &MO = MI.getOperand(OpNum); 892 LiveRegMap::iterator LRI; 893 bool New; 894 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg)); 895 if (New) { 896 if (!MO.isDead()) { 897 if (mayLiveOut(VirtReg)) { 898 LRI->LiveOut = true; 899 } else { 900 // It is a dead def without the dead flag; add the flag now. 901 MO.setIsDead(true); 902 } 903 } 904 } 905 if (LRI->PhysReg == 0) 906 allocVirtReg(MI, *LRI, 0, LookAtPhysRegUses); 907 else { 908 assert(!isRegUsedInInstr(LRI->PhysReg, LookAtPhysRegUses) && 909 "TODO: preassign mismatch"); 910 LLVM_DEBUG(dbgs() << "In def of " << printReg(VirtReg, TRI) 911 << " use existing assignment to " 912 << printReg(LRI->PhysReg, TRI) << '\n'); 913 } 914 915 MCPhysReg PhysReg = LRI->PhysReg; 916 assert(PhysReg != 0 && "Register not assigned"); 917 if (LRI->Reloaded || LRI->LiveOut) { 918 if (!MI.isImplicitDef()) { 919 MachineBasicBlock::iterator SpillBefore = 920 std::next((MachineBasicBlock::iterator)MI.getIterator()); 921 LLVM_DEBUG(dbgs() << "Spill Reason: LO: " << LRI->LiveOut << " RL: " 922 << LRI->Reloaded << '\n'); 923 bool Kill = LRI->LastUse == nullptr; 924 spill(SpillBefore, VirtReg, PhysReg, Kill, LRI->LiveOut); 925 LRI->LastUse = nullptr; 926 } 927 LRI->LiveOut = false; 928 LRI->Reloaded = false; 929 } 930 if (MI.getOpcode() == TargetOpcode::BUNDLE) { 931 BundleVirtRegsMap[VirtReg] = PhysReg; 932 } 933 markRegUsedInInstr(PhysReg); 934 setPhysReg(MI, MO, PhysReg); 935 } 936 937 /// Allocates a register for a VirtReg use. 938 void RegAllocFast::useVirtReg(MachineInstr &MI, unsigned OpNum, 939 Register VirtReg) { 940 assert(VirtReg.isVirtual() && "Not a virtual register"); 941 MachineOperand &MO = MI.getOperand(OpNum); 942 LiveRegMap::iterator LRI; 943 bool New; 944 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg)); 945 if (New) { 946 MachineOperand &MO = MI.getOperand(OpNum); 947 if (!MO.isKill()) { 948 if (mayLiveOut(VirtReg)) { 949 LRI->LiveOut = true; 950 } else { 951 // It is a last (killing) use without the kill flag; add the flag now. 952 MO.setIsKill(true); 953 } 954 } 955 } else { 956 assert((!MO.isKill() || LRI->LastUse == &MI) && "Invalid kill flag"); 957 } 958 959 // If necessary allocate a register. 960 if (LRI->PhysReg == 0) { 961 assert(!MO.isTied() && "tied op should be allocated"); 962 Register Hint; 963 if (MI.isCopy() && MI.getOperand(1).getSubReg() == 0) { 964 Hint = MI.getOperand(0).getReg(); 965 assert(Hint.isPhysical() && 966 "Copy destination should already be assigned"); 967 } 968 allocVirtReg(MI, *LRI, Hint, false); 969 if (LRI->Error) { 970 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); 971 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC); 972 setPhysReg(MI, MO, *AllocationOrder.begin()); 973 return; 974 } 975 } 976 977 LRI->LastUse = &MI; 978 979 if (MI.getOpcode() == TargetOpcode::BUNDLE) { 980 BundleVirtRegsMap[VirtReg] = LRI->PhysReg; 981 } 982 markRegUsedInInstr(LRI->PhysReg); 983 setPhysReg(MI, MO, LRI->PhysReg); 984 } 985 986 /// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This 987 /// may invalidate any operand pointers. Return true if the operand kills its 988 /// register. 989 void RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO, 990 MCPhysReg PhysReg) { 991 if (!MO.getSubReg()) { 992 MO.setReg(PhysReg); 993 MO.setIsRenamable(true); 994 return; 995 } 996 997 // Handle subregister index. 998 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : MCRegister()); 999 MO.setIsRenamable(true); 1000 // Note: We leave the subreg number around a little longer in case of defs. 1001 // This is so that the register freeing logic in allocateInstruction can still 1002 // recognize this as subregister defs. The code there will clear the number. 1003 if (!MO.isDef()) 1004 MO.setSubReg(0); 1005 1006 // A kill flag implies killing the full register. Add corresponding super 1007 // register kill. 1008 if (MO.isKill()) { 1009 MI.addRegisterKilled(PhysReg, TRI, true); 1010 return; 1011 } 1012 1013 // A <def,read-undef> of a sub-register requires an implicit def of the full 1014 // register. 1015 if (MO.isDef() && MO.isUndef()) { 1016 if (MO.isDead()) 1017 MI.addRegisterDead(PhysReg, TRI, true); 1018 else 1019 MI.addRegisterDefined(PhysReg, TRI); 1020 } 1021 } 1022 1023 #ifndef NDEBUG 1024 1025 void RegAllocFast::dumpState() const { 1026 for (unsigned Unit = 1, UnitE = TRI->getNumRegUnits(); Unit != UnitE; 1027 ++Unit) { 1028 switch (unsigned VirtReg = RegUnitStates[Unit]) { 1029 case regFree: 1030 break; 1031 case regPreAssigned: 1032 dbgs() << " " << printRegUnit(Unit, TRI) << "[P]"; 1033 break; 1034 case regLiveIn: 1035 llvm_unreachable("Should not have regLiveIn in map"); 1036 default: { 1037 dbgs() << ' ' << printRegUnit(Unit, TRI) << '=' << printReg(VirtReg); 1038 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg); 1039 assert(I != LiveVirtRegs.end() && "have LiveVirtRegs entry"); 1040 if (I->LiveOut || I->Reloaded) { 1041 dbgs() << '['; 1042 if (I->LiveOut) dbgs() << 'O'; 1043 if (I->Reloaded) dbgs() << 'R'; 1044 dbgs() << ']'; 1045 } 1046 assert(TRI->hasRegUnit(I->PhysReg, Unit) && "inverse mapping present"); 1047 break; 1048 } 1049 } 1050 } 1051 dbgs() << '\n'; 1052 // Check that LiveVirtRegs is the inverse. 1053 for (const LiveReg &LR : LiveVirtRegs) { 1054 Register VirtReg = LR.VirtReg; 1055 assert(VirtReg.isVirtual() && "Bad map key"); 1056 MCPhysReg PhysReg = LR.PhysReg; 1057 if (PhysReg != 0) { 1058 assert(Register::isPhysicalRegister(PhysReg) && 1059 "mapped to physreg"); 1060 for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) { 1061 assert(RegUnitStates[*UI] == VirtReg && "inverse map valid"); 1062 } 1063 } 1064 } 1065 } 1066 #endif 1067 1068 /// Count number of defs consumed from each register class by \p Reg 1069 void RegAllocFast::addRegClassDefCounts(std::vector<unsigned> &RegClassDefCounts, 1070 Register Reg) const { 1071 assert(RegClassDefCounts.size() == TRI->getNumRegClasses()); 1072 1073 if (Reg.isVirtual()) { 1074 const TargetRegisterClass *OpRC = MRI->getRegClass(Reg); 1075 for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses(); 1076 RCIdx != RCIdxEnd; ++RCIdx) { 1077 const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx); 1078 // FIXME: Consider aliasing sub/super registers. 1079 if (OpRC->hasSubClassEq(IdxRC)) 1080 ++RegClassDefCounts[RCIdx]; 1081 } 1082 1083 return; 1084 } 1085 1086 for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses(); 1087 RCIdx != RCIdxEnd; ++RCIdx) { 1088 const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx); 1089 for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) { 1090 if (IdxRC->contains(*Alias)) { 1091 ++RegClassDefCounts[RCIdx]; 1092 break; 1093 } 1094 } 1095 } 1096 } 1097 1098 void RegAllocFast::allocateInstruction(MachineInstr &MI) { 1099 // The basic algorithm here is: 1100 // 1. Mark registers of def operands as free 1101 // 2. Allocate registers to use operands and place reload instructions for 1102 // registers displaced by the allocation. 1103 // 1104 // However we need to handle some corner cases: 1105 // - pre-assigned defs and uses need to be handled before the other def/use 1106 // operands are processed to avoid the allocation heuristics clashing with 1107 // the pre-assignment. 1108 // - The "free def operands" step has to come last instead of first for tied 1109 // operands and early-clobbers. 1110 1111 UsedInInstr.clear(); 1112 RegMasks.clear(); 1113 BundleVirtRegsMap.clear(); 1114 1115 // Scan for special cases; Apply pre-assigned register defs to state. 1116 bool HasPhysRegUse = false; 1117 bool HasRegMask = false; 1118 bool HasVRegDef = false; 1119 bool HasDef = false; 1120 bool HasEarlyClobber = false; 1121 bool NeedToAssignLiveThroughs = false; 1122 for (MachineOperand &MO : MI.operands()) { 1123 if (MO.isReg()) { 1124 Register Reg = MO.getReg(); 1125 if (Reg.isVirtual()) { 1126 if (MO.isDef()) { 1127 HasDef = true; 1128 HasVRegDef = true; 1129 if (MO.isEarlyClobber()) { 1130 HasEarlyClobber = true; 1131 NeedToAssignLiveThroughs = true; 1132 } 1133 if (MO.isTied() || (MO.getSubReg() != 0 && !MO.isUndef())) 1134 NeedToAssignLiveThroughs = true; 1135 } 1136 } else if (Reg.isPhysical()) { 1137 if (!MRI->isReserved(Reg)) { 1138 if (MO.isDef()) { 1139 HasDef = true; 1140 bool displacedAny = definePhysReg(MI, Reg); 1141 if (MO.isEarlyClobber()) 1142 HasEarlyClobber = true; 1143 if (!displacedAny) 1144 MO.setIsDead(true); 1145 } 1146 if (MO.readsReg()) 1147 HasPhysRegUse = true; 1148 } 1149 } 1150 } else if (MO.isRegMask()) { 1151 HasRegMask = true; 1152 RegMasks.push_back(MO.getRegMask()); 1153 } 1154 } 1155 1156 // Allocate virtreg defs. 1157 if (HasDef) { 1158 if (HasVRegDef) { 1159 // Special handling for early clobbers, tied operands or subregister defs: 1160 // Compared to "normal" defs these: 1161 // - Must not use a register that is pre-assigned for a use operand. 1162 // - In order to solve tricky inline assembly constraints we change the 1163 // heuristic to figure out a good operand order before doing 1164 // assignments. 1165 if (NeedToAssignLiveThroughs) { 1166 DefOperandIndexes.clear(); 1167 PhysRegUses.clear(); 1168 1169 // Track number of defs which may consume a register from the class. 1170 std::vector<unsigned> RegClassDefCounts(TRI->getNumRegClasses(), 0); 1171 assert(RegClassDefCounts[0] == 0); 1172 1173 LLVM_DEBUG(dbgs() << "Need to assign livethroughs\n"); 1174 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) { 1175 const MachineOperand &MO = MI.getOperand(I); 1176 if (!MO.isReg()) 1177 continue; 1178 Register Reg = MO.getReg(); 1179 if (MO.readsReg()) { 1180 if (Reg.isPhysical()) { 1181 LLVM_DEBUG(dbgs() << "mark extra used: " << printReg(Reg, TRI) 1182 << '\n'); 1183 markPhysRegUsedInInstr(Reg); 1184 } 1185 } 1186 1187 if (MO.isDef()) { 1188 if (Reg.isVirtual()) 1189 DefOperandIndexes.push_back(I); 1190 1191 addRegClassDefCounts(RegClassDefCounts, Reg); 1192 } 1193 } 1194 1195 llvm::sort(DefOperandIndexes, [&](uint16_t I0, uint16_t I1) { 1196 const MachineOperand &MO0 = MI.getOperand(I0); 1197 const MachineOperand &MO1 = MI.getOperand(I1); 1198 Register Reg0 = MO0.getReg(); 1199 Register Reg1 = MO1.getReg(); 1200 const TargetRegisterClass &RC0 = *MRI->getRegClass(Reg0); 1201 const TargetRegisterClass &RC1 = *MRI->getRegClass(Reg1); 1202 1203 // Identify regclass that are easy to use up completely just in this 1204 // instruction. 1205 unsigned ClassSize0 = RegClassInfo.getOrder(&RC0).size(); 1206 unsigned ClassSize1 = RegClassInfo.getOrder(&RC1).size(); 1207 1208 bool SmallClass0 = ClassSize0 < RegClassDefCounts[RC0.getID()]; 1209 bool SmallClass1 = ClassSize1 < RegClassDefCounts[RC1.getID()]; 1210 if (SmallClass0 > SmallClass1) 1211 return true; 1212 if (SmallClass0 < SmallClass1) 1213 return false; 1214 1215 // Allocate early clobbers and livethrough operands first. 1216 bool Livethrough0 = MO0.isEarlyClobber() || MO0.isTied() || 1217 (MO0.getSubReg() == 0 && !MO0.isUndef()); 1218 bool Livethrough1 = MO1.isEarlyClobber() || MO1.isTied() || 1219 (MO1.getSubReg() == 0 && !MO1.isUndef()); 1220 if (Livethrough0 > Livethrough1) 1221 return true; 1222 if (Livethrough0 < Livethrough1) 1223 return false; 1224 1225 // Tie-break rule: operand index. 1226 return I0 < I1; 1227 }); 1228 1229 for (uint16_t OpIdx : DefOperandIndexes) { 1230 MachineOperand &MO = MI.getOperand(OpIdx); 1231 LLVM_DEBUG(dbgs() << "Allocating " << MO << '\n'); 1232 unsigned Reg = MO.getReg(); 1233 if (MO.isEarlyClobber() || MO.isTied() || 1234 (MO.getSubReg() && !MO.isUndef())) { 1235 defineLiveThroughVirtReg(MI, OpIdx, Reg); 1236 } else { 1237 defineVirtReg(MI, OpIdx, Reg); 1238 } 1239 } 1240 } else { 1241 // Assign virtual register defs. 1242 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) { 1243 MachineOperand &MO = MI.getOperand(I); 1244 if (!MO.isReg() || !MO.isDef()) 1245 continue; 1246 Register Reg = MO.getReg(); 1247 if (Reg.isVirtual()) 1248 defineVirtReg(MI, I, Reg); 1249 } 1250 } 1251 } 1252 1253 // Free registers occupied by defs. 1254 // Iterate operands in reverse order, so we see the implicit super register 1255 // defs first (we added them earlier in case of <def,read-undef>). 1256 for (MachineOperand &MO : llvm::reverse(MI.operands())) { 1257 if (!MO.isReg() || !MO.isDef()) 1258 continue; 1259 1260 // subreg defs don't free the full register. We left the subreg number 1261 // around as a marker in setPhysReg() to recognize this case here. 1262 if (MO.getSubReg() != 0) { 1263 MO.setSubReg(0); 1264 continue; 1265 } 1266 1267 assert((!MO.isTied() || !isClobberedByRegMasks(MO.getReg())) && 1268 "tied def assigned to clobbered register"); 1269 1270 // Do not free tied operands and early clobbers. 1271 if (MO.isTied() || MO.isEarlyClobber()) 1272 continue; 1273 Register Reg = MO.getReg(); 1274 if (!Reg) 1275 continue; 1276 assert(Reg.isPhysical()); 1277 if (MRI->isReserved(Reg)) 1278 continue; 1279 freePhysReg(Reg); 1280 unmarkRegUsedInInstr(Reg); 1281 } 1282 } 1283 1284 // Displace clobbered registers. 1285 if (HasRegMask) { 1286 assert(!RegMasks.empty() && "expected RegMask"); 1287 // MRI bookkeeping. 1288 for (const auto *RM : RegMasks) 1289 MRI->addPhysRegsUsedFromRegMask(RM); 1290 1291 // Displace clobbered registers. 1292 for (const LiveReg &LR : LiveVirtRegs) { 1293 MCPhysReg PhysReg = LR.PhysReg; 1294 if (PhysReg != 0 && isClobberedByRegMasks(PhysReg)) 1295 displacePhysReg(MI, PhysReg); 1296 } 1297 } 1298 1299 // Apply pre-assigned register uses to state. 1300 if (HasPhysRegUse) { 1301 for (MachineOperand &MO : MI.operands()) { 1302 if (!MO.isReg() || !MO.readsReg()) 1303 continue; 1304 Register Reg = MO.getReg(); 1305 if (!Reg.isPhysical()) 1306 continue; 1307 if (MRI->isReserved(Reg)) 1308 continue; 1309 bool displacedAny = usePhysReg(MI, Reg); 1310 if (!displacedAny && !MRI->isReserved(Reg)) 1311 MO.setIsKill(true); 1312 } 1313 } 1314 1315 // Allocate virtreg uses and insert reloads as necessary. 1316 bool HasUndefUse = false; 1317 for (unsigned I = 0; I < MI.getNumOperands(); ++I) { 1318 MachineOperand &MO = MI.getOperand(I); 1319 if (!MO.isReg() || !MO.isUse()) 1320 continue; 1321 Register Reg = MO.getReg(); 1322 if (!Reg.isVirtual()) 1323 continue; 1324 1325 if (MO.isUndef()) { 1326 HasUndefUse = true; 1327 continue; 1328 } 1329 1330 1331 // Populate MayLiveAcrossBlocks in case the use block is allocated before 1332 // the def block (removing the vreg uses). 1333 mayLiveIn(Reg); 1334 1335 1336 assert(!MO.isInternalRead() && "Bundles not supported"); 1337 assert(MO.readsReg() && "reading use"); 1338 useVirtReg(MI, I, Reg); 1339 } 1340 1341 // Allocate undef operands. This is a separate step because in a situation 1342 // like ` = OP undef %X, %X` both operands need the same register assign 1343 // so we should perform the normal assignment first. 1344 if (HasUndefUse) { 1345 for (MachineOperand &MO : MI.uses()) { 1346 if (!MO.isReg() || !MO.isUse()) 1347 continue; 1348 Register Reg = MO.getReg(); 1349 if (!Reg.isVirtual()) 1350 continue; 1351 1352 assert(MO.isUndef() && "Should only have undef virtreg uses left"); 1353 allocVirtRegUndef(MO); 1354 } 1355 } 1356 1357 // Free early clobbers. 1358 if (HasEarlyClobber) { 1359 for (MachineOperand &MO : llvm::reverse(MI.operands())) { 1360 if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber()) 1361 continue; 1362 // subreg defs don't free the full register. We left the subreg number 1363 // around as a marker in setPhysReg() to recognize this case here. 1364 if (MO.getSubReg() != 0) { 1365 MO.setSubReg(0); 1366 continue; 1367 } 1368 1369 Register Reg = MO.getReg(); 1370 if (!Reg) 1371 continue; 1372 assert(Reg.isPhysical() && "should have register assigned"); 1373 1374 // We sometimes get odd situations like: 1375 // early-clobber %x0 = INSTRUCTION %x0 1376 // which is semantically questionable as the early-clobber should 1377 // apply before the use. But in practice we consider the use to 1378 // happen before the early clobber now. Don't free the early clobber 1379 // register in this case. 1380 if (MI.readsRegister(Reg, TRI)) 1381 continue; 1382 1383 freePhysReg(Reg); 1384 } 1385 } 1386 1387 LLVM_DEBUG(dbgs() << "<< " << MI); 1388 if (MI.isCopy() && MI.getOperand(0).getReg() == MI.getOperand(1).getReg() && 1389 MI.getNumOperands() == 2) { 1390 LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n"); 1391 Coalesced.push_back(&MI); 1392 } 1393 } 1394 1395 void RegAllocFast::handleDebugValue(MachineInstr &MI) { 1396 // Ignore DBG_VALUEs that aren't based on virtual registers. These are 1397 // mostly constants and frame indices. 1398 for (Register Reg : MI.getUsedDebugRegs()) { 1399 if (!Register::isVirtualRegister(Reg)) 1400 continue; 1401 1402 // Already spilled to a stackslot? 1403 int SS = StackSlotForVirtReg[Reg]; 1404 if (SS != -1) { 1405 // Modify DBG_VALUE now that the value is in a spill slot. 1406 updateDbgValueForSpill(MI, SS, Reg); 1407 LLVM_DEBUG(dbgs() << "Rewrite DBG_VALUE for spilled memory: " << MI); 1408 continue; 1409 } 1410 1411 // See if this virtual register has already been allocated to a physical 1412 // register or spilled to a stack slot. 1413 LiveRegMap::iterator LRI = findLiveVirtReg(Reg); 1414 SmallVector<MachineOperand *> DbgOps; 1415 for (MachineOperand &Op : MI.getDebugOperandsForReg(Reg)) 1416 DbgOps.push_back(&Op); 1417 1418 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) { 1419 // Update every use of Reg within MI. 1420 for (auto &RegMO : DbgOps) 1421 setPhysReg(MI, *RegMO, LRI->PhysReg); 1422 } else { 1423 DanglingDbgValues[Reg].push_back(&MI); 1424 } 1425 1426 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so 1427 // that future spills of Reg will have DBG_VALUEs. 1428 LiveDbgValueMap[Reg].append(DbgOps.begin(), DbgOps.end()); 1429 } 1430 } 1431 1432 void RegAllocFast::handleBundle(MachineInstr &MI) { 1433 MachineBasicBlock::instr_iterator BundledMI = MI.getIterator(); 1434 ++BundledMI; 1435 while (BundledMI->isBundledWithPred()) { 1436 for (MachineOperand &MO : BundledMI->operands()) { 1437 if (!MO.isReg()) 1438 continue; 1439 1440 Register Reg = MO.getReg(); 1441 if (!Reg.isVirtual()) 1442 continue; 1443 1444 DenseMap<Register, MCPhysReg>::iterator DI; 1445 DI = BundleVirtRegsMap.find(Reg); 1446 assert(DI != BundleVirtRegsMap.end() && "Unassigned virtual register"); 1447 1448 setPhysReg(MI, MO, DI->second); 1449 } 1450 1451 ++BundledMI; 1452 } 1453 } 1454 1455 void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) { 1456 this->MBB = &MBB; 1457 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB); 1458 1459 RegUnitStates.assign(TRI->getNumRegUnits(), regFree); 1460 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?"); 1461 1462 for (auto &LiveReg : MBB.liveouts()) 1463 setPhysRegState(LiveReg.PhysReg, regPreAssigned); 1464 1465 Coalesced.clear(); 1466 1467 // Traverse block in reverse order allocating instructions one by one. 1468 for (MachineInstr &MI : reverse(MBB)) { 1469 LLVM_DEBUG( 1470 dbgs() << "\n>> " << MI << "Regs:"; 1471 dumpState() 1472 ); 1473 1474 // Special handling for debug values. Note that they are not allowed to 1475 // affect codegen of the other instructions in any way. 1476 if (MI.isDebugValue()) { 1477 handleDebugValue(MI); 1478 continue; 1479 } 1480 1481 allocateInstruction(MI); 1482 1483 // Once BUNDLE header is assigned registers, same assignments need to be 1484 // done for bundled MIs. 1485 if (MI.getOpcode() == TargetOpcode::BUNDLE) { 1486 handleBundle(MI); 1487 } 1488 } 1489 1490 LLVM_DEBUG( 1491 dbgs() << "Begin Regs:"; 1492 dumpState() 1493 ); 1494 1495 // Spill all physical registers holding virtual registers now. 1496 LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n"); 1497 reloadAtBegin(MBB); 1498 1499 // Erase all the coalesced copies. We are delaying it until now because 1500 // LiveVirtRegs might refer to the instrs. 1501 for (MachineInstr *MI : Coalesced) 1502 MBB.erase(MI); 1503 NumCoalesced += Coalesced.size(); 1504 1505 for (auto &UDBGPair : DanglingDbgValues) { 1506 for (MachineInstr *DbgValue : UDBGPair.second) { 1507 assert(DbgValue->isDebugValue() && "expected DBG_VALUE"); 1508 // Nothing to do if the vreg was spilled in the meantime. 1509 if (!DbgValue->hasDebugOperandForReg(UDBGPair.first)) 1510 continue; 1511 LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue 1512 << '\n'); 1513 DbgValue->setDebugValueUndef(); 1514 } 1515 } 1516 DanglingDbgValues.clear(); 1517 1518 LLVM_DEBUG(MBB.dump()); 1519 } 1520 1521 bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) { 1522 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n" 1523 << "********** Function: " << MF.getName() << '\n'); 1524 MRI = &MF.getRegInfo(); 1525 const TargetSubtargetInfo &STI = MF.getSubtarget(); 1526 TRI = STI.getRegisterInfo(); 1527 TII = STI.getInstrInfo(); 1528 MFI = &MF.getFrameInfo(); 1529 MRI->freezeReservedRegs(MF); 1530 RegClassInfo.runOnMachineFunction(MF); 1531 unsigned NumRegUnits = TRI->getNumRegUnits(); 1532 UsedInInstr.clear(); 1533 UsedInInstr.setUniverse(NumRegUnits); 1534 PhysRegUses.clear(); 1535 PhysRegUses.setUniverse(NumRegUnits); 1536 1537 // initialize the virtual->physical register map to have a 'null' 1538 // mapping for all virtual registers 1539 unsigned NumVirtRegs = MRI->getNumVirtRegs(); 1540 StackSlotForVirtReg.resize(NumVirtRegs); 1541 LiveVirtRegs.setUniverse(NumVirtRegs); 1542 MayLiveAcrossBlocks.clear(); 1543 MayLiveAcrossBlocks.resize(NumVirtRegs); 1544 1545 // Loop over all of the basic blocks, eliminating virtual register references 1546 for (MachineBasicBlock &MBB : MF) 1547 allocateBasicBlock(MBB); 1548 1549 if (ClearVirtRegs) { 1550 // All machine operands and other references to virtual registers have been 1551 // replaced. Remove the virtual registers. 1552 MRI->clearVirtRegs(); 1553 } 1554 1555 StackSlotForVirtReg.clear(); 1556 LiveDbgValueMap.clear(); 1557 return true; 1558 } 1559 1560 FunctionPass *llvm::createFastRegisterAllocator() { 1561 return new RegAllocFast(); 1562 } 1563 1564 FunctionPass *llvm::createFastRegisterAllocator( 1565 std::function<bool(const TargetRegisterInfo &TRI, 1566 const TargetRegisterClass &RC)> Ftor, bool ClearVirtRegs) { 1567 return new RegAllocFast(Ftor, ClearVirtRegs); 1568 } 1569