blob: 04aa47e158de750bdecd4a1f77a13be33f306811 [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),
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +000053 isBulkSpilling(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 Olesene6aba832010-05-17 02:07:32 +0000116 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
117 // completely after spilling all live registers. LiveRegMap entries should
118 // not be erased.
119 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000120
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 Olesen844db9c2010-05-17 02:49:15 +0000140 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000141 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000142 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000143 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000144
145 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000146 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000147 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
148 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000149 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
150 unsigned VirtReg, unsigned Hint);
151 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
152 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000153 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000154 bool setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000155 };
156 char RAFast::ID = 0;
157}
158
159/// getStackSpaceFor - This allocates space for the specified virtual register
160/// to be held on the stack.
161int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
162 // Find the location Reg would belong...
163 int SS = StackSlotForVirtReg[VirtReg];
164 if (SS != -1)
165 return SS; // Already has space allocated?
166
167 // Allocate a new stack object for this spill location...
168 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
169 RC->getAlignment());
170
171 // Assign the slot.
172 StackSlotForVirtReg[VirtReg] = FrameIdx;
173 return FrameIdx;
174}
175
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000176/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
177/// its virtual register, and it is guaranteed to be a block-local register.
178///
179bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
180 // Check for non-debug uses or defs following MO.
181 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000182 MachineOperand *Next = &MO;
183 while ((Next = Next->getNextOperandForReg()))
184 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000185 return false;
186
187 // If the register has ever been spilled or reloaded, we conservatively assume
188 // it is a global register used in multiple blocks.
189 if (StackSlotForVirtReg[MO.getReg()] != -1)
190 return false;
191
192 // Check that the use/def chain has exactly one operand - MO.
193 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
194}
195
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000196/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000197void RAFast::addKillFlag(const LiveReg &LR) {
198 if (!LR.LastUse) return;
199 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
200 if (MO.isDef())
201 MO.setIsDead();
202 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
203 MO.setIsKill();
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000204}
205
206/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000207void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
208 addKillFlag(LRI->second);
209 const LiveReg &LR = LRI->second;
210 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000211 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000212 // Erase from LiveVirtRegs unless we're spilling in bulk.
213 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000214 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000215}
216
217/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000218void RAFast::killVirtReg(unsigned VirtReg) {
219 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
220 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000221 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
222 if (LRI != LiveVirtRegs.end())
223 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000224}
225
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000226/// spillVirtReg - This method spills the value specified by VirtReg into the
227/// corresponding stack slot if needed. If isKill is set, the register is also
228/// killed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000229void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000230 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
231 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000232 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
233 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
234 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000235}
236
237/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000238void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000239 LiveRegMap::iterator LRI) {
240 LiveReg &LR = LRI->second;
241 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000242
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000243 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000244 // If this physreg is used by the instruction, we want to kill it on the
245 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000246 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000247 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000248 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000249 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000250 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
251 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000252 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000253 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000254 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000255
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000256 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000257 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000258 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000259 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000260}
261
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000262/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000263void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000264 isBulkSpilling = true;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000265 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
266 e = LiveVirtRegs.end(); i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000267 spillVirtReg(MI, i);
268 LiveVirtRegs.clear();
269 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000270}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000271
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000272/// usePhysReg - Handle the direct use of a physical register.
273/// Check that the register is not used by a virtreg.
274/// Kill the physreg, marking it free.
275/// This may add implicit kills to MO->getParent() and invalidate MO.
276void RAFast::usePhysReg(MachineOperand &MO) {
277 unsigned PhysReg = MO.getReg();
278 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
279 "Bad usePhysReg operand");
280
281 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000282 case regDisabled:
283 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000284 case regReserved:
285 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000286 // Fall through
287 case regFree:
288 UsedInInstr.set(PhysReg);
289 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000290 return;
291 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000292 // The physreg was allocated to a virtual register. That means to value we
293 // wanted has been clobbered.
294 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000295 }
296
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000297 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000298 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
299 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000300 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000301 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000302 break;
303 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000304 assert(TRI->isSuperRegister(PhysReg, Alias) &&
305 "Instruction is not using a subregister of a reserved register");
306 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000307 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000308 UsedInInstr.set(Alias);
309 MO.getParent()->addRegisterKilled(Alias, TRI, true);
310 return;
311 case regFree:
312 if (TRI->isSuperRegister(PhysReg, Alias)) {
313 // Leave the superregister in the working set.
314 UsedInInstr.set(Alias);
315 MO.getParent()->addRegisterKilled(Alias, TRI, true);
316 return;
317 }
318 // Some other alias was in the working set - clear it.
319 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000320 break;
321 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000322 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000323 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000324 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000325
326 // All aliases are disabled, bring register into working set.
327 PhysRegState[PhysReg] = regFree;
328 UsedInInstr.set(PhysReg);
329 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000330}
331
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000332/// definePhysReg - Mark PhysReg as reserved or free after spilling any
333/// virtregs. This is very similar to defineVirtReg except the physreg is
334/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000335void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
336 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000337 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000338 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
339 case regDisabled:
340 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000341 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000342 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000343 // Fall through.
344 case regFree:
345 case regReserved:
346 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000347 return;
348 }
349
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000350 // This is a disabled register, disable all aliases.
351 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000352 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
353 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000354 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000355 switch (unsigned VirtReg = PhysRegState[Alias]) {
356 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000357 break;
358 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000359 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000360 // Fall through.
361 case regFree:
362 case regReserved:
363 PhysRegState[Alias] = regDisabled;
364 if (TRI->isSuperRegister(PhysReg, Alias))
365 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000366 break;
367 }
368 }
369}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000370
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000371
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000372/// assignVirtToPhysReg - This method updates local state so that we know
373/// that PhysReg is the proper container for VirtReg now. The physical
374/// register must not be used for anything else when this is called.
375///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000376void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
377 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000378 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000379 PhysRegState[PhysReg] = LRE.first;
380 assert(!LRE.second.PhysReg && "Already assigned a physreg");
381 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000382}
383
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000384/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000385void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000386 const unsigned SpillCost = 100;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000387 const unsigned VirtReg = LRE.first;
388
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000389 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
390 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000391
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000392 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000393 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
394 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000395
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000396 // Ignore invalid hints.
397 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Chandler Carruth2c13ab22010-05-15 10:23:23 +0000398 !RC->contains(Hint) || UsedInInstr.test(Hint) ||
399 !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000400 Hint = 0;
401
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000402 // Take hint when possible.
403 if (Hint) {
404 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000405 Allocatable.test(Hint) && "Invalid hint should have been cleared");
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000406 switch(PhysRegState[Hint]) {
407 case regDisabled:
408 case regReserved:
409 break;
410 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000411 spillVirtReg(MI, PhysRegState[Hint]);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000412 // Fall through.
413 case regFree:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000414 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000415 }
416 }
417
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000418 // First try to find a completely free register.
419 unsigned BestCost = 0, BestReg = 0;
420 bool hasDisabled = false;
421 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
422 unsigned PhysReg = *I;
423 switch(PhysRegState[PhysReg]) {
424 case regDisabled:
425 hasDisabled = true;
426 case regReserved:
427 continue;
428 case regFree:
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000429 if (!UsedInInstr.test(PhysReg))
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000430 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000431 continue;
432 default:
433 // Grab the first spillable register we meet.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000434 if (!BestReg && !UsedInInstr.test(PhysReg))
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000435 BestReg = PhysReg, BestCost = SpillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000436 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000437 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000438 }
439
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000440 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000441 << " candidate=" << TRI->getName(BestReg) << "\n");
442
443 // Try to extend the working set for RC if there were any disabled registers.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000444 if (hasDisabled && (!BestReg || BestCost >= SpillCost)) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000445 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
446 unsigned PhysReg = *I;
447 if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg))
448 continue;
449
450 // Calculate the cost of bringing PhysReg into the working set.
451 unsigned Cost=0;
452 bool Impossible = false;
453 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
454 unsigned Alias = *AS; ++AS) {
455 if (UsedInInstr.test(Alias)) {
456 Impossible = true;
457 break;
458 }
459 switch (PhysRegState[Alias]) {
460 case regDisabled:
461 break;
462 case regReserved:
463 Impossible = true;
464 break;
465 case regFree:
466 Cost++;
467 break;
468 default:
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000469 Cost += SpillCost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000470 break;
471 }
472 }
473 if (Impossible) continue;
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000474 DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000475 << " cost=" << Cost << "\n");
476 if (!BestReg || Cost < BestCost) {
477 BestReg = PhysReg;
478 BestCost = Cost;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000479 if (Cost < SpillCost) break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000480 }
481 }
482 }
483
484 if (BestReg) {
485 // BestCost is 0 when all aliases are already disabled.
486 if (BestCost) {
487 if (PhysRegState[BestReg] != regDisabled)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000488 spillVirtReg(MI, PhysRegState[BestReg]);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000489 else {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000490 // Make sure all aliases are disabled.
491 for (const unsigned *AS = TRI->getAliasSet(BestReg);
492 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000493 switch (PhysRegState[Alias]) {
494 case regDisabled:
495 continue;
496 case regFree:
497 PhysRegState[Alias] = regDisabled;
498 break;
499 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000500 spillVirtReg(MI, PhysRegState[Alias]);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000501 PhysRegState[Alias] = regDisabled;
502 break;
503 }
504 }
505 }
506 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000507 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000508 }
509
510 // Nothing we can do.
511 std::string msg;
512 raw_string_ostream Msg(msg);
513 Msg << "Ran out of registers during register allocation!";
514 if (MI->isInlineAsm()) {
515 Msg << "\nPlease check your inline asm statement for "
516 << "invalid constraints:\n";
517 MI->print(Msg, TM);
518 }
519 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000520}
521
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000522/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000523RAFast::LiveRegMap::iterator
524RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
525 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000526 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
527 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000528 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000529 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000530 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
531 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000532 if (New) {
533 // If there is no hint, peek at the only use of this register.
534 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
535 MRI->hasOneNonDBGUse(VirtReg)) {
536 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
537 // It's a copy, use the destination register as a hint.
538 if (TII->isMoveInstr(*MRI->use_nodbg_begin(VirtReg),
539 SrcReg, DstReg, SrcSubReg, DstSubReg))
540 Hint = DstReg;
541 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000542 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000543 } else
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000544 addKillFlag(LR); // Kill before redefine.
545 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000546 LR.LastUse = MI;
547 LR.LastOpNum = OpNum;
548 LR.Dirty = true;
549 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000550 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000551}
552
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000553/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000554RAFast::LiveRegMap::iterator
555RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
556 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000557 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
558 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000559 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000560 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000561 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
562 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000563 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000564 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000565 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000566 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000567 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000568 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000569 << TRI->getName(LR.PhysReg) << "\n");
570 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000571 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000572 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000573 if (isLastUseOfLocalReg(MO)) {
574 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
575 MO.setIsKill();
576 } else if (MO.isKill()) {
577 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
578 MO.setIsKill(false);
579 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000580 } else if (MO.isKill()) {
581 // We must remove kill flags from uses of reloaded registers because the
582 // register would be killed immediately, and there might be a second use:
583 // %foo = OR %x<kill>, %x
584 // This would cause a second reload of %x into a different register.
585 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
586 MO.setIsKill(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000587 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000588 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000589 LR.LastUse = MI;
590 LR.LastOpNum = OpNum;
591 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000592 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000593}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000594
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000595// setPhysReg - Change MO the refer the PhysReg, considering subregs.
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000596// This may invalidate MO if it is necessary to add implicit kills for a
597// superregister.
598// Return tru if MO kills its register.
599bool RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
600 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000601 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000602 return MO.isKill() || MO.isDead();
603 }
604
605 // Handle subregister index.
606 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
607 MO.setSubReg(0);
608 if (MO.isUse()) {
609 if (MO.isKill()) {
610 MO.getParent()->addRegisterKilled(PhysReg, TRI, true);
611 return true;
612 }
613 return false;
614 }
615 // A subregister def implicitly defines the whole physreg.
616 if (MO.isDead()) {
617 MO.getParent()->addRegisterDead(PhysReg, TRI, true);
618 return true;
619 }
620 MO.getParent()->addRegisterDefined(PhysReg, TRI);
621 return false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000622}
623
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000624void RAFast::AllocateBasicBlock() {
625 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000626
627 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000628 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000629
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000630 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000631
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000632 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000633 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
634 E = MBB->livein_end(); I != E; ++I)
635 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000636
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000637 SmallVector<unsigned, 8> PhysECs;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000638 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000639
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000640 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000641 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000642 MachineInstr *MI = MII++;
643 const TargetInstrDesc &TID = MI->getDesc();
644 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000645 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000646 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
647 if (PhysRegState[Reg] == regDisabled) continue;
648 dbgs() << " " << TRI->getName(Reg);
649 switch(PhysRegState[Reg]) {
650 case regFree:
651 break;
652 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000653 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000654 break;
655 default:
656 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000657 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000658 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000659 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000660 "Bad inverse map");
661 break;
662 }
663 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000664 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000665 // Check that LiveVirtRegs is the inverse.
666 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
667 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000668 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
669 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000670 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000671 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000672 assert(PhysRegState[i->second.PhysReg] == i->first &&
673 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000674 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000675 });
676
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000677 // Debug values are not allowed to change codegen in any way.
678 if (MI->isDebugValue()) {
679 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
680 MachineOperand &MO = MI->getOperand(i);
681 if (!MO.isReg()) continue;
682 unsigned Reg = MO.getReg();
683 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000684 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
685 if (LRI != LiveVirtRegs.end())
686 setPhysReg(MO, LRI->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000687 else
688 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000689 }
690 // Next instruction.
691 continue;
692 }
693
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000694 // If this is a copy, we may be able to coalesce.
695 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
696 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
697 CopySrc = CopyDst = 0;
698
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000699 // Track registers used by instruction.
700 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000701 PhysECs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000702
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000703 // First scan.
704 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000705 // Find the end of the virtreg operands
706 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000707 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
708 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000709 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000710 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000711 if (!Reg) continue;
712 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
713 VirtOpEnd = i+1;
714 continue;
715 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000716 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000717 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000718 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000719 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000720 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000721 PhysECs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000722 }
723 }
724
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000725 // Second scan.
726 // Allocate virtreg uses and early clobbers.
727 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000728 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000729 MachineOperand &MO = MI->getOperand(i);
730 if (!MO.isReg()) continue;
731 unsigned Reg = MO.getReg();
732 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
733 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000734 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
735 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000736 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000737 if (setPhysReg(MO, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000738 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000739 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000740 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
741 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000742 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000743 PhysECs.push_back(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000744 }
745 }
746
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000747 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000748
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000749 // Track registers defined by instruction - early clobbers at this point.
750 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000751 for (unsigned i = 0, e = PhysECs.size(); i != e; ++i) {
752 unsigned PhysReg = PhysECs[i];
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000753 UsedInInstr.set(PhysReg);
754 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
755 unsigned Alias = *AS; ++AS)
756 UsedInInstr.set(Alias);
757 }
758
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000759 unsigned DefOpEnd = MI->getNumOperands();
760 if (TID.isCall()) {
761 // Spill all virtregs before a call. This serves two purposes: 1. If an
762 // exception is thrown, the landing pad is going to expect to find registers
763 // in their spill slots, and 2. we don't have to wade through all the
764 // <imp-def> operands on the call instruction.
765 DefOpEnd = VirtOpEnd;
766 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
767 spillAll(MI);
768 }
769
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000770 // Third scan.
771 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000772 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000773 MachineOperand &MO = MI->getOperand(i);
774 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
775 unsigned Reg = MO.getReg();
776
777 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000778 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000779 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
780 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000781 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000782 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000783 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
784 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000785 if (setPhysReg(MO, PhysReg)) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000786 killVirtReg(LRI);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000787 CopyDst = 0; // cancel coalescing;
788 } else
789 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000790 }
791
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000792 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000793
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000794 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
795 DEBUG(dbgs() << "-- coalescing: " << *MI);
796 Coalesced.push_back(MI);
797 } else {
798 DEBUG(dbgs() << "<< " << *MI);
799 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000800 }
801
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000802 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000803 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
804 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000805
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000806 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000807 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000808 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000809 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000810 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000811
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000812 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000813}
814
815/// runOnMachineFunction - Register allocate the whole function
816///
817bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000818 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
819 << "********** Function: "
820 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000821 if (VerifyFastRegalloc)
Jakob Stoklund Olesena0e618d2010-05-14 21:55:44 +0000822 Fn.verify(this, true);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000823 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000824 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000825 TM = &Fn.getTarget();
826 TRI = TM->getRegisterInfo();
827 TII = TM->getInstrInfo();
828
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000829 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000830 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000831
832 // initialize the virtual->physical register map to have a 'null'
833 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000834 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000835 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000836
837 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000838 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
839 MBBi != MBBe; ++MBBi) {
840 MBB = &*MBBi;
841 AllocateBasicBlock();
842 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000843
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000844 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000845 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000846
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000847 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000848 return true;
849}
850
851FunctionPass *llvm::createFastRegisterAllocator() {
852 return new RAFast();
853}