blob: 19414c70f08bd673f89da4d72f3a1ab7ff5f03bc [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),
76 Dirty(false) {
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000077 assert(p && "Don't create LiveRegs without a PhysReg");
78 }
79 };
80
81 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
82
83 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000084 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000085 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000086
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000087 // RegState - Track the state of a physical register.
88 enum RegState {
89 // A disabled register is not available for allocation, but an alias may
90 // be in use. A register can only be moved out of the disabled state if
91 // all aliases are disabled.
92 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000093
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000094 // A free register is not currently in use and can be allocated
95 // immediately without checking aliases.
96 regFree,
97
98 // A reserved register has been assigned expolicitly (e.g., setting up a
99 // call parameter), and it remains reserved until it is used.
100 regReserved
101
102 // A register state may also be a virtual register number, indication that
103 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000104 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000105 };
106
107 // PhysRegState - One of the RegState enums, or a virtreg.
108 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000109
110 // UsedInInstr - BitVector of physregs that are used in the current
111 // instruction, and so cannot be allocated.
112 BitVector UsedInInstr;
113
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000114 // Allocatable - vector of allocatable physical registers.
115 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000116
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000117 // atEndOfBlock - This flag is set after allocating all instructions in a
118 // block, before emitting final spills. When it is set, LiveRegMap is no
119 // longer updated properly sonce it will be cleared anyway.
120 bool atEndOfBlock;
121
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000122 public:
123 virtual const char *getPassName() const {
124 return "Fast Register Allocator";
125 }
126
127 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
128 AU.setPreservesCFG();
129 AU.addRequiredID(PHIEliminationID);
130 AU.addRequiredID(TwoAddressInstructionPassID);
131 MachineFunctionPass::getAnalysisUsage(AU);
132 }
133
134 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000135 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000136 void AllocateBasicBlock();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000137 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000138 bool isLastUseOfLocalReg(MachineOperand&);
139
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000140 void addKillFlag(LiveRegMap::iterator i);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000141 void killVirtReg(LiveRegMap::iterator i);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000142 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000143 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator i,
144 bool isKill);
145 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg,
146 bool isKill);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000147
148 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000149 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000150 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg,
151 unsigned PhysReg);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000152 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, unsigned VirtReg,
153 unsigned Hint);
154 unsigned defineVirtReg(MachineInstr *MI, unsigned OpNum, unsigned VirtReg,
155 unsigned Hint);
156 unsigned reloadVirtReg(MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000157 unsigned OpNum, unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000158 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000159 void setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000160 };
161 char RAFast::ID = 0;
162}
163
164/// getStackSpaceFor - This allocates space for the specified virtual register
165/// to be held on the stack.
166int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
167 // Find the location Reg would belong...
168 int SS = StackSlotForVirtReg[VirtReg];
169 if (SS != -1)
170 return SS; // Already has space allocated?
171
172 // Allocate a new stack object for this spill location...
173 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
174 RC->getAlignment());
175
176 // Assign the slot.
177 StackSlotForVirtReg[VirtReg] = FrameIdx;
178 return FrameIdx;
179}
180
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000181/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
182/// its virtual register, and it is guaranteed to be a block-local register.
183///
184bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
185 // Check for non-debug uses or defs following MO.
186 // This is the most likely way to fail - fast path it.
187 MachineOperand *i = &MO;
188 while ((i = i->getNextOperandForReg()))
189 if (!i->isDebug())
190 return false;
191
192 // If the register has ever been spilled or reloaded, we conservatively assume
193 // it is a global register used in multiple blocks.
194 if (StackSlotForVirtReg[MO.getReg()] != -1)
195 return false;
196
197 // Check that the use/def chain has exactly one operand - MO.
198 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
199}
200
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000201/// addKillFlag - Set kill flags on last use of a virtual register.
202void RAFast::addKillFlag(LiveRegMap::iterator lri) {
203 assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register");
204 const LiveReg &LR = lri->second;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000205 if (LR.LastUse) {
206 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000207 if (MO.isDef())
208 MO.setIsDead();
209 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
210 MO.setIsKill();
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000211 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000212}
213
214/// killVirtReg - Mark virtreg as no longer available.
215void RAFast::killVirtReg(LiveRegMap::iterator lri) {
216 addKillFlag(lri);
217 const LiveReg &LR = lri->second;
218 assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping");
219 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000220 // Erase from LiveVirtRegs unless we're at the end of the block when
221 // everything will be bulk erased.
222 if (!atEndOfBlock)
223 LiveVirtRegs.erase(lri);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000224}
225
226/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000227void RAFast::killVirtReg(unsigned VirtReg) {
228 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
229 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000230 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
231 if (lri != LiveVirtRegs.end())
232 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000233}
234
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000235/// spillVirtReg - This method spills the value specified by VirtReg into the
236/// corresponding stack slot if needed. If isKill is set, the register is also
237/// killed.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000238void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000239 unsigned VirtReg, bool isKill) {
240 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
241 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000242 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
243 assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register");
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000244 spillVirtReg(MI, lri, isKill);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000245}
246
247/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000248void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000249 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);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000262 int FI = getStackSpaceFor(lri->first, RC);
263 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
264 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, spillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000265 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000266
267 if (spillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000268 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000269 else if (!isKill) {
270 MachineInstr *Spill = llvm::prior(MI);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000271 LR.LastUse = Spill;
272 LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000273 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000274 }
275
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000276 if (isKill)
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000277 killVirtReg(lri);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000278}
279
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000280/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000281void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000282 SmallVector<unsigned, 16> Dirty;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000283 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
284 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000285 if (i->second.Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000286 Dirty.push_back(i->first);
287 for (unsigned i = 0, e = Dirty.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000288 spillVirtReg(MI, Dirty[i], false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000289}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000290
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000291/// usePhysReg - Handle the direct use of a physical register.
292/// Check that the register is not used by a virtreg.
293/// Kill the physreg, marking it free.
294/// This may add implicit kills to MO->getParent() and invalidate MO.
295void RAFast::usePhysReg(MachineOperand &MO) {
296 unsigned PhysReg = MO.getReg();
297 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
298 "Bad usePhysReg operand");
299
300 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000301 case regDisabled:
302 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000303 case regReserved:
304 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000305 // Fall through
306 case regFree:
307 UsedInInstr.set(PhysReg);
308 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000309 return;
310 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000311 // The physreg was allocated to a virtual register. That means to value we
312 // wanted has been clobbered.
313 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000314 }
315
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000316 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000317 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
318 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000319 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000320 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000321 break;
322 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000323 assert(TRI->isSuperRegister(PhysReg, Alias) &&
324 "Instruction is not using a subregister of a reserved register");
325 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000326 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000327 UsedInInstr.set(Alias);
328 MO.getParent()->addRegisterKilled(Alias, TRI, true);
329 return;
330 case regFree:
331 if (TRI->isSuperRegister(PhysReg, Alias)) {
332 // Leave the superregister in the working set.
333 UsedInInstr.set(Alias);
334 MO.getParent()->addRegisterKilled(Alias, TRI, true);
335 return;
336 }
337 // Some other alias was in the working set - clear it.
338 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000339 break;
340 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000341 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000342 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000343 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000344
345 // All aliases are disabled, bring register into working set.
346 PhysRegState[PhysReg] = regFree;
347 UsedInInstr.set(PhysReg);
348 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000349}
350
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000351/// definePhysReg - Mark PhysReg as reserved or free after spilling any
352/// virtregs. This is very similar to defineVirtReg except the physreg is
353/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000354void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
355 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000356 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000357 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
358 case regDisabled:
359 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000360 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000361 spillVirtReg(MI, VirtReg, true);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000362 // Fall through.
363 case regFree:
364 case regReserved:
365 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000366 return;
367 }
368
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000369 // This is a disabled register, disable all aliases.
370 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000371 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
372 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000373 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000374 switch (unsigned VirtReg = PhysRegState[Alias]) {
375 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000376 break;
377 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000378 spillVirtReg(MI, VirtReg, true);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379 // Fall through.
380 case regFree:
381 case regReserved:
382 PhysRegState[Alias] = regDisabled;
383 if (TRI->isSuperRegister(PhysReg, Alias))
384 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000385 break;
386 }
387 }
388}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000389
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000390
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000391/// assignVirtToPhysReg - This method updates local state so that we know
392/// that PhysReg is the proper container for VirtReg now. The physical
393/// register must not be used for anything else when this is called.
394///
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000395RAFast::LiveRegMap::iterator
396RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000397 DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000398 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000399 PhysRegState[PhysReg] = VirtReg;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000400 return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000401}
402
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000403/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000404RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000405 unsigned VirtReg,
406 unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000407 const unsigned spillCost = 100;
408 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
409 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000410
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000411 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000412 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
413 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000414
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000415 // Ignore invalid hints.
416 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Chandler Carruth2c13ab22010-05-15 10:23:23 +0000417 !RC->contains(Hint) || UsedInInstr.test(Hint) ||
418 !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000419 Hint = 0;
420
421 // If there is no hint, peek at the first use of this register.
422 if (!Hint && !MRI->use_nodbg_empty(VirtReg)) {
423 MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg);
424 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
425 // Copy to physreg -> use physreg as hint.
426 if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
427 SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000428 RC->contains(DstReg) && !UsedInInstr.test(DstReg) &&
429 Allocatable.test(DstReg)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000430 Hint = DstReg;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000431 DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000432 }
433 }
434
435 // Take hint when possible.
436 if (Hint) {
437 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000438 Allocatable.test(Hint) && "Invalid hint should have been cleared");
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000439 switch(PhysRegState[Hint]) {
440 case regDisabled:
441 case regReserved:
442 break;
443 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000444 spillVirtReg(MI, PhysRegState[Hint], true);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000445 // Fall through.
446 case regFree:
447 return assignVirtToPhysReg(VirtReg, Hint);
448 }
449 }
450
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000451 // First try to find a completely free register.
452 unsigned BestCost = 0, BestReg = 0;
453 bool hasDisabled = false;
454 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
455 unsigned PhysReg = *I;
456 switch(PhysRegState[PhysReg]) {
457 case regDisabled:
458 hasDisabled = true;
459 case regReserved:
460 continue;
461 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000462 if (!UsedInInstr.test(PhysReg))
463 return assignVirtToPhysReg(VirtReg, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000464 continue;
465 default:
466 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000467 if (!BestReg && !UsedInInstr.test(PhysReg))
468 BestReg = PhysReg, BestCost = spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000469 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000470 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000471 }
472
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000473 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000474 << " candidate=" << TRI->getName(BestReg) << "\n");
475
476 // Try to extend the working set for RC if there were any disabled registers.
477 if (hasDisabled && (!BestReg || BestCost >= spillCost)) {
478 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
479 unsigned PhysReg = *I;
480 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
481 continue;
482
483 // Calculate the cost of bringing PhysReg into the working set.
484 unsigned Cost=0;
485 bool Impossible = false;
486 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
487 unsigned Alias = *AS; ++AS) {
488 if (UsedInInstr.test(Alias)) {
489 Impossible = true;
490 break;
491 }
492 switch (PhysRegState[Alias]) {
493 case regDisabled:
494 break;
495 case regReserved:
496 Impossible = true;
497 break;
498 case regFree:
499 Cost++;
500 break;
501 default:
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000502 Cost += spillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 break;
504 }
505 }
506 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000507 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000508 << " cost=" << Cost << "\n");
509 if (!BestReg || Cost < BestCost) {
510 BestReg = PhysReg;
511 BestCost = Cost;
512 if (Cost < spillCost) break;
513 }
514 }
515 }
516
517 if (BestReg) {
518 // BestCost is 0 when all aliases are already disabled.
519 if (BestCost) {
520 if (PhysRegState[BestReg] != regDisabled)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000521 spillVirtReg(MI, PhysRegState[BestReg], true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000522 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000523 // Make sure all aliases are disabled.
524 for (const unsigned *AS = TRI->getAliasSet(BestReg);
525 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000526 switch (PhysRegState[Alias]) {
527 case regDisabled:
528 continue;
529 case regFree:
530 PhysRegState[Alias] = regDisabled;
531 break;
532 default:
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000533 spillVirtReg(MI, PhysRegState[Alias], true);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000534 PhysRegState[Alias] = regDisabled;
535 break;
536 }
537 }
538 }
539 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000540 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000541 }
542
543 // Nothing we can do.
544 std::string msg;
545 raw_string_ostream Msg(msg);
546 Msg << "Ran out of registers during register allocation!";
547 if (MI->isInlineAsm()) {
548 Msg << "\nPlease check your inline asm statement for "
549 << "invalid constraints:\n";
550 MI->print(Msg, TM);
551 }
552 report_fatal_error(Msg.str());
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000553 return LiveVirtRegs.end();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000554}
555
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000556/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000557unsigned RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
558 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000559 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
560 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000561 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
562 if (lri == LiveVirtRegs.end())
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000563 lri = allocVirtReg(MI, VirtReg, Hint);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000564 else
565 addKillFlag(lri); // Kill before redefine.
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000566 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000567 LR.LastUse = MI;
568 LR.LastOpNum = OpNum;
569 LR.Dirty = true;
570 UsedInInstr.set(LR.PhysReg);
571 return LR.PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000572}
573
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000574/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000575unsigned RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
576 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000577 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
578 "Not a virtual register");
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000579 LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg);
580 if (lri == LiveVirtRegs.end()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000581 lri = allocVirtReg(MI, VirtReg, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000582 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000583 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000584 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000585 << TRI->getName(lri->second.PhysReg) << "\n");
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000586 TII->loadRegFromStackSlot(*MBB, MI, lri->second.PhysReg, FrameIndex, RC,
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000587 TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000588 ++NumLoads;
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000589 } else if (lri->second.Dirty) {
590 MachineOperand &MO = MI->getOperand(OpNum);
591 if (isLastUseOfLocalReg(MO)) {
592 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
593 MO.setIsKill();
594 } else if (MO.isKill()) {
595 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
596 MO.setIsKill(false);
597 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000598 }
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000599 LiveReg &LR = lri->second;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000600 LR.LastUse = MI;
601 LR.LastOpNum = OpNum;
602 UsedInInstr.set(LR.PhysReg);
603 return LR.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000604}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000605
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000606// setPhysReg - Change MO the refer the PhysReg, considering subregs.
607void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
608 if (unsigned Idx = MO.getSubReg()) {
609 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0);
610 MO.setSubReg(0);
611 } else
612 MO.setReg(PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000613}
614
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000615void RAFast::AllocateBasicBlock() {
616 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000617
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000618 atEndOfBlock = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000619 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000620 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000621
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000622 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000623
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000624 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000625 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
626 E = MBB->livein_end(); I != E; ++I)
627 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000628
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000629 SmallVector<unsigned, 8> VirtKills, PhysDefs;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000630 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000631
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000632 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000633 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000634 MachineInstr *MI = MII++;
635 const TargetInstrDesc &TID = MI->getDesc();
636 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000637 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000638 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
639 if (PhysRegState[Reg] == regDisabled) continue;
640 dbgs() << " " << TRI->getName(Reg);
641 switch(PhysRegState[Reg]) {
642 case regFree:
643 break;
644 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000645 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000646 break;
647 default:
648 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000649 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000650 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000651 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000652 "Bad inverse map");
653 break;
654 }
655 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000656 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000657 // Check that LiveVirtRegs is the inverse.
658 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
659 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000660 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
661 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000662 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000663 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000664 assert(PhysRegState[i->second.PhysReg] == i->first &&
665 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000666 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000667 });
668
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000669 // Debug values are not allowed to change codegen in any way.
670 if (MI->isDebugValue()) {
671 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
672 MachineOperand &MO = MI->getOperand(i);
673 if (!MO.isReg()) continue;
674 unsigned Reg = MO.getReg();
675 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen1a1ad572010-05-12 00:11:19 +0000676 LiveRegMap::iterator lri = LiveVirtRegs.find(Reg);
677 if (lri != LiveVirtRegs.end())
678 setPhysReg(MO, lri->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000679 else
680 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000681 }
682 // Next instruction.
683 continue;
684 }
685
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000686 // If this is a copy, we may be able to coalesce.
687 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
688 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
689 CopySrc = CopyDst = 0;
690
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000691 // Track registers used by instruction.
692 UsedInInstr.reset();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000693 PhysDefs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000694
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000695 // First scan.
696 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000697 // Find the end of the virtreg operands
698 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000699 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
700 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000701 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000702 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000703 if (!Reg) continue;
704 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
705 VirtOpEnd = i+1;
706 continue;
707 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000708 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000709 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000710 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000711 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000712 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000713 PhysDefs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000714 }
715 }
716
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000717 // Second scan.
718 // Allocate virtreg uses and early clobbers.
719 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000720 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000721 MachineOperand &MO = MI->getOperand(i);
722 if (!MO.isReg()) continue;
723 unsigned Reg = MO.getReg();
724 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
725 if (MO.isUse()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000726 unsigned PhysReg = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000727 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000728 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000729 if (MO.isKill())
730 VirtKills.push_back(Reg);
731 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000732 unsigned PhysReg = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000733 setPhysReg(MO, PhysReg);
734 PhysDefs.push_back(PhysReg);
735 }
736 }
737
738 // Process virtreg kills
739 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
740 killVirtReg(VirtKills[i]);
741 VirtKills.clear();
742
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000743 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000744
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000745 // Track registers defined by instruction - early clobbers at this point.
746 UsedInInstr.reset();
747 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
748 unsigned PhysReg = PhysDefs[i];
749 UsedInInstr.set(PhysReg);
750 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
751 unsigned Alias = *AS; ++AS)
752 UsedInInstr.set(Alias);
753 }
754
755 // Third scan.
756 // Allocate defs and collect dead defs.
757 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
758 MachineOperand &MO = MI->getOperand(i);
759 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
760 unsigned Reg = MO.getReg();
761
762 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000763 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000764 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
765 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000766 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000767 }
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000768 unsigned PhysReg = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000769 if (MO.isDead()) {
770 VirtKills.push_back(Reg);
771 CopyDst = 0; // cancel coalescing;
772 } else
773 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000774 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000775 }
776
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000777 // Spill all dirty virtregs before a call, in case of an exception.
778 if (TID.isCall()) {
779 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000780 spillAll(MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000781 }
782
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000783 // Process virtreg deads.
784 for (unsigned i = 0, e = VirtKills.size(); i != e; ++i)
785 killVirtReg(VirtKills[i]);
786 VirtKills.clear();
787
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000788 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000789
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000790 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
791 DEBUG(dbgs() << "-- coalescing: " << *MI);
792 Coalesced.push_back(MI);
793 } else {
794 DEBUG(dbgs() << "<< " << *MI);
795 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000796 }
797
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000798 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000799 atEndOfBlock = true;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000800 MachineBasicBlock::iterator MI = MBB->getFirstTerminator();
801 if (MI != MBB->end() && MI->getDesc().isReturn()) {
Jakob Stoklund Olesen6a6328b2010-05-14 22:40:43 +0000802 // This is a return block, kill all virtual registers.
803 DEBUG(dbgs() << "Killing live registers at end of return block.\n");
804 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
805 i != e; ++i)
806 killVirtReg(i);
807 } else {
808 // This is a normal block, spill any dirty virtregs.
809 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
810 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
811 i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000812 spillVirtReg(MI, i, true);
Jakob Stoklund Olesen6a6328b2010-05-14 22:40:43 +0000813 }
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000814 LiveVirtRegs.clear();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000815
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000816 // Erase all the coalesced copies. We are delaying it until now because
817 // LiveVirtsRegs might refer to the instrs.
818 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000819 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000820 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000821
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000822 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000823}
824
825/// runOnMachineFunction - Register allocate the whole function
826///
827bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000828 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
829 << "********** Function: "
830 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000831 if (VerifyFastRegalloc)
Jakob Stoklund Olesena0e618d2010-05-14 21:55:44 +0000832 Fn.verify(this, true);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000833 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000834 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000835 TM = &Fn.getTarget();
836 TRI = TM->getRegisterInfo();
837 TII = TM->getInstrInfo();
838
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000839 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000840 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000841
842 // initialize the virtual->physical register map to have a 'null'
843 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000844 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000845 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000846
847 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000848 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
849 MBBi != MBBe; ++MBBi) {
850 MBB = &*MBBi;
851 AllocateBasicBlock();
852 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000853
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}