blob: 3c58afe0e427dfc1be7eade958efb265c2ad79cf [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
Bill Wendlinge23e00d2007-05-08 19:02:46 +000045 class VISIBILITY_HIDDEN RALocal : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000046 public:
Devang Patel19974732007-05-03 01:11:54 +000047 static char ID;
Bill Wendlinge23e00d2007-05-08 19:02:46 +000048 RALocal() : MachineFunctionPass((intptr_t)&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000049 private:
Chris Lattner580f9be2002-12-28 20:40:43 +000050 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000051 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000052 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000053 LiveVariables *LV;
Chris Lattnerff863ba2002-12-25 05:05:46 +000054
Chris Lattnerb8822ad2003-08-04 23:36:39 +000055 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
56 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000057 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000058
59 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000060 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000061 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000062
63 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000064 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000065 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000066
Chris Lattner64667b62004-02-09 01:26:13 +000067 // PhysRegsUsed - This array is effectively a map, containing entries for
68 // each physical register that currently has a value (ie, it is in
69 // Virt2PhysRegMap). The value mapped to is the virtual register
70 // corresponding to the physical register (the inverse of the
71 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000072 // because it is used by a future instruction, and to -2 if it is not
73 // allocatable. If the entry for a physical register is -1, then the
74 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000075 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000076 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000077
78 // PhysRegsUseOrder - This contains a list of the physical registers that
79 // currently have a virtual register value in them. This list provides an
80 // ordering of registers, imposing a reallocation order. This list is only
81 // used if all registers are allocated and we have to spill one, in which
82 // case we spill the least recently used register. Entries at the front of
83 // the list are the least recently used registers, entries at the back are
84 // the most recently used.
85 //
86 std::vector<unsigned> PhysRegsUseOrder;
87
Chris Lattner91a452b2003-01-13 00:25:40 +000088 // VirtRegModified - This bitset contains information about which virtual
89 // registers need to be spilled back to memory when their registers are
90 // scavenged. If a virtual register has simply been rematerialized, there
91 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000092 //
Chris Lattner91a452b2003-01-13 00:25:40 +000093 std::vector<bool> VirtRegModified;
94
95 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000096 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000097 Reg -= MRegisterInfo::FirstVirtualRegister;
98 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
99 VirtRegModified[Reg] = Val;
100 }
101
102 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +0000103 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000104 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000105 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000106 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
107 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000108
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000109 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000110 if (PhysRegsUseOrder.empty() ||
111 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000112
113 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000114 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
115 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
116 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
117 // Add it to the end of the list
118 PhysRegsUseOrder.push_back(RegMatch);
119 if (RegMatch == Reg)
120 return; // Found an exact match, exit early
121 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000122 }
123
124 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000125 virtual const char *getPassName() const {
126 return "Local Register Allocator";
127 }
128
Chris Lattner91a452b2003-01-13 00:25:40 +0000129 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000130 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000131 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000132 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000133 MachineFunctionPass::getAnalysisUsage(AU);
134 }
135
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000136 private:
137 /// runOnMachineFunction - Register allocate the whole function
138 bool runOnMachineFunction(MachineFunction &Fn);
139
140 /// AllocateBasicBlock - Register allocate the specified basic block.
141 void AllocateBasicBlock(MachineBasicBlock &MBB);
142
Chris Lattner82bee0f2002-12-18 08:14:26 +0000143
Chris Lattner82bee0f2002-12-18 08:14:26 +0000144 /// areRegsEqual - This method returns true if the specified registers are
145 /// related to each other. To do this, it checks to see if they are equal
146 /// or if the first register is in the alias set of the second register.
147 ///
148 bool areRegsEqual(unsigned R1, unsigned R2) const {
149 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000150 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
151 *AliasSet; ++AliasSet) {
152 if (*AliasSet == R1) return true;
153 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000154 return false;
155 }
156
Chris Lattner580f9be2002-12-28 20:40:43 +0000157 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000158 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000159 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000160
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000161 /// removePhysReg - This method marks the specified physical register as no
162 /// longer being in use.
163 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000164 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000165
166 /// spillVirtReg - This method spills the value specified by PhysReg into
167 /// the virtual register slot specified by VirtReg. It then updates the RA
168 /// data structures to indicate the fact that PhysReg is now available.
169 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000170 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000171 unsigned VirtReg, unsigned PhysReg);
172
Chris Lattnerc21be922002-12-16 17:44:42 +0000173 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000174 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
175 /// true, then the request is ignored if the physical register does not
176 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000177 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000178 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000179 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000180
Chris Lattner91a452b2003-01-13 00:25:40 +0000181 /// assignVirtToPhysReg - This method updates local state so that we know
182 /// that PhysReg is the proper container for VirtReg now. The physical
183 /// register must not be used for anything else when this is called.
184 ///
185 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
186
187 /// liberatePhysReg - Make sure the specified physical register is available
188 /// for use. If there is currently a value in it, it is either moved out of
189 /// the way or spilled to memory.
190 ///
191 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000192 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000193
Chris Lattnerae640432002-12-17 02:50:10 +0000194 /// isPhysRegAvailable - Return true if the specified physical register is
195 /// free and available for use. This also includes checking to see if
196 /// aliased registers are all free...
197 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000198 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000199
200 /// getFreeReg - Look to see if there is a free register available in the
201 /// specified register class. If not, return 0.
202 ///
203 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000204
Chris Lattner91a452b2003-01-13 00:25:40 +0000205 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000206 /// register. If all compatible physical registers are used, this method
207 /// spills the last used virtual register to the stack, and uses that
208 /// register.
209 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000210 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000211 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000212
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000213 /// reloadVirtReg - This method transforms the specified specified virtual
214 /// register use to refer to a physical register. This method may do this
215 /// in one of several ways: if the register is available in a physical
216 /// register already, it uses that physical register. If the value is not
217 /// in a physical register, and if there are physical registers available,
218 /// it loads it into a register. If register pressure is high, and it is
219 /// possible, it tries to fold the load of the virtual register into the
220 /// instruction itself. It avoids doing this if register pressure is low to
221 /// improve the chance that subsequent instructions can use the reloaded
222 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000223 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000224 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
225 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000226
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000227
228 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
229 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000230 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000231 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000232}
233
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000234/// getStackSpaceFor - This allocates space for the specified virtual register
235/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000236int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000237 // Find the location Reg would belong...
238 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000239
Chris Lattner580f9be2002-12-28 20:40:43 +0000240 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000241 return I->second; // Already has space allocated?
242
Chris Lattner580f9be2002-12-28 20:40:43 +0000243 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000244 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
245 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000246
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000247 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000248 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
249 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000250}
251
Chris Lattnerae640432002-12-17 02:50:10 +0000252
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000253/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000254/// longer being in use.
255///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000256void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000257 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000258
259 std::vector<unsigned>::iterator It =
260 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000261 if (It != PhysRegsUseOrder.end())
262 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000263}
264
Chris Lattner91a452b2003-01-13 00:25:40 +0000265
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000266/// spillVirtReg - This method spills the value specified by PhysReg into the
267/// virtual register slot specified by VirtReg. It then updates the RA data
268/// structures to indicate the fact that PhysReg is now available.
269///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000270void RALocal::spillVirtReg(MachineBasicBlock &MBB,
271 MachineBasicBlock::iterator I,
272 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000273 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000274 " Must not have appropriate kill for the register or use exists beyond"
275 " the intended one.");
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000276 DOUT << " Spilling register " << RegInfo->getName(PhysReg)
277 << " containing %reg" << VirtReg;
278 if (!isVirtRegModified(VirtReg))
279 DOUT << " which has not been modified, so no store necessary!";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000280
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000281 // Otherwise, there is a virtual register corresponding to this physical
282 // register. We only need to spill it into its stack slot if it has been
283 // modified.
284 if (isVirtRegModified(VirtReg)) {
285 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
286 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000287 DOUT << " to stack slot #" << FrameIndex;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000288 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000289 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000290 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000291
292 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000293
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000294 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000295 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000296}
297
Chris Lattnerae640432002-12-17 02:50:10 +0000298
Chris Lattner91a452b2003-01-13 00:25:40 +0000299/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000300/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
301/// then the request is ignored if the physical register does not contain a
302/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000303///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000304void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
305 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000306 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000307 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000308 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
309 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000310 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000311 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000312 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000313 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000314 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000315 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
316 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Chengddee8422006-11-15 20:55:15 +0000317 if (PhysRegsUsed[*AliasSet] == 0) {
318 // This must have been a dead def due to something like this:
319 // %EAX :=
320 // := op %AL
321 // No more use of %EAX, %AH, etc.
322 // %EAX isn't dead upon definition, but %AH is. However %AH isn't
323 // an operand of definition MI so it's not marked as such.
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000324 DOUT << " Register " << RegInfo->getName(*AliasSet)
325 << " [%reg" << *AliasSet
326 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000327 removePhysReg(*AliasSet);
328 } else
Chris Lattner64667b62004-02-09 01:26:13 +0000329 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000330 }
331}
332
333
334/// assignVirtToPhysReg - This method updates local state so that we know
335/// that PhysReg is the proper container for VirtReg now. The physical
336/// register must not be used for anything else when this is called.
337///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000338void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000339 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000340 // Update information to note the fact that this register was just used, and
341 // it holds VirtReg.
342 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000343 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000344 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
345}
346
347
Chris Lattnerae640432002-12-17 02:50:10 +0000348/// isPhysRegAvailable - Return true if the specified physical register is free
349/// and available for use. This also includes checking to see if aliased
350/// registers are all free...
351///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000352bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000353 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000354
355 // If the selected register aliases any other allocated registers, it is
356 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000357 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
358 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000359 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000360 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000361 return true;
362}
363
364
Chris Lattner91a452b2003-01-13 00:25:40 +0000365/// getFreeReg - Look to see if there is a free register available in the
366/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000367///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000368unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000369 // Get iterators defining the range of registers that are valid to allocate in
370 // this class, which also specifies the preferred allocation order.
371 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
372 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000373
Chris Lattner91a452b2003-01-13 00:25:40 +0000374 for (; RI != RE; ++RI)
375 if (isPhysRegAvailable(*RI)) { // Is reg unused?
376 assert(*RI != 0 && "Cannot use register!");
377 return *RI; // Found an unused register!
378 }
379 return 0;
380}
381
382
383/// liberatePhysReg - Make sure the specified physical register is available for
384/// use. If there is currently a value in it, it is either moved out of the way
385/// or spilled to memory.
386///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000387void RALocal::liberatePhysReg(MachineBasicBlock &MBB,
388 MachineBasicBlock::iterator &I,
389 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000390 spillPhysReg(MBB, I, PhysReg);
391}
392
393
394/// getReg - Find a physical register to hold the specified virtual
395/// register. If all compatible physical registers are used, this method spills
396/// the last used virtual register to the stack, and uses that register.
397///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000398unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
399 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000400 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
401
402 // First check to see if we have a free register of the requested type...
403 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000404
Chris Lattnerae640432002-12-17 02:50:10 +0000405 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000406 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000407 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000408
409 // Loop over all of the preallocated registers from the least recently used
410 // to the most recently used. When we find one that is capable of holding
411 // our register, use it.
412 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000413 assert(i != PhysRegsUseOrder.size() &&
414 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000415
Chris Lattnerae640432002-12-17 02:50:10 +0000416 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000417
418 // We can only use this register if it holds a virtual register (ie, it
419 // can be spilled). Do not use it if it is an explicitly allocated
420 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000421 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000422 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000423 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000424 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000425 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000426 PhysReg = R;
427 break;
428 } else {
429 // If one of the registers aliased to the current register is
430 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000431 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
432 *AliasIt; ++AliasIt) {
433 if (RC->contains(*AliasIt) &&
434 // If this is pinned down for some reason, don't use it. For
435 // example, if CL is pinned, and we run across CH, don't use
436 // CH as justification for using scavenging ECX (which will
437 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000438 PhysRegsUsed[*AliasIt] != 0 &&
439
440 // Make sure the register is allocatable. Don't allocate SIL on
441 // x86-32.
442 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000443 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000444 break;
445 }
446 }
Chris Lattner41822c72003-08-23 23:49:42 +0000447 }
Chris Lattnerae640432002-12-17 02:50:10 +0000448 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000449 }
450
Chris Lattnerae640432002-12-17 02:50:10 +0000451 assert(PhysReg && "Physical register not assigned!?!?");
452
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000453 // At this point PhysRegsUseOrder[i] is the least recently used register of
454 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000455 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000456 }
457
458 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000459 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000460 return PhysReg;
461}
462
Chris Lattnerae640432002-12-17 02:50:10 +0000463
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000464/// reloadVirtReg - This method transforms the specified specified virtual
465/// register use to refer to a physical register. This method may do this in
466/// one of several ways: if the register is available in a physical register
467/// already, it uses that physical register. If the value is not in a physical
468/// register, and if there are physical registers available, it loads it into a
469/// register. If register pressure is high, and it is possible, it tries to
470/// fold the load of the virtual register into the instruction itself. It
471/// avoids doing this if register pressure is low to improve the chance that
472/// subsequent instructions can use the reloaded value. This method returns the
473/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000474///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000475MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
476 unsigned OpNum) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000477 unsigned VirtReg = MI->getOperand(OpNum).getReg();
478
479 // If the virtual register is already available, just update the instruction
480 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000481 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000482 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000483 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000484 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000485 }
486
Chris Lattner1e3812c2004-02-17 04:08:37 +0000487 // Otherwise, we need to fold it into the current instruction, or reload it.
488 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000489 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000490 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000491 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000492
Chris Lattner11390e72004-02-17 08:09:40 +0000493 if (PhysReg) { // Register is available, allocate it!
494 assignVirtToPhysReg(VirtReg, PhysReg);
495 } else { // No registers available.
496 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000497 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000498 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000499 // Since we changed the address of MI, make sure to update live variables
500 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000501 LV->instructionChanged(MI, FMI);
502 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000503 }
504
505 // It looks like we can't fold this virtual register load into this
506 // instruction. Force some poor hapless value out of the register file to
507 // make room for the new register, and reload it.
508 PhysReg = getReg(MBB, MI, VirtReg);
509 }
510
Chris Lattner91a452b2003-01-13 00:25:40 +0000511 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
512
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000513 DOUT << " Reloading %reg" << VirtReg << " into "
514 << RegInfo->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000515
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000516 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000517 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000518 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000519
Evan Cheng6c087e52007-04-25 22:13:27 +0000520 MF->setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000521 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000522 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000523}
524
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000525
526
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000527void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000528 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000529 MachineBasicBlock::iterator MII = MBB.begin();
530 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000531
Evan Chengddee8422006-11-15 20:55:15 +0000532 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000533 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Chengddee8422006-11-15 20:55:15 +0000534
Chris Lattner44500e32006-06-15 22:21:53 +0000535 // If this is the first basic block in the machine function, add live-in
536 // registers as active.
537 if (&MBB == &*MF->begin()) {
538 for (MachineFunction::livein_iterator I = MF->livein_begin(),
539 E = MF->livein_end(); I != E; ++I) {
540 unsigned Reg = I->first;
Evan Cheng6c087e52007-04-25 22:13:27 +0000541 MF->setPhysRegUsed(Reg);
Chris Lattner44500e32006-06-15 22:21:53 +0000542 PhysRegsUsed[Reg] = 0; // It is free and reserved now
543 PhysRegsUseOrder.push_back(Reg);
544 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
545 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000546 if (PhysRegsUsed[*AliasSet] != -2) {
547 PhysRegsUseOrder.push_back(*AliasSet);
548 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000549 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000550 }
Chris Lattner44500e32006-06-15 22:21:53 +0000551 }
552 }
553 }
554
555 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000556 while (MII != MBB.end()) {
557 MachineInstr *MI = MII++;
558 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000559 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
560 DOUT << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000561 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000562 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000563 DOUT << "[" << RegInfo->getName(i)
564 << ",%reg" << PhysRegsUsed[i] << "] ";
565 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000566
Chris Lattnerae640432002-12-17 02:50:10 +0000567 // Loop over the implicit uses, making sure that they are at the head of the
568 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000569 if (TID.ImplicitUses) {
570 for (const unsigned *ImplicitUses = TID.ImplicitUses;
571 *ImplicitUses; ++ImplicitUses)
572 MarkPhysRegRecentlyUsed(*ImplicitUses);
573 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000574
Evan Chengddee8422006-11-15 20:55:15 +0000575 SmallVector<unsigned, 8> Kills;
576 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
577 MachineOperand& MO = MI->getOperand(i);
578 if (MO.isRegister() && MO.isKill())
579 Kills.push_back(MO.getReg());
580 }
581
Brian Gaeke53b99a02003-08-15 21:19:25 +0000582 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000583 // incoming values if we are out of registers. Note that we completely
584 // ignore physical register uses here. We assume that if an explicit
585 // physical register is referenced by the instruction, that it is guaranteed
586 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000587 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000588 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
589 MachineOperand& MO = MI->getOperand(i);
590 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000591 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000592 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000593 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000594 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000595
Evan Chengddee8422006-11-15 20:55:15 +0000596 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000597 // value, freeing the register being used, so it doesn't need to be
598 // spilled to memory.
599 //
Evan Chengddee8422006-11-15 20:55:15 +0000600 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
601 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000602 unsigned PhysReg = VirtReg;
603 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
604 // If the virtual register was never materialized into a register, it
605 // might not be in the map, but it won't hurt to zero it out anyway.
606 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
607 PhysReg = PhysRegSlot;
608 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000609 } else if (PhysRegsUsed[PhysReg] == -2) {
610 // Unallocatable register dead, ignore.
611 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000612 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000613
Chris Lattner56ddada2004-02-17 17:49:10 +0000614 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000615 DOUT << " Last use of " << RegInfo->getName(PhysReg)
616 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000617 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000618 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
619 *AliasSet; ++AliasSet) {
620 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000621 DOUT << " Last use of "
Evan Chengddee8422006-11-15 20:55:15 +0000622 << RegInfo->getName(*AliasSet)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000623 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000624 removePhysReg(*AliasSet);
625 }
626 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000627 }
628 }
629
630 // Loop over all of the operands of the instruction, spilling registers that
631 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000632 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
633 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000634 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000635 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
636 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000637 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
638
Evan Cheng6c087e52007-04-25 22:13:27 +0000639 MF->setPhysRegUsed(Reg);
Evan Chengddee8422006-11-15 20:55:15 +0000640 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000641 PhysRegsUsed[Reg] = 0; // It is free and reserved now
642 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000643 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
644 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000645 if (PhysRegsUsed[*AliasSet] != -2) {
646 PhysRegsUseOrder.push_back(*AliasSet);
647 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000648 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000649 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000650 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000651 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000652 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000653
654 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000655 if (TID.ImplicitDefs) {
656 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
657 *ImplicitDefs; ++ImplicitDefs) {
658 unsigned Reg = *ImplicitDefs;
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000659 bool IsNonAllocatable = PhysRegsUsed[Reg] == -2;
660 if (!IsNonAllocatable) {
661 spillPhysReg(MBB, MI, Reg, true);
662 PhysRegsUseOrder.push_back(Reg);
663 PhysRegsUsed[Reg] = 0; // It is free and reserved now
664 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000665 MF->setPhysRegUsed(Reg);
Chris Lattner0648b162005-01-23 22:51:56 +0000666
Jim Laskeycd4317e2006-07-21 21:15:20 +0000667 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
668 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000669 if (PhysRegsUsed[*AliasSet] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000670 if (!IsNonAllocatable) {
671 PhysRegsUseOrder.push_back(*AliasSet);
672 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
673 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000674 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000675 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000676 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000677 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000678 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000679
Evan Chengddee8422006-11-15 20:55:15 +0000680 SmallVector<unsigned, 8> DeadDefs;
681 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
682 MachineOperand& MO = MI->getOperand(i);
683 if (MO.isRegister() && MO.isDead())
684 DeadDefs.push_back(MO.getReg());
685 }
686
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000687 // Okay, we have allocated all of the source operands and spilled any values
688 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000689 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000690 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000691 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000692 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
693 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000694 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000695 MRegisterInfo::isVirtualRegister(MO.getReg())) {
696 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000697 unsigned DestPhysReg;
698
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000699 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000700 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000701 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Evan Cheng6c087e52007-04-25 22:13:27 +0000702 MF->setPhysRegUsed(DestPhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000703 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000704 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000705 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000706 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000707
Chris Lattner56ddada2004-02-17 17:49:10 +0000708 // If this instruction defines any registers that are immediately dead,
709 // kill them now.
710 //
Evan Chengddee8422006-11-15 20:55:15 +0000711 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
712 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000713 unsigned PhysReg = VirtReg;
714 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
715 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
716 PhysReg = PhysRegSlot;
717 assert(PhysReg != 0);
718 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000719 } else if (PhysRegsUsed[PhysReg] == -2) {
720 // Unallocatable register dead, ignore.
721 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000722 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000723
Chris Lattner56ddada2004-02-17 17:49:10 +0000724 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000725 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000726 << " [%reg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000727 << "] is never used, removing it frame live list\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000728 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000729 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
730 *AliasSet; ++AliasSet) {
731 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000732 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000733 << " [%reg" << *AliasSet
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000734 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000735 removePhysReg(*AliasSet);
736 }
737 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000738 }
739 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000740
741 // Finally, if this is a noop copy instruction, zap it.
742 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000743 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
744 LV->removeVirtualRegistersKilled(MI);
745 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000746 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000747 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000748 }
749
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000750 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000751
752 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000753 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000754 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000755 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000756 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000757 else
758 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000759
Chris Lattner9a5ef202005-11-09 05:28:45 +0000760#if 0
761 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000762 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000763 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
764 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000765 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +0000766 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +0000767 AllOk = false;
768 }
769 assert(AllOk && "Virtual registers still in phys regs?");
770#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000771
Chris Lattner128c2aa2003-08-17 18:01:15 +0000772 // Clear any physical register which appear live at the end of the basic
773 // block, but which do not hold any virtual registers. e.g., the stack
774 // pointer.
775 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000776}
777
Chris Lattner86c69a62002-12-17 03:16:10 +0000778
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000779/// runOnMachineFunction - Register allocate the whole function
780///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000781bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000782 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000783 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000784 TM = &Fn.getTarget();
785 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000786 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000787
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000788 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +0000789
790 // At various places we want to efficiently check to see whether a register
791 // is allocatable. To handle this, we mark all unallocatable registers as
792 // being pinned down, permanently.
793 {
Evan Cheng61de82d2007-02-15 05:59:24 +0000794 BitVector Allocable = RegInfo->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +0000795 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
796 if (!Allocable[i])
797 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
798 }
Chris Lattner64667b62004-02-09 01:26:13 +0000799
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000800 // initialize the virtual->physical register map to have a 'null'
801 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000802 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000803
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000804 // Loop over all of the basic blocks, eliminating virtual register references
805 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
806 MBB != MBBe; ++MBB)
807 AllocateBasicBlock(*MBB);
808
Chris Lattner580f9be2002-12-28 20:40:43 +0000809 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000810 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000811 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000812 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000813 return true;
814}
815
Chris Lattneref09c632004-01-31 21:27:19 +0000816FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000817 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000818}