1 //===- lib/Codegen/MachineRegisterInfo.cpp --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MachineRegisterInfo class.
12 //===----------------------------------------------------------------------===//
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"
39 static cl::opt<bool> EnableSubRegLiveness("enable-subreg-liveness", cl::Hidden,
40 cl::init(true), cl::desc("Enable subregister liveness tracking."));
42 // Pin the vtable to this file.
43 void MachineRegisterInfo::Delegate::anchor() {}
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]());
56 /// setRegClass - Set the register class of the specified virtual register.
59 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
60 assert(RC && RC->isAllocatable() && "Invalid RC for virtual register");
61 VRegInfo[Reg].first = RC;
64 void MachineRegisterInfo::setRegBank(unsigned Reg,
65 const RegisterBank &RegBank) {
66 VRegInfo[Reg].first = &RegBank;
69 static const TargetRegisterClass *
70 constrainRegClass(MachineRegisterInfo &MRI, unsigned Reg,
71 const TargetRegisterClass *OldRC,
72 const TargetRegisterClass *RC, unsigned MinNumRegs) {
75 const TargetRegisterClass *NewRC =
76 MRI.getTargetRegisterInfo()->getCommonSubClass(OldRC, RC);
77 if (!NewRC || NewRC == OldRC)
79 if (NewRC->getNumRegs() < MinNumRegs)
81 MRI.setRegClass(Reg, NewRC);
85 const TargetRegisterClass *
86 MachineRegisterInfo::constrainRegClass(unsigned Reg,
87 const TargetRegisterClass *RC,
88 unsigned MinNumRegs) {
89 return ::constrainRegClass(*this, Reg, getRegClass(Reg), RC, MinNumRegs);
93 MachineRegisterInfo::constrainRegAttrs(unsigned Reg,
94 unsigned ConstrainingReg,
95 unsigned MinNumRegs) {
96 const LLT RegTy = getType(Reg);
97 const LLT ConstrainingRegTy = getType(ConstrainingReg);
98 if (RegTy.isValid() && ConstrainingRegTy.isValid() &&
99 RegTy != ConstrainingRegTy)
101 const auto ConstrainingRegCB = getRegClassOrRegBank(ConstrainingReg);
102 if (!ConstrainingRegCB.isNull()) {
103 const auto RegCB = getRegClassOrRegBank(Reg);
105 setRegClassOrRegBank(Reg, ConstrainingRegCB);
106 else if (RegCB.is<const TargetRegisterClass *>() !=
107 ConstrainingRegCB.is<const TargetRegisterClass *>())
109 else if (RegCB.is<const TargetRegisterClass *>()) {
110 if (!::constrainRegClass(
111 *this, Reg, RegCB.get<const TargetRegisterClass *>(),
112 ConstrainingRegCB.get<const TargetRegisterClass *>(), MinNumRegs))
114 } else if (RegCB != ConstrainingRegCB)
117 if (ConstrainingRegTy.isValid())
118 setType(Reg, ConstrainingRegTy);
123 MachineRegisterInfo::recomputeRegClass(unsigned Reg) {
124 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
125 const TargetRegisterClass *OldRC = getRegClass(Reg);
126 const TargetRegisterClass *NewRC =
127 getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC, *MF);
129 // Stop early if there is no room to grow.
133 // Accumulate constraints from all uses.
134 for (MachineOperand &MO : reg_nodbg_operands(Reg)) {
135 // Apply the effect of the given operand to NewRC.
136 MachineInstr *MI = MO.getParent();
137 unsigned OpNo = &MO - &MI->getOperand(0);
138 NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII,
139 getTargetRegisterInfo());
140 if (!NewRC || NewRC == OldRC)
143 setRegClass(Reg, NewRC);
147 unsigned MachineRegisterInfo::createIncompleteVirtualRegister(StringRef Name) {
148 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
150 RegAllocHints.grow(Reg);
151 insertVRegByName(Name, Reg);
155 /// createVirtualRegister - Create and return a new virtual register in the
156 /// function with the specified register class.
159 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass,
161 assert(RegClass && "Cannot create register without RegClass!");
162 assert(RegClass->isAllocatable() &&
163 "Virtual register RegClass must be allocatable.");
165 // New virtual register number.
166 unsigned Reg = createIncompleteVirtualRegister(Name);
167 VRegInfo[Reg].first = RegClass;
169 TheDelegate->MRI_NoteNewVirtualRegister(Reg);
173 unsigned MachineRegisterInfo::cloneVirtualRegister(unsigned VReg,
175 unsigned Reg = createIncompleteVirtualRegister(Name);
176 VRegInfo[Reg].first = VRegInfo[VReg].first;
177 setType(Reg, getType(VReg));
179 TheDelegate->MRI_NoteNewVirtualRegister(Reg);
183 void MachineRegisterInfo::setType(unsigned VReg, LLT Ty) {
184 VRegToType.grow(VReg);
185 VRegToType[VReg] = Ty;
189 MachineRegisterInfo::createGenericVirtualRegister(LLT Ty, StringRef Name) {
190 // New virtual register number.
191 unsigned Reg = createIncompleteVirtualRegister(Name);
192 // FIXME: Should we use a dummy register class?
193 VRegInfo[Reg].first = static_cast<RegisterBank *>(nullptr);
196 TheDelegate->MRI_NoteNewVirtualRegister(Reg);
200 void MachineRegisterInfo::clearVirtRegTypes() { VRegToType.clear(); }
202 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
203 void MachineRegisterInfo::clearVirtRegs() {
205 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
206 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
207 if (!VRegInfo[Reg].second)
210 llvm_unreachable("Remaining virtual register operands");
214 for (auto &I : LiveIns)
218 void MachineRegisterInfo::verifyUseList(unsigned Reg) const {
221 for (MachineOperand &M : reg_operands(Reg)) {
222 MachineOperand *MO = &M;
223 MachineInstr *MI = MO->getParent();
225 errs() << printReg(Reg, getTargetRegisterInfo())
226 << " use list MachineOperand " << MO
227 << " has no parent instruction.\n";
231 MachineOperand *MO0 = &MI->getOperand(0);
232 unsigned NumOps = MI->getNumOperands();
233 if (!(MO >= MO0 && MO < MO0+NumOps)) {
234 errs() << printReg(Reg, getTargetRegisterInfo())
235 << " use list MachineOperand " << MO
236 << " doesn't belong to parent MI: " << *MI;
240 errs() << printReg(Reg, getTargetRegisterInfo())
241 << " MachineOperand " << MO << ": " << *MO
242 << " is not a register\n";
245 if (MO->getReg() != Reg) {
246 errs() << printReg(Reg, getTargetRegisterInfo())
247 << " use-list MachineOperand " << MO << ": "
248 << *MO << " is the wrong register\n";
252 assert(Valid && "Invalid use list");
256 void MachineRegisterInfo::verifyUseLists() const {
258 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
259 verifyUseList(TargetRegisterInfo::index2VirtReg(i));
260 for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i)
265 /// Add MO to the linked list of operands for its register.
266 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
267 assert(!MO->isOnRegUseList() && "Already on list");
268 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
269 MachineOperand *const Head = HeadRef;
271 // Head points to the first list element.
272 // Next is NULL on the last list element.
273 // Prev pointers are circular, so Head->Prev == Last.
275 // Head is NULL for an empty list.
277 MO->Contents.Reg.Prev = MO;
278 MO->Contents.Reg.Next = nullptr;
282 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
284 // Insert MO between Last and Head in the circular Prev chain.
285 MachineOperand *Last = Head->Contents.Reg.Prev;
286 assert(Last && "Inconsistent use list");
287 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
288 Head->Contents.Reg.Prev = MO;
289 MO->Contents.Reg.Prev = Last;
291 // Def operands always precede uses. This allows def_iterator to stop early.
292 // Insert def operands at the front, and use operands at the back.
294 // Insert def at the front.
295 MO->Contents.Reg.Next = Head;
298 // Insert use at the end.
299 MO->Contents.Reg.Next = nullptr;
300 Last->Contents.Reg.Next = MO;
304 /// Remove MO from its use-def list.
305 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
306 assert(MO->isOnRegUseList() && "Operand not on use list");
307 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
308 MachineOperand *const Head = HeadRef;
309 assert(Head && "List already empty");
311 // Unlink this from the doubly linked list of operands.
312 MachineOperand *Next = MO->Contents.Reg.Next;
313 MachineOperand *Prev = MO->Contents.Reg.Prev;
315 // Prev links are circular, next link is NULL instead of looping back to Head.
319 Prev->Contents.Reg.Next = Next;
321 (Next ? Next : Head)->Contents.Reg.Prev = Prev;
323 MO->Contents.Reg.Prev = nullptr;
324 MO->Contents.Reg.Next = nullptr;
327 /// Move NumOps operands from Src to Dst, updating use-def lists as needed.
329 /// The Dst range is assumed to be uninitialized memory. (Or it may contain
330 /// operands that won't be destroyed, which is OK because the MO destructor is
333 /// The Src and Dst ranges may overlap.
334 void MachineRegisterInfo::moveOperands(MachineOperand *Dst,
337 assert(Src != Dst && NumOps && "Noop moveOperands");
339 // Copy backwards if Dst is within the Src range.
341 if (Dst >= Src && Dst < Src + NumOps) {
347 // Copy one operand at a time.
349 new (Dst) MachineOperand(*Src);
351 // Dst takes Src's place in the use-def chain.
353 MachineOperand *&Head = getRegUseDefListHead(Src->getReg());
354 MachineOperand *Prev = Src->Contents.Reg.Prev;
355 MachineOperand *Next = Src->Contents.Reg.Next;
356 assert(Head && "List empty, but operand is chained");
357 assert(Prev && "Operand was not on use-def list");
359 // Prev links are circular, next link is NULL instead of looping back to
364 Prev->Contents.Reg.Next = Dst;
366 // Update Prev pointer. This also works when Src was pointing to itself
367 // in a 1-element list. In that case Head == Dst.
368 (Next ? Next : Head)->Contents.Reg.Prev = Dst;
376 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
377 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
378 /// except that it also changes any definitions of the register as well.
379 /// If ToReg is a physical register we apply the sub register to obtain the
380 /// final/proper physical register.
381 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
382 assert(FromReg != ToReg && "Cannot replace a reg with itself");
384 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
386 // TODO: This could be more efficient by bulk changing the operands.
387 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
388 MachineOperand &O = *I;
390 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
391 O.substPhysReg(ToReg, *TRI);
398 /// getVRegDef - Return the machine instr that defines the specified virtual
399 /// register or null if none is found. This assumes that the code is in SSA
400 /// form, so there should only be one definition.
401 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
402 // Since we are in SSA form, we can use the first definition.
403 def_instr_iterator I = def_instr_begin(Reg);
404 assert((I.atEnd() || std::next(I) == def_instr_end()) &&
405 "getVRegDef assumes a single definition or no definition");
406 return !I.atEnd() ? &*I : nullptr;
409 /// getUniqueVRegDef - Return the unique machine instr that defines the
410 /// specified virtual register or null if none is found. If there are
411 /// multiple definitions or no definition, return null.
412 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
413 if (def_empty(Reg)) return nullptr;
414 def_instr_iterator I = def_instr_begin(Reg);
415 if (std::next(I) != def_instr_end())
420 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
421 use_nodbg_iterator UI = use_nodbg_begin(RegNo);
422 if (UI == use_nodbg_end())
424 return ++UI == use_nodbg_end();
427 /// clearKillFlags - Iterate over all the uses of the given register and
428 /// clear the kill flag from the MachineOperand. This function is used by
429 /// optimization passes which extend register lifetimes and need only
430 /// preserve conservative kill flag information.
431 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
432 for (MachineOperand &MO : use_operands(Reg))
436 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
437 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
438 if (I->first == Reg || I->second == Reg)
443 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
444 /// corresponding live-in physical register.
445 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
446 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
447 if (I->second == VReg)
452 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
453 /// corresponding live-in physical register.
454 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
455 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
456 if (I->first == PReg)
461 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
462 /// into the given entry block.
464 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
465 const TargetRegisterInfo &TRI,
466 const TargetInstrInfo &TII) {
467 // Emit the copies into the top of the block.
468 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
469 if (LiveIns[i].second) {
470 if (use_nodbg_empty(LiveIns[i].second)) {
471 // The livein has no non-dbg uses. Drop it.
473 // It would be preferable to have isel avoid creating live-in
474 // records for unused arguments in the first place, but it's
475 // complicated by the debug info code for arguments.
476 LiveIns.erase(LiveIns.begin() + i);
480 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
481 TII.get(TargetOpcode::COPY), LiveIns[i].second)
482 .addReg(LiveIns[i].first);
484 // Add the register to the entry block live-in set.
485 EntryMBB->addLiveIn(LiveIns[i].first);
488 // Add the register to the entry block live-in set.
489 EntryMBB->addLiveIn(LiveIns[i].first);
493 LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(unsigned Reg) const {
494 // Lane masks are only defined for vregs.
495 assert(TargetRegisterInfo::isVirtualRegister(Reg));
496 const TargetRegisterClass &TRC = *getRegClass(Reg);
497 return TRC.getLaneMask();
500 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
501 LLVM_DUMP_METHOD void MachineRegisterInfo::dumpUses(unsigned Reg) const {
502 for (MachineInstr &I : use_instructions(Reg))
507 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
508 ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF);
509 assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() &&
510 "Invalid ReservedRegs vector from target");
513 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg) const {
514 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
516 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
517 if (TRI->isConstantPhysReg(PhysReg))
520 // Check if any overlapping register is modified, or allocatable so it may be
522 for (MCRegAliasIterator AI(PhysReg, TRI, true);
524 if (!def_empty(*AI) || isAllocatable(*AI))
530 MachineRegisterInfo::isCallerPreservedOrConstPhysReg(unsigned PhysReg) const {
531 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
532 return isConstantPhysReg(PhysReg) ||
533 TRI->isCallerPreservedPhysReg(PhysReg, *MF);
536 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
537 /// specified register as undefined which causes the DBG_VALUE to be
538 /// deleted during LiveDebugVariables analysis.
539 void MachineRegisterInfo::markUsesInDebugValueAsUndef(unsigned Reg) const {
540 // Mark any DBG_VALUE that uses Reg as undef (but don't delete it.)
541 MachineRegisterInfo::use_instr_iterator nextI;
542 for (use_instr_iterator I = use_instr_begin(Reg), E = use_instr_end();
544 nextI = std::next(I); // I is invalidated by the setReg
545 MachineInstr *UseMI = &*I;
546 if (UseMI->isDebugValue())
547 UseMI->getOperand(0).setReg(0U);
551 static const Function *getCalledFunction(const MachineInstr &MI) {
552 for (const MachineOperand &MO : MI.operands()) {
555 const Function *Func = dyn_cast<Function>(MO.getGlobal());
562 static bool isNoReturnDef(const MachineOperand &MO) {
563 // Anything which is not a noreturn function is a real def.
564 const MachineInstr &MI = *MO.getParent();
567 const MachineBasicBlock &MBB = *MI.getParent();
568 if (!MBB.succ_empty())
570 const MachineFunction &MF = *MBB.getParent();
571 // We need to keep correct unwind information even if the function will
572 // not return, since the runtime may need it.
573 if (MF.getFunction().hasFnAttribute(Attribute::UWTable))
575 const Function *Called = getCalledFunction(MI);
576 return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) ||
577 !Called->hasFnAttribute(Attribute::NoUnwind));
580 bool MachineRegisterInfo::isPhysRegModified(unsigned PhysReg,
581 bool SkipNoReturnDef) const {
582 if (UsedPhysRegMask.test(PhysReg))
584 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
585 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
586 for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) {
587 if (!SkipNoReturnDef && isNoReturnDef(MO))
595 bool MachineRegisterInfo::isPhysRegUsed(unsigned PhysReg) const {
596 if (UsedPhysRegMask.test(PhysReg))
598 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
599 for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid();
601 if (!reg_nodbg_empty(*AliasReg))
607 void MachineRegisterInfo::disableCalleeSavedRegister(unsigned Reg) {
609 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
610 assert(Reg && (Reg < TRI->getNumRegs()) &&
611 "Trying to disable an invalid register");
613 if (!IsUpdatedCSRsInitialized) {
614 const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF);
615 for (const MCPhysReg *I = CSR; *I; ++I)
616 UpdatedCSRs.push_back(*I);
618 // Zero value represents the end of the register list
619 // (no more registers should be pushed).
620 UpdatedCSRs.push_back(0);
622 IsUpdatedCSRsInitialized = true;
625 // Remove the register (and its aliases from the list).
626 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
627 UpdatedCSRs.erase(std::remove(UpdatedCSRs.begin(), UpdatedCSRs.end(), *AI),
631 const MCPhysReg *MachineRegisterInfo::getCalleeSavedRegs() const {
632 if (IsUpdatedCSRsInitialized)
633 return UpdatedCSRs.data();
635 return getTargetRegisterInfo()->getCalleeSavedRegs(MF);
638 void MachineRegisterInfo::setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs) {
639 if (IsUpdatedCSRsInitialized)
642 for (MCPhysReg Reg : CSRs)
643 UpdatedCSRs.push_back(Reg);
645 // Zero value represents the end of the register list
646 // (no more registers should be pushed).
647 UpdatedCSRs.push_back(0);
648 IsUpdatedCSRsInitialized = true;
651 bool MachineRegisterInfo::isReservedRegUnit(unsigned Unit) const {
652 const TargetRegisterInfo *TRI = getTargetRegisterInfo();
653 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) {
654 bool IsRootReserved = true;
655 for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true);
656 Super.isValid(); ++Super) {
657 unsigned Reg = *Super;
658 if (!isReserved(Reg)) {
659 IsRootReserved = false;