blob: d3d5796e851c203fa01d0e3a6a121a4a78e7d1ce [file] [log] [blame]
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb74e83c2002-12-16 16:15:28 +00009//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4cc662b2003-08-03 21:47:31 +000015#define DEBUG_TYPE "regalloc"
Evan Chengddee8422006-11-15 20:55:15 +000016#include "llvm/BasicBlock.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000017#include "llvm/CodeGen/Passes.h"
Chris Lattner580f9be2002-12-28 20:40:43 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000019#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerff863ba2002-12-25 05:05:46 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000022#include "llvm/CodeGen/LiveVariables.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000023#include "llvm/CodeGen/RegAllocRegistry.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000024#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000025#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000029#include "llvm/ADT/IndexedMap.h"
Evan Chengddee8422006-11-15 20:55:15 +000030#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattner27f29162004-10-26 15:35:58 +000032#include <algorithm>
Chris Lattneref09c632004-01-31 21:27:19 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NumStores, "Number of stores added");
36STATISTIC(NumLoads , "Number of loads added");
37STATISTIC(NumFolded, "Number of loads/stores folded into instructions");
Jim Laskey13ec7022006-08-01 14:21:23 +000038
Chris Lattnercd3245a2006-12-19 22:41:21 +000039namespace {
Jim Laskey13ec7022006-08-01 14:21:23 +000040 static RegisterRegAlloc
41 localRegAlloc("local", " local register allocator",
42 createLocalRegisterAllocator);
43
44
Chris Lattner95255282006-06-28 23:17:24 +000045 class VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattner580f9be2002-12-28 20:40:43 +000046 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000047 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000048 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000049 LiveVariables *LV;
Chris Lattnerff863ba2002-12-25 05:05:46 +000050
Chris Lattnerb8822ad2003-08-04 23:36:39 +000051 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
52 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000053 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000054
55 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000056 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000057 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000058
59 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000060 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000061 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000062
Chris Lattner64667b62004-02-09 01:26:13 +000063 // PhysRegsUsed - This array is effectively a map, containing entries for
64 // each physical register that currently has a value (ie, it is in
65 // Virt2PhysRegMap). The value mapped to is the virtual register
66 // corresponding to the physical register (the inverse of the
67 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000068 // because it is used by a future instruction, and to -2 if it is not
69 // allocatable. If the entry for a physical register is -1, then the
70 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000071 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000072 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000073
74 // PhysRegsUseOrder - This contains a list of the physical registers that
75 // currently have a virtual register value in them. This list provides an
76 // ordering of registers, imposing a reallocation order. This list is only
77 // used if all registers are allocated and we have to spill one, in which
78 // case we spill the least recently used register. Entries at the front of
79 // the list are the least recently used registers, entries at the back are
80 // the most recently used.
81 //
82 std::vector<unsigned> PhysRegsUseOrder;
83
Chris Lattner91a452b2003-01-13 00:25:40 +000084 // VirtRegModified - This bitset contains information about which virtual
85 // registers need to be spilled back to memory when their registers are
86 // scavenged. If a virtual register has simply been rematerialized, there
87 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000088 //
Chris Lattner91a452b2003-01-13 00:25:40 +000089 std::vector<bool> VirtRegModified;
90
91 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000092 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000093 Reg -= MRegisterInfo::FirstVirtualRegister;
94 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
95 VirtRegModified[Reg] = Val;
96 }
97
98 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +000099 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000100 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000101 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000102 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
103 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000104
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000105 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000106 if (PhysRegsUseOrder.empty() ||
107 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000108
109 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000110 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
111 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
112 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
113 // Add it to the end of the list
114 PhysRegsUseOrder.push_back(RegMatch);
115 if (RegMatch == Reg)
116 return; // Found an exact match, exit early
117 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000118 }
119
120 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000121 virtual const char *getPassName() const {
122 return "Local Register Allocator";
123 }
124
Chris Lattner91a452b2003-01-13 00:25:40 +0000125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000126 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000127 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000128 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000129 MachineFunctionPass::getAnalysisUsage(AU);
130 }
131
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000132 private:
133 /// runOnMachineFunction - Register allocate the whole function
134 bool runOnMachineFunction(MachineFunction &Fn);
135
136 /// AllocateBasicBlock - Register allocate the specified basic block.
137 void AllocateBasicBlock(MachineBasicBlock &MBB);
138
Chris Lattner82bee0f2002-12-18 08:14:26 +0000139
Chris Lattner82bee0f2002-12-18 08:14:26 +0000140 /// areRegsEqual - This method returns true if the specified registers are
141 /// related to each other. To do this, it checks to see if they are equal
142 /// or if the first register is in the alias set of the second register.
143 ///
144 bool areRegsEqual(unsigned R1, unsigned R2) const {
145 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000146 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
147 *AliasSet; ++AliasSet) {
148 if (*AliasSet == R1) return true;
149 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000150 return false;
151 }
152
Chris Lattner580f9be2002-12-28 20:40:43 +0000153 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000154 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000155 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000156
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000157 /// removePhysReg - This method marks the specified physical register as no
158 /// longer being in use.
159 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000160 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000161
162 /// spillVirtReg - This method spills the value specified by PhysReg into
163 /// the virtual register slot specified by VirtReg. It then updates the RA
164 /// data structures to indicate the fact that PhysReg is now available.
165 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000166 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000167 unsigned VirtReg, unsigned PhysReg);
168
Chris Lattnerc21be922002-12-16 17:44:42 +0000169 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000170 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
171 /// true, then the request is ignored if the physical register does not
172 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000173 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000174 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000175 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000176
Chris Lattner91a452b2003-01-13 00:25:40 +0000177 /// assignVirtToPhysReg - This method updates local state so that we know
178 /// that PhysReg is the proper container for VirtReg now. The physical
179 /// register must not be used for anything else when this is called.
180 ///
181 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
182
183 /// liberatePhysReg - Make sure the specified physical register is available
184 /// for use. If there is currently a value in it, it is either moved out of
185 /// the way or spilled to memory.
186 ///
187 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000188 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000189
Chris Lattnerae640432002-12-17 02:50:10 +0000190 /// isPhysRegAvailable - Return true if the specified physical register is
191 /// free and available for use. This also includes checking to see if
192 /// aliased registers are all free...
193 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000194 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000195
196 /// getFreeReg - Look to see if there is a free register available in the
197 /// specified register class. If not, return 0.
198 ///
199 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000200
Chris Lattner91a452b2003-01-13 00:25:40 +0000201 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000202 /// register. If all compatible physical registers are used, this method
203 /// spills the last used virtual register to the stack, and uses that
204 /// register.
205 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000206 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000207 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000208
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000209 /// reloadVirtReg - This method transforms the specified specified virtual
210 /// register use to refer to a physical register. This method may do this
211 /// in one of several ways: if the register is available in a physical
212 /// register already, it uses that physical register. If the value is not
213 /// in a physical register, and if there are physical registers available,
214 /// it loads it into a register. If register pressure is high, and it is
215 /// possible, it tries to fold the load of the virtual register into the
216 /// instruction itself. It avoids doing this if register pressure is low to
217 /// improve the chance that subsequent instructions can use the reloaded
218 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000219 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000220 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
221 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000222
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000223
224 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
225 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000226 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000227}
228
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000229/// getStackSpaceFor - This allocates space for the specified virtual register
230/// to be held on the stack.
231int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
232 // Find the location Reg would belong...
233 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000234
Chris Lattner580f9be2002-12-28 20:40:43 +0000235 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000236 return I->second; // Already has space allocated?
237
Chris Lattner580f9be2002-12-28 20:40:43 +0000238 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000239 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
240 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000241
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000242 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000243 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
244 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000245}
246
Chris Lattnerae640432002-12-17 02:50:10 +0000247
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000248/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000249/// longer being in use.
250///
251void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000252 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000253
254 std::vector<unsigned>::iterator It =
255 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000256 if (It != PhysRegsUseOrder.end())
257 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000258}
259
Chris Lattner91a452b2003-01-13 00:25:40 +0000260
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000261/// spillVirtReg - This method spills the value specified by PhysReg into the
262/// virtual register slot specified by VirtReg. It then updates the RA data
263/// structures to indicate the fact that PhysReg is now available.
264///
Chris Lattner688c8252004-02-22 19:08:15 +0000265void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000266 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000267 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000268 " Must not have appropriate kill for the register or use exists beyond"
269 " the intended one.");
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000270 DOUT << " Spilling register " << RegInfo->getName(PhysReg)
271 << " containing %reg" << VirtReg;
272 if (!isVirtRegModified(VirtReg))
273 DOUT << " which has not been modified, so no store necessary!";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000274
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000275 // Otherwise, there is a virtual register corresponding to this physical
276 // register. We only need to spill it into its stack slot if it has been
277 // modified.
278 if (isVirtRegModified(VirtReg)) {
279 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
280 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000281 DOUT << " to stack slot #" << FrameIndex;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000282 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000283 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000284 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000285
286 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000287
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000288 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000289 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000290}
291
Chris Lattnerae640432002-12-17 02:50:10 +0000292
Chris Lattner91a452b2003-01-13 00:25:40 +0000293/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000294/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
295/// then the request is ignored if the physical register does not contain a
296/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000297///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000298void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000299 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000300 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000301 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000302 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
303 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000304 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000305 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000306 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000307 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000308 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000309 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
310 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Chengddee8422006-11-15 20:55:15 +0000311 if (PhysRegsUsed[*AliasSet] == 0) {
312 // This must have been a dead def due to something like this:
313 // %EAX :=
314 // := op %AL
315 // No more use of %EAX, %AH, etc.
316 // %EAX isn't dead upon definition, but %AH is. However %AH isn't
317 // an operand of definition MI so it's not marked as such.
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000318 DOUT << " Register " << RegInfo->getName(*AliasSet)
319 << " [%reg" << *AliasSet
320 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000321 removePhysReg(*AliasSet);
322 } else
Chris Lattner64667b62004-02-09 01:26:13 +0000323 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000324 }
325}
326
327
328/// assignVirtToPhysReg - This method updates local state so that we know
329/// that PhysReg is the proper container for VirtReg now. The physical
330/// register must not be used for anything else when this is called.
331///
332void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000333 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000334 // Update information to note the fact that this register was just used, and
335 // it holds VirtReg.
336 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000337 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000338 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
339}
340
341
Chris Lattnerae640432002-12-17 02:50:10 +0000342/// isPhysRegAvailable - Return true if the specified physical register is free
343/// and available for use. This also includes checking to see if aliased
344/// registers are all free...
345///
346bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000347 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000348
349 // If the selected register aliases any other allocated registers, it is
350 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000351 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
352 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000353 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000354 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000355 return true;
356}
357
358
Chris Lattner91a452b2003-01-13 00:25:40 +0000359/// getFreeReg - Look to see if there is a free register available in the
360/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000361///
Chris Lattner91a452b2003-01-13 00:25:40 +0000362unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000363 // Get iterators defining the range of registers that are valid to allocate in
364 // this class, which also specifies the preferred allocation order.
365 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
366 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000367
Chris Lattner91a452b2003-01-13 00:25:40 +0000368 for (; RI != RE; ++RI)
369 if (isPhysRegAvailable(*RI)) { // Is reg unused?
370 assert(*RI != 0 && "Cannot use register!");
371 return *RI; // Found an unused register!
372 }
373 return 0;
374}
375
376
377/// liberatePhysReg - Make sure the specified physical register is available for
378/// use. If there is currently a value in it, it is either moved out of the way
379/// or spilled to memory.
380///
381void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000382 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000383 spillPhysReg(MBB, I, PhysReg);
384}
385
386
387/// getReg - Find a physical register to hold the specified virtual
388/// register. If all compatible physical registers are used, this method spills
389/// the last used virtual register to the stack, and uses that register.
390///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000391unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000392 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000393 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
394
395 // First check to see if we have a free register of the requested type...
396 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000397
Chris Lattnerae640432002-12-17 02:50:10 +0000398 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000399 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000400 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000401
402 // Loop over all of the preallocated registers from the least recently used
403 // to the most recently used. When we find one that is capable of holding
404 // our register, use it.
405 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000406 assert(i != PhysRegsUseOrder.size() &&
407 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000408
Chris Lattnerae640432002-12-17 02:50:10 +0000409 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000410
411 // We can only use this register if it holds a virtual register (ie, it
412 // can be spilled). Do not use it if it is an explicitly allocated
413 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000414 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000415 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000416 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000417 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000418 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000419 PhysReg = R;
420 break;
421 } else {
422 // If one of the registers aliased to the current register is
423 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000424 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
425 *AliasIt; ++AliasIt) {
426 if (RC->contains(*AliasIt) &&
427 // If this is pinned down for some reason, don't use it. For
428 // example, if CL is pinned, and we run across CH, don't use
429 // CH as justification for using scavenging ECX (which will
430 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000431 PhysRegsUsed[*AliasIt] != 0 &&
432
433 // Make sure the register is allocatable. Don't allocate SIL on
434 // x86-32.
435 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000436 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000437 break;
438 }
439 }
Chris Lattner41822c72003-08-23 23:49:42 +0000440 }
Chris Lattnerae640432002-12-17 02:50:10 +0000441 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000442 }
443
Chris Lattnerae640432002-12-17 02:50:10 +0000444 assert(PhysReg && "Physical register not assigned!?!?");
445
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000446 // At this point PhysRegsUseOrder[i] is the least recently used register of
447 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000448 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000449 }
450
451 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000452 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000453 return PhysReg;
454}
455
Chris Lattnerae640432002-12-17 02:50:10 +0000456
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000457/// reloadVirtReg - This method transforms the specified specified virtual
458/// register use to refer to a physical register. This method may do this in
459/// one of several ways: if the register is available in a physical register
460/// already, it uses that physical register. If the value is not in a physical
461/// register, and if there are physical registers available, it loads it into a
462/// register. If register pressure is high, and it is possible, it tries to
463/// fold the load of the virtual register into the instruction itself. It
464/// avoids doing this if register pressure is low to improve the chance that
465/// subsequent instructions can use the reloaded value. This method returns the
466/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000467///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000468MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
469 unsigned OpNum) {
470 unsigned VirtReg = MI->getOperand(OpNum).getReg();
471
472 // If the virtual register is already available, just update the instruction
473 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000474 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000475 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000476 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000477 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000478 }
479
Chris Lattner1e3812c2004-02-17 04:08:37 +0000480 // Otherwise, we need to fold it into the current instruction, or reload it.
481 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000482 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000483 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000484 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000485
Chris Lattner11390e72004-02-17 08:09:40 +0000486 if (PhysReg) { // Register is available, allocate it!
487 assignVirtToPhysReg(VirtReg, PhysReg);
488 } else { // No registers available.
489 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000490 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000491 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000492 // Since we changed the address of MI, make sure to update live variables
493 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000494 LV->instructionChanged(MI, FMI);
495 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000496 }
497
498 // It looks like we can't fold this virtual register load into this
499 // instruction. Force some poor hapless value out of the register file to
500 // make room for the new register, and reload it.
501 PhysReg = getReg(MBB, MI, VirtReg);
502 }
503
Chris Lattner91a452b2003-01-13 00:25:40 +0000504 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
505
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000506 DOUT << " Reloading %reg" << VirtReg << " into "
507 << RegInfo->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000508
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000509 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000510 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000511 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000512
Evan Cheng6c087e52007-04-25 22:13:27 +0000513 MF->setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000514 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000515 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000516}
517
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000518
519
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000520void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
521 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000522 MachineBasicBlock::iterator MII = MBB.begin();
523 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000524
Evan Chengddee8422006-11-15 20:55:15 +0000525 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000526 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Chengddee8422006-11-15 20:55:15 +0000527
Chris Lattner44500e32006-06-15 22:21:53 +0000528 // If this is the first basic block in the machine function, add live-in
529 // registers as active.
530 if (&MBB == &*MF->begin()) {
531 for (MachineFunction::livein_iterator I = MF->livein_begin(),
532 E = MF->livein_end(); I != E; ++I) {
533 unsigned Reg = I->first;
Evan Cheng6c087e52007-04-25 22:13:27 +0000534 MF->setPhysRegUsed(Reg);
Chris Lattner44500e32006-06-15 22:21:53 +0000535 PhysRegsUsed[Reg] = 0; // It is free and reserved now
536 PhysRegsUseOrder.push_back(Reg);
537 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
538 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000539 if (PhysRegsUsed[*AliasSet] != -2) {
540 PhysRegsUseOrder.push_back(*AliasSet);
541 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000542 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000543 }
Chris Lattner44500e32006-06-15 22:21:53 +0000544 }
545 }
546 }
547
548 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000549 while (MII != MBB.end()) {
550 MachineInstr *MI = MII++;
551 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000552 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
553 DOUT << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000554 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000555 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000556 DOUT << "[" << RegInfo->getName(i)
557 << ",%reg" << PhysRegsUsed[i] << "] ";
558 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000559
Chris Lattnerae640432002-12-17 02:50:10 +0000560 // Loop over the implicit uses, making sure that they are at the head of the
561 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000562 if (TID.ImplicitUses) {
563 for (const unsigned *ImplicitUses = TID.ImplicitUses;
564 *ImplicitUses; ++ImplicitUses)
565 MarkPhysRegRecentlyUsed(*ImplicitUses);
566 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000567
Evan Chengddee8422006-11-15 20:55:15 +0000568 SmallVector<unsigned, 8> Kills;
569 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
570 MachineOperand& MO = MI->getOperand(i);
571 if (MO.isRegister() && MO.isKill())
572 Kills.push_back(MO.getReg());
573 }
574
Brian Gaeke53b99a02003-08-15 21:19:25 +0000575 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000576 // incoming values if we are out of registers. Note that we completely
577 // ignore physical register uses here. We assume that if an explicit
578 // physical register is referenced by the instruction, that it is guaranteed
579 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000580 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000581 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
582 MachineOperand& MO = MI->getOperand(i);
583 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000584 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000585 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000586 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000587 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000588
Evan Chengddee8422006-11-15 20:55:15 +0000589 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000590 // value, freeing the register being used, so it doesn't need to be
591 // spilled to memory.
592 //
Evan Chengddee8422006-11-15 20:55:15 +0000593 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
594 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000595 unsigned PhysReg = VirtReg;
596 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
597 // If the virtual register was never materialized into a register, it
598 // might not be in the map, but it won't hurt to zero it out anyway.
599 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
600 PhysReg = PhysRegSlot;
601 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000602 } else if (PhysRegsUsed[PhysReg] == -2) {
603 // Unallocatable register dead, ignore.
604 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000605 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000606
Chris Lattner56ddada2004-02-17 17:49:10 +0000607 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000608 DOUT << " Last use of " << RegInfo->getName(PhysReg)
609 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000610 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000611 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
612 *AliasSet; ++AliasSet) {
613 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000614 DOUT << " Last use of "
Evan Chengddee8422006-11-15 20:55:15 +0000615 << RegInfo->getName(*AliasSet)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000616 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000617 removePhysReg(*AliasSet);
618 }
619 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000620 }
621 }
622
623 // Loop over all of the operands of the instruction, spilling registers that
624 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000625 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
626 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000627 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000628 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
629 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000630 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
631
Evan Cheng6c087e52007-04-25 22:13:27 +0000632 MF->setPhysRegUsed(Reg);
Evan Chengddee8422006-11-15 20:55:15 +0000633 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000634 PhysRegsUsed[Reg] = 0; // It is free and reserved now
635 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000636 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
637 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000638 if (PhysRegsUsed[*AliasSet] != -2) {
639 PhysRegsUseOrder.push_back(*AliasSet);
640 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000641 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000642 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000643 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000644 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000645 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000646
647 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000648 if (TID.ImplicitDefs) {
649 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
650 *ImplicitDefs; ++ImplicitDefs) {
651 unsigned Reg = *ImplicitDefs;
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000652 bool IsNonAllocatable = PhysRegsUsed[Reg] == -2;
653 if (!IsNonAllocatable) {
654 spillPhysReg(MBB, MI, Reg, true);
655 PhysRegsUseOrder.push_back(Reg);
656 PhysRegsUsed[Reg] = 0; // It is free and reserved now
657 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000658 MF->setPhysRegUsed(Reg);
Chris Lattner0648b162005-01-23 22:51:56 +0000659
Jim Laskeycd4317e2006-07-21 21:15:20 +0000660 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
661 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000662 if (PhysRegsUsed[*AliasSet] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000663 if (!IsNonAllocatable) {
664 PhysRegsUseOrder.push_back(*AliasSet);
665 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
666 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000667 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000668 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000669 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000670 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000671 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000672
Evan Chengddee8422006-11-15 20:55:15 +0000673 SmallVector<unsigned, 8> DeadDefs;
674 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
675 MachineOperand& MO = MI->getOperand(i);
676 if (MO.isRegister() && MO.isDead())
677 DeadDefs.push_back(MO.getReg());
678 }
679
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000680 // Okay, we have allocated all of the source operands and spilled any values
681 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000682 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000683 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000684 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000685 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
686 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000687 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000688 MRegisterInfo::isVirtualRegister(MO.getReg())) {
689 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000690 unsigned DestPhysReg;
691
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000692 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000693 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000694 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Evan Cheng6c087e52007-04-25 22:13:27 +0000695 MF->setPhysRegUsed(DestPhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000696 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000697 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000698 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000699 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000700
Chris Lattner56ddada2004-02-17 17:49:10 +0000701 // If this instruction defines any registers that are immediately dead,
702 // kill them now.
703 //
Evan Chengddee8422006-11-15 20:55:15 +0000704 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
705 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000706 unsigned PhysReg = VirtReg;
707 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
708 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
709 PhysReg = PhysRegSlot;
710 assert(PhysReg != 0);
711 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000712 } else if (PhysRegsUsed[PhysReg] == -2) {
713 // Unallocatable register dead, ignore.
714 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000715 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000716
Chris Lattner56ddada2004-02-17 17:49:10 +0000717 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000718 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000719 << " [%reg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000720 << "] is never used, removing it frame live list\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000721 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000722 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
723 *AliasSet; ++AliasSet) {
724 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000725 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000726 << " [%reg" << *AliasSet
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000727 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000728 removePhysReg(*AliasSet);
729 }
730 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000731 }
732 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000733
734 // Finally, if this is a noop copy instruction, zap it.
735 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000736 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
737 LV->removeVirtualRegistersKilled(MI);
738 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000739 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000740 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000741 }
742
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000743 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000744
745 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000746 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000747 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000748 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000749 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000750 else
751 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000752
Chris Lattner9a5ef202005-11-09 05:28:45 +0000753#if 0
754 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000755 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000756 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
757 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000758 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +0000759 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +0000760 AllOk = false;
761 }
762 assert(AllOk && "Virtual registers still in phys regs?");
763#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000764
Chris Lattner128c2aa2003-08-17 18:01:15 +0000765 // Clear any physical register which appear live at the end of the basic
766 // block, but which do not hold any virtual registers. e.g., the stack
767 // pointer.
768 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000769}
770
Chris Lattner86c69a62002-12-17 03:16:10 +0000771
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000772/// runOnMachineFunction - Register allocate the whole function
773///
774bool RA::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000775 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000776 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000777 TM = &Fn.getTarget();
778 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000779 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000780
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000781 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +0000782
783 // At various places we want to efficiently check to see whether a register
784 // is allocatable. To handle this, we mark all unallocatable registers as
785 // being pinned down, permanently.
786 {
Evan Cheng61de82d2007-02-15 05:59:24 +0000787 BitVector Allocable = RegInfo->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +0000788 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
789 if (!Allocable[i])
790 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
791 }
Chris Lattner64667b62004-02-09 01:26:13 +0000792
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000793 // initialize the virtual->physical register map to have a 'null'
794 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000795 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000796
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000797 // Loop over all of the basic blocks, eliminating virtual register references
798 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
799 MBB != MBBe; ++MBB)
800 AllocateBasicBlock(*MBB);
801
Chris Lattner580f9be2002-12-28 20:40:43 +0000802 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000803 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000804 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000805 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000806 return true;
807}
808
Chris Lattneref09c632004-01-31 21:27:19 +0000809FunctionPass *llvm::createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000810 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000811}