blob: 0b03efffd65f1ddb4e91f98121bf94f31ae2891c [file] [log] [blame]
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
15#define DEBUG_TYPE "regalloc"
16#include "llvm/BasicBlock.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/RegAllocRegistry.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/ADT/DenseMap.h"
30#include "llvm/ADT/IndexedMap.h"
31#include "llvm/ADT/SmallSet.h"
32#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
35#include <algorithm>
36using namespace llvm;
37
38STATISTIC(NumStores, "Number of stores added");
39STATISTIC(NumLoads , "Number of loads added");
40
41static RegisterRegAlloc
42 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
43
44namespace {
45 class RAFast : public MachineFunctionPass {
46 public:
47 static char ID;
48 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
49 private:
50 const TargetMachine *TM;
51 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000052 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000053 const TargetRegisterInfo *TRI;
54 const TargetInstrInfo *TII;
55
56 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
57 // values are spilled.
58 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
59
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000060 // Everything we know about a live virtual register.
61 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000062 MachineInstr *LastUse; // Last instr to use reg.
63 unsigned PhysReg; // Currently held here.
64 unsigned short LastOpNum; // OpNum on LastUse.
65 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000066
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000067 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
68 Dirty(false) {
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000069 assert(p && "Don't create LiveRegs without a PhysReg");
70 }
71 };
72
73 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
74
75 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000076 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000077 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000078
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000079 // RegState - Track the state of a physical register.
80 enum RegState {
81 // A disabled register is not available for allocation, but an alias may
82 // be in use. A register can only be moved out of the disabled state if
83 // all aliases are disabled.
84 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000085
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000086 // A free register is not currently in use and can be allocated
87 // immediately without checking aliases.
88 regFree,
89
90 // A reserved register has been assigned expolicitly (e.g., setting up a
91 // call parameter), and it remains reserved until it is used.
92 regReserved
93
94 // A register state may also be a virtual register number, indication that
95 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000096 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000097 };
98
99 // PhysRegState - One of the RegState enums, or a virtreg.
100 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000101
102 // UsedInInstr - BitVector of physregs that are used in the current
103 // instruction, and so cannot be allocated.
104 BitVector UsedInInstr;
105
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000106 // ReservedRegs - vector of reserved physical registers.
107 BitVector ReservedRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000108
109 public:
110 virtual const char *getPassName() const {
111 return "Fast Register Allocator";
112 }
113
114 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
115 AU.setPreservesCFG();
116 AU.addRequiredID(PHIEliminationID);
117 AU.addRequiredID(TwoAddressInstructionPassID);
118 MachineFunctionPass::getAnalysisUsage(AU);
119 }
120
121 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000122 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000123 void AllocateBasicBlock(MachineBasicBlock &MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000124 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000125 void addKillFlag(LiveRegMap::iterator i);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000126 void killVirtReg(LiveRegMap::iterator i);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000127 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000128 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000129 unsigned VirtReg, bool isKill);
130 void killPhysReg(unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000131 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000132 unsigned PhysReg, bool isKill);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000133 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg,
134 unsigned PhysReg);
135 LiveRegMap::iterator allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000136 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000137 unsigned defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000138 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000139 unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000140 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000141 void reservePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
142 unsigned PhysReg);
143 void spillAll(MachineBasicBlock &MBB, MachineInstr *MI);
144 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000145 };
146 char RAFast::ID = 0;
147}
148
149/// getStackSpaceFor - This allocates space for the specified virtual register
150/// to be held on the stack.
151int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
152 // Find the location Reg would belong...
153 int SS = StackSlotForVirtReg[VirtReg];
154 if (SS != -1)
155 return SS; // Already has space allocated?
156
157 // Allocate a new stack object for this spill location...
158 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
159 RC->getAlignment());
160
161 // Assign the slot.
162 StackSlotForVirtReg[VirtReg] = FrameIdx;
163 return FrameIdx;
164}
165
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000166/// addKillFlag - Set kill flags on last use of a virtual register.
167void RAFast::addKillFlag(LiveRegMap::iterator lri) {
168 assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register");
169 const LiveReg &LR = lri->second;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000170 if (LR.LastUse) {
171 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000172 if (MO.isDef())
173 MO.setIsDead();
174 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
175 MO.setIsKill();
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000176 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000177}
178
179/// killVirtReg - Mark virtreg as no longer available.
180void RAFast::killVirtReg(LiveRegMap::iterator lri) {
181 addKillFlag(lri);
182 const LiveReg &LR = lri->second;
183 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
184 PhysRegState[LR.PhysReg] = regFree;
185 LiveVirtRegs.erase(lri);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000186}
187
188/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000189void RAFast::killVirtReg(unsigned VirtReg) {
190 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
191 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000192 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
193 if (lri != LiveVirtRegs.end())
194 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000195}
196
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000197/// spillVirtReg - This method spills the value specified by VirtReg into the
198/// corresponding stack slot if needed. If isKill is set, the register is also
199/// killed.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000200void RAFast::spillVirtReg(MachineBasicBlock &MBB,
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000201 MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000202 unsigned VirtReg, bool isKill) {
203 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
204 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000205 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
206 assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register");
207 LiveReg &LR = lri->second;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000208 assert(PhysRegState[LR.PhysReg] == VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000209
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000210 // If this physreg is used by the instruction, we want to kill it on the
211 // instruction, not on the spill.
212 bool spillKill = isKill && LR.LastUse != MI;
213
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000214 if (LR.Dirty) {
215 LR.Dirty = false;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000216 DEBUG(dbgs() << "Spilling register " << TRI->getName(LR.PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000217 << " containing %reg" << VirtReg);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000218 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000219 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000220 DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000221 TII->storeRegToStackSlot(MBB, MI, LR.PhysReg, spillKill,
222 FrameIndex, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000223 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000224
225 if (spillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000226 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000227 else if (!isKill) {
228 MachineInstr *Spill = llvm::prior(MI);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000229 LR.LastUse = Spill;
230 LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000231 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000232 }
233
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000234 if (isKill)
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000235 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000236}
237
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000238/// spillAll - Spill all dirty virtregs without killing them.
239void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) {
240 SmallVector<unsigned, 16> Dirty;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000241 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
242 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000243 if (i->second.Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000244 Dirty.push_back(i->first);
245 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
246 spillVirtReg(MBB, MI, Dirty[i], false);
247}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000248
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000249/// killPhysReg - Kill any virtual register aliased by PhysReg.
250void RAFast::killPhysReg(unsigned PhysReg) {
251 // Fast path for the normal case.
252 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
253 case regDisabled:
254 break;
255 case regFree:
256 return;
257 case regReserved:
258 PhysRegState[PhysReg] = regFree;
259 return;
260 default:
261 killVirtReg(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000262 return;
263 }
264
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000265 // This is a disabled register, we have to check aliases.
266 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
267 unsigned Alias = *AS; ++AS) {
268 switch (unsigned VirtReg = PhysRegState[Alias]) {
269 case regDisabled:
270 case regFree:
271 break;
272 case regReserved:
273 PhysRegState[Alias] = regFree;
274 break;
275 default:
276 killVirtReg(VirtReg);
277 break;
278 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000279 }
280}
281
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000282/// spillPhysReg - Spill any dirty virtual registers that aliases PhysReg. If
283/// isKill is set, they are also killed.
284void RAFast::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
285 unsigned PhysReg, bool isKill) {
286 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
287 case regDisabled:
288 break;
289 case regFree:
290 return;
291 case regReserved:
292 if (isKill)
293 PhysRegState[PhysReg] = regFree;
294 return;
295 default:
296 spillVirtReg(MBB, MI, VirtReg, isKill);
297 return;
298 }
299
300 // This is a disabled register, we have to check aliases.
301 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
302 unsigned Alias = *AS; ++AS) {
303 switch (unsigned VirtReg = PhysRegState[Alias]) {
304 case regDisabled:
305 case regFree:
306 break;
307 case regReserved:
308 if (isKill)
309 PhysRegState[Alias] = regFree;
310 break;
311 default:
312 spillVirtReg(MBB, MI, VirtReg, isKill);
313 break;
314 }
315 }
316}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000317
318/// assignVirtToPhysReg - This method updates local state so that we know
319/// that PhysReg is the proper container for VirtReg now. The physical
320/// register must not be used for anything else when this is called.
321///
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000322RAFast::LiveRegMap::iterator
323RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000324 DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000325 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000326 PhysRegState[PhysReg] = VirtReg;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000327 return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000328}
329
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000330/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000331RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineBasicBlock &MBB,
332 MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000333 unsigned VirtReg,
334 unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000335 const unsigned spillCost = 100;
336 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
337 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000338
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000339 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000340 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
341 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000342
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000343 // Ignore invalid hints.
344 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
345 !RC->contains(Hint) || UsedInInstr.test(Hint)))
346 Hint = 0;
347
348 // If there is no hint, peek at the first use of this register.
349 if (!Hint && !MRI->use_nodbg_empty(VirtReg)) {
350 MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg);
351 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
352 // Copy to physreg -> use physreg as hint.
353 if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
354 SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) &&
355 RC->contains(DstReg) && !UsedInInstr.test(DstReg)) {
356 Hint = DstReg;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000357 DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000358 }
359 }
360
361 // Take hint when possible.
362 if (Hint) {
363 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
364 "Invalid hint should have been cleared");
365 switch(PhysRegState[Hint]) {
366 case regDisabled:
367 case regReserved:
368 break;
369 default:
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000370 spillVirtReg(MBB, MI, PhysRegState[Hint], true);
371 // Fall through.
372 case regFree:
373 return assignVirtToPhysReg(VirtReg, Hint);
374 }
375 }
376
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000377 // First try to find a completely free register.
378 unsigned BestCost = 0, BestReg = 0;
379 bool hasDisabled = false;
380 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
381 unsigned PhysReg = *I;
382 switch(PhysRegState[PhysReg]) {
383 case regDisabled:
384 hasDisabled = true;
385 case regReserved:
386 continue;
387 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000388 if (!UsedInInstr.test(PhysReg))
389 return assignVirtToPhysReg(VirtReg, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000390 continue;
391 default:
392 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000393 if (!BestReg && !UsedInInstr.test(PhysReg))
394 BestReg = PhysReg, BestCost = spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000395 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000396 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000397 }
398
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000399 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000400 << " candidate=" << TRI->getName(BestReg) << "\n");
401
402 // Try to extend the working set for RC if there were any disabled registers.
403 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
404 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
405 unsigned PhysReg = *I;
406 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
407 continue;
408
409 // Calculate the cost of bringing PhysReg into the working set.
410 unsigned Cost=0;
411 bool Impossible = false;
412 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
413 unsigned Alias = *AS; ++AS) {
414 if (UsedInInstr.test(Alias)) {
415 Impossible = true;
416 break;
417 }
418 switch (PhysRegState[Alias]) {
419 case regDisabled:
420 break;
421 case regReserved:
422 Impossible = true;
423 break;
424 case regFree:
425 Cost++;
426 break;
427 default:
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000428 Cost += spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000429 break;
430 }
431 }
432 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000433 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000434 << " cost=" << Cost << "\n");
435 if (!BestReg || Cost < BestCost) {
436 BestReg = PhysReg;
437 BestCost = Cost;
438 if (Cost < spillCost) break;
439 }
440 }
441 }
442
443 if (BestReg) {
444 // BestCost is 0 when all aliases are already disabled.
445 if (BestCost) {
446 if (PhysRegState[BestReg] != regDisabled)
447 spillVirtReg(MBB, MI, PhysRegState[BestReg], true);
448 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000449 // Make sure all aliases are disabled.
450 for (const unsigned *AS = TRI->getAliasSet(BestReg);
451 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000452 switch (PhysRegState[Alias]) {
453 case regDisabled:
454 continue;
455 case regFree:
456 PhysRegState[Alias] = regDisabled;
457 break;
458 default:
459 spillVirtReg(MBB, MI, PhysRegState[Alias], true);
460 PhysRegState[Alias] = regDisabled;
461 break;
462 }
463 }
464 }
465 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000466 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000467 }
468
469 // Nothing we can do.
470 std::string msg;
471 raw_string_ostream Msg(msg);
472 Msg << "Ran out of registers during register allocation!";
473 if (MI->isInlineAsm()) {
474 Msg << "\nPlease check your inline asm statement for "
475 << "invalid constraints:\n";
476 MI->print(Msg, TM);
477 }
478 report_fatal_error(Msg.str());
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000479 return LiveVirtRegs.end();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000480}
481
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000482/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
483unsigned RAFast::defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000484 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000485 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
486 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000487 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
488 if (lri == LiveVirtRegs.end())
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000489 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000490 else
491 addKillFlag(lri); // Kill before redefine.
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000492 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000493 LR.LastUse = MI;
494 LR.LastOpNum = OpNum;
495 LR.Dirty = true;
496 UsedInInstr.set(LR.PhysReg);
497 return LR.PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000498}
499
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000500/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
501unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000502 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
504 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000505 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
506 if (lri == LiveVirtRegs.end()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000507 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
508 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000509 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000510 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000511 << TRI->getName(lri->second.PhysReg) << "\n");
512 TII->loadRegFromStackSlot(MBB, MI, lri->second.PhysReg, FrameIndex, RC,
513 TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000514 ++NumLoads;
515 }
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000516 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000517 LR.LastUse = MI;
518 LR.LastOpNum = OpNum;
519 UsedInInstr.set(LR.PhysReg);
520 return LR.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000521}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000522
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000523/// reservePhysReg - Mark PhysReg as reserved. This is very similar to
Jakob Stoklund Olesen63e34f62010-05-13 00:19:39 +0000524/// defineVirtReg except the physreg is reserved instead of allocated.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000525void RAFast::reservePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
526 unsigned PhysReg) {
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000527 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000528 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
529 case regDisabled:
530 break;
531 case regFree:
532 PhysRegState[PhysReg] = regReserved;
533 return;
534 case regReserved:
535 return;
536 default:
537 spillVirtReg(MBB, MI, VirtReg, true);
538 PhysRegState[PhysReg] = regReserved;
539 return;
540 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000541
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000542 // This is a disabled register, disable all aliases.
543 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
544 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000545 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000546 switch (unsigned VirtReg = PhysRegState[Alias]) {
547 case regDisabled:
548 case regFree:
549 break;
550 case regReserved:
551 // is a super register already reserved?
552 if (TRI->isSuperRegister(PhysReg, Alias))
553 return;
554 break;
555 default:
556 spillVirtReg(MBB, MI, VirtReg, true);
557 break;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000558 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000559 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000560 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000561 PhysRegState[PhysReg] = regReserved;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000562}
563
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000564// setPhysReg - Change MO the refer the PhysReg, considering subregs.
565void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
566 if (unsigned Idx = MO.getSubReg()) {
567 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
568 MO.setSubReg(0);
569 } else
570 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000571}
572
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000573void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000574 DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000575
576 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000577 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000578
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000579 MachineBasicBlock::iterator MII = MBB.begin();
580
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000581 // Add live-in registers as live.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000582 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000583 E = MBB.livein_end(); I != E; ++I)
584 reservePhysReg(MBB, MII, *I);
585
586 SmallVector<unsigned, 8> VirtKills, PhysKills, PhysDefs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000587
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000588 // Otherwise, sequentially allocate each instruction in the MBB.
589 while (MII != MBB.end()) {
590 MachineInstr *MI = MII++;
591 const TargetInstrDesc &TID = MI->getDesc();
592 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000593 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000594 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
595 if (PhysRegState[Reg] == regDisabled) continue;
596 dbgs() << " " << TRI->getName(Reg);
597 switch(PhysRegState[Reg]) {
598 case regFree:
599 break;
600 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000601 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000602 break;
603 default:
604 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000605 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000606 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000607 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000608 "Bad inverse map");
609 break;
610 }
611 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000612 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000613 // Check that LiveVirtRegs is the inverse.
614 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
615 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000616 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
617 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000618 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000619 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000620 assert(PhysRegState[i->second.PhysReg] == i->first &&
621 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000622 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000623 });
624
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000625 // Debug values are not allowed to change codegen in any way.
626 if (MI->isDebugValue()) {
627 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
628 MachineOperand &MO = MI->getOperand(i);
629 if (!MO.isReg()) continue;
630 unsigned Reg = MO.getReg();
631 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000632 LiveRegMap::iterator lri = LiveVirtRegs.find(Reg);
633 if (lri != LiveVirtRegs.end())
634 setPhysReg(MO, lri->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000635 else
636 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000637 }
638 // Next instruction.
639 continue;
640 }
641
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000642 // If this is a copy, we may be able to coalesce.
643 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
644 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
645 CopySrc = CopyDst = 0;
646
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000647 // Track registers used by instruction.
648 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000649 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000650
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000651 // First scan.
652 // Mark physreg uses and early clobbers as used.
653 // Collect PhysKills.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000654 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
655 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000656 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000657
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000658 // FIXME: For now, don't trust kill flags
659 if (MO.isUse()) MO.setIsKill(false);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000660
661 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000662 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg) ||
663 ReservedRegs.test(Reg)) continue;
664 if (MO.isUse()) {
Jakob Stoklund Olesen63e34f62010-05-13 00:19:39 +0000665#ifndef NDEBUG
666 // We are using a physreg directly. It had better not be clobbered by a
667 // virtreg.
668 assert(PhysRegState[Reg] <= regReserved && "Using clobbered physreg");
669 if (PhysRegState[Reg] == regDisabled)
670 for (const unsigned *AS = TRI->getAliasSet(Reg);
671 unsigned Alias = *AS; ++AS)
672 assert(PhysRegState[Alias] <= regReserved &&
673 "Physreg alias was clobbered");
674#endif
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000675 PhysKills.push_back(Reg); // Any clean physreg use is a kill.
676 UsedInInstr.set(Reg);
677 } else if (MO.isEarlyClobber()) {
678 spillPhysReg(MBB, MI, Reg, true);
679 UsedInInstr.set(Reg);
680 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000681 }
682 }
683
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000684 // Second scan.
685 // Allocate virtreg uses and early clobbers.
686 // Collect VirtKills
687 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
688 MachineOperand &MO = MI->getOperand(i);
689 if (!MO.isReg()) continue;
690 unsigned Reg = MO.getReg();
691 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
692 if (MO.isUse()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000693 unsigned PhysReg = reloadVirtReg(MBB, MI, i, Reg, CopyDst);
694 if (CopySrc == Reg)
695 CopySrc = PhysReg;
696 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000697 if (MO.isKill())
698 VirtKills.push_back(Reg);
699 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000700 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, 0);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000701 setPhysReg(MO, PhysReg);
702 PhysDefs.push_back(PhysReg);
703 }
704 }
705
706 // Process virtreg kills
707 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
708 killVirtReg(VirtKills[i]);
709 VirtKills.clear();
710
711 // Process physreg kills
712 for (unsigned i = 0, e = PhysKills.size(); i != e; ++i)
713 killPhysReg(PhysKills[i]);
714 PhysKills.clear();
715
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000716 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000717
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000718 // Track registers defined by instruction - early clobbers at this point.
719 UsedInInstr.reset();
720 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
721 unsigned PhysReg = PhysDefs[i];
722 UsedInInstr.set(PhysReg);
723 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
724 unsigned Alias = *AS; ++AS)
725 UsedInInstr.set(Alias);
726 }
727
728 // Third scan.
729 // Allocate defs and collect dead defs.
730 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
731 MachineOperand &MO = MI->getOperand(i);
732 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
733 unsigned Reg = MO.getReg();
734
735 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
736 if (ReservedRegs.test(Reg)) continue;
737 if (MO.isImplicit())
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000738 spillPhysReg(MBB, MI, Reg, true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000739 else
740 reservePhysReg(MBB, MI, Reg);
741 if (MO.isDead())
742 PhysKills.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000743 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000744 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000745 if (MO.isDead())
746 VirtKills.push_back(Reg);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000747 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, CopySrc);
748 if (CopyDst == Reg)
749 CopyDst = PhysReg;
750 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000751 }
752
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000753 // Spill all dirty virtregs before a call, in case of an exception.
754 if (TID.isCall()) {
755 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
756 spillAll(MBB, MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000757 }
758
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000759 // Process virtreg deads.
760 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
761 killVirtReg(VirtKills[i]);
762 VirtKills.clear();
763
764 // Process physreg deads.
765 for (unsigned i = 0, e = PhysKills.size(); i != e; ++i)
766 killPhysReg(PhysKills[i]);
767 PhysKills.clear();
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000768
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000769 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000770
771 DEBUG(dbgs() << "<< " << *MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000772 }
773
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000774 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000775 DEBUG(dbgs() << "Killing live registers at end of block.\n");
776 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000777 while (!LiveVirtRegs.empty())
778 spillVirtReg(MBB, MI, LiveVirtRegs.begin()->first, true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000779
780 DEBUG(MBB.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000781}
782
783/// runOnMachineFunction - Register allocate the whole function
784///
785bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000786 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
787 << "********** Function: "
788 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000789 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000790 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000791 TM = &Fn.getTarget();
792 TRI = TM->getRegisterInfo();
793 TII = TM->getInstrInfo();
794
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000795 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000796 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000797
798 // initialize the virtual->physical register map to have a 'null'
799 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000800 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000801 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000802
803 // Loop over all of the basic blocks, eliminating virtual register references
804 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
805 MBB != MBBe; ++MBB)
806 AllocateBasicBlock(*MBB);
807
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000808 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000809 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000810
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000811 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000812 return true;
813}
814
815FunctionPass *llvm::createFastRegisterAllocator() {
816 return new RAFast();
817}