blob: 6bc492ed38ca7879f179e94164eb0f35da6c7146 [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;
52 const TargetRegisterInfo *TRI;
53 const TargetInstrInfo *TII;
54
55 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
56 // values are spilled.
57 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
58
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000059 // Virt2PhysMap - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000060 // that is currently available in a physical register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000061 DenseMap<unsigned, unsigned> Virt2PhysMap;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000062
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000063 // RegState - Track the state of a physical register.
64 enum RegState {
65 // A disabled register is not available for allocation, but an alias may
66 // be in use. A register can only be moved out of the disabled state if
67 // all aliases are disabled.
68 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000069
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000070 // A free register is not currently in use and can be allocated
71 // immediately without checking aliases.
72 regFree,
73
74 // A reserved register has been assigned expolicitly (e.g., setting up a
75 // call parameter), and it remains reserved until it is used.
76 regReserved
77
78 // A register state may also be a virtual register number, indication that
79 // the physical register is currently allocated to a virtual register. In
80 // that case, Virt2PhysMap contains the inverse mapping.
81 };
82
83 // PhysRegState - One of the RegState enums, or a virtreg.
84 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000085
86 // UsedInInstr - BitVector of physregs that are used in the current
87 // instruction, and so cannot be allocated.
88 BitVector UsedInInstr;
89
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000090 // PhysRegDirty - A bit is set for each physreg that holds a dirty virtual
91 // register. Bits for physregs that are not mapped to a virtual register are
92 // invalid.
93 BitVector PhysRegDirty;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000094
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000095 // ReservedRegs - vector of reserved physical registers.
96 BitVector ReservedRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000097
98 public:
99 virtual const char *getPassName() const {
100 return "Fast Register Allocator";
101 }
102
103 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
104 AU.setPreservesCFG();
105 AU.addRequiredID(PHIEliminationID);
106 AU.addRequiredID(TwoAddressInstructionPassID);
107 MachineFunctionPass::getAnalysisUsage(AU);
108 }
109
110 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000111 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000112 void AllocateBasicBlock(MachineBasicBlock &MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000113 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000114 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000115 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000116 unsigned VirtReg, bool isKill);
117 void killPhysReg(unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000118 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000119 unsigned PhysReg, bool isKill);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000120 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000121 unsigned allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
122 unsigned VirtReg);
123 unsigned defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
124 unsigned VirtReg);
125 unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
126 unsigned VirtReg);
127 void reservePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
128 unsigned PhysReg);
129 void spillAll(MachineBasicBlock &MBB, MachineInstr *MI);
130 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000131 };
132 char RAFast::ID = 0;
133}
134
135/// getStackSpaceFor - This allocates space for the specified virtual register
136/// to be held on the stack.
137int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
138 // Find the location Reg would belong...
139 int SS = StackSlotForVirtReg[VirtReg];
140 if (SS != -1)
141 return SS; // Already has space allocated?
142
143 // Allocate a new stack object for this spill location...
144 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
145 RC->getAlignment());
146
147 // Assign the slot.
148 StackSlotForVirtReg[VirtReg] = FrameIdx;
149 return FrameIdx;
150}
151
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000152/// killVirtReg - Mark virtreg as no longer available.
153void RAFast::killVirtReg(unsigned VirtReg) {
154 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
155 "killVirtReg needs a virtual register");
156 DEBUG(dbgs() << " Killing %reg" << VirtReg << "\n");
157 DenseMap<unsigned,unsigned>::iterator i = Virt2PhysMap.find(VirtReg);
158 if (i == Virt2PhysMap.end()) return;
159 unsigned PhysReg = i->second;
160 assert(PhysRegState[PhysReg] == VirtReg && "Broken RegState mapping");
161 PhysRegState[PhysReg] = regFree;
162 Virt2PhysMap.erase(i);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000163}
164
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000165/// spillVirtReg - This method spills the value specified by VirtReg into the
166/// corresponding stack slot if needed. If isKill is set, the register is also
167/// killed.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000168void RAFast::spillVirtReg(MachineBasicBlock &MBB,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000169 MachineBasicBlock::iterator I,
170 unsigned VirtReg, bool isKill) {
171 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
172 "Spilling a physical register is illegal!");
173 DenseMap<unsigned,unsigned>::iterator i = Virt2PhysMap.find(VirtReg);
174 assert(i != Virt2PhysMap.end() && "Spilling unmapped virtual register");
175 unsigned PhysReg = i->second;
176 assert(PhysRegState[PhysReg] == VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000177
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000178 if (PhysRegDirty.test(PhysReg)) {
179 PhysRegDirty.reset(PhysReg);
180 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
181 << " containing %reg" << VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000182 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
183 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000184 DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n");
Evan Cheng746ad692010-05-06 19:06:44 +0000185 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000186 ++NumStores; // Update statistics
187 }
188
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000189 if (isKill) {
190 PhysRegState[PhysReg] = regFree;
191 Virt2PhysMap.erase(i);
192 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000193}
194
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000195/// spillAll - Spill all dirty virtregs without killing them.
196void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) {
197 SmallVector<unsigned, 16> Dirty;
198 for (DenseMap<unsigned,unsigned>::iterator i = Virt2PhysMap.begin(),
199 e = Virt2PhysMap.end(); i != e; ++i)
200 if (PhysRegDirty.test(i->second))
201 Dirty.push_back(i->first);
202 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
203 spillVirtReg(MBB, MI, Dirty[i], false);
204}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000205
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000206/// killPhysReg - Kill any virtual register aliased by PhysReg.
207void RAFast::killPhysReg(unsigned PhysReg) {
208 // Fast path for the normal case.
209 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
210 case regDisabled:
211 break;
212 case regFree:
213 return;
214 case regReserved:
215 PhysRegState[PhysReg] = regFree;
216 return;
217 default:
218 killVirtReg(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000219 return;
220 }
221
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000222 // This is a disabled register, we have to check aliases.
223 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
224 unsigned Alias = *AS; ++AS) {
225 switch (unsigned VirtReg = PhysRegState[Alias]) {
226 case regDisabled:
227 case regFree:
228 break;
229 case regReserved:
230 PhysRegState[Alias] = regFree;
231 break;
232 default:
233 killVirtReg(VirtReg);
234 break;
235 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000236 }
237}
238
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000239/// spillPhysReg - Spill any dirty virtual registers that aliases PhysReg. If
240/// isKill is set, they are also killed.
241void RAFast::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
242 unsigned PhysReg, bool isKill) {
243 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
244 case regDisabled:
245 break;
246 case regFree:
247 return;
248 case regReserved:
249 if (isKill)
250 PhysRegState[PhysReg] = regFree;
251 return;
252 default:
253 spillVirtReg(MBB, MI, VirtReg, isKill);
254 return;
255 }
256
257 // This is a disabled register, we have to check aliases.
258 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
259 unsigned Alias = *AS; ++AS) {
260 switch (unsigned VirtReg = PhysRegState[Alias]) {
261 case regDisabled:
262 case regFree:
263 break;
264 case regReserved:
265 if (isKill)
266 PhysRegState[Alias] = regFree;
267 break;
268 default:
269 spillVirtReg(MBB, MI, VirtReg, isKill);
270 break;
271 }
272 }
273}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000274
275/// assignVirtToPhysReg - This method updates local state so that we know
276/// that PhysReg is the proper container for VirtReg now. The physical
277/// register must not be used for anything else when this is called.
278///
279void RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000280 DEBUG(dbgs() << " Assigning %reg" << VirtReg << " to "
281 << TRI->getName(PhysReg) << "\n");
282 Virt2PhysMap.insert(std::make_pair(VirtReg, PhysReg));
283 PhysRegState[PhysReg] = VirtReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000284}
285
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000286/// allocVirtReg - Allocate a physical register for VirtReg.
287unsigned RAFast::allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
288 unsigned VirtReg) {
289 const unsigned spillCost = 100;
290 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
291 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000292
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000293 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
294 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
295 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000296
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000297 // First try to find a completely free register.
298 unsigned BestCost = 0, BestReg = 0;
299 bool hasDisabled = false;
300 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
301 unsigned PhysReg = *I;
302 switch(PhysRegState[PhysReg]) {
303 case regDisabled:
304 hasDisabled = true;
305 case regReserved:
306 continue;
307 case regFree:
308 if (!UsedInInstr.test(PhysReg)) {
309 assignVirtToPhysReg(VirtReg, PhysReg);
310 return PhysReg;
311 }
312 continue;
313 default:
314 // Grab the first spillable register we meet.
315 if (!BestReg && !UsedInInstr.test(PhysReg)) {
316 BestReg = PhysReg;
317 BestCost = PhysRegDirty.test(PhysReg) ? spillCost : 1;
318 }
319 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000320 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000321 }
322
323 DEBUG(dbgs() << " Allocating %reg" << VirtReg << " from " << RC->getName()
324 << " candidate=" << TRI->getName(BestReg) << "\n");
325
326 // Try to extend the working set for RC if there were any disabled registers.
327 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
328 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
329 unsigned PhysReg = *I;
330 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
331 continue;
332
333 // Calculate the cost of bringing PhysReg into the working set.
334 unsigned Cost=0;
335 bool Impossible = false;
336 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
337 unsigned Alias = *AS; ++AS) {
338 if (UsedInInstr.test(Alias)) {
339 Impossible = true;
340 break;
341 }
342 switch (PhysRegState[Alias]) {
343 case regDisabled:
344 break;
345 case regReserved:
346 Impossible = true;
347 break;
348 case regFree:
349 Cost++;
350 break;
351 default:
352 Cost += PhysRegDirty.test(Alias) ? spillCost : 1;
353 break;
354 }
355 }
356 if (Impossible) continue;
357 DEBUG(dbgs() << " - candidate " << TRI->getName(PhysReg)
358 << " cost=" << Cost << "\n");
359 if (!BestReg || Cost < BestCost) {
360 BestReg = PhysReg;
361 BestCost = Cost;
362 if (Cost < spillCost) break;
363 }
364 }
365 }
366
367 if (BestReg) {
368 // BestCost is 0 when all aliases are already disabled.
369 if (BestCost) {
370 if (PhysRegState[BestReg] != regDisabled)
371 spillVirtReg(MBB, MI, PhysRegState[BestReg], true);
372 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000373 // Make sure all aliases are disabled.
374 for (const unsigned *AS = TRI->getAliasSet(BestReg);
375 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000376 switch (PhysRegState[Alias]) {
377 case regDisabled:
378 continue;
379 case regFree:
380 PhysRegState[Alias] = regDisabled;
381 break;
382 default:
383 spillVirtReg(MBB, MI, PhysRegState[Alias], true);
384 PhysRegState[Alias] = regDisabled;
385 break;
386 }
387 }
388 }
389 }
390 assignVirtToPhysReg(VirtReg, BestReg);
391 return BestReg;
392 }
393
394 // Nothing we can do.
395 std::string msg;
396 raw_string_ostream Msg(msg);
397 Msg << "Ran out of registers during register allocation!";
398 if (MI->isInlineAsm()) {
399 Msg << "\nPlease check your inline asm statement for "
400 << "invalid constraints:\n";
401 MI->print(Msg, TM);
402 }
403 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000404 return 0;
405}
406
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000407/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
408unsigned RAFast::defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
409 unsigned VirtReg) {
410 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
411 "Not a virtual register");
412 unsigned PhysReg = Virt2PhysMap.lookup(VirtReg);
413 if (!PhysReg)
414 PhysReg = allocVirtReg(MBB, MI, VirtReg);
415 UsedInInstr.set(PhysReg);
416 PhysRegDirty.set(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000417 return PhysReg;
418}
419
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000420/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
421unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
422 unsigned VirtReg) {
423 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
424 "Not a virtual register");
425 unsigned PhysReg = Virt2PhysMap.lookup(VirtReg);
426 if (!PhysReg) {
427 PhysReg = allocVirtReg(MBB, MI, VirtReg);
428 PhysRegDirty.reset(PhysReg);
429 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
430 int FrameIndex = getStackSpaceFor(VirtReg, RC);
431 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
432 << TRI->getName(PhysReg) << "\n");
433 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC, TRI);
434 ++NumLoads;
435 }
436 UsedInInstr.set(PhysReg);
437 return PhysReg;
438}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000439
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000440/// reservePhysReg - Mark PhysReg as reserved. This is very similar to
441/// defineVirtReg except the physreg is reverved instead of allocated.
442void RAFast::reservePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
443 unsigned PhysReg) {
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000444 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000445 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
446 case regDisabled:
447 break;
448 case regFree:
449 PhysRegState[PhysReg] = regReserved;
450 return;
451 case regReserved:
452 return;
453 default:
454 spillVirtReg(MBB, MI, VirtReg, true);
455 PhysRegState[PhysReg] = regReserved;
456 return;
457 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000458
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000459 // This is a disabled register, disable all aliases.
460 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
461 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000462 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000463 switch (unsigned VirtReg = PhysRegState[Alias]) {
464 case regDisabled:
465 case regFree:
466 break;
467 case regReserved:
468 // is a super register already reserved?
469 if (TRI->isSuperRegister(PhysReg, Alias))
470 return;
471 break;
472 default:
473 spillVirtReg(MBB, MI, VirtReg, true);
474 break;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000475 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000476 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000477 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000478 PhysRegState[PhysReg] = regReserved;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000479}
480
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000481// setPhysReg - Change MO the refer the PhysReg, considering subregs.
482void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
483 if (unsigned Idx = MO.getSubReg()) {
484 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
485 MO.setSubReg(0);
486 } else
487 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000488}
489
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000490void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000491 DEBUG(dbgs() << "\nBB#" << MBB.getNumber() << ", "<< MBB.getName() << "\n");
492
493 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
494 assert(Virt2PhysMap.empty() && "Mapping not cleared form last block?");
495 PhysRegDirty.reset();
496
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000497 MachineBasicBlock::iterator MII = MBB.begin();
498
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000499 // Add live-in registers as live.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000500 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000501 E = MBB.livein_end(); I != E; ++I)
502 reservePhysReg(MBB, MII, *I);
503
504 SmallVector<unsigned, 8> VirtKills, PhysKills, PhysDefs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000505
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000506 // Otherwise, sequentially allocate each instruction in the MBB.
507 while (MII != MBB.end()) {
508 MachineInstr *MI = MII++;
509 const TargetInstrDesc &TID = MI->getDesc();
510 DEBUG({
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000511 dbgs() << "\nStarting RegAlloc of: " << *MI << "Working set:";
512 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
513 if (PhysRegState[Reg] == regDisabled) continue;
514 dbgs() << " " << TRI->getName(Reg);
515 switch(PhysRegState[Reg]) {
516 case regFree:
517 break;
518 case regReserved:
519 dbgs() << "(resv)";
520 break;
521 default:
522 dbgs() << "=%reg" << PhysRegState[Reg];
523 if (PhysRegDirty.test(Reg))
524 dbgs() << "*";
525 assert(Virt2PhysMap.lookup(PhysRegState[Reg]) == Reg &&
526 "Bad inverse map");
527 break;
528 }
529 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000530 dbgs() << '\n';
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000531 // Check that Virt2PhysMap is the inverse.
532 for (DenseMap<unsigned,unsigned>::iterator i = Virt2PhysMap.begin(),
533 e = Virt2PhysMap.end(); i != e; ++i) {
534 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
535 "Bad map key");
536 assert(TargetRegisterInfo::isPhysicalRegister(i->second) &&
537 "Bad map value");
538 assert(PhysRegState[i->second] == i->first && "Bad inverse map");
539 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000540 });
541
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000542 // Debug values are not allowed to change codegen in any way.
543 if (MI->isDebugValue()) {
544 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
545 MachineOperand &MO = MI->getOperand(i);
546 if (!MO.isReg()) continue;
547 unsigned Reg = MO.getReg();
548 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
549 // This may be 0 if the register is currently spilled. Tough.
550 setPhysReg(MO, Virt2PhysMap.lookup(Reg));
551 }
552 // Next instruction.
553 continue;
554 }
555
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000556 // Track registers used by instruction.
557 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000558 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000559
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000560 // First scan.
561 // Mark physreg uses and early clobbers as used.
562 // Collect PhysKills.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000563 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
564 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000565 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000566
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000567 // FIXME: For now, don't trust kill flags
568 if (MO.isUse()) MO.setIsKill(false);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000569
570 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000571 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg) ||
572 ReservedRegs.test(Reg)) continue;
573 if (MO.isUse()) {
574 PhysKills.push_back(Reg); // Any clean physreg use is a kill.
575 UsedInInstr.set(Reg);
576 } else if (MO.isEarlyClobber()) {
577 spillPhysReg(MBB, MI, Reg, true);
578 UsedInInstr.set(Reg);
579 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000580 }
581 }
582
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000583 // Second scan.
584 // Allocate virtreg uses and early clobbers.
585 // Collect VirtKills
586 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
587 MachineOperand &MO = MI->getOperand(i);
588 if (!MO.isReg()) continue;
589 unsigned Reg = MO.getReg();
590 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
591 if (MO.isUse()) {
592 setPhysReg(MO, reloadVirtReg(MBB, MI, Reg));
593 if (MO.isKill())
594 VirtKills.push_back(Reg);
595 } else if (MO.isEarlyClobber()) {
596 unsigned PhysReg = defineVirtReg(MBB, MI, Reg);
597 setPhysReg(MO, PhysReg);
598 PhysDefs.push_back(PhysReg);
599 }
600 }
601
602 // Process virtreg kills
603 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
604 killVirtReg(VirtKills[i]);
605 VirtKills.clear();
606
607 // Process physreg kills
608 for (unsigned i = 0, e = PhysKills.size(); i != e; ++i)
609 killPhysReg(PhysKills[i]);
610 PhysKills.clear();
611
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000612 MF->getRegInfo().addPhysRegsUsed(UsedInInstr);
613
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000614 // Track registers defined by instruction - early clobbers at this point.
615 UsedInInstr.reset();
616 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
617 unsigned PhysReg = PhysDefs[i];
618 UsedInInstr.set(PhysReg);
619 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
620 unsigned Alias = *AS; ++AS)
621 UsedInInstr.set(Alias);
622 }
623
624 // Third scan.
625 // Allocate defs and collect dead defs.
626 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
627 MachineOperand &MO = MI->getOperand(i);
628 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
629 unsigned Reg = MO.getReg();
630
631 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
632 if (ReservedRegs.test(Reg)) continue;
633 if (MO.isImplicit())
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000634 spillPhysReg(MBB, MI, Reg, true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000635 else
636 reservePhysReg(MBB, MI, Reg);
637 if (MO.isDead())
638 PhysKills.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000639 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000640 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000641 if (MO.isDead())
642 VirtKills.push_back(Reg);
643 setPhysReg(MO, defineVirtReg(MBB, MI, Reg));
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000644 }
645
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000646 // Spill all dirty virtregs before a call, in case of an exception.
647 if (TID.isCall()) {
648 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
649 spillAll(MBB, MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000650 }
651
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000652 // Process virtreg deads.
653 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
654 killVirtReg(VirtKills[i]);
655 VirtKills.clear();
656
657 // Process physreg deads.
658 for (unsigned i = 0, e = PhysKills.size(); i != e; ++i)
659 killPhysReg(PhysKills[i]);
660 PhysKills.clear();
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000661
662 MF->getRegInfo().addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000663 }
664
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000665 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000666 DEBUG(dbgs() << "Killing live registers at end of block.\n");
667 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
668 while (!Virt2PhysMap.empty())
669 spillVirtReg(MBB, MI, Virt2PhysMap.begin()->first, true);
670
671 DEBUG(MBB.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000672}
673
674/// runOnMachineFunction - Register allocate the whole function
675///
676bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
677 DEBUG(dbgs() << "Machine Function\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000678 DEBUG(Fn.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000679 MF = &Fn;
680 TM = &Fn.getTarget();
681 TRI = TM->getRegisterInfo();
682 TII = TM->getInstrInfo();
683
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000684 PhysRegDirty.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000685 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000686 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000687
688 // initialize the virtual->physical register map to have a 'null'
689 // mapping for all virtual registers
690 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
691 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000692
693 // Loop over all of the basic blocks, eliminating virtual register references
694 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
695 MBB != MBBe; ++MBB)
696 AllocateBasicBlock(*MBB);
697
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000698 // Make sure the set of used physregs is closed under subreg operations.
699 MF->getRegInfo().closePhysRegsUsed(*TRI);
700
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000701 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000702 return true;
703}
704
705FunctionPass *llvm::createFastRegisterAllocator() {
706 return new RAFast();
707}