blob: 8f6496dcac794eae079cefebd4698d182c1930db [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"
Devang Patel459a36b2010-08-04 18:42:02 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/CodeGen/RegAllocRegistry.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/IndexedMap.h"
32#include "llvm/ADT/SmallSet.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/STLExtras.h"
36#include <algorithm>
37using namespace llvm;
38
39STATISTIC(NumStores, "Number of stores added");
40STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000041STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000042
43static RegisterRegAlloc
44 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
45
46namespace {
47 class RAFast : public MachineFunctionPass {
48 public:
49 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000050 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +000051 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000052 private:
53 const TargetMachine *TM;
54 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000055 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000056 const TargetRegisterInfo *TRI;
57 const TargetInstrInfo *TII;
58
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000059 // Basic block currently being allocated.
60 MachineBasicBlock *MBB;
61
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000062 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
63 // values are spilled.
64 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
65
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000066 // Everything we know about a live virtual register.
67 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000068 MachineInstr *LastUse; // Last instr to use reg.
69 unsigned PhysReg; // Currently held here.
70 unsigned short LastOpNum; // OpNum on LastUse.
71 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000072
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000073 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000074 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000075 };
76
77 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000078 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000079
80 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000081 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000082 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000083
Devang Patel459a36b2010-08-04 18:42:02 +000084 DenseMap<unsigned, MachineInstr *> LiveDbgValueMap;
85
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000086 // RegState - Track the state of a physical register.
87 enum RegState {
88 // A disabled register is not available for allocation, but an alias may
89 // be in use. A register can only be moved out of the disabled state if
90 // all aliases are disabled.
91 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000092
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000093 // A free register is not currently in use and can be allocated
94 // immediately without checking aliases.
95 regFree,
96
97 // A reserved register has been assigned expolicitly (e.g., setting up a
98 // call parameter), and it remains reserved until it is used.
99 regReserved
100
101 // A register state may also be a virtual register number, indication that
102 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000103 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000104 };
105
106 // PhysRegState - One of the RegState enums, or a virtreg.
107 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000108
109 // UsedInInstr - BitVector of physregs that are used in the current
110 // instruction, and so cannot be allocated.
111 BitVector UsedInInstr;
112
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000113 // Allocatable - vector of allocatable physical registers.
114 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000115
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000116 // SkippedInstrs - Descriptors of instructions whose clobber list was ignored
117 // because all registers were spilled. It is still necessary to mark all the
118 // clobbered registers as used by the function.
119 SmallPtrSet<const TargetInstrDesc*, 4> SkippedInstrs;
120
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000121 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
122 // completely after spilling all live registers. LiveRegMap entries should
123 // not be erased.
124 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000125
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000126 enum {
127 spillClean = 1,
128 spillDirty = 100,
129 spillImpossible = ~0u
130 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000131 public:
132 virtual const char *getPassName() const {
133 return "Fast Register Allocator";
134 }
135
136 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
137 AU.setPreservesCFG();
138 AU.addRequiredID(PHIEliminationID);
139 AU.addRequiredID(TwoAddressInstructionPassID);
140 MachineFunctionPass::getAnalysisUsage(AU);
141 }
142
143 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000144 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000145 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000146 void handleThroughOperands(MachineInstr *MI,
147 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000148 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000149 bool isLastUseOfLocalReg(MachineOperand&);
150
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000151 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000152 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000153 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000154 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000155 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000156
157 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000158 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000159 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000160 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
161 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000162 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
163 unsigned VirtReg, unsigned Hint);
164 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
165 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000166 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000167 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000168 };
169 char RAFast::ID = 0;
170}
171
172/// getStackSpaceFor - This allocates space for the specified virtual register
173/// to be held on the stack.
174int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
175 // Find the location Reg would belong...
176 int SS = StackSlotForVirtReg[VirtReg];
177 if (SS != -1)
178 return SS; // Already has space allocated?
179
180 // Allocate a new stack object for this spill location...
181 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
182 RC->getAlignment());
183
184 // Assign the slot.
185 StackSlotForVirtReg[VirtReg] = FrameIdx;
186 return FrameIdx;
187}
188
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000189/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
190/// its virtual register, and it is guaranteed to be a block-local register.
191///
192bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
193 // Check for non-debug uses or defs following MO.
194 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000195 MachineOperand *Next = &MO;
196 while ((Next = Next->getNextOperandForReg()))
197 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000198 return false;
199
200 // If the register has ever been spilled or reloaded, we conservatively assume
201 // it is a global register used in multiple blocks.
202 if (StackSlotForVirtReg[MO.getReg()] != -1)
203 return false;
204
205 // Check that the use/def chain has exactly one operand - MO.
206 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
207}
208
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000209/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000210void RAFast::addKillFlag(const LiveReg &LR) {
211 if (!LR.LastUse) return;
212 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000213 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
214 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000215 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000216 else
217 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
218 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000219}
220
221/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000222void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
223 addKillFlag(LRI->second);
224 const LiveReg &LR = LRI->second;
225 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000226 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000227 // Erase from LiveVirtRegs unless we're spilling in bulk.
228 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000229 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000230}
231
232/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000233void RAFast::killVirtReg(unsigned VirtReg) {
234 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
235 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000236 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
237 if (LRI != LiveVirtRegs.end())
238 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000239}
240
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000241/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000242/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000243void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000244 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
245 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000246 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
247 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
248 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000249}
250
251/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000252void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000253 LiveRegMap::iterator LRI) {
254 LiveReg &LR = LRI->second;
255 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000256
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000257 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000258 // If this physreg is used by the instruction, we want to kill it on the
259 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000260 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000261 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000262 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000263 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000264 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
265 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000266 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000267 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000268 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000269
Devang Patel459a36b2010-08-04 18:42:02 +0000270 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
271 // identify spilled location as the place to find corresponding variable's
272 // value.
273 if (MachineInstr *DBG = LiveDbgValueMap.lookup(LRI->first)) {
274 const MDNode *MDPtr =
275 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
276 int64_t Offset = 0;
277 if (DBG->getOperand(1).isImm())
278 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000279 DebugLoc DL;
280 if (MI == MBB->end()) {
281 // If MI is at basic block end then use last instruction's location.
282 MachineBasicBlock::iterator EI = MI;
283 DL = (--EI)->getDebugLoc();
284 }
285 else
286 DL = MI->getDebugLoc();
Devang Patel459a36b2010-08-04 18:42:02 +0000287 if (MachineInstr *NewDV =
288 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
289 MachineBasicBlock *MBB = DBG->getParent();
290 MBB->insert(MI, NewDV);
291 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
292 LiveDbgValueMap[LRI->first] = NewDV;
293 }
294 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000295 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000296 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000297 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000298 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000299}
300
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000301/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000302void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000303 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000304 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000305 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
306 // of spilling here is deterministic, if arbitrary.
307 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
308 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000309 spillVirtReg(MI, i);
310 LiveVirtRegs.clear();
311 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000312}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000313
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000314/// usePhysReg - Handle the direct use of a physical register.
315/// Check that the register is not used by a virtreg.
316/// Kill the physreg, marking it free.
317/// This may add implicit kills to MO->getParent() and invalidate MO.
318void RAFast::usePhysReg(MachineOperand &MO) {
319 unsigned PhysReg = MO.getReg();
320 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
321 "Bad usePhysReg operand");
322
323 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000324 case regDisabled:
325 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000326 case regReserved:
327 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000328 // Fall through
329 case regFree:
330 UsedInInstr.set(PhysReg);
331 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000332 return;
333 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000334 // The physreg was allocated to a virtual register. That means to value we
335 // wanted has been clobbered.
336 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000337 }
338
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000339 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000340 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
341 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000342 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000343 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000344 break;
345 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000346 assert(TRI->isSuperRegister(PhysReg, Alias) &&
347 "Instruction is not using a subregister of a reserved register");
348 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000349 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000350 UsedInInstr.set(Alias);
351 MO.getParent()->addRegisterKilled(Alias, TRI, true);
352 return;
353 case regFree:
354 if (TRI->isSuperRegister(PhysReg, Alias)) {
355 // Leave the superregister in the working set.
356 UsedInInstr.set(Alias);
357 MO.getParent()->addRegisterKilled(Alias, TRI, true);
358 return;
359 }
360 // Some other alias was in the working set - clear it.
361 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000362 break;
363 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000364 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000365 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000366 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000367
368 // All aliases are disabled, bring register into working set.
369 PhysRegState[PhysReg] = regFree;
370 UsedInInstr.set(PhysReg);
371 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000372}
373
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000374/// definePhysReg - Mark PhysReg as reserved or free after spilling any
375/// virtregs. This is very similar to defineVirtReg except the physreg is
376/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000377void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
378 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000380 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
381 case regDisabled:
382 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000383 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000384 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000385 // Fall through.
386 case regFree:
387 case regReserved:
388 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000389 return;
390 }
391
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000392 // This is a disabled register, disable all aliases.
393 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000394 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
395 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000396 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000397 switch (unsigned VirtReg = PhysRegState[Alias]) {
398 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000399 break;
400 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000401 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000402 // Fall through.
403 case regFree:
404 case regReserved:
405 PhysRegState[Alias] = regDisabled;
406 if (TRI->isSuperRegister(PhysReg, Alias))
407 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000408 break;
409 }
410 }
411}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000412
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000413
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000414// calcSpillCost - Return the cost of spilling clearing out PhysReg and
415// aliases so it is free for allocation.
416// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
417// can be allocated directly.
418// Returns spillImpossible when PhysReg or an alias can't be spilled.
419unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000420 if (UsedInInstr.test(PhysReg))
421 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000422 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
423 case regDisabled:
424 break;
425 case regFree:
426 return 0;
427 case regReserved:
428 return spillImpossible;
429 default:
430 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
431 }
432
433 // This is a disabled register, add up const of aliases.
434 unsigned Cost = 0;
435 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
436 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000437 if (UsedInInstr.test(Alias))
438 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000439 switch (unsigned VirtReg = PhysRegState[Alias]) {
440 case regDisabled:
441 break;
442 case regFree:
443 ++Cost;
444 break;
445 case regReserved:
446 return spillImpossible;
447 default:
448 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
449 break;
450 }
451 }
452 return Cost;
453}
454
455
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000456/// assignVirtToPhysReg - This method updates local state so that we know
457/// that PhysReg is the proper container for VirtReg now. The physical
458/// register must not be used for anything else when this is called.
459///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000460void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
461 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000462 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000463 PhysRegState[PhysReg] = LRE.first;
464 assert(!LRE.second.PhysReg && "Already assigned a physreg");
465 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000466}
467
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000468/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000469void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000470 const unsigned VirtReg = LRE.first;
471
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000472 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
473 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000474
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000475 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000476
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000477 // Ignore invalid hints.
478 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000479 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000480 Hint = 0;
481
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000482 // Take hint when possible.
483 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000484 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000485 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000486 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000487 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000488 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000489 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000490 case spillImpossible:
491 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000492 }
493 }
494
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000495 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
496 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
497
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000498 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000499 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
500 unsigned PhysReg = *I;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000501 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
502 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 }
504
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000505 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000506 << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000507
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000508 unsigned BestReg = 0, BestCost = spillImpossible;
509 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
510 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000511 // Cost is 0 when all aliases are already disabled.
512 if (Cost == 0)
513 return assignVirtToPhysReg(LRE, *I);
514 if (Cost < BestCost)
515 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000516 }
517
518 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000519 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000520 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000521 }
522
523 // Nothing we can do.
524 std::string msg;
525 raw_string_ostream Msg(msg);
526 Msg << "Ran out of registers during register allocation!";
527 if (MI->isInlineAsm()) {
528 Msg << "\nPlease check your inline asm statement for "
529 << "invalid constraints:\n";
530 MI->print(Msg, TM);
531 }
532 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000533}
534
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000535/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000536RAFast::LiveRegMap::iterator
537RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
538 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000539 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
540 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000541 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000542 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000543 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
544 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000545 if (New) {
546 // If there is no hint, peek at the only use of this register.
547 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
548 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000549 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000550 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000551 if (UseMI.isCopyLike())
552 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000553 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000554 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000555 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000556 // Redefining a live register - kill at the last use, unless it is this
557 // instruction defining VirtReg multiple times.
558 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
559 addKillFlag(LR);
560 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000561 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000562 LR.LastUse = MI;
563 LR.LastOpNum = OpNum;
564 LR.Dirty = true;
565 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000566 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000567}
568
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000569/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000570RAFast::LiveRegMap::iterator
571RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
572 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000573 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
574 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000575 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000576 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000577 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
578 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000579 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000580 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000581 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000582 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000583 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000584 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000585 << TRI->getName(LR.PhysReg) << "\n");
586 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000587 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000588 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000589 if (isLastUseOfLocalReg(MO)) {
590 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000591 if (MO.isUse())
592 MO.setIsKill();
593 else
594 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000595 } else if (MO.isKill()) {
596 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
597 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000598 } else if (MO.isDead()) {
599 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
600 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000601 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000602 } else if (MO.isKill()) {
603 // We must remove kill flags from uses of reloaded registers because the
604 // register would be killed immediately, and there might be a second use:
605 // %foo = OR %x<kill>, %x
606 // This would cause a second reload of %x into a different register.
607 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
608 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000609 } else if (MO.isDead()) {
610 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
611 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000612 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000613 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000614 LR.LastUse = MI;
615 LR.LastOpNum = OpNum;
616 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000617 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000618}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000619
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000620// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
621// subregs. This may invalidate any operand pointers.
622// Return true if the operand kills its register.
623bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
624 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000625 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000626 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000627 return MO.isKill() || MO.isDead();
628 }
629
630 // Handle subregister index.
631 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
632 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000633
634 // A kill flag implies killing the full register. Add corresponding super
635 // register kill.
636 if (MO.isKill()) {
637 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000638 return true;
639 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000640 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000641}
642
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000643// Handle special instruction operand like early clobbers and tied ops when
644// there are additional physreg defines.
645void RAFast::handleThroughOperands(MachineInstr *MI,
646 SmallVectorImpl<unsigned> &VirtDead) {
647 DEBUG(dbgs() << "Scanning for through registers:");
648 SmallSet<unsigned, 8> ThroughRegs;
649 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
650 MachineOperand &MO = MI->getOperand(i);
651 if (!MO.isReg()) continue;
652 unsigned Reg = MO.getReg();
653 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000654 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
655 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000656 if (ThroughRegs.insert(Reg))
657 DEBUG(dbgs() << " %reg" << Reg);
658 }
659 }
660
661 // If any physreg defines collide with preallocated through registers,
662 // we must spill and reallocate.
663 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
664 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
665 MachineOperand &MO = MI->getOperand(i);
666 if (!MO.isReg() || !MO.isDef()) continue;
667 unsigned Reg = MO.getReg();
668 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
669 UsedInInstr.set(Reg);
670 if (ThroughRegs.count(PhysRegState[Reg]))
671 definePhysReg(MI, Reg, regFree);
672 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
673 UsedInInstr.set(*AS);
674 if (ThroughRegs.count(PhysRegState[*AS]))
675 definePhysReg(MI, *AS, regFree);
676 }
677 }
678
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000679 SmallVector<unsigned, 8> PartialDefs;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000680 DEBUG(dbgs() << "Allocating tied uses and early clobbers.\n");
681 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
682 MachineOperand &MO = MI->getOperand(i);
683 if (!MO.isReg()) continue;
684 unsigned Reg = MO.getReg();
685 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
686 if (MO.isUse()) {
687 unsigned DefIdx = 0;
688 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
689 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
690 << DefIdx << ".\n");
691 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
692 unsigned PhysReg = LRI->second.PhysReg;
693 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000694 // Note: we don't update the def operand yet. That would cause the normal
695 // def-scan to attempt spilling.
696 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
697 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
698 // Reload the register, but don't assign to the operand just yet.
699 // That would confuse the later phys-def processing pass.
700 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
701 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000702 } else if (MO.isEarlyClobber()) {
703 // Note: defineVirtReg may invalidate MO.
704 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
705 unsigned PhysReg = LRI->second.PhysReg;
706 if (setPhysReg(MI, i, PhysReg))
707 VirtDead.push_back(Reg);
708 }
709 }
710
711 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
712 UsedInInstr.reset();
713 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
714 MachineOperand &MO = MI->getOperand(i);
715 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
716 unsigned Reg = MO.getReg();
717 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
718 UsedInInstr.set(Reg);
719 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
720 UsedInInstr.set(*AS);
721 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000722
723 // Also mark PartialDefs as used to avoid reallocation.
724 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
725 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000726}
727
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000728void RAFast::AllocateBasicBlock() {
729 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000730
731 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000732 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000733
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000734 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000735
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000736 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000737 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
738 E = MBB->livein_end(); I != E; ++I)
739 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000740
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000741 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000742 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000743
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000744 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000745 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000746 MachineInstr *MI = MII++;
747 const TargetInstrDesc &TID = MI->getDesc();
748 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000749 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000750 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
751 if (PhysRegState[Reg] == regDisabled) continue;
752 dbgs() << " " << TRI->getName(Reg);
753 switch(PhysRegState[Reg]) {
754 case regFree:
755 break;
756 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000757 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000758 break;
759 default:
760 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000761 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000762 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000763 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000764 "Bad inverse map");
765 break;
766 }
767 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000768 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000769 // Check that LiveVirtRegs is the inverse.
770 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
771 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000772 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
773 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000774 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000775 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000776 assert(PhysRegState[i->second.PhysReg] == i->first &&
777 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000778 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000779 });
780
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000781 // Debug values are not allowed to change codegen in any way.
782 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000783 bool ScanDbgValue = true;
784 while (ScanDbgValue) {
785 ScanDbgValue = false;
786 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
787 MachineOperand &MO = MI->getOperand(i);
788 if (!MO.isReg()) continue;
789 unsigned Reg = MO.getReg();
790 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Devang Patel459a36b2010-08-04 18:42:02 +0000791 LiveDbgValueMap[Reg] = MI;
Devang Patel58b81762010-07-19 23:25:39 +0000792 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
793 if (LRI != LiveVirtRegs.end())
794 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000795 else {
Devang Patel58b81762010-07-19 23:25:39 +0000796 int SS = StackSlotForVirtReg[Reg];
797 if (SS == -1)
Devang Patel7a029b62010-07-09 21:48:31 +0000798 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel58b81762010-07-19 23:25:39 +0000799 else {
800 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000801 int64_t Offset = MI->getOperand(1).getImm();
Devang Patel58b81762010-07-19 23:25:39 +0000802 const MDNode *MDPtr =
803 MI->getOperand(MI->getNumOperands()-1).getMetadata();
804 DebugLoc DL = MI->getDebugLoc();
805 if (MachineInstr *NewDV =
806 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
807 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
808 MachineBasicBlock *MBB = MI->getParent();
809 MBB->insert(MBB->erase(MI), NewDV);
810 // Scan NewDV operands from the beginning.
811 MI = NewDV;
812 ScanDbgValue = true;
813 break;
814 } else
815 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
816 }
Devang Patel7a029b62010-07-09 21:48:31 +0000817 }
818 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000819 }
820 // Next instruction.
821 continue;
822 }
823
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000824 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000825 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000826 if (MI->isCopy()) {
827 CopyDst = MI->getOperand(0).getReg();
828 CopySrc = MI->getOperand(1).getReg();
829 CopyDstSub = MI->getOperand(0).getSubReg();
830 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000831 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000832
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000833 // Track registers used by instruction.
834 UsedInInstr.reset();
835
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000836 // First scan.
837 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000838 // Find the end of the virtreg operands
839 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000840 bool hasTiedOps = false;
841 bool hasEarlyClobbers = false;
842 bool hasPartialRedefs = false;
843 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000844 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
845 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000846 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000847 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000848 if (!Reg) continue;
849 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
850 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000851 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000852 hasTiedOps = hasTiedOps ||
853 TID.getOperandConstraint(i, TOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000854 } else {
855 if (MO.isEarlyClobber())
856 hasEarlyClobbers = true;
857 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
858 hasPartialRedefs = true;
859 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000860 continue;
861 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000862 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000863 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000864 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000865 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000866 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
867 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000868 hasEarlyClobbers = true;
869 } else
870 hasPhysDefs = true;
871 }
872
873 // The instruction may have virtual register operands that must be allocated
874 // the same register at use-time and def-time: early clobbers and tied
875 // operands. If there are also physical defs, these registers must avoid
876 // both physical defs and uses, making them more constrained than normal
877 // operands.
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000878 // Similarly, if there are multiple defs and tied operands, we must make sure
879 // the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000880 // We didn't detect inline asm tied operands above, so just make this extra
881 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000882 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000883 (hasTiedOps && (hasPhysDefs || TID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000884 handleThroughOperands(MI, VirtDead);
885 // Don't attempt coalescing when we have funny stuff going on.
886 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000887 // Pretend we have early clobbers so the use operands get marked below.
888 // This is not necessary for the common case of a single tied use.
889 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000890 }
891
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000892 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000893 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000894 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000895 MachineOperand &MO = MI->getOperand(i);
896 if (!MO.isReg()) continue;
897 unsigned Reg = MO.getReg();
898 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
899 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000900 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
901 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000902 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000903 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000904 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000905 }
906 }
907
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000908 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000909
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000910 // Track registers defined by instruction - early clobbers and tied uses at
911 // this point.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000912 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000913 if (hasEarlyClobbers) {
914 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
915 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000916 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000917 unsigned Reg = MO.getReg();
918 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000919 // Look for physreg defs and tied uses.
920 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000921 UsedInInstr.set(Reg);
922 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
923 UsedInInstr.set(*AS);
924 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000925 }
926
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000927 unsigned DefOpEnd = MI->getNumOperands();
928 if (TID.isCall()) {
929 // Spill all virtregs before a call. This serves two purposes: 1. If an
930 // exception is thrown, the landing pad is going to expect to find registers
931 // in their spill slots, and 2. we don't have to wade through all the
932 // <imp-def> operands on the call instruction.
933 DefOpEnd = VirtOpEnd;
934 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
935 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000936
937 // The imp-defs are skipped below, but we still need to mark those
938 // registers as used by the function.
939 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000940 }
941
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000942 // Third scan.
943 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000944 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000945 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000946 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
947 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000948 unsigned Reg = MO.getReg();
949
950 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000951 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000952 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
953 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000954 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000955 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000956 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
957 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000958 if (setPhysReg(MI, i, PhysReg)) {
959 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000960 CopyDst = 0; // cancel coalescing;
961 } else
962 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000963 }
964
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000965 // Kill dead defs after the scan to ensure that multiple defs of the same
966 // register are allocated identically. We didn't need to do this for uses
967 // because we are crerating our own kill flags, and they are always at the
968 // last use.
969 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
970 killVirtReg(VirtDead[i]);
971 VirtDead.clear();
972
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000973 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000974
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000975 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
976 DEBUG(dbgs() << "-- coalescing: " << *MI);
977 Coalesced.push_back(MI);
978 } else {
979 DEBUG(dbgs() << "<< " << *MI);
980 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000981 }
982
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000983 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000984 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
985 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000986
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000987 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000988 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000989 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000990 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000991 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000992
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000993 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000994}
995
996/// runOnMachineFunction - Register allocate the whole function
997///
998bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000999 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1000 << "********** Function: "
1001 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001002 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001003 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001004 TM = &Fn.getTarget();
1005 TRI = TM->getRegisterInfo();
1006 TII = TM->getInstrInfo();
1007
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001008 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +00001009 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001010
1011 // initialize the virtual->physical register map to have a 'null'
1012 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001013 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001014 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001015
1016 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001017 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1018 MBBi != MBBe; ++MBBi) {
1019 MBB = &*MBBi;
1020 AllocateBasicBlock();
1021 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001022
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001023 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001024 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001025
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001026 // Add the clobber lists for all the instructions we skipped earlier.
1027 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
1028 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1029 if (const unsigned *Defs = (*I)->getImplicitDefs())
1030 while (*Defs)
1031 MRI->setPhysRegUsed(*Defs++);
1032
1033 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001034 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001035 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001036 return true;
1037}
1038
1039FunctionPass *llvm::createFastRegisterAllocator() {
1040 return new RAFast();
1041}