blob: f862023e8f88e9eff45e30039fefb7d0114ebfd7 [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 {
Chris Lattner580f9be2002-12-28 20:40:43 +000046 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000047 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000048 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000049 LiveVariables *LV;
Chris Lattner0648b162005-01-23 22:51:56 +000050 bool *PhysRegsEverUsed;
Chris Lattnerff863ba2002-12-25 05:05:46 +000051
Chris Lattnerb8822ad2003-08-04 23:36:39 +000052 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
53 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000054 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000055
56 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000057 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000058 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000059
60 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000061 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000062 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000063
Chris Lattner64667b62004-02-09 01:26:13 +000064 // PhysRegsUsed - This array is effectively a map, containing entries for
65 // each physical register that currently has a value (ie, it is in
66 // Virt2PhysRegMap). The value mapped to is the virtual register
67 // corresponding to the physical register (the inverse of the
68 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000069 // because it is used by a future instruction, and to -2 if it is not
70 // allocatable. If the entry for a physical register is -1, then the
71 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000072 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000073 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000074
75 // PhysRegsUseOrder - This contains a list of the physical registers that
76 // currently have a virtual register value in them. This list provides an
77 // ordering of registers, imposing a reallocation order. This list is only
78 // used if all registers are allocated and we have to spill one, in which
79 // case we spill the least recently used register. Entries at the front of
80 // the list are the least recently used registers, entries at the back are
81 // the most recently used.
82 //
83 std::vector<unsigned> PhysRegsUseOrder;
84
Chris Lattner91a452b2003-01-13 00:25:40 +000085 // VirtRegModified - This bitset contains information about which virtual
86 // registers need to be spilled back to memory when their registers are
87 // scavenged. If a virtual register has simply been rematerialized, there
88 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000089 //
Chris Lattner91a452b2003-01-13 00:25:40 +000090 std::vector<bool> VirtRegModified;
91
92 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000093 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000094 Reg -= MRegisterInfo::FirstVirtualRegister;
95 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
96 VirtRegModified[Reg] = Val;
97 }
98
99 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +0000100 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000101 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000102 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000103 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
104 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000105
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000106 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000107 if (PhysRegsUseOrder.empty() ||
108 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000109
110 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000111 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
112 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
113 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
114 // Add it to the end of the list
115 PhysRegsUseOrder.push_back(RegMatch);
116 if (RegMatch == Reg)
117 return; // Found an exact match, exit early
118 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000119 }
120
121 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000122 virtual const char *getPassName() const {
123 return "Local Register Allocator";
124 }
125
Chris Lattner91a452b2003-01-13 00:25:40 +0000126 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000127 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000128 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000129 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000130 MachineFunctionPass::getAnalysisUsage(AU);
131 }
132
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000133 private:
134 /// runOnMachineFunction - Register allocate the whole function
135 bool runOnMachineFunction(MachineFunction &Fn);
136
137 /// AllocateBasicBlock - Register allocate the specified basic block.
138 void AllocateBasicBlock(MachineBasicBlock &MBB);
139
Chris Lattner82bee0f2002-12-18 08:14:26 +0000140
Chris Lattner82bee0f2002-12-18 08:14:26 +0000141 /// areRegsEqual - This method returns true if the specified registers are
142 /// related to each other. To do this, it checks to see if they are equal
143 /// or if the first register is in the alias set of the second register.
144 ///
145 bool areRegsEqual(unsigned R1, unsigned R2) const {
146 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000147 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
148 *AliasSet; ++AliasSet) {
149 if (*AliasSet == R1) return true;
150 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000151 return false;
152 }
153
Chris Lattner580f9be2002-12-28 20:40:43 +0000154 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000155 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000156 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000157
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000158 /// removePhysReg - This method marks the specified physical register as no
159 /// longer being in use.
160 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000161 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000162
163 /// spillVirtReg - This method spills the value specified by PhysReg into
164 /// the virtual register slot specified by VirtReg. It then updates the RA
165 /// data structures to indicate the fact that PhysReg is now available.
166 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000167 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000168 unsigned VirtReg, unsigned PhysReg);
169
Chris Lattnerc21be922002-12-16 17:44:42 +0000170 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000171 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
172 /// true, then the request is ignored if the physical register does not
173 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000174 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000175 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000176 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000177
Chris Lattner91a452b2003-01-13 00:25:40 +0000178 /// assignVirtToPhysReg - This method updates local state so that we know
179 /// that PhysReg is the proper container for VirtReg now. The physical
180 /// register must not be used for anything else when this is called.
181 ///
182 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
183
184 /// liberatePhysReg - Make sure the specified physical register is available
185 /// for use. If there is currently a value in it, it is either moved out of
186 /// the way or spilled to memory.
187 ///
188 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000189 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000190
Chris Lattnerae640432002-12-17 02:50:10 +0000191 /// isPhysRegAvailable - Return true if the specified physical register is
192 /// free and available for use. This also includes checking to see if
193 /// aliased registers are all free...
194 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000195 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000196
197 /// getFreeReg - Look to see if there is a free register available in the
198 /// specified register class. If not, return 0.
199 ///
200 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000201
Chris Lattner91a452b2003-01-13 00:25:40 +0000202 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000203 /// register. If all compatible physical registers are used, this method
204 /// spills the last used virtual register to the stack, and uses that
205 /// register.
206 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000207 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000208 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000209
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000210 /// reloadVirtReg - This method transforms the specified specified virtual
211 /// register use to refer to a physical register. This method may do this
212 /// in one of several ways: if the register is available in a physical
213 /// register already, it uses that physical register. If the value is not
214 /// in a physical register, and if there are physical registers available,
215 /// it loads it into a register. If register pressure is high, and it is
216 /// possible, it tries to fold the load of the virtual register into the
217 /// instruction itself. It avoids doing this if register pressure is low to
218 /// improve the chance that subsequent instructions can use the reloaded
219 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000220 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000221 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
222 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000223
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000224
225 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
226 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000227 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000228}
229
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000230/// getStackSpaceFor - This allocates space for the specified virtual register
231/// to be held on the stack.
232int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
233 // Find the location Reg would belong...
234 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000235
Chris Lattner580f9be2002-12-28 20:40:43 +0000236 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000237 return I->second; // Already has space allocated?
238
Chris Lattner580f9be2002-12-28 20:40:43 +0000239 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000240 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
241 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000242
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000243 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000244 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
245 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000246}
247
Chris Lattnerae640432002-12-17 02:50:10 +0000248
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000249/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000250/// longer being in use.
251///
252void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000253 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000254
255 std::vector<unsigned>::iterator It =
256 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000257 if (It != PhysRegsUseOrder.end())
258 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000259}
260
Chris Lattner91a452b2003-01-13 00:25:40 +0000261
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000262/// spillVirtReg - This method spills the value specified by PhysReg into the
263/// virtual register slot specified by VirtReg. It then updates the RA data
264/// structures to indicate the fact that PhysReg is now available.
265///
Chris Lattner688c8252004-02-22 19:08:15 +0000266void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000267 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000268 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000269 " Must not have appropriate kill for the register or use exists beyond"
270 " the intended one.");
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000271 DOUT << " Spilling register " << RegInfo->getName(PhysReg)
272 << " containing %reg" << VirtReg;
273 if (!isVirtRegModified(VirtReg))
274 DOUT << " which has not been modified, so no store necessary!";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000275
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000276 // Otherwise, there is a virtual register corresponding to this physical
277 // register. We only need to spill it into its stack slot if it has been
278 // modified.
279 if (isVirtRegModified(VirtReg)) {
280 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
281 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000282 DOUT << " to stack slot #" << FrameIndex;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000283 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000284 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000285 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000286
287 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000288
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000289 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000290 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000291}
292
Chris Lattnerae640432002-12-17 02:50:10 +0000293
Chris Lattner91a452b2003-01-13 00:25:40 +0000294/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000295/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
296/// then the request is ignored if the physical register does not contain a
297/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000298///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000299void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000300 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000301 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000302 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000303 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
304 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000305 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000306 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000307 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000308 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000309 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000310 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
311 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Chengddee8422006-11-15 20:55:15 +0000312 if (PhysRegsUsed[*AliasSet] == 0) {
313 // This must have been a dead def due to something like this:
314 // %EAX :=
315 // := op %AL
316 // No more use of %EAX, %AH, etc.
317 // %EAX isn't dead upon definition, but %AH is. However %AH isn't
318 // an operand of definition MI so it's not marked as such.
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000319 DOUT << " Register " << RegInfo->getName(*AliasSet)
320 << " [%reg" << *AliasSet
321 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000322 removePhysReg(*AliasSet);
323 } else
Chris Lattner64667b62004-02-09 01:26:13 +0000324 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000325 }
326}
327
328
329/// assignVirtToPhysReg - This method updates local state so that we know
330/// that PhysReg is the proper container for VirtReg now. The physical
331/// register must not be used for anything else when this is called.
332///
333void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000334 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000335 // Update information to note the fact that this register was just used, and
336 // it holds VirtReg.
337 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000338 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000339 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
340}
341
342
Chris Lattnerae640432002-12-17 02:50:10 +0000343/// isPhysRegAvailable - Return true if the specified physical register is free
344/// and available for use. This also includes checking to see if aliased
345/// registers are all free...
346///
347bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000348 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000349
350 // If the selected register aliases any other allocated registers, it is
351 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000352 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
353 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000354 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000355 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000356 return true;
357}
358
359
Chris Lattner91a452b2003-01-13 00:25:40 +0000360/// getFreeReg - Look to see if there is a free register available in the
361/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000362///
Chris Lattner91a452b2003-01-13 00:25:40 +0000363unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000364 // Get iterators defining the range of registers that are valid to allocate in
365 // this class, which also specifies the preferred allocation order.
366 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
367 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000368
Chris Lattner91a452b2003-01-13 00:25:40 +0000369 for (; RI != RE; ++RI)
370 if (isPhysRegAvailable(*RI)) { // Is reg unused?
371 assert(*RI != 0 && "Cannot use register!");
372 return *RI; // Found an unused register!
373 }
374 return 0;
375}
376
377
378/// liberatePhysReg - Make sure the specified physical register is available for
379/// use. If there is currently a value in it, it is either moved out of the way
380/// or spilled to memory.
381///
382void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000383 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000384 spillPhysReg(MBB, I, PhysReg);
385}
386
387
388/// getReg - Find a physical register to hold the specified virtual
389/// register. If all compatible physical registers are used, this method spills
390/// the last used virtual register to the stack, and uses that register.
391///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000392unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000393 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000394 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
395
396 // First check to see if we have a free register of the requested type...
397 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000398
Chris Lattnerae640432002-12-17 02:50:10 +0000399 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000400 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000401 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000402
403 // Loop over all of the preallocated registers from the least recently used
404 // to the most recently used. When we find one that is capable of holding
405 // our register, use it.
406 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000407 assert(i != PhysRegsUseOrder.size() &&
408 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000409
Chris Lattnerae640432002-12-17 02:50:10 +0000410 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000411
412 // We can only use this register if it holds a virtual register (ie, it
413 // can be spilled). Do not use it if it is an explicitly allocated
414 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000415 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000416 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000417 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000418 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000419 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000420 PhysReg = R;
421 break;
422 } else {
423 // If one of the registers aliased to the current register is
424 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000425 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
426 *AliasIt; ++AliasIt) {
427 if (RC->contains(*AliasIt) &&
428 // If this is pinned down for some reason, don't use it. For
429 // example, if CL is pinned, and we run across CH, don't use
430 // CH as justification for using scavenging ECX (which will
431 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000432 PhysRegsUsed[*AliasIt] != 0 &&
433
434 // Make sure the register is allocatable. Don't allocate SIL on
435 // x86-32.
436 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000437 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000438 break;
439 }
440 }
Chris Lattner41822c72003-08-23 23:49:42 +0000441 }
Chris Lattnerae640432002-12-17 02:50:10 +0000442 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000443 }
444
Chris Lattnerae640432002-12-17 02:50:10 +0000445 assert(PhysReg && "Physical register not assigned!?!?");
446
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000447 // At this point PhysRegsUseOrder[i] is the least recently used register of
448 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000449 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000450 }
451
452 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000453 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000454 return PhysReg;
455}
456
Chris Lattnerae640432002-12-17 02:50:10 +0000457
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000458/// reloadVirtReg - This method transforms the specified specified virtual
459/// register use to refer to a physical register. This method may do this in
460/// one of several ways: if the register is available in a physical register
461/// already, it uses that physical register. If the value is not in a physical
462/// register, and if there are physical registers available, it loads it into a
463/// register. If register pressure is high, and it is possible, it tries to
464/// fold the load of the virtual register into the instruction itself. It
465/// avoids doing this if register pressure is low to improve the chance that
466/// subsequent instructions can use the reloaded value. This method returns the
467/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000468///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000469MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
470 unsigned OpNum) {
471 unsigned VirtReg = MI->getOperand(OpNum).getReg();
472
473 // If the virtual register is already available, just update the instruction
474 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000475 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000476 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000477 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000478 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000479 }
480
Chris Lattner1e3812c2004-02-17 04:08:37 +0000481 // Otherwise, we need to fold it into the current instruction, or reload it.
482 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000483 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000484 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000485 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000486
Chris Lattner11390e72004-02-17 08:09:40 +0000487 if (PhysReg) { // Register is available, allocate it!
488 assignVirtToPhysReg(VirtReg, PhysReg);
489 } else { // No registers available.
490 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000491 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000492 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000493 // Since we changed the address of MI, make sure to update live variables
494 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000495 LV->instructionChanged(MI, FMI);
496 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000497 }
498
499 // It looks like we can't fold this virtual register load into this
500 // instruction. Force some poor hapless value out of the register file to
501 // make room for the new register, and reload it.
502 PhysReg = getReg(MBB, MI, VirtReg);
503 }
504
Chris Lattner91a452b2003-01-13 00:25:40 +0000505 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
506
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000507 DOUT << " Reloading %reg" << VirtReg << " into "
508 << RegInfo->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000509
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000510 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000511 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000512 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000513
Chris Lattner0648b162005-01-23 22:51:56 +0000514 PhysRegsEverUsed[PhysReg] = true;
Chris Lattnere53f4a02006-05-04 17:52:23 +0000515 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000516 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000517}
518
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000519
520
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000521void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
522 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000523 MachineBasicBlock::iterator MII = MBB.begin();
524 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000525
Evan Chengddee8422006-11-15 20:55:15 +0000526 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000527 if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName());
Evan Chengddee8422006-11-15 20:55:15 +0000528
Chris Lattner44500e32006-06-15 22:21:53 +0000529 // If this is the first basic block in the machine function, add live-in
530 // registers as active.
531 if (&MBB == &*MF->begin()) {
532 for (MachineFunction::livein_iterator I = MF->livein_begin(),
533 E = MF->livein_end(); I != E; ++I) {
534 unsigned Reg = I->first;
535 PhysRegsEverUsed[Reg] = true;
536 PhysRegsUsed[Reg] = 0; // It is free and reserved now
537 PhysRegsUseOrder.push_back(Reg);
538 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
539 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000540 if (PhysRegsUsed[*AliasSet] != -2) {
541 PhysRegsUseOrder.push_back(*AliasSet);
542 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
543 PhysRegsEverUsed[*AliasSet] = true;
544 }
Chris Lattner44500e32006-06-15 22:21:53 +0000545 }
546 }
547 }
548
549 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000550 while (MII != MBB.end()) {
551 MachineInstr *MI = MII++;
552 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000553 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
554 DOUT << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000555 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000556 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000557 DOUT << "[" << RegInfo->getName(i)
558 << ",%reg" << PhysRegsUsed[i] << "] ";
559 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000560
Chris Lattnerae640432002-12-17 02:50:10 +0000561 // Loop over the implicit uses, making sure that they are at the head of the
562 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000563 if (TID.ImplicitUses) {
564 for (const unsigned *ImplicitUses = TID.ImplicitUses;
565 *ImplicitUses; ++ImplicitUses)
566 MarkPhysRegRecentlyUsed(*ImplicitUses);
567 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000568
Evan Chengddee8422006-11-15 20:55:15 +0000569 SmallVector<unsigned, 8> Kills;
570 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
571 MachineOperand& MO = MI->getOperand(i);
572 if (MO.isRegister() && MO.isKill())
573 Kills.push_back(MO.getReg());
574 }
575
Brian Gaeke53b99a02003-08-15 21:19:25 +0000576 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000577 // incoming values if we are out of registers. Note that we completely
578 // ignore physical register uses here. We assume that if an explicit
579 // physical register is referenced by the instruction, that it is guaranteed
580 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000581 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000582 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
583 MachineOperand& MO = MI->getOperand(i);
584 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000585 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000586 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000587 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000588 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000589
Evan Chengddee8422006-11-15 20:55:15 +0000590 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000591 // value, freeing the register being used, so it doesn't need to be
592 // spilled to memory.
593 //
Evan Chengddee8422006-11-15 20:55:15 +0000594 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
595 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000596 unsigned PhysReg = VirtReg;
597 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
598 // If the virtual register was never materialized into a register, it
599 // might not be in the map, but it won't hurt to zero it out anyway.
600 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
601 PhysReg = PhysRegSlot;
602 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000603 } else if (PhysRegsUsed[PhysReg] == -2) {
604 // Unallocatable register dead, ignore.
605 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000606 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000607
Chris Lattner56ddada2004-02-17 17:49:10 +0000608 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000609 DOUT << " Last use of " << RegInfo->getName(PhysReg)
610 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000611 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000612 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
613 *AliasSet; ++AliasSet) {
614 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000615 DOUT << " Last use of "
Evan Chengddee8422006-11-15 20:55:15 +0000616 << RegInfo->getName(*AliasSet)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000617 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000618 removePhysReg(*AliasSet);
619 }
620 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000621 }
622 }
623
624 // Loop over all of the operands of the instruction, spilling registers that
625 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000626 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
627 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000628 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000629 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
630 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000631 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
632
Chris Lattner0648b162005-01-23 22:51:56 +0000633 PhysRegsEverUsed[Reg] = true;
Evan Chengddee8422006-11-15 20:55:15 +0000634 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000635 PhysRegsUsed[Reg] = 0; // It is free and reserved now
636 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000637 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
638 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000639 if (PhysRegsUsed[*AliasSet] != -2) {
640 PhysRegsUseOrder.push_back(*AliasSet);
641 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
642 PhysRegsEverUsed[*AliasSet] = true;
643 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000644 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000645 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000646 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000647
648 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000649 if (TID.ImplicitDefs) {
650 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
651 *ImplicitDefs; ++ImplicitDefs) {
652 unsigned Reg = *ImplicitDefs;
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000653 bool IsNonAllocatable = PhysRegsUsed[Reg] == -2;
654 if (!IsNonAllocatable) {
655 spillPhysReg(MBB, MI, Reg, true);
656 PhysRegsUseOrder.push_back(Reg);
657 PhysRegsUsed[Reg] = 0; // It is free and reserved now
658 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000659 PhysRegsEverUsed[Reg] = true;
Chris Lattner0648b162005-01-23 22:51:56 +0000660
Jim Laskeycd4317e2006-07-21 21:15:20 +0000661 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
662 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000663 if (PhysRegsUsed[*AliasSet] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000664 if (!IsNonAllocatable) {
665 PhysRegsUseOrder.push_back(*AliasSet);
666 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
667 }
Chris Lattner45d57882006-09-08 19:03:30 +0000668 PhysRegsEverUsed[*AliasSet] = true;
669 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000670 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000671 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000672 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000673
Evan Chengddee8422006-11-15 20:55:15 +0000674 SmallVector<unsigned, 8> DeadDefs;
675 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
676 MachineOperand& MO = MI->getOperand(i);
677 if (MO.isRegister() && MO.isDead())
678 DeadDefs.push_back(MO.getReg());
679 }
680
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000681 // Okay, we have allocated all of the source operands and spilled any values
682 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000683 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000684 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000685 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000686 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
687 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000688 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000689 MRegisterInfo::isVirtualRegister(MO.getReg())) {
690 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000691 unsigned DestPhysReg;
692
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000693 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000694 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000695 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattner0648b162005-01-23 22:51:56 +0000696 PhysRegsEverUsed[DestPhysReg] = true;
Chris Lattnerd5725632003-05-12 03:54:14 +0000697 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000698 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000699 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000700 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000701
Chris Lattner56ddada2004-02-17 17:49:10 +0000702 // If this instruction defines any registers that are immediately dead,
703 // kill them now.
704 //
Evan Chengddee8422006-11-15 20:55:15 +0000705 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
706 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000707 unsigned PhysReg = VirtReg;
708 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
709 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
710 PhysReg = PhysRegSlot;
711 assert(PhysReg != 0);
712 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000713 } else if (PhysRegsUsed[PhysReg] == -2) {
714 // Unallocatable register dead, ignore.
715 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000716 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000717
Chris Lattner56ddada2004-02-17 17:49:10 +0000718 if (PhysReg) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000719 DOUT << " Register " << RegInfo->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000720 << " [%reg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000721 << "] is never used, removing it frame live list\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000722 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000723 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
724 *AliasSet; ++AliasSet) {
725 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000726 DOUT << " Register " << RegInfo->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000727 << " [%reg" << *AliasSet
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000728 << "] is never used, removing it frame live list\n";
Evan Chengddee8422006-11-15 20:55:15 +0000729 removePhysReg(*AliasSet);
730 }
731 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000732 }
733 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000734
735 // Finally, if this is a noop copy instruction, zap it.
736 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000737 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
738 LV->removeVirtualRegistersKilled(MI);
739 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000740 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000741 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000742 }
743
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000744 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000745
746 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000747 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000748 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000749 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000750 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000751 else
752 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000753
Chris Lattner9a5ef202005-11-09 05:28:45 +0000754#if 0
755 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000756 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000757 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
758 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000759 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +0000760 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +0000761 AllOk = false;
762 }
763 assert(AllOk && "Virtual registers still in phys regs?");
764#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000765
Chris Lattner128c2aa2003-08-17 18:01:15 +0000766 // Clear any physical register which appear live at the end of the basic
767 // block, but which do not hold any virtual registers. e.g., the stack
768 // pointer.
769 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000770}
771
Chris Lattner86c69a62002-12-17 03:16:10 +0000772
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000773/// runOnMachineFunction - Register allocate the whole function
774///
775bool RA::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000776 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000777 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000778 TM = &Fn.getTarget();
779 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000780 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000781
Chris Lattner0648b162005-01-23 22:51:56 +0000782 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
783 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
784 Fn.setUsedPhysRegs(PhysRegsEverUsed);
785
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}