blob: 6090b4db13e94e56c3696d62f72a96a857de263d [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/DenseMap.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 Lattnerb74e83c2002-12-16 16:15:28 +000035namespace {
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000036 static Statistic<> NumStores("ra-local", "Number of stores added");
37 static Statistic<> NumLoads ("ra-local", "Number of loads added");
Andrew Lenharthae6153f2006-07-20 17:43:27 +000038 static Statistic<> NumFolded("ra-local", "Number of loads/stores folded "
39 "into instructions");
Jim Laskey13ec7022006-08-01 14:21:23 +000040
41 static RegisterRegAlloc
42 localRegAlloc("local", " local register allocator",
43 createLocalRegisterAllocator);
44
45
Chris Lattner95255282006-06-28 23:17:24 +000046 class VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattner580f9be2002-12-28 20:40:43 +000047 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000048 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000049 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000050 LiveVariables *LV;
Chris Lattner0648b162005-01-23 22:51:56 +000051 bool *PhysRegsEverUsed;
Chris Lattnerff863ba2002-12-25 05:05:46 +000052
Chris Lattnerb8822ad2003-08-04 23:36:39 +000053 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
54 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000055 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000056
57 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000058 // that is currently available in a physical register.
59 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000060
61 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000062 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000063 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000064
Chris Lattner64667b62004-02-09 01:26:13 +000065 // PhysRegsUsed - This array is effectively a map, containing entries for
66 // each physical register that currently has a value (ie, it is in
67 // Virt2PhysRegMap). The value mapped to is the virtual register
68 // corresponding to the physical register (the inverse of the
69 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000070 // because it is used by a future instruction, and to -2 if it is not
71 // allocatable. If the entry for a physical register is -1, then the
72 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000073 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000074 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000075
76 // PhysRegsUseOrder - This contains a list of the physical registers that
77 // currently have a virtual register value in them. This list provides an
78 // ordering of registers, imposing a reallocation order. This list is only
79 // used if all registers are allocated and we have to spill one, in which
80 // case we spill the least recently used register. Entries at the front of
81 // the list are the least recently used registers, entries at the back are
82 // the most recently used.
83 //
84 std::vector<unsigned> PhysRegsUseOrder;
85
Chris Lattner91a452b2003-01-13 00:25:40 +000086 // VirtRegModified - This bitset contains information about which virtual
87 // registers need to be spilled back to memory when their registers are
88 // scavenged. If a virtual register has simply been rematerialized, there
89 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000090 //
Chris Lattner91a452b2003-01-13 00:25:40 +000091 std::vector<bool> VirtRegModified;
92
93 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000094 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000095 Reg -= MRegisterInfo::FirstVirtualRegister;
96 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
97 VirtRegModified[Reg] = Val;
98 }
99
100 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +0000101 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000102 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000103 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000104 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
105 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000106
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000107 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000108 if (PhysRegsUseOrder.empty() ||
109 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000110
111 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000112 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
113 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
114 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
115 // Add it to the end of the list
116 PhysRegsUseOrder.push_back(RegMatch);
117 if (RegMatch == Reg)
118 return; // Found an exact match, exit early
119 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000120 }
121
122 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000123 virtual const char *getPassName() const {
124 return "Local Register Allocator";
125 }
126
Chris Lattner91a452b2003-01-13 00:25:40 +0000127 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000128 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000129 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000130 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000131 MachineFunctionPass::getAnalysisUsage(AU);
132 }
133
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000134 private:
135 /// runOnMachineFunction - Register allocate the whole function
136 bool runOnMachineFunction(MachineFunction &Fn);
137
138 /// AllocateBasicBlock - Register allocate the specified basic block.
139 void AllocateBasicBlock(MachineBasicBlock &MBB);
140
Chris Lattner82bee0f2002-12-18 08:14:26 +0000141
Chris Lattner82bee0f2002-12-18 08:14:26 +0000142 /// areRegsEqual - This method returns true if the specified registers are
143 /// related to each other. To do this, it checks to see if they are equal
144 /// or if the first register is in the alias set of the second register.
145 ///
146 bool areRegsEqual(unsigned R1, unsigned R2) const {
147 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000148 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
149 *AliasSet; ++AliasSet) {
150 if (*AliasSet == R1) return true;
151 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000152 return false;
153 }
154
Chris Lattner580f9be2002-12-28 20:40:43 +0000155 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000156 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000157 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000158
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000159 /// removePhysReg - This method marks the specified physical register as no
160 /// longer being in use.
161 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000162 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000163
164 /// spillVirtReg - This method spills the value specified by PhysReg into
165 /// the virtual register slot specified by VirtReg. It then updates the RA
166 /// data structures to indicate the fact that PhysReg is now available.
167 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000168 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000169 unsigned VirtReg, unsigned PhysReg);
170
Chris Lattnerc21be922002-12-16 17:44:42 +0000171 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000172 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
173 /// true, then the request is ignored if the physical register does not
174 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000175 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000176 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000177 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000178
Chris Lattner91a452b2003-01-13 00:25:40 +0000179 /// assignVirtToPhysReg - This method updates local state so that we know
180 /// that PhysReg is the proper container for VirtReg now. The physical
181 /// register must not be used for anything else when this is called.
182 ///
183 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
184
185 /// liberatePhysReg - Make sure the specified physical register is available
186 /// for use. If there is currently a value in it, it is either moved out of
187 /// the way or spilled to memory.
188 ///
189 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000190 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000191
Chris Lattnerae640432002-12-17 02:50:10 +0000192 /// isPhysRegAvailable - Return true if the specified physical register is
193 /// free and available for use. This also includes checking to see if
194 /// aliased registers are all free...
195 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000196 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000197
198 /// getFreeReg - Look to see if there is a free register available in the
199 /// specified register class. If not, return 0.
200 ///
201 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000202
Chris Lattner91a452b2003-01-13 00:25:40 +0000203 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000204 /// register. If all compatible physical registers are used, this method
205 /// spills the last used virtual register to the stack, and uses that
206 /// register.
207 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000208 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000209 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000210
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000211 /// reloadVirtReg - This method transforms the specified specified virtual
212 /// register use to refer to a physical register. This method may do this
213 /// in one of several ways: if the register is available in a physical
214 /// register already, it uses that physical register. If the value is not
215 /// in a physical register, and if there are physical registers available,
216 /// it loads it into a register. If register pressure is high, and it is
217 /// possible, it tries to fold the load of the virtual register into the
218 /// instruction itself. It avoids doing this if register pressure is low to
219 /// improve the chance that subsequent instructions can use the reloaded
220 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000221 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000222 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
223 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000224
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000225
226 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
227 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000228 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000229}
230
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000231/// getStackSpaceFor - This allocates space for the specified virtual register
232/// to be held on the stack.
233int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
234 // Find the location Reg would belong...
235 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000236
Chris Lattner580f9be2002-12-28 20:40:43 +0000237 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000238 return I->second; // Already has space allocated?
239
Chris Lattner580f9be2002-12-28 20:40:43 +0000240 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000241 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
242 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000243
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000244 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000245 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
246 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000247}
248
Chris Lattnerae640432002-12-17 02:50:10 +0000249
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000250/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000251/// longer being in use.
252///
253void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000254 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000255
256 std::vector<unsigned>::iterator It =
257 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000258 if (It != PhysRegsUseOrder.end())
259 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000260}
261
Chris Lattner91a452b2003-01-13 00:25:40 +0000262
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000263/// spillVirtReg - This method spills the value specified by PhysReg into the
264/// virtual register slot specified by VirtReg. It then updates the RA data
265/// structures to indicate the fact that PhysReg is now available.
266///
Chris Lattner688c8252004-02-22 19:08:15 +0000267void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000268 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000269 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000270 " Must not have appropriate kill for the register or use exists beyond"
271 " the intended one.");
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000272 DOUT << " Spilling register " << RegInfo->getName(PhysReg)
273 << " containing %reg" << VirtReg;
274 if (!isVirtRegModified(VirtReg))
275 DOUT << " which has not been modified, so no store necessary!";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000276
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000277 // Otherwise, there is a virtual register corresponding to this physical
278 // register. We only need to spill it into its stack slot if it has been
279 // modified.
280 if (isVirtRegModified(VirtReg)) {
281 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
282 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000283 DOUT << " to stack slot #" << FrameIndex;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000284 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000285 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000286 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000287
288 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000289
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000290 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000291 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000292}
293
Chris Lattnerae640432002-12-17 02:50:10 +0000294
Chris Lattner91a452b2003-01-13 00:25:40 +0000295/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000296/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
297/// then the request is ignored if the physical register does not contain a
298/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000299///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000300void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000301 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000302 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000303 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000304 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
305 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000306 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000307 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000308 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000309 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000310 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000311 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
312 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Chengddee8422006-11-15 20:55:15 +0000313 if (PhysRegsUsed[*AliasSet] == 0) {
314 // This must have been a dead def due to something like this:
315 // %EAX :=
316 // := op %AL
317 // No more use of %EAX, %AH, etc.
318 // %EAX isn't dead upon definition, but %AH is. However %AH isn't
319 // an operand of definition MI so it's not marked as such.
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000320 DOUT << " Register " << RegInfo->getName(*AliasSet)
321 << " [%reg" << *AliasSet
322 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000323 removePhysReg(*AliasSet);
324 } else
Chris Lattner64667b62004-02-09 01:26:13 +0000325 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000326 }
327}
328
329
330/// assignVirtToPhysReg - This method updates local state so that we know
331/// that PhysReg is the proper container for VirtReg now. The physical
332/// register must not be used for anything else when this is called.
333///
334void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000335 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000336 // Update information to note the fact that this register was just used, and
337 // it holds VirtReg.
338 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000339 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000340 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
341}
342
343
Chris Lattnerae640432002-12-17 02:50:10 +0000344/// isPhysRegAvailable - Return true if the specified physical register is free
345/// and available for use. This also includes checking to see if aliased
346/// registers are all free...
347///
348bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000349 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000350
351 // If the selected register aliases any other allocated registers, it is
352 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000353 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
354 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000355 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000356 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000357 return true;
358}
359
360
Chris Lattner91a452b2003-01-13 00:25:40 +0000361/// getFreeReg - Look to see if there is a free register available in the
362/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000363///
Chris Lattner91a452b2003-01-13 00:25:40 +0000364unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000365 // Get iterators defining the range of registers that are valid to allocate in
366 // this class, which also specifies the preferred allocation order.
367 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
368 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000369
Chris Lattner91a452b2003-01-13 00:25:40 +0000370 for (; RI != RE; ++RI)
371 if (isPhysRegAvailable(*RI)) { // Is reg unused?
372 assert(*RI != 0 && "Cannot use register!");
373 return *RI; // Found an unused register!
374 }
375 return 0;
376}
377
378
379/// liberatePhysReg - Make sure the specified physical register is available for
380/// use. If there is currently a value in it, it is either moved out of the way
381/// or spilled to memory.
382///
383void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000384 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000385 spillPhysReg(MBB, I, PhysReg);
386}
387
388
389/// getReg - Find a physical register to hold the specified virtual
390/// register. If all compatible physical registers are used, this method spills
391/// the last used virtual register to the stack, and uses that register.
392///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000393unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000394 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000395 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
396
397 // First check to see if we have a free register of the requested type...
398 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000399
Chris Lattnerae640432002-12-17 02:50:10 +0000400 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000401 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000402 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000403
404 // Loop over all of the preallocated registers from the least recently used
405 // to the most recently used. When we find one that is capable of holding
406 // our register, use it.
407 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000408 assert(i != PhysRegsUseOrder.size() &&
409 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000410
Chris Lattnerae640432002-12-17 02:50:10 +0000411 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000412
413 // We can only use this register if it holds a virtual register (ie, it
414 // can be spilled). Do not use it if it is an explicitly allocated
415 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000416 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000417 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000418 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000419 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000420 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000421 PhysReg = R;
422 break;
423 } else {
424 // If one of the registers aliased to the current register is
425 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000426 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
427 *AliasIt; ++AliasIt) {
428 if (RC->contains(*AliasIt) &&
429 // If this is pinned down for some reason, don't use it. For
430 // example, if CL is pinned, and we run across CH, don't use
431 // CH as justification for using scavenging ECX (which will
432 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000433 PhysRegsUsed[*AliasIt] != 0 &&
434
435 // Make sure the register is allocatable. Don't allocate SIL on
436 // x86-32.
437 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000438 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000439 break;
440 }
441 }
Chris Lattner41822c72003-08-23 23:49:42 +0000442 }
Chris Lattnerae640432002-12-17 02:50:10 +0000443 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000444 }
445
Chris Lattnerae640432002-12-17 02:50:10 +0000446 assert(PhysReg && "Physical register not assigned!?!?");
447
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000448 // At this point PhysRegsUseOrder[i] is the least recently used register of
449 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000450 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000451 }
452
453 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000454 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000455 return PhysReg;
456}
457
Chris Lattnerae640432002-12-17 02:50:10 +0000458
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000459/// reloadVirtReg - This method transforms the specified specified virtual
460/// register use to refer to a physical register. This method may do this in
461/// one of several ways: if the register is available in a physical register
462/// already, it uses that physical register. If the value is not in a physical
463/// register, and if there are physical registers available, it loads it into a
464/// register. If register pressure is high, and it is possible, it tries to
465/// fold the load of the virtual register into the instruction itself. It
466/// avoids doing this if register pressure is low to improve the chance that
467/// subsequent instructions can use the reloaded value. This method returns the
468/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000469///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000470MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
471 unsigned OpNum) {
472 unsigned VirtReg = MI->getOperand(OpNum).getReg();
473
474 // If the virtual register is already available, just update the instruction
475 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000476 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000477 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000478 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000479 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000480 }
481
Chris Lattner1e3812c2004-02-17 04:08:37 +0000482 // Otherwise, we need to fold it into the current instruction, or reload it.
483 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000484 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000485 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000486 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000487
Chris Lattner11390e72004-02-17 08:09:40 +0000488 if (PhysReg) { // Register is available, allocate it!
489 assignVirtToPhysReg(VirtReg, PhysReg);
490 } else { // No registers available.
491 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000492 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000493 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000494 // Since we changed the address of MI, make sure to update live variables
495 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000496 LV->instructionChanged(MI, FMI);
497 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000498 }
499
500 // It looks like we can't fold this virtual register load into this
501 // instruction. Force some poor hapless value out of the register file to
502 // make room for the new register, and reload it.
503 PhysReg = getReg(MBB, MI, VirtReg);
504 }
505
Chris Lattner91a452b2003-01-13 00:25:40 +0000506 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
507
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000508 DOUT << " Reloading %reg" << VirtReg << " into "
509 << RegInfo->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000510
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000511 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000512 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000513 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000514
Chris Lattner0648b162005-01-23 22:51:56 +0000515 PhysRegsEverUsed[PhysReg] = true;
Chris Lattnere53f4a02006-05-04 17:52:23 +0000516 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000517 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000518}
519
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000520
521
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000522void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
523 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000524 MachineBasicBlock::iterator MII = MBB.begin();
525 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000526
Evan Chengddee8422006-11-15 20:55:15 +0000527 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000528 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Chengddee8422006-11-15 20:55:15 +0000529
Chris Lattner44500e32006-06-15 22:21:53 +0000530 // If this is the first basic block in the machine function, add live-in
531 // registers as active.
532 if (&MBB == &*MF->begin()) {
533 for (MachineFunction::livein_iterator I = MF->livein_begin(),
534 E = MF->livein_end(); I != E; ++I) {
535 unsigned Reg = I->first;
536 PhysRegsEverUsed[Reg] = true;
537 PhysRegsUsed[Reg] = 0; // It is free and reserved now
538 PhysRegsUseOrder.push_back(Reg);
539 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
540 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000541 if (PhysRegsUsed[*AliasSet] != -2) {
542 PhysRegsUseOrder.push_back(*AliasSet);
543 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
544 PhysRegsEverUsed[*AliasSet] = true;
545 }
Chris Lattner44500e32006-06-15 22:21:53 +0000546 }
547 }
548 }
549
550 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000551 while (MII != MBB.end()) {
552 MachineInstr *MI = MII++;
553 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000554 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
555 DOUT << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000556 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000557 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000558 DOUT << "[" << RegInfo->getName(i)
559 << ",%reg" << PhysRegsUsed[i] << "] ";
560 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000561
Chris Lattnerae640432002-12-17 02:50:10 +0000562 // Loop over the implicit uses, making sure that they are at the head of the
563 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000564 if (TID.ImplicitUses) {
565 for (const unsigned *ImplicitUses = TID.ImplicitUses;
566 *ImplicitUses; ++ImplicitUses)
567 MarkPhysRegRecentlyUsed(*ImplicitUses);
568 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000569
Evan Chengddee8422006-11-15 20:55:15 +0000570 SmallVector<unsigned, 8> Kills;
571 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
572 MachineOperand& MO = MI->getOperand(i);
573 if (MO.isRegister() && MO.isKill())
574 Kills.push_back(MO.getReg());
575 }
576
Brian Gaeke53b99a02003-08-15 21:19:25 +0000577 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000578 // incoming values if we are out of registers. Note that we completely
579 // ignore physical register uses here. We assume that if an explicit
580 // physical register is referenced by the instruction, that it is guaranteed
581 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000582 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000583 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
584 MachineOperand& MO = MI->getOperand(i);
585 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000586 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000587 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000588 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000589 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000590
Evan Chengddee8422006-11-15 20:55:15 +0000591 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000592 // value, freeing the register being used, so it doesn't need to be
593 // spilled to memory.
594 //
Evan Chengddee8422006-11-15 20:55:15 +0000595 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
596 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000597 unsigned PhysReg = VirtReg;
598 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
599 // If the virtual register was never materialized into a register, it
600 // might not be in the map, but it won't hurt to zero it out anyway.
601 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
602 PhysReg = PhysRegSlot;
603 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000604 } else if (PhysRegsUsed[PhysReg] == -2) {
605 // Unallocatable register dead, ignore.
606 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000607 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000608
Chris Lattner56ddada2004-02-17 17:49:10 +0000609 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000610 DOUT << " Last use of " << RegInfo->getName(PhysReg)
611 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000612 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000613 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
614 *AliasSet; ++AliasSet) {
615 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000616 DOUT << " Last use of "
Evan Chengddee8422006-11-15 20:55:15 +0000617 << RegInfo->getName(*AliasSet)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000618 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000619 removePhysReg(*AliasSet);
620 }
621 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000622 }
623 }
624
625 // Loop over all of the operands of the instruction, spilling registers that
626 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000627 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
628 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000629 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000630 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
631 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000632 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
633
Chris Lattner0648b162005-01-23 22:51:56 +0000634 PhysRegsEverUsed[Reg] = true;
Evan Chengddee8422006-11-15 20:55:15 +0000635 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000636 PhysRegsUsed[Reg] = 0; // It is free and reserved now
637 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000638 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
639 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000640 if (PhysRegsUsed[*AliasSet] != -2) {
641 PhysRegsUseOrder.push_back(*AliasSet);
642 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
643 PhysRegsEverUsed[*AliasSet] = true;
644 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000645 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000646 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000647 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000648
649 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000650 if (TID.ImplicitDefs) {
651 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
652 *ImplicitDefs; ++ImplicitDefs) {
653 unsigned Reg = *ImplicitDefs;
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000654 bool IsNonAllocatable = PhysRegsUsed[Reg] == -2;
655 if (!IsNonAllocatable) {
656 spillPhysReg(MBB, MI, Reg, true);
657 PhysRegsUseOrder.push_back(Reg);
658 PhysRegsUsed[Reg] = 0; // It is free and reserved now
659 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000660 PhysRegsEverUsed[Reg] = true;
Chris Lattner0648b162005-01-23 22:51:56 +0000661
Jim Laskeycd4317e2006-07-21 21:15:20 +0000662 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
663 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000664 if (PhysRegsUsed[*AliasSet] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000665 if (!IsNonAllocatable) {
666 PhysRegsUseOrder.push_back(*AliasSet);
667 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
668 }
Chris Lattner45d57882006-09-08 19:03:30 +0000669 PhysRegsEverUsed[*AliasSet] = true;
670 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000671 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000672 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000673 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000674
Evan Chengddee8422006-11-15 20:55:15 +0000675 SmallVector<unsigned, 8> DeadDefs;
676 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
677 MachineOperand& MO = MI->getOperand(i);
678 if (MO.isRegister() && MO.isDead())
679 DeadDefs.push_back(MO.getReg());
680 }
681
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000682 // Okay, we have allocated all of the source operands and spilled any values
683 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000684 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000685 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000686 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000687 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
688 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000689 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000690 MRegisterInfo::isVirtualRegister(MO.getReg())) {
691 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000692 unsigned DestPhysReg;
693
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000694 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000695 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000696 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattner0648b162005-01-23 22:51:56 +0000697 PhysRegsEverUsed[DestPhysReg] = true;
Chris Lattnerd5725632003-05-12 03:54:14 +0000698 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000699 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000700 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000701 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000702
Chris Lattner56ddada2004-02-17 17:49:10 +0000703 // If this instruction defines any registers that are immediately dead,
704 // kill them now.
705 //
Evan Chengddee8422006-11-15 20:55:15 +0000706 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
707 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000708 unsigned PhysReg = VirtReg;
709 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
710 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
711 PhysReg = PhysRegSlot;
712 assert(PhysReg != 0);
713 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000714 } else if (PhysRegsUsed[PhysReg] == -2) {
715 // Unallocatable register dead, ignore.
716 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000717 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000718
Chris Lattner56ddada2004-02-17 17:49:10 +0000719 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000720 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000721 << " [%reg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000722 << "] is never used, removing it frame live list\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000723 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000724 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
725 *AliasSet; ++AliasSet) {
726 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000727 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000728 << " [%reg" << *AliasSet
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000729 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000730 removePhysReg(*AliasSet);
731 }
732 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000733 }
734 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000735
736 // Finally, if this is a noop copy instruction, zap it.
737 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000738 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
739 LV->removeVirtualRegistersKilled(MI);
740 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000741 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000742 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000743 }
744
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000745 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000746
747 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000748 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000749 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000750 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000751 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000752 else
753 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000754
Chris Lattner9a5ef202005-11-09 05:28:45 +0000755#if 0
756 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000757 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000758 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
759 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000760 if (unsigned PR = Virt2PhysRegMap[i]) {
761 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
762 AllOk = false;
763 }
764 assert(AllOk && "Virtual registers still in phys regs?");
765#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000766
Chris Lattner128c2aa2003-08-17 18:01:15 +0000767 // Clear any physical register which appear live at the end of the basic
768 // block, but which do not hold any virtual registers. e.g., the stack
769 // pointer.
770 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000771}
772
Chris Lattner86c69a62002-12-17 03:16:10 +0000773
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000774/// runOnMachineFunction - Register allocate the whole function
775///
776bool RA::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000777 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000778 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000779 TM = &Fn.getTarget();
780 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000781 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000782
Chris Lattner0648b162005-01-23 22:51:56 +0000783 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
784 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
785 Fn.setUsedPhysRegs(PhysRegsEverUsed);
786
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000787 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +0000788
789 // At various places we want to efficiently check to see whether a register
790 // is allocatable. To handle this, we mark all unallocatable registers as
791 // being pinned down, permanently.
792 {
793 std::vector<bool> Allocable = RegInfo->getAllocatableSet(Fn);
794 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
795 if (!Allocable[i])
796 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
797 }
Chris Lattner64667b62004-02-09 01:26:13 +0000798
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000799 // initialize the virtual->physical register map to have a 'null'
800 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000801 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000802
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000803 // Loop over all of the basic blocks, eliminating virtual register references
804 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
805 MBB != MBBe; ++MBB)
806 AllocateBasicBlock(*MBB);
807
Chris Lattner580f9be2002-12-28 20:40:43 +0000808 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000809 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000810 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000811 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000812 return true;
813}
814
Chris Lattneref09c632004-01-31 21:27:19 +0000815FunctionPass *llvm::createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000816 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000817}