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