blob: 2b3dce324b911f985511d60ed567a3f563243fee [file] [log] [blame]
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb74e83c2002-12-16 16:15:28 +00009//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4cc662b2003-08-03 21:47:31 +000015#define DEBUG_TYPE "regalloc"
Evan Chengddee8422006-11-15 20:55:15 +000016#include "llvm/BasicBlock.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000017#include "llvm/CodeGen/Passes.h"
Chris Lattner580f9be2002-12-28 20:40:43 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000019#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerff863ba2002-12-25 05:05:46 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner91a452b2003-01-13 00:25:40 +000022#include "llvm/CodeGen/LiveVariables.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000023#include "llvm/CodeGen/RegAllocRegistry.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000024#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000025#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/DenseMap.h"
Evan Chengddee8422006-11-15 20:55:15 +000030#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattner27f29162004-10-26 15:35:58 +000032#include <algorithm>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000033#include <iostream>
Chris Lattneref09c632004-01-31 21:27:19 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattnerb74e83c2002-12-16 16:15:28 +000036namespace {
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000037 static Statistic<> NumStores("ra-local", "Number of stores added");
38 static Statistic<> NumLoads ("ra-local", "Number of loads added");
Andrew Lenharthae6153f2006-07-20 17:43:27 +000039 static Statistic<> NumFolded("ra-local", "Number of loads/stores folded "
40 "into instructions");
Jim Laskey13ec7022006-08-01 14:21:23 +000041
42 static RegisterRegAlloc
43 localRegAlloc("local", " local register allocator",
44 createLocalRegisterAllocator);
45
46
Chris Lattner95255282006-06-28 23:17:24 +000047 class VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattner580f9be2002-12-28 20:40:43 +000048 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000049 MachineFunction *MF;
Chris Lattner580f9be2002-12-28 20:40:43 +000050 const MRegisterInfo *RegInfo;
Chris Lattner91a452b2003-01-13 00:25:40 +000051 LiveVariables *LV;
Chris Lattner0648b162005-01-23 22:51:56 +000052 bool *PhysRegsEverUsed;
Chris Lattnerff863ba2002-12-25 05:05:46 +000053
Chris Lattnerb8822ad2003-08-04 23:36:39 +000054 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
55 // values are spilled.
Chris Lattner580f9be2002-12-28 20:40:43 +000056 std::map<unsigned, int> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000057
58 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000059 // that is currently available in a physical register.
60 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000061
62 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000063 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000064 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000065
Chris Lattner64667b62004-02-09 01:26:13 +000066 // PhysRegsUsed - This array is effectively a map, containing entries for
67 // each physical register that currently has a value (ie, it is in
68 // Virt2PhysRegMap). The value mapped to is the virtual register
69 // corresponding to the physical register (the inverse of the
70 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000071 // because it is used by a future instruction, and to -2 if it is not
72 // allocatable. If the entry for a physical register is -1, then the
73 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000074 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000075 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000076
77 // PhysRegsUseOrder - This contains a list of the physical registers that
78 // currently have a virtual register value in them. This list provides an
79 // ordering of registers, imposing a reallocation order. This list is only
80 // used if all registers are allocated and we have to spill one, in which
81 // case we spill the least recently used register. Entries at the front of
82 // the list are the least recently used registers, entries at the back are
83 // the most recently used.
84 //
85 std::vector<unsigned> PhysRegsUseOrder;
86
Chris Lattner91a452b2003-01-13 00:25:40 +000087 // VirtRegModified - This bitset contains information about which virtual
88 // registers need to be spilled back to memory when their registers are
89 // scavenged. If a virtual register has simply been rematerialized, there
90 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +000091 //
Chris Lattner91a452b2003-01-13 00:25:40 +000092 std::vector<bool> VirtRegModified;
93
94 void markVirtRegModified(unsigned Reg, bool Val = true) {
Chris Lattneref09c632004-01-31 21:27:19 +000095 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +000096 Reg -= MRegisterInfo::FirstVirtualRegister;
97 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
98 VirtRegModified[Reg] = Val;
99 }
100
101 bool isVirtRegModified(unsigned Reg) const {
Chris Lattneref09c632004-01-31 21:27:19 +0000102 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000103 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000104 && "Illegal virtual register!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000105 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
106 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000107
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000108 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000109 if (PhysRegsUseOrder.empty() ||
110 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000111
112 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000113 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
114 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
115 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
116 // Add it to the end of the list
117 PhysRegsUseOrder.push_back(RegMatch);
118 if (RegMatch == Reg)
119 return; // Found an exact match, exit early
120 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000121 }
122
123 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000124 virtual const char *getPassName() const {
125 return "Local Register Allocator";
126 }
127
Chris Lattner91a452b2003-01-13 00:25:40 +0000128 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner56ddada2004-02-17 17:49:10 +0000129 AU.addRequired<LiveVariables>();
Chris Lattner91a452b2003-01-13 00:25:40 +0000130 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000131 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000132 MachineFunctionPass::getAnalysisUsage(AU);
133 }
134
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000135 private:
136 /// runOnMachineFunction - Register allocate the whole function
137 bool runOnMachineFunction(MachineFunction &Fn);
138
139 /// AllocateBasicBlock - Register allocate the specified basic block.
140 void AllocateBasicBlock(MachineBasicBlock &MBB);
141
Chris Lattner82bee0f2002-12-18 08:14:26 +0000142
Chris Lattner82bee0f2002-12-18 08:14:26 +0000143 /// areRegsEqual - This method returns true if the specified registers are
144 /// related to each other. To do this, it checks to see if they are equal
145 /// or if the first register is in the alias set of the second register.
146 ///
147 bool areRegsEqual(unsigned R1, unsigned R2) const {
148 if (R1 == R2) return true;
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000149 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
150 *AliasSet; ++AliasSet) {
151 if (*AliasSet == R1) return true;
152 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000153 return false;
154 }
155
Chris Lattner580f9be2002-12-28 20:40:43 +0000156 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000157 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000158 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000159
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000160 /// removePhysReg - This method marks the specified physical register as no
161 /// longer being in use.
162 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000163 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000164
165 /// spillVirtReg - This method spills the value specified by PhysReg into
166 /// the virtual register slot specified by VirtReg. It then updates the RA
167 /// data structures to indicate the fact that PhysReg is now available.
168 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000169 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000170 unsigned VirtReg, unsigned PhysReg);
171
Chris Lattnerc21be922002-12-16 17:44:42 +0000172 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000173 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
174 /// true, then the request is ignored if the physical register does not
175 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000176 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000177 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000178 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000179
Chris Lattner91a452b2003-01-13 00:25:40 +0000180 /// assignVirtToPhysReg - This method updates local state so that we know
181 /// that PhysReg is the proper container for VirtReg now. The physical
182 /// register must not be used for anything else when this is called.
183 ///
184 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
185
186 /// liberatePhysReg - Make sure the specified physical register is available
187 /// for use. If there is currently a value in it, it is either moved out of
188 /// the way or spilled to memory.
189 ///
190 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000191 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000192
Chris Lattnerae640432002-12-17 02:50:10 +0000193 /// isPhysRegAvailable - Return true if the specified physical register is
194 /// free and available for use. This also includes checking to see if
195 /// aliased registers are all free...
196 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000197 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000198
199 /// getFreeReg - Look to see if there is a free register available in the
200 /// specified register class. If not, return 0.
201 ///
202 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000203
Chris Lattner91a452b2003-01-13 00:25:40 +0000204 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000205 /// register. If all compatible physical registers are used, this method
206 /// spills the last used virtual register to the stack, and uses that
207 /// register.
208 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000209 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000210 unsigned VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000211
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000212 /// reloadVirtReg - This method transforms the specified specified virtual
213 /// register use to refer to a physical register. This method may do this
214 /// in one of several ways: if the register is available in a physical
215 /// register already, it uses that physical register. If the value is not
216 /// in a physical register, and if there are physical registers available,
217 /// it loads it into a register. If register pressure is high, and it is
218 /// possible, it tries to fold the load of the virtual register into the
219 /// instruction itself. It avoids doing this if register pressure is low to
220 /// improve the chance that subsequent instructions can use the reloaded
221 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000222 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000223 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
224 unsigned OpNum);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000225
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000226
227 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
228 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000229 };
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000230}
231
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000232/// getStackSpaceFor - This allocates space for the specified virtual register
233/// to be held on the stack.
234int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
235 // Find the location Reg would belong...
236 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000237
Chris Lattner580f9be2002-12-28 20:40:43 +0000238 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000239 return I->second; // Already has space allocated?
240
Chris Lattner580f9be2002-12-28 20:40:43 +0000241 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000242 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
243 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000244
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000245 // Assign the slot...
Chris Lattner580f9be2002-12-28 20:40:43 +0000246 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
247 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000248}
249
Chris Lattnerae640432002-12-17 02:50:10 +0000250
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000251/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000252/// longer being in use.
253///
254void RA::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000255 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000256
257 std::vector<unsigned>::iterator It =
258 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000259 if (It != PhysRegsUseOrder.end())
260 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000261}
262
Chris Lattner91a452b2003-01-13 00:25:40 +0000263
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000264/// spillVirtReg - This method spills the value specified by PhysReg into the
265/// virtual register slot specified by VirtReg. It then updates the RA data
266/// structures to indicate the fact that PhysReg is now available.
267///
Chris Lattner688c8252004-02-22 19:08:15 +0000268void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000269 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000270 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000271 " Must not have appropriate kill for the register or use exists beyond"
272 " the intended one.");
273 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
274 std::cerr << " containing %reg" << VirtReg;
275 if (!isVirtRegModified(VirtReg))
276 std::cerr << " which has not been modified, so no store necessary!");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000277
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000278 // Otherwise, there is a virtual register corresponding to this physical
279 // register. We only need to spill it into its stack slot if it has been
280 // modified.
281 if (isVirtRegModified(VirtReg)) {
282 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
283 int FrameIndex = getStackSpaceFor(VirtReg, RC);
284 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000285 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000286 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000287 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000288
289 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000290
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000291 DEBUG(std::cerr << "\n");
Chris Lattner82bee0f2002-12-18 08:14:26 +0000292 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000293}
294
Chris Lattnerae640432002-12-17 02:50:10 +0000295
Chris Lattner91a452b2003-01-13 00:25:40 +0000296/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000297/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
298/// then the request is ignored if the physical register does not contain a
299/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000300///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000301void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000302 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000303 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000304 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000305 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
306 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000307 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000308 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000309 // sure that one of the aliases isn't alive.
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000310 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000311 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000312 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
313 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Chengddee8422006-11-15 20:55:15 +0000314 if (PhysRegsUsed[*AliasSet] == 0) {
315 // This must have been a dead def due to something like this:
316 // %EAX :=
317 // := op %AL
318 // No more use of %EAX, %AH, etc.
319 // %EAX isn't dead upon definition, but %AH is. However %AH isn't
320 // an operand of definition MI so it's not marked as such.
321 DEBUG(std::cerr << " Register " << RegInfo->getName(*AliasSet)
322 << " [%reg" << *AliasSet
323 << "] is never used, removing it frame live list\n");
324 removePhysReg(*AliasSet);
325 } else
Chris Lattner64667b62004-02-09 01:26:13 +0000326 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000327 }
328}
329
330
331/// assignVirtToPhysReg - This method updates local state so that we know
332/// that PhysReg is the proper container for VirtReg now. The physical
333/// register must not be used for anything else when this is called.
334///
335void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000336 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000337 // Update information to note the fact that this register was just used, and
338 // it holds VirtReg.
339 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000340 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Chris Lattner91a452b2003-01-13 00:25:40 +0000341 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
342}
343
344
Chris Lattnerae640432002-12-17 02:50:10 +0000345/// isPhysRegAvailable - Return true if the specified physical register is free
346/// and available for use. This also includes checking to see if aliased
347/// registers are all free...
348///
349bool RA::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000350 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000351
352 // If the selected register aliases any other allocated registers, it is
353 // not free!
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000354 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
355 *AliasSet; ++AliasSet)
Chris Lattner64667b62004-02-09 01:26:13 +0000356 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000357 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000358 return true;
359}
360
361
Chris Lattner91a452b2003-01-13 00:25:40 +0000362/// getFreeReg - Look to see if there is a free register available in the
363/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000364///
Chris Lattner91a452b2003-01-13 00:25:40 +0000365unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000366 // Get iterators defining the range of registers that are valid to allocate in
367 // this class, which also specifies the preferred allocation order.
368 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
369 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000370
Chris Lattner91a452b2003-01-13 00:25:40 +0000371 for (; RI != RE; ++RI)
372 if (isPhysRegAvailable(*RI)) { // Is reg unused?
373 assert(*RI != 0 && "Cannot use register!");
374 return *RI; // Found an unused register!
375 }
376 return 0;
377}
378
379
380/// liberatePhysReg - Make sure the specified physical register is available for
381/// use. If there is currently a value in it, it is either moved out of the way
382/// or spilled to memory.
383///
384void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000385 unsigned PhysReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000386 spillPhysReg(MBB, I, PhysReg);
387}
388
389
390/// getReg - Find a physical register to hold the specified virtual
391/// register. If all compatible physical registers are used, this method spills
392/// the last used virtual register to the stack, and uses that register.
393///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000394unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000395 unsigned VirtReg) {
Chris Lattner91a452b2003-01-13 00:25:40 +0000396 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
397
398 // First check to see if we have a free register of the requested type...
399 unsigned PhysReg = getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000400
Chris Lattnerae640432002-12-17 02:50:10 +0000401 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000402 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000403 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000404
405 // Loop over all of the preallocated registers from the least recently used
406 // to the most recently used. When we find one that is capable of holding
407 // our register, use it.
408 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000409 assert(i != PhysRegsUseOrder.size() &&
410 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000411
Chris Lattnerae640432002-12-17 02:50:10 +0000412 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000413
414 // We can only use this register if it holds a virtual register (ie, it
415 // can be spilled). Do not use it if it is an explicitly allocated
416 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000417 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000418 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000419 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000420 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000421 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000422 PhysReg = R;
423 break;
424 } else {
425 // If one of the registers aliased to the current register is
426 // compatible, use it.
Chris Lattner5e503492006-09-03 07:15:37 +0000427 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
428 *AliasIt; ++AliasIt) {
429 if (RC->contains(*AliasIt) &&
430 // If this is pinned down for some reason, don't use it. For
431 // example, if CL is pinned, and we run across CH, don't use
432 // CH as justification for using scavenging ECX (which will
433 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000434 PhysRegsUsed[*AliasIt] != 0 &&
435
436 // Make sure the register is allocatable. Don't allocate SIL on
437 // x86-32.
438 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000439 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000440 break;
441 }
442 }
Chris Lattner41822c72003-08-23 23:49:42 +0000443 }
Chris Lattnerae640432002-12-17 02:50:10 +0000444 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000445 }
446
Chris Lattnerae640432002-12-17 02:50:10 +0000447 assert(PhysReg && "Physical register not assigned!?!?");
448
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000449 // At this point PhysRegsUseOrder[i] is the least recently used register of
450 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000451 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000452 }
453
454 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000455 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000456 return PhysReg;
457}
458
Chris Lattnerae640432002-12-17 02:50:10 +0000459
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000460/// reloadVirtReg - This method transforms the specified specified virtual
461/// register use to refer to a physical register. This method may do this in
462/// one of several ways: if the register is available in a physical register
463/// already, it uses that physical register. If the value is not in a physical
464/// register, and if there are physical registers available, it loads it into a
465/// register. If register pressure is high, and it is possible, it tries to
466/// fold the load of the virtual register into the instruction itself. It
467/// avoids doing this if register pressure is low to improve the chance that
468/// subsequent instructions can use the reloaded value. This method returns the
469/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000470///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000471MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
472 unsigned OpNum) {
473 unsigned VirtReg = MI->getOperand(OpNum).getReg();
474
475 // If the virtual register is already available, just update the instruction
476 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000477 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000478 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000479 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000480 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000481 }
482
Chris Lattner1e3812c2004-02-17 04:08:37 +0000483 // Otherwise, we need to fold it into the current instruction, or reload it.
484 // If we have registers available to hold the value, use them.
Chris Lattnerff863ba2002-12-25 05:05:46 +0000485 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000486 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000487 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000488
Chris Lattner11390e72004-02-17 08:09:40 +0000489 if (PhysReg) { // Register is available, allocate it!
490 assignVirtToPhysReg(VirtReg, PhysReg);
491 } else { // No registers available.
492 // If we can fold this spill into this instruction, do so now.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000493 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
Alkis Evlogimenosd6f6d1a2004-02-21 18:07:33 +0000494 ++NumFolded;
Chris Lattnerd368c612004-02-19 18:34:02 +0000495 // Since we changed the address of MI, make sure to update live variables
496 // to know that the new instruction has the properties of the old one.
Alkis Evlogimenos39354c92004-03-14 07:19:51 +0000497 LV->instructionChanged(MI, FMI);
498 return MBB.insert(MBB.erase(MI), FMI);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000499 }
500
501 // It looks like we can't fold this virtual register load into this
502 // instruction. Force some poor hapless value out of the register file to
503 // make room for the new register, and reload it.
504 PhysReg = getReg(MBB, MI, VirtReg);
505 }
506
Chris Lattner91a452b2003-01-13 00:25:40 +0000507 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
508
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000509 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
510 << RegInfo->getName(PhysReg) << "\n");
511
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000512 // Add move instruction(s)
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000513 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000514 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000515
Chris Lattner0648b162005-01-23 22:51:56 +0000516 PhysRegsEverUsed[PhysReg] = true;
Chris Lattnere53f4a02006-05-04 17:52:23 +0000517 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000518 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000519}
520
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000521
522
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000523void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
524 // loop over each instruction
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000525 MachineBasicBlock::iterator MII = MBB.begin();
526 const TargetInstrInfo &TII = *TM->getInstrInfo();
Chris Lattner44500e32006-06-15 22:21:53 +0000527
Evan Chengddee8422006-11-15 20:55:15 +0000528 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
529 if (LBB) std::cerr << "\nStarting RegAlloc of BB: " << LBB->getName());
530
Chris Lattner44500e32006-06-15 22:21:53 +0000531 // If this is the first basic block in the machine function, add live-in
532 // registers as active.
533 if (&MBB == &*MF->begin()) {
534 for (MachineFunction::livein_iterator I = MF->livein_begin(),
535 E = MF->livein_end(); I != E; ++I) {
536 unsigned Reg = I->first;
537 PhysRegsEverUsed[Reg] = true;
538 PhysRegsUsed[Reg] = 0; // It is free and reserved now
539 PhysRegsUseOrder.push_back(Reg);
540 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
541 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000542 if (PhysRegsUsed[*AliasSet] != -2) {
543 PhysRegsUseOrder.push_back(*AliasSet);
544 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
545 PhysRegsEverUsed[*AliasSet] = true;
546 }
Chris Lattner44500e32006-06-15 22:21:53 +0000547 }
548 }
549 }
550
551 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000552 while (MII != MBB.end()) {
553 MachineInstr *MI = MII++;
554 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000555 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
556 std::cerr << " Regs have values: ";
Chris Lattner64667b62004-02-09 01:26:13 +0000557 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000558 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000559 std::cerr << "[" << RegInfo->getName(i)
560 << ",%reg" << PhysRegsUsed[i] << "] ";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000561 std::cerr << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000562
Chris Lattnerae640432002-12-17 02:50:10 +0000563 // Loop over the implicit uses, making sure that they are at the head of the
564 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000565 if (TID.ImplicitUses) {
566 for (const unsigned *ImplicitUses = TID.ImplicitUses;
567 *ImplicitUses; ++ImplicitUses)
568 MarkPhysRegRecentlyUsed(*ImplicitUses);
569 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000570
Evan Chengddee8422006-11-15 20:55:15 +0000571 SmallVector<unsigned, 8> Kills;
572 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
573 MachineOperand& MO = MI->getOperand(i);
574 if (MO.isRegister() && MO.isKill())
575 Kills.push_back(MO.getReg());
576 }
577
Brian Gaeke53b99a02003-08-15 21:19:25 +0000578 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000579 // incoming values if we are out of registers. Note that we completely
580 // ignore physical register uses here. We assume that if an explicit
581 // physical register is referenced by the instruction, that it is guaranteed
582 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000583 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000584 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
585 MachineOperand& MO = MI->getOperand(i);
586 // here we are looking for only used operands (never def&use)
Evan Chengddee8422006-11-15 20:55:15 +0000587 if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000588 MRegisterInfo::isVirtualRegister(MO.getReg()))
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000589 MI = reloadVirtReg(MBB, MI, i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000590 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000591
Evan Chengddee8422006-11-15 20:55:15 +0000592 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000593 // value, freeing the register being used, so it doesn't need to be
594 // spilled to memory.
595 //
Evan Chengddee8422006-11-15 20:55:15 +0000596 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
597 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000598 unsigned PhysReg = VirtReg;
599 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
600 // If the virtual register was never materialized into a register, it
601 // might not be in the map, but it won't hurt to zero it out anyway.
602 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
603 PhysReg = PhysRegSlot;
604 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000605 } else if (PhysRegsUsed[PhysReg] == -2) {
606 // Unallocatable register dead, ignore.
607 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000608 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000609
Chris Lattner56ddada2004-02-17 17:49:10 +0000610 if (PhysReg) {
611 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
612 << "[%reg" << VirtReg <<"], removing it from live set\n");
613 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000614 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
615 *AliasSet; ++AliasSet) {
616 if (PhysRegsUsed[*AliasSet] != -2) {
617 DEBUG(std::cerr << " Last use of "
618 << RegInfo->getName(*AliasSet)
619 << "[%reg" << VirtReg <<"], removing it from live set\n");
620 removePhysReg(*AliasSet);
621 }
622 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000623 }
624 }
625
626 // Loop over all of the operands of the instruction, spilling registers that
627 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000628 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
629 MachineOperand& MO = MI->getOperand(i);
Evan Cheng438f7bc2006-11-10 08:43:01 +0000630 if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000631 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
632 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000633 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
634
Chris Lattner0648b162005-01-23 22:51:56 +0000635 PhysRegsEverUsed[Reg] = true;
Evan Chengddee8422006-11-15 20:55:15 +0000636 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000637 PhysRegsUsed[Reg] = 0; // It is free and reserved now
638 PhysRegsUseOrder.push_back(Reg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000639 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
640 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000641 if (PhysRegsUsed[*AliasSet] != -2) {
642 PhysRegsUseOrder.push_back(*AliasSet);
643 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
644 PhysRegsEverUsed[*AliasSet] = true;
645 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000646 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000647 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000648 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000649
650 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000651 if (TID.ImplicitDefs) {
652 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
653 *ImplicitDefs; ++ImplicitDefs) {
654 unsigned Reg = *ImplicitDefs;
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000655 bool IsNonAllocatable = PhysRegsUsed[Reg] == -2;
656 if (!IsNonAllocatable) {
657 spillPhysReg(MBB, MI, Reg, true);
658 PhysRegsUseOrder.push_back(Reg);
659 PhysRegsUsed[Reg] = 0; // It is free and reserved now
660 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000661 PhysRegsEverUsed[Reg] = true;
Chris Lattner0648b162005-01-23 22:51:56 +0000662
Jim Laskeycd4317e2006-07-21 21:15:20 +0000663 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
664 *AliasSet; ++AliasSet) {
Chris Lattner45d57882006-09-08 19:03:30 +0000665 if (PhysRegsUsed[*AliasSet] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000666 if (!IsNonAllocatable) {
667 PhysRegsUseOrder.push_back(*AliasSet);
668 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
669 }
Chris Lattner45d57882006-09-08 19:03:30 +0000670 PhysRegsEverUsed[*AliasSet] = true;
671 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000672 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000673 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000674 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000675
Evan Chengddee8422006-11-15 20:55:15 +0000676 SmallVector<unsigned, 8> DeadDefs;
677 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
678 MachineOperand& MO = MI->getOperand(i);
679 if (MO.isRegister() && MO.isDead())
680 DeadDefs.push_back(MO.getReg());
681 }
682
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000683 // Okay, we have allocated all of the source operands and spilled any values
684 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000685 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000686 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000687 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000688 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
689 MachineOperand& MO = MI->getOperand(i);
Evan Cheng5d8062b2006-09-05 20:32:06 +0000690 if (MO.isRegister() && MO.isDef() && MO.getReg() &&
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000691 MRegisterInfo::isVirtualRegister(MO.getReg())) {
692 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000693 unsigned DestPhysReg;
694
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000695 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000696 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000697 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattner0648b162005-01-23 22:51:56 +0000698 PhysRegsEverUsed[DestPhysReg] = true;
Chris Lattnerd5725632003-05-12 03:54:14 +0000699 markVirtRegModified(DestVirtReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000700 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000701 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000702 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000703
Chris Lattner56ddada2004-02-17 17:49:10 +0000704 // If this instruction defines any registers that are immediately dead,
705 // kill them now.
706 //
Evan Chengddee8422006-11-15 20:55:15 +0000707 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
708 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000709 unsigned PhysReg = VirtReg;
710 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
711 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
712 PhysReg = PhysRegSlot;
713 assert(PhysReg != 0);
714 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000715 } else if (PhysRegsUsed[PhysReg] == -2) {
716 // Unallocatable register dead, ignore.
717 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000718 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000719
Chris Lattner56ddada2004-02-17 17:49:10 +0000720 if (PhysReg) {
721 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
722 << " [%reg" << VirtReg
723 << "] is never used, removing it frame live list\n");
724 removePhysReg(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000725 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
726 *AliasSet; ++AliasSet) {
727 if (PhysRegsUsed[*AliasSet] != -2) {
728 DEBUG(std::cerr << " Register " << RegInfo->getName(*AliasSet)
729 << " [%reg" << *AliasSet
730 << "] is never used, removing it frame live list\n");
731 removePhysReg(*AliasSet);
732 }
733 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000734 }
735 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000736
737 // Finally, if this is a noop copy instruction, zap it.
738 unsigned SrcReg, DstReg;
Chris Lattner2ac0d432006-09-03 00:06:08 +0000739 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
740 LV->removeVirtualRegistersKilled(MI);
741 LV->removeVirtualRegistersDead(MI);
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000742 MBB.erase(MI);
Chris Lattner2ac0d432006-09-03 00:06:08 +0000743 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000744 }
745
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000746 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000747
748 // Spill all physical registers holding virtual registers now.
Chris Lattner64667b62004-02-09 01:26:13 +0000749 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000750 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Chris Lattner64667b62004-02-09 01:26:13 +0000751 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000752 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +0000753 else
754 removePhysReg(i);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000755
Chris Lattner9a5ef202005-11-09 05:28:45 +0000756#if 0
757 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +0000758 bool AllOk = true;
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000759 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
760 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +0000761 if (unsigned PR = Virt2PhysRegMap[i]) {
762 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
763 AllOk = false;
764 }
765 assert(AllOk && "Virtual registers still in phys regs?");
766#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000767
Chris Lattner128c2aa2003-08-17 18:01:15 +0000768 // Clear any physical register which appear live at the end of the basic
769 // block, but which do not hold any virtual registers. e.g., the stack
770 // pointer.
771 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000772}
773
Chris Lattner86c69a62002-12-17 03:16:10 +0000774
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000775/// runOnMachineFunction - Register allocate the whole function
776///
777bool RA::runOnMachineFunction(MachineFunction &Fn) {
778 DEBUG(std::cerr << "Machine Function " << "\n");
779 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +0000780 TM = &Fn.getTarget();
781 RegInfo = TM->getRegisterInfo();
Chris Lattner56ddada2004-02-17 17:49:10 +0000782 LV = &getAnalysis<LiveVariables>();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000783
Chris Lattner0648b162005-01-23 22:51:56 +0000784 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
785 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
786 Fn.setUsedPhysRegs(PhysRegsEverUsed);
787
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000788 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +0000789
790 // At various places we want to efficiently check to see whether a register
791 // is allocatable. To handle this, we mark all unallocatable registers as
792 // being pinned down, permanently.
793 {
794 std::vector<bool> Allocable = RegInfo->getAllocatableSet(Fn);
795 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
796 if (!Allocable[i])
797 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
798 }
Chris Lattner64667b62004-02-09 01:26:13 +0000799
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000800 // initialize the virtual->physical register map to have a 'null'
801 // mapping for all virtual registers
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +0000802 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
Chris Lattnerecea5632004-02-09 02:12:04 +0000803
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000804 // Loop over all of the basic blocks, eliminating virtual register references
805 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
806 MBB != MBBe; ++MBB)
807 AllocateBasicBlock(*MBB);
808
Chris Lattner580f9be2002-12-28 20:40:43 +0000809 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000810 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +0000811 VirtRegModified.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +0000812 Virt2PhysRegMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000813 return true;
814}
815
Chris Lattneref09c632004-01-31 21:27:19 +0000816FunctionPass *llvm::createLocalRegisterAllocator() {
Chris Lattner580f9be2002-12-28 20:40:43 +0000817 return new RA();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000818}