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/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/Target/TargetInstrInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Support/raw_os_ostream.h"
22 MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
23 : TRI(&TRI), IsSSA(true), TracksLiveness(true) {
24 VRegInfo.reserve(256);
25 RegAllocHints.reserve(256);
26 UsedRegUnits.resize(TRI.getNumRegUnits());
27 UsedPhysRegMask.resize(TRI.getNumRegs());
29 // Create the physreg use/def lists.
30 PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
31 memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
34 MachineRegisterInfo::~MachineRegisterInfo() {
35 delete [] PhysRegUseDefLists;
38 /// setRegClass - Set the register class of the specified virtual register.
41 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
42 assert(RC && RC->isAllocatable() && "Invalid RC for virtual register");
43 VRegInfo[Reg].first = RC;
46 const TargetRegisterClass *
47 MachineRegisterInfo::constrainRegClass(unsigned Reg,
48 const TargetRegisterClass *RC,
49 unsigned MinNumRegs) {
50 const TargetRegisterClass *OldRC = getRegClass(Reg);
53 const TargetRegisterClass *NewRC = TRI->getCommonSubClass(OldRC, RC);
54 if (!NewRC || NewRC == OldRC)
56 if (NewRC->getNumRegs() < MinNumRegs)
58 setRegClass(Reg, NewRC);
63 MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
64 const TargetInstrInfo *TII = TM.getInstrInfo();
65 const TargetRegisterClass *OldRC = getRegClass(Reg);
66 const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
68 // Stop early if there is no room to grow.
72 // Accumulate constraints from all uses.
73 for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
75 const TargetRegisterClass *OpRC =
76 I->getRegClassConstraint(I.getOperandNo(), TII, TRI);
77 if (unsigned SubIdx = I.getOperand().getSubReg()) {
79 NewRC = TRI->getMatchingSuperRegClass(NewRC, OpRC, SubIdx);
81 NewRC = TRI->getSubClassWithSubReg(NewRC, SubIdx);
83 NewRC = TRI->getCommonSubClass(NewRC, OpRC);
84 if (!NewRC || NewRC == OldRC)
87 setRegClass(Reg, NewRC);
91 /// createVirtualRegister - Create and return a new virtual register in the
92 /// function with the specified register class.
95 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
96 assert(RegClass && "Cannot create register without RegClass!");
97 assert(RegClass->isAllocatable() &&
98 "Virtual register RegClass must be allocatable.");
100 // New virtual register number.
101 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
103 VRegInfo[Reg].first = RegClass;
104 RegAllocHints.grow(Reg);
108 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
109 void MachineRegisterInfo::clearVirtRegs() {
111 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
112 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
113 if (!VRegInfo[Reg].second)
116 llvm_unreachable("Remaining virtual register operands");
122 void MachineRegisterInfo::verifyUseList(unsigned Reg) const {
125 for (reg_iterator I = reg_begin(Reg), E = reg_end(); I != E; ++I) {
126 MachineOperand *MO = &I.getOperand();
127 MachineInstr *MI = MO->getParent();
129 errs() << PrintReg(Reg, TRI) << " use list MachineOperand " << MO
130 << " has no parent instruction.\n";
133 MachineOperand *MO0 = &MI->getOperand(0);
134 unsigned NumOps = MI->getNumOperands();
135 if (!(MO >= MO0 && MO < MO0+NumOps)) {
136 errs() << PrintReg(Reg, TRI) << " use list MachineOperand " << MO
137 << " doesn't belong to parent MI: " << *MI;
141 errs() << PrintReg(Reg, TRI) << " MachineOperand " << MO << ": " << *MO
142 << " is not a register\n";
145 if (MO->getReg() != Reg) {
146 errs() << PrintReg(Reg, TRI) << " use-list MachineOperand " << MO << ": "
147 << *MO << " is the wrong register\n";
151 assert(Valid && "Invalid use list");
155 void MachineRegisterInfo::verifyUseLists() const {
157 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
158 verifyUseList(TargetRegisterInfo::index2VirtReg(i));
159 for (unsigned i = 1, e = TRI->getNumRegs(); i != e; ++i)
164 /// Add MO to the linked list of operands for its register.
165 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
166 assert(!MO->isOnRegUseList() && "Already on list");
167 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
168 MachineOperand *const Head = HeadRef;
170 // Head points to the first list element.
171 // Next is NULL on the last list element.
172 // Prev pointers are circular, so Head->Prev == Last.
174 // Head is NULL for an empty list.
176 MO->Contents.Reg.Prev = MO;
177 MO->Contents.Reg.Next = 0;
181 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
183 // Insert MO between Last and Head in the circular Prev chain.
184 MachineOperand *Last = Head->Contents.Reg.Prev;
185 assert(Last && "Inconsistent use list");
186 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
187 Head->Contents.Reg.Prev = MO;
188 MO->Contents.Reg.Prev = Last;
190 // Def operands always precede uses. This allows def_iterator to stop early.
191 // Insert def operands at the front, and use operands at the back.
193 // Insert def at the front.
194 MO->Contents.Reg.Next = Head;
197 // Insert use at the end.
198 MO->Contents.Reg.Next = 0;
199 Last->Contents.Reg.Next = MO;
203 /// Remove MO from its use-def list.
204 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
205 assert(MO->isOnRegUseList() && "Operand not on use list");
206 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
207 MachineOperand *const Head = HeadRef;
208 assert(Head && "List already empty");
210 // Unlink this from the doubly linked list of operands.
211 MachineOperand *Next = MO->Contents.Reg.Next;
212 MachineOperand *Prev = MO->Contents.Reg.Prev;
214 // Prev links are circular, next link is NULL instead of looping back to Head.
218 Prev->Contents.Reg.Next = Next;
220 (Next ? Next : Head)->Contents.Reg.Prev = Prev;
222 MO->Contents.Reg.Prev = 0;
223 MO->Contents.Reg.Next = 0;
226 /// Move NumOps operands from Src to Dst, updating use-def lists as needed.
228 /// The Dst range is assumed to be uninitialized memory. (Or it may contain
229 /// operands that won't be destroyed, which is OK because the MO destructor is
232 /// The Src and Dst ranges may overlap.
233 void MachineRegisterInfo::moveOperands(MachineOperand *Dst,
236 assert(Src != Dst && NumOps && "Noop moveOperands");
238 // Copy backwards if Dst is within the Src range.
240 if (Dst >= Src && Dst < Src + NumOps) {
246 // Copy one operand at a time.
248 new (Dst) MachineOperand(*Src);
250 // Dst takes Src's place in the use-def chain.
252 MachineOperand *&Head = getRegUseDefListHead(Src->getReg());
253 MachineOperand *Prev = Src->Contents.Reg.Prev;
254 MachineOperand *Next = Src->Contents.Reg.Next;
255 assert(Head && "List empty, but operand is chained");
256 assert(Prev && "Operand was not on use-def list");
258 // Prev links are circular, next link is NULL instead of looping back to
263 Prev->Contents.Reg.Next = Dst;
265 // Update Prev pointer. This also works when Src was pointing to itself
266 // in a 1-element list. In that case Head == Dst.
267 (Next ? Next : Head)->Contents.Reg.Prev = Dst;
275 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
276 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
277 /// except that it also changes any definitions of the register as well.
278 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
279 assert(FromReg != ToReg && "Cannot replace a reg with itself");
281 // TODO: This could be more efficient by bulk changing the operands.
282 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
283 MachineOperand &O = I.getOperand();
290 /// getVRegDef - Return the machine instr that defines the specified virtual
291 /// register or null if none is found. This assumes that the code is in SSA
292 /// form, so there should only be one definition.
293 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
294 // Since we are in SSA form, we can use the first definition.
295 def_iterator I = def_begin(Reg);
296 assert((I.atEnd() || llvm::next(I) == def_end()) &&
297 "getVRegDef assumes a single definition or no definition");
298 return !I.atEnd() ? &*I : 0;
301 /// getUniqueVRegDef - Return the unique machine instr that defines the
302 /// specified virtual register or null if none is found. If there are
303 /// multiple definitions or no definition, return null.
304 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
305 if (def_empty(Reg)) return 0;
306 def_iterator I = def_begin(Reg);
307 if (llvm::next(I) != def_end())
312 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
313 use_nodbg_iterator UI = use_nodbg_begin(RegNo);
314 if (UI == use_nodbg_end())
316 return ++UI == use_nodbg_end();
319 /// clearKillFlags - Iterate over all the uses of the given register and
320 /// clear the kill flag from the MachineOperand. This function is used by
321 /// optimization passes which extend register lifetimes and need only
322 /// preserve conservative kill flag information.
323 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
324 for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
325 UI.getOperand().setIsKill(false);
328 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
329 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
330 if (I->first == Reg || I->second == Reg)
335 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
336 /// corresponding live-in physical register.
337 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
338 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
339 if (I->second == VReg)
344 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
345 /// corresponding live-in physical register.
346 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
347 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
348 if (I->first == PReg)
353 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
354 /// into the given entry block.
356 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
357 const TargetRegisterInfo &TRI,
358 const TargetInstrInfo &TII) {
359 // Emit the copies into the top of the block.
360 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
361 if (LiveIns[i].second) {
362 if (use_empty(LiveIns[i].second)) {
363 // The livein has no uses. Drop it.
365 // It would be preferable to have isel avoid creating live-in
366 // records for unused arguments in the first place, but it's
367 // complicated by the debug info code for arguments.
368 LiveIns.erase(LiveIns.begin() + i);
372 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
373 TII.get(TargetOpcode::COPY), LiveIns[i].second)
374 .addReg(LiveIns[i].first);
376 // Add the register to the entry block live-in set.
377 EntryMBB->addLiveIn(LiveIns[i].first);
380 // Add the register to the entry block live-in set.
381 EntryMBB->addLiveIn(LiveIns[i].first);
386 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
387 for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
388 I.getOperand().getParent()->dump();
392 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
393 ReservedRegs = TRI->getReservedRegs(MF);
394 assert(ReservedRegs.size() == TRI->getNumRegs() &&
395 "Invalid ReservedRegs vector from target");
398 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
399 const MachineFunction &MF) const {
400 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
402 // Check if any overlapping register is modified, or allocatable so it may be
404 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
405 if (!def_empty(*AI) || isAllocatable(*AI))