1 //===-- lib/Codegen/MachineRegisterInfo.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MachineRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineRegisterInfo.h"
17 MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI) {
18 VRegInfo.reserve(256);
19 RegClass2VRegMap.resize(TRI.getNumRegClasses()+1); // RC ID starts at 1.
20 UsedPhysRegs.resize(TRI.getNumRegs());
22 // Create the physreg use/def lists.
23 PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
24 memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
27 MachineRegisterInfo::~MachineRegisterInfo() {
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");
35 delete [] PhysRegUseDefLists;
38 /// HandleVRegListReallocation - We just added a virtual register to the
39 /// VRegInfo info list and it reallocated. Update the use/def lists info
41 void 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;
47 // Update the back-pointer to be accurate once more.
48 List->Contents.Reg.Prev = &VRegInfo[i].second;
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.
55 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
56 assert(FromReg != ToReg && "Cannot replace a reg with itself");
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();
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.
70 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
71 assert(Reg-TargetRegisterInfo::FirstVirtualRegister < VRegInfo.size() &&
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())
83 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
84 for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
85 I.getOperand().getParent()->dump();