blob: ca4962e339d7d4d0855cdac8c40c5509b46b121f [file] [log] [blame]
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb74e83c2002-12-16 16:15:28 +00009//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4cc662b2003-08-03 21:47:31 +000015#define DEBUG_TYPE "regalloc"
Evan Chengddee8422006-11-15 20:55:15 +000016#include "llvm/BasicBlock.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000017#include "llvm/CodeGen/Passes.h"
Chris Lattner580f9be2002-12-28 20:40:43 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000019#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerff863ba2002-12-25 05:05:46 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000022#include "llvm/CodeGen/LiveVariables.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000023#include "llvm/CodeGen/RegAllocRegistry.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000024#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000025#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000029#include "llvm/ADT/IndexedMap.h"
Evan Chengddee8422006-11-15 20:55:15 +000030#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattner27f29162004-10-26 15:35:58 +000032#include <algorithm>
Chris Lattneref09c632004-01-31 21:27:19 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NumStores, "Number of stores added");
36STATISTIC(NumLoads , "Number of loads added");
37STATISTIC(NumFolded, "Number of loads/stores folded into instructions");
Jim Laskey13ec7022006-08-01 14:21:23 +000038
Chris Lattnercd3245a2006-12-19 22:41:21 +000039namespace {
Jim Laskey13ec7022006-08-01 14:21:23 +000040 static RegisterRegAlloc
41 localRegAlloc("local", " local register allocator",
42 createLocalRegisterAllocator);
43
44
Chris Lattner95255282006-06-28 23:17:24 +000045 class VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000046 public:
Devang Patel19974732007-05-03 01:11:54 +000047 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000048 RA() : MachineFunctionPass((intptr_t)&ID) {}
49 private:
Chris Lattner580f9be2002-12-28 20:40:43 +000050 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000051 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000052 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000053 LiveVariables *LV;
Chris Lattnerff863ba2002-12-25 05:05:46 +000054
Chris Lattnerb8822ad2003-08-04 23:36:39 +000055 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
56 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000057 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000058
59 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000060 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000061 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000062
63 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000064 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000065 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000066
Chris Lattner64667b62004-02-09 01:26:13 +000067 // PhysRegsUsed - This array is effectively a map, containing entries for
68 // each physical register that currently has a value (ie, it is in
69 // Virt2PhysRegMap). The value mapped to is the virtual register
70 // corresponding to the physical register (the inverse of the
71 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000072 // because it is used by a future instruction, and to -2 if it is not
73 // allocatable. If the entry for a physical register is -1, then the
74 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000075 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000076 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000077
78 // PhysRegsUseOrder - This contains a list of the physical registers that
79 // currently have a virtual register value in them. This list provides an
80 // ordering of registers, imposing a reallocation order. This list is only
81 // used if all registers are allocated and we have to spill one, in which
82 // case we spill the least recently used register. Entries at the front of
83 // the list are the least recently used registers, entries at the back are
84 // the most recently used.
85 //
86 std::vector<unsigned> PhysRegsUseOrder;
87
Chris Lattner91a452b2003-01-13 00:25:40 +000088 // VirtRegModified - This bitset contains information about which virtual
89 // registers need to be spilled back to memory when their registers are
90 // scavenged. If a virtual register has simply been rematerialized, there
91 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000092 //
Chris Lattner91a452b2003-01-13 00:25:40 +000093 std::vector<bool> VirtRegModified;
94
95 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000096 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000097 Reg -= MRegisterInfo::FirstVirtualRegister;
98 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
99 VirtRegModified[Reg] = Val;
100 }
101
102 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +0000103 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000104 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000105 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000106 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
107 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000108
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000109 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000110 if (PhysRegsUseOrder.empty() ||
111 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000112
113 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000114 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
115 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
116 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
117 // Add it to the end of the list
118 PhysRegsUseOrder.push_back(RegMatch);
119 if (RegMatch == Reg)
120 return; // Found an exact match, exit early
121 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000122 }
123
124 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000125 virtual const char *getPassName() const {
126 return "Local Register Allocator";
127 }
128
Chris Lattner91a452b2003-01-13 00:25:40 +0000129 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000130 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000131 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000132 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000133 MachineFunctionPass::getAnalysisUsage(AU);
134 }
135
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000136 private:
137 /// runOnMachineFunction - Register allocate the whole function
138 bool runOnMachineFunction(MachineFunction &Fn);
139
140 /// AllocateBasicBlock - Register allocate the specified basic block.
141 void AllocateBasicBlock(MachineBasicBlock &MBB);
142
Chris Lattner82bee0f2002-12-18 08:14:26 +0000143
Chris Lattner82bee0f2002-12-18 08:14:26 +0000144 /// areRegsEqual - This method returns true if the specified registers are
145 /// related to each other. To do this, it checks to see if they are equal
146 /// or if the first register is in the alias set of the second register.
147 ///
148 bool areRegsEqual(unsigned R1, unsigned R2) const {
149 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000150 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
151 *AliasSet; ++AliasSet) {
152 if (*AliasSet == R1) return true;
153 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000154 return false;
155 }
156
Chris Lattner580f9be2002-12-28 20:40:43 +0000157 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000158 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000159 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000160
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000161 /// removePhysReg - This method marks the specified physical register as no
162 /// longer being in use.
163 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000164 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000165
166 /// spillVirtReg - This method spills the value specified by PhysReg into
167 /// the virtual register slot specified by VirtReg. It then updates the RA
168 /// data structures to indicate the fact that PhysReg is now available.
169 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000170 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000171 unsigned VirtReg, unsigned PhysReg);
172
Chris Lattnerc21be922002-12-16 17:44:42 +0000173 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000174 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
175 /// true, then the request is ignored if the physical register does not
176 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000177 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000178 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000179 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000180
Chris Lattner91a452b2003-01-13 00:25:40 +0000181 /// assignVirtToPhysReg - This method updates local state so that we know
182 /// that PhysReg is the proper container for VirtReg now. The physical
183 /// register must not be used for anything else when this is called.
184 ///
185 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
186
187 /// liberatePhysReg - Make sure the specified physical register is available
188 /// for use. If there is currently a value in it, it is either moved out of
189 /// the way or spilled to memory.
190 ///
191 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000192 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000193
Chris Lattnerae640432002-12-17 02:50:10 +0000194 /// isPhysRegAvailable - Return true if the specified physical register is
195 /// free and available for use. This also includes checking to see if
196 /// aliased registers are all free...
197 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000198 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000199
200 /// getFreeReg - Look to see if there is a free register available in the
201 /// specified register class. If not, return 0.
202 ///
203 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000204
Chris Lattner91a452b2003-01-13 00:25:40 +0000205 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000206 /// register. If all compatible physical registers are used, this method
207 /// spills the last used virtual register to the stack, and uses that
208 /// register.
209 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000210 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000211 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000212
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000213 /// reloadVirtReg - This method transforms the specified specified virtual
214 /// register use to refer to a physical register. This method may do this
215 /// in one of several ways: if the register is available in a physical
216 /// register already, it uses that physical register. If the value is not
217 /// in a physical register, and if there are physical registers available,
218 /// it loads it into a register. If register pressure is high, and it is
219 /// possible, it tries to fold the load of the virtual register into the
220 /// instruction itself. It avoids doing this if register pressure is low to
221 /// improve the chance that subsequent instructions can use the reloaded
222 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000223 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000224 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
225 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000226
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000227
228 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
229 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000230 };
Devang Patel19974732007-05-03 01:11:54 +0000231 char RA::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000232}
233
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000234/// getStackSpaceFor - This allocates space for the specified virtual register
235/// to be held on the stack.
236int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
237 // Find the location Reg would belong...
238 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000239
Chris Lattner580f9be2002-12-28 20:40:43 +0000240 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000241 return I->second; // Already has space allocated?
242
Chris Lattner580f9be2002-12-28 20:40:43 +0000243 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000244 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
245 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000246
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000247 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000248 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
249 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000250}
251
Chris Lattnerae640432002-12-17 02:50:10 +0000252
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000253/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000254/// longer being in use.
255///
256void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000257 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000258
259 std::vector<unsigned>::iterator It =
260 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000261 if (It != PhysRegsUseOrder.end())
262 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000263}
264
Chris Lattner91a452b2003-01-13 00:25:40 +0000265
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000266/// spillVirtReg - This method spills the value specified by PhysReg into the
267/// virtual register slot specified by VirtReg. It then updates the RA data
268/// structures to indicate the fact that PhysReg is now available.
269///
Chris Lattner688c8252004-02-22 19:08:15 +0000270void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000271 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000272 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000273 " Must not have appropriate kill for the register or use exists beyond"
274 " the intended one.");
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000275 DOUT << " Spilling register " << RegInfo->getName(PhysReg)
276 << " containing %reg" << VirtReg;
277 if (!isVirtRegModified(VirtReg))
278 DOUT << " which has not been modified, so no store necessary!";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000279
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000280 // Otherwise, there is a virtual register corresponding to this physical
281 // register. We only need to spill it into its stack slot if it has been
282 // modified.
283 if (isVirtRegModified(VirtReg)) {
284 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
285 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000286 DOUT << " to stack slot #" << FrameIndex;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000287 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000288 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000289 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000290
291 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000292
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000293 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000294 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000295}
296
Chris Lattnerae640432002-12-17 02:50:10 +0000297
Chris Lattner91a452b2003-01-13 00:25:40 +0000298/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000299/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
300/// then the request is ignored if the physical register does not contain a
301/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000302///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000303void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000304 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000305 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000306 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000307 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
308 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000309 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000310 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000311 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000312 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000313 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000314 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
315 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Chengddee8422006-11-15 20:55:15 +0000316 if (PhysRegsUsed[*AliasSet] == 0) {
317 // This must have been a dead def due to something like this:
318 // %EAX :=
319 // := op %AL
320 // No more use of %EAX, %AH, etc.
321 // %EAX isn't dead upon definition, but %AH is. However %AH isn't
322 // an operand of definition MI so it's not marked as such.
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000323 DOUT << " Register " << RegInfo->getName(*AliasSet)
324 << " [%reg" << *AliasSet
325 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000326 removePhysReg(*AliasSet);
327 } else
Chris Lattner64667b62004-02-09 01:26:13 +0000328 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000329 }
330}
331
332
333/// assignVirtToPhysReg - This method updates local state so that we know
334/// that PhysReg is the proper container for VirtReg now. The physical
335/// register must not be used for anything else when this is called.
336///
337void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000338 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000339 // Update information to note the fact that this register was just used, and
340 // it holds VirtReg.
341 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000342 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000343 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
344}
345
346
Chris Lattnerae640432002-12-17 02:50:10 +0000347/// isPhysRegAvailable - Return true if the specified physical register is free
348/// and available for use. This also includes checking to see if aliased
349/// registers are all free...
350///
351bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000352 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000353
354 // If the selected register aliases any other allocated registers, it is
355 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000356 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
357 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000358 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000359 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000360 return true;
361}
362
363
Chris Lattner91a452b2003-01-13 00:25:40 +0000364/// getFreeReg - Look to see if there is a free register available in the
365/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000366///
Chris Lattner91a452b2003-01-13 00:25:40 +0000367unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000368 // Get iterators defining the range of registers that are valid to allocate in
369 // this class, which also specifies the preferred allocation order.
370 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
371 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000372
Chris Lattner91a452b2003-01-13 00:25:40 +0000373 for (; RI != RE; ++RI)
374 if (isPhysRegAvailable(*RI)) { // Is reg unused?
375 assert(*RI != 0 && "Cannot use register!");
376 return *RI; // Found an unused register!
377 }
378 return 0;
379}
380
381
382/// liberatePhysReg - Make sure the specified physical register is available for
383/// use. If there is currently a value in it, it is either moved out of the way
384/// or spilled to memory.
385///
386void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000387 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000388 spillPhysReg(MBB, I, PhysReg);
389}
390
391
392/// getReg - Find a physical register to hold the specified virtual
393/// register. If all compatible physical registers are used, this method spills
394/// the last used virtual register to the stack, and uses that register.
395///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000396unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000397 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000398 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
399
400 // First check to see if we have a free register of the requested type...
401 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000402
Chris Lattnerae640432002-12-17 02:50:10 +0000403 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000404 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000405 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000406
407 // Loop over all of the preallocated registers from the least recently used
408 // to the most recently used. When we find one that is capable of holding
409 // our register, use it.
410 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000411 assert(i != PhysRegsUseOrder.size() &&
412 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000413
Chris Lattnerae640432002-12-17 02:50:10 +0000414 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000415
416 // We can only use this register if it holds a virtual register (ie, it
417 // can be spilled). Do not use it if it is an explicitly allocated
418 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000419 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000420 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000421 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000422 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000423 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000424 PhysReg = R;
425 break;
426 } else {
427 // If one of the registers aliased to the current register is
428 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000429 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
430 *AliasIt; ++AliasIt) {
431 if (RC->contains(*AliasIt) &&
432 // If this is pinned down for some reason, don't use it. For
433 // example, if CL is pinned, and we run across CH, don't use
434 // CH as justification for using scavenging ECX (which will
435 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000436 PhysRegsUsed[*AliasIt] != 0 &&
437
438 // Make sure the register is allocatable. Don't allocate SIL on
439 // x86-32.
440 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000441 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000442 break;
443 }
444 }
Chris Lattner41822c72003-08-23 23:49:42 +0000445 }
Chris Lattnerae640432002-12-17 02:50:10 +0000446 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000447 }
448
Chris Lattnerae640432002-12-17 02:50:10 +0000449 assert(PhysReg && "Physical register not assigned!?!?");
450
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000451 // At this point PhysRegsUseOrder[i] is the least recently used register of
452 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000453 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000454 }
455
456 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000457 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000458 return PhysReg;
459}
460
Chris Lattnerae640432002-12-17 02:50:10 +0000461
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000462/// reloadVirtReg - This method transforms the specified specified virtual
463/// register use to refer to a physical register. This method may do this in
464/// one of several ways: if the register is available in a physical register
465/// already, it uses that physical register. If the value is not in a physical
466/// register, and if there are physical registers available, it loads it into a
467/// register. If register pressure is high, and it is possible, it tries to
468/// fold the load of the virtual register into the instruction itself. It
469/// avoids doing this if register pressure is low to improve the chance that
470/// subsequent instructions can use the reloaded value. This method returns the
471/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000472///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000473MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
474 unsigned OpNum) {
475 unsigned VirtReg = MI->getOperand(OpNum).getReg();
476
477 // If the virtual register is already available, just update the instruction
478 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000479 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000480 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000481 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000482 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000483 }
484
Chris Lattner1e3812c2004-02-17 04:08:37 +0000485 // Otherwise, we need to fold it into the current instruction, or reload it.
486 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000487 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000488 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000489 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000490
Chris Lattner11390e72004-02-17 08:09:40 +0000491 if (PhysReg) { // Register is available, allocate it!
492 assignVirtToPhysReg(VirtReg, PhysReg);
493 } else { // No registers available.
494 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000495 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000496 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000497 // Since we changed the address of MI, make sure to update live variables
498 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000499 LV->instructionChanged(MI, FMI);
500 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000501 }
502
503 // It looks like we can't fold this virtual register load into this
504 // instruction. Force some poor hapless value out of the register file to
505 // make room for the new register, and reload it.
506 PhysReg = getReg(MBB, MI, VirtReg);
507 }
508
Chris Lattner91a452b2003-01-13 00:25:40 +0000509 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
510
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000511 DOUT << " Reloading %reg" << VirtReg << " into "
512 << RegInfo->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000513
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000514 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000515 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000516 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000517
Evan Cheng6c087e52007-04-25 22:13:27 +0000518 MF->setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000519 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000520 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000521}
522
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000523
524
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000525void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
526 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000527 MachineBasicBlock::iterator MII = MBB.begin();
528 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000529
Evan Chengddee8422006-11-15 20:55:15 +0000530 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000531 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Chengddee8422006-11-15 20:55:15 +0000532
Chris Lattner44500e32006-06-15 22:21:53 +0000533 // If this is the first basic block in the machine function, add live-in
534 // registers as active.
535 if (&MBB == &*MF->begin()) {
536 for (MachineFunction::livein_iterator I = MF->livein_begin(),
537 E = MF->livein_end(); I != E; ++I) {
538 unsigned Reg = I->first;
Evan Cheng6c087e52007-04-25 22:13:27 +0000539 MF->setPhysRegUsed(Reg);
Chris Lattner44500e32006-06-15 22:21:53 +0000540 PhysRegsUsed[Reg] = 0; // It is free and reserved now
541 PhysRegsUseOrder.push_back(Reg);
542 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
543 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000544 if (PhysRegsUsed[*AliasSet] != -2) {
545 PhysRegsUseOrder.push_back(*AliasSet);
546 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000547 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000548 }
Chris Lattner44500e32006-06-15 22:21:53 +0000549 }
550 }
551 }
552
553 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000554 while (MII != MBB.end()) {
555 MachineInstr *MI = MII++;
556 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000557 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
558 DOUT << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000559 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000560 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000561 DOUT << "[" << RegInfo->getName(i)
562 << ",%reg" << PhysRegsUsed[i] << "] ";
563 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000564
Chris Lattnerae640432002-12-17 02:50:10 +0000565 // Loop over the implicit uses, making sure that they are at the head of the
566 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000567 if (TID.ImplicitUses) {
568 for (const unsigned *ImplicitUses = TID.ImplicitUses;
569 *ImplicitUses; ++ImplicitUses)
570 MarkPhysRegRecentlyUsed(*ImplicitUses);
571 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000572
Evan Chengddee8422006-11-15 20:55:15 +0000573 SmallVector<unsigned, 8> Kills;
574 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
575 MachineOperand& MO = MI->getOperand(i);
576 if (MO.isRegister() && MO.isKill())
577 Kills.push_back(MO.getReg());
578 }
579
Brian Gaeke53b99a02003-08-15 21:19:25 +0000580 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000581 // incoming values if we are out of registers. Note that we completely
582 // ignore physical register uses here. We assume that if an explicit
583 // physical register is referenced by the instruction, that it is guaranteed
584 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000585 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000586 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
587 MachineOperand& MO = MI->getOperand(i);
588 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000589 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000590 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000591 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000592 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000593
Evan Chengddee8422006-11-15 20:55:15 +0000594 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000595 // value, freeing the register being used, so it doesn't need to be
596 // spilled to memory.
597 //
Evan Chengddee8422006-11-15 20:55:15 +0000598 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
599 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000600 unsigned PhysReg = VirtReg;
601 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
602 // If the virtual register was never materialized into a register, it
603 // might not be in the map, but it won't hurt to zero it out anyway.
604 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
605 PhysReg = PhysRegSlot;
606 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000607 } else if (PhysRegsUsed[PhysReg] == -2) {
608 // Unallocatable register dead, ignore.
609 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000610 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000611
Chris Lattner56ddada2004-02-17 17:49:10 +0000612 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000613 DOUT << " Last use of " << RegInfo->getName(PhysReg)
614 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000615 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000616 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
617 *AliasSet; ++AliasSet) {
618 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000619 DOUT << " Last use of "
Evan Chengddee8422006-11-15 20:55:15 +0000620 << RegInfo->getName(*AliasSet)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000621 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000622 removePhysReg(*AliasSet);
623 }
624 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000625 }
626 }
627
628 // Loop over all of the operands of the instruction, spilling registers that
629 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000630 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
631 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000632 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000633 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
634 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000635 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
636
Evan Cheng6c087e52007-04-25 22:13:27 +0000637 MF->setPhysRegUsed(Reg);
Evan Chengddee8422006-11-15 20:55:15 +0000638 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000639 PhysRegsUsed[Reg] = 0; // It is free and reserved now
640 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000641 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
642 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000643 if (PhysRegsUsed[*AliasSet] != -2) {
644 PhysRegsUseOrder.push_back(*AliasSet);
645 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Evan Cheng6c087e52007-04-25 22:13:27 +0000646 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000647 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000648 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000649 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000650 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000651
652 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000653 if (TID.ImplicitDefs) {
654 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
655 *ImplicitDefs; ++ImplicitDefs) {
656 unsigned Reg = *ImplicitDefs;
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000657 bool IsNonAllocatable = PhysRegsUsed[Reg] == -2;
658 if (!IsNonAllocatable) {
659 spillPhysReg(MBB, MI, Reg, true);
660 PhysRegsUseOrder.push_back(Reg);
661 PhysRegsUsed[Reg] = 0; // It is free and reserved now
662 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000663 MF->setPhysRegUsed(Reg);
Chris Lattner0648b162005-01-23 22:51:56 +0000664
Jim Laskeycd4317e2006-07-21 21:15:20 +0000665 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
666 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000667 if (PhysRegsUsed[*AliasSet] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000668 if (!IsNonAllocatable) {
669 PhysRegsUseOrder.push_back(*AliasSet);
670 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
671 }
Evan Cheng6c087e52007-04-25 22:13:27 +0000672 MF->setPhysRegUsed(*AliasSet);
Chris Lattner45d57882006-09-08 19:03:30 +0000673 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000674 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000675 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000676 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000677
Evan Chengddee8422006-11-15 20:55:15 +0000678 SmallVector<unsigned, 8> DeadDefs;
679 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
680 MachineOperand& MO = MI->getOperand(i);
681 if (MO.isRegister() && MO.isDead())
682 DeadDefs.push_back(MO.getReg());
683 }
684
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000685 // Okay, we have allocated all of the source operands and spilled any values
686 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000687 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000688 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000689 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000690 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
691 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000692 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000693 MRegisterInfo::isVirtualRegister(MO.getReg())) {
694 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000695 unsigned DestPhysReg;
696
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000697 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000698 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000699 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Evan Cheng6c087e52007-04-25 22:13:27 +0000700 MF->setPhysRegUsed(DestPhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000701 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000702 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000703 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000704 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000705
Chris Lattner56ddada2004-02-17 17:49:10 +0000706 // If this instruction defines any registers that are immediately dead,
707 // kill them now.
708 //
Evan Chengddee8422006-11-15 20:55:15 +0000709 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
710 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000711 unsigned PhysReg = VirtReg;
712 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
713 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
714 PhysReg = PhysRegSlot;
715 assert(PhysReg != 0);
716 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000717 } else if (PhysRegsUsed[PhysReg] == -2) {
718 // Unallocatable register dead, ignore.
719 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000720 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000721
Chris Lattner56ddada2004-02-17 17:49:10 +0000722 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000723 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000724 << " [%reg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000725 << "] is never used, removing it frame live list\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000726 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000727 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
728 *AliasSet; ++AliasSet) {
729 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000730 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000731 << " [%reg" << *AliasSet
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000732 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000733 removePhysReg(*AliasSet);
734 }
735 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000736 }
737 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000738
739 // Finally, if this is a noop copy instruction, zap it.
740 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000741 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
742 LV->removeVirtualRegistersKilled(MI);
743 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000744 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000745 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000746 }
747
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000748 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000749
750 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000751 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000752 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000753 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000754 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000755 else
756 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000757
Chris Lattner9a5ef202005-11-09 05:28:45 +0000758#if 0
759 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000760 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000761 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
762 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000763 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +0000764 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +0000765 AllOk = false;
766 }
767 assert(AllOk && "Virtual registers still in phys regs?");
768#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000769
Chris Lattner128c2aa2003-08-17 18:01:15 +0000770 // Clear any physical register which appear live at the end of the basic
771 // block, but which do not hold any virtual registers. e.g., the stack
772 // pointer.
773 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000774}
775
Chris Lattner86c69a62002-12-17 03:16:10 +0000776
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000777/// runOnMachineFunction - Register allocate the whole function
778///
779bool RA::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000780 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000781 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000782 TM = &Fn.getTarget();
783 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000784 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000785
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000786 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +0000787
788 // At various places we want to efficiently check to see whether a register
789 // is allocatable. To handle this, we mark all unallocatable registers as
790 // being pinned down, permanently.
791 {
Evan Cheng61de82d2007-02-15 05:59:24 +0000792 BitVector Allocable = RegInfo->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +0000793 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
794 if (!Allocable[i])
795 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
796 }
Chris Lattner64667b62004-02-09 01:26:13 +0000797
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000798 // initialize the virtual->physical register map to have a 'null'
799 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000800 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000801
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000802 // Loop over all of the basic blocks, eliminating virtual register references
803 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
804 MBB != MBBe; ++MBB)
805 AllocateBasicBlock(*MBB);
806
Chris Lattner580f9be2002-12-28 20:40:43 +0000807 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000808 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000809 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000810 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000811 return true;
812}
813
Chris Lattneref09c632004-01-31 21:27:19 +0000814FunctionPass *llvm::createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000815 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000816}