blob: afbc7eb0e12b59f3ea31d721ee9b52de4c194461 [file] [log] [blame]
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb74e83c2002-12-16 16:15:28 +00009//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4cc662b2003-08-03 21:47:31 +000015#define DEBUG_TYPE "regalloc"
Chris Lattner91a452b2003-01-13 00:25:40 +000016#include "llvm/CodeGen/Passes.h"
Chris Lattner580f9be2002-12-28 20:40:43 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000018#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerff863ba2002-12-25 05:05:46 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000021#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner82bee0f2002-12-18 08:14:26 +000024#include "Support/CommandLine.h"
Chris Lattnera11136b2003-08-01 22:21:34 +000025#include "Support/Debug.h"
26#include "Support/Statistic.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000027#include <iostream>
Chris Lattneref09c632004-01-31 21:27:19 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattnerb74e83c2002-12-16 16:15:28 +000030namespace {
31 Statistic<> NumSpilled ("ra-local", "Number of registers spilled");
32 Statistic<> NumReloaded("ra-local", "Number of registers reloaded");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000033 cl::opt<bool> DisableKill("disable-kill", cl::Hidden,
Chris Lattner82bee0f2002-12-18 08:14:26 +000034 cl::desc("Disable register kill in local-ra"));
Chris Lattnerb74e83c2002-12-16 16:15:28 +000035
Chris Lattner580f9be2002-12-28 20:40:43 +000036 class RA : public MachineFunctionPass {
37 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000038 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000039 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000040 LiveVariables *LV;
Chris Lattnerff863ba2002-12-25 05:05:46 +000041
Chris Lattnerb8822ad2003-08-04 23:36:39 +000042 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
43 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000044 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000045
46 // Virt2PhysRegMap - This map contains entries for each virtual register
Chris Lattnerecea5632004-02-09 02:12:04 +000047 // that is currently available in a physical register. This is "logically"
48 // a map from virtual register numbers to physical register numbers.
49 // Instead of using a map, however, which is slow, we use a vector. The
50 // index is the VREG number - FirstVirtualRegister. If the entry is zero,
51 // then it is logically "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000052 //
Chris Lattnerecea5632004-02-09 02:12:04 +000053 std::vector<unsigned> Virt2PhysRegMap;
54
55 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
56 assert(VirtReg >= MRegisterInfo::FirstVirtualRegister &&"Illegal VREG #");
57 assert(VirtReg-MRegisterInfo::FirstVirtualRegister <Virt2PhysRegMap.size()
58 && "VirtReg not in map!");
59 return Virt2PhysRegMap[VirtReg-MRegisterInfo::FirstVirtualRegister];
60 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000061
Chris Lattner64667b62004-02-09 01:26:13 +000062 // PhysRegsUsed - This array is effectively a map, containing entries for
63 // each physical register that currently has a value (ie, it is in
64 // Virt2PhysRegMap). The value mapped to is the virtual register
65 // corresponding to the physical register (the inverse of the
66 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
67 // because it is used by a future instruction. If the entry for a physical
68 // register is -1, then the physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000069 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000070 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000071
72 // PhysRegsUseOrder - This contains a list of the physical registers that
73 // currently have a virtual register value in them. This list provides an
74 // ordering of registers, imposing a reallocation order. This list is only
75 // used if all registers are allocated and we have to spill one, in which
76 // case we spill the least recently used register. Entries at the front of
77 // the list are the least recently used registers, entries at the back are
78 // the most recently used.
79 //
80 std::vector<unsigned> PhysRegsUseOrder;
81
Chris Lattner91a452b2003-01-13 00:25:40 +000082 // VirtRegModified - This bitset contains information about which virtual
83 // registers need to be spilled back to memory when their registers are
84 // scavenged. If a virtual register has simply been rematerialized, there
85 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000086 //
Chris Lattner91a452b2003-01-13 00:25:40 +000087 std::vector<bool> VirtRegModified;
88
89 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000090 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000091 Reg -= MRegisterInfo::FirstVirtualRegister;
92 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
93 VirtRegModified[Reg] = Val;
94 }
95
96 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +000097 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000098 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000099 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000100 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
101 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000102
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000103 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner82bee0f2002-12-18 08:14:26 +0000104 assert(!PhysRegsUseOrder.empty() && "No registers used!");
Chris Lattner0eb172c2002-12-24 00:04:55 +0000105 if (PhysRegsUseOrder.back() == Reg) return; // Already most recently used
106
107 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000108 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
109 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
110 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
111 // Add it to the end of the list
112 PhysRegsUseOrder.push_back(RegMatch);
113 if (RegMatch == Reg)
114 return; // Found an exact match, exit early
115 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000116 }
117
118 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000119 virtual const char *getPassName() const {
120 return "Local Register Allocator";
121 }
122
Chris Lattner91a452b2003-01-13 00:25:40 +0000123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 if (!DisableKill)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000125 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000126 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000127 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000128 MachineFunctionPass::getAnalysisUsage(AU);
129 }
130
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000131 private:
132 /// runOnMachineFunction - Register allocate the whole function
133 bool runOnMachineFunction(MachineFunction &Fn);
134
135 /// AllocateBasicBlock - Register allocate the specified basic block.
136 void AllocateBasicBlock(MachineBasicBlock &MBB);
137
Chris Lattner82bee0f2002-12-18 08:14:26 +0000138
Chris Lattner82bee0f2002-12-18 08:14:26 +0000139 /// areRegsEqual - This method returns true if the specified registers are
140 /// related to each other. To do this, it checks to see if they are equal
141 /// or if the first register is in the alias set of the second register.
142 ///
143 bool areRegsEqual(unsigned R1, unsigned R2) const {
144 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000145 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
146 *AliasSet; ++AliasSet) {
147 if (*AliasSet == R1) return true;
148 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000149 return false;
150 }
151
Chris Lattner580f9be2002-12-28 20:40:43 +0000152 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000153 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000154 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000155
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000156 /// removePhysReg - This method marks the specified physical register as no
157 /// longer being in use.
158 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000159 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000160
161 /// spillVirtReg - This method spills the value specified by PhysReg into
162 /// the virtual register slot specified by VirtReg. It then updates the RA
163 /// data structures to indicate the fact that PhysReg is now available.
164 ///
165 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
166 unsigned VirtReg, unsigned PhysReg);
167
Chris Lattnerc21be922002-12-16 17:44:42 +0000168 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000169 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
170 /// true, then the request is ignored if the physical register does not
171 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000172 ///
Chris Lattnerc21be922002-12-16 17:44:42 +0000173 void spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000174 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000175
Chris Lattner91a452b2003-01-13 00:25:40 +0000176 /// assignVirtToPhysReg - This method updates local state so that we know
177 /// that PhysReg is the proper container for VirtReg now. The physical
178 /// register must not be used for anything else when this is called.
179 ///
180 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
181
182 /// liberatePhysReg - Make sure the specified physical register is available
183 /// for use. If there is currently a value in it, it is either moved out of
184 /// the way or spilled to memory.
185 ///
186 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000187 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000188
Chris Lattnerae640432002-12-17 02:50:10 +0000189 /// isPhysRegAvailable - Return true if the specified physical register is
190 /// free and available for use. This also includes checking to see if
191 /// aliased registers are all free...
192 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000193 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000194
195 /// getFreeReg - Look to see if there is a free register available in the
196 /// specified register class. If not, return 0.
197 ///
198 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000199
Chris Lattner91a452b2003-01-13 00:25:40 +0000200 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000201 /// register. If all compatible physical registers are used, this method
202 /// spills the last used virtual register to the stack, and uses that
203 /// register.
204 ///
Chris Lattner91a452b2003-01-13 00:25:40 +0000205 unsigned getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000206 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000207
208 /// reloadVirtReg - This method loads the specified virtual register into a
209 /// physical register, returning the physical register chosen. This updates
210 /// the regalloc data structures to reflect the fact that the virtual reg is
211 /// now alive in a physical register, and the previous one isn't.
212 ///
213 unsigned reloadVirtReg(MachineBasicBlock &MBB,
214 MachineBasicBlock::iterator &I, unsigned VirtReg);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000215
216 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
217 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000218 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000219}
220
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000221/// getStackSpaceFor - This allocates space for the specified virtual register
222/// to be held on the stack.
223int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
224 // Find the location Reg would belong...
225 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000226
Chris Lattner580f9be2002-12-28 20:40:43 +0000227 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000228 return I->second; // Already has space allocated?
229
Chris Lattner580f9be2002-12-28 20:40:43 +0000230 // Allocate a new stack object for this spill location...
Chris Lattner91a452b2003-01-13 00:25:40 +0000231 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000232
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000233 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000234 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
235 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000236}
237
Chris Lattnerae640432002-12-17 02:50:10 +0000238
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000239/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000240/// longer being in use.
241///
242void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000243 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000244
245 std::vector<unsigned>::iterator It =
246 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000247 if (It != PhysRegsUseOrder.end())
248 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000249}
250
Chris Lattner91a452b2003-01-13 00:25:40 +0000251
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000252/// spillVirtReg - This method spills the value specified by PhysReg into the
253/// virtual register slot specified by VirtReg. It then updates the RA data
254/// structures to indicate the fact that PhysReg is now available.
255///
256void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
257 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000258 if (!VirtReg && DisableKill) return;
259 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000260 " Must not have appropriate kill for the register or use exists beyond"
261 " the intended one.");
262 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
263 std::cerr << " containing %reg" << VirtReg;
264 if (!isVirtRegModified(VirtReg))
265 std::cerr << " which has not been modified, so no store necessary!");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000266
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000267 // Otherwise, there is a virtual register corresponding to this physical
268 // register. We only need to spill it into its stack slot if it has been
269 // modified.
270 if (isVirtRegModified(VirtReg)) {
271 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
272 int FrameIndex = getStackSpaceFor(VirtReg, RC);
273 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
274 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
275 ++NumSpilled; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000276 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000277
278 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000279
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000280 DEBUG(std::cerr << "\n");
Chris Lattner82bee0f2002-12-18 08:14:26 +0000281 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000282}
283
Chris Lattnerae640432002-12-17 02:50:10 +0000284
Chris Lattner91a452b2003-01-13 00:25:40 +0000285/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000286/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
287/// then the request is ignored if the physical register does not contain a
288/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000289///
290void RA::spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000291 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000292 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
293 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
294 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000295 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000296 // If the selected register aliases any other registers, we must make
297 // sure that one of the aliases isn't alive...
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000298 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000299 *AliasSet; ++AliasSet)
300 if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
301 if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
302 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000303 }
304}
305
306
307/// assignVirtToPhysReg - This method updates local state so that we know
308/// that PhysReg is the proper container for VirtReg now. The physical
309/// register must not be used for anything else when this is called.
310///
311void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000312 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000313 // Update information to note the fact that this register was just used, and
314 // it holds VirtReg.
315 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000316 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000317 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
318}
319
320
Chris Lattnerae640432002-12-17 02:50:10 +0000321/// isPhysRegAvailable - Return true if the specified physical register is free
322/// and available for use. This also includes checking to see if aliased
323/// registers are all free...
324///
325bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000326 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000327
328 // If the selected register aliases any other allocated registers, it is
329 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000330 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
331 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000332 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000333 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000334 return true;
335}
336
337
Chris Lattner91a452b2003-01-13 00:25:40 +0000338/// getFreeReg - Look to see if there is a free register available in the
339/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000340///
Chris Lattner91a452b2003-01-13 00:25:40 +0000341unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000342 // Get iterators defining the range of registers that are valid to allocate in
343 // this class, which also specifies the preferred allocation order.
344 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
345 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000346
Chris Lattner91a452b2003-01-13 00:25:40 +0000347 for (; RI != RE; ++RI)
348 if (isPhysRegAvailable(*RI)) { // Is reg unused?
349 assert(*RI != 0 && "Cannot use register!");
350 return *RI; // Found an unused register!
351 }
352 return 0;
353}
354
355
356/// liberatePhysReg - Make sure the specified physical register is available for
357/// use. If there is currently a value in it, it is either moved out of the way
358/// or spilled to memory.
359///
360void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000361 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000362 // FIXME: This code checks to see if a register is available, but it really
363 // wants to know if a reg is available BEFORE the instruction executes. If
364 // called after killed operands are freed, it runs the risk of reallocating a
365 // used operand...
366#if 0
367 if (isPhysRegAvailable(PhysReg)) return; // Already available...
368
369 // Check to see if the register is directly used, not indirectly used through
370 // aliases. If aliased registers are the ones actually used, we cannot be
371 // sure that we will be able to save the whole thing if we do a reg-reg copy.
Chris Lattner64667b62004-02-09 01:26:13 +0000372 if (PhysRegsUsed[PhysReg] != -1) {
373 // The virtual register held...
374 unsigned VirtReg = PhysRegsUsed[PhysReg]->second;
Chris Lattner91a452b2003-01-13 00:25:40 +0000375
376 // Check to see if there is a compatible register available. If so, we can
377 // move the value into the new register...
378 //
379 const TargetRegisterClass *RC = RegInfo->getRegClass(PhysReg);
380 if (unsigned NewReg = getFreeReg(RC)) {
381 // Emit the code to copy the value...
382 RegInfo->copyRegToReg(MBB, I, NewReg, PhysReg, RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000383
Chris Lattner91a452b2003-01-13 00:25:40 +0000384 // Update our internal state to indicate that PhysReg is available and Reg
385 // isn't.
Chris Lattnerecea5632004-02-09 02:12:04 +0000386 getVirt2PhysRegMapSlot[VirtReg] = 0;
Chris Lattner91a452b2003-01-13 00:25:40 +0000387 removePhysReg(PhysReg); // Free the physreg
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000388
Chris Lattner91a452b2003-01-13 00:25:40 +0000389 // Move reference over to new register...
390 assignVirtToPhysReg(VirtReg, NewReg);
391 return;
Chris Lattnerae640432002-12-17 02:50:10 +0000392 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000393 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000394#endif
395 spillPhysReg(MBB, I, PhysReg);
396}
397
398
399/// getReg - Find a physical register to hold the specified virtual
400/// register. If all compatible physical registers are used, this method spills
401/// the last used virtual register to the stack, and uses that register.
402///
403unsigned RA::getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000404 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000405 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
406
407 // First check to see if we have a free register of the requested type...
408 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000409
Chris Lattnerae640432002-12-17 02:50:10 +0000410 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000411 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000412 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000413
414 // Loop over all of the preallocated registers from the least recently used
415 // to the most recently used. When we find one that is capable of holding
416 // our register, use it.
417 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000418 assert(i != PhysRegsUseOrder.size() &&
419 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000420
Chris Lattnerae640432002-12-17 02:50:10 +0000421 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000422
423 // We can only use this register if it holds a virtual register (ie, it
424 // can be spilled). Do not use it if it is an explicitly allocated
425 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000426 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000427 "PhysReg in PhysRegsUseOrder, but is not allocated?");
428 if (PhysRegsUsed[R]) {
429 // If the current register is compatible, use it.
430 if (RegInfo->getRegClass(R) == RC) {
431 PhysReg = R;
432 break;
433 } else {
434 // If one of the registers aliased to the current register is
435 // compatible, use it.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000436 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
437 *AliasSet; ++AliasSet) {
438 if (RegInfo->getRegClass(*AliasSet) == RC) {
439 PhysReg = *AliasSet; // Take an aliased register
440 break;
441 }
442 }
Chris Lattner41822c72003-08-23 23:49:42 +0000443 }
Chris Lattnerae640432002-12-17 02:50:10 +0000444 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000445 }
446
Chris Lattnerae640432002-12-17 02:50:10 +0000447 assert(PhysReg && "Physical register not assigned!?!?");
448
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000449 // At this point PhysRegsUseOrder[i] is the least recently used register of
450 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000451 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000452 }
453
454 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000455 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000456 return PhysReg;
457}
458
Chris Lattnerae640432002-12-17 02:50:10 +0000459
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000460/// reloadVirtReg - This method loads the specified virtual register into a
461/// physical register, returning the physical register chosen. This updates the
462/// regalloc data structures to reflect the fact that the virtual reg is now
463/// alive in a physical register, and the previous one isn't.
464///
465unsigned RA::reloadVirtReg(MachineBasicBlock &MBB,
466 MachineBasicBlock::iterator &I,
467 unsigned VirtReg) {
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000468 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000469 MarkPhysRegRecentlyUsed(PR);
470 return PR; // Already have this value available!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000471 }
472
Chris Lattner91a452b2003-01-13 00:25:40 +0000473 unsigned PhysReg = getReg(MBB, I, VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000474
Chris Lattnerff863ba2002-12-25 05:05:46 +0000475 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner580f9be2002-12-28 20:40:43 +0000476 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000477
Chris Lattner91a452b2003-01-13 00:25:40 +0000478 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
479
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000480 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
481 << RegInfo->getName(PhysReg) << "\n");
482
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000483 // Add move instruction(s)
Chris Lattner580f9be2002-12-28 20:40:43 +0000484 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000485 ++NumReloaded; // Update statistics
486 return PhysReg;
487}
488
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000489
490
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000491void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
492 // loop over each instruction
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000493 MachineBasicBlock::iterator MI = MBB.begin();
494 for (; MI != MBB.end(); ++MI) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000495 const TargetInstrDescriptor &TID = TM->getInstrInfo().get(MI->getOpcode());
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000496 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
497 std::cerr << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000498 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
499 if (PhysRegsUsed[i] != -1)
500 std::cerr << "[" << RegInfo->getName(i)
501 << ",%reg" << PhysRegsUsed[i] << "] ";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000502 std::cerr << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000503
Chris Lattnerae640432002-12-17 02:50:10 +0000504 // Loop over the implicit uses, making sure that they are at the head of the
505 // use order list, so they don't get reallocated.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000506 for (const unsigned *ImplicitUses = TID.ImplicitUses;
507 *ImplicitUses; ++ImplicitUses)
Chris Lattnerecea5632004-02-09 02:12:04 +0000508 MarkPhysRegRecentlyUsed(*ImplicitUses);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000509
Brian Gaeke53b99a02003-08-15 21:19:25 +0000510 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000511 // incoming values if we are out of registers. Note that we completely
512 // ignore physical register uses here. We assume that if an explicit
513 // physical register is referenced by the instruction, that it is guaranteed
514 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000515 //
516 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000517 if (MI->getOperand(i).isUse() &&
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000518 !MI->getOperand(i).isDef() && MI->getOperand(i).isRegister() &&
519 MRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000520 unsigned VirtSrcReg = MI->getOperand(i).getAllocatedRegNum();
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000521 unsigned PhysSrcReg = reloadVirtReg(MBB, MI, VirtSrcReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000522 MI->SetMachineOperandReg(i, PhysSrcReg); // Assign the input register
523 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000524
Chris Lattner91a452b2003-01-13 00:25:40 +0000525 if (!DisableKill) {
526 // If this instruction is the last user of anything in registers, kill the
527 // value, freeing the register being used, so it doesn't need to be
528 // spilled to memory.
529 //
530 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
Chris Lattnerd5725632003-05-12 03:54:14 +0000531 KE = LV->killed_end(MI); KI != KE; ++KI) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000532 unsigned VirtReg = KI->second;
Chris Lattnerd5725632003-05-12 03:54:14 +0000533 unsigned PhysReg = VirtReg;
Chris Lattneref09c632004-01-31 21:27:19 +0000534 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000535 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
536 PhysReg = PhysRegSlot;
537 assert(PhysReg != 0);
538 PhysRegSlot = 0;
Chris Lattnerd5725632003-05-12 03:54:14 +0000539 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000540
Chris Lattnerd5725632003-05-12 03:54:14 +0000541 if (PhysReg) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000542 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
543 << "[%reg" << VirtReg <<"], removing it from live set\n");
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000544 removePhysReg(PhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000545 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000546 }
547 }
548
549 // Loop over all of the operands of the instruction, spilling registers that
550 // are defined, and marking explicit destinations in the PhysRegsUsed map.
551 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
Chris Lattner3d878d82004-02-10 20:41:10 +0000552 if (MI->getOperand(i).isDef() && MI->getOperand(i).isRegister() &&
553 MRegisterInfo::isPhysicalRegister(MI->getOperand(i).getReg())) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000554 unsigned Reg = MI->getOperand(i).getAllocatedRegNum();
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000555 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000556 PhysRegsUsed[Reg] = 0; // It is free and reserved now
557 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000558 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
559 *AliasSet; ++AliasSet) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000560 PhysRegsUseOrder.push_back(*AliasSet);
561 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000562 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000563 }
564
565 // Loop over the implicit defs, spilling them as well.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000566 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
567 *ImplicitDefs; ++ImplicitDefs) {
568 unsigned Reg = *ImplicitDefs;
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000569 spillPhysReg(MBB, MI, Reg);
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000570 PhysRegsUseOrder.push_back(Reg);
571 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000572 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
573 *AliasSet; ++AliasSet) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000574 PhysRegsUseOrder.push_back(*AliasSet);
575 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000576 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000577 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000578
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000579 // Okay, we have allocated all of the source operands and spilled any values
580 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner91a452b2003-01-13 00:25:40 +0000581 // implicit defs and assign them to a register, spilling incoming values if
582 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000583 //
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000584 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
Chris Lattner1cbe4d02004-02-10 21:12:22 +0000585 if (MI->getOperand(i).isDef() && MI->getOperand(i).isRegister() &&
586 MRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000587 unsigned DestVirtReg = MI->getOperand(i).getAllocatedRegNum();
588 unsigned DestPhysReg;
589
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000590 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000591 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000592 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000593 markVirtRegModified(DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000594 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
595 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000596
597 if (!DisableKill) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000598 // If this instruction defines any registers that are immediately dead,
599 // kill them now.
600 //
601 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
Chris Lattnerd5725632003-05-12 03:54:14 +0000602 KE = LV->dead_end(MI); KI != KE; ++KI) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000603 unsigned VirtReg = KI->second;
Chris Lattnerd5725632003-05-12 03:54:14 +0000604 unsigned PhysReg = VirtReg;
Chris Lattneref09c632004-01-31 21:27:19 +0000605 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000606 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
607 PhysReg = PhysRegSlot;
608 assert(PhysReg != 0);
609 PhysRegSlot = 0;
Chris Lattnerd5725632003-05-12 03:54:14 +0000610 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000611
Chris Lattnerd5725632003-05-12 03:54:14 +0000612 if (PhysReg) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000613 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
614 << " [%reg" << VirtReg
615 << "] is never used, removing it frame live list\n");
Chris Lattnerd5725632003-05-12 03:54:14 +0000616 removePhysReg(PhysReg);
617 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000618 }
619 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000620 }
621
622 // Rewind the iterator to point to the first flow control instruction...
Chris Lattner3501fea2003-01-14 22:00:31 +0000623 const TargetInstrInfo &TII = TM->getInstrInfo();
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000624 MI = MBB.end();
625 while (MI != MBB.begin() && TII.isTerminatorInstr((--MI)->getOpcode()));
626 ++MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000627
628 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000629 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
630 if (PhysRegsUsed[i] != -1)
631 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000632 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000633 else
634 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000635
Chris Lattnerecea5632004-02-09 02:12:04 +0000636#ifndef NDEBUG
637 bool AllOk = true;
638 for (unsigned i = 0, e = Virt2PhysRegMap.size(); i != e; ++i)
639 if (unsigned PR = Virt2PhysRegMap[i]) {
640 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
641 AllOk = false;
642 }
643 assert(AllOk && "Virtual registers still in phys regs?");
644#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000645
Chris Lattner128c2aa2003-08-17 18:01:15 +0000646 // Clear any physical register which appear live at the end of the basic
647 // block, but which do not hold any virtual registers. e.g., the stack
648 // pointer.
649 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000650}
651
Chris Lattner86c69a62002-12-17 03:16:10 +0000652
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000653/// runOnMachineFunction - Register allocate the whole function
654///
655bool RA::runOnMachineFunction(MachineFunction &Fn) {
656 DEBUG(std::cerr << "Machine Function " << "\n");
657 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000658 TM = &Fn.getTarget();
659 RegInfo = TM->getRegisterInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000660
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000661 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner64667b62004-02-09 01:26:13 +0000662
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000663 // initialize the virtual->physical register map to have a 'null'
664 // mapping for all virtual registers
665 Virt2PhysRegMap.assign(MF->getSSARegMap()->getNumVirtualRegs(), 0);
Chris Lattnerecea5632004-02-09 02:12:04 +0000666
Chris Lattner82bee0f2002-12-18 08:14:26 +0000667 if (!DisableKill)
Chris Lattner91a452b2003-01-13 00:25:40 +0000668 LV = &getAnalysis<LiveVariables>();
Chris Lattner82bee0f2002-12-18 08:14:26 +0000669
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000670 // Loop over all of the basic blocks, eliminating virtual register references
671 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
672 MBB != MBBe; ++MBB)
673 AllocateBasicBlock(*MBB);
674
Chris Lattner580f9be2002-12-28 20:40:43 +0000675 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000676 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000677 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000678 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000679 return true;
680}
681
Chris Lattneref09c632004-01-31 21:27:19 +0000682FunctionPass *llvm::createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000683 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000684}