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