blob: db6c7096e3234b689e214d3f8cab37c32930bf32 [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
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +000038static cl::opt<bool> VerifyFastRegalloc("verify-fast-regalloc", cl::Hidden,
39 cl::desc("Verify machine code before fast regalloc"));
40
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000041STATISTIC(NumStores, "Number of stores added");
42STATISTIC(NumLoads , "Number of loads added");
43
44static RegisterRegAlloc
45 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
46
47namespace {
48 class RAFast : public MachineFunctionPass {
49 public:
50 static char ID;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +000051 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1),
52 atEndOfBlock(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000053 private:
54 const TargetMachine *TM;
55 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000056 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000057 const TargetRegisterInfo *TRI;
58 const TargetInstrInfo *TII;
59
60 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
61 // values are spilled.
62 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
63
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000064 // Everything we know about a live virtual register.
65 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000066 MachineInstr *LastUse; // Last instr to use reg.
67 unsigned PhysReg; // Currently held here.
68 unsigned short LastOpNum; // OpNum on LastUse.
69 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000070
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000071 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
72 Dirty(false) {
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000073 assert(p && "Don't create LiveRegs without a PhysReg");
74 }
75 };
76
77 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
78
79 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000080 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000081 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000082
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000083 // RegState - Track the state of a physical register.
84 enum RegState {
85 // A disabled register is not available for allocation, but an alias may
86 // be in use. A register can only be moved out of the disabled state if
87 // all aliases are disabled.
88 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000089
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000090 // A free register is not currently in use and can be allocated
91 // immediately without checking aliases.
92 regFree,
93
94 // A reserved register has been assigned expolicitly (e.g., setting up a
95 // call parameter), and it remains reserved until it is used.
96 regReserved
97
98 // A register state may also be a virtual register number, indication that
99 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000100 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000101 };
102
103 // PhysRegState - One of the RegState enums, or a virtreg.
104 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000105
106 // UsedInInstr - BitVector of physregs that are used in the current
107 // instruction, and so cannot be allocated.
108 BitVector UsedInInstr;
109
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000110 // ReservedRegs - vector of reserved physical registers.
111 BitVector ReservedRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000112
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000113 // atEndOfBlock - This flag is set after allocating all instructions in a
114 // block, before emitting final spills. When it is set, LiveRegMap is no
115 // longer updated properly sonce it will be cleared anyway.
116 bool atEndOfBlock;
117
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000118 public:
119 virtual const char *getPassName() const {
120 return "Fast Register Allocator";
121 }
122
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesCFG();
125 AU.addRequiredID(PHIEliminationID);
126 AU.addRequiredID(TwoAddressInstructionPassID);
127 MachineFunctionPass::getAnalysisUsage(AU);
128 }
129
130 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000131 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000132 void AllocateBasicBlock(MachineBasicBlock &MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000133 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000134 void addKillFlag(LiveRegMap::iterator i);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000135 void killVirtReg(LiveRegMap::iterator i);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000136 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000137 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000138 LiveRegMap::iterator i, bool isKill);
139 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000140 unsigned VirtReg, bool isKill);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000141
142 void usePhysReg(MachineOperand&);
143 void definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
144 unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000145 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg,
146 unsigned PhysReg);
147 LiveRegMap::iterator allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000148 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000149 unsigned defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000150 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000151 unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000152 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000153 void spillAll(MachineBasicBlock &MBB, MachineInstr *MI);
154 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000155 };
156 char RAFast::ID = 0;
157}
158
159/// getStackSpaceFor - This allocates space for the specified virtual register
160/// to be held on the stack.
161int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
162 // Find the location Reg would belong...
163 int SS = StackSlotForVirtReg[VirtReg];
164 if (SS != -1)
165 return SS; // Already has space allocated?
166
167 // Allocate a new stack object for this spill location...
168 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
169 RC->getAlignment());
170
171 // Assign the slot.
172 StackSlotForVirtReg[VirtReg] = FrameIdx;
173 return FrameIdx;
174}
175
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000176/// addKillFlag - Set kill flags on last use of a virtual register.
177void RAFast::addKillFlag(LiveRegMap::iterator lri) {
178 assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register");
179 const LiveReg &LR = lri->second;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000180 if (LR.LastUse) {
181 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000182 if (MO.isDef())
183 MO.setIsDead();
184 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
185 MO.setIsKill();
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000186 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000187}
188
189/// killVirtReg - Mark virtreg as no longer available.
190void RAFast::killVirtReg(LiveRegMap::iterator lri) {
191 addKillFlag(lri);
192 const LiveReg &LR = lri->second;
193 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
194 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000195 // Erase from LiveVirtRegs unless we're at the end of the block when
196 // everything will be bulk erased.
197 if (!atEndOfBlock)
198 LiveVirtRegs.erase(lri);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000199}
200
201/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000202void RAFast::killVirtReg(unsigned VirtReg) {
203 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
204 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000205 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
206 if (lri != LiveVirtRegs.end())
207 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000208}
209
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000210/// spillVirtReg - This method spills the value specified by VirtReg into the
211/// corresponding stack slot if needed. If isKill is set, the register is also
212/// killed.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000213void RAFast::spillVirtReg(MachineBasicBlock &MBB,
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000214 MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000215 unsigned VirtReg, bool isKill) {
216 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
217 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000218 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
219 assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register");
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000220 spillVirtReg(MBB, MI, lri, isKill);
221}
222
223/// spillVirtReg - Do the actual work of spilling.
224void RAFast::spillVirtReg(MachineBasicBlock &MBB,
225 MachineBasicBlock::iterator MI,
226 LiveRegMap::iterator lri, bool isKill) {
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000227 LiveReg &LR = lri->second;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000228 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000229
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000230 // If this physreg is used by the instruction, we want to kill it on the
231 // instruction, not on the spill.
232 bool spillKill = isKill && LR.LastUse != MI;
233
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000234 if (LR.Dirty) {
235 LR.Dirty = false;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000236 DEBUG(dbgs() << "Spilling %reg" << lri->first
237 << " in " << TRI->getName(LR.PhysReg));
238 const TargetRegisterClass *RC = MRI->getRegClass(lri->first);
239 int FrameIndex = getStackSpaceFor(lri->first, RC);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000240 DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000241 TII->storeRegToStackSlot(MBB, MI, LR.PhysReg, spillKill,
242 FrameIndex, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000243 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000244
245 if (spillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000246 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000247 else if (!isKill) {
248 MachineInstr *Spill = llvm::prior(MI);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000249 LR.LastUse = Spill;
250 LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000251 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000252 }
253
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000254 if (isKill)
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000255 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000256}
257
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000258/// spillAll - Spill all dirty virtregs without killing them.
259void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) {
260 SmallVector<unsigned, 16> Dirty;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000261 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
262 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000263 if (i->second.Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000264 Dirty.push_back(i->first);
265 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
266 spillVirtReg(MBB, MI, Dirty[i], false);
267}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000268
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000269/// usePhysReg - Handle the direct use of a physical register.
270/// Check that the register is not used by a virtreg.
271/// Kill the physreg, marking it free.
272/// This may add implicit kills to MO->getParent() and invalidate MO.
273void RAFast::usePhysReg(MachineOperand &MO) {
274 unsigned PhysReg = MO.getReg();
275 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
276 "Bad usePhysReg operand");
277
278 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000279 case regDisabled:
280 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000281 case regReserved:
282 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000283 // Fall through
284 case regFree:
285 UsedInInstr.set(PhysReg);
286 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000287 return;
288 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000289 // The physreg was allocated to a virtual register. That means to value we
290 // wanted has been clobbered.
291 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000292 }
293
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000294 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000295 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
296 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000297 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000298 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000299 break;
300 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000301 assert(TRI->isSuperRegister(PhysReg, Alias) &&
302 "Instruction is not using a subregister of a reserved register");
303 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000304 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000305 UsedInInstr.set(Alias);
306 MO.getParent()->addRegisterKilled(Alias, TRI, true);
307 return;
308 case regFree:
309 if (TRI->isSuperRegister(PhysReg, Alias)) {
310 // Leave the superregister in the working set.
311 UsedInInstr.set(Alias);
312 MO.getParent()->addRegisterKilled(Alias, TRI, true);
313 return;
314 }
315 // Some other alias was in the working set - clear it.
316 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000317 break;
318 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000319 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000320 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000321 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000322
323 // All aliases are disabled, bring register into working set.
324 PhysRegState[PhysReg] = regFree;
325 UsedInInstr.set(PhysReg);
326 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000327}
328
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000329/// definePhysReg - Mark PhysReg as reserved or free after spilling any
330/// virtregs. This is very similar to defineVirtReg except the physreg is
331/// reserved instead of allocated.
332void RAFast::definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
333 unsigned PhysReg, RegState NewState) {
334 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000335 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
336 case regDisabled:
337 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000338 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000339 spillVirtReg(MBB, MI, VirtReg, true);
340 // Fall through.
341 case regFree:
342 case regReserved:
343 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000344 return;
345 }
346
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000347 // This is a disabled register, disable all aliases.
348 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000349 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
350 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000351 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000352 switch (unsigned VirtReg = PhysRegState[Alias]) {
353 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000354 break;
355 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000356 spillVirtReg(MBB, MI, VirtReg, true);
357 // Fall through.
358 case regFree:
359 case regReserved:
360 PhysRegState[Alias] = regDisabled;
361 if (TRI->isSuperRegister(PhysReg, Alias))
362 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000363 break;
364 }
365 }
366}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000367
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000368
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000369/// assignVirtToPhysReg - This method updates local state so that we know
370/// that PhysReg is the proper container for VirtReg now. The physical
371/// register must not be used for anything else when this is called.
372///
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000373RAFast::LiveRegMap::iterator
374RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000375 DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000376 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000377 PhysRegState[PhysReg] = VirtReg;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000378 return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000379}
380
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000381/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000382RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineBasicBlock &MBB,
383 MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000384 unsigned VirtReg,
385 unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000386 const unsigned spillCost = 100;
387 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
388 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000389
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000390 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000391 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
392 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000393
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000394 // Ignore invalid hints.
395 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
396 !RC->contains(Hint) || UsedInInstr.test(Hint)))
397 Hint = 0;
398
399 // If there is no hint, peek at the first use of this register.
400 if (!Hint && !MRI->use_nodbg_empty(VirtReg)) {
401 MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg);
402 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
403 // Copy to physreg -> use physreg as hint.
404 if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
405 SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) &&
406 RC->contains(DstReg) && !UsedInInstr.test(DstReg)) {
407 Hint = DstReg;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000408 DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000409 }
410 }
411
412 // Take hint when possible.
413 if (Hint) {
414 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
415 "Invalid hint should have been cleared");
416 switch(PhysRegState[Hint]) {
417 case regDisabled:
418 case regReserved:
419 break;
420 default:
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000421 spillVirtReg(MBB, MI, PhysRegState[Hint], true);
422 // Fall through.
423 case regFree:
424 return assignVirtToPhysReg(VirtReg, Hint);
425 }
426 }
427
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000428 // First try to find a completely free register.
429 unsigned BestCost = 0, BestReg = 0;
430 bool hasDisabled = false;
431 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
432 unsigned PhysReg = *I;
433 switch(PhysRegState[PhysReg]) {
434 case regDisabled:
435 hasDisabled = true;
436 case regReserved:
437 continue;
438 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000439 if (!UsedInInstr.test(PhysReg))
440 return assignVirtToPhysReg(VirtReg, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000441 continue;
442 default:
443 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000444 if (!BestReg && !UsedInInstr.test(PhysReg))
445 BestReg = PhysReg, BestCost = spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000446 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000447 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000448 }
449
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000450 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000451 << " candidate=" << TRI->getName(BestReg) << "\n");
452
453 // Try to extend the working set for RC if there were any disabled registers.
454 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
455 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
456 unsigned PhysReg = *I;
457 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
458 continue;
459
460 // Calculate the cost of bringing PhysReg into the working set.
461 unsigned Cost=0;
462 bool Impossible = false;
463 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
464 unsigned Alias = *AS; ++AS) {
465 if (UsedInInstr.test(Alias)) {
466 Impossible = true;
467 break;
468 }
469 switch (PhysRegState[Alias]) {
470 case regDisabled:
471 break;
472 case regReserved:
473 Impossible = true;
474 break;
475 case regFree:
476 Cost++;
477 break;
478 default:
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000479 Cost += spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000480 break;
481 }
482 }
483 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000484 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000485 << " cost=" << Cost << "\n");
486 if (!BestReg || Cost < BestCost) {
487 BestReg = PhysReg;
488 BestCost = Cost;
489 if (Cost < spillCost) break;
490 }
491 }
492 }
493
494 if (BestReg) {
495 // BestCost is 0 when all aliases are already disabled.
496 if (BestCost) {
497 if (PhysRegState[BestReg] != regDisabled)
498 spillVirtReg(MBB, MI, PhysRegState[BestReg], true);
499 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000500 // Make sure all aliases are disabled.
501 for (const unsigned *AS = TRI->getAliasSet(BestReg);
502 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 switch (PhysRegState[Alias]) {
504 case regDisabled:
505 continue;
506 case regFree:
507 PhysRegState[Alias] = regDisabled;
508 break;
509 default:
510 spillVirtReg(MBB, MI, PhysRegState[Alias], true);
511 PhysRegState[Alias] = regDisabled;
512 break;
513 }
514 }
515 }
516 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000517 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000518 }
519
520 // Nothing we can do.
521 std::string msg;
522 raw_string_ostream Msg(msg);
523 Msg << "Ran out of registers during register allocation!";
524 if (MI->isInlineAsm()) {
525 Msg << "\nPlease check your inline asm statement for "
526 << "invalid constraints:\n";
527 MI->print(Msg, TM);
528 }
529 report_fatal_error(Msg.str());
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000530 return LiveVirtRegs.end();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000531}
532
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000533/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
534unsigned RAFast::defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000535 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000536 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
537 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000538 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
539 if (lri == LiveVirtRegs.end())
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000540 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000541 else
542 addKillFlag(lri); // Kill before redefine.
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000543 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000544 LR.LastUse = MI;
545 LR.LastOpNum = OpNum;
546 LR.Dirty = true;
547 UsedInInstr.set(LR.PhysReg);
548 return LR.PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000549}
550
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000551/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
552unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000553 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000554 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
555 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000556 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
557 if (lri == LiveVirtRegs.end()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000558 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
559 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000560 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000561 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000562 << TRI->getName(lri->second.PhysReg) << "\n");
563 TII->loadRegFromStackSlot(MBB, MI, lri->second.PhysReg, FrameIndex, RC,
564 TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000565 ++NumLoads;
566 }
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000567 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000568 LR.LastUse = MI;
569 LR.LastOpNum = OpNum;
570 UsedInInstr.set(LR.PhysReg);
571 return LR.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000572}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000573
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000574// setPhysReg - Change MO the refer the PhysReg, considering subregs.
575void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
576 if (unsigned Idx = MO.getSubReg()) {
577 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
578 MO.setSubReg(0);
579 } else
580 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000581}
582
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000583void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000584 DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000585
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000586 atEndOfBlock = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000587 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000588 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000589
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000590 MachineBasicBlock::iterator MII = MBB.begin();
591
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000592 // Add live-in registers as live.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000593 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000594 E = MBB.livein_end(); I != E; ++I)
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000595 definePhysReg(MBB, MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000596
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000597 SmallVector<unsigned, 8> VirtKills, PhysDefs;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000598 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000599
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000600 // Otherwise, sequentially allocate each instruction in the MBB.
601 while (MII != MBB.end()) {
602 MachineInstr *MI = MII++;
603 const TargetInstrDesc &TID = MI->getDesc();
604 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000605 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000606 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
607 if (PhysRegState[Reg] == regDisabled) continue;
608 dbgs() << " " << TRI->getName(Reg);
609 switch(PhysRegState[Reg]) {
610 case regFree:
611 break;
612 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000613 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000614 break;
615 default:
616 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000617 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000618 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000619 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620 "Bad inverse map");
621 break;
622 }
623 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000624 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000625 // Check that LiveVirtRegs is the inverse.
626 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
627 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000628 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
629 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000630 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000631 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000632 assert(PhysRegState[i->second.PhysReg] == i->first &&
633 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000634 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000635 });
636
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000637 // Debug values are not allowed to change codegen in any way.
638 if (MI->isDebugValue()) {
639 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
640 MachineOperand &MO = MI->getOperand(i);
641 if (!MO.isReg()) continue;
642 unsigned Reg = MO.getReg();
643 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000644 LiveRegMap::iterator lri = LiveVirtRegs.find(Reg);
645 if (lri != LiveVirtRegs.end())
646 setPhysReg(MO, lri->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000647 else
648 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000649 }
650 // Next instruction.
651 continue;
652 }
653
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000654 // If this is a copy, we may be able to coalesce.
655 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
656 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
657 CopySrc = CopyDst = 0;
658
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000659 // Track registers used by instruction.
660 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000661 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000662
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000663 // First scan.
664 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000665 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
666 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000667 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000668 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000669 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg) ||
670 ReservedRegs.test(Reg)) continue;
671 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000672 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000673 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000674 definePhysReg(MBB, MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000675 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000676 }
677 }
678
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000679
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000680 // Second scan.
681 // Allocate virtreg uses and early clobbers.
682 // Collect VirtKills
683 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
684 MachineOperand &MO = MI->getOperand(i);
685 if (!MO.isReg()) continue;
686 unsigned Reg = MO.getReg();
687 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
688 if (MO.isUse()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000689 unsigned PhysReg = reloadVirtReg(MBB, MI, i, Reg, CopyDst);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000690 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000691 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000692 if (MO.isKill())
693 VirtKills.push_back(Reg);
694 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000695 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, 0);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000696 setPhysReg(MO, PhysReg);
697 PhysDefs.push_back(PhysReg);
698 }
699 }
700
701 // Process virtreg kills
702 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
703 killVirtReg(VirtKills[i]);
704 VirtKills.clear();
705
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000706 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000707
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000708 // Track registers defined by instruction - early clobbers at this point.
709 UsedInInstr.reset();
710 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
711 unsigned PhysReg = PhysDefs[i];
712 UsedInInstr.set(PhysReg);
713 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
714 unsigned Alias = *AS; ++AS)
715 UsedInInstr.set(Alias);
716 }
717
718 // Third scan.
719 // Allocate defs and collect dead defs.
720 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
721 MachineOperand &MO = MI->getOperand(i);
722 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
723 unsigned Reg = MO.getReg();
724
725 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
726 if (ReservedRegs.test(Reg)) continue;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000727 definePhysReg(MBB, MI, Reg, (MO.isImplicit() || MO.isDead()) ?
728 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000729 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000730 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000731 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, CopySrc);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000732 if (MO.isDead()) {
733 VirtKills.push_back(Reg);
734 CopyDst = 0; // cancel coalescing;
735 } else
736 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000737 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000738 }
739
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000740 // Spill all dirty virtregs before a call, in case of an exception.
741 if (TID.isCall()) {
742 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
743 spillAll(MBB, MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000744 }
745
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000746 // Process virtreg deads.
747 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
748 killVirtReg(VirtKills[i]);
749 VirtKills.clear();
750
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000751 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000752
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000753 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
754 DEBUG(dbgs() << "-- coalescing: " << *MI);
755 Coalesced.push_back(MI);
756 } else {
757 DEBUG(dbgs() << "<< " << *MI);
758 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000759 }
760
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000761 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000762 atEndOfBlock = true;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000763 DEBUG(dbgs() << "Killing live registers at end of block.\n");
764 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000765 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
766 i != e; ++i)
767 spillVirtReg(MBB, MI, i, true);
768 LiveVirtRegs.clear();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000769
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000770 // Erase all the coalesced copies. We are delaying it until now because
771 // LiveVirtsRegs might refer to the instrs.
772 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
773 MBB.erase(Coalesced[i]);
774
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000775 DEBUG(MBB.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000776}
777
778/// runOnMachineFunction - Register allocate the whole function
779///
780bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000781 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
782 << "********** Function: "
783 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000784 if (VerifyFastRegalloc)
785 Fn.verify();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000786 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000787 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000788 TM = &Fn.getTarget();
789 TRI = TM->getRegisterInfo();
790 TII = TM->getInstrInfo();
791
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000792 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000793 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000794
795 // initialize the virtual->physical register map to have a 'null'
796 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000797 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000798 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000799
800 // Loop over all of the basic blocks, eliminating virtual register references
801 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
802 MBB != MBBe; ++MBB)
803 AllocateBasicBlock(*MBB);
804
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000805 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000806 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000807
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000808 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000809 return true;
810}
811
812FunctionPass *llvm::createFastRegisterAllocator() {
813 return new RAFast();
814}