blob: 66ba86ac94c55fa4f6dcc942a3b1b537e8eb136d [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;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +000048 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1),
49 atEndOfBlock(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000050 private:
51 const TargetMachine *TM;
52 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000053 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000054 const TargetRegisterInfo *TRI;
55 const TargetInstrInfo *TII;
56
57 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
58 // values are spilled.
59 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
60
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000061 // Everything we know about a live virtual register.
62 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000063 MachineInstr *LastUse; // Last instr to use reg.
64 unsigned PhysReg; // Currently held here.
65 unsigned short LastOpNum; // OpNum on LastUse.
66 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000067
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000068 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
69 Dirty(false) {
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000070 assert(p && "Don't create LiveRegs without a PhysReg");
71 }
72 };
73
74 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
75
76 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000077 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000078 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000079
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000080 // RegState - Track the state of a physical register.
81 enum RegState {
82 // A disabled register is not available for allocation, but an alias may
83 // be in use. A register can only be moved out of the disabled state if
84 // all aliases are disabled.
85 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000086
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000087 // A free register is not currently in use and can be allocated
88 // immediately without checking aliases.
89 regFree,
90
91 // A reserved register has been assigned expolicitly (e.g., setting up a
92 // call parameter), and it remains reserved until it is used.
93 regReserved
94
95 // A register state may also be a virtual register number, indication that
96 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000097 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000098 };
99
100 // PhysRegState - One of the RegState enums, or a virtreg.
101 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000102
103 // UsedInInstr - BitVector of physregs that are used in the current
104 // instruction, and so cannot be allocated.
105 BitVector UsedInInstr;
106
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000107 // ReservedRegs - vector of reserved physical registers.
108 BitVector ReservedRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000109
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000110 // atEndOfBlock - This flag is set after allocating all instructions in a
111 // block, before emitting final spills. When it is set, LiveRegMap is no
112 // longer updated properly sonce it will be cleared anyway.
113 bool atEndOfBlock;
114
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000115 public:
116 virtual const char *getPassName() const {
117 return "Fast Register Allocator";
118 }
119
120 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.setPreservesCFG();
122 AU.addRequiredID(PHIEliminationID);
123 AU.addRequiredID(TwoAddressInstructionPassID);
124 MachineFunctionPass::getAnalysisUsage(AU);
125 }
126
127 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000128 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000129 void AllocateBasicBlock(MachineBasicBlock &MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000130 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000131 void addKillFlag(LiveRegMap::iterator i);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000132 void killVirtReg(LiveRegMap::iterator i);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000133 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000134 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000135 LiveRegMap::iterator i, bool isKill);
136 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000137 unsigned VirtReg, bool isKill);
138 void killPhysReg(unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000139 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000140 unsigned PhysReg, bool isKill);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000141 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg,
142 unsigned PhysReg);
143 LiveRegMap::iterator allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000144 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000145 unsigned defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000146 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000147 unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000148 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000149 void reservePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
150 unsigned PhysReg);
151 void spillAll(MachineBasicBlock &MBB, MachineInstr *MI);
152 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000153 };
154 char RAFast::ID = 0;
155}
156
157/// getStackSpaceFor - This allocates space for the specified virtual register
158/// to be held on the stack.
159int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
160 // Find the location Reg would belong...
161 int SS = StackSlotForVirtReg[VirtReg];
162 if (SS != -1)
163 return SS; // Already has space allocated?
164
165 // Allocate a new stack object for this spill location...
166 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
167 RC->getAlignment());
168
169 // Assign the slot.
170 StackSlotForVirtReg[VirtReg] = FrameIdx;
171 return FrameIdx;
172}
173
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000174/// addKillFlag - Set kill flags on last use of a virtual register.
175void RAFast::addKillFlag(LiveRegMap::iterator lri) {
176 assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register");
177 const LiveReg &LR = lri->second;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000178 if (LR.LastUse) {
179 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000180 if (MO.isDef())
181 MO.setIsDead();
182 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
183 MO.setIsKill();
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000184 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000185}
186
187/// killVirtReg - Mark virtreg as no longer available.
188void RAFast::killVirtReg(LiveRegMap::iterator lri) {
189 addKillFlag(lri);
190 const LiveReg &LR = lri->second;
191 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
192 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000193 // Erase from LiveVirtRegs unless we're at the end of the block when
194 // everything will be bulk erased.
195 if (!atEndOfBlock)
196 LiveVirtRegs.erase(lri);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000197}
198
199/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000200void RAFast::killVirtReg(unsigned VirtReg) {
201 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
202 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000203 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
204 if (lri != LiveVirtRegs.end())
205 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000206}
207
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000208/// spillVirtReg - This method spills the value specified by VirtReg into the
209/// corresponding stack slot if needed. If isKill is set, the register is also
210/// killed.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000211void RAFast::spillVirtReg(MachineBasicBlock &MBB,
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000212 MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000213 unsigned VirtReg, bool isKill) {
214 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
215 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000216 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
217 assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register");
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000218 spillVirtReg(MBB, MI, lri, isKill);
219}
220
221/// spillVirtReg - Do the actual work of spilling.
222void RAFast::spillVirtReg(MachineBasicBlock &MBB,
223 MachineBasicBlock::iterator MI,
224 LiveRegMap::iterator lri, bool isKill) {
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000225 LiveReg &LR = lri->second;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000226 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000227
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000228 // If this physreg is used by the instruction, we want to kill it on the
229 // instruction, not on the spill.
230 bool spillKill = isKill && LR.LastUse != MI;
231
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000232 if (LR.Dirty) {
233 LR.Dirty = false;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000234 DEBUG(dbgs() << "Spilling %reg" << lri->first
235 << " in " << TRI->getName(LR.PhysReg));
236 const TargetRegisterClass *RC = MRI->getRegClass(lri->first);
237 int FrameIndex = getStackSpaceFor(lri->first, RC);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000238 DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000239 TII->storeRegToStackSlot(MBB, MI, LR.PhysReg, spillKill,
240 FrameIndex, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000241 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000242
243 if (spillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000244 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000245 else if (!isKill) {
246 MachineInstr *Spill = llvm::prior(MI);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000247 LR.LastUse = Spill;
248 LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000249 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000250 }
251
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000252 if (isKill)
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000253 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000254}
255
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000256/// spillAll - Spill all dirty virtregs without killing them.
257void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) {
258 SmallVector<unsigned, 16> Dirty;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000259 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
260 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000261 if (i->second.Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000262 Dirty.push_back(i->first);
263 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
264 spillVirtReg(MBB, MI, Dirty[i], false);
265}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000266
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000267/// killPhysReg - Kill any virtual register aliased by PhysReg.
268void RAFast::killPhysReg(unsigned PhysReg) {
269 // Fast path for the normal case.
270 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
271 case regDisabled:
272 break;
273 case regFree:
274 return;
275 case regReserved:
276 PhysRegState[PhysReg] = regFree;
277 return;
278 default:
279 killVirtReg(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000280 return;
281 }
282
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000283 // This is a disabled register, we have to check aliases.
284 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
285 unsigned Alias = *AS; ++AS) {
286 switch (unsigned VirtReg = PhysRegState[Alias]) {
287 case regDisabled:
288 case regFree:
289 break;
290 case regReserved:
291 PhysRegState[Alias] = regFree;
292 break;
293 default:
294 killVirtReg(VirtReg);
295 break;
296 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000297 }
298}
299
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000300/// spillPhysReg - Spill any dirty virtual registers that aliases PhysReg. If
301/// isKill is set, they are also killed.
302void RAFast::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
303 unsigned PhysReg, bool isKill) {
304 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
305 case regDisabled:
306 break;
307 case regFree:
308 return;
309 case regReserved:
310 if (isKill)
311 PhysRegState[PhysReg] = regFree;
312 return;
313 default:
314 spillVirtReg(MBB, MI, VirtReg, isKill);
315 return;
316 }
317
318 // This is a disabled register, we have to check aliases.
319 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
320 unsigned Alias = *AS; ++AS) {
321 switch (unsigned VirtReg = PhysRegState[Alias]) {
322 case regDisabled:
323 case regFree:
324 break;
325 case regReserved:
326 if (isKill)
327 PhysRegState[Alias] = regFree;
328 break;
329 default:
330 spillVirtReg(MBB, MI, VirtReg, isKill);
331 break;
332 }
333 }
334}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000335
336/// assignVirtToPhysReg - This method updates local state so that we know
337/// that PhysReg is the proper container for VirtReg now. The physical
338/// register must not be used for anything else when this is called.
339///
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000340RAFast::LiveRegMap::iterator
341RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000342 DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000343 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000344 PhysRegState[PhysReg] = VirtReg;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000345 return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000346}
347
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000348/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000349RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineBasicBlock &MBB,
350 MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000351 unsigned VirtReg,
352 unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000353 const unsigned spillCost = 100;
354 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
355 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000356
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000357 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000358 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
359 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000360
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000361 // Ignore invalid hints.
362 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
363 !RC->contains(Hint) || UsedInInstr.test(Hint)))
364 Hint = 0;
365
366 // If there is no hint, peek at the first use of this register.
367 if (!Hint && !MRI->use_nodbg_empty(VirtReg)) {
368 MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg);
369 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
370 // Copy to physreg -> use physreg as hint.
371 if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
372 SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) &&
373 RC->contains(DstReg) && !UsedInInstr.test(DstReg)) {
374 Hint = DstReg;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000375 DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000376 }
377 }
378
379 // Take hint when possible.
380 if (Hint) {
381 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
382 "Invalid hint should have been cleared");
383 switch(PhysRegState[Hint]) {
384 case regDisabled:
385 case regReserved:
386 break;
387 default:
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000388 spillVirtReg(MBB, MI, PhysRegState[Hint], true);
389 // Fall through.
390 case regFree:
391 return assignVirtToPhysReg(VirtReg, Hint);
392 }
393 }
394
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000395 // First try to find a completely free register.
396 unsigned BestCost = 0, BestReg = 0;
397 bool hasDisabled = false;
398 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
399 unsigned PhysReg = *I;
400 switch(PhysRegState[PhysReg]) {
401 case regDisabled:
402 hasDisabled = true;
403 case regReserved:
404 continue;
405 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000406 if (!UsedInInstr.test(PhysReg))
407 return assignVirtToPhysReg(VirtReg, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000408 continue;
409 default:
410 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000411 if (!BestReg && !UsedInInstr.test(PhysReg))
412 BestReg = PhysReg, BestCost = spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000413 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000414 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000415 }
416
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000417 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000418 << " candidate=" << TRI->getName(BestReg) << "\n");
419
420 // Try to extend the working set for RC if there were any disabled registers.
421 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
422 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
423 unsigned PhysReg = *I;
424 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
425 continue;
426
427 // Calculate the cost of bringing PhysReg into the working set.
428 unsigned Cost=0;
429 bool Impossible = false;
430 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
431 unsigned Alias = *AS; ++AS) {
432 if (UsedInInstr.test(Alias)) {
433 Impossible = true;
434 break;
435 }
436 switch (PhysRegState[Alias]) {
437 case regDisabled:
438 break;
439 case regReserved:
440 Impossible = true;
441 break;
442 case regFree:
443 Cost++;
444 break;
445 default:
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000446 Cost += spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000447 break;
448 }
449 }
450 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000451 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000452 << " cost=" << Cost << "\n");
453 if (!BestReg || Cost < BestCost) {
454 BestReg = PhysReg;
455 BestCost = Cost;
456 if (Cost < spillCost) break;
457 }
458 }
459 }
460
461 if (BestReg) {
462 // BestCost is 0 when all aliases are already disabled.
463 if (BestCost) {
464 if (PhysRegState[BestReg] != regDisabled)
465 spillVirtReg(MBB, MI, PhysRegState[BestReg], true);
466 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000467 // Make sure all aliases are disabled.
468 for (const unsigned *AS = TRI->getAliasSet(BestReg);
469 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000470 switch (PhysRegState[Alias]) {
471 case regDisabled:
472 continue;
473 case regFree:
474 PhysRegState[Alias] = regDisabled;
475 break;
476 default:
477 spillVirtReg(MBB, MI, PhysRegState[Alias], true);
478 PhysRegState[Alias] = regDisabled;
479 break;
480 }
481 }
482 }
483 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000484 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000485 }
486
487 // Nothing we can do.
488 std::string msg;
489 raw_string_ostream Msg(msg);
490 Msg << "Ran out of registers during register allocation!";
491 if (MI->isInlineAsm()) {
492 Msg << "\nPlease check your inline asm statement for "
493 << "invalid constraints:\n";
494 MI->print(Msg, TM);
495 }
496 report_fatal_error(Msg.str());
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000497 return LiveVirtRegs.end();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000498}
499
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000500/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
501unsigned RAFast::defineVirtReg(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);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000508 else
509 addKillFlag(lri); // Kill before redefine.
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000510 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000511 LR.LastUse = MI;
512 LR.LastOpNum = OpNum;
513 LR.Dirty = true;
514 UsedInInstr.set(LR.PhysReg);
515 return LR.PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000516}
517
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000518/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
519unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000520 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000521 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
522 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000523 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
524 if (lri == LiveVirtRegs.end()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000525 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
526 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000527 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000528 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000529 << TRI->getName(lri->second.PhysReg) << "\n");
530 TII->loadRegFromStackSlot(MBB, MI, lri->second.PhysReg, FrameIndex, RC,
531 TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000532 ++NumLoads;
533 }
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000534 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000535 LR.LastUse = MI;
536 LR.LastOpNum = OpNum;
537 UsedInInstr.set(LR.PhysReg);
538 return LR.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000539}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000540
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000541/// reservePhysReg - Mark PhysReg as reserved. This is very similar to
Jakob Stoklund Olesen63e34f62010-05-13 00:19:39 +0000542/// defineVirtReg except the physreg is reserved instead of allocated.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000543void RAFast::reservePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
544 unsigned PhysReg) {
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000545 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000546 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
547 case regDisabled:
548 break;
549 case regFree:
550 PhysRegState[PhysReg] = regReserved;
551 return;
552 case regReserved:
553 return;
554 default:
555 spillVirtReg(MBB, MI, VirtReg, true);
556 PhysRegState[PhysReg] = regReserved;
557 return;
558 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000559
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000560 // This is a disabled register, disable all aliases.
561 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
562 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000563 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000564 switch (unsigned VirtReg = PhysRegState[Alias]) {
565 case regDisabled:
566 case regFree:
567 break;
568 case regReserved:
569 // is a super register already reserved?
570 if (TRI->isSuperRegister(PhysReg, Alias))
571 return;
572 break;
573 default:
574 spillVirtReg(MBB, MI, VirtReg, true);
575 break;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000576 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000577 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000578 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000579 PhysRegState[PhysReg] = regReserved;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000580}
581
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000582// setPhysReg - Change MO the refer the PhysReg, considering subregs.
583void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
584 if (unsigned Idx = MO.getSubReg()) {
585 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
586 MO.setSubReg(0);
587 } else
588 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000589}
590
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000591void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000592 DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000593
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000594 atEndOfBlock = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000595 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000596 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000597
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000598 MachineBasicBlock::iterator MII = MBB.begin();
599
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000600 // Add live-in registers as live.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000601 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000602 E = MBB.livein_end(); I != E; ++I)
603 reservePhysReg(MBB, MII, *I);
604
605 SmallVector<unsigned, 8> VirtKills, PhysKills, PhysDefs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000606
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000607 // Otherwise, sequentially allocate each instruction in the MBB.
608 while (MII != MBB.end()) {
609 MachineInstr *MI = MII++;
610 const TargetInstrDesc &TID = MI->getDesc();
611 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000612 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000613 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
614 if (PhysRegState[Reg] == regDisabled) continue;
615 dbgs() << " " << TRI->getName(Reg);
616 switch(PhysRegState[Reg]) {
617 case regFree:
618 break;
619 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000620 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000621 break;
622 default:
623 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000624 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000625 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000626 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000627 "Bad inverse map");
628 break;
629 }
630 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000631 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000632 // Check that LiveVirtRegs is the inverse.
633 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
634 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000635 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
636 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000637 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000638 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000639 assert(PhysRegState[i->second.PhysReg] == i->first &&
640 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000641 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000642 });
643
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000644 // Debug values are not allowed to change codegen in any way.
645 if (MI->isDebugValue()) {
646 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
647 MachineOperand &MO = MI->getOperand(i);
648 if (!MO.isReg()) continue;
649 unsigned Reg = MO.getReg();
650 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000651 LiveRegMap::iterator lri = LiveVirtRegs.find(Reg);
652 if (lri != LiveVirtRegs.end())
653 setPhysReg(MO, lri->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000654 else
655 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000656 }
657 // Next instruction.
658 continue;
659 }
660
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000661 // If this is a copy, we may be able to coalesce.
662 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
663 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
664 CopySrc = CopyDst = 0;
665
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000666 // Track registers used by instruction.
667 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000668 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000669
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000670 // First scan.
671 // Mark physreg uses and early clobbers as used.
672 // Collect PhysKills.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000673 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
674 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000675 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000676 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000677 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg) ||
678 ReservedRegs.test(Reg)) continue;
679 if (MO.isUse()) {
Jakob Stoklund Olesen63e34f62010-05-13 00:19:39 +0000680#ifndef NDEBUG
681 // We are using a physreg directly. It had better not be clobbered by a
682 // virtreg.
683 assert(PhysRegState[Reg] <= regReserved && "Using clobbered physreg");
684 if (PhysRegState[Reg] == regDisabled)
685 for (const unsigned *AS = TRI->getAliasSet(Reg);
686 unsigned Alias = *AS; ++AS)
687 assert(PhysRegState[Alias] <= regReserved &&
688 "Physreg alias was clobbered");
689#endif
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000690 PhysKills.push_back(Reg); // Any clean physreg use is a kill.
691 UsedInInstr.set(Reg);
692 } else if (MO.isEarlyClobber()) {
693 spillPhysReg(MBB, MI, Reg, true);
694 UsedInInstr.set(Reg);
695 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000696 }
697 }
698
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000699 // Second scan.
700 // Allocate virtreg uses and early clobbers.
701 // Collect VirtKills
702 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
703 MachineOperand &MO = MI->getOperand(i);
704 if (!MO.isReg()) continue;
705 unsigned Reg = MO.getReg();
706 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
707 if (MO.isUse()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000708 unsigned PhysReg = reloadVirtReg(MBB, MI, i, Reg, CopyDst);
709 if (CopySrc == Reg)
710 CopySrc = PhysReg;
711 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000712 if (MO.isKill())
713 VirtKills.push_back(Reg);
714 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000715 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, 0);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000716 setPhysReg(MO, PhysReg);
717 PhysDefs.push_back(PhysReg);
718 }
719 }
720
721 // Process virtreg kills
722 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
723 killVirtReg(VirtKills[i]);
724 VirtKills.clear();
725
726 // Process physreg kills
727 for (unsigned i = 0, e = PhysKills.size(); i != e; ++i)
728 killPhysReg(PhysKills[i]);
729 PhysKills.clear();
730
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000731 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000732
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000733 // Track registers defined by instruction - early clobbers at this point.
734 UsedInInstr.reset();
735 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
736 unsigned PhysReg = PhysDefs[i];
737 UsedInInstr.set(PhysReg);
738 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
739 unsigned Alias = *AS; ++AS)
740 UsedInInstr.set(Alias);
741 }
742
743 // Third scan.
744 // Allocate defs and collect dead defs.
745 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
746 MachineOperand &MO = MI->getOperand(i);
747 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
748 unsigned Reg = MO.getReg();
749
750 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
751 if (ReservedRegs.test(Reg)) continue;
752 if (MO.isImplicit())
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000753 spillPhysReg(MBB, MI, Reg, true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000754 else
755 reservePhysReg(MBB, MI, Reg);
756 if (MO.isDead())
757 PhysKills.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000758 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000759 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000760 if (MO.isDead())
761 VirtKills.push_back(Reg);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000762 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, CopySrc);
763 if (CopyDst == Reg)
764 CopyDst = PhysReg;
765 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000766 }
767
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000768 // Spill all dirty virtregs before a call, in case of an exception.
769 if (TID.isCall()) {
770 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
771 spillAll(MBB, MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000772 }
773
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000774 // Process virtreg deads.
775 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
776 killVirtReg(VirtKills[i]);
777 VirtKills.clear();
778
779 // Process physreg deads.
780 for (unsigned i = 0, e = PhysKills.size(); i != e; ++i)
781 killPhysReg(PhysKills[i]);
782 PhysKills.clear();
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000783
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000784 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000785
786 DEBUG(dbgs() << "<< " << *MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000787 }
788
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000789 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000790 atEndOfBlock = true;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000791 DEBUG(dbgs() << "Killing live registers at end of block.\n");
792 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000793 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
794 i != e; ++i)
795 spillVirtReg(MBB, MI, i, true);
796 LiveVirtRegs.clear();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000797
798 DEBUG(MBB.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000799}
800
801/// runOnMachineFunction - Register allocate the whole function
802///
803bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000804 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
805 << "********** Function: "
806 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000807 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000808 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000809 TM = &Fn.getTarget();
810 TRI = TM->getRegisterInfo();
811 TII = TM->getInstrInfo();
812
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000813 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000814 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000815
816 // initialize the virtual->physical register map to have a 'null'
817 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000818 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000819 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000820
821 // Loop over all of the basic blocks, eliminating virtual register references
822 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
823 MBB != MBBe; ++MBB)
824 AllocateBasicBlock(*MBB);
825
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000826 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000827 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000828
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000829 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000830 return true;
831}
832
833FunctionPass *llvm::createFastRegisterAllocator() {
834 return new RAFast();
835}