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 // Pin the vtable to this file.
23 void MachineRegisterInfo::Delegate::anchor() {}
25 MachineRegisterInfo::MachineRegisterInfo(const TargetMachine &TM)
26 : TM(TM), TheDelegate(0), IsSSA(true), TracksLiveness(true) {
27 VRegInfo.reserve(256);
28 RegAllocHints.reserve(256);
29 UsedRegUnits.resize(getTargetRegisterInfo()->getNumRegUnits());
30 UsedPhysRegMask.resize(getTargetRegisterInfo()->getNumRegs());
32 // Create the physreg use/def lists.
34 new MachineOperand*[getTargetRegisterInfo()->getNumRegs()];
35 memset(PhysRegUseDefLists, 0,
36 sizeof(MachineOperand*)*getTargetRegisterInfo()->getNumRegs());
39 MachineRegisterInfo::~MachineRegisterInfo() {
40 delete [] PhysRegUseDefLists;
43 /// setRegClass - Set the register class of the specified virtual register.
46 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
47 assert(RC && RC->isAllocatable() && "Invalid RC for virtual register");
48 VRegInfo[Reg].first = RC;
51 const TargetRegisterClass *
52 MachineRegisterInfo::constrainRegClass(unsigned Reg,
53 const TargetRegisterClass *RC,
54 unsigned MinNumRegs) {
55 const TargetRegisterClass *OldRC = getRegClass(Reg);
58 const TargetRegisterClass *NewRC =
59 getTargetRegisterInfo()->getCommonSubClass(OldRC, RC);
60 if (!NewRC || NewRC == OldRC)
62 if (NewRC->getNumRegs() < MinNumRegs)
64 setRegClass(Reg, NewRC);
69 MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
70 const TargetInstrInfo *TII = TM.getInstrInfo();
71 const TargetRegisterClass *OldRC = getRegClass(Reg);
72 const TargetRegisterClass *NewRC =
73 getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC);
75 // Stop early if there is no room to grow.
79 // Accumulate constraints from all uses.
80 for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
82 const TargetRegisterClass *OpRC =
83 I->getRegClassConstraint(I.getOperandNo(), TII,
84 getTargetRegisterInfo());
85 if (unsigned SubIdx = I.getOperand().getSubReg()) {
87 NewRC = getTargetRegisterInfo()->getMatchingSuperRegClass(NewRC, OpRC,
90 NewRC = getTargetRegisterInfo()->getSubClassWithSubReg(NewRC, SubIdx);
92 NewRC = getTargetRegisterInfo()->getCommonSubClass(NewRC, OpRC);
93 if (!NewRC || NewRC == OldRC)
96 setRegClass(Reg, NewRC);
100 /// createVirtualRegister - Create and return a new virtual register in the
101 /// function with the specified register class.
104 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
105 assert(RegClass && "Cannot create register without RegClass!");
106 assert(RegClass->isAllocatable() &&
107 "Virtual register RegClass must be allocatable.");
109 // New virtual register number.
110 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
112 VRegInfo[Reg].first = RegClass;
113 RegAllocHints.grow(Reg);
115 TheDelegate->MRI_NoteNewVirtualRegister(Reg);
119 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
120 void MachineRegisterInfo::clearVirtRegs() {
122 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
123 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
124 if (!VRegInfo[Reg].second)
127 llvm_unreachable("Remaining virtual register operands");
133 void MachineRegisterInfo::verifyUseList(unsigned Reg) const {
136 for (reg_iterator I = reg_begin(Reg), E = reg_end(); I != E; ++I) {
137 MachineOperand *MO = &I.getOperand();
138 MachineInstr *MI = MO->getParent();
140 errs() << PrintReg(Reg, getTargetRegisterInfo())
141 << " use list MachineOperand " << MO
142 << " has no parent instruction.\n";
145 MachineOperand *MO0 = &MI->getOperand(0);
146 unsigned NumOps = MI->getNumOperands();
147 if (!(MO >= MO0 && MO < MO0+NumOps)) {
148 errs() << PrintReg(Reg, getTargetRegisterInfo())
149 << " use list MachineOperand " << MO
150 << " doesn't belong to parent MI: " << *MI;
154 errs() << PrintReg(Reg, getTargetRegisterInfo())
155 << " MachineOperand " << MO << ": " << *MO
156 << " is not a register\n";
159 if (MO->getReg() != Reg) {
160 errs() << PrintReg(Reg, getTargetRegisterInfo())
161 << " use-list MachineOperand " << MO << ": "
162 << *MO << " is the wrong register\n";
166 assert(Valid && "Invalid use list");
170 void MachineRegisterInfo::verifyUseLists() const {
172 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
173 verifyUseList(TargetRegisterInfo::index2VirtReg(i));
174 for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i)
179 /// Add MO to the linked list of operands for its register.
180 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
181 assert(!MO->isOnRegUseList() && "Already on list");
182 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
183 MachineOperand *const Head = HeadRef;
185 // Head points to the first list element.
186 // Next is NULL on the last list element.
187 // Prev pointers are circular, so Head->Prev == Last.
189 // Head is NULL for an empty list.
191 MO->Contents.Reg.Prev = MO;
192 MO->Contents.Reg.Next = 0;
196 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
198 // Insert MO between Last and Head in the circular Prev chain.
199 MachineOperand *Last = Head->Contents.Reg.Prev;
200 assert(Last && "Inconsistent use list");
201 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
202 Head->Contents.Reg.Prev = MO;
203 MO->Contents.Reg.Prev = Last;
205 // Def operands always precede uses. This allows def_iterator to stop early.
206 // Insert def operands at the front, and use operands at the back.
208 // Insert def at the front.
209 MO->Contents.Reg.Next = Head;
212 // Insert use at the end.
213 MO->Contents.Reg.Next = 0;
214 Last->Contents.Reg.Next = MO;
218 /// Remove MO from its use-def list.
219 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
220 assert(MO->isOnRegUseList() && "Operand not on use list");
221 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
222 MachineOperand *const Head = HeadRef;
223 assert(Head && "List already empty");
225 // Unlink this from the doubly linked list of operands.
226 MachineOperand *Next = MO->Contents.Reg.Next;
227 MachineOperand *Prev = MO->Contents.Reg.Prev;
229 // Prev links are circular, next link is NULL instead of looping back to Head.
233 Prev->Contents.Reg.Next = Next;
235 (Next ? Next : Head)->Contents.Reg.Prev = Prev;
237 MO->Contents.Reg.Prev = 0;
238 MO->Contents.Reg.Next = 0;
241 /// Move NumOps operands from Src to Dst, updating use-def lists as needed.
243 /// The Dst range is assumed to be uninitialized memory. (Or it may contain
244 /// operands that won't be destroyed, which is OK because the MO destructor is
247 /// The Src and Dst ranges may overlap.
248 void MachineRegisterInfo::moveOperands(MachineOperand *Dst,
251 assert(Src != Dst && NumOps && "Noop moveOperands");
253 // Copy backwards if Dst is within the Src range.
255 if (Dst >= Src && Dst < Src + NumOps) {
261 // Copy one operand at a time.
263 new (Dst) MachineOperand(*Src);
265 // Dst takes Src's place in the use-def chain.
267 MachineOperand *&Head = getRegUseDefListHead(Src->getReg());
268 MachineOperand *Prev = Src->Contents.Reg.Prev;
269 MachineOperand *Next = Src->Contents.Reg.Next;
270 assert(Head && "List empty, but operand is chained");
271 assert(Prev && "Operand was not on use-def list");
273 // Prev links are circular, next link is NULL instead of looping back to
278 Prev->Contents.Reg.Next = Dst;
280 // Update Prev pointer. This also works when Src was pointing to itself
281 // in a 1-element list. In that case Head == Dst.
282 (Next ? Next : Head)->Contents.Reg.Prev = Dst;
290 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
291 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
292 /// except that it also changes any definitions of the register as well.
293 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
294 assert(FromReg != ToReg && "Cannot replace a reg with itself");
296 // TODO: This could be more efficient by bulk changing the operands.
297 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
298 MachineOperand &O = I.getOperand();
305 /// getVRegDef - Return the machine instr that defines the specified virtual
306 /// register or null if none is found. This assumes that the code is in SSA
307 /// form, so there should only be one definition.
308 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
309 // Since we are in SSA form, we can use the first definition.
310 def_iterator I = def_begin(Reg);
311 assert((I.atEnd() || llvm::next(I) == def_end()) &&
312 "getVRegDef assumes a single definition or no definition");
313 return !I.atEnd() ? &*I : 0;
316 /// getUniqueVRegDef - Return the unique machine instr that defines the
317 /// specified virtual register or null if none is found. If there are
318 /// multiple definitions or no definition, return null.
319 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
320 if (def_empty(Reg)) return 0;
321 def_iterator I = def_begin(Reg);
322 if (llvm::next(I) != def_end())
327 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
328 use_nodbg_iterator UI = use_nodbg_begin(RegNo);
329 if (UI == use_nodbg_end())
331 return ++UI == use_nodbg_end();
334 /// clearKillFlags - Iterate over all the uses of the given register and
335 /// clear the kill flag from the MachineOperand. This function is used by
336 /// optimization passes which extend register lifetimes and need only
337 /// preserve conservative kill flag information.
338 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
339 for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
340 UI.getOperand().setIsKill(false);
343 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
344 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
345 if (I->first == Reg || I->second == Reg)
350 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
351 /// corresponding live-in physical register.
352 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
353 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
354 if (I->second == VReg)
359 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
360 /// corresponding live-in physical register.
361 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
362 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
363 if (I->first == PReg)
368 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
369 /// into the given entry block.
371 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
372 const TargetRegisterInfo &TRI,
373 const TargetInstrInfo &TII) {
374 // Emit the copies into the top of the block.
375 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
376 if (LiveIns[i].second) {
377 if (use_empty(LiveIns[i].second)) {
378 // The livein has no uses. Drop it.
380 // It would be preferable to have isel avoid creating live-in
381 // records for unused arguments in the first place, but it's
382 // complicated by the debug info code for arguments.
383 LiveIns.erase(LiveIns.begin() + i);
387 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
388 TII.get(TargetOpcode::COPY), LiveIns[i].second)
389 .addReg(LiveIns[i].first);
391 // Add the register to the entry block live-in set.
392 EntryMBB->addLiveIn(LiveIns[i].first);
395 // Add the register to the entry block live-in set.
396 EntryMBB->addLiveIn(LiveIns[i].first);
401 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
402 for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
403 I.getOperand().getParent()->dump();
407 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
408 ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF);
409 assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() &&
410 "Invalid ReservedRegs vector from target");
413 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
414 const MachineFunction &MF) const {
415 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
417 // Check if any overlapping register is modified, or allocatable so it may be
419 for (MCRegAliasIterator AI(PhysReg, getTargetRegisterInfo(), true);
421 if (!def_empty(*AI) || isAllocatable(*AI))