blob: 803e248065f6409c6c39c3fe61e308cace069f27 [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 Olesen0eeb05c2010-05-18 21:10:50 +0000157 bool setPhysReg(MachineInstr *MI, unsigned OpNum, 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);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000203 if (MO.getReg() == LR.PhysReg) {
204 if (MO.isDef())
205 MO.setIsDead();
206 else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum))
207 MO.setIsKill();
208 } else {
209 if (MO.isDef())
210 LR.LastUse->addRegisterDead(LR.PhysReg, TRI, true);
211 else
212 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
213 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000214}
215
216/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000217void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
218 addKillFlag(LRI->second);
219 const LiveReg &LR = LRI->second;
220 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000221 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000222 // Erase from LiveVirtRegs unless we're spilling in bulk.
223 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000224 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000225}
226
227/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000228void RAFast::killVirtReg(unsigned VirtReg) {
229 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
230 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000231 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
232 if (LRI != LiveVirtRegs.end())
233 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000234}
235
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000236/// spillVirtReg - This method spills the value specified by VirtReg into the
237/// corresponding stack slot if needed. If isKill is set, the register is also
238/// killed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000239void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000240 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
241 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000242 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
243 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
244 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000245}
246
247/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000248void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000249 LiveRegMap::iterator LRI) {
250 LiveReg &LR = LRI->second;
251 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000252
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000253 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000254 // If this physreg is used by the instruction, we want to kill it on the
255 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000256 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000257 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000258 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000259 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000260 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
261 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000262 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000263 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000264 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000265
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000266 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000267 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000268 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000269 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000270}
271
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000272/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000273void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000274 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000275 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000276 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
277 // of spilling here is deterministic, if arbitrary.
278 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
279 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000280 spillVirtReg(MI, i);
281 LiveVirtRegs.clear();
282 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000283}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000284
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000285/// usePhysReg - Handle the direct use of a physical register.
286/// Check that the register is not used by a virtreg.
287/// Kill the physreg, marking it free.
288/// This may add implicit kills to MO->getParent() and invalidate MO.
289void RAFast::usePhysReg(MachineOperand &MO) {
290 unsigned PhysReg = MO.getReg();
291 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
292 "Bad usePhysReg operand");
293
294 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000295 case regDisabled:
296 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000297 case regReserved:
298 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000299 // Fall through
300 case regFree:
301 UsedInInstr.set(PhysReg);
302 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000303 return;
304 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000305 // The physreg was allocated to a virtual register. That means to value we
306 // wanted has been clobbered.
307 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000308 }
309
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000310 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000311 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
312 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000313 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000314 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000315 break;
316 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000317 assert(TRI->isSuperRegister(PhysReg, Alias) &&
318 "Instruction is not using a subregister of a reserved register");
319 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000320 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000321 UsedInInstr.set(Alias);
322 MO.getParent()->addRegisterKilled(Alias, TRI, true);
323 return;
324 case regFree:
325 if (TRI->isSuperRegister(PhysReg, Alias)) {
326 // Leave the superregister in the working set.
327 UsedInInstr.set(Alias);
328 MO.getParent()->addRegisterKilled(Alias, TRI, true);
329 return;
330 }
331 // Some other alias was in the working set - clear it.
332 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000333 break;
334 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000335 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000336 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000337 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000338
339 // All aliases are disabled, bring register into working set.
340 PhysRegState[PhysReg] = regFree;
341 UsedInInstr.set(PhysReg);
342 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000343}
344
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000345/// definePhysReg - Mark PhysReg as reserved or free after spilling any
346/// virtregs. This is very similar to defineVirtReg except the physreg is
347/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000348void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
349 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000350 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000351 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
352 case regDisabled:
353 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000354 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000355 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000356 // Fall through.
357 case regFree:
358 case regReserved:
359 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000360 return;
361 }
362
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000363 // This is a disabled register, disable all aliases.
364 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000365 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
366 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000367 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000368 switch (unsigned VirtReg = PhysRegState[Alias]) {
369 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000370 break;
371 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000372 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000373 // Fall through.
374 case regFree:
375 case regReserved:
376 PhysRegState[Alias] = regDisabled;
377 if (TRI->isSuperRegister(PhysReg, Alias))
378 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000379 break;
380 }
381 }
382}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000383
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000384
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000385// calcSpillCost - Return the cost of spilling clearing out PhysReg and
386// aliases so it is free for allocation.
387// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
388// can be allocated directly.
389// Returns spillImpossible when PhysReg or an alias can't be spilled.
390unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000391 if (UsedInInstr.test(PhysReg))
392 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000393 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
394 case regDisabled:
395 break;
396 case regFree:
397 return 0;
398 case regReserved:
399 return spillImpossible;
400 default:
401 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
402 }
403
404 // This is a disabled register, add up const of aliases.
405 unsigned Cost = 0;
406 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
407 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000408 if (UsedInInstr.test(Alias))
409 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000410 switch (unsigned VirtReg = PhysRegState[Alias]) {
411 case regDisabled:
412 break;
413 case regFree:
414 ++Cost;
415 break;
416 case regReserved:
417 return spillImpossible;
418 default:
419 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
420 break;
421 }
422 }
423 return Cost;
424}
425
426
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000427/// assignVirtToPhysReg - This method updates local state so that we know
428/// that PhysReg is the proper container for VirtReg now. The physical
429/// register must not be used for anything else when this is called.
430///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000431void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
432 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000433 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000434 PhysRegState[PhysReg] = LRE.first;
435 assert(!LRE.second.PhysReg && "Already assigned a physreg");
436 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000437}
438
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000439/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000440void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000441 const unsigned VirtReg = LRE.first;
442
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000443 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
444 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000445
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000446 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000447
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000448 // Ignore invalid hints.
449 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000450 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000451 Hint = 0;
452
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000453 // Take hint when possible.
454 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000455 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000456 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000457 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000458 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000459 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000460 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000461 case spillImpossible:
462 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000463 }
464 }
465
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000466 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
467 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
468
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000469 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000470 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
471 unsigned PhysReg = *I;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000472 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
473 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000474 }
475
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000476 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000477 << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000478
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000479 unsigned BestReg = 0, BestCost = spillImpossible;
480 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
481 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000482 // Cost is 0 when all aliases are already disabled.
483 if (Cost == 0)
484 return assignVirtToPhysReg(LRE, *I);
485 if (Cost < BestCost)
486 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000487 }
488
489 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000490 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000491 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000492 }
493
494 // Nothing we can do.
495 std::string msg;
496 raw_string_ostream Msg(msg);
497 Msg << "Ran out of registers during register allocation!";
498 if (MI->isInlineAsm()) {
499 Msg << "\nPlease check your inline asm statement for "
500 << "invalid constraints:\n";
501 MI->print(Msg, TM);
502 }
503 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000504}
505
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000506/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000507RAFast::LiveRegMap::iterator
508RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
509 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000510 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
511 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000512 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000513 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000514 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
515 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000516 if (New) {
517 // If there is no hint, peek at the only use of this register.
518 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
519 MRI->hasOneNonDBGUse(VirtReg)) {
520 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
521 // It's a copy, use the destination register as a hint.
522 if (TII->isMoveInstr(*MRI->use_nodbg_begin(VirtReg),
523 SrcReg, DstReg, SrcSubReg, DstSubReg))
524 Hint = DstReg;
525 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000526 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000527 } else if (LR.LastUse) {
528 // Redefining a live register - kill at the last use, unless it is this
529 // instruction defining VirtReg multiple times.
530 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
531 addKillFlag(LR);
532 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000533 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000534 LR.LastUse = MI;
535 LR.LastOpNum = OpNum;
536 LR.Dirty = true;
537 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000538 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000539}
540
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000541/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000542RAFast::LiveRegMap::iterator
543RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
544 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000545 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
546 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000547 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000548 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000549 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
550 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000551 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000552 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000553 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000554 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000555 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000556 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000557 << TRI->getName(LR.PhysReg) << "\n");
558 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000559 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000560 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000561 if (isLastUseOfLocalReg(MO)) {
562 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
563 MO.setIsKill();
564 } else if (MO.isKill()) {
565 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
566 MO.setIsKill(false);
567 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000568 } else if (MO.isKill()) {
569 // We must remove kill flags from uses of reloaded registers because the
570 // register would be killed immediately, and there might be a second use:
571 // %foo = OR %x<kill>, %x
572 // This would cause a second reload of %x into a different register.
573 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
574 MO.setIsKill(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000575 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000576 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000577 LR.LastUse = MI;
578 LR.LastOpNum = OpNum;
579 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000580 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000581}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000582
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000583// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
584// subregs. This may invalidate any operand pointers.
585// Return true if the operand kills its register.
586bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
587 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000588 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000589 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000590 return MO.isKill() || MO.isDead();
591 }
592
593 // Handle subregister index.
594 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
595 MO.setSubReg(0);
596 if (MO.isUse()) {
597 if (MO.isKill()) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000598 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000599 return true;
600 }
601 return false;
602 }
603 // A subregister def implicitly defines the whole physreg.
604 if (MO.isDead()) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000605 MI->addRegisterDead(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000606 return true;
607 }
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000608 MI->addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000609 return false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000610}
611
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000612void RAFast::AllocateBasicBlock() {
613 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000614
615 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000616 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000617
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000618 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000619
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000621 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
622 E = MBB->livein_end(); I != E; ++I)
623 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000624
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000625 SmallVector<unsigned, 8> PhysECs, VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000626 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000627
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000628 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000629 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000630 MachineInstr *MI = MII++;
631 const TargetInstrDesc &TID = MI->getDesc();
632 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000633 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000634 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
635 if (PhysRegState[Reg] == regDisabled) continue;
636 dbgs() << " " << TRI->getName(Reg);
637 switch(PhysRegState[Reg]) {
638 case regFree:
639 break;
640 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000641 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000642 break;
643 default:
644 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000645 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000646 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000647 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000648 "Bad inverse map");
649 break;
650 }
651 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000652 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000653 // Check that LiveVirtRegs is the inverse.
654 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
655 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000656 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
657 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000658 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000659 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000660 assert(PhysRegState[i->second.PhysReg] == i->first &&
661 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000662 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000663 });
664
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000665 // Debug values are not allowed to change codegen in any way.
666 if (MI->isDebugValue()) {
667 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
668 MachineOperand &MO = MI->getOperand(i);
669 if (!MO.isReg()) continue;
670 unsigned Reg = MO.getReg();
671 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000672 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
673 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000674 setPhysReg(MI, i, LRI->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000675 else
676 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000677 }
678 // Next instruction.
679 continue;
680 }
681
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000682 // If this is a copy, we may be able to coalesce.
683 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
684 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
685 CopySrc = CopyDst = 0;
686
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000687 // Track registers used by instruction.
688 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000689 PhysECs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000690
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000691 // First scan.
692 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000693 // Find the end of the virtreg operands
694 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000695 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
696 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000697 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000698 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000699 if (!Reg) continue;
700 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
701 VirtOpEnd = i+1;
702 continue;
703 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000704 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000705 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000706 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000707 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000708 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000709 PhysECs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000710 }
711 }
712
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000713 // Second scan.
714 // Allocate virtreg uses and early clobbers.
715 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000716 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000717 MachineOperand &MO = MI->getOperand(i);
718 if (!MO.isReg()) continue;
719 unsigned Reg = MO.getReg();
720 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
721 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000722 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
723 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000724 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000725 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000726 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000727 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000728 // Note: defineVirtReg may invalidate MO.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000729 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
730 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000731 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000732 PhysECs.push_back(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000733 }
734 }
735
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000736 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000737
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000738 // Track registers defined by instruction - early clobbers at this point.
739 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000740 for (unsigned i = 0, e = PhysECs.size(); i != e; ++i) {
741 unsigned PhysReg = PhysECs[i];
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000742 UsedInInstr.set(PhysReg);
743 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
744 unsigned Alias = *AS; ++AS)
745 UsedInInstr.set(Alias);
746 }
747
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000748 unsigned DefOpEnd = MI->getNumOperands();
749 if (TID.isCall()) {
750 // Spill all virtregs before a call. This serves two purposes: 1. If an
751 // exception is thrown, the landing pad is going to expect to find registers
752 // in their spill slots, and 2. we don't have to wade through all the
753 // <imp-def> operands on the call instruction.
754 DefOpEnd = VirtOpEnd;
755 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
756 spillAll(MI);
757 }
758
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000759 // Third scan.
760 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000761 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000762 MachineOperand &MO = MI->getOperand(i);
763 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
764 unsigned Reg = MO.getReg();
765
766 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000767 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000768 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
769 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000770 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000771 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000772 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
773 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000774 if (setPhysReg(MI, i, PhysReg)) {
775 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000776 CopyDst = 0; // cancel coalescing;
777 } else
778 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000779 }
780
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000781 // Kill dead defs after the scan to ensure that multiple defs of the same
782 // register are allocated identically. We didn't need to do this for uses
783 // because we are crerating our own kill flags, and they are always at the
784 // last use.
785 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
786 killVirtReg(VirtDead[i]);
787 VirtDead.clear();
788
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000789 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000790
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000791 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
792 DEBUG(dbgs() << "-- coalescing: " << *MI);
793 Coalesced.push_back(MI);
794 } else {
795 DEBUG(dbgs() << "<< " << *MI);
796 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000797 }
798
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000799 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000800 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
801 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000802
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000803 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000804 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000805 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000806 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000807 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000808
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000809 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000810}
811
812/// runOnMachineFunction - Register allocate the whole function
813///
814bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000815 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
816 << "********** Function: "
817 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000818 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000819 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000820 TM = &Fn.getTarget();
821 TRI = TM->getRegisterInfo();
822 TII = TM->getInstrInfo();
823
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000824 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000825 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000826
827 // initialize the virtual->physical register map to have a 'null'
828 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000829 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000830 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000831
832 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000833 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
834 MBBi != MBBe; ++MBBi) {
835 MBB = &*MBBi;
836 AllocateBasicBlock();
837 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000838
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000839 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000840 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000841
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000842 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000843 return true;
844}
845
846FunctionPass *llvm::createFastRegisterAllocator() {
847 return new RAFast();
848}