blob: 3270b5620263de11edfca3a34308c36c90c2a7e0 [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
38STATISTIC(NumStores, "Number of stores added");
39STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000040STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000041
42static RegisterRegAlloc
43 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
44
45namespace {
46 class RAFast : public MachineFunctionPass {
47 public:
48 static char ID;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +000049 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1),
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +000050 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000051 private:
52 const TargetMachine *TM;
53 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000054 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000055 const TargetRegisterInfo *TRI;
56 const TargetInstrInfo *TII;
57
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000058 // Basic block currently being allocated.
59 MachineBasicBlock *MBB;
60
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000061 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
62 // values are spilled.
63 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
64
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000065 // Everything we know about a live virtual register.
66 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000067 MachineInstr *LastUse; // Last instr to use reg.
68 unsigned PhysReg; // Currently held here.
69 unsigned short LastOpNum; // OpNum on LastUse.
70 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000071
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000072 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000073 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000074 };
75
76 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000077 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000078
79 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000080 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000081 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000082
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000083 // RegState - Track the state of a physical register.
84 enum RegState {
85 // A disabled register is not available for allocation, but an alias may
86 // be in use. A register can only be moved out of the disabled state if
87 // all aliases are disabled.
88 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000089
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000090 // A free register is not currently in use and can be allocated
91 // immediately without checking aliases.
92 regFree,
93
94 // A reserved register has been assigned expolicitly (e.g., setting up a
95 // call parameter), and it remains reserved until it is used.
96 regReserved
97
98 // A register state may also be a virtual register number, indication that
99 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000100 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000101 };
102
103 // PhysRegState - One of the RegState enums, or a virtreg.
104 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000105
106 // UsedInInstr - BitVector of physregs that are used in the current
107 // instruction, and so cannot be allocated.
108 BitVector UsedInInstr;
109
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000110 // Allocatable - vector of allocatable physical registers.
111 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000112
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000113 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
114 // completely after spilling all live registers. LiveRegMap entries should
115 // not be erased.
116 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000117
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000118 enum {
119 spillClean = 1,
120 spillDirty = 100,
121 spillImpossible = ~0u
122 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000123 public:
124 virtual const char *getPassName() const {
125 return "Fast Register Allocator";
126 }
127
128 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
129 AU.setPreservesCFG();
130 AU.addRequiredID(PHIEliminationID);
131 AU.addRequiredID(TwoAddressInstructionPassID);
132 MachineFunctionPass::getAnalysisUsage(AU);
133 }
134
135 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000136 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000137 void AllocateBasicBlock();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000138 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000139 bool isLastUseOfLocalReg(MachineOperand&);
140
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000141 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000142 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000143 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000144 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000145 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000146
147 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000148 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000149 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000150 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
151 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000152 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
153 unsigned VirtReg, unsigned Hint);
154 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
155 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000156 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000157 bool setPhysReg(MachineOperand &MO, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000158 };
159 char RAFast::ID = 0;
160}
161
162/// getStackSpaceFor - This allocates space for the specified virtual register
163/// to be held on the stack.
164int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
165 // Find the location Reg would belong...
166 int SS = StackSlotForVirtReg[VirtReg];
167 if (SS != -1)
168 return SS; // Already has space allocated?
169
170 // Allocate a new stack object for this spill location...
171 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
172 RC->getAlignment());
173
174 // Assign the slot.
175 StackSlotForVirtReg[VirtReg] = FrameIdx;
176 return FrameIdx;
177}
178
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000179/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
180/// its virtual register, and it is guaranteed to be a block-local register.
181///
182bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
183 // Check for non-debug uses or defs following MO.
184 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000185 MachineOperand *Next = &MO;
186 while ((Next = Next->getNextOperandForReg()))
187 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000188 return false;
189
190 // If the register has ever been spilled or reloaded, we conservatively assume
191 // it is a global register used in multiple blocks.
192 if (StackSlotForVirtReg[MO.getReg()] != -1)
193 return false;
194
195 // Check that the use/def chain has exactly one operand - MO.
196 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
197}
198
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000199/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000200void RAFast::addKillFlag(const LiveReg &LR) {
201 if (!LR.LastUse) return;
202 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
203 if (MO.isDef())
204 MO.setIsDead();
205 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
206 MO.setIsKill();
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000207}
208
209/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000210void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
211 addKillFlag(LRI->second);
212 const LiveReg &LR = LRI->second;
213 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000214 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000215 // Erase from LiveVirtRegs unless we're spilling in bulk.
216 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000217 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000218}
219
220/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000221void RAFast::killVirtReg(unsigned VirtReg) {
222 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
223 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000224 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
225 if (LRI != LiveVirtRegs.end())
226 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000227}
228
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000229/// spillVirtReg - This method spills the value specified by VirtReg into the
230/// corresponding stack slot if needed. If isKill is set, the register is also
231/// killed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000232void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000233 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
234 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000235 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
236 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
237 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000238}
239
240/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000241void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000242 LiveRegMap::iterator LRI) {
243 LiveReg &LR = LRI->second;
244 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000245
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000246 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000247 // If this physreg is used by the instruction, we want to kill it on the
248 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000249 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000250 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000251 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000252 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000253 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
254 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000255 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000256 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000257 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000258
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000259 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000260 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000261 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000262 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000263}
264
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000265/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000266void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000267 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000268 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000269 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
270 // of spilling here is deterministic, if arbitrary.
271 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
272 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000273 spillVirtReg(MI, i);
274 LiveVirtRegs.clear();
275 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000276}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000277
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000278/// usePhysReg - Handle the direct use of a physical register.
279/// Check that the register is not used by a virtreg.
280/// Kill the physreg, marking it free.
281/// This may add implicit kills to MO->getParent() and invalidate MO.
282void RAFast::usePhysReg(MachineOperand &MO) {
283 unsigned PhysReg = MO.getReg();
284 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
285 "Bad usePhysReg operand");
286
287 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000288 case regDisabled:
289 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000290 case regReserved:
291 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000292 // Fall through
293 case regFree:
294 UsedInInstr.set(PhysReg);
295 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000296 return;
297 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000298 // The physreg was allocated to a virtual register. That means to value we
299 // wanted has been clobbered.
300 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000301 }
302
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000303 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000304 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
305 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000306 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000307 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000308 break;
309 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000310 assert(TRI->isSuperRegister(PhysReg, Alias) &&
311 "Instruction is not using a subregister of a reserved register");
312 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000313 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000314 UsedInInstr.set(Alias);
315 MO.getParent()->addRegisterKilled(Alias, TRI, true);
316 return;
317 case regFree:
318 if (TRI->isSuperRegister(PhysReg, Alias)) {
319 // Leave the superregister in the working set.
320 UsedInInstr.set(Alias);
321 MO.getParent()->addRegisterKilled(Alias, TRI, true);
322 return;
323 }
324 // Some other alias was in the working set - clear it.
325 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000326 break;
327 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000328 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000329 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000330 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000331
332 // All aliases are disabled, bring register into working set.
333 PhysRegState[PhysReg] = regFree;
334 UsedInInstr.set(PhysReg);
335 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000336}
337
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000338/// definePhysReg - Mark PhysReg as reserved or free after spilling any
339/// virtregs. This is very similar to defineVirtReg except the physreg is
340/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000341void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
342 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000343 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000344 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
345 case regDisabled:
346 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000347 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000348 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000349 // Fall through.
350 case regFree:
351 case regReserved:
352 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000353 return;
354 }
355
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000356 // This is a disabled register, disable all aliases.
357 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000358 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
359 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000360 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000361 switch (unsigned VirtReg = PhysRegState[Alias]) {
362 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000363 break;
364 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000365 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000366 // Fall through.
367 case regFree:
368 case regReserved:
369 PhysRegState[Alias] = regDisabled;
370 if (TRI->isSuperRegister(PhysReg, Alias))
371 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000372 break;
373 }
374 }
375}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000376
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000377
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000378// calcSpillCost - Return the cost of spilling clearing out PhysReg and
379// aliases so it is free for allocation.
380// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
381// can be allocated directly.
382// Returns spillImpossible when PhysReg or an alias can't be spilled.
383unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
384 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
385 case regDisabled:
386 break;
387 case regFree:
388 return 0;
389 case regReserved:
390 return spillImpossible;
391 default:
392 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
393 }
394
395 // This is a disabled register, add up const of aliases.
396 unsigned Cost = 0;
397 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
398 unsigned Alias = *AS; ++AS) {
399 switch (unsigned VirtReg = PhysRegState[Alias]) {
400 case regDisabled:
401 break;
402 case regFree:
403 ++Cost;
404 break;
405 case regReserved:
406 return spillImpossible;
407 default:
408 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
409 break;
410 }
411 }
412 return Cost;
413}
414
415
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000416/// assignVirtToPhysReg - This method updates local state so that we know
417/// that PhysReg is the proper container for VirtReg now. The physical
418/// register must not be used for anything else when this is called.
419///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000420void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
421 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000422 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000423 PhysRegState[PhysReg] = LRE.first;
424 assert(!LRE.second.PhysReg && "Already assigned a physreg");
425 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000426}
427
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000428/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000429void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000430 const unsigned VirtReg = LRE.first;
431
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000432 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
433 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000434
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000435 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000436
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000437 // Ignore invalid hints.
438 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Chandler Carruth2c13ab22010-05-15 10:23:23 +0000439 !RC->contains(Hint) || UsedInInstr.test(Hint) ||
440 !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000441 Hint = 0;
442
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000443 // Take hint when possible.
444 if (Hint) {
445 assert(RC->contains(Hint) && !UsedInInstr.test(Hint) &&
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000446 Allocatable.test(Hint) && "Invalid hint should have been cleared");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000447 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000448 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000449 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000450 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000451 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000452 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000453 case spillImpossible:
454 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000455 }
456 }
457
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000458 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
459 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
460
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000461 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000462 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
463 unsigned PhysReg = *I;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000464 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
465 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000466 }
467
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000468 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000469 << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000470
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000471 unsigned BestReg = 0, BestCost = spillImpossible;
472 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
Jakob Stoklund Olesenaa4b0152010-05-17 17:18:59 +0000473 if (UsedInInstr.test(*I)) continue;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000474 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000475 // Cost is 0 when all aliases are already disabled.
476 if (Cost == 0)
477 return assignVirtToPhysReg(LRE, *I);
478 if (Cost < BestCost)
479 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000480 }
481
482 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000483 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000484 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000485 }
486
487 // Nothing we can do.
488 std::string msg;
489 raw_string_ostream Msg(msg);
490 Msg << "Ran out of registers during register allocation!";
491 if (MI->isInlineAsm()) {
492 Msg << "\nPlease check your inline asm statement for "
493 << "invalid constraints:\n";
494 MI->print(Msg, TM);
495 }
496 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000497}
498
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000499/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000500RAFast::LiveRegMap::iterator
501RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
502 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
504 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000505 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000506 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000507 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
508 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000509 if (New) {
510 // If there is no hint, peek at the only use of this register.
511 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
512 MRI->hasOneNonDBGUse(VirtReg)) {
513 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
514 // It's a copy, use the destination register as a hint.
515 if (TII->isMoveInstr(*MRI->use_nodbg_begin(VirtReg),
516 SrcReg, DstReg, SrcSubReg, DstSubReg))
517 Hint = DstReg;
518 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000519 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000520 } else
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000521 addKillFlag(LR); // Kill before redefine.
522 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000523 LR.LastUse = MI;
524 LR.LastOpNum = OpNum;
525 LR.Dirty = true;
526 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000527 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000528}
529
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000530/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000531RAFast::LiveRegMap::iterator
532RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
533 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000534 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
535 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000536 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000537 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000538 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
539 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000540 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000541 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000542 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000543 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000544 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000545 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000546 << TRI->getName(LR.PhysReg) << "\n");
547 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000548 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000549 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000550 if (isLastUseOfLocalReg(MO)) {
551 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
552 MO.setIsKill();
553 } else if (MO.isKill()) {
554 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
555 MO.setIsKill(false);
556 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000557 } else if (MO.isKill()) {
558 // We must remove kill flags from uses of reloaded registers because the
559 // register would be killed immediately, and there might be a second use:
560 // %foo = OR %x<kill>, %x
561 // This would cause a second reload of %x into a different register.
562 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
563 MO.setIsKill(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000564 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000565 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000566 LR.LastUse = MI;
567 LR.LastOpNum = OpNum;
568 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000569 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000570}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000571
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000572// setPhysReg - Change MO the refer the PhysReg, considering subregs.
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000573// This may invalidate MO if it is necessary to add implicit kills for a
574// superregister.
575// Return tru if MO kills its register.
576bool RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) {
577 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000578 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000579 return MO.isKill() || MO.isDead();
580 }
581
582 // Handle subregister index.
583 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
584 MO.setSubReg(0);
585 if (MO.isUse()) {
586 if (MO.isKill()) {
587 MO.getParent()->addRegisterKilled(PhysReg, TRI, true);
588 return true;
589 }
590 return false;
591 }
592 // A subregister def implicitly defines the whole physreg.
593 if (MO.isDead()) {
594 MO.getParent()->addRegisterDead(PhysReg, TRI, true);
595 return true;
596 }
597 MO.getParent()->addRegisterDefined(PhysReg, TRI);
598 return false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000599}
600
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000601void RAFast::AllocateBasicBlock() {
602 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000603
604 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000605 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000606
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000607 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000608
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000609 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000610 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
611 E = MBB->livein_end(); I != E; ++I)
612 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000613
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000614 SmallVector<unsigned, 8> PhysECs;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000615 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000616
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000617 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000618 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000619 MachineInstr *MI = MII++;
620 const TargetInstrDesc &TID = MI->getDesc();
621 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000622 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000623 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
624 if (PhysRegState[Reg] == regDisabled) continue;
625 dbgs() << " " << TRI->getName(Reg);
626 switch(PhysRegState[Reg]) {
627 case regFree:
628 break;
629 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000630 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000631 break;
632 default:
633 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000634 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000635 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000636 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000637 "Bad inverse map");
638 break;
639 }
640 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000641 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000642 // Check that LiveVirtRegs is the inverse.
643 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
644 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000645 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
646 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000647 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000648 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000649 assert(PhysRegState[i->second.PhysReg] == i->first &&
650 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000651 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000652 });
653
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000654 // Debug values are not allowed to change codegen in any way.
655 if (MI->isDebugValue()) {
656 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
657 MachineOperand &MO = MI->getOperand(i);
658 if (!MO.isReg()) continue;
659 unsigned Reg = MO.getReg();
660 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000661 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
662 if (LRI != LiveVirtRegs.end())
663 setPhysReg(MO, LRI->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000664 else
665 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000666 }
667 // Next instruction.
668 continue;
669 }
670
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000671 // If this is a copy, we may be able to coalesce.
672 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
673 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
674 CopySrc = CopyDst = 0;
675
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000676 // Track registers used by instruction.
677 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000678 PhysECs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000679
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000680 // First scan.
681 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000682 // Find the end of the virtreg operands
683 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000684 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
685 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000686 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000687 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000688 if (!Reg) continue;
689 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
690 VirtOpEnd = i+1;
691 continue;
692 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000693 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000694 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000695 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000696 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000697 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000698 PhysECs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000699 }
700 }
701
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000702 // Second scan.
703 // Allocate virtreg uses and early clobbers.
704 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000705 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000706 MachineOperand &MO = MI->getOperand(i);
707 if (!MO.isReg()) continue;
708 unsigned Reg = MO.getReg();
709 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
710 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000711 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
712 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000713 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000714 if (setPhysReg(MO, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000715 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000716 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000717 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
718 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000719 setPhysReg(MO, PhysReg);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000720 PhysECs.push_back(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000721 }
722 }
723
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000724 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000725
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000726 // Track registers defined by instruction - early clobbers at this point.
727 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000728 for (unsigned i = 0, e = PhysECs.size(); i != e; ++i) {
729 unsigned PhysReg = PhysECs[i];
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000730 UsedInInstr.set(PhysReg);
731 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
732 unsigned Alias = *AS; ++AS)
733 UsedInInstr.set(Alias);
734 }
735
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000736 unsigned DefOpEnd = MI->getNumOperands();
737 if (TID.isCall()) {
738 // Spill all virtregs before a call. This serves two purposes: 1. If an
739 // exception is thrown, the landing pad is going to expect to find registers
740 // in their spill slots, and 2. we don't have to wade through all the
741 // <imp-def> operands on the call instruction.
742 DefOpEnd = VirtOpEnd;
743 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
744 spillAll(MI);
745 }
746
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000747 // Third scan.
748 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000749 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000750 MachineOperand &MO = MI->getOperand(i);
751 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
752 unsigned Reg = MO.getReg();
753
754 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000755 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000756 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
757 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000758 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000759 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000760 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
761 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000762 if (setPhysReg(MO, PhysReg)) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000763 killVirtReg(LRI);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000764 CopyDst = 0; // cancel coalescing;
765 } else
766 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000767 }
768
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000769 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000770
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000771 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
772 DEBUG(dbgs() << "-- coalescing: " << *MI);
773 Coalesced.push_back(MI);
774 } else {
775 DEBUG(dbgs() << "<< " << *MI);
776 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000777 }
778
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000779 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000780 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
781 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000782
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000783 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000784 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000785 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000786 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000787 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000788
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000789 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000790}
791
792/// runOnMachineFunction - Register allocate the whole function
793///
794bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000795 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
796 << "********** Function: "
797 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000798 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000799 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000800 TM = &Fn.getTarget();
801 TRI = TM->getRegisterInfo();
802 TII = TM->getInstrInfo();
803
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000804 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000805 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000806
807 // initialize the virtual->physical register map to have a 'null'
808 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000809 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000810 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000811
812 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000813 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
814 MBBi != MBBe; ++MBBi) {
815 MBB = &*MBBi;
816 AllocateBasicBlock();
817 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000818
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000819 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000820 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000821
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000822 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000823 return true;
824}
825
826FunctionPass *llvm::createFastRegisterAllocator() {
827 return new RAFast();
828}