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 using namespace llvm;
00016 
00017 MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI) {
00018   VRegInfo.reserve(256);
00019   RegAllocHints.reserve(256);
00020   RegClass2VRegMap.resize(TRI.getNumRegClasses()+1); // RC ID starts at 1.
00021   UsedPhysRegs.resize(TRI.getNumRegs());
00022   
00023   // Create the physreg use/def lists.
00024   PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
00025   memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
00026 }
00027 
00028 MachineRegisterInfo::~MachineRegisterInfo() {
00029 #ifndef NDEBUG
00030   for (unsigned i = 0, e = VRegInfo.size(); i != e; ++i)
00031     assert(VRegInfo[i].second == 0 && "Vreg use list non-empty still?");
00032   for (unsigned i = 0, e = UsedPhysRegs.size(); i != e; ++i)
00033     assert(!PhysRegUseDefLists[i] &&
00034            "PhysRegUseDefLists has entries after all instructions are deleted");
00035 #endif
00036   delete [] PhysRegUseDefLists;
00037 }
00038 
00039 /// setRegClass - Set the register class of the specified virtual register.
00040 ///
00041 void
00042 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
00043   unsigned VR = Reg;
00044   Reg -= TargetRegisterInfo::FirstVirtualRegister;
00045   assert(Reg < VRegInfo.size() && "Invalid vreg!");
00046   const TargetRegisterClass *OldRC = VRegInfo[Reg].first;
00047   VRegInfo[Reg].first = RC;
00048 
00049   // Remove from old register class's vregs list. This may be slow but
00050   // fortunately this operation is rarely needed.
00051   std::vector<unsigned> &VRegs = RegClass2VRegMap[OldRC->getID()];
00052   std::vector<unsigned>::iterator I=std::find(VRegs.begin(), VRegs.end(), VR);
00053   VRegs.erase(I);
00054 
00055   // Add to new register class's vregs list.
00056   RegClass2VRegMap[RC->getID()].push_back(VR);
00057 }
00058 
00059 /// createVirtualRegister - Create and return a new virtual register in the
00060 /// function with the specified register class.
00061 ///
00062 unsigned
00063 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
00064   assert(RegClass && "Cannot create register without RegClass!");
00065   // Add a reg, but keep track of whether the vector reallocated or not.
00066   void *ArrayBase = VRegInfo.empty() ? 0 : &VRegInfo[0];
00067   VRegInfo.push_back(std::make_pair(RegClass, (MachineOperand*)0));
00068   RegAllocHints.push_back(std::make_pair(0, 0));
00069 
00070   if (!((&VRegInfo[0] == ArrayBase || VRegInfo.size() == 1)))
00071     // The vector reallocated, handle this now.
00072     HandleVRegListReallocation();
00073   unsigned VR = getLastVirtReg();
00074   RegClass2VRegMap[RegClass->getID()].push_back(VR);
00075   return VR;
00076 }
00077 
00078 /// HandleVRegListReallocation - We just added a virtual register to the
00079 /// VRegInfo info list and it reallocated.  Update the use/def lists info
00080 /// pointers.
00081 void MachineRegisterInfo::HandleVRegListReallocation() {
00082   // The back pointers for the vreg lists point into the previous vector.
00083   // Update them to point to their correct slots.
00084   for (unsigned i = 0, e = VRegInfo.size(); i != e; ++i) {
00085     MachineOperand *List = VRegInfo[i].second;
00086     if (!List) continue;
00087     // Update the back-pointer to be accurate once more.
00088     List->Contents.Reg.Prev = &VRegInfo[i].second;
00089   }
00090 }
00091 
00092 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
00093 /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
00094 /// except that it also changes any definitions of the register as well.
00095 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
00096   assert(FromReg != ToReg && "Cannot replace a reg with itself");
00097 
00098   // TODO: This could be more efficient by bulk changing the operands.
00099   for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
00100     MachineOperand &O = I.getOperand();
00101     ++I;
00102     O.setReg(ToReg);
00103   }
00104 }
00105 
00106 
00107 /// getVRegDef - Return the machine instr that defines the specified virtual
00108 /// register or null if none is found.  This assumes that the code is in SSA
00109 /// form, so there should only be one definition.
00110 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
00111   assert(Reg-TargetRegisterInfo::FirstVirtualRegister < VRegInfo.size() &&
00112          "Invalid vreg!");
00113   for (reg_iterator I = reg_begin(Reg), E = reg_end(); I != E; ++I) {
00114     // Since we are in SSA form, we can stop at the first definition.
00115     if (I.getOperand().isDef())
00116       return &*I;
00117   }
00118   return 0;
00119 }
00120 
00121 
00122 #ifndef NDEBUG
00123 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
00124   for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
00125     I.getOperand().getParent()->dump();
00126 }
00127 #endif