blob: 891ee3c0f38a1306a7ca8800e47958cfbab2de0f [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"
Chris Lattner91a452b2003-01-13 00:25:40 +000016#include "llvm/CodeGen/Passes.h"
Chris Lattner580f9be2002-12-28 20:40:43 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000018#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerff863ba2002-12-25 05:05:46 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000021#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000023#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
Chris Lattner95255282006-06-28 23:17:24 +000026#include "llvm/Support/Visibility.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/Statistic.h"
Chris Lattner27f29162004-10-26 15:35:58 +000029#include <algorithm>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000030#include <iostream>
Chris Lattneref09c632004-01-31 21:27:19 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattnerb74e83c2002-12-16 16:15:28 +000033namespace {
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +000034 Statistic<> NumStores("ra-local", "Number of stores added");
35 Statistic<> NumLoads ("ra-local", "Number of loads added");
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +000036 Statistic<> NumFolded("ra-local", "Number of loads/stores folded into "
37 "instructions");
Chris Lattner95255282006-06-28 23:17:24 +000038 class VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattner580f9be2002-12-28 20:40:43 +000039 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000040 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000041 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000042 LiveVariables *LV;
Chris Lattner0648b162005-01-23 22:51:56 +000043 bool *PhysRegsEverUsed;
Chris Lattnerff863ba2002-12-25 05:05:46 +000044
Chris Lattnerb8822ad2003-08-04 23:36:39 +000045 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
46 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000047 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000048
49 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000050 // that is currently available in a physical register.
51 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000052
53 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000054 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000055 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000056
Chris Lattner64667b62004-02-09 01:26:13 +000057 // PhysRegsUsed - This array is effectively a map, containing entries for
58 // each physical register that currently has a value (ie, it is in
59 // Virt2PhysRegMap). The value mapped to is the virtual register
60 // corresponding to the physical register (the inverse of the
61 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
62 // because it is used by a future instruction. If the entry for a physical
63 // register is -1, then the physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000064 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000065 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000066
67 // PhysRegsUseOrder - This contains a list of the physical registers that
68 // currently have a virtual register value in them. This list provides an
69 // ordering of registers, imposing a reallocation order. This list is only
70 // used if all registers are allocated and we have to spill one, in which
71 // case we spill the least recently used register. Entries at the front of
72 // the list are the least recently used registers, entries at the back are
73 // the most recently used.
74 //
75 std::vector<unsigned> PhysRegsUseOrder;
76
Chris Lattner91a452b2003-01-13 00:25:40 +000077 // VirtRegModified - This bitset contains information about which virtual
78 // registers need to be spilled back to memory when their registers are
79 // scavenged. If a virtual register has simply been rematerialized, there
80 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000081 //
Chris Lattner91a452b2003-01-13 00:25:40 +000082 std::vector<bool> VirtRegModified;
83
84 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000085 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000086 Reg -= MRegisterInfo::FirstVirtualRegister;
87 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
88 VirtRegModified[Reg] = Val;
89 }
90
91 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +000092 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000093 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000094 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +000095 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
96 }
Chris Lattner82bee0f2002-12-18 08:14:26 +000097
Chris Lattnerb74e83c2002-12-16 16:15:28 +000098 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattneraebcce82004-06-16 06:57:29 +000099 if(PhysRegsUseOrder.empty() ||
100 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000101
102 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000103 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
104 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
105 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
106 // Add it to the end of the list
107 PhysRegsUseOrder.push_back(RegMatch);
108 if (RegMatch == Reg)
109 return; // Found an exact match, exit early
110 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000111 }
112
113 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000114 virtual const char *getPassName() const {
115 return "Local Register Allocator";
116 }
117
Chris Lattner91a452b2003-01-13 00:25:40 +0000118 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000119 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000120 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000121 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000122 MachineFunctionPass::getAnalysisUsage(AU);
123 }
124
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000125 private:
126 /// runOnMachineFunction - Register allocate the whole function
127 bool runOnMachineFunction(MachineFunction &Fn);
128
129 /// AllocateBasicBlock - Register allocate the specified basic block.
130 void AllocateBasicBlock(MachineBasicBlock &MBB);
131
Chris Lattner82bee0f2002-12-18 08:14:26 +0000132
Chris Lattner82bee0f2002-12-18 08:14:26 +0000133 /// areRegsEqual - This method returns true if the specified registers are
134 /// related to each other. To do this, it checks to see if they are equal
135 /// or if the first register is in the alias set of the second register.
136 ///
137 bool areRegsEqual(unsigned R1, unsigned R2) const {
138 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000139 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
140 *AliasSet; ++AliasSet) {
141 if (*AliasSet == R1) return true;
142 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000143 return false;
144 }
145
Chris Lattner580f9be2002-12-28 20:40:43 +0000146 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000147 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000148 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000149
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000150 /// removePhysReg - This method marks the specified physical register as no
151 /// longer being in use.
152 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000153 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000154
155 /// spillVirtReg - This method spills the value specified by PhysReg into
156 /// the virtual register slot specified by VirtReg. It then updates the RA
157 /// data structures to indicate the fact that PhysReg is now available.
158 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000159 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000160 unsigned VirtReg, unsigned PhysReg);
161
Chris Lattnerc21be922002-12-16 17:44:42 +0000162 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000163 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
164 /// true, then the request is ignored if the physical register does not
165 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000166 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000167 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000168 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000169
Chris Lattner91a452b2003-01-13 00:25:40 +0000170 /// assignVirtToPhysReg - This method updates local state so that we know
171 /// that PhysReg is the proper container for VirtReg now. The physical
172 /// register must not be used for anything else when this is called.
173 ///
174 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
175
176 /// liberatePhysReg - Make sure the specified physical register is available
177 /// for use. If there is currently a value in it, it is either moved out of
178 /// the way or spilled to memory.
179 ///
180 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000181 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000182
Chris Lattnerae640432002-12-17 02:50:10 +0000183 /// isPhysRegAvailable - Return true if the specified physical register is
184 /// free and available for use. This also includes checking to see if
185 /// aliased registers are all free...
186 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000187 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000188
189 /// getFreeReg - Look to see if there is a free register available in the
190 /// specified register class. If not, return 0.
191 ///
192 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000193
Chris Lattner91a452b2003-01-13 00:25:40 +0000194 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000195 /// register. If all compatible physical registers are used, this method
196 /// spills the last used virtual register to the stack, and uses that
197 /// register.
198 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000199 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000200 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000201
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000202 /// reloadVirtReg - This method transforms the specified specified virtual
203 /// register use to refer to a physical register. This method may do this
204 /// in one of several ways: if the register is available in a physical
205 /// register already, it uses that physical register. If the value is not
206 /// in a physical register, and if there are physical registers available,
207 /// it loads it into a register. If register pressure is high, and it is
208 /// possible, it tries to fold the load of the virtual register into the
209 /// instruction itself. It avoids doing this if register pressure is low to
210 /// improve the chance that subsequent instructions can use the reloaded
211 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000212 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000213 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
214 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000216
217 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
218 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000219 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000220}
221
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000222/// getStackSpaceFor - This allocates space for the specified virtual register
223/// to be held on the stack.
224int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
225 // Find the location Reg would belong...
226 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000227
Chris Lattner580f9be2002-12-28 20:40:43 +0000228 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000229 return I->second; // Already has space allocated?
230
Chris Lattner580f9be2002-12-28 20:40:43 +0000231 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000232 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
233 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000234
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000235 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000236 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
237 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000238}
239
Chris Lattnerae640432002-12-17 02:50:10 +0000240
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000241/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000242/// longer being in use.
243///
244void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000245 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000246
247 std::vector<unsigned>::iterator It =
248 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000249 if (It != PhysRegsUseOrder.end())
250 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000251}
252
Chris Lattner91a452b2003-01-13 00:25:40 +0000253
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000254/// spillVirtReg - This method spills the value specified by PhysReg into the
255/// virtual register slot specified by VirtReg. It then updates the RA data
256/// structures to indicate the fact that PhysReg is now available.
257///
Chris Lattner688c8252004-02-22 19:08:15 +0000258void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000259 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000260 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000261 " Must not have appropriate kill for the register or use exists beyond"
262 " the intended one.");
263 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
264 std::cerr << " containing %reg" << VirtReg;
265 if (!isVirtRegModified(VirtReg))
266 std::cerr << " which has not been modified, so no store necessary!");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000267
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000268 // Otherwise, there is a virtual register corresponding to this physical
269 // register. We only need to spill it into its stack slot if it has been
270 // modified.
271 if (isVirtRegModified(VirtReg)) {
272 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
273 int FrameIndex = getStackSpaceFor(VirtReg, RC);
274 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000275 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000276 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000277 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000278
279 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000280
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000281 DEBUG(std::cerr << "\n");
Chris Lattner82bee0f2002-12-18 08:14:26 +0000282 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000283}
284
Chris Lattnerae640432002-12-17 02:50:10 +0000285
Chris Lattner91a452b2003-01-13 00:25:40 +0000286/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000287/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
288/// then the request is ignored if the physical register does not contain a
289/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000290///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000291void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000292 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000293 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
294 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
295 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000296 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000297 // If the selected register aliases any other registers, we must make
298 // sure that one of the aliases isn't alive...
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000299 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000300 *AliasSet; ++AliasSet)
301 if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
302 if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
303 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000304 }
305}
306
307
308/// assignVirtToPhysReg - This method updates local state so that we know
309/// that PhysReg is the proper container for VirtReg now. The physical
310/// register must not be used for anything else when this is called.
311///
312void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000313 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000314 // Update information to note the fact that this register was just used, and
315 // it holds VirtReg.
316 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000317 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000318 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
319}
320
321
Chris Lattnerae640432002-12-17 02:50:10 +0000322/// isPhysRegAvailable - Return true if the specified physical register is free
323/// and available for use. This also includes checking to see if aliased
324/// registers are all free...
325///
326bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000327 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000328
329 // If the selected register aliases any other allocated registers, it is
330 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000331 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
332 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000333 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000334 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000335 return true;
336}
337
338
Chris Lattner91a452b2003-01-13 00:25:40 +0000339/// getFreeReg - Look to see if there is a free register available in the
340/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000341///
Chris Lattner91a452b2003-01-13 00:25:40 +0000342unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000343 // Get iterators defining the range of registers that are valid to allocate in
344 // this class, which also specifies the preferred allocation order.
345 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
346 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000347
Chris Lattner91a452b2003-01-13 00:25:40 +0000348 for (; RI != RE; ++RI)
349 if (isPhysRegAvailable(*RI)) { // Is reg unused?
350 assert(*RI != 0 && "Cannot use register!");
351 return *RI; // Found an unused register!
352 }
353 return 0;
354}
355
356
357/// liberatePhysReg - Make sure the specified physical register is available for
358/// use. If there is currently a value in it, it is either moved out of the way
359/// or spilled to memory.
360///
361void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000362 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000363 spillPhysReg(MBB, I, PhysReg);
364}
365
366
367/// getReg - Find a physical register to hold the specified virtual
368/// register. If all compatible physical registers are used, this method spills
369/// the last used virtual register to the stack, and uses that register.
370///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000371unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000372 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000373 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
374
375 // First check to see if we have a free register of the requested type...
376 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000377
Chris Lattnerae640432002-12-17 02:50:10 +0000378 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000379 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000380 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000381
382 // Loop over all of the preallocated registers from the least recently used
383 // to the most recently used. When we find one that is capable of holding
384 // our register, use it.
385 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000386 assert(i != PhysRegsUseOrder.size() &&
387 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000388
Chris Lattnerae640432002-12-17 02:50:10 +0000389 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000390
391 // We can only use this register if it holds a virtual register (ie, it
392 // can be spilled). Do not use it if it is an explicitly allocated
393 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000394 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000395 "PhysReg in PhysRegsUseOrder, but is not allocated?");
396 if (PhysRegsUsed[R]) {
397 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000398 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000399 PhysReg = R;
400 break;
401 } else {
402 // If one of the registers aliased to the current register is
403 // compatible, use it.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000404 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
405 *AliasSet; ++AliasSet) {
Chris Lattner3bba0262004-08-15 22:23:09 +0000406 if (RC->contains(*AliasSet)) {
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000407 PhysReg = *AliasSet; // Take an aliased register
408 break;
409 }
410 }
Chris Lattner41822c72003-08-23 23:49:42 +0000411 }
Chris Lattnerae640432002-12-17 02:50:10 +0000412 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000413 }
414
Chris Lattnerae640432002-12-17 02:50:10 +0000415 assert(PhysReg && "Physical register not assigned!?!?");
416
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000417 // At this point PhysRegsUseOrder[i] is the least recently used register of
418 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000419 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000420 }
421
422 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000423 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000424 return PhysReg;
425}
426
Chris Lattnerae640432002-12-17 02:50:10 +0000427
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000428/// reloadVirtReg - This method transforms the specified specified virtual
429/// register use to refer to a physical register. This method may do this in
430/// one of several ways: if the register is available in a physical register
431/// already, it uses that physical register. If the value is not in a physical
432/// register, and if there are physical registers available, it loads it into a
433/// register. If register pressure is high, and it is possible, it tries to
434/// fold the load of the virtual register into the instruction itself. It
435/// avoids doing this if register pressure is low to improve the chance that
436/// subsequent instructions can use the reloaded value. This method returns the
437/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000438///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000439MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
440 unsigned OpNum) {
441 unsigned VirtReg = MI->getOperand(OpNum).getReg();
442
443 // If the virtual register is already available, just update the instruction
444 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000445 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000446 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000447 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000448 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000449 }
450
Chris Lattner1e3812c2004-02-17 04:08:37 +0000451 // Otherwise, we need to fold it into the current instruction, or reload it.
452 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000453 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000454 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000455 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000456
Chris Lattner11390e72004-02-17 08:09:40 +0000457 if (PhysReg) { // Register is available, allocate it!
458 assignVirtToPhysReg(VirtReg, PhysReg);
459 } else { // No registers available.
460 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000461 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000462 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000463 // Since we changed the address of MI, make sure to update live variables
464 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000465 LV->instructionChanged(MI, FMI);
466 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000467 }
468
469 // It looks like we can't fold this virtual register load into this
470 // instruction. Force some poor hapless value out of the register file to
471 // make room for the new register, and reload it.
472 PhysReg = getReg(MBB, MI, VirtReg);
473 }
474
Chris Lattner91a452b2003-01-13 00:25:40 +0000475 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
476
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000477 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
478 << RegInfo->getName(PhysReg) << "\n");
479
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000480 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000481 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000482 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000483
Chris Lattner0648b162005-01-23 22:51:56 +0000484 PhysRegsEverUsed[PhysReg] = true;
Chris Lattnere53f4a02006-05-04 17:52:23 +0000485 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000486 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000487}
488
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000489
490
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000491void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
492 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000493 MachineBasicBlock::iterator MII = MBB.begin();
494 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000495
496 // If this is the first basic block in the machine function, add live-in
497 // registers as active.
498 if (&MBB == &*MF->begin()) {
499 for (MachineFunction::livein_iterator I = MF->livein_begin(),
500 E = MF->livein_end(); I != E; ++I) {
501 unsigned Reg = I->first;
502 PhysRegsEverUsed[Reg] = true;
503 PhysRegsUsed[Reg] = 0; // It is free and reserved now
504 PhysRegsUseOrder.push_back(Reg);
505 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
506 *AliasSet; ++AliasSet) {
507 PhysRegsUseOrder.push_back(*AliasSet);
508 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
509 PhysRegsEverUsed[*AliasSet] = true;
510 }
511 }
512 }
513
514 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000515 while (MII != MBB.end()) {
516 MachineInstr *MI = MII++;
517 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000518 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
519 std::cerr << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000520 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
521 if (PhysRegsUsed[i] != -1)
522 std::cerr << "[" << RegInfo->getName(i)
523 << ",%reg" << PhysRegsUsed[i] << "] ";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000524 std::cerr << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000525
Chris Lattnerae640432002-12-17 02:50:10 +0000526 // Loop over the implicit uses, making sure that they are at the head of the
527 // use order list, so they don't get reallocated.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000528 for (const unsigned *ImplicitUses = TID.ImplicitUses;
529 *ImplicitUses; ++ImplicitUses)
Chris Lattnerecea5632004-02-09 02:12:04 +0000530 MarkPhysRegRecentlyUsed(*ImplicitUses);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000531
Brian Gaeke53b99a02003-08-15 21:19:25 +0000532 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000533 // incoming values if we are out of registers. Note that we completely
534 // ignore physical register uses here. We assume that if an explicit
535 // physical register is referenced by the instruction, that it is guaranteed
536 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000537 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000538 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
539 MachineOperand& MO = MI->getOperand(i);
540 // here we are looking for only used operands (never def&use)
541 if (!MO.isDef() && MO.isRegister() && MO.getReg() &&
542 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000543 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000544 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000545
Chris Lattner56ddada2004-02-17 17:49:10 +0000546 // If this instruction is the last user of anything in registers, kill the
547 // value, freeing the register being used, so it doesn't need to be
548 // spilled to memory.
549 //
550 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
551 KE = LV->killed_end(MI); KI != KE; ++KI) {
Chris Lattner44b94c22005-08-23 23:42:17 +0000552 unsigned VirtReg = *KI;
Chris Lattner56ddada2004-02-17 17:49:10 +0000553 unsigned PhysReg = VirtReg;
554 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
555 // If the virtual register was never materialized into a register, it
556 // might not be in the map, but it won't hurt to zero it out anyway.
557 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
558 PhysReg = PhysRegSlot;
559 PhysRegSlot = 0;
560 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000561
Chris Lattner56ddada2004-02-17 17:49:10 +0000562 if (PhysReg) {
563 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
564 << "[%reg" << VirtReg <<"], removing it from live set\n");
565 removePhysReg(PhysReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000566 }
567 }
568
569 // Loop over all of the operands of the instruction, spilling registers that
570 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000571 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
572 MachineOperand& MO = MI->getOperand(i);
573 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
574 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
575 unsigned Reg = MO.getReg();
Chris Lattner0648b162005-01-23 22:51:56 +0000576 PhysRegsEverUsed[Reg] = true;
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000577 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000578 PhysRegsUsed[Reg] = 0; // It is free and reserved now
579 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000580 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
581 *AliasSet; ++AliasSet) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000582 PhysRegsUseOrder.push_back(*AliasSet);
583 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Chris Lattner0648b162005-01-23 22:51:56 +0000584 PhysRegsEverUsed[*AliasSet] = true;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000585 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000586 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000587 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000588
589 // Loop over the implicit defs, spilling them as well.
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000590 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
591 *ImplicitDefs; ++ImplicitDefs) {
592 unsigned Reg = *ImplicitDefs;
Chris Lattner11390e72004-02-17 08:09:40 +0000593 spillPhysReg(MBB, MI, Reg, true);
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000594 PhysRegsUseOrder.push_back(Reg);
595 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Chris Lattner0648b162005-01-23 22:51:56 +0000596 PhysRegsEverUsed[Reg] = true;
597
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000598 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
599 *AliasSet; ++AliasSet) {
Chris Lattnerecea5632004-02-09 02:12:04 +0000600 PhysRegsUseOrder.push_back(*AliasSet);
601 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
Chris Lattner0648b162005-01-23 22:51:56 +0000602 PhysRegsEverUsed[*AliasSet] = true;
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000603 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000604 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000605
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000606 // Okay, we have allocated all of the source operands and spilled any values
607 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000608 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000609 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000610 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000611 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
612 MachineOperand& MO = MI->getOperand(i);
613 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
614 MRegisterInfo::isVirtualRegister(MO.getReg())) {
615 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000616 unsigned DestPhysReg;
617
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000618 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000619 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000620 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattner0648b162005-01-23 22:51:56 +0000621 PhysRegsEverUsed[DestPhysReg] = true;
Chris Lattnerd5725632003-05-12 03:54:14 +0000622 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000623 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000624 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000625 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000626
Chris Lattner56ddada2004-02-17 17:49:10 +0000627 // If this instruction defines any registers that are immediately dead,
628 // kill them now.
629 //
630 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
631 KE = LV->dead_end(MI); KI != KE; ++KI) {
Chris Lattner44b94c22005-08-23 23:42:17 +0000632 unsigned VirtReg = *KI;
Chris Lattner56ddada2004-02-17 17:49:10 +0000633 unsigned PhysReg = VirtReg;
634 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
635 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
636 PhysReg = PhysRegSlot;
637 assert(PhysReg != 0);
638 PhysRegSlot = 0;
639 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000640
Chris Lattner56ddada2004-02-17 17:49:10 +0000641 if (PhysReg) {
642 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
643 << " [%reg" << VirtReg
644 << "] is never used, removing it frame live list\n");
645 removePhysReg(PhysReg);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000646 }
647 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000648
649 // Finally, if this is a noop copy instruction, zap it.
650 unsigned SrcReg, DstReg;
651 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg)
652 MBB.erase(MI);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000653 }
654
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000655 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000656
657 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000658 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
659 if (PhysRegsUsed[i] != -1)
660 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000661 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000662 else
663 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000664
Chris Lattner9a5ef202005-11-09 05:28:45 +0000665#if 0
666 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000667 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000668 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
669 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000670 if (unsigned PR = Virt2PhysRegMap[i]) {
671 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
672 AllOk = false;
673 }
674 assert(AllOk && "Virtual registers still in phys regs?");
675#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000676
Chris Lattner128c2aa2003-08-17 18:01:15 +0000677 // Clear any physical register which appear live at the end of the basic
678 // block, but which do not hold any virtual registers. e.g., the stack
679 // pointer.
680 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000681}
682
Chris Lattner86c69a62002-12-17 03:16:10 +0000683
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000684/// runOnMachineFunction - Register allocate the whole function
685///
686bool RA::runOnMachineFunction(MachineFunction &Fn) {
687 DEBUG(std::cerr << "Machine Function " << "\n");
688 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000689 TM = &Fn.getTarget();
690 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000691 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000692
Chris Lattner0648b162005-01-23 22:51:56 +0000693 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
694 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
695 Fn.setUsedPhysRegs(PhysRegsEverUsed);
696
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000697 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner64667b62004-02-09 01:26:13 +0000698
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000699 // initialize the virtual->physical register map to have a 'null'
700 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000701 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000702
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000703 // Loop over all of the basic blocks, eliminating virtual register references
704 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
705 MBB != MBBe; ++MBB)
706 AllocateBasicBlock(*MBB);
707
Chris Lattner580f9be2002-12-28 20:40:43 +0000708 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000709 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000710 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000711 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000712 return true;
713}
714
Chris Lattneref09c632004-01-31 21:27:19 +0000715FunctionPass *llvm::createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000716 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000717}