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