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/Config/llvm-config.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <cassert>
36 
37 using namespace llvm;
38 
39 static cl::opt<bool> EnableSubRegLiveness("enable-subreg-liveness", cl::Hidden,
40   cl::init(true), cl::desc("Enable subregister liveness tracking."));
41 
42 // Pin the vtable to this file.
43 void MachineRegisterInfo::Delegate::anchor() {}
44 
45 MachineRegisterInfo::MachineRegisterInfo(MachineFunction *MF)
46     : MF(MF), TracksSubRegLiveness(MF->getSubtarget().enableSubRegLiveness() &&
47                                    EnableSubRegLiveness),
48       IsUpdatedCSRsInitialized(false) {
49   unsigned NumRegs = getTargetRegisterInfo()->getNumRegs();
50   VRegInfo.reserve(256);
51   RegAllocHints.reserve(256);
52   UsedPhysRegMask.resize(NumRegs);
53   PhysRegUseDefLists.reset(new MachineOperand*[NumRegs]());
54 }
55 
56 /// setRegClass - Set the register class of the specified virtual register.
57 ///
58 void
59 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
60   assert(RC && RC->isAllocatable() && "Invalid RC for virtual register");
61   VRegInfo[Reg].first = RC;
62 }
63 
64 void MachineRegisterInfo::setRegBank(unsigned Reg,
65                                      const RegisterBank &RegBank) {
66   VRegInfo[Reg].first = &RegBank;
67 }
68 
69 static const TargetRegisterClass *
70 constrainRegClass(MachineRegisterInfo &MRI, unsigned Reg,
71                   const TargetRegisterClass *OldRC,
72                   const TargetRegisterClass *RC, unsigned MinNumRegs) {
73   if (OldRC == RC)
74     return RC;
75   const TargetRegisterClass *NewRC =
76       MRI.getTargetRegisterInfo()->getCommonSubClass(OldRC, RC);
77   if (!NewRC || NewRC == OldRC)
78     return NewRC;
79   if (NewRC->getNumRegs() < MinNumRegs)
80     return nullptr;
81   MRI.setRegClass(Reg, NewRC);
82   return NewRC;
83 }
84 
85 const TargetRegisterClass *
86 MachineRegisterInfo::constrainRegClass(unsigned Reg,
87                                        const TargetRegisterClass *RC,
88                                        unsigned MinNumRegs) {
89   return ::constrainRegClass(*this, Reg, getRegClass(Reg), RC, MinNumRegs);
90 }
91 
92 bool
93 MachineRegisterInfo::constrainRegAttrs(unsigned Reg,
94                                        unsigned ConstrainingReg,
95                                        unsigned MinNumRegs) {
96   auto const *OldRC = getRegClassOrNull(Reg);
97   auto const *RC = getRegClassOrNull(ConstrainingReg);
98   // A virtual register at any point must have either a low-level type
99   // or a class assigned, but not both. The only exception is the internals of
100   // GlobalISel's instruction selection pass, which is allowed to temporarily
101   // introduce registers with types and classes both.
102   assert((OldRC || getType(Reg).isValid()) && "Reg has neither class nor type");
103   assert((!OldRC || !getType(Reg).isValid()) && "Reg has class and type both");
104   assert((RC || getType(ConstrainingReg).isValid()) &&
105          "ConstrainingReg has neither class nor type");
106   assert((!RC || !getType(ConstrainingReg).isValid()) &&
107          "ConstrainingReg has class and type both");
108   if (OldRC && RC)
109     return ::constrainRegClass(*this, Reg, OldRC, RC, MinNumRegs);
110   // If one of the virtual registers is generic (used in generic machine
111   // instructions, has a low-level type, doesn't have a class), and the other is
112   // concrete (used in target specific instructions, doesn't have a low-level
113   // type, has a class), we can not unify them.
114   if (OldRC || RC)
115     return false;
116   // At this point, both registers are guaranteed to have a valid low-level
117   // type, and they must agree.
118   if (getType(Reg) != getType(ConstrainingReg))
119     return false;
120   auto const *OldRB = getRegBankOrNull(Reg);
121   auto const *RB = getRegBankOrNull(ConstrainingReg);
122   if (OldRB)
123     return !RB || RB == OldRB;
124   if (RB)
125     setRegBank(Reg, *RB);
126   return true;
127 }
128 
129 bool
130 MachineRegisterInfo::recomputeRegClass(unsigned Reg) {
131   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
132   const TargetRegisterClass *OldRC = getRegClass(Reg);
133   const TargetRegisterClass *NewRC =
134       getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC, *MF);
135 
136   // Stop early if there is no room to grow.
137   if (NewRC == OldRC)
138     return false;
139 
140   // Accumulate constraints from all uses.
141   for (MachineOperand &MO : reg_nodbg_operands(Reg)) {
142     // Apply the effect of the given operand to NewRC.
143     MachineInstr *MI = MO.getParent();
144     unsigned OpNo = &MO - &MI->getOperand(0);
145     NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII,
146                                             getTargetRegisterInfo());
147     if (!NewRC || NewRC == OldRC)
148       return false;
149   }
150   setRegClass(Reg, NewRC);
151   return true;
152 }
153 
154 unsigned MachineRegisterInfo::createIncompleteVirtualRegister(StringRef Name) {
155   unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
156   VRegInfo.grow(Reg);
157   RegAllocHints.grow(Reg);
158   insertVRegByName(Name, Reg);
159   return Reg;
160 }
161 
162 /// createVirtualRegister - Create and return a new virtual register in the
163 /// function with the specified register class.
164 ///
165 unsigned
166 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass,
167                                            StringRef Name) {
168   assert(RegClass && "Cannot create register without RegClass!");
169   assert(RegClass->isAllocatable() &&
170          "Virtual register RegClass must be allocatable.");
171 
172   // New virtual register number.
173   unsigned Reg = createIncompleteVirtualRegister(Name);
174   VRegInfo[Reg].first = RegClass;
175   if (TheDelegate)
176     TheDelegate->MRI_NoteNewVirtualRegister(Reg);
177   return Reg;
178 }
179 
180 LLT MachineRegisterInfo::getType(unsigned VReg) const {
181   VRegToTypeMap::const_iterator TypeIt = getVRegToType().find(VReg);
182   return TypeIt != getVRegToType().end() ? TypeIt->second : LLT{};
183 }
184 
185 void MachineRegisterInfo::setType(unsigned VReg, LLT Ty) {
186   // Check that VReg doesn't have a class.
187   assert((getRegClassOrRegBank(VReg).isNull() ||
188          !getRegClassOrRegBank(VReg).is<const TargetRegisterClass *>()) &&
189          "Can't set the size of a non-generic virtual register");
190   getVRegToType()[VReg] = Ty;
191 }
192 
193 unsigned
194 MachineRegisterInfo::createGenericVirtualRegister(LLT Ty, StringRef Name) {
195   // New virtual register number.
196   unsigned Reg = createIncompleteVirtualRegister(Name);
197   // FIXME: Should we use a dummy register class?
198   VRegInfo[Reg].first = static_cast<RegisterBank *>(nullptr);
199   getVRegToType()[Reg] = Ty;
200   if (TheDelegate)
201     TheDelegate->MRI_NoteNewVirtualRegister(Reg);
202   return Reg;
203 }
204 
205 void MachineRegisterInfo::clearVirtRegTypes() {
206   getVRegToType().clear();
207 }
208 
209 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
210 void MachineRegisterInfo::clearVirtRegs() {
211 #ifndef NDEBUG
212   for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
213     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
214     if (!VRegInfo[Reg].second)
215       continue;
216     verifyUseList(Reg);
217     llvm_unreachable("Remaining virtual register operands");
218   }
219 #endif
220   VRegInfo.clear();
221   for (auto &I : LiveIns)
222     I.second = 0;
223 }
224 
225 void MachineRegisterInfo::verifyUseList(unsigned 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(TargetRegisterInfo::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(unsigned FromReg, unsigned 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 (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
395     MachineOperand &O = *I;
396     ++I;
397     if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
398       O.substPhysReg(ToReg, *TRI);
399     } else {
400       O.setReg(ToReg);
401     }
402   }
403 }
404 
405 /// getVRegDef - Return the machine instr that defines the specified virtual
406 /// register or null if none is found.  This assumes that the code is in SSA
407 /// form, so there should only be one definition.
408 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
409   // Since we are in SSA form, we can use the first definition.
410   def_instr_iterator I = def_instr_begin(Reg);
411   assert((I.atEnd() || std::next(I) == def_instr_end()) &&
412          "getVRegDef assumes a single definition or no definition");
413   return !I.atEnd() ? &*I : nullptr;
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(unsigned 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(unsigned RegNo) const {
428   use_nodbg_iterator UI = use_nodbg_begin(RegNo);
429   if (UI == use_nodbg_end())
430     return false;
431   return ++UI == use_nodbg_end();
432 }
433 
434 /// clearKillFlags - Iterate over all the uses of the given register and
435 /// clear the kill flag from the MachineOperand. This function is used by
436 /// optimization passes which extend register lifetimes and need only
437 /// preserve conservative kill flag information.
438 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
439   for (MachineOperand &MO : use_operands(Reg))
440     MO.setIsKill(false);
441 }
442 
443 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
444   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
445     if (I->first == Reg || I->second == Reg)
446       return true;
447   return false;
448 }
449 
450 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
451 /// corresponding live-in physical register.
452 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
453   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
454     if (I->second == VReg)
455       return I->first;
456   return 0;
457 }
458 
459 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
460 /// corresponding live-in physical register.
461 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
462   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
463     if (I->first == PReg)
464       return I->second;
465   return 0;
466 }
467 
468 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
469 /// into the given entry block.
470 void
471 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
472                                       const TargetRegisterInfo &TRI,
473                                       const TargetInstrInfo &TII) {
474   // Emit the copies into the top of the block.
475   for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
476     if (LiveIns[i].second) {
477       if (use_nodbg_empty(LiveIns[i].second)) {
478         // The livein has no non-dbg uses. Drop it.
479         //
480         // It would be preferable to have isel avoid creating live-in
481         // records for unused arguments in the first place, but it's
482         // complicated by the debug info code for arguments.
483         LiveIns.erase(LiveIns.begin() + i);
484         --i; --e;
485       } else {
486         // Emit a copy.
487         BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
488                 TII.get(TargetOpcode::COPY), LiveIns[i].second)
489           .addReg(LiveIns[i].first);
490 
491         // Add the register to the entry block live-in set.
492         EntryMBB->addLiveIn(LiveIns[i].first);
493       }
494     } else {
495       // Add the register to the entry block live-in set.
496       EntryMBB->addLiveIn(LiveIns[i].first);
497     }
498 }
499 
500 LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(unsigned Reg) const {
501   // Lane masks are only defined for vregs.
502   assert(TargetRegisterInfo::isVirtualRegister(Reg));
503   const TargetRegisterClass &TRC = *getRegClass(Reg);
504   return TRC.getLaneMask();
505 }
506 
507 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
508 LLVM_DUMP_METHOD void MachineRegisterInfo::dumpUses(unsigned Reg) const {
509   for (MachineInstr &I : use_instructions(Reg))
510     I.dump();
511 }
512 #endif
513 
514 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
515   ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF);
516   assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() &&
517          "Invalid ReservedRegs vector from target");
518 }
519 
520 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg) const {
521   assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
522 
523   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
524   if (TRI->isConstantPhysReg(PhysReg))
525     return true;
526 
527   // Check if any overlapping register is modified, or allocatable so it may be
528   // used later.
529   for (MCRegAliasIterator AI(PhysReg, TRI, true);
530        AI.isValid(); ++AI)
531     if (!def_empty(*AI) || isAllocatable(*AI))
532       return false;
533   return true;
534 }
535 
536 bool
537 MachineRegisterInfo::isCallerPreservedOrConstPhysReg(unsigned PhysReg) const {
538   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
539   return isConstantPhysReg(PhysReg) ||
540       TRI->isCallerPreservedPhysReg(PhysReg, *MF);
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(unsigned Reg) const {
547   // Mark any DBG_VALUE that uses Reg as undef (but don't delete it.)
548   MachineRegisterInfo::use_instr_iterator nextI;
549   for (use_instr_iterator I = use_instr_begin(Reg), E = use_instr_end();
550        I != E; I = nextI) {
551     nextI = std::next(I);  // I is invalidated by the setReg
552     MachineInstr *UseMI = &*I;
553     if (UseMI->isDebugValue())
554       UseMI->getOperand(0).setReg(0U);
555   }
556 }
557 
558 static const Function *getCalledFunction(const MachineInstr &MI) {
559   for (const MachineOperand &MO : MI.operands()) {
560     if (!MO.isGlobal())
561       continue;
562     const Function *Func = dyn_cast<Function>(MO.getGlobal());
563     if (Func != nullptr)
564       return Func;
565   }
566   return nullptr;
567 }
568 
569 static bool isNoReturnDef(const MachineOperand &MO) {
570   // Anything which is not a noreturn function is a real def.
571   const MachineInstr &MI = *MO.getParent();
572   if (!MI.isCall())
573     return false;
574   const MachineBasicBlock &MBB = *MI.getParent();
575   if (!MBB.succ_empty())
576     return false;
577   const MachineFunction &MF = *MBB.getParent();
578   // We need to keep correct unwind information even if the function will
579   // not return, since the runtime may need it.
580   if (MF.getFunction().hasFnAttribute(Attribute::UWTable))
581     return false;
582   const Function *Called = getCalledFunction(MI);
583   return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) ||
584            !Called->hasFnAttribute(Attribute::NoUnwind));
585 }
586 
587 bool MachineRegisterInfo::isPhysRegModified(unsigned PhysReg,
588                                             bool SkipNoReturnDef) const {
589   if (UsedPhysRegMask.test(PhysReg))
590     return true;
591   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
592   for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
593     for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) {
594       if (!SkipNoReturnDef && isNoReturnDef(MO))
595         continue;
596       return true;
597     }
598   }
599   return false;
600 }
601 
602 bool MachineRegisterInfo::isPhysRegUsed(unsigned PhysReg) const {
603   if (UsedPhysRegMask.test(PhysReg))
604     return true;
605   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
606   for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid();
607        ++AliasReg) {
608     if (!reg_nodbg_empty(*AliasReg))
609       return true;
610   }
611   return false;
612 }
613 
614 void MachineRegisterInfo::disableCalleeSavedRegister(unsigned Reg) {
615 
616   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
617   assert(Reg && (Reg < TRI->getNumRegs()) &&
618          "Trying to disable an invalid register");
619 
620   if (!IsUpdatedCSRsInitialized) {
621     const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF);
622     for (const MCPhysReg *I = CSR; *I; ++I)
623       UpdatedCSRs.push_back(*I);
624 
625     // Zero value represents the end of the register list
626     // (no more registers should be pushed).
627     UpdatedCSRs.push_back(0);
628 
629     IsUpdatedCSRsInitialized = true;
630   }
631 
632   // Remove the register (and its aliases from the list).
633   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
634     UpdatedCSRs.erase(std::remove(UpdatedCSRs.begin(), UpdatedCSRs.end(), *AI),
635                       UpdatedCSRs.end());
636 }
637 
638 const MCPhysReg *MachineRegisterInfo::getCalleeSavedRegs() const {
639   if (IsUpdatedCSRsInitialized)
640     return UpdatedCSRs.data();
641 
642   return getTargetRegisterInfo()->getCalleeSavedRegs(MF);
643 }
644 
645 void MachineRegisterInfo::setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs) {
646   if (IsUpdatedCSRsInitialized)
647     UpdatedCSRs.clear();
648 
649   for (MCPhysReg Reg : CSRs)
650     UpdatedCSRs.push_back(Reg);
651 
652   // Zero value represents the end of the register list
653   // (no more registers should be pushed).
654   UpdatedCSRs.push_back(0);
655   IsUpdatedCSRsInitialized = true;
656 }
657 
658 bool MachineRegisterInfo::isReservedRegUnit(unsigned Unit) const {
659   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
660   for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
661     bool IsRootReserved = true;
662     for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true);
663          Super.isValid(); ++Super) {
664       unsigned Reg = *Super;
665       if (!isReserved(Reg)) {
666         IsRootReserved = false;
667         break;
668       }
669     }
670     if (IsRootReserved)
671       return true;
672   }
673   return false;
674 }
675