Add a MinNumRegs argument to MRI::constrainRegClass().
[oota-llvm.git] / lib / CodeGen / MachineRegisterInfo.cpp
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"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/Target/TargetInstrInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 using namespace llvm;
19
20 MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
21   : IsSSA(true) {
22   VRegInfo.reserve(256);
23   RegAllocHints.reserve(256);
24   UsedPhysRegs.resize(TRI.getNumRegs());
25   
26   // Create the physreg use/def lists.
27   PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
28   memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
29 }
30
31 MachineRegisterInfo::~MachineRegisterInfo() {
32 #ifndef NDEBUG
33   for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
34     assert(VRegInfo[TargetRegisterInfo::index2VirtReg(i)].second == 0 &&
35            "Vreg use list non-empty still?");
36   for (unsigned i = 0, e = UsedPhysRegs.size(); i != e; ++i)
37     assert(!PhysRegUseDefLists[i] &&
38            "PhysRegUseDefLists has entries after all instructions are deleted");
39 #endif
40   delete [] PhysRegUseDefLists;
41 }
42
43 /// setRegClass - Set the register class of the specified virtual register.
44 ///
45 void
46 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
47   VRegInfo[Reg].first = RC;
48 }
49
50 const TargetRegisterClass *
51 MachineRegisterInfo::constrainRegClass(unsigned Reg,
52                                        const TargetRegisterClass *RC,
53                                        unsigned MinNumRegs) {
54   const TargetRegisterClass *OldRC = getRegClass(Reg);
55   if (OldRC == RC)
56     return RC;
57   const TargetRegisterClass *NewRC = getCommonSubClass(OldRC, RC);
58   if (!NewRC || NewRC == OldRC)
59     return NewRC;
60   if (NewRC->getNumRegs() < MinNumRegs)
61     return 0;
62   setRegClass(Reg, NewRC);
63   return NewRC;
64 }
65
66 bool
67 MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
68   const TargetInstrInfo *TII = TM.getInstrInfo();
69   const TargetRegisterInfo *TRI = TM.getRegisterInfo();
70   const TargetRegisterClass *OldRC = getRegClass(Reg);
71   const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
72
73   // Stop early if there is no room to grow.
74   if (NewRC == OldRC)
75     return false;
76
77   // Accumulate constraints from all uses.
78   for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
79        ++I) {
80     // TRI doesn't have accurate enough information to model this yet.
81     if (I.getOperand().getSubReg())
82       return false;
83     // Inline asm instuctions don't remember their constraints.
84     if (I->isInlineAsm())
85       return false;
86     const TargetRegisterClass *OpRC =
87       TII->getRegClass(I->getDesc(), I.getOperandNo(), TRI);
88     if (OpRC)
89       NewRC = getCommonSubClass(NewRC, OpRC);
90     if (!NewRC || NewRC == OldRC)
91       return false;
92   }
93   setRegClass(Reg, NewRC);
94   return true;
95 }
96
97 /// createVirtualRegister - Create and return a new virtual register in the
98 /// function with the specified register class.
99 ///
100 unsigned
101 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
102   assert(RegClass && "Cannot create register without RegClass!");
103   assert(RegClass->isAllocatable() &&
104          "Virtual register RegClass must be allocatable.");
105
106   // New virtual register number.
107   unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
108
109   // Add a reg, but keep track of whether the vector reallocated or not.
110   const unsigned FirstVirtReg = TargetRegisterInfo::index2VirtReg(0);
111   void *ArrayBase = getNumVirtRegs() == 0 ? 0 : &VRegInfo[FirstVirtReg];
112   VRegInfo.grow(Reg);
113   VRegInfo[Reg].first = RegClass;
114   RegAllocHints.grow(Reg);
115
116   if (ArrayBase && &VRegInfo[FirstVirtReg] != ArrayBase)
117     // The vector reallocated, handle this now.
118     HandleVRegListReallocation();
119   return Reg;
120 }
121
122 /// HandleVRegListReallocation - We just added a virtual register to the
123 /// VRegInfo info list and it reallocated.  Update the use/def lists info
124 /// pointers.
125 void MachineRegisterInfo::HandleVRegListReallocation() {
126   // The back pointers for the vreg lists point into the previous vector.
127   // Update them to point to their correct slots.
128   for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
129     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
130     MachineOperand *List = VRegInfo[Reg].second;
131     if (!List) continue;
132     // Update the back-pointer to be accurate once more.
133     List->Contents.Reg.Prev = &VRegInfo[Reg].second;
134   }
135 }
136
137 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
138 /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
139 /// except that it also changes any definitions of the register as well.
140 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
141   assert(FromReg != ToReg && "Cannot replace a reg with itself");
142
143   // TODO: This could be more efficient by bulk changing the operands.
144   for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
145     MachineOperand &O = I.getOperand();
146     ++I;
147     O.setReg(ToReg);
148   }
149 }
150
151
152 /// getVRegDef - Return the machine instr that defines the specified virtual
153 /// register or null if none is found.  This assumes that the code is in SSA
154 /// form, so there should only be one definition.
155 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
156   // Since we are in SSA form, we can use the first definition.
157   if (!def_empty(Reg))
158     return &*def_begin(Reg);
159   return 0;
160 }
161
162 bool MachineRegisterInfo::hasOneUse(unsigned RegNo) const {
163   use_iterator UI = use_begin(RegNo);
164   if (UI == use_end())
165     return false;
166   return ++UI == use_end();
167 }
168
169 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
170   use_nodbg_iterator UI = use_nodbg_begin(RegNo);
171   if (UI == use_nodbg_end())
172     return false;
173   return ++UI == use_nodbg_end();
174 }
175
176 /// clearKillFlags - Iterate over all the uses of the given register and
177 /// clear the kill flag from the MachineOperand. This function is used by
178 /// optimization passes which extend register lifetimes and need only
179 /// preserve conservative kill flag information.
180 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
181   for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
182     UI.getOperand().setIsKill(false);
183 }
184
185 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
186   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
187     if (I->first == Reg || I->second == Reg)
188       return true;
189   return false;
190 }
191
192 bool MachineRegisterInfo::isLiveOut(unsigned Reg) const {
193   for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
194     if (*I == Reg)
195       return true;
196   return false;
197 }
198
199 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
200 /// corresponding live-in physical register.
201 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
202   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
203     if (I->second == VReg)
204       return I->first;
205   return 0;
206 }
207
208 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
209 /// corresponding live-in physical register.
210 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
211   for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
212     if (I->first == PReg)
213       return I->second;
214   return 0;
215 }
216
217 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
218 /// into the given entry block.
219 void
220 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
221                                       const TargetRegisterInfo &TRI,
222                                       const TargetInstrInfo &TII) {
223   // Emit the copies into the top of the block.
224   for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
225     if (LiveIns[i].second) {
226       if (use_empty(LiveIns[i].second)) {
227         // The livein has no uses. Drop it.
228         //
229         // It would be preferable to have isel avoid creating live-in
230         // records for unused arguments in the first place, but it's
231         // complicated by the debug info code for arguments.
232         LiveIns.erase(LiveIns.begin() + i);
233         --i; --e;
234       } else {
235         // Emit a copy.
236         BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
237                 TII.get(TargetOpcode::COPY), LiveIns[i].second)
238           .addReg(LiveIns[i].first);
239
240         // Add the register to the entry block live-in set.
241         EntryMBB->addLiveIn(LiveIns[i].first);
242       }
243     } else {
244       // Add the register to the entry block live-in set.
245       EntryMBB->addLiveIn(LiveIns[i].first);
246     }
247 }
248
249 void MachineRegisterInfo::closePhysRegsUsed(const TargetRegisterInfo &TRI) {
250   for (int i = UsedPhysRegs.find_first(); i >= 0;
251        i = UsedPhysRegs.find_next(i))
252          for (const unsigned *SS = TRI.getSubRegisters(i);
253               unsigned SubReg = *SS; ++SS)
254            if (SubReg > unsigned(i))
255              UsedPhysRegs.set(SubReg);
256 }
257
258 #ifndef NDEBUG
259 void MachineRegisterInfo::dumpUses(unsigned Reg) const {
260   for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
261     I.getOperand().getParent()->dump();
262 }
263 #endif