LLVM API Documentation

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