LLVM API Documentation
00001 //===-- lib/Codegen/MachineRegisterInfo.cpp -------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Implementation of the MachineRegisterInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/MachineRegisterInfo.h" 00015 #include "llvm/CodeGen/MachineInstrBuilder.h" 00016 #include "llvm/Support/raw_os_ostream.h" 00017 #include "llvm/Target/TargetInstrInfo.h" 00018 #include "llvm/Target/TargetMachine.h" 00019 #include "llvm/Target/TargetSubtargetInfo.h" 00020 00021 using namespace llvm; 00022 00023 // Pin the vtable to this file. 00024 void MachineRegisterInfo::Delegate::anchor() {} 00025 00026 MachineRegisterInfo::MachineRegisterInfo(const MachineFunction *MF) 00027 : MF(MF), TheDelegate(nullptr), IsSSA(true), TracksLiveness(true) { 00028 VRegInfo.reserve(256); 00029 RegAllocHints.reserve(256); 00030 UsedRegUnits.resize(getTargetRegisterInfo()->getNumRegUnits()); 00031 UsedPhysRegMask.resize(getTargetRegisterInfo()->getNumRegs()); 00032 00033 // Create the physreg use/def lists. 00034 PhysRegUseDefLists.resize(getTargetRegisterInfo()->getNumRegs(), nullptr); 00035 } 00036 00037 /// setRegClass - Set the register class of the specified virtual register. 00038 /// 00039 void 00040 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) { 00041 assert(RC && RC->isAllocatable() && "Invalid RC for virtual register"); 00042 VRegInfo[Reg].first = RC; 00043 } 00044 00045 const TargetRegisterClass * 00046 MachineRegisterInfo::constrainRegClass(unsigned Reg, 00047 const TargetRegisterClass *RC, 00048 unsigned MinNumRegs) { 00049 const TargetRegisterClass *OldRC = getRegClass(Reg); 00050 if (OldRC == RC) 00051 return RC; 00052 const TargetRegisterClass *NewRC = 00053 getTargetRegisterInfo()->getCommonSubClass(OldRC, RC); 00054 if (!NewRC || NewRC == OldRC) 00055 return NewRC; 00056 if (NewRC->getNumRegs() < MinNumRegs) 00057 return nullptr; 00058 setRegClass(Reg, NewRC); 00059 return NewRC; 00060 } 00061 00062 bool 00063 MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) { 00064 const TargetInstrInfo *TII = TM.getSubtargetImpl()->getInstrInfo(); 00065 const TargetRegisterClass *OldRC = getRegClass(Reg); 00066 const TargetRegisterClass *NewRC = 00067 getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC); 00068 00069 // Stop early if there is no room to grow. 00070 if (NewRC == OldRC) 00071 return false; 00072 00073 // Accumulate constraints from all uses. 00074 for (MachineOperand &MO : reg_nodbg_operands(Reg)) { 00075 // Apply the effect of the given operand to NewRC. 00076 MachineInstr *MI = MO.getParent(); 00077 unsigned OpNo = &MO - &MI->getOperand(0); 00078 NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII, 00079 getTargetRegisterInfo()); 00080 if (!NewRC || NewRC == OldRC) 00081 return false; 00082 } 00083 setRegClass(Reg, NewRC); 00084 return true; 00085 } 00086 00087 /// createVirtualRegister - Create and return a new virtual register in the 00088 /// function with the specified register class. 00089 /// 00090 unsigned 00091 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){ 00092 assert(RegClass && "Cannot create register without RegClass!"); 00093 assert(RegClass->isAllocatable() && 00094 "Virtual register RegClass must be allocatable."); 00095 00096 // New virtual register number. 00097 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs()); 00098 VRegInfo.grow(Reg); 00099 VRegInfo[Reg].first = RegClass; 00100 RegAllocHints.grow(Reg); 00101 if (TheDelegate) 00102 TheDelegate->MRI_NoteNewVirtualRegister(Reg); 00103 return Reg; 00104 } 00105 00106 /// clearVirtRegs - Remove all virtual registers (after physreg assignment). 00107 void MachineRegisterInfo::clearVirtRegs() { 00108 #ifndef NDEBUG 00109 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) { 00110 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 00111 if (!VRegInfo[Reg].second) 00112 continue; 00113 verifyUseList(Reg); 00114 llvm_unreachable("Remaining virtual register operands"); 00115 } 00116 #endif 00117 VRegInfo.clear(); 00118 } 00119 00120 void MachineRegisterInfo::verifyUseList(unsigned Reg) const { 00121 #ifndef NDEBUG 00122 bool Valid = true; 00123 for (MachineOperand &M : reg_operands(Reg)) { 00124 MachineOperand *MO = &M; 00125 MachineInstr *MI = MO->getParent(); 00126 if (!MI) { 00127 errs() << PrintReg(Reg, getTargetRegisterInfo()) 00128 << " use list MachineOperand " << MO 00129 << " has no parent instruction.\n"; 00130 Valid = false; 00131 } 00132 MachineOperand *MO0 = &MI->getOperand(0); 00133 unsigned NumOps = MI->getNumOperands(); 00134 if (!(MO >= MO0 && MO < MO0+NumOps)) { 00135 errs() << PrintReg(Reg, getTargetRegisterInfo()) 00136 << " use list MachineOperand " << MO 00137 << " doesn't belong to parent MI: " << *MI; 00138 Valid = false; 00139 } 00140 if (!MO->isReg()) { 00141 errs() << PrintReg(Reg, getTargetRegisterInfo()) 00142 << " MachineOperand " << MO << ": " << *MO 00143 << " is not a register\n"; 00144 Valid = false; 00145 } 00146 if (MO->getReg() != Reg) { 00147 errs() << PrintReg(Reg, getTargetRegisterInfo()) 00148 << " use-list MachineOperand " << MO << ": " 00149 << *MO << " is the wrong register\n"; 00150 Valid = false; 00151 } 00152 } 00153 assert(Valid && "Invalid use list"); 00154 #endif 00155 } 00156 00157 void MachineRegisterInfo::verifyUseLists() const { 00158 #ifndef NDEBUG 00159 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) 00160 verifyUseList(TargetRegisterInfo::index2VirtReg(i)); 00161 for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i) 00162 verifyUseList(i); 00163 #endif 00164 } 00165 00166 /// Add MO to the linked list of operands for its register. 00167 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) { 00168 assert(!MO->isOnRegUseList() && "Already on list"); 00169 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg()); 00170 MachineOperand *const Head = HeadRef; 00171 00172 // Head points to the first list element. 00173 // Next is NULL on the last list element. 00174 // Prev pointers are circular, so Head->Prev == Last. 00175 00176 // Head is NULL for an empty list. 00177 if (!Head) { 00178 MO->Contents.Reg.Prev = MO; 00179 MO->Contents.Reg.Next = nullptr; 00180 HeadRef = MO; 00181 return; 00182 } 00183 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!"); 00184 00185 // Insert MO between Last and Head in the circular Prev chain. 00186 MachineOperand *Last = Head->Contents.Reg.Prev; 00187 assert(Last && "Inconsistent use list"); 00188 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!"); 00189 Head->Contents.Reg.Prev = MO; 00190 MO->Contents.Reg.Prev = Last; 00191 00192 // Def operands always precede uses. This allows def_iterator to stop early. 00193 // Insert def operands at the front, and use operands at the back. 00194 if (MO->isDef()) { 00195 // Insert def at the front. 00196 MO->Contents.Reg.Next = Head; 00197 HeadRef = MO; 00198 } else { 00199 // Insert use at the end. 00200 MO->Contents.Reg.Next = nullptr; 00201 Last->Contents.Reg.Next = MO; 00202 } 00203 } 00204 00205 /// Remove MO from its use-def list. 00206 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) { 00207 assert(MO->isOnRegUseList() && "Operand not on use list"); 00208 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg()); 00209 MachineOperand *const Head = HeadRef; 00210 assert(Head && "List already empty"); 00211 00212 // Unlink this from the doubly linked list of operands. 00213 MachineOperand *Next = MO->Contents.Reg.Next; 00214 MachineOperand *Prev = MO->Contents.Reg.Prev; 00215 00216 // Prev links are circular, next link is NULL instead of looping back to Head. 00217 if (MO == Head) 00218 HeadRef = Next; 00219 else 00220 Prev->Contents.Reg.Next = Next; 00221 00222 (Next ? Next : Head)->Contents.Reg.Prev = Prev; 00223 00224 MO->Contents.Reg.Prev = nullptr; 00225 MO->Contents.Reg.Next = nullptr; 00226 } 00227 00228 /// Move NumOps operands from Src to Dst, updating use-def lists as needed. 00229 /// 00230 /// The Dst range is assumed to be uninitialized memory. (Or it may contain 00231 /// operands that won't be destroyed, which is OK because the MO destructor is 00232 /// trivial anyway). 00233 /// 00234 /// The Src and Dst ranges may overlap. 00235 void MachineRegisterInfo::moveOperands(MachineOperand *Dst, 00236 MachineOperand *Src, 00237 unsigned NumOps) { 00238 assert(Src != Dst && NumOps && "Noop moveOperands"); 00239 00240 // Copy backwards if Dst is within the Src range. 00241 int Stride = 1; 00242 if (Dst >= Src && Dst < Src + NumOps) { 00243 Stride = -1; 00244 Dst += NumOps - 1; 00245 Src += NumOps - 1; 00246 } 00247 00248 // Copy one operand at a time. 00249 do { 00250 new (Dst) MachineOperand(*Src); 00251 00252 // Dst takes Src's place in the use-def chain. 00253 if (Src->isReg()) { 00254 MachineOperand *&Head = getRegUseDefListHead(Src->getReg()); 00255 MachineOperand *Prev = Src->Contents.Reg.Prev; 00256 MachineOperand *Next = Src->Contents.Reg.Next; 00257 assert(Head && "List empty, but operand is chained"); 00258 assert(Prev && "Operand was not on use-def list"); 00259 00260 // Prev links are circular, next link is NULL instead of looping back to 00261 // Head. 00262 if (Src == Head) 00263 Head = Dst; 00264 else 00265 Prev->Contents.Reg.Next = Dst; 00266 00267 // Update Prev pointer. This also works when Src was pointing to itself 00268 // in a 1-element list. In that case Head == Dst. 00269 (Next ? Next : Head)->Contents.Reg.Prev = Dst; 00270 } 00271 00272 Dst += Stride; 00273 Src += Stride; 00274 } while (--NumOps); 00275 } 00276 00277 /// replaceRegWith - Replace all instances of FromReg with ToReg in the 00278 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y), 00279 /// except that it also changes any definitions of the register as well. 00280 /// If ToReg is a physical register we apply the sub register to obtain the 00281 /// final/proper physical register. 00282 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) { 00283 assert(FromReg != ToReg && "Cannot replace a reg with itself"); 00284 00285 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 00286 00287 // TODO: This could be more efficient by bulk changing the operands. 00288 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) { 00289 MachineOperand &O = *I; 00290 ++I; 00291 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 00292 O.substPhysReg(ToReg, *TRI); 00293 } else { 00294 O.setReg(ToReg); 00295 } 00296 } 00297 } 00298 00299 /// getVRegDef - Return the machine instr that defines the specified virtual 00300 /// register or null if none is found. This assumes that the code is in SSA 00301 /// form, so there should only be one definition. 00302 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const { 00303 // Since we are in SSA form, we can use the first definition. 00304 def_instr_iterator I = def_instr_begin(Reg); 00305 assert((I.atEnd() || std::next(I) == def_instr_end()) && 00306 "getVRegDef assumes a single definition or no definition"); 00307 return !I.atEnd() ? &*I : nullptr; 00308 } 00309 00310 /// getUniqueVRegDef - Return the unique machine instr that defines the 00311 /// specified virtual register or null if none is found. If there are 00312 /// multiple definitions or no definition, return null. 00313 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const { 00314 if (def_empty(Reg)) return nullptr; 00315 def_instr_iterator I = def_instr_begin(Reg); 00316 if (std::next(I) != def_instr_end()) 00317 return nullptr; 00318 return &*I; 00319 } 00320 00321 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const { 00322 use_nodbg_iterator UI = use_nodbg_begin(RegNo); 00323 if (UI == use_nodbg_end()) 00324 return false; 00325 return ++UI == use_nodbg_end(); 00326 } 00327 00328 /// clearKillFlags - Iterate over all the uses of the given register and 00329 /// clear the kill flag from the MachineOperand. This function is used by 00330 /// optimization passes which extend register lifetimes and need only 00331 /// preserve conservative kill flag information. 00332 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const { 00333 for (MachineOperand &MO : use_operands(Reg)) 00334 MO.setIsKill(false); 00335 } 00336 00337 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const { 00338 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) 00339 if (I->first == Reg || I->second == Reg) 00340 return true; 00341 return false; 00342 } 00343 00344 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the 00345 /// corresponding live-in physical register. 00346 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const { 00347 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) 00348 if (I->second == VReg) 00349 return I->first; 00350 return 0; 00351 } 00352 00353 /// getLiveInVirtReg - If PReg is a live-in physical register, return the 00354 /// corresponding live-in physical register. 00355 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const { 00356 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) 00357 if (I->first == PReg) 00358 return I->second; 00359 return 0; 00360 } 00361 00362 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers 00363 /// into the given entry block. 00364 void 00365 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB, 00366 const TargetRegisterInfo &TRI, 00367 const TargetInstrInfo &TII) { 00368 // Emit the copies into the top of the block. 00369 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i) 00370 if (LiveIns[i].second) { 00371 if (use_empty(LiveIns[i].second)) { 00372 // The livein has no uses. Drop it. 00373 // 00374 // It would be preferable to have isel avoid creating live-in 00375 // records for unused arguments in the first place, but it's 00376 // complicated by the debug info code for arguments. 00377 LiveIns.erase(LiveIns.begin() + i); 00378 --i; --e; 00379 } else { 00380 // Emit a copy. 00381 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(), 00382 TII.get(TargetOpcode::COPY), LiveIns[i].second) 00383 .addReg(LiveIns[i].first); 00384 00385 // Add the register to the entry block live-in set. 00386 EntryMBB->addLiveIn(LiveIns[i].first); 00387 } 00388 } else { 00389 // Add the register to the entry block live-in set. 00390 EntryMBB->addLiveIn(LiveIns[i].first); 00391 } 00392 } 00393 00394 #ifndef NDEBUG 00395 void MachineRegisterInfo::dumpUses(unsigned Reg) const { 00396 for (MachineInstr &I : use_instructions(Reg)) 00397 I.dump(); 00398 } 00399 #endif 00400 00401 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) { 00402 ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF); 00403 assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() && 00404 "Invalid ReservedRegs vector from target"); 00405 } 00406 00407 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg, 00408 const MachineFunction &MF) const { 00409 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg)); 00410 00411 // Check if any overlapping register is modified, or allocatable so it may be 00412 // used later. 00413 for (MCRegAliasIterator AI(PhysReg, getTargetRegisterInfo(), true); 00414 AI.isValid(); ++AI) 00415 if (!def_empty(*AI) || isAllocatable(*AI)) 00416 return false; 00417 return true; 00418 } 00419 00420 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the 00421 /// specified register as undefined which causes the DBG_VALUE to be 00422 /// deleted during LiveDebugVariables analysis. 00423 void MachineRegisterInfo::markUsesInDebugValueAsUndef(unsigned Reg) const { 00424 // Mark any DBG_VALUE that uses Reg as undef (but don't delete it.) 00425 MachineRegisterInfo::use_instr_iterator nextI; 00426 for (use_instr_iterator I = use_instr_begin(Reg), E = use_instr_end(); 00427 I != E; I = nextI) { 00428 nextI = std::next(I); // I is invalidated by the setReg 00429 MachineInstr *UseMI = &*I; 00430 if (UseMI->isDebugValue()) 00431 UseMI->getOperand(0).setReg(0U); 00432 } 00433 }