Home | History | Annotate | Download | only in CodeGen
      1 //===-- lib/Codegen/MachineRegisterInfo.cpp -------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Implementation of the MachineRegisterInfo class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     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 using namespace llvm;
     19 
     20 MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
     21   : TRI(&TRI), IsSSA(true), TracksLiveness(true) {
     22   VRegInfo.reserve(256);
     23   RegAllocHints.reserve(256);
     24   UsedPhysRegs.resize(TRI.getNumRegs());
     25   UsedPhysRegMask.resize(TRI.getNumRegs());
     26 
     27   // Create the physreg use/def lists.
     28   PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
     29   memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
     30 }
     31 
     32 MachineRegisterInfo::~MachineRegisterInfo() {
     33 #ifndef NDEBUG
     34   clearVirtRegs();
     35   for (unsigned i = 0, e = UsedPhysRegs.size(); i != e; ++i)
     36     assert(!PhysRegUseDefLists[i] &&
     37            "PhysRegUseDefLists has entries after all instructions are deleted");
     38 #endif
     39   delete [] PhysRegUseDefLists;
     40 }
     41 
     42 /// setRegClass - Set the register class of the specified virtual register.
     43 ///
     44 void
     45 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
     46   VRegInfo[Reg].first = RC;
     47 }
     48 
     49 const TargetRegisterClass *
     50 MachineRegisterInfo::constrainRegClass(unsigned Reg,
     51                                        const TargetRegisterClass *RC,
     52                                        unsigned MinNumRegs) {
     53   const TargetRegisterClass *OldRC = getRegClass(Reg);
     54   if (OldRC == RC)
     55     return RC;
     56   const TargetRegisterClass *NewRC = TRI->getCommonSubClass(OldRC, RC);
     57   if (!NewRC || NewRC == OldRC)
     58     return NewRC;
     59   if (NewRC->getNumRegs() < MinNumRegs)
     60     return 0;
     61   setRegClass(Reg, NewRC);
     62   return NewRC;
     63 }
     64 
     65 bool
     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);
     70 
     71   // Stop early if there is no room to grow.
     72   if (NewRC == OldRC)
     73     return false;
     74 
     75   // Accumulate constraints from all uses.
     76   for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
     77        ++I) {
     78     const TargetRegisterClass *OpRC =
     79       I->getRegClassConstraint(I.getOperandNo(), TII, TRI);
     80     if (unsigned SubIdx = I.getOperand().getSubReg()) {
     81       if (OpRC)
     82         NewRC = TRI->getMatchingSuperRegClass(NewRC, OpRC, SubIdx);
     83       else
     84         NewRC = TRI->getSubClassWithSubReg(NewRC, SubIdx);
     85     } else if (OpRC)
     86       NewRC = TRI->getCommonSubClass(NewRC, OpRC);
     87     if (!NewRC || NewRC == OldRC)
     88       return false;
     89   }
     90   setRegClass(Reg, NewRC);
     91   return true;
     92 }
     93 
     94 /// createVirtualRegister - Create and return a new virtual register in the
     95 /// function with the specified register class.
     96 ///
     97 unsigned
     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.");
    102 
    103   // New virtual register number.
    104   unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
    105   VRegInfo.grow(Reg);
    106   VRegInfo[Reg].first = RegClass;
    107   RegAllocHints.grow(Reg);
    108   return Reg;
    109 }
    110 
    111 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
    112 void MachineRegisterInfo::clearVirtRegs() {
    113 #ifndef NDEBUG
    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?");
    117 #endif
    118   VRegInfo.clear();
    119 }
    120 
    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;
    126 
    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.
    130 
    131   // Head is NULL for an empty list.
    132   if (!Head) {
    133     MO->Contents.Reg.Prev = MO;
    134     MO->Contents.Reg.Next = 0;
    135     HeadRef = MO;
    136     return;
    137   }
    138   assert(MO->getReg() == Head->getReg() && "Different regs on the same list!");
    139 
    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;
    146 
    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.
    149   if (MO->isDef()) {
    150     // Insert def at the front.
    151     MO->Contents.Reg.Next = Head;
    152     HeadRef = MO;
    153   } else {
    154     // Insert use at the end.
    155     MO->Contents.Reg.Next = 0;
    156     Last->Contents.Reg.Next = MO;
    157   }
    158 }
    159 
    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");
    166 
    167   // Unlink this from the doubly linked list of operands.
    168   MachineOperand *Next = MO->Contents.Reg.Next;
    169   MachineOperand *Prev = MO->Contents.Reg.Prev;
    170 
    171   // Prev links are circular, next link is NULL instead of looping back to Head.
    172   if (MO == Head)
    173     HeadRef = Next;
    174   else
    175     Prev->Contents.Reg.Next = Next;
    176 
    177   (Next ? Next : Head)->Contents.Reg.Prev = Prev;
    178 
    179   MO->Contents.Reg.Prev = 0;
    180   MO->Contents.Reg.Next = 0;
    181 }
    182 
    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");
    188 
    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();
    192     ++I;
    193     O.setReg(ToReg);
    194   }
    195 }
    196 
    197 
    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;
    207 }
    208 
    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())
    216     return 0;
    217   return &*I;
    218 }
    219 
    220 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
    221   use_nodbg_iterator UI = use_nodbg_begin(RegNo);
    222   if (UI == use_nodbg_end())
    223     return false;
    224   return ++UI == use_nodbg_end();
    225 }
    226 
    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);
    234 }
    235 
    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)
    239       return true;
    240   return false;
    241 }
    242 
    243 bool MachineRegisterInfo::isLiveOut(unsigned Reg) const {
    244   for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
    245     if (*I == Reg)
    246       return true;
    247   return false;
    248 }
    249 
    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)
    255       return I->first;
    256   return 0;
    257 }
    258 
    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)
    264       return I->second;
    265   return 0;
    266 }
    267 
    268 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
    269 /// into the given entry block.
    270 void
    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.
    279         //
    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);
    284         --i; --e;
    285       } else {
    286         // Emit a copy.
    287         BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
    288                 TII.get(TargetOpcode::COPY), LiveIns[i].second)
    289           .addReg(LiveIns[i].first);
    290 
    291         // Add the register to the entry block live-in set.
    292         EntryMBB->addLiveIn(LiveIns[i].first);
    293       }
    294     } else {
    295       // Add the register to the entry block live-in set.
    296       EntryMBB->addLiveIn(LiveIns[i].first);
    297     }
    298 }
    299 
    300 #ifndef NDEBUG
    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();
    304 }
    305 #endif
    306 
    307 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) {
    308   ReservedRegs = TRI->getReservedRegs(MF);
    309 }
    310 
    311 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg,
    312                                             const MachineFunction &MF) const {
    313   assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
    314 
    315   // Check if any overlapping register is modified.
    316   for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
    317     if (!def_empty(*AI))
    318       return false;
    319 
    320   // Check if any overlapping register is allocatable so it may be used later.
    321   if (AllocatableRegs.empty())
    322     AllocatableRegs = TRI->getAllocatableSet(MF);
    323   for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI)
    324     if (AllocatableRegs.test(*AI))
    325       return false;
    326   return true;
    327 }
    328