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/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/Support/raw_os_ostream.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetSubtargetInfo.h"
21 
22 using namespace llvm;
23 
24 // Pin the vtable to this file.
anchor()25 void MachineRegisterInfo::Delegate::anchor() {}
26 
MachineRegisterInfo(const MachineFunction * MF)27 MachineRegisterInfo::MachineRegisterInfo(const MachineFunction *MF)
28   : MF(MF), TheDelegate(nullptr), IsSSA(true), TracksLiveness(true),
29     TracksSubRegLiveness(false) {
30   unsigned NumRegs = getTargetRegisterInfo()->getNumRegs();
31   VRegInfo.reserve(256);
32   RegAllocHints.reserve(256);
33   UsedPhysRegMask.resize(NumRegs);
34   PhysRegUseDefLists.reset(new MachineOperand*[NumRegs]());
35 }
36 
37 /// setRegClass - Set the register class of the specified virtual register.
38 ///
39 void
setRegClass(unsigned Reg,const TargetRegisterClass * RC)40 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
41   assert(RC && RC->isAllocatable() && "Invalid RC for virtual register");
42   VRegInfo[Reg].first = RC;
43 }
44 
45 const TargetRegisterClass *
constrainRegClass(unsigned Reg,const TargetRegisterClass * RC,unsigned MinNumRegs)46 MachineRegisterInfo::constrainRegClass(unsigned Reg,
47                                        const TargetRegisterClass *RC,
48                                        unsigned MinNumRegs) {
49   const TargetRegisterClass *OldRC = getRegClass(Reg);
50   if (OldRC == RC)
51     return RC;
52   const TargetRegisterClass *NewRC =
53     getTargetRegisterInfo()->getCommonSubClass(OldRC, RC);
54   if (!NewRC || NewRC == OldRC)
55     return NewRC;
56   if (NewRC->getNumRegs() < MinNumRegs)
57     return nullptr;
58   setRegClass(Reg, NewRC);
59   return NewRC;
60 }
61 
62 bool
recomputeRegClass(unsigned Reg)63 MachineRegisterInfo::recomputeRegClass(unsigned Reg) {
64   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
65   const TargetRegisterClass *OldRC = getRegClass(Reg);
66   const TargetRegisterClass *NewRC =
67       getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC, *MF);
68 
69   // Stop early if there is no room to grow.
70   if (NewRC == OldRC)
71     return false;
72 
73   // Accumulate constraints from all uses.
74   for (MachineOperand &MO : reg_nodbg_operands(Reg)) {
75     // Apply the effect of the given operand to NewRC.
76     MachineInstr *MI = MO.getParent();
77     unsigned OpNo = &MO - &MI->getOperand(0);
78     NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII,
79                                             getTargetRegisterInfo());
80     if (!NewRC || NewRC == OldRC)
81       return false;
82   }
83   setRegClass(Reg, NewRC);
84   return true;
85 }
86 
87 /// createVirtualRegister - Create and return a new virtual register in the
88 /// function with the specified register class.
89 ///
90 unsigned
createVirtualRegister(const TargetRegisterClass * RegClass)91 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
92   assert(RegClass && "Cannot create register without RegClass!");
93   assert(RegClass->isAllocatable() &&
94          "Virtual register RegClass must be allocatable.");
95 
96   // New virtual register number.
97   unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
98   VRegInfo.grow(Reg);
99   VRegInfo[Reg].first = RegClass;
100   RegAllocHints.grow(Reg);
101   if (TheDelegate)
102     TheDelegate->MRI_NoteNewVirtualRegister(Reg);
103   return Reg;
104 }
105 
106 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
clearVirtRegs()107 void MachineRegisterInfo::clearVirtRegs() {
108 #ifndef NDEBUG
109   for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
110     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
111     if (!VRegInfo[Reg].second)
112       continue;
113     verifyUseList(Reg);
114     llvm_unreachable("Remaining virtual register operands");
115   }
116 #endif
117   VRegInfo.clear();
118   for (auto &I : LiveIns)
119     I.second = 0;
120 }
121 
verifyUseList(unsigned Reg) const122 void MachineRegisterInfo::verifyUseList(unsigned Reg) const {
123 #ifndef NDEBUG
124   bool Valid = true;
125   for (MachineOperand &M : reg_operands(Reg)) {
126     MachineOperand *MO = &M;
127     MachineInstr *MI = MO->getParent();
128     if (!MI) {
129       errs() << PrintReg(Reg, getTargetRegisterInfo())
130              << " use list MachineOperand " << MO
131              << " has no parent instruction.\n";
132       Valid = false;
133       continue;
134     }
135     MachineOperand *MO0 = &MI->getOperand(0);
136     unsigned NumOps = MI->getNumOperands();
137     if (!(MO >= MO0 && MO < MO0+NumOps)) {
138       errs() << PrintReg(Reg, getTargetRegisterInfo())
139              << " use list MachineOperand " << MO
140              << " doesn't belong to parent MI: " << *MI;
141       Valid = false;
142     }
143     if (!MO->isReg()) {
144       errs() << PrintReg(Reg, getTargetRegisterInfo())
145              << " MachineOperand " << MO << ": " << *MO
146              << " is not a register\n";
147       Valid = false;
148     }
149     if (MO->getReg() != Reg) {
150       errs() << PrintReg(Reg, getTargetRegisterInfo())
151              << " use-list MachineOperand " << MO << ": "
152              << *MO << " is the wrong register\n";
153       Valid = false;
154     }
155   }
156   assert(Valid && "Invalid use list");
157 #endif
158 }
159 
verifyUseLists() const160 void MachineRegisterInfo::verifyUseLists() const {
161 #ifndef NDEBUG
162   for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
163     verifyUseList(TargetRegisterInfo::index2VirtReg(i));
164   for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i)
165     verifyUseList(i);
166 #endif
167 }
168 
169 /// Add MO to the linked list of operands for its register.
addRegOperandToUseList(MachineOperand * MO)170 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
171   assert(!MO->isOnRegUseList() && "Already on list");
172   MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
173   MachineOperand *const Head = HeadRef;
174 
175   // Head points to the first list element.
176   // Next is NULL on the last list element.
177   // Prev pointers are circular, so Head->Prev == Last.
178 
179   // Head is NULL for an empty list.
180   if (!Head) {
181     MO->Contents.Reg.Prev = MO;
182     MO->Contents.Reg.Next = nullptr;
183     HeadRef = MO;
184     return;
185   }
186   assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
187 
188   // Insert MO between Last and Head in the circular Prev chain.
189   MachineOperand *Last = Head->Contents.Reg.Prev;
190   assert(Last && "Inconsistent use list");
191   assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
192   Head->Contents.Reg.Prev = MO;
193   MO->Contents.Reg.Prev = Last;
194 
195   // Def operands always precede uses. This allows def_iterator to stop early.
196   // Insert def operands at the front, and use operands at the back.
197   if (MO->isDef()) {
198     // Insert def at the front.
199     MO->Contents.Reg.Next = Head;
200     HeadRef = MO;
201   } else {
202     // Insert use at the end.
203     MO->Contents.Reg.Next = nullptr;
204     Last->Contents.Reg.Next = MO;
205   }
206 }
207 
208 /// Remove MO from its use-def list.
removeRegOperandFromUseList(MachineOperand * MO)209 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
210   assert(MO->isOnRegUseList() && "Operand not on use list");
211   MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
212   MachineOperand *const Head = HeadRef;
213   assert(Head && "List already empty");
214 
215   // Unlink this from the doubly linked list of operands.
216   MachineOperand *Next = MO->Contents.Reg.Next;
217   MachineOperand *Prev = MO->Contents.Reg.Prev;
218 
219   // Prev links are circular, next link is NULL instead of looping back to Head.
220   if (MO == Head)
221     HeadRef = Next;
222   else
223     Prev->Contents.Reg.Next = Next;
224 
225   (Next ? Next : Head)->Contents.Reg.Prev = Prev;
226 
227   MO->Contents.Reg.Prev = nullptr;
228   MO->Contents.Reg.Next = nullptr;
229 }
230 
231 /// Move NumOps operands from Src to Dst, updating use-def lists as needed.
232 ///
233 /// The Dst range is assumed to be uninitialized memory. (Or it may contain
234 /// operands that won't be destroyed, which is OK because the MO destructor is
235 /// trivial anyway).
236 ///
237 /// The Src and Dst ranges may overlap.
moveOperands(MachineOperand * Dst,MachineOperand * Src,unsigned NumOps)238 void MachineRegisterInfo::moveOperands(MachineOperand *Dst,
239                                        MachineOperand *Src,
240                                        unsigned NumOps) {
241   assert(Src != Dst && NumOps && "Noop moveOperands");
242 
243   // Copy backwards if Dst is within the Src range.
244   int Stride = 1;
245   if (Dst >= Src && Dst < Src + NumOps) {
246     Stride = -1;
247     Dst += NumOps - 1;
248     Src += NumOps - 1;
249   }
250 
251   // Copy one operand at a time.
252   do {
253     new (Dst) MachineOperand(*Src);
254 
255     // Dst takes Src's place in the use-def chain.
256     if (Src->isReg()) {
257       MachineOperand *&Head = getRegUseDefListHead(Src->getReg());
258       MachineOperand *Prev = Src->Contents.Reg.Prev;
259       MachineOperand *Next = Src->Contents.Reg.Next;
260       assert(Head && "List empty, but operand is chained");
261       assert(Prev && "Operand was not on use-def list");
262 
263       // Prev links are circular, next link is NULL instead of looping back to
264       // Head.
265       if (Src == Head)
266         Head = Dst;
267       else
268         Prev->Contents.Reg.Next = Dst;
269 
270       // Update Prev pointer. This also works when Src was pointing to itself
271       // in a 1-element list. In that case Head == Dst.
272       (Next ? Next : Head)->Contents.Reg.Prev = Dst;
273     }
274 
275     Dst += Stride;
276     Src += Stride;
277   } while (--NumOps);
278 }
279 
280 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
281 /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
282 /// except that it also changes any definitions of the register as well.
283 /// If ToReg is a physical register we apply the sub register to obtain the
284 /// final/proper physical register.
replaceRegWith(unsigned FromReg,unsigned ToReg)285 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
286   assert(FromReg != ToReg && "Cannot replace a reg with itself");
287 
288   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
289 
290   // TODO: This could be more efficient by bulk changing the operands.
291   for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
292     MachineOperand &O = *I;
293     ++I;
294     if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
295       O.substPhysReg(ToReg, *TRI);
296     } else {
297       O.setReg(ToReg);
298     }
299   }
300 }
301 
302 /// getVRegDef - Return the machine instr that defines the specified virtual
303 /// register or null if none is found.  This assumes that the code is in SSA
304 /// form, so there should only be one definition.
getVRegDef(unsigned Reg) const305 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
306   // Since we are in SSA form, we can use the first definition.
307   def_instr_iterator I = def_instr_begin(Reg);
308   assert((I.atEnd() || std::next(I) == def_instr_end()) &&
309          "getVRegDef assumes a single definition or no definition");
310   return !I.atEnd() ? &*I : nullptr;
311 }
312 
313 /// getUniqueVRegDef - Return the unique machine instr that defines the
314 /// specified virtual register or null if none is found.  If there are
315 /// multiple definitions or no definition, return null.
getUniqueVRegDef(unsigned Reg) const316 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
317   if (def_empty(Reg)) return nullptr;
318   def_instr_iterator I = def_instr_begin(Reg);
319   if (std::next(I) != def_instr_end())
320     return nullptr;
321   return &*I;
322 }
323 
hasOneNonDBGUse(unsigned RegNo) const324 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
325   use_nodbg_iterator UI = use_nodbg_begin(RegNo);
326   if (UI == use_nodbg_end())
327     return false;
328   return ++UI == use_nodbg_end();
329 }
330 
331 /// clearKillFlags - Iterate over all the uses of the given register and
332 /// clear the kill flag from the MachineOperand. This function is used by
333 /// optimization passes which extend register lifetimes and need only
334 /// preserve conservative kill flag information.
clearKillFlags(unsigned Reg) const335 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
336   for (MachineOperand &MO : use_operands(Reg))
337     MO.setIsKill(false);
338 }
339 
isLiveIn(unsigned Reg) const340 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
341   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
342     if (I->first == Reg || I->second == Reg)
343       return true;
344   return false;
345 }
346 
347 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
348 /// corresponding live-in physical register.
getLiveInPhysReg(unsigned VReg) const349 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
350   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
351     if (I->second == VReg)
352       return I->first;
353   return 0;
354 }
355 
356 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
357 /// corresponding live-in physical register.
getLiveInVirtReg(unsigned PReg) const358 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
359   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
360     if (I->first == PReg)
361       return I->second;
362   return 0;
363 }
364 
365 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
366 /// into the given entry block.
367 void
EmitLiveInCopies(MachineBasicBlock * EntryMBB,const TargetRegisterInfo & TRI,const TargetInstrInfo & TII)368 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
369                                       const TargetRegisterInfo &TRI,
370                                       const TargetInstrInfo &TII) {
371   // Emit the copies into the top of the block.
372   for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
373     if (LiveIns[i].second) {
374       if (use_empty(LiveIns[i].second)) {
375         // The livein has no uses. Drop it.
376         //
377         // It would be preferable to have isel avoid creating live-in
378         // records for unused arguments in the first place, but it's
379         // complicated by the debug info code for arguments.
380         LiveIns.erase(LiveIns.begin() + i);
381         --i; --e;
382       } else {
383         // Emit a copy.
384         BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
385                 TII.get(TargetOpcode::COPY), LiveIns[i].second)
386           .addReg(LiveIns[i].first);
387 
388         // Add the register to the entry block live-in set.
389         EntryMBB->addLiveIn(LiveIns[i].first);
390       }
391     } else {
392       // Add the register to the entry block live-in set.
393       EntryMBB->addLiveIn(LiveIns[i].first);
394     }
395 }
396 
getMaxLaneMaskForVReg(unsigned Reg) const397 LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(unsigned Reg) const {
398   // Lane masks are only defined for vregs.
399   assert(TargetRegisterInfo::isVirtualRegister(Reg));
400   const TargetRegisterClass &TRC = *getRegClass(Reg);
401   return TRC.getLaneMask();
402 }
403 
404 #ifndef NDEBUG
dumpUses(unsigned Reg) const405 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
406   for (MachineInstr &I : use_instructions(Reg))
407     I.dump();
408 }
409 #endif
410 
freezeReservedRegs(const MachineFunction & MF)411 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
412   ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF);
413   assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() &&
414          "Invalid ReservedRegs vector from target");
415 }
416 
isConstantPhysReg(unsigned PhysReg,const MachineFunction & MF) const417 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
418                                             const MachineFunction &MF) const {
419   assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
420 
421   // Check if any overlapping register is modified, or allocatable so it may be
422   // used later.
423   for (MCRegAliasIterator AI(PhysReg, getTargetRegisterInfo(), true);
424        AI.isValid(); ++AI)
425     if (!def_empty(*AI) || isAllocatable(*AI))
426       return false;
427   return true;
428 }
429 
430 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
431 /// specified register as undefined which causes the DBG_VALUE to be
432 /// deleted during LiveDebugVariables analysis.
markUsesInDebugValueAsUndef(unsigned Reg) const433 void MachineRegisterInfo::markUsesInDebugValueAsUndef(unsigned Reg) const {
434   // Mark any DBG_VALUE that uses Reg as undef (but don't delete it.)
435   MachineRegisterInfo::use_instr_iterator nextI;
436   for (use_instr_iterator I = use_instr_begin(Reg), E = use_instr_end();
437        I != E; I = nextI) {
438     nextI = std::next(I);  // I is invalidated by the setReg
439     MachineInstr *UseMI = &*I;
440     if (UseMI->isDebugValue())
441       UseMI->getOperand(0).setReg(0U);
442   }
443 }
444 
getCalledFunction(const MachineInstr & MI)445 static const Function *getCalledFunction(const MachineInstr &MI) {
446   for (const MachineOperand &MO : MI.operands()) {
447     if (!MO.isGlobal())
448       continue;
449     const Function *Func = dyn_cast<Function>(MO.getGlobal());
450     if (Func != nullptr)
451       return Func;
452   }
453   return nullptr;
454 }
455 
isNoReturnDef(const MachineOperand & MO)456 static bool isNoReturnDef(const MachineOperand &MO) {
457   // Anything which is not a noreturn function is a real def.
458   const MachineInstr &MI = *MO.getParent();
459   if (!MI.isCall())
460     return false;
461   const MachineBasicBlock &MBB = *MI.getParent();
462   if (!MBB.succ_empty())
463     return false;
464   const MachineFunction &MF = *MBB.getParent();
465   // We need to keep correct unwind information even if the function will
466   // not return, since the runtime may need it.
467   if (MF.getFunction()->hasFnAttribute(Attribute::UWTable))
468     return false;
469   const Function *Called = getCalledFunction(MI);
470   return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) ||
471            !Called->hasFnAttribute(Attribute::NoUnwind));
472 }
473 
isPhysRegModified(unsigned PhysReg) const474 bool MachineRegisterInfo::isPhysRegModified(unsigned PhysReg) const {
475   if (UsedPhysRegMask.test(PhysReg))
476     return true;
477   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
478   for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
479     for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) {
480       if (isNoReturnDef(MO))
481         continue;
482       return true;
483     }
484   }
485   return false;
486 }
487 
isPhysRegUsed(unsigned PhysReg) const488 bool MachineRegisterInfo::isPhysRegUsed(unsigned PhysReg) const {
489   if (UsedPhysRegMask.test(PhysReg))
490     return true;
491   const TargetRegisterInfo *TRI = getTargetRegisterInfo();
492   for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid();
493        ++AliasReg) {
494     if (!reg_nodbg_empty(*AliasReg))
495       return true;
496   }
497   return false;
498 }
499