MachineRegisterInfo.cpp revision 11a26f3697ea6520022ea6d3fa6a07b3c1b988cd
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" 15using namespace llvm; 16 17MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI) { 18 VRegInfo.reserve(256); 19 RegClass2VRegMap.resize(TRI.getNumRegClasses()+1); // RC ID starts at 1. 20 UsedPhysRegs.resize(TRI.getNumRegs()); 21 22 // Create the physreg use/def lists. 23 PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()]; 24 memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs()); 25} 26 27MachineRegisterInfo::~MachineRegisterInfo() { 28#ifndef NDEBUG 29 for (unsigned i = 0, e = VRegInfo.size(); i != e; ++i) 30 assert(VRegInfo[i].second == 0 && "Vreg use list non-empty still?"); 31 for (unsigned i = 0, e = UsedPhysRegs.size(); i != e; ++i) 32 assert(!PhysRegUseDefLists[i] && 33 "PhysRegUseDefLists has entries after all instructions are deleted"); 34#endif 35 delete [] PhysRegUseDefLists; 36} 37 38/// HandleVRegListReallocation - We just added a virtual register to the 39/// VRegInfo info list and it reallocated. Update the use/def lists info 40/// pointers. 41void MachineRegisterInfo::HandleVRegListReallocation() { 42 // The back pointers for the vreg lists point into the previous vector. 43 // Update them to point to their correct slots. 44 for (unsigned i = 0, e = VRegInfo.size(); i != e; ++i) { 45 MachineOperand *List = VRegInfo[i].second; 46 if (!List) continue; 47 // Update the back-pointer to be accurate once more. 48 List->Contents.Reg.Prev = &VRegInfo[i].second; 49 } 50} 51 52/// replaceRegWith - Replace all instances of FromReg with ToReg in the 53/// machine function. This is like llvm-level X->replaceAllUsesWith(Y), 54/// except that it also changes any definitions of the register as well. 55void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) { 56 assert(FromReg != ToReg && "Cannot replace a reg with itself"); 57 58 // TODO: This could be more efficient by bulk changing the operands. 59 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) { 60 MachineOperand &O = I.getOperand(); 61 ++I; 62 O.setReg(ToReg); 63 } 64} 65 66 67/// getVRegDef - Return the machine instr that defines the specified virtual 68/// register or null if none is found. This assumes that the code is in SSA 69/// form, so there should only be one definition. 70MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const { 71 assert(Reg-TargetRegisterInfo::FirstVirtualRegister < VRegInfo.size() && 72 "Invalid vreg!"); 73 for (reg_iterator I = reg_begin(Reg), E = reg_end(); I != E; ++I) { 74 // Since we are in SSA form, we can stop at the first definition. 75 if (I.getOperand().isDef()) 76 return &*I; 77 } 78 return 0; 79} 80 81 82#ifndef NDEBUG 83void MachineRegisterInfo::dumpUses(unsigned Reg) const { 84 for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I) 85 I.getOperand().getParent()->dump(); 86} 87#endif 88