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"
20 MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
21 : TRI(&TRI), IsSSA(true), TracksLiveness(true) {
22 VRegInfo.reserve(256);
23 RegAllocHints.reserve(256);
24 UsedRegUnits.resize(TRI.getNumRegUnits());
25 UsedPhysRegMask.resize(TRI.getNumRegs());
27 // Create the physreg use/def lists.
28 PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
29 memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
32 MachineRegisterInfo::~MachineRegisterInfo() {
35 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
36 assert(!PhysRegUseDefLists[i] &&
37 "PhysRegUseDefLists has entries after all instructions are deleted");
39 delete [] PhysRegUseDefLists;
42 /// setRegClass - Set the register class of the specified virtual register.
45 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
46 VRegInfo[Reg].first = RC;
49 const TargetRegisterClass *
50 MachineRegisterInfo::constrainRegClass(unsigned Reg,
51 const TargetRegisterClass *RC,
52 unsigned MinNumRegs) {
53 const TargetRegisterClass *OldRC = getRegClass(Reg);
56 const TargetRegisterClass *NewRC = TRI->getCommonSubClass(OldRC, RC);
57 if (!NewRC || NewRC == OldRC)
59 if (NewRC->getNumRegs() < MinNumRegs)
61 setRegClass(Reg, NewRC);
66 MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
67 const TargetInstrInfo *TII = TM.getInstrInfo();
68 const TargetRegisterClass *OldRC = getRegClass(Reg);
69 const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
71 // Stop early if there is no room to grow.
75 // Accumulate constraints from all uses.
76 for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
78 const TargetRegisterClass *OpRC =
79 I->getRegClassConstraint(I.getOperandNo(), TII, TRI);
80 if (unsigned SubIdx = I.getOperand().getSubReg()) {
82 NewRC = TRI->getMatchingSuperRegClass(NewRC, OpRC, SubIdx);
84 NewRC = TRI->getSubClassWithSubReg(NewRC, SubIdx);
86 NewRC = TRI->getCommonSubClass(NewRC, OpRC);
87 if (!NewRC || NewRC == OldRC)
90 setRegClass(Reg, NewRC);
94 /// createVirtualRegister - Create and return a new virtual register in the
95 /// function with the specified register class.
98 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
99 assert(RegClass && "Cannot create register without RegClass!");
100 assert(RegClass->isAllocatable() &&
101 "Virtual register RegClass must be allocatable.");
103 // New virtual register number.
104 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
106 VRegInfo[Reg].first = RegClass;
107 RegAllocHints.grow(Reg);
111 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
112 void MachineRegisterInfo::clearVirtRegs() {
114 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
115 assert(VRegInfo[TargetRegisterInfo::index2VirtReg(i)].second == 0 &&
116 "Vreg use list non-empty still?");
121 /// Add MO to the linked list of operands for its register.
122 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
123 assert(!MO->isOnRegUseList() && "Already on list");
124 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
125 MachineOperand *const Head = HeadRef;
127 // Head points to the first list element.
128 // Next is NULL on the last list element.
129 // Prev pointers are circular, so Head->Prev == Last.
131 // Head is NULL for an empty list.
133 MO->Contents.Reg.Prev = MO;
134 MO->Contents.Reg.Next = 0;
138 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
140 // Insert MO between Last and Head in the circular Prev chain.
141 MachineOperand *Last = Head->Contents.Reg.Prev;
142 assert(Last && "Inconsistent use list");
143 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!");
144 Head->Contents.Reg.Prev = MO;
145 MO->Contents.Reg.Prev = Last;
147 // Def operands always precede uses. This allows def_iterator to stop early.
148 // Insert def operands at the front, and use operands at the back.
150 // Insert def at the front.
151 MO->Contents.Reg.Next = Head;
154 // Insert use at the end.
155 MO->Contents.Reg.Next = 0;
156 Last->Contents.Reg.Next = MO;
160 /// Remove MO from its use-def list.
161 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) {
162 assert(MO->isOnRegUseList() && "Operand not on use list");
163 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg());
164 MachineOperand *const Head = HeadRef;
165 assert(Head && "List already empty");
167 // Unlink this from the doubly linked list of operands.
168 MachineOperand *Next = MO->Contents.Reg.Next;
169 MachineOperand *Prev = MO->Contents.Reg.Prev;
171 // Prev links are circular, next link is NULL instead of looping back to Head.
175 Prev->Contents.Reg.Next = Next;
177 (Next ? Next : Head)->Contents.Reg.Prev = Prev;
179 MO->Contents.Reg.Prev = 0;
180 MO->Contents.Reg.Next = 0;
183 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
184 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
185 /// except that it also changes any definitions of the register as well.
186 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
187 assert(FromReg != ToReg && "Cannot replace a reg with itself");
189 // TODO: This could be more efficient by bulk changing the operands.
190 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
191 MachineOperand &O = I.getOperand();
198 /// getVRegDef - Return the machine instr that defines the specified virtual
199 /// register or null if none is found. This assumes that the code is in SSA
200 /// form, so there should only be one definition.
201 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
202 // Since we are in SSA form, we can use the first definition.
203 def_iterator I = def_begin(Reg);
204 assert((I.atEnd() || llvm::next(I) == def_end()) &&
205 "getVRegDef assumes a single definition or no definition");
206 return !I.atEnd() ? &*I : 0;
209 /// getUniqueVRegDef - Return the unique machine instr that defines the
210 /// specified virtual register or null if none is found. If there are
211 /// multiple definitions or no definition, return null.
212 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const {
213 if (def_empty(Reg)) return 0;
214 def_iterator I = def_begin(Reg);
215 if (llvm::next(I) != def_end())
220 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
221 use_nodbg_iterator UI = use_nodbg_begin(RegNo);
222 if (UI == use_nodbg_end())
224 return ++UI == use_nodbg_end();
227 /// clearKillFlags - Iterate over all the uses of the given register and
228 /// clear the kill flag from the MachineOperand. This function is used by
229 /// optimization passes which extend register lifetimes and need only
230 /// preserve conservative kill flag information.
231 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
232 for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
233 UI.getOperand().setIsKill(false);
236 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
237 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
238 if (I->first == Reg || I->second == Reg)
243 bool MachineRegisterInfo::isLiveOut(unsigned Reg) const {
244 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
250 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
251 /// corresponding live-in physical register.
252 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
253 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
254 if (I->second == VReg)
259 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
260 /// corresponding live-in physical register.
261 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
262 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
263 if (I->first == PReg)
268 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
269 /// into the given entry block.
271 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
272 const TargetRegisterInfo &TRI,
273 const TargetInstrInfo &TII) {
274 // Emit the copies into the top of the block.
275 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
276 if (LiveIns[i].second) {
277 if (use_empty(LiveIns[i].second)) {
278 // The livein has no uses. Drop it.
280 // It would be preferable to have isel avoid creating live-in
281 // records for unused arguments in the first place, but it's
282 // complicated by the debug info code for arguments.
283 LiveIns.erase(LiveIns.begin() + i);
287 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
288 TII.get(TargetOpcode::COPY), LiveIns[i].second)
289 .addReg(LiveIns[i].first);
291 // Add the register to the entry block live-in set.
292 EntryMBB->addLiveIn(LiveIns[i].first);
295 // Add the register to the entry block live-in set.
296 EntryMBB->addLiveIn(LiveIns[i].first);
301 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
302 for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
303 I.getOperand().getParent()->dump();
307 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
308 ReservedRegs = TRI->getReservedRegs(MF);
309 assert(ReservedRegs.size() == TRI->getNumRegs() &&
310 "Invalid ReservedRegs vector from target");
313 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
314 const MachineFunction &MF) const {
315 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
317 // Check if any overlapping register is modified, or allocatable so it may be
319 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
320 if (!def_empty(*AI) || isAllocatable(*AI))