blob: 6454900a3ee74ec588b672f787767b9fa7c15e4b [file] [log] [blame]
Chris Lattner101b8cd2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +00002//
John Criswell482202a2003-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 Evlogimenosde6a3812004-02-13 18:20:47 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner101b8cd2002-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 Lattner74e4e9b2003-08-03 21:47:31 +000015#define DEBUG_TYPE "regalloc"
Evan Cheng0ffff1c2006-11-15 20:55:15 +000016#include "llvm/BasicBlock.h"
Chris Lattnerbfa53192003-01-13 00:25:40 +000017#include "llvm/CodeGen/Passes.h"
Chris Lattnerb4e41112002-12-28 20:40:43 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner101b8cd2002-12-16 16:15:28 +000019#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner42714ec2002-12-25 05:05:46 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerca4362f2002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerbfa53192003-01-13 00:25:40 +000022#include "llvm/CodeGen/LiveVariables.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000023#include "llvm/CodeGen/RegAllocRegistry.h"
Chris Lattnerb4d58d72003-01-14 22:00:31 +000024#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner101b8cd2002-12-16 16:15:28 +000025#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner1003dc72007-02-01 05:32:05 +000029#include "llvm/ADT/IndexedMap.h"
Evan Cheng0ffff1c2006-11-15 20:55:15 +000030#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattnerc8b07dd2004-10-26 15:35:58 +000032#include <algorithm>
Chris Lattnerc330b982004-01-31 21:27:19 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattneraee775a2006-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 Laskey95eda5b2006-08-01 14:21:23 +000038
Chris Lattneraee775a2006-12-19 22:41:21 +000039namespace {
Jim Laskey95eda5b2006-08-01 14:21:23 +000040 static RegisterRegAlloc
41 localRegAlloc("local", " local register allocator",
42 createLocalRegisterAllocator);
43
44
Bill Wendling31fd60b2007-05-08 19:02:46 +000045 class VISIBILITY_HIDDEN RALocal : public MachineFunctionPass {
Devang Patel09f162c2007-05-01 21:15:47 +000046 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000047 static char ID;
Bill Wendling31fd60b2007-05-08 19:02:46 +000048 RALocal() : MachineFunctionPass((intptr_t)&ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000049 private:
Chris Lattnerb4e41112002-12-28 20:40:43 +000050 const TargetMachine *TM;
Chris Lattner101b8cd2002-12-16 16:15:28 +000051 MachineFunction *MF;
Chris Lattnerb4e41112002-12-28 20:40:43 +000052 const MRegisterInfo *RegInfo;
Chris Lattnerbfa53192003-01-13 00:25:40 +000053 LiveVariables *LV;
Chris Lattner42714ec2002-12-25 05:05:46 +000054
Chris Lattner815b85e2003-08-04 23:36:39 +000055 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
56 // values are spilled.
Chris Lattnerb4e41112002-12-28 20:40:43 +000057 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattner101b8cd2002-12-16 16:15:28 +000058
59 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenosd8bace72004-02-25 21:55:45 +000060 // that is currently available in a physical register.
Chris Lattner1003dc72007-02-01 05:32:05 +000061 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattner80cbed42004-02-09 02:12:04 +000062
63 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenosd8bace72004-02-25 21:55:45 +000064 return Virt2PhysRegMap[VirtReg];
Chris Lattner80cbed42004-02-09 02:12:04 +000065 }
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +000066
Chris Lattner490627a2004-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 Lattner9b1a6eb2006-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 Lattner101b8cd2002-12-16 16:15:28 +000075 //
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +000076 std::vector<int> PhysRegsUsed;
Chris Lattner101b8cd2002-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 Lattnerbfa53192003-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 Lattnerd4627092002-12-18 08:14:26 +000092 //
Chris Lattnerbfa53192003-01-13 00:25:40 +000093 std::vector<bool> VirtRegModified;
94
95 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattnerc330b982004-01-31 21:27:19 +000096 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattnerbfa53192003-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 Lattnerc330b982004-01-31 21:27:19 +0000103 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattnerbfa53192003-01-13 00:25:40 +0000104 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000105 && "Illegal virtual register!");
Chris Lattnerbfa53192003-01-13 00:25:40 +0000106 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
107 }
Chris Lattnerd4627092002-12-18 08:14:26 +0000108
Evan Cheng4bf87f12007-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 Lattner101b8cd2002-12-16 16:15:28 +0000117 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner7cc20d42006-09-03 07:15:37 +0000118 if (PhysRegsUseOrder.empty() ||
119 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner763729c52002-12-24 00:04:55 +0000120
121 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenosde6a3812004-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 Lattner101b8cd2002-12-16 16:15:28 +0000130 }
131
132 public:
Chris Lattner101b8cd2002-12-16 16:15:28 +0000133 virtual const char *getPassName() const {
134 return "Local Register Allocator";
135 }
136
Chris Lattnerbfa53192003-01-13 00:25:40 +0000137 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner3d894dd2004-02-17 17:49:10 +0000138 AU.addRequired<LiveVariables>();
Chris Lattnerbfa53192003-01-13 00:25:40 +0000139 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos71390902003-12-18 22:40:24 +0000140 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattnerbfa53192003-01-13 00:25:40 +0000141 MachineFunctionPass::getAnalysisUsage(AU);
142 }
143
Chris Lattner101b8cd2002-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 Lattnerd4627092002-12-18 08:14:26 +0000151
Chris Lattnerd4627092002-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 Evlogimenos5f1f3372003-10-08 05:20:08 +0000158 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
159 *AliasSet; ++AliasSet) {
160 if (*AliasSet == R1) return true;
161 }
Chris Lattnerd4627092002-12-18 08:14:26 +0000162 return false;
163 }
164
Chris Lattnerb4e41112002-12-28 20:40:43 +0000165 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattner815b85e2003-08-04 23:36:39 +0000166 /// register on the stack, allocating space if necessary.
Chris Lattnerb4e41112002-12-28 20:40:43 +0000167 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000168
Chris Lattner815b85e2003-08-04 23:36:39 +0000169 /// removePhysReg - This method marks the specified physical register as no
170 /// longer being in use.
171 ///
Chris Lattnerd4627092002-12-18 08:14:26 +0000172 void removePhysReg(unsigned PhysReg);
Chris Lattner101b8cd2002-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 Lattner84b40662004-02-22 19:08:15 +0000178 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattner101b8cd2002-12-16 16:15:28 +0000179 unsigned VirtReg, unsigned PhysReg);
180
Chris Lattner0129b862002-12-16 17:44:42 +0000181 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner931947d2003-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 Lattnerbfa53192003-01-13 00:25:40 +0000185 ///
Chris Lattnerddedac52004-02-17 03:57:19 +0000186 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner931947d2003-08-17 18:01:15 +0000187 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattner0129b862002-12-16 17:44:42 +0000188
Chris Lattnerbfa53192003-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 Lattner4664bd52002-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 Lattnerd4627092002-12-18 08:14:26 +0000199 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattnerbfa53192003-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 Evlogimenosde6a3812004-02-13 18:20:47 +0000205
Chris Lattnerbfa53192003-01-13 00:25:40 +0000206 /// getReg - Find a physical register to hold the specified virtual
Chris Lattner101b8cd2002-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 Lattnerddedac52004-02-17 03:57:19 +0000211 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000212 unsigned VirtReg);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000213
Chris Lattnerddedac52004-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 Lattner101b8cd2002-12-16 16:15:28 +0000224 ///
Chris Lattnerddedac52004-02-17 03:57:19 +0000225 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
226 unsigned OpNum);
Misha Brukman835702a2005-04-21 22:36:52 +0000227
Chris Lattner815b85e2003-08-04 23:36:39 +0000228
229 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
230 unsigned PhysReg);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000231 };
Bill Wendling31fd60b2007-05-08 19:02:46 +0000232 char RALocal::ID = 0;
Chris Lattner101b8cd2002-12-16 16:15:28 +0000233}
234
Chris Lattner815b85e2003-08-04 23:36:39 +0000235/// getStackSpaceFor - This allocates space for the specified virtual register
236/// to be held on the stack.
Bill Wendling31fd60b2007-05-08 19:02:46 +0000237int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattner815b85e2003-08-04 23:36:39 +0000238 // Find the location Reg would belong...
239 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000240
Chris Lattnerb4e41112002-12-28 20:40:43 +0000241 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner101b8cd2002-12-16 16:15:28 +0000242 return I->second; // Already has space allocated?
243
Chris Lattnerb4e41112002-12-28 20:40:43 +0000244 // Allocate a new stack object for this spill location...
Chris Lattnerc66f27f2004-08-15 22:02:22 +0000245 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
246 RC->getAlignment());
Chris Lattner101b8cd2002-12-16 16:15:28 +0000247
Chris Lattner101b8cd2002-12-16 16:15:28 +0000248 // Assign the slot...
Chris Lattnerb4e41112002-12-28 20:40:43 +0000249 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
250 return FrameIdx;
Chris Lattner101b8cd2002-12-16 16:15:28 +0000251}
252
Chris Lattner4664bd52002-12-17 02:50:10 +0000253
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000254/// removePhysReg - This method marks the specified physical register as no
Chris Lattnerd4627092002-12-18 08:14:26 +0000255/// longer being in use.
256///
Bill Wendling31fd60b2007-05-08 19:02:46 +0000257void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner490627a2004-02-09 01:26:13 +0000258 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattnerd4627092002-12-18 08:14:26 +0000259
260 std::vector<unsigned>::iterator It =
261 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000262 if (It != PhysRegsUseOrder.end())
263 PhysRegsUseOrder.erase(It);
Chris Lattnerd4627092002-12-18 08:14:26 +0000264}
265
Chris Lattnerbfa53192003-01-13 00:25:40 +0000266
Chris Lattner101b8cd2002-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 Wendling31fd60b2007-05-08 19:02:46 +0000271void RALocal::spillVirtReg(MachineBasicBlock &MBB,
272 MachineBasicBlock::iterator I,
273 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner92a199d2003-08-05 04:13:58 +0000274 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattner506fa682003-08-05 00:49:09 +0000275 " Must not have appropriate kill for the register or use exists beyond"
276 " the intended one.");
Bill Wendling9d46fcd2006-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 Lattner101b8cd2002-12-16 16:15:28 +0000281
Chris Lattner506fa682003-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 Wendling9d46fcd2006-11-17 02:09:07 +0000288 DOUT << " to stack slot #" << FrameIndex;
Chris Lattner5a6199f2005-09-30 01:29:00 +0000289 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenosd0a60b72004-02-19 06:19:09 +0000290 ++NumStores; // Update statistics
Chris Lattner101b8cd2002-12-16 16:15:28 +0000291 }
Chris Lattner80cbed42004-02-09 02:12:04 +0000292
293 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattner101b8cd2002-12-16 16:15:28 +0000294
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000295 DOUT << "\n";
Chris Lattnerd4627092002-12-18 08:14:26 +0000296 removePhysReg(PhysReg);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000297}
298
Chris Lattner4664bd52002-12-17 02:50:10 +0000299
Chris Lattnerbfa53192003-01-13 00:25:40 +0000300/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner931947d2003-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 Lattnerbfa53192003-01-13 00:25:40 +0000304///
Bill Wendling31fd60b2007-05-08 19:02:46 +0000305void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
306 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner490627a2004-02-09 01:26:13 +0000307 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000308 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner490627a2004-02-09 01:26:13 +0000309 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
310 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000311 } else {
Chris Lattnerbfa53192003-01-13 00:25:40 +0000312 // If the selected register aliases any other registers, we must make
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000313 // sure that one of the aliases isn't alive.
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000314 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner490627a2004-02-09 01:26:13 +0000315 *AliasSet; ++AliasSet)
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000316 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
317 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Cheng4bf87f12007-06-26 21:05:13 +0000318 if (PhysRegsUsed[*AliasSet])
319 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattnerbfa53192003-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 Wendling31fd60b2007-05-08 19:02:46 +0000328void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner490627a2004-02-09 01:26:13 +0000329 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattnerbfa53192003-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 Evlogimenosde6a3812004-02-13 18:20:47 +0000333 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Evan Cheng4bf87f12007-06-26 21:05:13 +0000334 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattnerbfa53192003-01-13 00:25:40 +0000335}
336
337
Chris Lattner4664bd52002-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 Wendling31fd60b2007-05-08 19:02:46 +0000342bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner490627a2004-02-09 01:26:13 +0000343 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattner4664bd52002-12-17 02:50:10 +0000344
345 // If the selected register aliases any other allocated registers, it is
346 // not free!
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000347 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
348 *AliasSet; ++AliasSet)
Chris Lattner490627a2004-02-09 01:26:13 +0000349 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000350 return false; // Can't use this reg then.
Chris Lattner4664bd52002-12-17 02:50:10 +0000351 return true;
352}
353
354
Chris Lattnerbfa53192003-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 Lattner101b8cd2002-12-16 16:15:28 +0000357///
Bill Wendling31fd60b2007-05-08 19:02:46 +0000358unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattnerb4e41112002-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 Lattner4664bd52002-12-17 02:50:10 +0000363
Chris Lattnerbfa53192003-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 Lattnerbfa53192003-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 Wendling31fd60b2007-05-08 19:02:46 +0000377unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
378 unsigned VirtReg) {
Chris Lattnerbfa53192003-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 Lattner101b8cd2002-12-16 16:15:28 +0000383
Chris Lattner4664bd52002-12-17 02:50:10 +0000384 // If we didn't find an unused register, scavenge one now!
Chris Lattner101b8cd2002-12-16 16:15:28 +0000385 if (PhysReg == 0) {
Chris Lattner0129b862002-12-16 17:44:42 +0000386 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattner4664bd52002-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 Lattner101b8cd2002-12-16 16:15:28 +0000392 assert(i != PhysRegsUseOrder.size() &&
393 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000394
Chris Lattner4664bd52002-12-17 02:50:10 +0000395 unsigned R = PhysRegsUseOrder[i];
Chris Lattnere6235442003-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 Lattner490627a2004-02-09 01:26:13 +0000400 assert(PhysRegsUsed[R] != -1 &&
Chris Lattnere6235442003-08-23 23:49:42 +0000401 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000402 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattnere6235442003-08-23 23:49:42 +0000403 // If the current register is compatible, use it.
Chris Lattner5943c502004-08-15 22:23:09 +0000404 if (RC->contains(R)) {
Chris Lattnere6235442003-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 Lattner7cc20d42006-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 Lattner9b1a6eb2006-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 Lattner7cc20d42006-09-03 07:15:37 +0000422 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000423 break;
424 }
425 }
Chris Lattnere6235442003-08-23 23:49:42 +0000426 }
Chris Lattner4664bd52002-12-17 02:50:10 +0000427 }
Chris Lattner101b8cd2002-12-16 16:15:28 +0000428 }
429
Chris Lattner4664bd52002-12-17 02:50:10 +0000430 assert(PhysReg && "Physical register not assigned!?!?");
431
Chris Lattner101b8cd2002-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 Lattner0129b862002-12-16 17:44:42 +0000434 spillPhysReg(MBB, I, PhysReg);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000435 }
436
437 // Now that we know which register we need to assign this to, do it now!
Chris Lattnerbfa53192003-01-13 00:25:40 +0000438 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000439 return PhysReg;
440}
441
Chris Lattner4664bd52002-12-17 02:50:10 +0000442
Chris Lattnerddedac52004-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 Lattner101b8cd2002-12-16 16:15:28 +0000453///
Bill Wendling31fd60b2007-05-08 19:02:46 +0000454MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
455 unsigned OpNum) {
Chris Lattnerddedac52004-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 Evlogimenosde6a3812004-02-13 18:20:47 +0000460 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattnerddedac52004-02-17 03:57:19 +0000461 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattner10d63412006-05-04 17:52:23 +0000462 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattnerddedac52004-02-17 03:57:19 +0000463 return MI;
Chris Lattner101b8cd2002-12-16 16:15:28 +0000464 }
465
Chris Lattnerba9e3e22004-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 Lattner42714ec2002-12-25 05:05:46 +0000468 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattnerba9e3e22004-02-17 04:08:37 +0000469 unsigned PhysReg = getFreeReg(RC);
Chris Lattner4e21b232004-02-17 08:09:40 +0000470 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattnerba9e3e22004-02-17 04:08:37 +0000471
Chris Lattner4e21b232004-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.
Alkis Evlogimenos48da2f82004-03-14 07:19:51 +0000476 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenos334114b2004-02-21 18:07:33 +0000477 ++NumFolded;
Chris Lattnerf5c5e1f2004-02-19 18:34:02 +0000478 // Since we changed the address of MI, make sure to update live variables
479 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos48da2f82004-03-14 07:19:51 +0000480 LV->instructionChanged(MI, FMI);
481 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattnerba9e3e22004-02-17 04:08:37 +0000482 }
483
484 // It looks like we can't fold this virtual register load into this
485 // instruction. Force some poor hapless value out of the register file to
486 // make room for the new register, and reload it.
487 PhysReg = getReg(MBB, MI, VirtReg);
488 }
489
Chris Lattnerbfa53192003-01-13 00:25:40 +0000490 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
491
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000492 DOUT << " Reloading %reg" << VirtReg << " into "
493 << RegInfo->getName(PhysReg) << "\n";
Chris Lattner815b85e2003-08-04 23:36:39 +0000494
Chris Lattner101b8cd2002-12-16 16:15:28 +0000495 // Add move instruction(s)
Chris Lattner5a6199f2005-09-30 01:29:00 +0000496 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenosd0a60b72004-02-19 06:19:09 +0000497 ++NumLoads; // Update statistics
Chris Lattnerddedac52004-02-17 03:57:19 +0000498
Evan Cheng0ba17452007-04-25 22:13:27 +0000499 MF->setPhysRegUsed(PhysReg);
Chris Lattner10d63412006-05-04 17:52:23 +0000500 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattnerddedac52004-02-17 03:57:19 +0000501 return MI;
Chris Lattner101b8cd2002-12-16 16:15:28 +0000502}
503
Evan Cheng4bf87f12007-06-26 21:05:13 +0000504/// isReadModWriteImplicitKill - True if this is an implicit kill for a
505/// read/mod/write register, i.e. update partial register.
506static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
507 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
508 MachineOperand& MO = MI->getOperand(i);
509 if (MO.isRegister() && MO.getReg() == Reg && MO.isImplicit() &&
510 MO.isDef() && !MO.isDead())
511 return true;
512 }
513 return false;
514}
Chris Lattner815b85e2003-08-04 23:36:39 +0000515
Evan Cheng4bf87f12007-06-26 21:05:13 +0000516/// isReadModWriteImplicitDef - True if this is an implicit def for a
517/// read/mod/write register, i.e. update partial register.
518static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
519 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
520 MachineOperand& MO = MI->getOperand(i);
521 if (MO.isRegister() && MO.getReg() == Reg && MO.isImplicit() &&
522 !MO.isDef() && MO.isKill())
523 return true;
524 }
525 return false;
526}
Chris Lattner815b85e2003-08-04 23:36:39 +0000527
Bill Wendling31fd60b2007-05-08 19:02:46 +0000528void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner101b8cd2002-12-16 16:15:28 +0000529 // loop over each instruction
Chris Lattner619dfaa2005-11-09 18:22:42 +0000530 MachineBasicBlock::iterator MII = MBB.begin();
531 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner4ff6c162006-06-15 22:21:53 +0000532
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000533 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000534 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000535
Chris Lattner4ff6c162006-06-15 22:21:53 +0000536 // If this is the first basic block in the machine function, add live-in
537 // registers as active.
538 if (&MBB == &*MF->begin()) {
539 for (MachineFunction::livein_iterator I = MF->livein_begin(),
540 E = MF->livein_end(); I != E; ++I) {
541 unsigned Reg = I->first;
Evan Cheng0ba17452007-04-25 22:13:27 +0000542 MF->setPhysRegUsed(Reg);
Chris Lattner4ff6c162006-06-15 22:21:53 +0000543 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Evan Cheng4bf87f12007-06-26 21:05:13 +0000544 AddToPhysRegsUseOrder(Reg);
545 for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg);
Chris Lattner4ff6c162006-06-15 22:21:53 +0000546 *AliasSet; ++AliasSet) {
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000547 if (PhysRegsUsed[*AliasSet] != -2) {
Evan Cheng4bf87f12007-06-26 21:05:13 +0000548 AddToPhysRegsUseOrder(*AliasSet);
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000549 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng0ba17452007-04-25 22:13:27 +0000550 MF->setPhysRegUsed(*AliasSet);
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000551 }
Chris Lattner4ff6c162006-06-15 22:21:53 +0000552 }
553 }
554 }
555
556 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattner619dfaa2005-11-09 18:22:42 +0000557 while (MII != MBB.end()) {
558 MachineInstr *MI = MII++;
559 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000560 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
561 DOUT << " Regs have values: ";
Chris Lattner490627a2004-02-09 01:26:13 +0000562 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000563 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000564 DOUT << "[" << RegInfo->getName(i)
565 << ",%reg" << PhysRegsUsed[i] << "] ";
566 DOUT << "\n");
Chris Lattner101b8cd2002-12-16 16:15:28 +0000567
Chris Lattner4664bd52002-12-17 02:50:10 +0000568 // Loop over the implicit uses, making sure that they are at the head of the
569 // use order list, so they don't get reallocated.
Jim Laskey4b49c232006-07-21 21:15:20 +0000570 if (TID.ImplicitUses) {
571 for (const unsigned *ImplicitUses = TID.ImplicitUses;
572 *ImplicitUses; ++ImplicitUses)
573 MarkPhysRegRecentlyUsed(*ImplicitUses);
574 }
Chris Lattner101b8cd2002-12-16 16:15:28 +0000575
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000576 SmallVector<unsigned, 8> Kills;
577 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
578 MachineOperand& MO = MI->getOperand(i);
Evan Cheng4bf87f12007-06-26 21:05:13 +0000579 if (MO.isRegister() && MO.isKill()) {
580 if (!MO.isImplicit())
581 Kills.push_back(MO.getReg());
582 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
583 // These are extra physical register kills when a sub-register
584 // is defined (def of a sub-register is a read/mod/write of the
585 // larger registers). Ignore.
586 Kills.push_back(MO.getReg());
587 }
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000588 }
589
Brian Gaeke91e16e72003-08-15 21:19:25 +0000590 // Get the used operands into registers. This has the potential to spill
Chris Lattner815b85e2003-08-04 23:36:39 +0000591 // incoming values if we are out of registers. Note that we completely
592 // ignore physical register uses here. We assume that if an explicit
593 // physical register is referenced by the instruction, that it is guaranteed
594 // to be live-in, or the input is badly hosed.
Chris Lattner101b8cd2002-12-16 16:15:28 +0000595 //
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000596 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
597 MachineOperand& MO = MI->getOperand(i);
598 // here we are looking for only used operands (never def&use)
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000599 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000600 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattnerddedac52004-02-17 03:57:19 +0000601 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000602 }
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000603
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000604 // If this instruction is the last user of this register, kill the
Chris Lattner3d894dd2004-02-17 17:49:10 +0000605 // value, freeing the register being used, so it doesn't need to be
606 // spilled to memory.
607 //
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000608 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
609 unsigned VirtReg = Kills[i];
Chris Lattner3d894dd2004-02-17 17:49:10 +0000610 unsigned PhysReg = VirtReg;
611 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
612 // If the virtual register was never materialized into a register, it
613 // might not be in the map, but it won't hurt to zero it out anyway.
614 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
615 PhysReg = PhysRegSlot;
616 PhysRegSlot = 0;
Chris Lattnerb2e73162006-09-08 20:21:31 +0000617 } else if (PhysRegsUsed[PhysReg] == -2) {
618 // Unallocatable register dead, ignore.
619 continue;
Evan Cheng4bf87f12007-06-26 21:05:13 +0000620 } else {
621 assert(!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1 &&
622 "Silently clearing a virtual register?");
Chris Lattner3d894dd2004-02-17 17:49:10 +0000623 }
Chris Lattnerbfa53192003-01-13 00:25:40 +0000624
Chris Lattner3d894dd2004-02-17 17:49:10 +0000625 if (PhysReg) {
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000626 DOUT << " Last use of " << RegInfo->getName(PhysReg)
627 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner3d894dd2004-02-17 17:49:10 +0000628 removePhysReg(PhysReg);
Evan Cheng4bf87f12007-06-26 21:05:13 +0000629 for (const unsigned *AliasSet = RegInfo->getSubRegisters(PhysReg);
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000630 *AliasSet; ++AliasSet) {
631 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000632 DOUT << " Last use of "
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000633 << RegInfo->getName(*AliasSet)
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000634 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000635 removePhysReg(*AliasSet);
636 }
637 }
Chris Lattnerbfa53192003-01-13 00:25:40 +0000638 }
639 }
640
641 // Loop over all of the operands of the instruction, spilling registers that
642 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000643 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
644 MachineOperand& MO = MI->getOperand(i);
Evan Cheng8c9c6d72006-11-10 08:43:01 +0000645 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000646 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
647 unsigned Reg = MO.getReg();
Chris Lattner050c64c2006-09-08 19:11:11 +0000648 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
Evan Cheng4bf87f12007-06-26 21:05:13 +0000649 // These are extra physical register defs when a sub-register
650 // is defined (def of a sub-register is a read/mod/write of the
651 // larger registers). Ignore.
652 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
653
Evan Cheng0ba17452007-04-25 22:13:27 +0000654 MF->setPhysRegUsed(Reg);
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000655 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattnerbfa53192003-01-13 00:25:40 +0000656 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Evan Cheng4bf87f12007-06-26 21:05:13 +0000657 AddToPhysRegsUseOrder(Reg);
658
659 for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg);
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000660 *AliasSet; ++AliasSet) {
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000661 if (PhysRegsUsed[*AliasSet] != -2) {
Evan Cheng0ba17452007-04-25 22:13:27 +0000662 MF->setPhysRegUsed(*AliasSet);
Evan Cheng4bf87f12007-06-26 21:05:13 +0000663 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
664 AddToPhysRegsUseOrder(*AliasSet);
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000665 }
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000666 }
Chris Lattnerbfa53192003-01-13 00:25:40 +0000667 }
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000668 }
Chris Lattnerbfa53192003-01-13 00:25:40 +0000669
670 // Loop over the implicit defs, spilling them as well.
Jim Laskey4b49c232006-07-21 21:15:20 +0000671 if (TID.ImplicitDefs) {
672 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
673 *ImplicitDefs; ++ImplicitDefs) {
674 unsigned Reg = *ImplicitDefs;
Evan Cheng4bf87f12007-06-26 21:05:13 +0000675 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner698000b2006-09-19 18:02:01 +0000676 spillPhysReg(MBB, MI, Reg, true);
Evan Cheng4bf87f12007-06-26 21:05:13 +0000677 AddToPhysRegsUseOrder(Reg);
Chris Lattner698000b2006-09-19 18:02:01 +0000678 PhysRegsUsed[Reg] = 0; // It is free and reserved now
679 }
Evan Cheng0ba17452007-04-25 22:13:27 +0000680 MF->setPhysRegUsed(Reg);
Evan Cheng4bf87f12007-06-26 21:05:13 +0000681 for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg);
Jim Laskey4b49c232006-07-21 21:15:20 +0000682 *AliasSet; ++AliasSet) {
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000683 if (PhysRegsUsed[*AliasSet] != -2) {
Evan Cheng4bf87f12007-06-26 21:05:13 +0000684 AddToPhysRegsUseOrder(*AliasSet);
685 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng0ba17452007-04-25 22:13:27 +0000686 MF->setPhysRegUsed(*AliasSet);
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000687 }
Jim Laskey4b49c232006-07-21 21:15:20 +0000688 }
Alkis Evlogimenosebbd66c2004-01-13 06:24:30 +0000689 }
Alkis Evlogimenos9bced942003-12-13 01:20:58 +0000690 }
Chris Lattnerbfa53192003-01-13 00:25:40 +0000691
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000692 SmallVector<unsigned, 8> DeadDefs;
693 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
694 MachineOperand& MO = MI->getOperand(i);
695 if (MO.isRegister() && MO.isDead())
696 DeadDefs.push_back(MO.getReg());
697 }
698
Chris Lattner101b8cd2002-12-16 16:15:28 +0000699 // Okay, we have allocated all of the source operands and spilled any values
700 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner24f0f0e2005-01-23 22:51:56 +0000701 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattnerbfa53192003-01-13 00:25:40 +0000702 // we need to scavenge a register.
Chris Lattnerd4627092002-12-18 08:14:26 +0000703 //
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000704 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
705 MachineOperand& MO = MI->getOperand(i);
Evan Chengddfb10b2006-09-05 20:32:06 +0000706 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000707 MRegisterInfo::isVirtualRegister(MO.getReg())) {
708 unsigned DestVirtReg = MO.getReg();
Chris Lattner101b8cd2002-12-16 16:15:28 +0000709 unsigned DestPhysReg;
710
Alkis Evlogimenosc17d57b2003-12-18 13:08:52 +0000711 // If DestVirtReg already has a value, use it.
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000712 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000713 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Evan Cheng0ba17452007-04-25 22:13:27 +0000714 MF->setPhysRegUsed(DestPhysReg);
Chris Lattner5a78ee82003-05-12 03:54:14 +0000715 markVirtRegModified(DestVirtReg);
Chris Lattner10d63412006-05-04 17:52:23 +0000716 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattner101b8cd2002-12-16 16:15:28 +0000717 }
Alkis Evlogimenos61719d42004-02-26 22:00:20 +0000718 }
Chris Lattnerd4627092002-12-18 08:14:26 +0000719
Chris Lattner3d894dd2004-02-17 17:49:10 +0000720 // If this instruction defines any registers that are immediately dead,
721 // kill them now.
722 //
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000723 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
724 unsigned VirtReg = DeadDefs[i];
Chris Lattner3d894dd2004-02-17 17:49:10 +0000725 unsigned PhysReg = VirtReg;
726 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
727 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
728 PhysReg = PhysRegSlot;
729 assert(PhysReg != 0);
730 PhysRegSlot = 0;
Chris Lattnerb2e73162006-09-08 20:21:31 +0000731 } else if (PhysRegsUsed[PhysReg] == -2) {
732 // Unallocatable register dead, ignore.
733 continue;
Chris Lattner3d894dd2004-02-17 17:49:10 +0000734 }
Chris Lattnerbfa53192003-01-13 00:25:40 +0000735
Chris Lattner3d894dd2004-02-17 17:49:10 +0000736 if (PhysReg) {
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000737 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner3d894dd2004-02-17 17:49:10 +0000738 << " [%reg" << VirtReg
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000739 << "] is never used, removing it frame live list\n";
Chris Lattner3d894dd2004-02-17 17:49:10 +0000740 removePhysReg(PhysReg);
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000741 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
742 *AliasSet; ++AliasSet) {
743 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000744 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000745 << " [%reg" << *AliasSet
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000746 << "] is never used, removing it frame live list\n";
Evan Cheng0ffff1c2006-11-15 20:55:15 +0000747 removePhysReg(*AliasSet);
748 }
749 }
Chris Lattnerd4627092002-12-18 08:14:26 +0000750 }
751 }
Chris Lattner619dfaa2005-11-09 18:22:42 +0000752
753 // Finally, if this is a noop copy instruction, zap it.
754 unsigned SrcReg, DstReg;
Chris Lattnerbd794582006-09-03 00:06:08 +0000755 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
756 LV->removeVirtualRegistersKilled(MI);
757 LV->removeVirtualRegistersDead(MI);
Chris Lattner619dfaa2005-11-09 18:22:42 +0000758 MBB.erase(MI);
Chris Lattnerbd794582006-09-03 00:06:08 +0000759 }
Chris Lattner101b8cd2002-12-16 16:15:28 +0000760 }
761
Chris Lattner619dfaa2005-11-09 18:22:42 +0000762 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattner101b8cd2002-12-16 16:15:28 +0000763
764 // Spill all physical registers holding virtual registers now.
Chris Lattner490627a2004-02-09 01:26:13 +0000765 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000766 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner490627a2004-02-09 01:26:13 +0000767 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000768 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner490627a2004-02-09 01:26:13 +0000769 else
770 removePhysReg(i);
Chris Lattner101b8cd2002-12-16 16:15:28 +0000771
Chris Lattner35ecaa72005-11-09 05:28:45 +0000772#if 0
773 // This checking code is very expensive.
Chris Lattner80cbed42004-02-09 02:12:04 +0000774 bool AllOk = true;
Alkis Evlogimenosd8bace72004-02-25 21:55:45 +0000775 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
776 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattner80cbed42004-02-09 02:12:04 +0000777 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000778 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattner80cbed42004-02-09 02:12:04 +0000779 AllOk = false;
780 }
781 assert(AllOk && "Virtual registers still in phys regs?");
782#endif
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000783
Chris Lattner931947d2003-08-17 18:01:15 +0000784 // Clear any physical register which appear live at the end of the basic
785 // block, but which do not hold any virtual registers. e.g., the stack
786 // pointer.
787 PhysRegsUseOrder.clear();
Chris Lattner101b8cd2002-12-16 16:15:28 +0000788}
789
Chris Lattner0ea32b82002-12-17 03:16:10 +0000790
Chris Lattner101b8cd2002-12-16 16:15:28 +0000791/// runOnMachineFunction - Register allocate the whole function
792///
Bill Wendling31fd60b2007-05-08 19:02:46 +0000793bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendling9d46fcd2006-11-17 02:09:07 +0000794 DOUT << "Machine Function " << "\n";
Chris Lattner101b8cd2002-12-16 16:15:28 +0000795 MF = &Fn;
Chris Lattnerb4e41112002-12-28 20:40:43 +0000796 TM = &Fn.getTarget();
797 RegInfo = TM->getRegisterInfo();
Chris Lattner3d894dd2004-02-17 17:49:10 +0000798 LV = &getAnalysis<LiveVariables>();
Chris Lattner101b8cd2002-12-16 16:15:28 +0000799
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000800 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000801
802 // At various places we want to efficiently check to see whether a register
803 // is allocatable. To handle this, we mark all unallocatable registers as
804 // being pinned down, permanently.
805 {
Evan Cheng14edd192007-02-15 05:59:24 +0000806 BitVector Allocable = RegInfo->getAllocatableSet(Fn);
Chris Lattner9b1a6eb2006-09-08 19:03:30 +0000807 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
808 if (!Allocable[i])
809 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
810 }
Chris Lattner490627a2004-02-09 01:26:13 +0000811
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000812 // initialize the virtual->physical register map to have a 'null'
813 // mapping for all virtual registers
Alkis Evlogimenosd8bace72004-02-25 21:55:45 +0000814 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattner80cbed42004-02-09 02:12:04 +0000815
Chris Lattner101b8cd2002-12-16 16:15:28 +0000816 // Loop over all of the basic blocks, eliminating virtual register references
817 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
818 MBB != MBBe; ++MBB)
819 AllocateBasicBlock(*MBB);
820
Chris Lattnerb4e41112002-12-28 20:40:43 +0000821 StackSlotForVirtReg.clear();
Alkis Evlogimenosde6a3812004-02-13 18:20:47 +0000822 PhysRegsUsed.clear();
Chris Lattnerbfa53192003-01-13 00:25:40 +0000823 VirtRegModified.clear();
Chris Lattner80cbed42004-02-09 02:12:04 +0000824 Virt2PhysRegMap.clear();
Chris Lattner101b8cd2002-12-16 16:15:28 +0000825 return true;
826}
827
Chris Lattnerc330b982004-01-31 21:27:19 +0000828FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendling31fd60b2007-05-08 19:02:46 +0000829 return new RALocal();
Chris Lattner101b8cd2002-12-16 16:15:28 +0000830}