blob: ea8d4229d97bccc409cd5c511c7e4b9b4dc57b8e [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 Olesen6de07172010-06-04 18:08:29 +0000113 // SkippedInstrs - Descriptors of instructions whose clobber list was ignored
114 // because all registers were spilled. It is still necessary to mark all the
115 // clobbered registers as used by the function.
116 SmallPtrSet<const TargetInstrDesc*, 4> SkippedInstrs;
117
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000118 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
119 // completely after spilling all live registers. LiveRegMap entries should
120 // not be erased.
121 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000122
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000123 enum {
124 spillClean = 1,
125 spillDirty = 100,
126 spillImpossible = ~0u
127 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000128 public:
129 virtual const char *getPassName() const {
130 return "Fast Register Allocator";
131 }
132
133 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
134 AU.setPreservesCFG();
135 AU.addRequiredID(PHIEliminationID);
136 AU.addRequiredID(TwoAddressInstructionPassID);
137 MachineFunctionPass::getAnalysisUsage(AU);
138 }
139
140 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000141 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000142 void AllocateBasicBlock();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000143 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000144 bool isLastUseOfLocalReg(MachineOperand&);
145
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000146 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000147 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000148 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000149 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000150 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000151
152 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000153 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000154 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000155 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
156 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000157 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
158 unsigned VirtReg, unsigned Hint);
159 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
160 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000161 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000162 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000163 };
164 char RAFast::ID = 0;
165}
166
167/// getStackSpaceFor - This allocates space for the specified virtual register
168/// to be held on the stack.
169int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
170 // Find the location Reg would belong...
171 int SS = StackSlotForVirtReg[VirtReg];
172 if (SS != -1)
173 return SS; // Already has space allocated?
174
175 // Allocate a new stack object for this spill location...
176 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
177 RC->getAlignment());
178
179 // Assign the slot.
180 StackSlotForVirtReg[VirtReg] = FrameIdx;
181 return FrameIdx;
182}
183
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000184/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
185/// its virtual register, and it is guaranteed to be a block-local register.
186///
187bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
188 // Check for non-debug uses or defs following MO.
189 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000190 MachineOperand *Next = &MO;
191 while ((Next = Next->getNextOperandForReg()))
192 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000193 return false;
194
195 // If the register has ever been spilled or reloaded, we conservatively assume
196 // it is a global register used in multiple blocks.
197 if (StackSlotForVirtReg[MO.getReg()] != -1)
198 return false;
199
200 // Check that the use/def chain has exactly one operand - MO.
201 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
202}
203
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000204/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000205void RAFast::addKillFlag(const LiveReg &LR) {
206 if (!LR.LastUse) return;
207 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000208 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
209 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000210 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000211 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 Olesend32e7352010-05-19 21:36:05 +0000516 bool PartialRedef = MI->getOperand(OpNum).getSubReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000517 if (New) {
518 // If there is no hint, peek at the only use of this register.
519 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
520 MRI->hasOneNonDBGUse(VirtReg)) {
521 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
522 // It's a copy, use the destination register as a hint.
523 if (TII->isMoveInstr(*MRI->use_nodbg_begin(VirtReg),
524 SrcReg, DstReg, SrcSubReg, DstSubReg))
525 Hint = DstReg;
526 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000527 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000528 // If this is only a partial redefinition, we must reload the other parts.
529 if (PartialRedef && MI->readsVirtualRegister(VirtReg)) {
530 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
531 int FI = getStackSpaceFor(VirtReg, RC);
532 DEBUG(dbgs() << "Reloading for partial redef: %reg" << VirtReg << "\n");
533 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FI, RC, TRI);
534 ++NumLoads;
535 }
536 } else if (LR.LastUse && !PartialRedef) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000537 // Redefining a live register - kill at the last use, unless it is this
538 // instruction defining VirtReg multiple times.
539 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
540 addKillFlag(LR);
541 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000542 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000543 LR.LastUse = MI;
544 LR.LastOpNum = OpNum;
545 LR.Dirty = true;
546 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000547 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000548}
549
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000550/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000551RAFast::LiveRegMap::iterator
552RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
553 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000554 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
555 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000556 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000557 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000558 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
559 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000560 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000561 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000562 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000563 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000564 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000565 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000566 << TRI->getName(LR.PhysReg) << "\n");
567 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000568 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000569 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000570 if (isLastUseOfLocalReg(MO)) {
571 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
572 MO.setIsKill();
573 } else if (MO.isKill()) {
574 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
575 MO.setIsKill(false);
576 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000577 } else if (MO.isKill()) {
578 // We must remove kill flags from uses of reloaded registers because the
579 // register would be killed immediately, and there might be a second use:
580 // %foo = OR %x<kill>, %x
581 // This would cause a second reload of %x into a different register.
582 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
583 MO.setIsKill(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000584 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000585 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000586 LR.LastUse = MI;
587 LR.LastOpNum = OpNum;
588 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000589 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000590}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000591
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000592// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
593// subregs. This may invalidate any operand pointers.
594// Return true if the operand kills its register.
595bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
596 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000597 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000598 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000599 return MO.isKill() || MO.isDead();
600 }
601
602 // Handle subregister index.
603 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
604 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000605
606 // A kill flag implies killing the full register. Add corresponding super
607 // register kill.
608 if (MO.isKill()) {
609 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000610 return true;
611 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000612 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000613}
614
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000615void RAFast::AllocateBasicBlock() {
616 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000617
618 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000619 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000621 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000622
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000623 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000624 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
625 E = MBB->livein_end(); I != E; ++I)
626 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000627
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000628 SmallVector<unsigned, 8> PhysECs, VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000629 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000630
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000631 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000632 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000633 MachineInstr *MI = MII++;
634 const TargetInstrDesc &TID = MI->getDesc();
635 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000636 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000637 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
638 if (PhysRegState[Reg] == regDisabled) continue;
639 dbgs() << " " << TRI->getName(Reg);
640 switch(PhysRegState[Reg]) {
641 case regFree:
642 break;
643 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000644 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000645 break;
646 default:
647 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000648 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000649 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000650 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000651 "Bad inverse map");
652 break;
653 }
654 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000655 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000656 // Check that LiveVirtRegs is the inverse.
657 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
658 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000659 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
660 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000661 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000662 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000663 assert(PhysRegState[i->second.PhysReg] == i->first &&
664 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000665 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000666 });
667
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000668 // Debug values are not allowed to change codegen in any way.
669 if (MI->isDebugValue()) {
670 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
671 MachineOperand &MO = MI->getOperand(i);
672 if (!MO.isReg()) continue;
673 unsigned Reg = MO.getReg();
674 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000675 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
676 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000677 setPhysReg(MI, i, LRI->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000678 else
679 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000680 }
681 // Next instruction.
682 continue;
683 }
684
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000685 // If this is a copy, we may be able to coalesce.
686 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
687 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
688 CopySrc = CopyDst = 0;
689
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000690 // Track registers used by instruction.
691 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000692 PhysECs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000693
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000694 // First scan.
695 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000696 // Find the end of the virtreg operands
697 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000698 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
699 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000700 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000701 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000702 if (!Reg) continue;
703 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
704 VirtOpEnd = i+1;
705 continue;
706 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000707 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000708 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000709 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000710 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000711 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
712 regFree : regReserved);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000713 PhysECs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000714 }
715 }
716
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000717 // Second scan.
718 // Allocate virtreg uses and early clobbers.
719 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000720 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000721 MachineOperand &MO = MI->getOperand(i);
722 if (!MO.isReg()) continue;
723 unsigned Reg = MO.getReg();
724 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
725 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000726 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
727 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000728 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000729 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000730 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000731 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000732 // Note: defineVirtReg may invalidate MO.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000733 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
734 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000735 if (setPhysReg(MI, i, PhysReg))
736 VirtDead.push_back(Reg);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000737 PhysECs.push_back(PhysReg);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000738 // Don't attempt coalescing when earlyclobbers are present.
739 CopyDst = 0;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000740 }
741 }
742
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000743 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000744
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000745 // Track registers defined by instruction - early clobbers at this point.
746 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000747 for (unsigned i = 0, e = PhysECs.size(); i != e; ++i) {
748 unsigned PhysReg = PhysECs[i];
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000749 UsedInInstr.set(PhysReg);
750 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
751 unsigned Alias = *AS; ++AS)
752 UsedInInstr.set(Alias);
753 }
754
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000755 unsigned DefOpEnd = MI->getNumOperands();
756 if (TID.isCall()) {
757 // Spill all virtregs before a call. This serves two purposes: 1. If an
758 // exception is thrown, the landing pad is going to expect to find registers
759 // in their spill slots, and 2. we don't have to wade through all the
760 // <imp-def> operands on the call instruction.
761 DefOpEnd = VirtOpEnd;
762 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
763 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000764
765 // The imp-defs are skipped below, but we still need to mark those
766 // registers as used by the function.
767 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000768 }
769
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000770 // Third scan.
771 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000772 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000773 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000774 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
775 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000776 unsigned Reg = MO.getReg();
777
778 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000779 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000780 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
781 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000782 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000783 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000784 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
785 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000786 if (setPhysReg(MI, i, PhysReg)) {
787 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000788 CopyDst = 0; // cancel coalescing;
789 } else
790 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000791 }
792
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000793 // Kill dead defs after the scan to ensure that multiple defs of the same
794 // register are allocated identically. We didn't need to do this for uses
795 // because we are crerating our own kill flags, and they are always at the
796 // last use.
797 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
798 killVirtReg(VirtDead[i]);
799 VirtDead.clear();
800
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000801 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000802
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000803 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
804 DEBUG(dbgs() << "-- coalescing: " << *MI);
805 Coalesced.push_back(MI);
806 } else {
807 DEBUG(dbgs() << "<< " << *MI);
808 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000809 }
810
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000811 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000812 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
813 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000814
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000815 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000816 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000817 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000818 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000819 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000820
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000821 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000822}
823
824/// runOnMachineFunction - Register allocate the whole function
825///
826bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000827 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
828 << "********** Function: "
829 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000830 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000831 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000832 TM = &Fn.getTarget();
833 TRI = TM->getRegisterInfo();
834 TII = TM->getInstrInfo();
835
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000836 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000837 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000838
839 // initialize the virtual->physical register map to have a 'null'
840 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000841 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000842 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000843
844 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000845 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
846 MBBi != MBBe; ++MBBi) {
847 MBB = &*MBBi;
848 AllocateBasicBlock();
849 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000850
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000851 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000852 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000853
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000854 // Add the clobber lists for all the instructions we skipped earlier.
855 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
856 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
857 if (const unsigned *Defs = (*I)->getImplicitDefs())
858 while (*Defs)
859 MRI->setPhysRegUsed(*Defs++);
860
861 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000862 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000863 return true;
864}
865
866FunctionPass *llvm::createFastRegisterAllocator() {
867 return new RAFast();
868}