blob: be2613b4eb866e95c427d372cf445fdb5e36771e [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");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000043STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000044
45static RegisterRegAlloc
46 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
47
48namespace {
49 class RAFast : public MachineFunctionPass {
50 public:
51 static char ID;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +000052 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1),
53 atEndOfBlock(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000054 private:
55 const TargetMachine *TM;
56 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000057 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000058 const TargetRegisterInfo *TRI;
59 const TargetInstrInfo *TII;
60
61 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
62 // values are spilled.
63 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
64
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000065 // Everything we know about a live virtual register.
66 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000067 MachineInstr *LastUse; // Last instr to use reg.
68 unsigned PhysReg; // Currently held here.
69 unsigned short LastOpNum; // OpNum on LastUse.
70 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000071
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000072 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
73 Dirty(false) {
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000074 assert(p && "Don't create LiveRegs without a PhysReg");
75 }
76 };
77
78 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
79
80 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000081 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000082 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000083
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000084 // RegState - Track the state of a physical register.
85 enum RegState {
86 // A disabled register is not available for allocation, but an alias may
87 // be in use. A register can only be moved out of the disabled state if
88 // all aliases are disabled.
89 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000090
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000091 // A free register is not currently in use and can be allocated
92 // immediately without checking aliases.
93 regFree,
94
95 // A reserved register has been assigned expolicitly (e.g., setting up a
96 // call parameter), and it remains reserved until it is used.
97 regReserved
98
99 // A register state may also be a virtual register number, indication that
100 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000101 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000102 };
103
104 // PhysRegState - One of the RegState enums, or a virtreg.
105 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000106
107 // UsedInInstr - BitVector of physregs that are used in the current
108 // instruction, and so cannot be allocated.
109 BitVector UsedInInstr;
110
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000111 // Allocatable - vector of allocatable physical registers.
112 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000113
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000114 // atEndOfBlock - This flag is set after allocating all instructions in a
115 // block, before emitting final spills. When it is set, LiveRegMap is no
116 // longer updated properly sonce it will be cleared anyway.
117 bool atEndOfBlock;
118
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000119 public:
120 virtual const char *getPassName() const {
121 return "Fast Register Allocator";
122 }
123
124 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
125 AU.setPreservesCFG();
126 AU.addRequiredID(PHIEliminationID);
127 AU.addRequiredID(TwoAddressInstructionPassID);
128 MachineFunctionPass::getAnalysisUsage(AU);
129 }
130
131 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000132 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000133 void AllocateBasicBlock(MachineBasicBlock &MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000134 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000135 bool isLastUseOfLocalReg(MachineOperand&);
136
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000137 void addKillFlag(LiveRegMap::iterator i);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000138 void killVirtReg(LiveRegMap::iterator i);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000139 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000140 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000141 LiveRegMap::iterator i, bool isKill);
142 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000143 unsigned VirtReg, bool isKill);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000144
145 void usePhysReg(MachineOperand&);
146 void definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
147 unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000148 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg,
149 unsigned PhysReg);
150 LiveRegMap::iterator allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000151 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000152 unsigned defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000153 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000154 unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000155 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000156 void spillAll(MachineBasicBlock &MBB, MachineInstr *MI);
157 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000158 };
159 char RAFast::ID = 0;
160}
161
162/// getStackSpaceFor - This allocates space for the specified virtual register
163/// to be held on the stack.
164int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
165 // Find the location Reg would belong...
166 int SS = StackSlotForVirtReg[VirtReg];
167 if (SS != -1)
168 return SS; // Already has space allocated?
169
170 // Allocate a new stack object for this spill location...
171 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
172 RC->getAlignment());
173
174 // Assign the slot.
175 StackSlotForVirtReg[VirtReg] = FrameIdx;
176 return FrameIdx;
177}
178
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000179/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
180/// its virtual register, and it is guaranteed to be a block-local register.
181///
182bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
183 // Check for non-debug uses or defs following MO.
184 // This is the most likely way to fail - fast path it.
185 MachineOperand *i = &MO;
186 while ((i = i->getNextOperandForReg()))
187 if (!i->isDebug())
188 return false;
189
190 // If the register has ever been spilled or reloaded, we conservatively assume
191 // it is a global register used in multiple blocks.
192 if (StackSlotForVirtReg[MO.getReg()] != -1)
193 return false;
194
195 // Check that the use/def chain has exactly one operand - MO.
196 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
197}
198
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000199/// addKillFlag - Set kill flags on last use of a virtual register.
200void RAFast::addKillFlag(LiveRegMap::iterator lri) {
201 assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register");
202 const LiveReg &LR = lri->second;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000203 if (LR.LastUse) {
204 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000205 if (MO.isDef())
206 MO.setIsDead();
207 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
208 MO.setIsKill();
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000209 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000210}
211
212/// killVirtReg - Mark virtreg as no longer available.
213void RAFast::killVirtReg(LiveRegMap::iterator lri) {
214 addKillFlag(lri);
215 const LiveReg &LR = lri->second;
216 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
217 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000218 // Erase from LiveVirtRegs unless we're at the end of the block when
219 // everything will be bulk erased.
220 if (!atEndOfBlock)
221 LiveVirtRegs.erase(lri);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000222}
223
224/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000225void RAFast::killVirtReg(unsigned VirtReg) {
226 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
227 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000228 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
229 if (lri != LiveVirtRegs.end())
230 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000231}
232
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000233/// spillVirtReg - This method spills the value specified by VirtReg into the
234/// corresponding stack slot if needed. If isKill is set, the register is also
235/// killed.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000236void RAFast::spillVirtReg(MachineBasicBlock &MBB,
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000237 MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000238 unsigned VirtReg, bool isKill) {
239 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
240 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000241 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
242 assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register");
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000243 spillVirtReg(MBB, MI, lri, isKill);
244}
245
246/// spillVirtReg - Do the actual work of spilling.
247void RAFast::spillVirtReg(MachineBasicBlock &MBB,
248 MachineBasicBlock::iterator MI,
249 LiveRegMap::iterator lri, bool isKill) {
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000250 LiveReg &LR = lri->second;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000251 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000252
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000253 // If this physreg is used by the instruction, we want to kill it on the
254 // instruction, not on the spill.
255 bool spillKill = isKill && LR.LastUse != MI;
256
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000257 if (LR.Dirty) {
258 LR.Dirty = false;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000259 DEBUG(dbgs() << "Spilling %reg" << lri->first
260 << " in " << TRI->getName(LR.PhysReg));
261 const TargetRegisterClass *RC = MRI->getRegClass(lri->first);
262 int FrameIndex = getStackSpaceFor(lri->first, RC);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000263 DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000264 TII->storeRegToStackSlot(MBB, MI, LR.PhysReg, spillKill,
265 FrameIndex, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000266 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000267
268 if (spillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000269 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000270 else if (!isKill) {
271 MachineInstr *Spill = llvm::prior(MI);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000272 LR.LastUse = Spill;
273 LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000274 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000275 }
276
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000277 if (isKill)
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000278 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000279}
280
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000281/// spillAll - Spill all dirty virtregs without killing them.
282void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) {
283 SmallVector<unsigned, 16> Dirty;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000284 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
285 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000286 if (i->second.Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000287 Dirty.push_back(i->first);
288 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
289 spillVirtReg(MBB, MI, Dirty[i], false);
290}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000291
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000292/// usePhysReg - Handle the direct use of a physical register.
293/// Check that the register is not used by a virtreg.
294/// Kill the physreg, marking it free.
295/// This may add implicit kills to MO->getParent() and invalidate MO.
296void RAFast::usePhysReg(MachineOperand &MO) {
297 unsigned PhysReg = MO.getReg();
298 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
299 "Bad usePhysReg operand");
300
301 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000302 case regDisabled:
303 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000304 case regReserved:
305 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000306 // Fall through
307 case regFree:
308 UsedInInstr.set(PhysReg);
309 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000310 return;
311 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000312 // The physreg was allocated to a virtual register. That means to value we
313 // wanted has been clobbered.
314 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000315 }
316
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000317 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000318 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
319 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000320 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000321 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000322 break;
323 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000324 assert(TRI->isSuperRegister(PhysReg, Alias) &&
325 "Instruction is not using a subregister of a reserved register");
326 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000327 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000328 UsedInInstr.set(Alias);
329 MO.getParent()->addRegisterKilled(Alias, TRI, true);
330 return;
331 case regFree:
332 if (TRI->isSuperRegister(PhysReg, Alias)) {
333 // Leave the superregister in the working set.
334 UsedInInstr.set(Alias);
335 MO.getParent()->addRegisterKilled(Alias, TRI, true);
336 return;
337 }
338 // Some other alias was in the working set - clear it.
339 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000340 break;
341 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000342 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000343 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000344 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000345
346 // All aliases are disabled, bring register into working set.
347 PhysRegState[PhysReg] = regFree;
348 UsedInInstr.set(PhysReg);
349 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000350}
351
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000352/// definePhysReg - Mark PhysReg as reserved or free after spilling any
353/// virtregs. This is very similar to defineVirtReg except the physreg is
354/// reserved instead of allocated.
355void RAFast::definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI,
356 unsigned PhysReg, RegState NewState) {
357 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000358 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
359 case regDisabled:
360 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000361 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000362 spillVirtReg(MBB, MI, VirtReg, true);
363 // Fall through.
364 case regFree:
365 case regReserved:
366 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000367 return;
368 }
369
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000370 // This is a disabled register, disable all aliases.
371 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000372 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
373 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000374 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000375 switch (unsigned VirtReg = PhysRegState[Alias]) {
376 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000377 break;
378 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379 spillVirtReg(MBB, MI, VirtReg, true);
380 // Fall through.
381 case regFree:
382 case regReserved:
383 PhysRegState[Alias] = regDisabled;
384 if (TRI->isSuperRegister(PhysReg, Alias))
385 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000386 break;
387 }
388 }
389}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000390
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000391
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000392/// assignVirtToPhysReg - This method updates local state so that we know
393/// that PhysReg is the proper container for VirtReg now. The physical
394/// register must not be used for anything else when this is called.
395///
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000396RAFast::LiveRegMap::iterator
397RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000398 DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000399 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000400 PhysRegState[PhysReg] = VirtReg;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000401 return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000402}
403
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000404/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000405RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineBasicBlock &MBB,
406 MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000407 unsigned VirtReg,
408 unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000409 const unsigned spillCost = 100;
410 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
411 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000412
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000413 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000414 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
415 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000416
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000417 // Ignore invalid hints.
418 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Chandler Carruth2c13ab22010-05-15 10:23:23 +0000419 !RC->contains(Hint) || UsedInInstr.test(Hint) ||
420 !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000421 Hint = 0;
422
423 // If there is no hint, peek at the first use of this register.
424 if (!Hint && !MRI->use_nodbg_empty(VirtReg)) {
425 MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg);
426 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
427 // Copy to physreg -> use physreg as hint.
428 if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
429 SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000430 RC->contains(DstReg) && !UsedInInstr.test(DstReg) &&
431 Allocatable.test(DstReg)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000432 Hint = DstReg;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000433 DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000434 }
435 }
436
437 // Take hint when possible.
438 if (Hint) {
439 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000440 Allocatable.test(Hint) && "Invalid hint should have been cleared");
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000441 switch(PhysRegState[Hint]) {
442 case regDisabled:
443 case regReserved:
444 break;
445 default:
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000446 spillVirtReg(MBB, MI, PhysRegState[Hint], true);
447 // Fall through.
448 case regFree:
449 return assignVirtToPhysReg(VirtReg, Hint);
450 }
451 }
452
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000453 // First try to find a completely free register.
454 unsigned BestCost = 0, BestReg = 0;
455 bool hasDisabled = false;
456 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
457 unsigned PhysReg = *I;
458 switch(PhysRegState[PhysReg]) {
459 case regDisabled:
460 hasDisabled = true;
461 case regReserved:
462 continue;
463 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000464 if (!UsedInInstr.test(PhysReg))
465 return assignVirtToPhysReg(VirtReg, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000466 continue;
467 default:
468 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000469 if (!BestReg && !UsedInInstr.test(PhysReg))
470 BestReg = PhysReg, BestCost = spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000471 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000472 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000473 }
474
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000475 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000476 << " candidate=" << TRI->getName(BestReg) << "\n");
477
478 // Try to extend the working set for RC if there were any disabled registers.
479 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
480 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
481 unsigned PhysReg = *I;
482 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
483 continue;
484
485 // Calculate the cost of bringing PhysReg into the working set.
486 unsigned Cost=0;
487 bool Impossible = false;
488 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
489 unsigned Alias = *AS; ++AS) {
490 if (UsedInInstr.test(Alias)) {
491 Impossible = true;
492 break;
493 }
494 switch (PhysRegState[Alias]) {
495 case regDisabled:
496 break;
497 case regReserved:
498 Impossible = true;
499 break;
500 case regFree:
501 Cost++;
502 break;
503 default:
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000504 Cost += spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000505 break;
506 }
507 }
508 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000509 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000510 << " cost=" << Cost << "\n");
511 if (!BestReg || Cost < BestCost) {
512 BestReg = PhysReg;
513 BestCost = Cost;
514 if (Cost < spillCost) break;
515 }
516 }
517 }
518
519 if (BestReg) {
520 // BestCost is 0 when all aliases are already disabled.
521 if (BestCost) {
522 if (PhysRegState[BestReg] != regDisabled)
523 spillVirtReg(MBB, MI, PhysRegState[BestReg], true);
524 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000525 // Make sure all aliases are disabled.
526 for (const unsigned *AS = TRI->getAliasSet(BestReg);
527 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000528 switch (PhysRegState[Alias]) {
529 case regDisabled:
530 continue;
531 case regFree:
532 PhysRegState[Alias] = regDisabled;
533 break;
534 default:
535 spillVirtReg(MBB, MI, PhysRegState[Alias], true);
536 PhysRegState[Alias] = regDisabled;
537 break;
538 }
539 }
540 }
541 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000542 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000543 }
544
545 // Nothing we can do.
546 std::string msg;
547 raw_string_ostream Msg(msg);
548 Msg << "Ran out of registers during register allocation!";
549 if (MI->isInlineAsm()) {
550 Msg << "\nPlease check your inline asm statement for "
551 << "invalid constraints:\n";
552 MI->print(Msg, TM);
553 }
554 report_fatal_error(Msg.str());
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000555 return LiveVirtRegs.end();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000556}
557
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000558/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
559unsigned RAFast::defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000560 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000561 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
562 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000563 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
564 if (lri == LiveVirtRegs.end())
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000565 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000566 else
567 addKillFlag(lri); // Kill before redefine.
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000568 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000569 LR.LastUse = MI;
570 LR.LastOpNum = OpNum;
571 LR.Dirty = true;
572 UsedInInstr.set(LR.PhysReg);
573 return LR.PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000574}
575
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000576/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
577unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000578 unsigned OpNum, unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000579 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
580 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000581 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
582 if (lri == LiveVirtRegs.end()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000583 lri = allocVirtReg(MBB, MI, VirtReg, Hint);
584 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000585 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000586 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000587 << TRI->getName(lri->second.PhysReg) << "\n");
588 TII->loadRegFromStackSlot(MBB, MI, lri->second.PhysReg, FrameIndex, RC,
589 TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000590 ++NumLoads;
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000591 } else if (lri->second.Dirty) {
592 MachineOperand &MO = MI->getOperand(OpNum);
593 if (isLastUseOfLocalReg(MO)) {
594 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
595 MO.setIsKill();
596 } else if (MO.isKill()) {
597 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
598 MO.setIsKill(false);
599 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000600 }
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000601 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000602 LR.LastUse = MI;
603 LR.LastOpNum = OpNum;
604 UsedInInstr.set(LR.PhysReg);
605 return LR.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000606}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000607
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000608// setPhysReg - Change MO the refer the PhysReg, considering subregs.
609void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
610 if (unsigned Idx = MO.getSubReg()) {
611 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
612 MO.setSubReg(0);
613 } else
614 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000615}
616
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000617void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000618 DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000619
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000620 atEndOfBlock = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000621 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000622 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000623
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000624 MachineBasicBlock::iterator MII = MBB.begin();
625
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000626 // Add live-in registers as live.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000627 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000628 E = MBB.livein_end(); I != E; ++I)
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000629 definePhysReg(MBB, MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000630
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000631 SmallVector<unsigned, 8> VirtKills, PhysDefs;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000632 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000633
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000634 // Otherwise, sequentially allocate each instruction in the MBB.
635 while (MII != MBB.end()) {
636 MachineInstr *MI = MII++;
637 const TargetInstrDesc &TID = MI->getDesc();
638 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000639 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000640 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
641 if (PhysRegState[Reg] == regDisabled) continue;
642 dbgs() << " " << TRI->getName(Reg);
643 switch(PhysRegState[Reg]) {
644 case regFree:
645 break;
646 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000647 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000648 break;
649 default:
650 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000651 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000652 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000653 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000654 "Bad inverse map");
655 break;
656 }
657 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000658 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000659 // Check that LiveVirtRegs is the inverse.
660 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
661 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000662 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
663 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000664 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000665 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000666 assert(PhysRegState[i->second.PhysReg] == i->first &&
667 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000668 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000669 });
670
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000671 // Debug values are not allowed to change codegen in any way.
672 if (MI->isDebugValue()) {
673 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
674 MachineOperand &MO = MI->getOperand(i);
675 if (!MO.isReg()) continue;
676 unsigned Reg = MO.getReg();
677 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000678 LiveRegMap::iterator lri = LiveVirtRegs.find(Reg);
679 if (lri != LiveVirtRegs.end())
680 setPhysReg(MO, lri->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000681 else
682 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000683 }
684 // Next instruction.
685 continue;
686 }
687
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000688 // If this is a copy, we may be able to coalesce.
689 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
690 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
691 CopySrc = CopyDst = 0;
692
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000693 // Track registers used by instruction.
694 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000695 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000696
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000697 // First scan.
698 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000699 // Find the end of the virtreg operands
700 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000701 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
702 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000703 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000704 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000705 if (!Reg) continue;
706 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
707 VirtOpEnd = i+1;
708 continue;
709 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000710 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000711 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000712 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000713 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000714 definePhysReg(MBB, MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000715 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000716 }
717 }
718
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000719 // Second scan.
720 // Allocate virtreg uses and early clobbers.
721 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000722 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000723 MachineOperand &MO = MI->getOperand(i);
724 if (!MO.isReg()) continue;
725 unsigned Reg = MO.getReg();
726 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
727 if (MO.isUse()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000728 unsigned PhysReg = reloadVirtReg(MBB, MI, i, Reg, CopyDst);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000729 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000730 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000731 if (MO.isKill())
732 VirtKills.push_back(Reg);
733 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000734 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, 0);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000735 setPhysReg(MO, PhysReg);
736 PhysDefs.push_back(PhysReg);
737 }
738 }
739
740 // Process virtreg kills
741 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
742 killVirtReg(VirtKills[i]);
743 VirtKills.clear();
744
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000745 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000746
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000747 // Track registers defined by instruction - early clobbers at this point.
748 UsedInInstr.reset();
749 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
750 unsigned PhysReg = PhysDefs[i];
751 UsedInInstr.set(PhysReg);
752 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
753 unsigned Alias = *AS; ++AS)
754 UsedInInstr.set(Alias);
755 }
756
757 // Third scan.
758 // Allocate defs and collect dead defs.
759 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
760 MachineOperand &MO = MI->getOperand(i);
761 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
762 unsigned Reg = MO.getReg();
763
764 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000765 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000766 definePhysReg(MBB, MI, Reg, (MO.isImplicit() || MO.isDead()) ?
767 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000768 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000769 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000770 unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, CopySrc);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000771 if (MO.isDead()) {
772 VirtKills.push_back(Reg);
773 CopyDst = 0; // cancel coalescing;
774 } else
775 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000776 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000777 }
778
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000779 // Spill all dirty virtregs before a call, in case of an exception.
780 if (TID.isCall()) {
781 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
782 spillAll(MBB, MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000783 }
784
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000785 // Process virtreg deads.
786 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
787 killVirtReg(VirtKills[i]);
788 VirtKills.clear();
789
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000790 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000791
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000792 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
793 DEBUG(dbgs() << "-- coalescing: " << *MI);
794 Coalesced.push_back(MI);
795 } else {
796 DEBUG(dbgs() << "<< " << *MI);
797 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000798 }
799
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000800 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000801 atEndOfBlock = true;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000802 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Jakob Stoklund Olesen6a6328b2010-05-14 22:40:43 +0000803 if (MI != MBB.end() && MI->getDesc().isReturn()) {
804 // This is a return block, kill all virtual registers.
805 DEBUG(dbgs() << "Killing live registers at end of return block.\n");
806 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
807 i != e; ++i)
808 killVirtReg(i);
809 } else {
810 // This is a normal block, spill any dirty virtregs.
811 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
812 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
813 i != e; ++i)
814 spillVirtReg(MBB, MI, i, true);
815 }
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000816 LiveVirtRegs.clear();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000817
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000818 // Erase all the coalesced copies. We are delaying it until now because
819 // LiveVirtsRegs might refer to the instrs.
820 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
821 MBB.erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000822 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000823
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000824 DEBUG(MBB.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000825}
826
827/// runOnMachineFunction - Register allocate the whole function
828///
829bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000830 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
831 << "********** Function: "
832 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000833 if (VerifyFastRegalloc)
Jakob Stoklund Olesena0e618d2010-05-14 21:55:44 +0000834 Fn.verify(this, true);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000835 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000836 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000837 TM = &Fn.getTarget();
838 TRI = TM->getRegisterInfo();
839 TII = TM->getInstrInfo();
840
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000841 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000842 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000843
844 // initialize the virtual->physical register map to have a 'null'
845 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000846 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000847 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000848
849 // Loop over all of the basic blocks, eliminating virtual register references
850 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
851 MBB != MBBe; ++MBB)
852 AllocateBasicBlock(*MBB);
853
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000854 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000855 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000856
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000857 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000858 return true;
859}
860
861FunctionPass *llvm::createFastRegisterAllocator() {
862 return new RAFast();
863}