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