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