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 MachineInstr *MachineRegisterInfo::getOneNonDBGUser(Register RegNo) const { 436 auto RegNoDbgUsers = use_nodbg_instructions(RegNo); 437 return hasSingleElement(RegNoDbgUsers) ? &*RegNoDbgUsers.begin() : nullptr; 438 } 439 440 bool MachineRegisterInfo::hasAtMostUserInstrs(Register Reg, 441 unsigned MaxUsers) const { 442 return hasNItemsOrLess(use_instr_nodbg_begin(Reg), use_instr_nodbg_end(), 443 MaxUsers); 444 } 445 446 /// clearKillFlags - Iterate over all the uses of the given register and 447 /// clear the kill flag from the MachineOperand. This function is used by 448 /// optimization passes which extend register lifetimes and need only 449 /// preserve conservative kill flag information. 450 void MachineRegisterInfo::clearKillFlags(Register Reg) const { 451 for (MachineOperand &MO : use_operands(Reg)) 452 MO.setIsKill(false); 453 } 454 455 bool MachineRegisterInfo::isLiveIn(Register Reg) const { 456 for (const std::pair<MCRegister, Register> &LI : liveins()) 457 if ((Register)LI.first == Reg || LI.second == Reg) 458 return true; 459 return false; 460 } 461 462 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the 463 /// corresponding live-in physical register. 464 MCRegister MachineRegisterInfo::getLiveInPhysReg(Register VReg) const { 465 for (const std::pair<MCRegister, Register> &LI : liveins()) 466 if (LI.second == VReg) 467 return LI.first; 468 return MCRegister(); 469 } 470 471 /// getLiveInVirtReg - If PReg is a live-in physical register, return the 472 /// corresponding live-in physical register. 473 Register MachineRegisterInfo::getLiveInVirtReg(MCRegister PReg) const { 474 for (const std::pair<MCRegister, Register> &LI : liveins()) 475 if (LI.first == PReg) 476 return LI.second; 477 return Register(); 478 } 479 480 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers 481 /// into the given entry block. 482 void 483 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB, 484 const TargetRegisterInfo &TRI, 485 const TargetInstrInfo &TII) { 486 // Emit the copies into the top of the block. 487 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i) 488 if (LiveIns[i].second) { 489 if (use_nodbg_empty(LiveIns[i].second)) { 490 // The livein has no non-dbg uses. Drop it. 491 // 492 // It would be preferable to have isel avoid creating live-in 493 // records for unused arguments in the first place, but it's 494 // complicated by the debug info code for arguments. 495 LiveIns.erase(LiveIns.begin() + i); 496 --i; --e; 497 } else { 498 // Emit a copy. 499 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(), 500 TII.get(TargetOpcode::COPY), LiveIns[i].second) 501 .addReg(LiveIns[i].first); 502 503 // Add the register to the entry block live-in set. 504 EntryMBB->addLiveIn(LiveIns[i].first); 505 } 506 } else { 507 // Add the register to the entry block live-in set. 508 EntryMBB->addLiveIn(LiveIns[i].first); 509 } 510 } 511 512 LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(Register Reg) const { 513 // Lane masks are only defined for vregs. 514 assert(Reg.isVirtual()); 515 const TargetRegisterClass &TRC = *getRegClass(Reg); 516 return TRC.getLaneMask(); 517 } 518 519 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 520 LLVM_DUMP_METHOD void MachineRegisterInfo::dumpUses(Register Reg) const { 521 for (MachineInstr &I : use_instructions(Reg)) 522 I.dump(); 523 } 524 #endif 525 526 void MachineRegisterInfo::freezeReservedRegs() { 527 ReservedRegs = getTargetRegisterInfo()->getReservedRegs(*MF); 528 assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() && 529 "Invalid ReservedRegs vector from target"); 530 } 531 532 bool MachineRegisterInfo::isConstantPhysReg(MCRegister PhysReg) const { 533 assert(PhysReg.isPhysical()); 534 535 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 536 if (TRI->isConstantPhysReg(PhysReg)) 537 return true; 538 539 // Check if any overlapping register is modified, or allocatable so it may be 540 // used later. 541 for (MCRegAliasIterator AI(PhysReg, TRI, true); 542 AI.isValid(); ++AI) 543 if (!def_empty(*AI) || isAllocatable(*AI)) 544 return false; 545 return true; 546 } 547 548 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the 549 /// specified register as undefined which causes the DBG_VALUE to be 550 /// deleted during LiveDebugVariables analysis. 551 void MachineRegisterInfo::markUsesInDebugValueAsUndef(Register Reg) const { 552 // Mark any DBG_VALUE* that uses Reg as undef (but don't delete it.) 553 // We use make_early_inc_range because setReg invalidates the iterator. 554 for (MachineInstr &UseMI : llvm::make_early_inc_range(use_instructions(Reg))) { 555 if (UseMI.isDebugValue() && UseMI.hasDebugOperandForReg(Reg)) 556 UseMI.setDebugValueUndef(); 557 } 558 } 559 560 static const Function *getCalledFunction(const MachineInstr &MI) { 561 for (const MachineOperand &MO : MI.operands()) { 562 if (!MO.isGlobal()) 563 continue; 564 const Function *Func = dyn_cast<Function>(MO.getGlobal()); 565 if (Func != nullptr) 566 return Func; 567 } 568 return nullptr; 569 } 570 571 static bool isNoReturnDef(const MachineOperand &MO) { 572 // Anything which is not a noreturn function is a real def. 573 const MachineInstr &MI = *MO.getParent(); 574 if (!MI.isCall()) 575 return false; 576 const MachineBasicBlock &MBB = *MI.getParent(); 577 if (!MBB.succ_empty()) 578 return false; 579 const MachineFunction &MF = *MBB.getParent(); 580 // We need to keep correct unwind information even if the function will 581 // not return, since the runtime may need it. 582 if (MF.getFunction().hasFnAttribute(Attribute::UWTable)) 583 return false; 584 const Function *Called = getCalledFunction(MI); 585 return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) || 586 !Called->hasFnAttribute(Attribute::NoUnwind)); 587 } 588 589 bool MachineRegisterInfo::isPhysRegModified(MCRegister PhysReg, 590 bool SkipNoReturnDef) const { 591 if (UsedPhysRegMask.test(PhysReg.id())) 592 return true; 593 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 594 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) { 595 for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) { 596 if (!SkipNoReturnDef && isNoReturnDef(MO)) 597 continue; 598 return true; 599 } 600 } 601 return false; 602 } 603 604 bool MachineRegisterInfo::isPhysRegUsed(MCRegister PhysReg, 605 bool SkipRegMaskTest) const { 606 if (!SkipRegMaskTest && UsedPhysRegMask.test(PhysReg.id())) 607 return true; 608 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 609 for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid(); 610 ++AliasReg) { 611 if (!reg_nodbg_empty(*AliasReg)) 612 return true; 613 } 614 return false; 615 } 616 617 void MachineRegisterInfo::disableCalleeSavedRegister(MCRegister Reg) { 618 619 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 620 assert(Reg && (Reg < TRI->getNumRegs()) && 621 "Trying to disable an invalid register"); 622 623 if (!IsUpdatedCSRsInitialized) { 624 const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF); 625 for (const MCPhysReg *I = CSR; *I; ++I) 626 UpdatedCSRs.push_back(*I); 627 628 // Zero value represents the end of the register list 629 // (no more registers should be pushed). 630 UpdatedCSRs.push_back(0); 631 632 IsUpdatedCSRsInitialized = true; 633 } 634 635 // Remove the register (and its aliases from the list). 636 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 637 llvm::erase(UpdatedCSRs, *AI); 638 } 639 640 const MCPhysReg *MachineRegisterInfo::getCalleeSavedRegs() const { 641 if (IsUpdatedCSRsInitialized) 642 return UpdatedCSRs.data(); 643 644 const MCPhysReg *Regs = getTargetRegisterInfo()->getCalleeSavedRegs(MF); 645 646 for (unsigned I = 0; Regs[I]; ++I) 647 if (MF->getSubtarget().isRegisterReservedByUser(Regs[I])) 648 MF->getRegInfo().disableCalleeSavedRegister(Regs[I]); 649 650 return Regs; 651 } 652 653 void MachineRegisterInfo::setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs) { 654 if (IsUpdatedCSRsInitialized) 655 UpdatedCSRs.clear(); 656 657 append_range(UpdatedCSRs, CSRs); 658 659 // Zero value represents the end of the register list 660 // (no more registers should be pushed). 661 UpdatedCSRs.push_back(0); 662 IsUpdatedCSRsInitialized = true; 663 } 664 665 bool MachineRegisterInfo::isReservedRegUnit(unsigned Unit) const { 666 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 667 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { 668 if (all_of(TRI->superregs_inclusive(*Root), 669 [&](MCPhysReg Super) { return isReserved(Super); })) 670 return true; 671 } 672 return false; 673 } 674