blob: 25595be468614169d0fd7153167af70a1011d242 [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
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000061 // Basic block currently being allocated.
62 MachineBasicBlock *MBB;
63
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000064 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
65 // values are spilled.
66 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
67
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000068 // Everything we know about a live virtual register.
69 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000070 MachineInstr *LastUse; // Last instr to use reg.
71 unsigned PhysReg; // Currently held here.
72 unsigned short LastOpNum; // OpNum on LastUse.
73 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000074
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000075 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000076 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000077 };
78
79 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000080 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000081
82 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000083 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000084 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000085
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000086 // RegState - Track the state of a physical register.
87 enum RegState {
88 // A disabled register is not available for allocation, but an alias may
89 // be in use. A register can only be moved out of the disabled state if
90 // all aliases are disabled.
91 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000092
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000093 // A free register is not currently in use and can be allocated
94 // immediately without checking aliases.
95 regFree,
96
97 // A reserved register has been assigned expolicitly (e.g., setting up a
98 // call parameter), and it remains reserved until it is used.
99 regReserved
100
101 // A register state may also be a virtual register number, indication that
102 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000103 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000104 };
105
106 // PhysRegState - One of the RegState enums, or a virtreg.
107 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000108
109 // UsedInInstr - BitVector of physregs that are used in the current
110 // instruction, and so cannot be allocated.
111 BitVector UsedInInstr;
112
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000113 // Allocatable - vector of allocatable physical registers.
114 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000115
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000116 // atEndOfBlock - This flag is set after allocating all instructions in a
117 // block, before emitting final spills. When it is set, LiveRegMap is no
118 // longer updated properly sonce it will be cleared anyway.
119 bool atEndOfBlock;
120
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000121 public:
122 virtual const char *getPassName() const {
123 return "Fast Register Allocator";
124 }
125
126 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
127 AU.setPreservesCFG();
128 AU.addRequiredID(PHIEliminationID);
129 AU.addRequiredID(TwoAddressInstructionPassID);
130 MachineFunctionPass::getAnalysisUsage(AU);
131 }
132
133 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000134 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000135 void AllocateBasicBlock();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000136 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000137 bool isLastUseOfLocalReg(MachineOperand&);
138
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000139 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000140 void killVirtReg(LiveRegMap::iterator i);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000141 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000142 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator i,
143 bool isKill);
144 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg,
145 bool isKill);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000146
147 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000148 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000149 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
150 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
151 unsigned defineVirtReg(MachineInstr *MI, unsigned OpNum,
152 unsigned VirtReg, unsigned Hint);
153 unsigned reloadVirtReg(MachineInstr *MI, unsigned OpNum,
154 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000155 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000156 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000157 };
158 char RAFast::ID = 0;
159}
160
161/// getStackSpaceFor - This allocates space for the specified virtual register
162/// to be held on the stack.
163int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
164 // Find the location Reg would belong...
165 int SS = StackSlotForVirtReg[VirtReg];
166 if (SS != -1)
167 return SS; // Already has space allocated?
168
169 // Allocate a new stack object for this spill location...
170 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
171 RC->getAlignment());
172
173 // Assign the slot.
174 StackSlotForVirtReg[VirtReg] = FrameIdx;
175 return FrameIdx;
176}
177
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000178/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
179/// its virtual register, and it is guaranteed to be a block-local register.
180///
181bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
182 // Check for non-debug uses or defs following MO.
183 // This is the most likely way to fail - fast path it.
184 MachineOperand *i = &MO;
185 while ((i = i->getNextOperandForReg()))
186 if (!i->isDebug())
187 return false;
188
189 // If the register has ever been spilled or reloaded, we conservatively assume
190 // it is a global register used in multiple blocks.
191 if (StackSlotForVirtReg[MO.getReg()] != -1)
192 return false;
193
194 // Check that the use/def chain has exactly one operand - MO.
195 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
196}
197
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000198/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000199void RAFast::addKillFlag(const LiveReg &LR) {
200 if (!LR.LastUse) return;
201 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
202 if (MO.isDef())
203 MO.setIsDead();
204 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
205 MO.setIsKill();
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000206}
207
208/// killVirtReg - Mark virtreg as no longer available.
209void RAFast::killVirtReg(LiveRegMap::iterator lri) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000210 addKillFlag(lri->second);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000211 const LiveReg &LR = lri->second;
212 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
213 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000214 // Erase from LiveVirtRegs unless we're at the end of the block when
215 // everything will be bulk erased.
216 if (!atEndOfBlock)
217 LiveVirtRegs.erase(lri);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000218}
219
220/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000221void RAFast::killVirtReg(unsigned VirtReg) {
222 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
223 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000224 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
225 if (lri != LiveVirtRegs.end())
226 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000227}
228
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000229/// spillVirtReg - This method spills the value specified by VirtReg into the
230/// corresponding stack slot if needed. If isKill is set, the register is also
231/// killed.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000232void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000233 unsigned VirtReg, bool isKill) {
234 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
235 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000236 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
237 assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register");
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000238 spillVirtReg(MI, lri, isKill);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000239}
240
241/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000242void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000243 LiveRegMap::iterator lri, bool isKill) {
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000244 LiveReg &LR = lri->second;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000245 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000246
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000247 // If this physreg is used by the instruction, we want to kill it on the
248 // instruction, not on the spill.
249 bool spillKill = isKill && LR.LastUse != MI;
250
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000251 if (LR.Dirty) {
252 LR.Dirty = false;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000253 DEBUG(dbgs() << "Spilling %reg" << lri->first
254 << " in " << TRI->getName(LR.PhysReg));
255 const TargetRegisterClass *RC = MRI->getRegClass(lri->first);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000256 int FI = getStackSpaceFor(lri->first, RC);
257 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
258 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, spillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000259 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000260
261 if (spillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000262 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000263 else if (!isKill) {
264 MachineInstr *Spill = llvm::prior(MI);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000265 LR.LastUse = Spill;
266 LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000267 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000268 }
269
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000270 if (isKill)
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000271 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000272}
273
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000274/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000275void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000276 SmallVector<unsigned, 16> Dirty;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000277 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
278 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000279 if (i->second.Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000280 Dirty.push_back(i->first);
281 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000282 spillVirtReg(MI, Dirty[i], false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000283}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000284
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000285/// usePhysReg - Handle the direct use of a physical register.
286/// Check that the register is not used by a virtreg.
287/// Kill the physreg, marking it free.
288/// This may add implicit kills to MO->getParent() and invalidate MO.
289void RAFast::usePhysReg(MachineOperand &MO) {
290 unsigned PhysReg = MO.getReg();
291 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
292 "Bad usePhysReg operand");
293
294 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000295 case regDisabled:
296 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000297 case regReserved:
298 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000299 // Fall through
300 case regFree:
301 UsedInInstr.set(PhysReg);
302 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000303 return;
304 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000305 // The physreg was allocated to a virtual register. That means to value we
306 // wanted has been clobbered.
307 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000308 }
309
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000310 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000311 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
312 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000313 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000314 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000315 break;
316 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000317 assert(TRI->isSuperRegister(PhysReg, Alias) &&
318 "Instruction is not using a subregister of a reserved register");
319 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000320 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000321 UsedInInstr.set(Alias);
322 MO.getParent()->addRegisterKilled(Alias, TRI, true);
323 return;
324 case regFree:
325 if (TRI->isSuperRegister(PhysReg, Alias)) {
326 // Leave the superregister in the working set.
327 UsedInInstr.set(Alias);
328 MO.getParent()->addRegisterKilled(Alias, TRI, true);
329 return;
330 }
331 // Some other alias was in the working set - clear it.
332 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000333 break;
334 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000335 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000336 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000337 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000338
339 // All aliases are disabled, bring register into working set.
340 PhysRegState[PhysReg] = regFree;
341 UsedInInstr.set(PhysReg);
342 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000343}
344
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000345/// definePhysReg - Mark PhysReg as reserved or free after spilling any
346/// virtregs. This is very similar to defineVirtReg except the physreg is
347/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000348void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
349 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000350 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000351 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
352 case regDisabled:
353 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000354 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000355 spillVirtReg(MI, VirtReg, true);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000356 // Fall through.
357 case regFree:
358 case regReserved:
359 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000360 return;
361 }
362
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000363 // This is a disabled register, disable all aliases.
364 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000365 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
366 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000367 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000368 switch (unsigned VirtReg = PhysRegState[Alias]) {
369 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000370 break;
371 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000372 spillVirtReg(MI, VirtReg, true);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000373 // Fall through.
374 case regFree:
375 case regReserved:
376 PhysRegState[Alias] = regDisabled;
377 if (TRI->isSuperRegister(PhysReg, Alias))
378 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000379 break;
380 }
381 }
382}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000383
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000384
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000385/// assignVirtToPhysReg - This method updates local state so that we know
386/// that PhysReg is the proper container for VirtReg now. The physical
387/// register must not be used for anything else when this is called.
388///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000389void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
390 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000391 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000392 PhysRegState[PhysReg] = LRE.first;
393 assert(!LRE.second.PhysReg && "Already assigned a physreg");
394 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000395}
396
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000397/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000398void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000399 const unsigned spillCost = 100;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000400 const unsigned VirtReg = LRE.first;
401
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000402 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
403 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000404
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000405 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000406 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
407 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000408
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000409 // Ignore invalid hints.
410 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Chandler Carruth2c13ab22010-05-15 10:23:23 +0000411 !RC->contains(Hint) || UsedInInstr.test(Hint) ||
412 !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000413 Hint = 0;
414
415 // If there is no hint, peek at the first use of this register.
416 if (!Hint && !MRI->use_nodbg_empty(VirtReg)) {
417 MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg);
418 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
419 // Copy to physreg -> use physreg as hint.
420 if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
421 SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000422 RC->contains(DstReg) && !UsedInInstr.test(DstReg) &&
423 Allocatable.test(DstReg)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000424 Hint = DstReg;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000425 DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000426 }
427 }
428
429 // Take hint when possible.
430 if (Hint) {
431 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000432 Allocatable.test(Hint) && "Invalid hint should have been cleared");
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000433 switch(PhysRegState[Hint]) {
434 case regDisabled:
435 case regReserved:
436 break;
437 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000438 spillVirtReg(MI, PhysRegState[Hint], true);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000439 // Fall through.
440 case regFree:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000441 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000442 }
443 }
444
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000445 // First try to find a completely free register.
446 unsigned BestCost = 0, BestReg = 0;
447 bool hasDisabled = false;
448 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
449 unsigned PhysReg = *I;
450 switch(PhysRegState[PhysReg]) {
451 case regDisabled:
452 hasDisabled = true;
453 case regReserved:
454 continue;
455 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000456 if (!UsedInInstr.test(PhysReg))
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000457 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000458 continue;
459 default:
460 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000461 if (!BestReg && !UsedInInstr.test(PhysReg))
462 BestReg = PhysReg, BestCost = spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000463 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000464 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000465 }
466
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000467 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000468 << " candidate=" << TRI->getName(BestReg) << "\n");
469
470 // Try to extend the working set for RC if there were any disabled registers.
471 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
472 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
473 unsigned PhysReg = *I;
474 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
475 continue;
476
477 // Calculate the cost of bringing PhysReg into the working set.
478 unsigned Cost=0;
479 bool Impossible = false;
480 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
481 unsigned Alias = *AS; ++AS) {
482 if (UsedInInstr.test(Alias)) {
483 Impossible = true;
484 break;
485 }
486 switch (PhysRegState[Alias]) {
487 case regDisabled:
488 break;
489 case regReserved:
490 Impossible = true;
491 break;
492 case regFree:
493 Cost++;
494 break;
495 default:
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000496 Cost += spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000497 break;
498 }
499 }
500 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000501 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000502 << " cost=" << Cost << "\n");
503 if (!BestReg || Cost < BestCost) {
504 BestReg = PhysReg;
505 BestCost = Cost;
506 if (Cost < spillCost) break;
507 }
508 }
509 }
510
511 if (BestReg) {
512 // BestCost is 0 when all aliases are already disabled.
513 if (BestCost) {
514 if (PhysRegState[BestReg] != regDisabled)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000515 spillVirtReg(MI, PhysRegState[BestReg], true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000516 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000517 // Make sure all aliases are disabled.
518 for (const unsigned *AS = TRI->getAliasSet(BestReg);
519 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000520 switch (PhysRegState[Alias]) {
521 case regDisabled:
522 continue;
523 case regFree:
524 PhysRegState[Alias] = regDisabled;
525 break;
526 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000527 spillVirtReg(MI, PhysRegState[Alias], true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000528 PhysRegState[Alias] = regDisabled;
529 break;
530 }
531 }
532 }
533 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000534 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000535 }
536
537 // Nothing we can do.
538 std::string msg;
539 raw_string_ostream Msg(msg);
540 Msg << "Ran out of registers during register allocation!";
541 if (MI->isInlineAsm()) {
542 Msg << "\nPlease check your inline asm statement for "
543 << "invalid constraints:\n";
544 MI->print(Msg, TM);
545 }
546 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000547}
548
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000549/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000550unsigned RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
551 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000552 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
553 "Not a virtual register");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000554 LiveRegMap::iterator lri;
555 bool New;
556 tie(lri, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000557 LiveReg &LR = lri->second;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000558 if (New)
559 allocVirtReg(MI, *lri, Hint);
560 else
561 addKillFlag(LR); // Kill before redefine.
562 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000563 LR.LastUse = MI;
564 LR.LastOpNum = OpNum;
565 LR.Dirty = true;
566 UsedInInstr.set(LR.PhysReg);
567 return LR.PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000568}
569
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000570/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000571unsigned RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
572 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000573 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
574 "Not a virtual register");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000575 LiveRegMap::iterator lri;
576 bool New;
577 tie(lri, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
578 LiveReg &LR = lri->second;
579 if (New) {
580 allocVirtReg(MI, *lri, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000581 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000582 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000583 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000584 << TRI->getName(LR.PhysReg) << "\n");
585 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000586 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000587 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000588 MachineOperand &MO = MI->getOperand(OpNum);
589 if (isLastUseOfLocalReg(MO)) {
590 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
591 MO.setIsKill();
592 } else if (MO.isKill()) {
593 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
594 MO.setIsKill(false);
595 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000596 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000597 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000598 LR.LastUse = MI;
599 LR.LastOpNum = OpNum;
600 UsedInInstr.set(LR.PhysReg);
601 return LR.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000602}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000603
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000604// setPhysReg - Change MO the refer the PhysReg, considering subregs.
605void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
606 if (unsigned Idx = MO.getSubReg()) {
607 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
608 MO.setSubReg(0);
609 } else
610 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000611}
612
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000613void RAFast::AllocateBasicBlock() {
614 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000615
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000616 atEndOfBlock = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000617 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000618 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000619
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000620 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000621
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000622 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000623 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
624 E = MBB->livein_end(); I != E; ++I)
625 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000626
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000627 SmallVector<unsigned, 8> VirtKills, PhysDefs;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000628 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000629
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000630 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000631 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000632 MachineInstr *MI = MII++;
633 const TargetInstrDesc &TID = MI->getDesc();
634 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000635 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000636 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
637 if (PhysRegState[Reg] == regDisabled) continue;
638 dbgs() << " " << TRI->getName(Reg);
639 switch(PhysRegState[Reg]) {
640 case regFree:
641 break;
642 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000643 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000644 break;
645 default:
646 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000647 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000648 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000649 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000650 "Bad inverse map");
651 break;
652 }
653 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000654 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000655 // Check that LiveVirtRegs is the inverse.
656 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
657 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000658 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
659 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000660 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000661 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000662 assert(PhysRegState[i->second.PhysReg] == i->first &&
663 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000664 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000665 });
666
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000667 // Debug values are not allowed to change codegen in any way.
668 if (MI->isDebugValue()) {
669 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
670 MachineOperand &MO = MI->getOperand(i);
671 if (!MO.isReg()) continue;
672 unsigned Reg = MO.getReg();
673 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000674 LiveRegMap::iterator lri = LiveVirtRegs.find(Reg);
675 if (lri != LiveVirtRegs.end())
676 setPhysReg(MO, lri->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000677 else
678 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000679 }
680 // Next instruction.
681 continue;
682 }
683
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000684 // If this is a copy, we may be able to coalesce.
685 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
686 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
687 CopySrc = CopyDst = 0;
688
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000689 // Track registers used by instruction.
690 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000691 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000692
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000693 // First scan.
694 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000695 // Find the end of the virtreg operands
696 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000697 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
698 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000699 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000700 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000701 if (!Reg) continue;
702 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
703 VirtOpEnd = i+1;
704 continue;
705 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000706 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000707 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000708 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000709 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000710 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000711 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000712 }
713 }
714
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000715 // Second scan.
716 // Allocate virtreg uses and early clobbers.
717 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000718 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000719 MachineOperand &MO = MI->getOperand(i);
720 if (!MO.isReg()) continue;
721 unsigned Reg = MO.getReg();
722 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
723 if (MO.isUse()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000724 unsigned PhysReg = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000725 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000726 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000727 if (MO.isKill())
728 VirtKills.push_back(Reg);
729 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000730 unsigned PhysReg = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000731 setPhysReg(MO, PhysReg);
732 PhysDefs.push_back(PhysReg);
733 }
734 }
735
736 // Process virtreg kills
737 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
738 killVirtReg(VirtKills[i]);
739 VirtKills.clear();
740
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000741 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000742
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000743 // Track registers defined by instruction - early clobbers at this point.
744 UsedInInstr.reset();
745 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
746 unsigned PhysReg = PhysDefs[i];
747 UsedInInstr.set(PhysReg);
748 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
749 unsigned Alias = *AS; ++AS)
750 UsedInInstr.set(Alias);
751 }
752
753 // Third scan.
754 // Allocate defs and collect dead defs.
755 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
756 MachineOperand &MO = MI->getOperand(i);
757 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
758 unsigned Reg = MO.getReg();
759
760 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000761 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000762 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
763 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000764 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000765 }
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000766 unsigned PhysReg = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000767 if (MO.isDead()) {
768 VirtKills.push_back(Reg);
769 CopyDst = 0; // cancel coalescing;
770 } else
771 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000772 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000773 }
774
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000775 // Spill all dirty virtregs before a call, in case of an exception.
776 if (TID.isCall()) {
777 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000778 spillAll(MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000779 }
780
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000781 // Process virtreg deads.
782 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
783 killVirtReg(VirtKills[i]);
784 VirtKills.clear();
785
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000786 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000787
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000788 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
789 DEBUG(dbgs() << "-- coalescing: " << *MI);
790 Coalesced.push_back(MI);
791 } else {
792 DEBUG(dbgs() << "<< " << *MI);
793 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000794 }
795
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000796 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000797 atEndOfBlock = true;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000798 MachineBasicBlock::iterator MI = MBB->getFirstTerminator();
799 if (MI != MBB->end() && MI->getDesc().isReturn()) {
Jakob Stoklund Olesen6a6328b2010-05-14 22:40:43 +0000800 // This is a return block, kill all virtual registers.
801 DEBUG(dbgs() << "Killing live registers at end of return block.\n");
802 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
803 i != e; ++i)
804 killVirtReg(i);
805 } else {
806 // This is a normal block, spill any dirty virtregs.
807 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
808 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
809 i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000810 spillVirtReg(MI, i, true);
Jakob Stoklund Olesen6a6328b2010-05-14 22:40:43 +0000811 }
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000812 LiveVirtRegs.clear();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000813
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000814 // Erase all the coalesced copies. We are delaying it until now because
815 // LiveVirtsRegs might refer to the instrs.
816 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000817 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000818 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000819
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000820 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000821}
822
823/// runOnMachineFunction - Register allocate the whole function
824///
825bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000826 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
827 << "********** Function: "
828 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000829 if (VerifyFastRegalloc)
Jakob Stoklund Olesena0e618d2010-05-14 21:55:44 +0000830 Fn.verify(this, true);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000831 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000832 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000833 TM = &Fn.getTarget();
834 TRI = TM->getRegisterInfo();
835 TII = TM->getInstrInfo();
836
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000837 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000838 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000839
840 // initialize the virtual->physical register map to have a 'null'
841 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000842 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000843 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000844
845 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000846 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
847 MBBi != MBBe; ++MBBi) {
848 MBB = &*MBBi;
849 AllocateBasicBlock();
850 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000851
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000852 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000853 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000854
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000855 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000856 return true;
857}
858
859FunctionPass *llvm::createFastRegisterAllocator() {
860 return new RAFast();
861}