blob: 8d19b694715412c720d1f5b65ec481b2b46e2adb [file] [log] [blame]
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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>
28
Chris Lattnerb74e83c2002-12-16 16:15:28 +000029namespace {
30 Statistic<> NumSpilled ("ra-local", "Number of registers spilled");
31 Statistic<> NumReloaded("ra-local", "Number of registers reloaded");
Chris Lattner3e430262003-10-24 20:05:58 +000032 cl::opt<bool> DisableKill("disable-kill", cl::Hidden,
Chris Lattner82bee0f2002-12-18 08:14:26 +000033 cl::desc("Disable register kill in local-ra"));
Chris Lattnerb74e83c2002-12-16 16:15:28 +000034
Chris Lattner580f9be2002-12-28 20:40:43 +000035 class RA : public MachineFunctionPass {
36 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000037 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000038 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000039 LiveVariables *LV;
Chris Lattnerff863ba2002-12-25 05:05:46 +000040
Chris Lattnerb8822ad2003-08-04 23:36:39 +000041 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
42 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000043 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000044
45 // Virt2PhysRegMap - This map contains entries for each virtual register
46 // that is currently available in a physical register.
47 //
48 std::map<unsigned, unsigned> Virt2PhysRegMap;
49
50 // PhysRegsUsed - This map contains entries for each physical register that
51 // currently has a value (ie, it is in Virt2PhysRegMap). The value mapped
52 // to is the virtual register corresponding to the physical register (the
53 // inverse of the Virt2PhysRegMap), or 0. The value is set to 0 if this
54 // register is pinned because it is used by a future instruction.
55 //
56 std::map<unsigned, unsigned> PhysRegsUsed;
57
58 // PhysRegsUseOrder - This contains a list of the physical registers that
59 // currently have a virtual register value in them. This list provides an
60 // ordering of registers, imposing a reallocation order. This list is only
61 // used if all registers are allocated and we have to spill one, in which
62 // case we spill the least recently used register. Entries at the front of
63 // the list are the least recently used registers, entries at the back are
64 // the most recently used.
65 //
66 std::vector<unsigned> PhysRegsUseOrder;
67
Chris Lattner91a452b2003-01-13 00:25:40 +000068 // VirtRegModified - This bitset contains information about which virtual
69 // registers need to be spilled back to memory when their registers are
70 // scavenged. If a virtual register has simply been rematerialized, there
71 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000072 //
Chris Lattner91a452b2003-01-13 00:25:40 +000073 std::vector<bool> VirtRegModified;
74
75 void markVirtRegModified(unsigned Reg, bool Val = true) {
76 assert(Reg >= MRegisterInfo::FirstVirtualRegister && "Illegal VirtReg!");
77 Reg -= MRegisterInfo::FirstVirtualRegister;
78 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
79 VirtRegModified[Reg] = Val;
80 }
81
82 bool isVirtRegModified(unsigned Reg) const {
83 assert(Reg >= MRegisterInfo::FirstVirtualRegister && "Illegal VirtReg!");
84 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
85 && "Illegal virtual register!");
86 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
87 }
Chris Lattner82bee0f2002-12-18 08:14:26 +000088
Chris Lattnerb74e83c2002-12-16 16:15:28 +000089 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner82bee0f2002-12-18 08:14:26 +000090 assert(!PhysRegsUseOrder.empty() && "No registers used!");
Chris Lattner0eb172c2002-12-24 00:04:55 +000091 if (PhysRegsUseOrder.back() == Reg) return; // Already most recently used
92
93 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
94 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
95 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
96 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
97 // Add it to the end of the list
98 PhysRegsUseOrder.push_back(RegMatch);
99 if (RegMatch == Reg)
100 return; // Found an exact match, exit early
101 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000102 }
103
104 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000105 virtual const char *getPassName() const {
106 return "Local Register Allocator";
107 }
108
Chris Lattner91a452b2003-01-13 00:25:40 +0000109 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
110 if (!DisableKill)
111 AU.addRequired<LiveVariables>();
112 AU.addRequiredID(PHIEliminationID);
113 MachineFunctionPass::getAnalysisUsage(AU);
114 }
115
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000116 private:
117 /// runOnMachineFunction - Register allocate the whole function
118 bool runOnMachineFunction(MachineFunction &Fn);
119
120 /// AllocateBasicBlock - Register allocate the specified basic block.
121 void AllocateBasicBlock(MachineBasicBlock &MBB);
122
Chris Lattner82bee0f2002-12-18 08:14:26 +0000123
Chris Lattner82bee0f2002-12-18 08:14:26 +0000124 /// areRegsEqual - This method returns true if the specified registers are
125 /// related to each other. To do this, it checks to see if they are equal
126 /// or if the first register is in the alias set of the second register.
127 ///
128 bool areRegsEqual(unsigned R1, unsigned R2) const {
129 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000130 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
131 *AliasSet; ++AliasSet) {
132 if (*AliasSet == R1) return true;
133 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000134 return false;
135 }
136
Chris Lattner580f9be2002-12-28 20:40:43 +0000137 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000138 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000139 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000140
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000141 /// removePhysReg - This method marks the specified physical register as no
142 /// longer being in use.
143 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000144 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000145
146 /// spillVirtReg - This method spills the value specified by PhysReg into
147 /// the virtual register slot specified by VirtReg. It then updates the RA
148 /// data structures to indicate the fact that PhysReg is now available.
149 ///
150 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
151 unsigned VirtReg, unsigned PhysReg);
152
Chris Lattnerc21be922002-12-16 17:44:42 +0000153 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000154 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
155 /// true, then the request is ignored if the physical register does not
156 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000157 ///
Chris Lattnerc21be922002-12-16 17:44:42 +0000158 void spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000159 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000160
Chris Lattner91a452b2003-01-13 00:25:40 +0000161 /// assignVirtToPhysReg - This method updates local state so that we know
162 /// that PhysReg is the proper container for VirtReg now. The physical
163 /// register must not be used for anything else when this is called.
164 ///
165 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
166
167 /// liberatePhysReg - Make sure the specified physical register is available
168 /// for use. If there is currently a value in it, it is either moved out of
169 /// the way or spilled to memory.
170 ///
171 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
172 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000173
Chris Lattnerae640432002-12-17 02:50:10 +0000174 /// isPhysRegAvailable - Return true if the specified physical register is
175 /// free and available for use. This also includes checking to see if
176 /// aliased registers are all free...
177 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000178 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000179
180 /// getFreeReg - Look to see if there is a free register available in the
181 /// specified register class. If not, return 0.
182 ///
183 unsigned getFreeReg(const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000184
Chris Lattner91a452b2003-01-13 00:25:40 +0000185 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000186 /// register. If all compatible physical registers are used, this method
187 /// spills the last used virtual register to the stack, and uses that
188 /// register.
189 ///
Chris Lattner91a452b2003-01-13 00:25:40 +0000190 unsigned getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
191 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000192
193 /// reloadVirtReg - This method loads the specified virtual register into a
194 /// physical register, returning the physical register chosen. This updates
195 /// the regalloc data structures to reflect the fact that the virtual reg is
196 /// now alive in a physical register, and the previous one isn't.
197 ///
198 unsigned reloadVirtReg(MachineBasicBlock &MBB,
199 MachineBasicBlock::iterator &I, unsigned VirtReg);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000200
201 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
202 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000203 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000204}
205
Chris Lattnerae640432002-12-17 02:50:10 +0000206
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000207/// getStackSpaceFor - This allocates space for the specified virtual register
208/// to be held on the stack.
209int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
210 // Find the location Reg would belong...
211 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000212
Chris Lattner580f9be2002-12-28 20:40:43 +0000213 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000214 return I->second; // Already has space allocated?
215
Chris Lattner580f9be2002-12-28 20:40:43 +0000216 // Allocate a new stack object for this spill location...
Chris Lattner91a452b2003-01-13 00:25:40 +0000217 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000218
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000219 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000220 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
221 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000222}
223
Chris Lattnerae640432002-12-17 02:50:10 +0000224
Chris Lattner82bee0f2002-12-18 08:14:26 +0000225/// removePhysReg - This method marks the specified physical register as no
226/// longer being in use.
227///
228void RA::removePhysReg(unsigned PhysReg) {
229 PhysRegsUsed.erase(PhysReg); // PhyReg no longer used
230
231 std::vector<unsigned>::iterator It =
232 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
233 assert(It != PhysRegsUseOrder.end() &&
234 "Spilled a physical register, but it was not in use list!");
235 PhysRegsUseOrder.erase(It);
236}
237
Chris Lattner91a452b2003-01-13 00:25:40 +0000238
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000239/// spillVirtReg - This method spills the value specified by PhysReg into the
240/// virtual register slot specified by VirtReg. It then updates the RA data
241/// structures to indicate the fact that PhysReg is now available.
242///
243void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
244 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000245 if (!VirtReg && DisableKill) return;
246 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000247 " Must not have appropriate kill for the register or use exists beyond"
248 " the intended one.");
249 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
250 std::cerr << " containing %reg" << VirtReg;
251 if (!isVirtRegModified(VirtReg))
252 std::cerr << " which has not been modified, so no store necessary!");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000253
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000254 // Otherwise, there is a virtual register corresponding to this physical
255 // register. We only need to spill it into its stack slot if it has been
256 // modified.
257 if (isVirtRegModified(VirtReg)) {
258 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
259 int FrameIndex = getStackSpaceFor(VirtReg, RC);
260 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
261 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
262 ++NumSpilled; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000263 }
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000264 Virt2PhysRegMap.erase(VirtReg); // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000265
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000266 DEBUG(std::cerr << "\n");
Chris Lattner82bee0f2002-12-18 08:14:26 +0000267 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000268}
269
Chris Lattnerae640432002-12-17 02:50:10 +0000270
Chris Lattner91a452b2003-01-13 00:25:40 +0000271/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000272/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
273/// then the request is ignored if the physical register does not contain a
274/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000275///
276void RA::spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000277 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000278 std::map<unsigned, unsigned>::iterator PI = PhysRegsUsed.find(PhysReg);
279 if (PI != PhysRegsUsed.end()) { // Only spill it if it's used!
Chris Lattner128c2aa2003-08-17 18:01:15 +0000280 if (PI->second || !OnlyVirtRegs)
281 spillVirtReg(MBB, I, PI->second, PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000282 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000283 // If the selected register aliases any other registers, we must make
284 // sure that one of the aliases isn't alive...
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000285 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
286 *AliasSet; ++AliasSet) {
287 PI = PhysRegsUsed.find(*AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000288 if (PI != PhysRegsUsed.end()) // Spill aliased register...
Chris Lattner128c2aa2003-08-17 18:01:15 +0000289 if (PI->second || !OnlyVirtRegs)
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000290 spillVirtReg(MBB, I, PI->second, *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000291 }
292 }
293}
294
295
296/// assignVirtToPhysReg - This method updates local state so that we know
297/// that PhysReg is the proper container for VirtReg now. The physical
298/// register must not be used for anything else when this is called.
299///
300void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
301 assert(PhysRegsUsed.find(PhysReg) == PhysRegsUsed.end() &&
302 "Phys reg already assigned!");
303 // Update information to note the fact that this register was just used, and
304 // it holds VirtReg.
305 PhysRegsUsed[PhysReg] = VirtReg;
306 Virt2PhysRegMap[VirtReg] = PhysReg;
307 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
308}
309
310
Chris Lattnerae640432002-12-17 02:50:10 +0000311/// isPhysRegAvailable - Return true if the specified physical register is free
312/// and available for use. This also includes checking to see if aliased
313/// registers are all free...
314///
315bool RA::isPhysRegAvailable(unsigned PhysReg) const {
316 if (PhysRegsUsed.count(PhysReg)) return false;
317
318 // If the selected register aliases any other allocated registers, it is
319 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000320 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
321 *AliasSet; ++AliasSet)
322 if (PhysRegsUsed.count(*AliasSet)) // Aliased register in use?
323 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000324 return true;
325}
326
327
Chris Lattner91a452b2003-01-13 00:25:40 +0000328/// getFreeReg - Look to see if there is a free register available in the
329/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000330///
Chris Lattner91a452b2003-01-13 00:25:40 +0000331unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000332 // Get iterators defining the range of registers that are valid to allocate in
333 // this class, which also specifies the preferred allocation order.
334 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
335 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000336
Chris Lattner91a452b2003-01-13 00:25:40 +0000337 for (; RI != RE; ++RI)
338 if (isPhysRegAvailable(*RI)) { // Is reg unused?
339 assert(*RI != 0 && "Cannot use register!");
340 return *RI; // Found an unused register!
341 }
342 return 0;
343}
344
345
346/// liberatePhysReg - Make sure the specified physical register is available for
347/// use. If there is currently a value in it, it is either moved out of the way
348/// or spilled to memory.
349///
350void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
351 unsigned PhysReg) {
352 // FIXME: This code checks to see if a register is available, but it really
353 // wants to know if a reg is available BEFORE the instruction executes. If
354 // called after killed operands are freed, it runs the risk of reallocating a
355 // used operand...
356#if 0
357 if (isPhysRegAvailable(PhysReg)) return; // Already available...
358
359 // Check to see if the register is directly used, not indirectly used through
360 // aliases. If aliased registers are the ones actually used, we cannot be
361 // sure that we will be able to save the whole thing if we do a reg-reg copy.
362 std::map<unsigned, unsigned>::iterator PRUI = PhysRegsUsed.find(PhysReg);
363 if (PRUI != PhysRegsUsed.end()) {
364 unsigned VirtReg = PRUI->second; // The virtual register held...
365
366 // Check to see if there is a compatible register available. If so, we can
367 // move the value into the new register...
368 //
369 const TargetRegisterClass *RC = RegInfo->getRegClass(PhysReg);
370 if (unsigned NewReg = getFreeReg(RC)) {
371 // Emit the code to copy the value...
372 RegInfo->copyRegToReg(MBB, I, NewReg, PhysReg, RC);
373
374 // Update our internal state to indicate that PhysReg is available and Reg
375 // isn't.
376 Virt2PhysRegMap.erase(VirtReg);
377 removePhysReg(PhysReg); // Free the physreg
378
379 // Move reference over to new register...
380 assignVirtToPhysReg(VirtReg, NewReg);
381 return;
Chris Lattnerae640432002-12-17 02:50:10 +0000382 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000383 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000384#endif
385 spillPhysReg(MBB, I, PhysReg);
386}
387
388
389/// getReg - Find a physical register to hold the specified virtual
390/// register. If all compatible physical registers are used, this method spills
391/// the last used virtual register to the stack, and uses that register.
392///
393unsigned RA::getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
394 unsigned VirtReg) {
395 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
396
397 // First check to see if we have a free register of the requested type...
398 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000399
Chris Lattnerae640432002-12-17 02:50:10 +0000400 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000401 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000402 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000403
404 // Loop over all of the preallocated registers from the least recently used
405 // to the most recently used. When we find one that is capable of holding
406 // our register, use it.
407 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000408 assert(i != PhysRegsUseOrder.size() &&
409 "Couldn't find a register of the appropriate class!");
Chris Lattnerae640432002-12-17 02:50:10 +0000410
411 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000412
413 // We can only use this register if it holds a virtual register (ie, it
414 // can be spilled). Do not use it if it is an explicitly allocated
415 // physical register!
416 assert(PhysRegsUsed.count(R) &&
417 "PhysReg in PhysRegsUseOrder, but is not allocated?");
418 if (PhysRegsUsed[R]) {
419 // If the current register is compatible, use it.
420 if (RegInfo->getRegClass(R) == RC) {
421 PhysReg = R;
422 break;
423 } else {
424 // If one of the registers aliased to the current register is
425 // compatible, use it.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000426 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
427 *AliasSet; ++AliasSet) {
428 if (RegInfo->getRegClass(*AliasSet) == RC) {
429 PhysReg = *AliasSet; // Take an aliased register
430 break;
431 }
432 }
Chris Lattner41822c72003-08-23 23:49:42 +0000433 }
Chris Lattnerae640432002-12-17 02:50:10 +0000434 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000435 }
436
Chris Lattnerae640432002-12-17 02:50:10 +0000437 assert(PhysReg && "Physical register not assigned!?!?");
438
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000439 // At this point PhysRegsUseOrder[i] is the least recently used register of
440 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000441 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000442 }
443
444 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000445 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000446 return PhysReg;
447}
448
Chris Lattnerae640432002-12-17 02:50:10 +0000449
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000450/// reloadVirtReg - This method loads the specified virtual register into a
451/// physical register, returning the physical register chosen. This updates the
452/// regalloc data structures to reflect the fact that the virtual reg is now
453/// alive in a physical register, and the previous one isn't.
454///
455unsigned RA::reloadVirtReg(MachineBasicBlock &MBB,
456 MachineBasicBlock::iterator &I,
457 unsigned VirtReg) {
458 std::map<unsigned, unsigned>::iterator It = Virt2PhysRegMap.find(VirtReg);
459 if (It != Virt2PhysRegMap.end()) {
460 MarkPhysRegRecentlyUsed(It->second);
461 return It->second; // Already have this value available!
462 }
463
Chris Lattner91a452b2003-01-13 00:25:40 +0000464 unsigned PhysReg = getReg(MBB, I, VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000465
Chris Lattnerff863ba2002-12-25 05:05:46 +0000466 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner580f9be2002-12-28 20:40:43 +0000467 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000468
Chris Lattner91a452b2003-01-13 00:25:40 +0000469 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
470
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000471 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
472 << RegInfo->getName(PhysReg) << "\n");
473
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000474 // Add move instruction(s)
Chris Lattner580f9be2002-12-28 20:40:43 +0000475 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000476 ++NumReloaded; // Update statistics
477 return PhysReg;
478}
479
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000480
481
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000482void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
483 // loop over each instruction
484 MachineBasicBlock::iterator I = MBB.begin();
485 for (; I != MBB.end(); ++I) {
486 MachineInstr *MI = *I;
Chris Lattner3501fea2003-01-14 22:00:31 +0000487 const TargetInstrDescriptor &TID = TM->getInstrInfo().get(MI->getOpcode());
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000488 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
489 std::cerr << " Regs have values: ";
490 for (std::map<unsigned, unsigned>::const_iterator
491 I = PhysRegsUsed.begin(), E = PhysRegsUsed.end(); I != E; ++I)
492 std::cerr << "[" << RegInfo->getName(I->first)
493 << ",%reg" << I->second << "] ";
494 std::cerr << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000495
Chris Lattnerae640432002-12-17 02:50:10 +0000496 // Loop over the implicit uses, making sure that they are at the head of the
497 // use order list, so they don't get reallocated.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000498 for (const unsigned *ImplicitUses = TID.ImplicitUses;
499 *ImplicitUses; ++ImplicitUses)
500 MarkPhysRegRecentlyUsed(*ImplicitUses);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000501
Brian Gaeke53b99a02003-08-15 21:19:25 +0000502 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000503 // incoming values if we are out of registers. Note that we completely
504 // ignore physical register uses here. We assume that if an explicit
505 // physical register is referenced by the instruction, that it is guaranteed
506 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000507 //
508 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000509 if (MI->getOperand(i).opIsUse() && MI->getOperand(i).isVirtualRegister()){
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000510 unsigned VirtSrcReg = MI->getOperand(i).getAllocatedRegNum();
511 unsigned PhysSrcReg = reloadVirtReg(MBB, I, VirtSrcReg);
512 MI->SetMachineOperandReg(i, PhysSrcReg); // Assign the input register
513 }
514
Chris Lattner91a452b2003-01-13 00:25:40 +0000515 if (!DisableKill) {
516 // If this instruction is the last user of anything in registers, kill the
517 // value, freeing the register being used, so it doesn't need to be
518 // spilled to memory.
519 //
520 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
Chris Lattnerd5725632003-05-12 03:54:14 +0000521 KE = LV->killed_end(MI); KI != KE; ++KI) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000522 unsigned VirtReg = KI->second;
Chris Lattnerd5725632003-05-12 03:54:14 +0000523 unsigned PhysReg = VirtReg;
524 if (VirtReg >= MRegisterInfo::FirstVirtualRegister) {
525 std::map<unsigned, unsigned>::iterator I =
526 Virt2PhysRegMap.find(VirtReg);
527 assert(I != Virt2PhysRegMap.end());
528 PhysReg = I->second;
529 Virt2PhysRegMap.erase(I);
530 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000531
Chris Lattnerd5725632003-05-12 03:54:14 +0000532 if (PhysReg) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000533 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
534 << "[%reg" << VirtReg <<"], removing it from live set\n");
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000535 removePhysReg(PhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000536 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000537 }
538 }
539
540 // Loop over all of the operands of the instruction, spilling registers that
541 // are defined, and marking explicit destinations in the PhysRegsUsed map.
542 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
Chris Lattnerd3fd79f2003-08-03 13:49:03 +0000543 if ((MI->getOperand(i).opIsDefOnly() ||
544 MI->getOperand(i).opIsDefAndUse()) &&
Chris Lattner91a452b2003-01-13 00:25:40 +0000545 MI->getOperand(i).isPhysicalRegister()) {
546 unsigned Reg = MI->getOperand(i).getAllocatedRegNum();
Chris Lattner128c2aa2003-08-17 18:01:15 +0000547 spillPhysReg(MBB, I, Reg, true); // Spill any existing value in the reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000548 PhysRegsUsed[Reg] = 0; // It is free and reserved now
549 PhysRegsUseOrder.push_back(Reg);
550 }
551
552 // Loop over the implicit defs, spilling them as well.
Chris Lattner3501fea2003-01-14 22:00:31 +0000553 if (const unsigned *ImplicitDefs = TID.ImplicitDefs)
Chris Lattner91a452b2003-01-13 00:25:40 +0000554 for (unsigned i = 0; ImplicitDefs[i]; ++i) {
555 unsigned Reg = ImplicitDefs[i];
Chris Lattnerd5725632003-05-12 03:54:14 +0000556 spillPhysReg(MBB, I, Reg);
557 PhysRegsUseOrder.push_back(Reg);
558 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Chris Lattner91a452b2003-01-13 00:25:40 +0000559 }
560
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000561 // Okay, we have allocated all of the source operands and spilled any values
562 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner91a452b2003-01-13 00:25:40 +0000563 // implicit defs and assign them to a register, spilling incoming values if
564 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000565 //
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000566 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000567 if ((MI->getOperand(i).opIsDefOnly() || MI->getOperand(i).opIsDefAndUse())
568 && MI->getOperand(i).isVirtualRegister()) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000569 unsigned DestVirtReg = MI->getOperand(i).getAllocatedRegNum();
570 unsigned DestPhysReg;
571
Chris Lattnerd5725632003-05-12 03:54:14 +0000572 // If DestVirtReg already has a value, forget about it. Why doesn't
573 // getReg do this right?
574 std::map<unsigned, unsigned>::iterator DestI =
575 Virt2PhysRegMap.find(DestVirtReg);
576 if (DestI != Virt2PhysRegMap.end()) {
577 unsigned PhysReg = DestI->second;
578 Virt2PhysRegMap.erase(DestI);
579 removePhysReg(PhysReg);
580 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000581
Chris Lattner580f9be2002-12-28 20:40:43 +0000582 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000583 // must be same register number as the first operand
584 // This maps a = b + c into b += c, and saves b into a's spot
585 assert(MI->getOperand(1).isRegister() &&
586 MI->getOperand(1).getAllocatedRegNum() &&
587 MI->getOperand(1).opIsUse() &&
588 "Two address instruction invalid!");
589 DestPhysReg = MI->getOperand(1).getAllocatedRegNum();
590
Chris Lattnerd5725632003-05-12 03:54:14 +0000591 liberatePhysReg(MBB, I, DestPhysReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000592 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000593 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000594 DestPhysReg = getReg(MBB, I, DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000595 }
Chris Lattnerd5725632003-05-12 03:54:14 +0000596 markVirtRegModified(DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000597 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
598 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000599
600 if (!DisableKill) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000601 // If this instruction defines any registers that are immediately dead,
602 // kill them now.
603 //
604 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
Chris Lattnerd5725632003-05-12 03:54:14 +0000605 KE = LV->dead_end(MI); KI != KE; ++KI) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000606 unsigned VirtReg = KI->second;
Chris Lattnerd5725632003-05-12 03:54:14 +0000607 unsigned PhysReg = VirtReg;
608 if (VirtReg >= MRegisterInfo::FirstVirtualRegister) {
609 std::map<unsigned, unsigned>::iterator I =
610 Virt2PhysRegMap.find(VirtReg);
611 assert(I != Virt2PhysRegMap.end());
612 PhysReg = I->second;
613 Virt2PhysRegMap.erase(I);
614 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000615
Chris Lattnerd5725632003-05-12 03:54:14 +0000616 if (PhysReg) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000617 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
618 << " [%reg" << VirtReg
619 << "] is never used, removing it frame live list\n");
Chris Lattnerd5725632003-05-12 03:54:14 +0000620 removePhysReg(PhysReg);
621 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000622 }
623 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000624 }
625
626 // Rewind the iterator to point to the first flow control instruction...
Chris Lattner3501fea2003-01-14 22:00:31 +0000627 const TargetInstrInfo &TII = TM->getInstrInfo();
Chris Lattner0416d2a2003-01-16 18:06:43 +0000628 I = MBB.end();
Chris Lattner3501fea2003-01-14 22:00:31 +0000629 while (I != MBB.begin() && TII.isTerminatorInstr((*(I-1))->getOpcode()))
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000630 --I;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000631
632 // Spill all physical registers holding virtual registers now.
633 while (!PhysRegsUsed.empty())
Chris Lattner8c819452003-08-05 04:13:58 +0000634 if (unsigned VirtReg = PhysRegsUsed.begin()->second)
635 spillVirtReg(MBB, I, VirtReg, PhysRegsUsed.begin()->first);
636 else
637 removePhysReg(PhysRegsUsed.begin()->first);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000638
Chris Lattner91a452b2003-01-13 00:25:40 +0000639 for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(),
Chris Lattnerd5725632003-05-12 03:54:14 +0000640 E = Virt2PhysRegMap.end(); I != E; ++I)
Chris Lattner91a452b2003-01-13 00:25:40 +0000641 std::cerr << "Register still mapped: " << I->first << " -> "
Chris Lattnerd5725632003-05-12 03:54:14 +0000642 << I->second << "\n";
Chris Lattner91a452b2003-01-13 00:25:40 +0000643
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000644 assert(Virt2PhysRegMap.empty() && "Virtual registers still in phys regs?");
Chris Lattner128c2aa2003-08-17 18:01:15 +0000645
646 // 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
Chris Lattner82bee0f2002-12-18 08:14:26 +0000661 if (!DisableKill)
Chris Lattner91a452b2003-01-13 00:25:40 +0000662 LV = &getAnalysis<LiveVariables>();
Chris Lattner82bee0f2002-12-18 08:14:26 +0000663
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000664 // Loop over all of the basic blocks, eliminating virtual register references
665 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
666 MBB != MBBe; ++MBB)
667 AllocateBasicBlock(*MBB);
668
Chris Lattner580f9be2002-12-28 20:40:43 +0000669 StackSlotForVirtReg.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000670 VirtRegModified.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000671 return true;
672}
673
Brian Gaeke19df3872003-08-13 18:18:15 +0000674FunctionPass *createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000675 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000676}