1 //===- lib/Codegen/MachineRegisterInfo.cpp --------------------------------===// 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 // Implementation of the MachineRegisterInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/MachineRegisterInfo.h" 14 #include "llvm/ADT/iterator_range.h" 15 #include "llvm/CodeGen/MachineBasicBlock.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/CodeGen/MachineInstr.h" 18 #include "llvm/CodeGen/MachineInstrBuilder.h" 19 #include "llvm/CodeGen/MachineOperand.h" 20 #include "llvm/CodeGen/TargetInstrInfo.h" 21 #include "llvm/CodeGen/TargetRegisterInfo.h" 22 #include "llvm/CodeGen/TargetSubtargetInfo.h" 23 #include "llvm/Config/llvm-config.h" 24 #include "llvm/IR/Attributes.h" 25 #include "llvm/IR/DebugLoc.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/MC/MCRegisterInfo.h" 28 #include "llvm/Support/Casting.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Support/Compiler.h" 31 #include "llvm/Support/ErrorHandling.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <cassert> 34 35 using namespace llvm; 36 37 static cl::opt<bool> EnableSubRegLiveness("enable-subreg-liveness", cl::Hidden, 38 cl::init(true), cl::desc("Enable subregister liveness tracking.")); 39 40 // Pin the vtable to this file. 41 void MachineRegisterInfo::Delegate::anchor() {} 42 43 MachineRegisterInfo::MachineRegisterInfo(MachineFunction *MF) 44 : MF(MF), 45 TracksSubRegLiveness(EnableSubRegLiveness.getNumOccurrences() 46 ? EnableSubRegLiveness 47 : MF->getSubtarget().enableSubRegLiveness()) { 48 unsigned NumRegs = getTargetRegisterInfo()->getNumRegs(); 49 VRegInfo.reserve(256); 50 UsedPhysRegMask.resize(NumRegs); 51 PhysRegUseDefLists.reset(new MachineOperand*[NumRegs]()); 52 TheDelegates.clear(); 53 } 54 55 /// setRegClass - Set the register class of the specified virtual register. 56 /// 57 void 58 MachineRegisterInfo::setRegClass(Register Reg, const TargetRegisterClass *RC) { 59 assert(RC && RC->isAllocatable() && "Invalid RC for virtual register"); 60 VRegInfo[Reg].first = RC; 61 } 62 63 void MachineRegisterInfo::setRegBank(Register Reg, 64 const RegisterBank &RegBank) { 65 VRegInfo[Reg].first = &RegBank; 66 } 67 68 static const TargetRegisterClass * 69 constrainRegClass(MachineRegisterInfo &MRI, Register Reg, 70 const TargetRegisterClass *OldRC, 71 const TargetRegisterClass *RC, unsigned MinNumRegs) { 72 if (OldRC == RC) 73 return RC; 74 const TargetRegisterClass *NewRC = 75 MRI.getTargetRegisterInfo()->getCommonSubClass(OldRC, RC); 76 if (!NewRC || NewRC == OldRC) 77 return NewRC; 78 if (NewRC->getNumRegs() < MinNumRegs) 79 return nullptr; 80 MRI.setRegClass(Reg, NewRC); 81 return NewRC; 82 } 83 84 const TargetRegisterClass *MachineRegisterInfo::constrainRegClass( 85 Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs) { 86 if (Reg.isPhysical()) 87 return nullptr; 88 return ::constrainRegClass(*this, Reg, getRegClass(Reg), RC, MinNumRegs); 89 } 90 91 bool 92 MachineRegisterInfo::constrainRegAttrs(Register Reg, 93 Register ConstrainingReg, 94 unsigned MinNumRegs) { 95 const LLT RegTy = getType(Reg); 96 const LLT ConstrainingRegTy = getType(ConstrainingReg); 97 if (RegTy.isValid() && ConstrainingRegTy.isValid() && 98 RegTy != ConstrainingRegTy) 99 return false; 100 const auto &ConstrainingRegCB = getRegClassOrRegBank(ConstrainingReg); 101 if (!ConstrainingRegCB.isNull()) { 102 const auto &RegCB = getRegClassOrRegBank(Reg); 103 if (RegCB.isNull()) 104 setRegClassOrRegBank(Reg, ConstrainingRegCB); 105 else if (isa<const TargetRegisterClass *>(RegCB) != 106 isa<const TargetRegisterClass *>(ConstrainingRegCB)) 107 return false; 108 else if (isa<const TargetRegisterClass *>(RegCB)) { 109 if (!::constrainRegClass( 110 *this, Reg, cast<const TargetRegisterClass *>(RegCB), 111 cast<const TargetRegisterClass *>(ConstrainingRegCB), MinNumRegs)) 112 return false; 113 } else if (RegCB != ConstrainingRegCB) 114 return false; 115 } 116 if (ConstrainingRegTy.isValid()) 117 setType(Reg, ConstrainingRegTy); 118 return true; 119 } 120 121 bool 122 MachineRegisterInfo::recomputeRegClass(Register Reg) { 123 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 124 const TargetRegisterClass *OldRC = getRegClass(Reg); 125 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 126 const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC, *MF); 127 128 // Stop early if there is no room to grow. 129 if (NewRC == OldRC) 130 return false; 131 132 // Accumulate constraints from all uses. 133 for (MachineOperand &MO : reg_nodbg_operands(Reg)) { 134 // Apply the effect of the given operand to NewRC. 135 MachineInstr *MI = MO.getParent(); 136 unsigned OpNo = &MO - &MI->getOperand(0); 137 NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII, TRI); 138 if (!NewRC || NewRC == OldRC) 139 return false; 140 } 141 setRegClass(Reg, NewRC); 142 return true; 143 } 144 145 Register MachineRegisterInfo::createIncompleteVirtualRegister(StringRef Name) { 146 Register Reg = Register::index2VirtReg(getNumVirtRegs()); 147 VRegInfo.grow(Reg); 148 insertVRegByName(Name, Reg); 149 return Reg; 150 } 151 152 /// createVirtualRegister - Create and return a new virtual register in the 153 /// function with the specified register class. 154 /// 155 Register 156 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass, 157 StringRef Name) { 158 assert(RegClass && "Cannot create register without RegClass!"); 159 assert(RegClass->isAllocatable() && 160 "Virtual register RegClass must be allocatable."); 161 162 // New virtual register number. 163 Register Reg = createIncompleteVirtualRegister(Name); 164 VRegInfo[Reg].first = RegClass; 165 noteNewVirtualRegister(Reg); 166 return Reg; 167 } 168 169 Register MachineRegisterInfo::createVirtualRegister(VRegAttrs RegAttr, 170 StringRef Name) { 171 Register Reg = createIncompleteVirtualRegister(Name); 172 VRegInfo[Reg].first = RegAttr.RCOrRB; 173 setType(Reg, RegAttr.Ty); 174 noteNewVirtualRegister(Reg); 175 return Reg; 176 } 177 178 Register MachineRegisterInfo::cloneVirtualRegister(Register VReg, 179 StringRef Name) { 180 Register Reg = createIncompleteVirtualRegister(Name); 181 VRegInfo[Reg].first = VRegInfo[VReg].first; 182 setType(Reg, getType(VReg)); 183 noteCloneVirtualRegister(Reg, VReg); 184 return Reg; 185 } 186 187 void MachineRegisterInfo::setType(Register VReg, LLT Ty) { 188 VRegToType.grow(VReg); 189 VRegToType[VReg] = Ty; 190 } 191 192 Register 193 MachineRegisterInfo::createGenericVirtualRegister(LLT Ty, StringRef Name) { 194 // New virtual register number. 195 Register Reg = createIncompleteVirtualRegister(Name); 196 // FIXME: Should we use a dummy register class? 197 VRegInfo[Reg].first = static_cast<RegisterBank *>(nullptr); 198 setType(Reg, Ty); 199 noteNewVirtualRegister(Reg); 200 return Reg; 201 } 202 203 void MachineRegisterInfo::clearVirtRegTypes() { VRegToType.clear(); } 204 205 /// clearVirtRegs - Remove all virtual registers (after physreg assignment). 206 void MachineRegisterInfo::clearVirtRegs() { 207 #ifndef NDEBUG 208 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) { 209 Register Reg = Register::index2VirtReg(i); 210 if (!VRegInfo[Reg].second) 211 continue; 212 verifyUseList(Reg); 213 errs() << "Remaining virtual register " 214 << printReg(Reg, getTargetRegisterInfo()) << "...\n"; 215 for (MachineInstr &MI : reg_instructions(Reg)) 216 errs() << "...in instruction: " << MI << "\n"; 217 std::abort(); 218 } 219 #endif 220 VRegInfo.clear(); 221 for (auto &I : LiveIns) 222 I.second = 0; 223 } 224 225 void MachineRegisterInfo::verifyUseList(Register Reg) const { 226 #ifndef NDEBUG 227 bool Valid = true; 228 for (MachineOperand &M : reg_operands(Reg)) { 229 MachineOperand *MO = &M; 230 MachineInstr *MI = MO->getParent(); 231 if (!MI) { 232 errs() << printReg(Reg, getTargetRegisterInfo()) 233 << " use list MachineOperand " << MO 234 << " has no parent instruction.\n"; 235 Valid = false; 236 continue; 237 } 238 MachineOperand *MO0 = &MI->getOperand(0); 239 unsigned NumOps = MI->getNumOperands(); 240 if (!(MO >= MO0 && MO < MO0+NumOps)) { 241 errs() << printReg(Reg, getTargetRegisterInfo()) 242 << " use list MachineOperand " << MO 243 << " doesn't belong to parent MI: " << *MI; 244 Valid = false; 245 } 246 if (!MO->isReg()) { 247 errs() << printReg(Reg, getTargetRegisterInfo()) 248 << " MachineOperand " << MO << ": " << *MO 249 << " is not a register\n"; 250 Valid = false; 251 } 252 if (MO->getReg() != Reg) { 253 errs() << printReg(Reg, getTargetRegisterInfo()) 254 << " use-list MachineOperand " << MO << ": " 255 << *MO << " is the wrong register\n"; 256 Valid = false; 257 } 258 } 259 assert(Valid && "Invalid use list"); 260 #endif 261 } 262 263 void MachineRegisterInfo::verifyUseLists() const { 264 #ifndef NDEBUG 265 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) 266 verifyUseList(Register::index2VirtReg(i)); 267 for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i) 268 verifyUseList(i); 269 #endif 270 } 271 272 /// Add MO to the linked list of operands for its register. 273 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) { 274 assert(!MO->isOnRegUseList() && "Already on list"); 275 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg()); 276 MachineOperand *const Head = HeadRef; 277 278 // Head points to the first list element. 279 // Next is NULL on the last list element. 280 // Prev pointers are circular, so Head->Prev == Last. 281 282 // Head is NULL for an empty list. 283 if (!Head) { 284 MO->Contents.Reg.Prev = MO; 285 MO->Contents.Reg.Next = nullptr; 286 HeadRef = MO; 287 return; 288 } 289 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!"); 290 291 // Insert MO between Last and Head in the circular Prev chain. 292 MachineOperand *Last = Head->Contents.Reg.Prev; 293 assert(Last && "Inconsistent use list"); 294 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!"); 295 Head->Contents.Reg.Prev = MO; 296 MO->Contents.Reg.Prev = Last; 297 298 // Def operands always precede uses. This allows def_iterator to stop early. 299 // Insert def operands at the front, and use operands at the back. 300 if (MO->isDef()) { 301 // Insert def at the front. 302 MO->Contents.Reg.Next = Head; 303 HeadRef = MO; 304 } else { 305 // Insert use at the end. 306 MO->Contents.Reg.Next = nullptr; 307 Last->Contents.Reg.Next = MO; 308 } 309 } 310 311 /// Remove MO from its use-def list. 312 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) { 313 assert(MO->isOnRegUseList() && "Operand not on use list"); 314 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg()); 315 MachineOperand *const Head = HeadRef; 316 assert(Head && "List already empty"); 317 318 // Unlink this from the doubly linked list of operands. 319 MachineOperand *Next = MO->Contents.Reg.Next; 320 MachineOperand *Prev = MO->Contents.Reg.Prev; 321 322 // Prev links are circular, next link is NULL instead of looping back to Head. 323 if (MO == Head) 324 HeadRef = Next; 325 else 326 Prev->Contents.Reg.Next = Next; 327 328 (Next ? Next : Head)->Contents.Reg.Prev = Prev; 329 330 MO->Contents.Reg.Prev = nullptr; 331 MO->Contents.Reg.Next = nullptr; 332 } 333 334 /// Move NumOps operands from Src to Dst, updating use-def lists as needed. 335 /// 336 /// The Dst range is assumed to be uninitialized memory. (Or it may contain 337 /// operands that won't be destroyed, which is OK because the MO destructor is 338 /// trivial anyway). 339 /// 340 /// The Src and Dst ranges may overlap. 341 void MachineRegisterInfo::moveOperands(MachineOperand *Dst, 342 MachineOperand *Src, 343 unsigned NumOps) { 344 assert(Src != Dst && NumOps && "Noop moveOperands"); 345 346 // Copy backwards if Dst is within the Src range. 347 int Stride = 1; 348 if (Dst >= Src && Dst < Src + NumOps) { 349 Stride = -1; 350 Dst += NumOps - 1; 351 Src += NumOps - 1; 352 } 353 354 // Copy one operand at a time. 355 do { 356 new (Dst) MachineOperand(*Src); 357 358 // Dst takes Src's place in the use-def chain. 359 if (Src->isReg()) { 360 MachineOperand *&Head = getRegUseDefListHead(Src->getReg()); 361 MachineOperand *Prev = Src->Contents.Reg.Prev; 362 MachineOperand *Next = Src->Contents.Reg.Next; 363 assert(Head && "List empty, but operand is chained"); 364 assert(Prev && "Operand was not on use-def list"); 365 366 // Prev links are circular, next link is NULL instead of looping back to 367 // Head. 368 if (Src == Head) 369 Head = Dst; 370 else 371 Prev->Contents.Reg.Next = Dst; 372 373 // Update Prev pointer. This also works when Src was pointing to itself 374 // in a 1-element list. In that case Head == Dst. 375 (Next ? Next : Head)->Contents.Reg.Prev = Dst; 376 } 377 378 Dst += Stride; 379 Src += Stride; 380 } while (--NumOps); 381 } 382 383 /// replaceRegWith - Replace all instances of FromReg with ToReg in the 384 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y), 385 /// except that it also changes any definitions of the register as well. 386 /// If ToReg is a physical register we apply the sub register to obtain the 387 /// final/proper physical register. 388 void MachineRegisterInfo::replaceRegWith(Register FromReg, Register ToReg) { 389 assert(FromReg != ToReg && "Cannot replace a reg with itself"); 390 391 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 392 393 // TODO: This could be more efficient by bulk changing the operands. 394 for (MachineOperand &O : llvm::make_early_inc_range(reg_operands(FromReg))) { 395 if (ToReg.isPhysical()) { 396 O.substPhysReg(ToReg, *TRI); 397 } else { 398 O.setReg(ToReg); 399 } 400 } 401 } 402 403 /// getVRegDef - Return the machine instr that defines the specified virtual 404 /// register or null if none is found. This assumes that the code is in SSA 405 /// form, so there should only be one definition. 406 MachineInstr *MachineRegisterInfo::getVRegDef(Register Reg) const { 407 // Since we are in SSA form, we can use the first definition. 408 def_instr_iterator I = def_instr_begin(Reg); 409 if (I == def_instr_end()) 410 return nullptr; 411 assert(std::next(I) == def_instr_end() && 412 "getVRegDef assumes at most one definition"); 413 return &*I; 414 } 415 416 /// getUniqueVRegDef - Return the unique machine instr that defines the 417 /// specified virtual register or null if none is found. If there are 418 /// multiple definitions or no definition, return null. 419 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(Register Reg) const { 420 if (def_empty(Reg)) return nullptr; 421 def_instr_iterator I = def_instr_begin(Reg); 422 if (std::next(I) != def_instr_end()) 423 return nullptr; 424 return &*I; 425 } 426 427 bool MachineRegisterInfo::hasOneNonDBGUse(Register RegNo) const { 428 return hasSingleElement(use_nodbg_operands(RegNo)); 429 } 430 431 bool MachineRegisterInfo::hasOneNonDBGUser(Register RegNo) const { 432 return hasSingleElement(use_nodbg_instructions(RegNo)); 433 } 434 435 bool MachineRegisterInfo::hasAtMostUserInstrs(Register Reg, 436 unsigned MaxUsers) const { 437 return hasNItemsOrLess(use_instr_nodbg_begin(Reg), use_instr_nodbg_end(), 438 MaxUsers); 439 } 440 441 /// clearKillFlags - Iterate over all the uses of the given register and 442 /// clear the kill flag from the MachineOperand. This function is used by 443 /// optimization passes which extend register lifetimes and need only 444 /// preserve conservative kill flag information. 445 void MachineRegisterInfo::clearKillFlags(Register Reg) const { 446 for (MachineOperand &MO : use_operands(Reg)) 447 MO.setIsKill(false); 448 } 449 450 bool MachineRegisterInfo::isLiveIn(Register Reg) const { 451 for (const std::pair<MCRegister, Register> &LI : liveins()) 452 if ((Register)LI.first == Reg || LI.second == Reg) 453 return true; 454 return false; 455 } 456 457 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the 458 /// corresponding live-in physical register. 459 MCRegister MachineRegisterInfo::getLiveInPhysReg(Register VReg) const { 460 for (const std::pair<MCRegister, Register> &LI : liveins()) 461 if (LI.second == VReg) 462 return LI.first; 463 return MCRegister(); 464 } 465 466 /// getLiveInVirtReg - If PReg is a live-in physical register, return the 467 /// corresponding live-in physical register. 468 Register MachineRegisterInfo::getLiveInVirtReg(MCRegister PReg) const { 469 for (const std::pair<MCRegister, Register> &LI : liveins()) 470 if (LI.first == PReg) 471 return LI.second; 472 return Register(); 473 } 474 475 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers 476 /// into the given entry block. 477 void 478 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB, 479 const TargetRegisterInfo &TRI, 480 const TargetInstrInfo &TII) { 481 // Emit the copies into the top of the block. 482 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i) 483 if (LiveIns[i].second) { 484 if (use_nodbg_empty(LiveIns[i].second)) { 485 // The livein has no non-dbg uses. Drop it. 486 // 487 // It would be preferable to have isel avoid creating live-in 488 // records for unused arguments in the first place, but it's 489 // complicated by the debug info code for arguments. 490 LiveIns.erase(LiveIns.begin() + i); 491 --i; --e; 492 } else { 493 // Emit a copy. 494 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(), 495 TII.get(TargetOpcode::COPY), LiveIns[i].second) 496 .addReg(LiveIns[i].first); 497 498 // Add the register to the entry block live-in set. 499 EntryMBB->addLiveIn(LiveIns[i].first); 500 } 501 } else { 502 // Add the register to the entry block live-in set. 503 EntryMBB->addLiveIn(LiveIns[i].first); 504 } 505 } 506 507 LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(Register Reg) const { 508 // Lane masks are only defined for vregs. 509 assert(Reg.isVirtual()); 510 const TargetRegisterClass &TRC = *getRegClass(Reg); 511 return TRC.getLaneMask(); 512 } 513 514 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 515 LLVM_DUMP_METHOD void MachineRegisterInfo::dumpUses(Register Reg) const { 516 for (MachineInstr &I : use_instructions(Reg)) 517 I.dump(); 518 } 519 #endif 520 521 void MachineRegisterInfo::freezeReservedRegs() { 522 ReservedRegs = getTargetRegisterInfo()->getReservedRegs(*MF); 523 assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() && 524 "Invalid ReservedRegs vector from target"); 525 } 526 527 bool MachineRegisterInfo::isConstantPhysReg(MCRegister PhysReg) const { 528 assert(PhysReg.isPhysical()); 529 530 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 531 if (TRI->isConstantPhysReg(PhysReg)) 532 return true; 533 534 // Check if any overlapping register is modified, or allocatable so it may be 535 // used later. 536 for (MCRegAliasIterator AI(PhysReg, TRI, true); 537 AI.isValid(); ++AI) 538 if (!def_empty(*AI) || isAllocatable(*AI)) 539 return false; 540 return true; 541 } 542 543 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the 544 /// specified register as undefined which causes the DBG_VALUE to be 545 /// deleted during LiveDebugVariables analysis. 546 void MachineRegisterInfo::markUsesInDebugValueAsUndef(Register Reg) const { 547 // Mark any DBG_VALUE* that uses Reg as undef (but don't delete it.) 548 // We use make_early_inc_range because setReg invalidates the iterator. 549 for (MachineInstr &UseMI : llvm::make_early_inc_range(use_instructions(Reg))) { 550 if (UseMI.isDebugValue() && UseMI.hasDebugOperandForReg(Reg)) 551 UseMI.setDebugValueUndef(); 552 } 553 } 554 555 static const Function *getCalledFunction(const MachineInstr &MI) { 556 for (const MachineOperand &MO : MI.operands()) { 557 if (!MO.isGlobal()) 558 continue; 559 const Function *Func = dyn_cast<Function>(MO.getGlobal()); 560 if (Func != nullptr) 561 return Func; 562 } 563 return nullptr; 564 } 565 566 static bool isNoReturnDef(const MachineOperand &MO) { 567 // Anything which is not a noreturn function is a real def. 568 const MachineInstr &MI = *MO.getParent(); 569 if (!MI.isCall()) 570 return false; 571 const MachineBasicBlock &MBB = *MI.getParent(); 572 if (!MBB.succ_empty()) 573 return false; 574 const MachineFunction &MF = *MBB.getParent(); 575 // We need to keep correct unwind information even if the function will 576 // not return, since the runtime may need it. 577 if (MF.getFunction().hasFnAttribute(Attribute::UWTable)) 578 return false; 579 const Function *Called = getCalledFunction(MI); 580 return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) || 581 !Called->hasFnAttribute(Attribute::NoUnwind)); 582 } 583 584 bool MachineRegisterInfo::isPhysRegModified(MCRegister PhysReg, 585 bool SkipNoReturnDef) const { 586 if (UsedPhysRegMask.test(PhysReg.id())) 587 return true; 588 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 589 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) { 590 for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) { 591 if (!SkipNoReturnDef && isNoReturnDef(MO)) 592 continue; 593 return true; 594 } 595 } 596 return false; 597 } 598 599 bool MachineRegisterInfo::isPhysRegUsed(MCRegister PhysReg, 600 bool SkipRegMaskTest) const { 601 if (!SkipRegMaskTest && UsedPhysRegMask.test(PhysReg.id())) 602 return true; 603 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 604 for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid(); 605 ++AliasReg) { 606 if (!reg_nodbg_empty(*AliasReg)) 607 return true; 608 } 609 return false; 610 } 611 612 void MachineRegisterInfo::disableCalleeSavedRegister(MCRegister Reg) { 613 614 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 615 assert(Reg && (Reg < TRI->getNumRegs()) && 616 "Trying to disable an invalid register"); 617 618 if (!IsUpdatedCSRsInitialized) { 619 const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF); 620 for (const MCPhysReg *I = CSR; *I; ++I) 621 UpdatedCSRs.push_back(*I); 622 623 // Zero value represents the end of the register list 624 // (no more registers should be pushed). 625 UpdatedCSRs.push_back(0); 626 627 IsUpdatedCSRsInitialized = true; 628 } 629 630 // Remove the register (and its aliases from the list). 631 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 632 llvm::erase(UpdatedCSRs, *AI); 633 } 634 635 const MCPhysReg *MachineRegisterInfo::getCalleeSavedRegs() const { 636 if (IsUpdatedCSRsInitialized) 637 return UpdatedCSRs.data(); 638 639 const MCPhysReg *Regs = getTargetRegisterInfo()->getCalleeSavedRegs(MF); 640 641 for (unsigned I = 0; Regs[I]; ++I) 642 if (MF->getSubtarget().isRegisterReservedByUser(Regs[I])) 643 MF->getRegInfo().disableCalleeSavedRegister(Regs[I]); 644 645 return Regs; 646 } 647 648 void MachineRegisterInfo::setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs) { 649 if (IsUpdatedCSRsInitialized) 650 UpdatedCSRs.clear(); 651 652 append_range(UpdatedCSRs, CSRs); 653 654 // Zero value represents the end of the register list 655 // (no more registers should be pushed). 656 UpdatedCSRs.push_back(0); 657 IsUpdatedCSRsInitialized = true; 658 } 659 660 bool MachineRegisterInfo::isReservedRegUnit(unsigned Unit) const { 661 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 662 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { 663 if (all_of(TRI->superregs_inclusive(*Root), 664 [&](MCPhysReg Super) { return isReserved(Super); })) 665 return true; 666 } 667 return false; 668 } 669