blob: 076af6dd48f9a2d70336c672a289e182afc79913 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Evan Cheng7ac19af2007-06-26 21:05:13 +0000109 void AddToPhysRegsUseOrder(unsigned Reg) {
110 std::vector<unsigned>::iterator It =
111 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
112 if (It != PhysRegsUseOrder.end())
113 PhysRegsUseOrder.erase(It);
114 PhysRegsUseOrder.push_back(Reg);
115 }
116
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000117 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000118 if (PhysRegsUseOrder.empty() ||
119 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000120
121 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000122 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
123 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
124 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
125 // Add it to the end of the list
126 PhysRegsUseOrder.push_back(RegMatch);
127 if (RegMatch == Reg)
128 return; // Found an exact match, exit early
129 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000130 }
131
132 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000133 virtual const char *getPassName() const {
134 return "Local Register Allocator";
135 }
136
Chris Lattner91a452b2003-01-13 00:25:40 +0000137 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000138 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000139 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000140 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000141 MachineFunctionPass::getAnalysisUsage(AU);
142 }
143
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000144 private:
145 /// runOnMachineFunction - Register allocate the whole function
146 bool runOnMachineFunction(MachineFunction &Fn);
147
148 /// AllocateBasicBlock - Register allocate the specified basic block.
149 void AllocateBasicBlock(MachineBasicBlock &MBB);
150
Chris Lattner82bee0f2002-12-18 08:14:26 +0000151
Chris Lattner82bee0f2002-12-18 08:14:26 +0000152 /// areRegsEqual - This method returns true if the specified registers are
153 /// related to each other. To do this, it checks to see if they are equal
154 /// or if the first register is in the alias set of the second register.
155 ///
156 bool areRegsEqual(unsigned R1, unsigned R2) const {
157 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000158 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
159 *AliasSet; ++AliasSet) {
160 if (*AliasSet == R1) return true;
161 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000162 return false;
163 }
164
Chris Lattner580f9be2002-12-28 20:40:43 +0000165 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000166 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000167 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000168
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000169 /// removePhysReg - This method marks the specified physical register as no
170 /// longer being in use.
171 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000172 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000173
174 /// spillVirtReg - This method spills the value specified by PhysReg into
175 /// the virtual register slot specified by VirtReg. It then updates the RA
176 /// data structures to indicate the fact that PhysReg is now available.
177 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000178 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000179 unsigned VirtReg, unsigned PhysReg);
180
Chris Lattnerc21be922002-12-16 17:44:42 +0000181 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000182 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
183 /// true, then the request is ignored if the physical register does not
184 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000185 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000186 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000187 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000188
Chris Lattner91a452b2003-01-13 00:25:40 +0000189 /// assignVirtToPhysReg - This method updates local state so that we know
190 /// that PhysReg is the proper container for VirtReg now. The physical
191 /// register must not be used for anything else when this is called.
192 ///
193 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
194
Chris Lattnerae640432002-12-17 02:50:10 +0000195 /// isPhysRegAvailable - Return true if the specified physical register is
196 /// free and available for use. This also includes checking to see if
197 /// aliased registers are all free...
198 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000199 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000200
201 /// getFreeReg - Look to see if there is a free register available in the
202 /// specified register class. If not, return 0.
203 ///
204 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000205
Chris Lattner91a452b2003-01-13 00:25:40 +0000206 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000207 /// register. If all compatible physical registers are used, this method
208 /// spills the last used virtual register to the stack, and uses that
209 /// register.
210 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000211 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000212 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000213
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000214 /// reloadVirtReg - This method transforms the specified specified virtual
215 /// register use to refer to a physical register. This method may do this
216 /// in one of several ways: if the register is available in a physical
217 /// register already, it uses that physical register. If the value is not
218 /// in a physical register, and if there are physical registers available,
219 /// it loads it into a register. If register pressure is high, and it is
220 /// possible, it tries to fold the load of the virtual register into the
221 /// instruction itself. It avoids doing this if register pressure is low to
222 /// improve the chance that subsequent instructions can use the reloaded
223 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000224 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000225 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
226 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000227
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000228
229 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
230 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000231 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000232 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000233}
234
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000235/// getStackSpaceFor - This allocates space for the specified virtual register
236/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000237int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000238 // Find the location Reg would belong...
239 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000240
Chris Lattner580f9be2002-12-28 20:40:43 +0000241 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000242 return I->second; // Already has space allocated?
243
Chris Lattner580f9be2002-12-28 20:40:43 +0000244 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000245 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
246 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000247
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000248 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000249 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
250 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000251}
252
Chris Lattnerae640432002-12-17 02:50:10 +0000253
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000254/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000255/// longer being in use.
256///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000257void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000258 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000259
260 std::vector<unsigned>::iterator It =
261 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000262 if (It != PhysRegsUseOrder.end())
263 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000264}
265
Chris Lattner91a452b2003-01-13 00:25:40 +0000266
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000267/// spillVirtReg - This method spills the value specified by PhysReg into the
268/// virtual register slot specified by VirtReg. It then updates the RA data
269/// structures to indicate the fact that PhysReg is now available.
270///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000271void RALocal::spillVirtReg(MachineBasicBlock &MBB,
272 MachineBasicBlock::iterator I,
273 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000274 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000275 " Must not have appropriate kill for the register or use exists beyond"
276 " the intended one.");
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000277 DOUT << " Spilling register " << RegInfo->getName(PhysReg)
278 << " containing %reg" << VirtReg;
279 if (!isVirtRegModified(VirtReg))
280 DOUT << " which has not been modified, so no store necessary!";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000281
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000282 // Otherwise, there is a virtual register corresponding to this physical
283 // register. We only need to spill it into its stack slot if it has been
284 // modified.
285 if (isVirtRegModified(VirtReg)) {
286 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
287 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000288 DOUT << " to stack slot #" << FrameIndex;
Evan Chengd64b5c82007-12-05 03:14:33 +0000289 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000290 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000291 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000292
293 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000294
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000295 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000296 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000297}
298
Chris Lattnerae640432002-12-17 02:50:10 +0000299
Chris Lattner91a452b2003-01-13 00:25:40 +0000300/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000301/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
302/// then the request is ignored if the physical register does not contain a
303/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000304///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000305void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
306 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000307 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000308 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000309 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
310 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000311 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000312 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000313 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000314 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000315 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000316 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
317 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Cheng7ac19af2007-06-26 21:05:13 +0000318 if (PhysRegsUsed[*AliasSet])
319 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000320 }
321}
322
323
324/// assignVirtToPhysReg - This method updates local state so that we know
325/// that PhysReg is the proper container for VirtReg now. The physical
326/// register must not be used for anything else when this is called.
327///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000328void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000329 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000330 // Update information to note the fact that this register was just used, and
331 // it holds VirtReg.
332 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000333 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000334 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000335}
336
337
Chris Lattnerae640432002-12-17 02:50:10 +0000338/// isPhysRegAvailable - Return true if the specified physical register is free
339/// and available for use. This also includes checking to see if aliased
340/// registers are all free...
341///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000342bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000343 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000344
345 // If the selected register aliases any other allocated registers, it is
346 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000347 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
348 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000349 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000350 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000351 return true;
352}
353
354
Chris Lattner91a452b2003-01-13 00:25:40 +0000355/// getFreeReg - Look to see if there is a free register available in the
356/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000357///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000358unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000359 // Get iterators defining the range of registers that are valid to allocate in
360 // this class, which also specifies the preferred allocation order.
361 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
362 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000363
Chris Lattner91a452b2003-01-13 00:25:40 +0000364 for (; RI != RE; ++RI)
365 if (isPhysRegAvailable(*RI)) { // Is reg unused?
366 assert(*RI != 0 && "Cannot use register!");
367 return *RI; // Found an unused register!
368 }
369 return 0;
370}
371
372
Chris Lattner91a452b2003-01-13 00:25:40 +0000373/// getReg - Find a physical register to hold the specified virtual
374/// register. If all compatible physical registers are used, this method spills
375/// the last used virtual register to the stack, and uses that register.
376///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000377unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
378 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000379 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
380
381 // First check to see if we have a free register of the requested type...
382 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000383
Chris Lattnerae640432002-12-17 02:50:10 +0000384 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000385 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000386 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000387
388 // Loop over all of the preallocated registers from the least recently used
389 // to the most recently used. When we find one that is capable of holding
390 // our register, use it.
391 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000392 assert(i != PhysRegsUseOrder.size() &&
393 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000394
Chris Lattnerae640432002-12-17 02:50:10 +0000395 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000396
397 // We can only use this register if it holds a virtual register (ie, it
398 // can be spilled). Do not use it if it is an explicitly allocated
399 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000400 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000401 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000402 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000403 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000404 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000405 PhysReg = R;
406 break;
407 } else {
408 // If one of the registers aliased to the current register is
409 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000410 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
411 *AliasIt; ++AliasIt) {
412 if (RC->contains(*AliasIt) &&
413 // If this is pinned down for some reason, don't use it. For
414 // example, if CL is pinned, and we run across CH, don't use
415 // CH as justification for using scavenging ECX (which will
416 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000417 PhysRegsUsed[*AliasIt] != 0 &&
418
419 // Make sure the register is allocatable. Don't allocate SIL on
420 // x86-32.
421 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000422 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000423 break;
424 }
425 }
Chris Lattner41822c72003-08-23 23:49:42 +0000426 }
Chris Lattnerae640432002-12-17 02:50:10 +0000427 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000428 }
429
Chris Lattnerae640432002-12-17 02:50:10 +0000430 assert(PhysReg && "Physical register not assigned!?!?");
431
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000432 // At this point PhysRegsUseOrder[i] is the least recently used register of
433 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000434 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000435 }
436
437 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000438 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000439 return PhysReg;
440}
441
Chris Lattnerae640432002-12-17 02:50:10 +0000442
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000443/// reloadVirtReg - This method transforms the specified specified virtual
444/// register use to refer to a physical register. This method may do this in
445/// one of several ways: if the register is available in a physical register
446/// already, it uses that physical register. If the value is not in a physical
447/// register, and if there are physical registers available, it loads it into a
448/// register. If register pressure is high, and it is possible, it tries to
449/// fold the load of the virtual register into the instruction itself. It
450/// avoids doing this if register pressure is low to improve the chance that
451/// subsequent instructions can use the reloaded value. This method returns the
452/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000453///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000454MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
455 unsigned OpNum) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000456 unsigned VirtReg = MI->getOperand(OpNum).getReg();
457
458 // If the virtual register is already available, just update the instruction
459 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000460 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000461 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000462 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000463 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000464 }
465
Chris Lattner1e3812c2004-02-17 04:08:37 +0000466 // Otherwise, we need to fold it into the current instruction, or reload it.
467 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000468 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000469 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000470 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000471
Chris Lattner11390e72004-02-17 08:09:40 +0000472 if (PhysReg) { // Register is available, allocate it!
473 assignVirtToPhysReg(VirtReg, PhysReg);
474 } else { // No registers available.
475 // If we can fold this spill into this instruction, do so now.
Evan Chengaee4af62007-12-02 08:30:39 +0000476 SmallVector<unsigned, 2> Ops;
477 Ops.push_back(OpNum);
478 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, Ops, FrameIndex)) {
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000479 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000480 // Since we changed the address of MI, make sure to update live variables
481 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000482 LV->instructionChanged(MI, FMI);
483 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000484 }
485
486 // It looks like we can't fold this virtual register load into this
487 // instruction. Force some poor hapless value out of the register file to
488 // make room for the new register, and reload it.
489 PhysReg = getReg(MBB, MI, VirtReg);
490 }
491
Chris Lattner91a452b2003-01-13 00:25:40 +0000492 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
493
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000494 DOUT << " Reloading %reg" << VirtReg << " into "
495 << RegInfo->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000496
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000497 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000498 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000499 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000500
Evan Cheng6c087e52007-04-25 22:13:27 +0000501 MF->setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000502 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000503 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000504}
505
Evan Cheng7ac19af2007-06-26 21:05:13 +0000506/// isReadModWriteImplicitKill - True if this is an implicit kill for a
507/// read/mod/write register, i.e. update partial register.
508static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
509 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
510 MachineOperand& MO = MI->getOperand(i);
511 if (MO.isRegister() && MO.getReg() == Reg && MO.isImplicit() &&
512 MO.isDef() && !MO.isDead())
513 return true;
514 }
515 return false;
516}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000517
Evan Cheng7ac19af2007-06-26 21:05:13 +0000518/// isReadModWriteImplicitDef - True if this is an implicit def for a
519/// read/mod/write register, i.e. update partial register.
520static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
521 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
522 MachineOperand& MO = MI->getOperand(i);
523 if (MO.isRegister() && MO.getReg() == Reg && MO.isImplicit() &&
524 !MO.isDef() && MO.isKill())
525 return true;
526 }
527 return false;
528}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000529
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000530void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000531 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000532 MachineBasicBlock::iterator MII = MBB.begin();
533 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000534
Evan Chengddee8422006-11-15 20:55:15 +0000535 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000536 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Chengddee8422006-11-15 20:55:15 +0000537
Chris Lattner44500e32006-06-15 22:21:53 +0000538 // If this is the first basic block in the machine function, add live-in
539 // registers as active.
540 if (&MBB == &*MF->begin()) {
541 for (MachineFunction::livein_iterator I = MF->livein_begin(),
542 E = MF->livein_end(); I != E; ++I) {
543 unsigned Reg = I->first;
Evan Cheng6c087e52007-04-25 22:13:27 +0000544 MF->setPhysRegUsed(Reg);
Chris Lattner44500e32006-06-15 22:21:53 +0000545 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Evan Cheng7ac19af2007-06-26 21:05:13 +0000546 AddToPhysRegsUseOrder(Reg);
547 for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg);
Chris Lattner44500e32006-06-15 22:21:53 +0000548 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000549 if (PhysRegsUsed[*AliasSet] != -2) {
Evan Cheng7ac19af2007-06-26 21:05:13 +0000550 AddToPhysRegsUseOrder(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000551 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000552 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000553 }
Chris Lattner44500e32006-06-15 22:21:53 +0000554 }
555 }
556 }
557
558 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000559 while (MII != MBB.end()) {
560 MachineInstr *MI = MII++;
561 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000562 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
563 DOUT << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000564 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000565 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000566 DOUT << "[" << RegInfo->getName(i)
567 << ",%reg" << PhysRegsUsed[i] << "] ";
568 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000569
Chris Lattnerae640432002-12-17 02:50:10 +0000570 // Loop over the implicit uses, making sure that they are at the head of the
571 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000572 if (TID.ImplicitUses) {
573 for (const unsigned *ImplicitUses = TID.ImplicitUses;
574 *ImplicitUses; ++ImplicitUses)
575 MarkPhysRegRecentlyUsed(*ImplicitUses);
576 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000577
Evan Chengddee8422006-11-15 20:55:15 +0000578 SmallVector<unsigned, 8> Kills;
579 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
580 MachineOperand& MO = MI->getOperand(i);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000581 if (MO.isRegister() && MO.isKill()) {
582 if (!MO.isImplicit())
583 Kills.push_back(MO.getReg());
584 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
585 // These are extra physical register kills when a sub-register
586 // is defined (def of a sub-register is a read/mod/write of the
587 // larger registers). Ignore.
588 Kills.push_back(MO.getReg());
589 }
Evan Chengddee8422006-11-15 20:55:15 +0000590 }
591
Brian Gaeke53b99a02003-08-15 21:19:25 +0000592 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000593 // incoming values if we are out of registers. Note that we completely
594 // ignore physical register uses here. We assume that if an explicit
595 // physical register is referenced by the instruction, that it is guaranteed
596 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000597 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000598 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
599 MachineOperand& MO = MI->getOperand(i);
600 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000601 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000602 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000603 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000604 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000605
Evan Chengddee8422006-11-15 20:55:15 +0000606 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000607 // value, freeing the register being used, so it doesn't need to be
608 // spilled to memory.
609 //
Evan Chengddee8422006-11-15 20:55:15 +0000610 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
611 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000612 unsigned PhysReg = VirtReg;
613 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
614 // If the virtual register was never materialized into a register, it
615 // might not be in the map, but it won't hurt to zero it out anyway.
616 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
617 PhysReg = PhysRegSlot;
618 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000619 } else if (PhysRegsUsed[PhysReg] == -2) {
620 // Unallocatable register dead, ignore.
621 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000622 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000623 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000624 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000625 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000626
Chris Lattner56ddada2004-02-17 17:49:10 +0000627 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000628 DOUT << " Last use of " << RegInfo->getName(PhysReg)
629 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000630 removePhysReg(PhysReg);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000631 for (const unsigned *AliasSet = RegInfo->getSubRegisters(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000632 *AliasSet; ++AliasSet) {
633 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000634 DOUT << " Last use of "
Evan Chengddee8422006-11-15 20:55:15 +0000635 << RegInfo->getName(*AliasSet)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000636 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000637 removePhysReg(*AliasSet);
638 }
639 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000640 }
641 }
642
643 // Loop over all of the operands of the instruction, spilling registers that
644 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000645 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
646 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000647 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000648 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
649 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000650 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
Evan Cheng7ac19af2007-06-26 21:05:13 +0000651 // These are extra physical register defs when a sub-register
652 // is defined (def of a sub-register is a read/mod/write of the
653 // larger registers). Ignore.
654 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
655
Evan Cheng6c087e52007-04-25 22:13:27 +0000656 MF->setPhysRegUsed(Reg);
Evan Chengddee8422006-11-15 20:55:15 +0000657 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000658 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Evan Cheng7ac19af2007-06-26 21:05:13 +0000659 AddToPhysRegsUseOrder(Reg);
660
661 for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000662 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000663 if (PhysRegsUsed[*AliasSet] != -2) {
Evan Cheng6c087e52007-04-25 22:13:27 +0000664 MF->setPhysRegUsed(*AliasSet);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000665 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
666 AddToPhysRegsUseOrder(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000667 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000668 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000669 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000670 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000671
672 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000673 if (TID.ImplicitDefs) {
674 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
675 *ImplicitDefs; ++ImplicitDefs) {
676 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000677 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000678 spillPhysReg(MBB, MI, Reg, true);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000679 AddToPhysRegsUseOrder(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000680 PhysRegsUsed[Reg] = 0; // It is free and reserved now
681 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000682 MF->setPhysRegUsed(Reg);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000683 for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg);
Jim Laskeycd4317e2006-07-21 21:15:20 +0000684 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000685 if (PhysRegsUsed[*AliasSet] != -2) {
Evan Cheng7ac19af2007-06-26 21:05:13 +0000686 AddToPhysRegsUseOrder(*AliasSet);
687 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000688 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000689 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000690 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000691 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000692 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000693
Evan Chengddee8422006-11-15 20:55:15 +0000694 SmallVector<unsigned, 8> DeadDefs;
695 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
696 MachineOperand& MO = MI->getOperand(i);
697 if (MO.isRegister() && MO.isDead())
698 DeadDefs.push_back(MO.getReg());
699 }
700
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000701 // Okay, we have allocated all of the source operands and spilled any values
702 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000703 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000704 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000705 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000706 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
707 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000708 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000709 MRegisterInfo::isVirtualRegister(MO.getReg())) {
710 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000711 unsigned DestPhysReg;
712
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000713 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000714 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000715 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Evan Cheng6c087e52007-04-25 22:13:27 +0000716 MF->setPhysRegUsed(DestPhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000717 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000718 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000719 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000720 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000721
Chris Lattner56ddada2004-02-17 17:49:10 +0000722 // If this instruction defines any registers that are immediately dead,
723 // kill them now.
724 //
Evan Chengddee8422006-11-15 20:55:15 +0000725 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
726 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000727 unsigned PhysReg = VirtReg;
728 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
729 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
730 PhysReg = PhysRegSlot;
731 assert(PhysReg != 0);
732 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000733 } else if (PhysRegsUsed[PhysReg] == -2) {
734 // Unallocatable register dead, ignore.
735 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000736 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000737
Chris Lattner56ddada2004-02-17 17:49:10 +0000738 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000739 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000740 << " [%reg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000741 << "] is never used, removing it frame live list\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000742 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000743 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
744 *AliasSet; ++AliasSet) {
745 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000746 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000747 << " [%reg" << *AliasSet
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000748 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000749 removePhysReg(*AliasSet);
750 }
751 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000752 }
753 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000754
755 // Finally, if this is a noop copy instruction, zap it.
756 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000757 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
758 LV->removeVirtualRegistersKilled(MI);
759 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000760 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000761 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000762 }
763
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000764 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000765
766 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000767 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000768 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000769 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000770 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000771 else
772 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000773
Chris Lattner9a5ef202005-11-09 05:28:45 +0000774#if 0
775 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000776 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000777 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
778 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000779 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +0000780 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +0000781 AllOk = false;
782 }
783 assert(AllOk && "Virtual registers still in phys regs?");
784#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000785
Chris Lattner128c2aa2003-08-17 18:01:15 +0000786 // Clear any physical register which appear live at the end of the basic
787 // block, but which do not hold any virtual registers. e.g., the stack
788 // pointer.
789 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000790}
791
Chris Lattner86c69a62002-12-17 03:16:10 +0000792
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000793/// runOnMachineFunction - Register allocate the whole function
794///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000795bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000796 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000797 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000798 TM = &Fn.getTarget();
799 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000800 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000801
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000802 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +0000803
804 // At various places we want to efficiently check to see whether a register
805 // is allocatable. To handle this, we mark all unallocatable registers as
806 // being pinned down, permanently.
807 {
Evan Cheng61de82d2007-02-15 05:59:24 +0000808 BitVector Allocable = RegInfo->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +0000809 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
810 if (!Allocable[i])
811 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
812 }
Chris Lattner64667b62004-02-09 01:26:13 +0000813
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000814 // initialize the virtual->physical register map to have a 'null'
815 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000816 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000817
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000818 // Loop over all of the basic blocks, eliminating virtual register references
819 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
820 MBB != MBBe; ++MBB)
821 AllocateBasicBlock(*MBB);
822
Chris Lattner580f9be2002-12-28 20:40:43 +0000823 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000824 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000825 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000826 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000827 return true;
828}
829
Chris Lattneref09c632004-01-31 21:27:19 +0000830FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000831 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000832}