blob: 194fc14848bc0494d531d0a0dc9956be36afbda2 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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
15#define DEBUG_TYPE "regalloc"
16#include "llvm/BasicBlock.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng04d9d0b2008-02-06 08:00:32 +000021#include "llvm/CodeGen/Passes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/RegAllocRegistry.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Owen Anderson8050fa12008-07-10 01:56:35 +000029#include "llvm/ADT/DenseMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/IndexedMap.h"
Evan Cheng548bc502009-01-29 02:20:59 +000031#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/Statistic.h"
Evan Chenga1d9dfb2008-02-06 19:16:53 +000034#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include <algorithm>
36using namespace llvm;
37
38STATISTIC(NumStores, "Number of stores added");
39STATISTIC(NumLoads , "Number of loads added");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
Dan Gohman089efff2008-05-13 00:00:25 +000041static RegisterRegAlloc
Dan Gohman669b9bf2008-10-14 20:25:08 +000042 localRegAlloc("local", "local register allocator",
Dan Gohman089efff2008-05-13 00:00:25 +000043 createLocalRegisterAllocator);
44
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000046 class RALocal : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 public:
48 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +000049 RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 private:
51 const TargetMachine *TM;
52 MachineFunction *MF;
Dan Gohman1e57df32008-02-10 18:45:23 +000053 const TargetRegisterInfo *TRI;
Owen Andersonbf15ae22008-01-07 01:35:56 +000054 const TargetInstrInfo *TII;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
57 // values are spilled.
Evan Cheng33dc9712008-07-10 18:23:23 +000058 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 // Virt2PhysRegMap - This map contains entries for each virtual register
61 // that is currently available in a physical register.
62 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
63
64 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
65 return Virt2PhysRegMap[VirtReg];
66 }
67
68 // PhysRegsUsed - This array is effectively a map, containing entries for
69 // each physical register that currently has a value (ie, it is in
70 // Virt2PhysRegMap). The value mapped to is the virtual register
71 // corresponding to the physical register (the inverse of the
72 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
73 // because it is used by a future instruction, and to -2 if it is not
74 // allocatable. If the entry for a physical register is -1, then the
75 // physical register is "not in the map".
76 //
77 std::vector<int> PhysRegsUsed;
78
79 // PhysRegsUseOrder - This contains a list of the physical registers that
80 // currently have a virtual register value in them. This list provides an
81 // ordering of registers, imposing a reallocation order. This list is only
82 // used if all registers are allocated and we have to spill one, in which
83 // case we spill the least recently used register. Entries at the front of
84 // the list are the least recently used registers, entries at the back are
85 // the most recently used.
86 //
87 std::vector<unsigned> PhysRegsUseOrder;
88
Evan Chenga94efbd2008-01-17 02:08:17 +000089 // Virt2LastUseMap - This maps each virtual register to its last use
90 // (MachineInstr*, operand index pair).
91 IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
92 Virt2LastUseMap;
93
94 std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
Dan Gohman1e57df32008-02-10 18:45:23 +000095 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Evan Chenga94efbd2008-01-17 02:08:17 +000096 return Virt2LastUseMap[Reg];
97 }
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 // VirtRegModified - This bitset contains information about which virtual
100 // registers need to be spilled back to memory when their registers are
101 // scavenged. If a virtual register has simply been rematerialized, there
102 // is no reason to spill it to memory when we need the register back.
103 //
Evan Cheng9e66d8c2008-01-17 00:35:26 +0000104 BitVector VirtRegModified;
Owen Anderson9196a392008-07-08 22:24:50 +0000105
106 // UsedInMultipleBlocks - Tracks whether a particular register is used in
107 // more than one block.
108 BitVector UsedInMultipleBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 void markVirtRegModified(unsigned Reg, bool Val = true) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000111 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
112 Reg -= TargetRegisterInfo::FirstVirtualRegister;
Evan Cheng9e66d8c2008-01-17 00:35:26 +0000113 if (Val)
114 VirtRegModified.set(Reg);
115 else
116 VirtRegModified.reset(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 }
118
119 bool isVirtRegModified(unsigned Reg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000120 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
121 assert(Reg - TargetRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 && "Illegal virtual register!");
Dan Gohman1e57df32008-02-10 18:45:23 +0000123 return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 }
125
126 void AddToPhysRegsUseOrder(unsigned Reg) {
127 std::vector<unsigned>::iterator It =
128 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
129 if (It != PhysRegsUseOrder.end())
130 PhysRegsUseOrder.erase(It);
131 PhysRegsUseOrder.push_back(Reg);
132 }
133
134 void MarkPhysRegRecentlyUsed(unsigned Reg) {
135 if (PhysRegsUseOrder.empty() ||
136 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
137
138 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
139 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
140 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
141 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
142 // Add it to the end of the list
143 PhysRegsUseOrder.push_back(RegMatch);
144 if (RegMatch == Reg)
145 return; // Found an exact match, exit early
146 }
147 }
148
149 public:
150 virtual const char *getPassName() const {
151 return "Local Register Allocator";
152 }
153
154 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +0000155 AU.setPreservesCFG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 AU.addRequiredID(PHIEliminationID);
157 AU.addRequiredID(TwoAddressInstructionPassID);
158 MachineFunctionPass::getAnalysisUsage(AU);
159 }
160
161 private:
162 /// runOnMachineFunction - Register allocate the whole function
163 bool runOnMachineFunction(MachineFunction &Fn);
164
165 /// AllocateBasicBlock - Register allocate the specified basic block.
166 void AllocateBasicBlock(MachineBasicBlock &MBB);
167
168
169 /// areRegsEqual - This method returns true if the specified registers are
170 /// related to each other. To do this, it checks to see if they are equal
171 /// or if the first register is in the alias set of the second register.
172 ///
173 bool areRegsEqual(unsigned R1, unsigned R2) const {
174 if (R1 == R2) return true;
Dan Gohman1e57df32008-02-10 18:45:23 +0000175 for (const unsigned *AliasSet = TRI->getAliasSet(R2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 *AliasSet; ++AliasSet) {
177 if (*AliasSet == R1) return true;
178 }
179 return false;
180 }
181
182 /// getStackSpaceFor - This returns the frame index of the specified virtual
183 /// register on the stack, allocating space if necessary.
184 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
185
186 /// removePhysReg - This method marks the specified physical register as no
187 /// longer being in use.
188 ///
189 void removePhysReg(unsigned PhysReg);
190
191 /// spillVirtReg - This method spills the value specified by PhysReg into
192 /// the virtual register slot specified by VirtReg. It then updates the RA
193 /// data structures to indicate the fact that PhysReg is now available.
194 ///
195 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
196 unsigned VirtReg, unsigned PhysReg);
197
198 /// spillPhysReg - This method spills the specified physical register into
199 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
200 /// true, then the request is ignored if the physical register does not
201 /// contain a virtual register.
202 ///
203 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
204 unsigned PhysReg, bool OnlyVirtRegs = false);
205
206 /// assignVirtToPhysReg - This method updates local state so that we know
207 /// that PhysReg is the proper container for VirtReg now. The physical
208 /// register must not be used for anything else when this is called.
209 ///
210 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
211
212 /// isPhysRegAvailable - Return true if the specified physical register is
213 /// free and available for use. This also includes checking to see if
214 /// aliased registers are all free...
215 ///
216 bool isPhysRegAvailable(unsigned PhysReg) const;
217
218 /// getFreeReg - Look to see if there is a free register available in the
219 /// specified register class. If not, return 0.
220 ///
221 unsigned getFreeReg(const TargetRegisterClass *RC);
222
223 /// getReg - Find a physical register to hold the specified virtual
224 /// register. If all compatible physical registers are used, this method
225 /// spills the last used virtual register to the stack, and uses that
Evan Cheng308d1852009-01-29 01:13:00 +0000226 /// register. If NoFree is true, that means the caller knows there isn't
227 /// a free register, do not call getFreeReg().
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng308d1852009-01-29 01:13:00 +0000229 unsigned VirtReg, bool NoFree = false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230
Bob Wilsond983fb42009-05-07 21:19:45 +0000231 /// reloadVirtReg - This method transforms the specified virtual
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 /// register use to refer to a physical register. This method may do this
233 /// in one of several ways: if the register is available in a physical
234 /// register already, it uses that physical register. If the value is not
235 /// in a physical register, and if there are physical registers available,
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000236 /// it loads it into a register: PhysReg if that is an available physical
237 /// register, otherwise any physical register of the right class.
238 /// If register pressure is high, and it is possible, it tries to fold the
239 /// load of the virtual register into the instruction itself. It avoids
240 /// doing this if register pressure is low to improve the chance that
241 /// subsequent instructions can use the reloaded value. This method
242 /// returns the modified instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 ///
244 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000245 unsigned OpNum, SmallSet<unsigned, 4> &RRegs,
246 unsigned PhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
Owen Andersonff01ccf2008-07-09 20:14:53 +0000248 /// ComputeLocalLiveness - Computes liveness of registers within a basic
249 /// block, setting the killed/dead flags as appropriate.
250 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
253 unsigned PhysReg);
254 };
255 char RALocal::ID = 0;
256}
257
258/// getStackSpaceFor - This allocates space for the specified virtual register
259/// to be held on the stack.
260int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
261 // Find the location Reg would belong...
Evan Cheng33dc9712008-07-10 18:23:23 +0000262 int SS = StackSlotForVirtReg[VirtReg];
263 if (SS != -1)
264 return SS; // Already has space allocated?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
266 // Allocate a new stack object for this spill location...
David Greene6424ab92009-11-12 20:49:22 +0000267 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
268 RC->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269
270 // Assign the slot...
Evan Cheng33dc9712008-07-10 18:23:23 +0000271 StackSlotForVirtReg[VirtReg] = FrameIdx;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 return FrameIdx;
273}
274
275
276/// removePhysReg - This method marks the specified physical register as no
277/// longer being in use.
278///
279void RALocal::removePhysReg(unsigned PhysReg) {
280 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
281
282 std::vector<unsigned>::iterator It =
283 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
284 if (It != PhysRegsUseOrder.end())
285 PhysRegsUseOrder.erase(It);
286}
287
288
289/// spillVirtReg - This method spills the value specified by PhysReg into the
290/// virtual register slot specified by VirtReg. It then updates the RA data
291/// structures to indicate the fact that PhysReg is now available.
292///
293void RALocal::spillVirtReg(MachineBasicBlock &MBB,
294 MachineBasicBlock::iterator I,
295 unsigned VirtReg, unsigned PhysReg) {
296 assert(VirtReg && "Spilling a physical register is illegal!"
297 " Must not have appropriate kill for the register or use exists beyond"
298 " the intended one.");
David Greene3dbc2a72010-01-05 01:26:05 +0000299 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
Bill Wendling9dcc0632009-08-22 20:38:09 +0000300 << " containing %reg" << VirtReg);
Owen Anderson81875432008-01-01 21:11:32 +0000301
Evan Chenga94efbd2008-01-17 02:08:17 +0000302 if (!isVirtRegModified(VirtReg)) {
David Greene3dbc2a72010-01-05 01:26:05 +0000303 DEBUG(dbgs() << " which has not been modified, so no store necessary!");
Evan Chenga94efbd2008-01-17 02:08:17 +0000304 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
305 if (LastUse.first)
306 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Chenga1d9dfb2008-02-06 19:16:53 +0000307 } else {
308 // Otherwise, there is a virtual register corresponding to this physical
309 // register. We only need to spill it into its stack slot if it has been
310 // modified.
Chris Lattner1b989192007-12-31 04:13:23 +0000311 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 int FrameIndex = getStackSpaceFor(VirtReg, RC);
David Greene3dbc2a72010-01-05 01:26:05 +0000313 DEBUG(dbgs() << " to stack slot #" << FrameIndex);
Evan Chenga1d9dfb2008-02-06 19:16:53 +0000314 // If the instruction reads the register that's spilled, (e.g. this can
315 // happen if it is a move to a physical register), then the spill
316 // instruction is not a kill.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000317 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Evan Chengb4272522008-02-11 08:30:52 +0000318 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 ++NumStores; // Update statistics
320 }
321
322 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
323
David Greene3dbc2a72010-01-05 01:26:05 +0000324 DEBUG(dbgs() << '\n');
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 removePhysReg(PhysReg);
326}
327
328
329/// spillPhysReg - This method spills the specified physical register into the
330/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
331/// then the request is ignored if the physical register does not contain a
332/// virtual register.
333///
334void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
335 unsigned PhysReg, bool OnlyVirtRegs) {
336 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
337 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
338 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
339 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
340 } else {
341 // If the selected register aliases any other registers, we must make
342 // sure that one of the aliases isn't alive.
Dan Gohman1e57df32008-02-10 18:45:23 +0000343 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 *AliasSet; ++AliasSet)
345 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
346 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
347 if (PhysRegsUsed[*AliasSet])
348 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
349 }
350}
351
352
353/// assignVirtToPhysReg - This method updates local state so that we know
354/// that PhysReg is the proper container for VirtReg now. The physical
355/// register must not be used for anything else when this is called.
356///
357void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
358 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
359 // Update information to note the fact that this register was just used, and
360 // it holds VirtReg.
361 PhysRegsUsed[PhysReg] = VirtReg;
362 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
363 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
364}
365
366
367/// isPhysRegAvailable - Return true if the specified physical register is free
368/// and available for use. This also includes checking to see if aliased
369/// registers are all free...
370///
371bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
372 if (PhysRegsUsed[PhysReg] != -1) return false;
373
374 // If the selected register aliases any other allocated registers, it is
375 // not free!
Dan Gohman1e57df32008-02-10 18:45:23 +0000376 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 *AliasSet; ++AliasSet)
Evan Chengf90128d2008-02-22 20:30:53 +0000378 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 return false; // Can't use this reg then.
380 return true;
381}
382
383
384/// getFreeReg - Look to see if there is a free register available in the
385/// specified register class. If not, return 0.
386///
387unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
388 // Get iterators defining the range of registers that are valid to allocate in
389 // this class, which also specifies the preferred allocation order.
390 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
391 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
392
393 for (; RI != RE; ++RI)
394 if (isPhysRegAvailable(*RI)) { // Is reg unused?
395 assert(*RI != 0 && "Cannot use register!");
396 return *RI; // Found an unused register!
397 }
398 return 0;
399}
400
401
402/// getReg - Find a physical register to hold the specified virtual
403/// register. If all compatible physical registers are used, this method spills
404/// the last used virtual register to the stack, and uses that register.
405///
406unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng308d1852009-01-29 01:13:00 +0000407 unsigned VirtReg, bool NoFree) {
Chris Lattner1b989192007-12-31 04:13:23 +0000408 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409
410 // First check to see if we have a free register of the requested type...
Evan Cheng308d1852009-01-29 01:13:00 +0000411 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
413 // If we didn't find an unused register, scavenge one now!
414 if (PhysReg == 0) {
415 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
416
417 // Loop over all of the preallocated registers from the least recently used
418 // to the most recently used. When we find one that is capable of holding
419 // our register, use it.
420 for (unsigned i = 0; PhysReg == 0; ++i) {
421 assert(i != PhysRegsUseOrder.size() &&
422 "Couldn't find a register of the appropriate class!");
423
424 unsigned R = PhysRegsUseOrder[i];
425
426 // We can only use this register if it holds a virtual register (ie, it
427 // can be spilled). Do not use it if it is an explicitly allocated
428 // physical register!
429 assert(PhysRegsUsed[R] != -1 &&
430 "PhysReg in PhysRegsUseOrder, but is not allocated?");
431 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
432 // If the current register is compatible, use it.
433 if (RC->contains(R)) {
434 PhysReg = R;
435 break;
436 } else {
437 // If one of the registers aliased to the current register is
438 // compatible, use it.
Dan Gohman1e57df32008-02-10 18:45:23 +0000439 for (const unsigned *AliasIt = TRI->getAliasSet(R);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 *AliasIt; ++AliasIt) {
441 if (RC->contains(*AliasIt) &&
442 // If this is pinned down for some reason, don't use it. For
443 // example, if CL is pinned, and we run across CH, don't use
444 // CH as justification for using scavenging ECX (which will
445 // fail).
446 PhysRegsUsed[*AliasIt] != 0 &&
447
448 // Make sure the register is allocatable. Don't allocate SIL on
449 // x86-32.
450 PhysRegsUsed[*AliasIt] != -2) {
451 PhysReg = *AliasIt; // Take an aliased register
452 break;
453 }
454 }
455 }
456 }
457 }
458
459 assert(PhysReg && "Physical register not assigned!?!?");
460
461 // At this point PhysRegsUseOrder[i] is the least recently used register of
462 // compatible register class. Spill it to memory and reap its remains.
463 spillPhysReg(MBB, I, PhysReg);
464 }
465
466 // Now that we know which register we need to assign this to, do it now!
467 assignVirtToPhysReg(VirtReg, PhysReg);
468 return PhysReg;
469}
470
471
Bob Wilson219866c2009-05-07 21:20:42 +0000472/// reloadVirtReg - This method transforms the specified virtual
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473/// register use to refer to a physical register. This method may do this in
474/// one of several ways: if the register is available in a physical register
475/// already, it uses that physical register. If the value is not in a physical
476/// register, and if there are physical registers available, it loads it into a
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000477/// register: PhysReg if that is an available physical register, otherwise any
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478/// register. If register pressure is high, and it is possible, it tries to
479/// fold the load of the virtual register into the instruction itself. It
480/// avoids doing this if register pressure is low to improve the chance that
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000481/// subsequent instructions can use the reloaded value. This method returns
482/// the modified instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483///
484MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng548bc502009-01-29 02:20:59 +0000485 unsigned OpNum,
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000486 SmallSet<unsigned, 4> &ReloadedRegs,
487 unsigned PhysReg) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 unsigned VirtReg = MI->getOperand(OpNum).getReg();
489
490 // If the virtual register is already available, just update the instruction
491 // and return.
492 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Dale Johannesen55057292010-02-16 01:27:47 +0000494 if (!MI->isDebugValue()) {
495 // Do not do these for DBG_VALUE as they can affect codegen.
496 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Dale Johannesene7dda272010-02-15 01:45:47 +0000497 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Dale Johannesen55057292010-02-16 01:27:47 +0000498 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 return MI;
500 }
501
502 // Otherwise, we need to fold it into the current instruction, or reload it.
503 // If we have registers available to hold the value, use them.
Chris Lattner1b989192007-12-31 04:13:23 +0000504 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000505 // If we already have a PhysReg (this happens when the instruction is a
506 // reg-to-reg copy with a PhysReg destination) use that.
507 if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) ||
508 !isPhysRegAvailable(PhysReg))
509 PhysReg = getFreeReg(RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 int FrameIndex = getStackSpaceFor(VirtReg, RC);
511
512 if (PhysReg) { // Register is available, allocate it!
513 assignVirtToPhysReg(VirtReg, PhysReg);
514 } else { // No registers available.
Evan Cheng71f91ed2008-02-07 19:46:55 +0000515 // Force some poor hapless value out of the register file to
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 // make room for the new register, and reload it.
Evan Cheng308d1852009-01-29 01:13:00 +0000517 PhysReg = getReg(MBB, MI, VirtReg, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 }
519
520 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
521
David Greene3dbc2a72010-01-05 01:26:05 +0000522 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
Bill Wendling9dcc0632009-08-22 20:38:09 +0000523 << TRI->getName(PhysReg) << "\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524
525 // Add move instruction(s)
Owen Anderson81875432008-01-01 21:11:32 +0000526 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 ++NumLoads; // Update statistics
528
Chris Lattner1b989192007-12-31 04:13:23 +0000529 MF->getRegInfo().setPhysRegUsed(PhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Chenga94efbd2008-01-17 02:08:17 +0000531 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng548bc502009-01-29 02:20:59 +0000532
533 if (!ReloadedRegs.insert(PhysReg)) {
Edwin Törökced9ff82009-07-11 13:10:19 +0000534 std::string msg;
535 raw_string_ostream Msg(msg);
536 Msg << "Ran out of registers during register allocation!";
Chris Lattner4052b292010-02-09 19:54:29 +0000537 if (MI->isInlineAsm()) {
Edwin Törökced9ff82009-07-11 13:10:19 +0000538 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng548bc502009-01-29 02:20:59 +0000539 << "constraints:\n";
Edwin Törökced9ff82009-07-11 13:10:19 +0000540 MI->print(Msg, TM);
Evan Cheng548bc502009-01-29 02:20:59 +0000541 }
Edwin Törökced9ff82009-07-11 13:10:19 +0000542 llvm_report_error(Msg.str());
Evan Cheng548bc502009-01-29 02:20:59 +0000543 }
544 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
545 *SubRegs; ++SubRegs) {
546 if (!ReloadedRegs.insert(*SubRegs)) {
Edwin Törökced9ff82009-07-11 13:10:19 +0000547 std::string msg;
548 raw_string_ostream Msg(msg);
549 Msg << "Ran out of registers during register allocation!";
Chris Lattner4052b292010-02-09 19:54:29 +0000550 if (MI->isInlineAsm()) {
Edwin Törökced9ff82009-07-11 13:10:19 +0000551 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng548bc502009-01-29 02:20:59 +0000552 << "constraints:\n";
Edwin Törökced9ff82009-07-11 13:10:19 +0000553 MI->print(Msg, TM);
Evan Cheng548bc502009-01-29 02:20:59 +0000554 }
Edwin Törökced9ff82009-07-11 13:10:19 +0000555 llvm_report_error(Msg.str());
Evan Cheng548bc502009-01-29 02:20:59 +0000556 }
557 }
558
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 return MI;
560}
561
562/// isReadModWriteImplicitKill - True if this is an implicit kill for a
563/// read/mod/write register, i.e. update partial register.
564static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
565 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
566 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000567 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 MO.isDef() && !MO.isDead())
569 return true;
570 }
571 return false;
572}
573
574/// isReadModWriteImplicitDef - True if this is an implicit def for a
575/// read/mod/write register, i.e. update partial register.
576static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
577 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
578 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000579 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 !MO.isDef() && MO.isKill())
581 return true;
582 }
583 return false;
584}
585
Owen Anderson9196a392008-07-08 22:24:50 +0000586// precedes - Helper function to determine with MachineInstr A
587// precedes MachineInstr B within the same MBB.
588static bool precedes(MachineBasicBlock::iterator A,
589 MachineBasicBlock::iterator B) {
590 if (A == B)
591 return false;
592
593 MachineBasicBlock::iterator I = A->getParent()->begin();
594 while (I != A->getParent()->end()) {
595 if (I == A)
596 return true;
597 else if (I == B)
598 return false;
599
600 ++I;
601 }
602
603 return false;
604}
605
Owen Andersonff01ccf2008-07-09 20:14:53 +0000606/// ComputeLocalLiveness - Computes liveness of registers within a basic
607/// block, setting the killed/dead flags as appropriate.
608void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Owen Anderson9196a392008-07-08 22:24:50 +0000609 MachineRegisterInfo& MRI = MBB.getParent()->getRegInfo();
610 // Keep track of the most recently seen previous use or def of each reg,
611 // so that we can update them with dead/kill markers.
Owen Anderson8050fa12008-07-10 01:56:35 +0000612 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson9196a392008-07-08 22:24:50 +0000613 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
614 I != E; ++I) {
Dale Johannesene7dda272010-02-15 01:45:47 +0000615 if (I->isDebugValue())
616 continue;
Owen Anderson9196a392008-07-08 22:24:50 +0000617 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
618 MachineOperand& MO = I->getOperand(i);
619 // Uses don't trigger any flags, but we need to save
620 // them for later. Also, we have to process these
621 // _before_ processing the defs, since an instr
622 // uses regs before it defs them.
Owen Andersona4d28702008-10-08 04:30:51 +0000623 if (MO.isReg() && MO.getReg() && MO.isUse()) {
Owen Anderson9196a392008-07-08 22:24:50 +0000624 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Andersona4d28702008-10-08 04:30:51 +0000625
626
627 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
628
Evan Cheng548bc502009-01-29 02:20:59 +0000629 const unsigned* Aliases = TRI->getAliasSet(MO.getReg());
630 if (Aliases) {
631 while (*Aliases) {
Owen Andersona4d28702008-10-08 04:30:51 +0000632 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Evan Cheng548bc502009-01-29 02:20:59 +0000633 alias = LastUseDef.find(*Aliases);
Owen Andersona4d28702008-10-08 04:30:51 +0000634
Evan Cheng548bc502009-01-29 02:20:59 +0000635 if (alias != LastUseDef.end() && alias->second.first != I)
636 LastUseDef[*Aliases] = std::make_pair(I, i);
Owen Andersona4d28702008-10-08 04:30:51 +0000637
Evan Cheng548bc502009-01-29 02:20:59 +0000638 ++Aliases;
Owen Andersona4d28702008-10-08 04:30:51 +0000639 }
640 }
641 }
Owen Anderson9196a392008-07-08 22:24:50 +0000642 }
643
644 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
645 MachineOperand& MO = I->getOperand(i);
646 // Defs others than 2-addr redefs _do_ trigger flag changes:
647 // - A def followed by a def is dead
648 // - A use followed by a def is a kill
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000649 if (MO.isReg() && MO.getReg() && MO.isDef()) {
Owen Anderson8050fa12008-07-10 01:56:35 +0000650 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson9196a392008-07-08 22:24:50 +0000651 last = LastUseDef.find(MO.getReg());
652 if (last != LastUseDef.end()) {
Owen Anderson348946a2008-07-10 01:53:01 +0000653 // Check if this is a two address instruction. If so, then
654 // the def does not kill the use.
Evan Chengf1107fd2008-07-10 07:35:43 +0000655 if (last->second.first == I &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000656 I->isRegTiedToUseOperand(i))
Evan Chengf1107fd2008-07-10 07:35:43 +0000657 continue;
Owen Anderson77162402008-07-09 21:15:10 +0000658
Owen Anderson9196a392008-07-08 22:24:50 +0000659 MachineOperand& lastUD =
660 last->second.first->getOperand(last->second.second);
661 if (lastUD.isDef())
662 lastUD.setIsDead(true);
Evan Chengf1107fd2008-07-10 07:35:43 +0000663 else
Owen Anderson9196a392008-07-08 22:24:50 +0000664 lastUD.setIsKill(true);
665 }
666
667 LastUseDef[MO.getReg()] = std::make_pair(I, i);
668 }
669 }
670 }
671
672 // Live-out (of the function) registers contain return values of the function,
673 // so we need to make sure they are alive at return time.
Bill Wendling617d39e2010-03-16 02:01:51 +0000674 MachineBasicBlock::iterator Ret = MBB.getFirstTerminator();
675 bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn());
676
677 if (BBEndsInReturn)
Owen Anderson9196a392008-07-08 22:24:50 +0000678 for (MachineRegisterInfo::liveout_iterator
679 I = MF->getRegInfo().liveout_begin(),
680 E = MF->getRegInfo().liveout_end(); I != E; ++I)
681 if (!Ret->readsRegister(*I)) {
682 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
683 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
684 }
Owen Anderson9196a392008-07-08 22:24:50 +0000685
686 // Finally, loop over the final use/def of each reg
687 // in the block and determine if it is dead.
Owen Anderson8050fa12008-07-10 01:56:35 +0000688 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson9196a392008-07-08 22:24:50 +0000689 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
690 MachineInstr* MI = I->second.first;
691 unsigned idx = I->second.second;
692 MachineOperand& MO = MI->getOperand(idx);
693
694 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
695
696 // A crude approximation of "live-out" calculation
697 bool usedOutsideBlock = isPhysReg ? false :
698 UsedInMultipleBlocks.test(MO.getReg() -
699 TargetRegisterInfo::FirstVirtualRegister);
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000700
701 // If the machine BB ends in a return instruction, then the value isn't used
702 // outside of the BB.
703 if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
Dale Johannesen55057292010-02-16 01:27:47 +0000704 // DBG_VALUE complicates this: if the only refs of a register outside
705 // this block are DBG_VALUE, we can't keep the reg live just for that,
706 // as it will cause the reg to be spilled at the end of this block when
707 // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
708 // happens.
709 bool UsedByDebugValueOnly = false;
Owen Anderson9196a392008-07-08 22:24:50 +0000710 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000711 UE = MRI.reg_end(); UI != UE; ++UI) {
Owen Anderson9196a392008-07-08 22:24:50 +0000712 // Two cases:
713 // - used in another block
714 // - used in the same block before it is defined (loop)
715 if (UI->getParent() != &MBB ||
Owen Anderson074e69a2008-07-08 23:36:37 +0000716 (MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) {
Dale Johannesen55057292010-02-16 01:27:47 +0000717 if (UI->isDebugValue()) {
718 UsedByDebugValueOnly = true;
719 continue;
720 }
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000721
Dale Johannesen55057292010-02-16 01:27:47 +0000722 // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
Owen Anderson9196a392008-07-08 22:24:50 +0000723 UsedInMultipleBlocks.set(MO.getReg() -
724 TargetRegisterInfo::FirstVirtualRegister);
725 usedOutsideBlock = true;
Dale Johannesen55057292010-02-16 01:27:47 +0000726 UsedByDebugValueOnly = false;
Owen Anderson9196a392008-07-08 22:24:50 +0000727 break;
728 }
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000729 }
730
Dale Johannesen55057292010-02-16 01:27:47 +0000731 if (UsedByDebugValueOnly)
732 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
733 UE = MRI.reg_end(); UI != UE; ++UI)
734 if (UI->isDebugValue() &&
735 (UI->getParent() != &MBB ||
736 (MO.isDef() && precedes(&*UI, MI))))
737 UI.getOperand().setReg(0U);
738 }
739
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000740 // Physical registers and those that are not live-out of the block are
741 // killed/dead at their last use/def within this block.
742 if (isPhysReg || !usedOutsideBlock || BBEndsInReturn)
Dan Gohmanec06ecd2008-10-04 00:31:14 +0000743 if (MO.isUse()) {
744 // Don't mark uses that are tied to defs as kills.
Evan Cheng48555e82009-03-19 20:30:06 +0000745 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohmanec06ecd2008-10-04 00:31:14 +0000746 MO.setIsKill(true);
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000747 } else {
Owen Anderson9196a392008-07-08 22:24:50 +0000748 MO.setIsDead(true);
Bill Wendling5c3a53f2010-03-16 01:05:35 +0000749 }
Owen Anderson9196a392008-07-08 22:24:50 +0000750 }
Owen Andersonff01ccf2008-07-09 20:14:53 +0000751}
752
753void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
754 // loop over each instruction
755 MachineBasicBlock::iterator MII = MBB.begin();
756
Bill Wendling9dcc0632009-08-22 20:38:09 +0000757 DEBUG({
758 const BasicBlock *LBB = MBB.getBasicBlock();
759 if (LBB)
David Greene3dbc2a72010-01-05 01:26:05 +0000760 dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName();
Bill Wendling9dcc0632009-08-22 20:38:09 +0000761 });
Owen Andersonff01ccf2008-07-09 20:14:53 +0000762
Evan Chengb0f4d5f2009-01-29 18:37:30 +0000763 // Add live-in registers as active.
764 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Andersonff01ccf2008-07-09 20:14:53 +0000765 E = MBB.livein_end(); I != E; ++I) {
Evan Chengb0f4d5f2009-01-29 18:37:30 +0000766 unsigned Reg = *I;
767 MF->getRegInfo().setPhysRegUsed(Reg);
768 PhysRegsUsed[Reg] = 0; // It is free and reserved now
769 AddToPhysRegsUseOrder(Reg);
770 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
771 *SubRegs; ++SubRegs) {
772 if (PhysRegsUsed[*SubRegs] != -2) {
773 AddToPhysRegsUseOrder(*SubRegs);
774 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
775 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Owen Andersonff01ccf2008-07-09 20:14:53 +0000776 }
Evan Chengb0f4d5f2009-01-29 18:37:30 +0000777 }
Owen Andersonff01ccf2008-07-09 20:14:53 +0000778 }
779
780 ComputeLocalLiveness(MBB);
Owen Anderson9196a392008-07-08 22:24:50 +0000781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 // Otherwise, sequentially allocate each instruction in the MBB.
783 while (MII != MBB.end()) {
784 MachineInstr *MI = MII++;
Chris Lattner5b930372008-01-07 07:27:27 +0000785 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendling9dcc0632009-08-22 20:38:09 +0000786 DEBUG({
David Greene3dbc2a72010-01-05 01:26:05 +0000787 dbgs() << "\nStarting RegAlloc of: " << *MI;
788 dbgs() << " Regs have values: ";
Bill Wendling9dcc0632009-08-22 20:38:09 +0000789 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
790 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
David Greene3dbc2a72010-01-05 01:26:05 +0000791 dbgs() << "[" << TRI->getName(i)
Bill Wendling9dcc0632009-08-22 20:38:09 +0000792 << ",%reg" << PhysRegsUsed[i] << "] ";
David Greene3dbc2a72010-01-05 01:26:05 +0000793 dbgs() << '\n';
Bill Wendling9dcc0632009-08-22 20:38:09 +0000794 });
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000796 // Determine whether this is a copy instruction. The cases where the
797 // source or destination are phys regs are handled specially.
798 unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg;
Dale Johannesenda4d84a2010-02-03 01:40:33 +0000799 unsigned SrcCopyPhysReg = 0U;
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000800 bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
801 SrcCopySubReg, DstCopySubReg);
Dale Johannesenda4d84a2010-02-03 01:40:33 +0000802 if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg))
803 SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg);
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 // Loop over the implicit uses, making sure that they are at the head of the
806 // use order list, so they don't get reallocated.
807 if (TID.ImplicitUses) {
808 for (const unsigned *ImplicitUses = TID.ImplicitUses;
809 *ImplicitUses; ++ImplicitUses)
810 MarkPhysRegRecentlyUsed(*ImplicitUses);
811 }
812
813 SmallVector<unsigned, 8> Kills;
814 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
815 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000816 if (MO.isReg() && MO.isKill()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 if (!MO.isImplicit())
818 Kills.push_back(MO.getReg());
819 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
820 // These are extra physical register kills when a sub-register
821 // is defined (def of a sub-register is a read/mod/write of the
822 // larger registers). Ignore.
823 Kills.push_back(MO.getReg());
824 }
825 }
826
Dale Johannesen47e30e42008-09-24 23:13:09 +0000827 // If any physical regs are earlyclobber, spill any value they might
828 // have in them, then mark them unallocatable.
829 // If any virtual regs are earlyclobber, allocate them now (before
830 // freeing inputs that are killed).
Chris Lattner4052b292010-02-09 19:54:29 +0000831 if (MI->isInlineAsm()) {
Dale Johannesen47e30e42008-09-24 23:13:09 +0000832 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
833 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000834 if (MO.isReg() && MO.isDef() && MO.isEarlyClobber() &&
Dale Johannesen47e30e42008-09-24 23:13:09 +0000835 MO.getReg()) {
836 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
837 unsigned DestVirtReg = MO.getReg();
838 unsigned DestPhysReg;
839
840 // If DestVirtReg already has a value, use it.
841 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
842 DestPhysReg = getReg(MBB, MI, DestVirtReg);
843 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
844 markVirtRegModified(DestVirtReg);
845 getVirtRegLastUse(DestVirtReg) =
846 std::make_pair((MachineInstr*)0, 0);
David Greene3dbc2a72010-01-05 01:26:05 +0000847 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
Bill Wendling9dcc0632009-08-22 20:38:09 +0000848 << " to %reg" << DestVirtReg << "\n");
Dale Johannesen47e30e42008-09-24 23:13:09 +0000849 MO.setReg(DestPhysReg); // Assign the earlyclobber register
850 } else {
851 unsigned Reg = MO.getReg();
852 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
853 // These are extra physical register defs when a sub-register
854 // is defined (def of a sub-register is a read/mod/write of the
855 // larger registers). Ignore.
856 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
857
858 MF->getRegInfo().setPhysRegUsed(Reg);
859 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
860 PhysRegsUsed[Reg] = 0; // It is free and reserved now
861 AddToPhysRegsUseOrder(Reg);
862
Evan Cheng548bc502009-01-29 02:20:59 +0000863 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
864 *SubRegs; ++SubRegs) {
865 if (PhysRegsUsed[*SubRegs] != -2) {
866 MF->getRegInfo().setPhysRegUsed(*SubRegs);
867 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
868 AddToPhysRegsUseOrder(*SubRegs);
Dale Johannesen47e30e42008-09-24 23:13:09 +0000869 }
870 }
871 }
872 }
873 }
874 }
875
Dale Johannesene186ea02010-02-10 00:11:11 +0000876 // If a DBG_VALUE says something is located in a spilled register,
877 // change the DBG_VALUE to be undef, which prevents the register
Dale Johannesen3940d842010-01-30 00:57:47 +0000878 // from being reloaded here. Doing that would change the generated
879 // code, unless another use immediately follows this instruction.
Chris Lattner4052b292010-02-09 19:54:29 +0000880 if (MI->isDebugValue() &&
Dale Johannesen3940d842010-01-30 00:57:47 +0000881 MI->getNumOperands()==3 && MI->getOperand(0).isReg()) {
882 unsigned VirtReg = MI->getOperand(0).getReg();
883 if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) &&
884 !getVirt2PhysRegMapSlot(VirtReg))
885 MI->getOperand(0).setReg(0U);
886 }
887
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 // Get the used operands into registers. This has the potential to spill
889 // incoming values if we are out of registers. Note that we completely
890 // ignore physical register uses here. We assume that if an explicit
891 // physical register is referenced by the instruction, that it is guaranteed
892 // to be live-in, or the input is badly hosed.
893 //
Evan Cheng548bc502009-01-29 02:20:59 +0000894 SmallSet<unsigned, 4> ReloadedRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
896 MachineOperand& MO = MI->getOperand(i);
897 // here we are looking for only used operands (never def&use)
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000898 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000899 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Dale Johannesen5d25f9b2009-12-16 00:29:41 +0000900 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs,
901 isCopy ? DstCopyReg : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 }
903
904 // If this instruction is the last user of this register, kill the
905 // value, freeing the register being used, so it doesn't need to be
906 // spilled to memory.
907 //
908 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
909 unsigned VirtReg = Kills[i];
910 unsigned PhysReg = VirtReg;
Dan Gohman1e57df32008-02-10 18:45:23 +0000911 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 // If the virtual register was never materialized into a register, it
913 // might not be in the map, but it won't hurt to zero it out anyway.
914 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
915 PhysReg = PhysRegSlot;
916 PhysRegSlot = 0;
917 } else if (PhysRegsUsed[PhysReg] == -2) {
918 // Unallocatable register dead, ignore.
919 continue;
920 } else {
Evan Cheng358d8dd2007-10-22 19:42:28 +0000921 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 "Silently clearing a virtual register?");
923 }
924
925 if (PhysReg) {
David Greene3dbc2a72010-01-05 01:26:05 +0000926 DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg)
Bill Wendling9dcc0632009-08-22 20:38:09 +0000927 << "[%reg" << VirtReg <<"], removing it from live set\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 removePhysReg(PhysReg);
Evan Cheng548bc502009-01-29 02:20:59 +0000929 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
930 *SubRegs; ++SubRegs) {
931 if (PhysRegsUsed[*SubRegs] != -2) {
David Greene3dbc2a72010-01-05 01:26:05 +0000932 DEBUG(dbgs() << " Last use of "
Bill Wendling9dcc0632009-08-22 20:38:09 +0000933 << TRI->getName(*SubRegs) << "[%reg" << VirtReg
934 <<"], removing it from live set\n");
Evan Cheng548bc502009-01-29 02:20:59 +0000935 removePhysReg(*SubRegs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 }
937 }
938 }
939 }
940
941 // Loop over all of the operands of the instruction, spilling registers that
942 // are defined, and marking explicit destinations in the PhysRegsUsed map.
943 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
944 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000945 if (MO.isReg() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Dale Johannesen47e30e42008-09-24 23:13:09 +0000946 !MO.isEarlyClobber() &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000947 TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 unsigned Reg = MO.getReg();
949 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
950 // These are extra physical register defs when a sub-register
951 // is defined (def of a sub-register is a read/mod/write of the
952 // larger registers). Ignore.
953 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
954
Chris Lattner1b989192007-12-31 04:13:23 +0000955 MF->getRegInfo().setPhysRegUsed(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
957 PhysRegsUsed[Reg] = 0; // It is free and reserved now
958 AddToPhysRegsUseOrder(Reg);
959
Evan Cheng548bc502009-01-29 02:20:59 +0000960 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
961 *SubRegs; ++SubRegs) {
962 if (PhysRegsUsed[*SubRegs] != -2) {
963 MF->getRegInfo().setPhysRegUsed(*SubRegs);
964 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
965 AddToPhysRegsUseOrder(*SubRegs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 }
967 }
968 }
969 }
970
971 // Loop over the implicit defs, spilling them as well.
972 if (TID.ImplicitDefs) {
973 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
974 *ImplicitDefs; ++ImplicitDefs) {
975 unsigned Reg = *ImplicitDefs;
976 if (PhysRegsUsed[Reg] != -2) {
977 spillPhysReg(MBB, MI, Reg, true);
978 AddToPhysRegsUseOrder(Reg);
979 PhysRegsUsed[Reg] = 0; // It is free and reserved now
980 }
Chris Lattner1b989192007-12-31 04:13:23 +0000981 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng548bc502009-01-29 02:20:59 +0000982 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
983 *SubRegs; ++SubRegs) {
984 if (PhysRegsUsed[*SubRegs] != -2) {
985 AddToPhysRegsUseOrder(*SubRegs);
986 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
987 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 }
989 }
990 }
991 }
992
993 SmallVector<unsigned, 8> DeadDefs;
994 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
995 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000996 if (MO.isReg() && MO.isDead())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 DeadDefs.push_back(MO.getReg());
998 }
999
1000 // Okay, we have allocated all of the source operands and spilled any values
1001 // that would be destroyed by defs of this instruction. Loop over the
1002 // explicit defs and assign them to a register, spilling incoming values if
1003 // we need to scavenge a register.
1004 //
1005 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1006 MachineOperand& MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001007 if (MO.isReg() && MO.isDef() && MO.getReg() &&
Dale Johannesen47e30e42008-09-24 23:13:09 +00001008 !MO.isEarlyClobber() &&
Dan Gohman1e57df32008-02-10 18:45:23 +00001009 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 unsigned DestVirtReg = MO.getReg();
1011 unsigned DestPhysReg;
1012
1013 // If DestVirtReg already has a value, use it.
Dale Johannesen5d25f9b2009-12-16 00:29:41 +00001014 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) {
Dale Johannesenda4d84a2010-02-03 01:40:33 +00001015 // If this is a copy try to reuse the input as the output;
1016 // that will make the copy go away.
Dale Johannesen5d25f9b2009-12-16 00:29:41 +00001017 // If this is a copy, the source reg is a phys reg, and
1018 // that reg is available, use that phys reg for DestPhysReg.
Dale Johannesenda4d84a2010-02-03 01:40:33 +00001019 // If this is a copy, the source reg is a virtual reg, and
1020 // the phys reg that was assigned to that virtual reg is now
1021 // available, use that phys reg for DestPhysReg. (If it's now
1022 // available that means this was the last use of the source.)
Dale Johannesen5d25f9b2009-12-16 00:29:41 +00001023 if (isCopy &&
1024 TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) &&
1025 isPhysRegAvailable(SrcCopyReg)) {
1026 DestPhysReg = SrcCopyReg;
1027 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
Dale Johannesenda4d84a2010-02-03 01:40:33 +00001028 } else if (isCopy &&
1029 TargetRegisterInfo::isVirtualRegister(SrcCopyReg) &&
1030 SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) &&
1031 MF->getRegInfo().getRegClass(DestVirtReg)->
1032 contains(SrcCopyPhysReg)) {
1033 DestPhysReg = SrcCopyPhysReg;
1034 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
Dale Johannesen5d25f9b2009-12-16 00:29:41 +00001035 } else
1036 DestPhysReg = getReg(MBB, MI, DestVirtReg);
1037 }
Chris Lattner1b989192007-12-31 04:13:23 +00001038 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 markVirtRegModified(DestVirtReg);
Evan Chenga94efbd2008-01-17 02:08:17 +00001040 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
David Greene3dbc2a72010-01-05 01:26:05 +00001041 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
Bill Wendling9dcc0632009-08-22 20:38:09 +00001042 << " to %reg" << DestVirtReg << "\n");
Dan Gohman7f31037a2008-07-09 20:12:26 +00001043 MO.setReg(DestPhysReg); // Assign the output register
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 }
1045 }
1046
1047 // If this instruction defines any registers that are immediately dead,
1048 // kill them now.
1049 //
1050 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
1051 unsigned VirtReg = DeadDefs[i];
1052 unsigned PhysReg = VirtReg;
Dan Gohman1e57df32008-02-10 18:45:23 +00001053 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
1055 PhysReg = PhysRegSlot;
1056 assert(PhysReg != 0);
1057 PhysRegSlot = 0;
1058 } else if (PhysRegsUsed[PhysReg] == -2) {
1059 // Unallocatable register dead, ignore.
1060 continue;
1061 }
1062
1063 if (PhysReg) {
David Greene3dbc2a72010-01-05 01:26:05 +00001064 DEBUG(dbgs() << " Register " << TRI->getName(PhysReg)
Bill Wendling9dcc0632009-08-22 20:38:09 +00001065 << " [%reg" << VirtReg
1066 << "] is never used, removing it from live set\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 removePhysReg(PhysReg);
Dan Gohman1e57df32008-02-10 18:45:23 +00001068 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 *AliasSet; ++AliasSet) {
1070 if (PhysRegsUsed[*AliasSet] != -2) {
David Greene3dbc2a72010-01-05 01:26:05 +00001071 DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet)
Bill Wendling9dcc0632009-08-22 20:38:09 +00001072 << " [%reg" << *AliasSet
1073 << "] is never used, removing it from live set\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 removePhysReg(*AliasSet);
1075 }
1076 }
1077 }
1078 }
1079
Bob Wilsona43eb6b2009-05-07 23:47:03 +00001080 // Finally, if this is a noop copy instruction, zap it. (Except that if
1081 // the copy is dead, it must be kept to avoid messing up liveness info for
1082 // the register scavenger. See pr4100.)
Dale Johannesen5d25f9b2009-12-16 00:29:41 +00001083 if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
1084 SrcCopySubReg, DstCopySubReg) &&
1085 SrcCopyReg == DstCopyReg && DeadDefs.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 MBB.erase(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087 }
1088
1089 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
1090
1091 // Spill all physical registers holding virtual registers now.
Dan Gohman1e57df32008-02-10 18:45:23 +00001092 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +00001093 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 if (unsigned VirtReg = PhysRegsUsed[i])
1095 spillVirtReg(MBB, MI, VirtReg, i);
1096 else
1097 removePhysReg(i);
Anton Korobeynikov6a4a9332008-02-20 12:07:57 +00001098 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099
1100#if 0
1101 // This checking code is very expensive.
1102 bool AllOk = true;
Dan Gohman1e57df32008-02-10 18:45:23 +00001103 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner1b989192007-12-31 04:13:23 +00001104 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 if (unsigned PR = Virt2PhysRegMap[i]) {
1106 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
1107 AllOk = false;
1108 }
1109 assert(AllOk && "Virtual registers still in phys regs?");
1110#endif
1111
1112 // Clear any physical register which appear live at the end of the basic
1113 // block, but which do not hold any virtual registers. e.g., the stack
1114 // pointer.
1115 PhysRegsUseOrder.clear();
1116}
1117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118/// runOnMachineFunction - Register allocate the whole function
1119///
1120bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
David Greene3dbc2a72010-01-05 01:26:05 +00001121 DEBUG(dbgs() << "Machine Function\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 MF = &Fn;
1123 TM = &Fn.getTarget();
Dan Gohman1e57df32008-02-10 18:45:23 +00001124 TRI = TM->getRegisterInfo();
Owen Andersonbf15ae22008-01-07 01:35:56 +00001125 TII = TM->getInstrInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126
Dan Gohman1e57df32008-02-10 18:45:23 +00001127 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128
1129 // At various places we want to efficiently check to see whether a register
1130 // is allocatable. To handle this, we mark all unallocatable registers as
1131 // being pinned down, permanently.
1132 {
Dan Gohman1e57df32008-02-10 18:45:23 +00001133 BitVector Allocable = TRI->getAllocatableSet(Fn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1135 if (!Allocable[i])
1136 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1137 }
1138
1139 // initialize the virtual->physical register map to have a 'null'
1140 // mapping for all virtual registers
Evan Cheng9e66d8c2008-01-17 00:35:26 +00001141 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Cheng33dc9712008-07-10 18:23:23 +00001142 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng9e66d8c2008-01-17 00:35:26 +00001143 Virt2PhysRegMap.grow(LastVirtReg);
Evan Chenga94efbd2008-01-17 02:08:17 +00001144 Virt2LastUseMap.grow(LastVirtReg);
Dan Gohman1e57df32008-02-10 18:45:23 +00001145 VirtRegModified.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson9196a392008-07-08 22:24:50 +00001146 UsedInMultipleBlocks.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
1147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 // Loop over all of the basic blocks, eliminating virtual register references
1149 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1150 MBB != MBBe; ++MBB)
1151 AllocateBasicBlock(*MBB);
1152
1153 StackSlotForVirtReg.clear();
1154 PhysRegsUsed.clear();
1155 VirtRegModified.clear();
Owen Anderson9196a392008-07-08 22:24:50 +00001156 UsedInMultipleBlocks.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 Virt2PhysRegMap.clear();
Evan Chenga94efbd2008-01-17 02:08:17 +00001158 Virt2LastUseMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 return true;
1160}
1161
1162FunctionPass *llvm::createLocalRegisterAllocator() {
1163 return new RALocal();
1164}