blob: f0ccd351d2ac816cc4650d01064eb1153f30e5dd [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 Anderson9ccaf532010-08-05 23:42:04 +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
242/// corresponding stack slot if needed. If isKill is set, the register is also
243/// killed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000244void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000245 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
246 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000247 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
248 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
249 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000250}
251
252/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000253void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000254 LiveRegMap::iterator LRI) {
255 LiveReg &LR = LRI->second;
256 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000257
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000258 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000259 // If this physreg is used by the instruction, we want to kill it on the
260 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000261 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000262 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000263 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000264 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000265 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
266 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000267 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000268 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000269 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000270
Devang Patel459a36b2010-08-04 18:42:02 +0000271 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
272 // identify spilled location as the place to find corresponding variable's
273 // value.
274 if (MachineInstr *DBG = LiveDbgValueMap.lookup(LRI->first)) {
275 const MDNode *MDPtr =
276 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
277 int64_t Offset = 0;
278 if (DBG->getOperand(1).isImm())
279 Offset = DBG->getOperand(1).getImm();
280 DebugLoc DL = MI->getDebugLoc();
281 if (MachineInstr *NewDV =
282 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
283 MachineBasicBlock *MBB = DBG->getParent();
284 MBB->insert(MI, NewDV);
285 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
286 LiveDbgValueMap[LRI->first] = NewDV;
287 }
288 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000289 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000290 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000291 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000292 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000293}
294
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000295/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000296void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000297 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000298 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000299 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
300 // of spilling here is deterministic, if arbitrary.
301 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
302 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000303 spillVirtReg(MI, i);
304 LiveVirtRegs.clear();
305 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000306}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000307
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000308/// usePhysReg - Handle the direct use of a physical register.
309/// Check that the register is not used by a virtreg.
310/// Kill the physreg, marking it free.
311/// This may add implicit kills to MO->getParent() and invalidate MO.
312void RAFast::usePhysReg(MachineOperand &MO) {
313 unsigned PhysReg = MO.getReg();
314 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
315 "Bad usePhysReg operand");
316
317 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000318 case regDisabled:
319 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000320 case regReserved:
321 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000322 // Fall through
323 case regFree:
324 UsedInInstr.set(PhysReg);
325 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000326 return;
327 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000328 // The physreg was allocated to a virtual register. That means to value we
329 // wanted has been clobbered.
330 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000331 }
332
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000333 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000334 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
335 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000336 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000337 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000338 break;
339 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000340 assert(TRI->isSuperRegister(PhysReg, Alias) &&
341 "Instruction is not using a subregister of a reserved register");
342 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000343 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000344 UsedInInstr.set(Alias);
345 MO.getParent()->addRegisterKilled(Alias, TRI, true);
346 return;
347 case regFree:
348 if (TRI->isSuperRegister(PhysReg, Alias)) {
349 // Leave the superregister in the working set.
350 UsedInInstr.set(Alias);
351 MO.getParent()->addRegisterKilled(Alias, TRI, true);
352 return;
353 }
354 // Some other alias was in the working set - clear it.
355 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000356 break;
357 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000358 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000359 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000360 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000361
362 // All aliases are disabled, bring register into working set.
363 PhysRegState[PhysReg] = regFree;
364 UsedInInstr.set(PhysReg);
365 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000366}
367
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000368/// definePhysReg - Mark PhysReg as reserved or free after spilling any
369/// virtregs. This is very similar to defineVirtReg except the physreg is
370/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000371void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
372 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000373 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000374 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
375 case regDisabled:
376 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000377 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000378 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379 // Fall through.
380 case regFree:
381 case regReserved:
382 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000383 return;
384 }
385
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000386 // This is a disabled register, disable all aliases.
387 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000388 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
389 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000390 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000391 switch (unsigned VirtReg = PhysRegState[Alias]) {
392 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000393 break;
394 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000395 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000396 // Fall through.
397 case regFree:
398 case regReserved:
399 PhysRegState[Alias] = regDisabled;
400 if (TRI->isSuperRegister(PhysReg, Alias))
401 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000402 break;
403 }
404 }
405}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000406
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000407
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000408// calcSpillCost - Return the cost of spilling clearing out PhysReg and
409// aliases so it is free for allocation.
410// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
411// can be allocated directly.
412// Returns spillImpossible when PhysReg or an alias can't be spilled.
413unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000414 if (UsedInInstr.test(PhysReg))
415 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000416 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
417 case regDisabled:
418 break;
419 case regFree:
420 return 0;
421 case regReserved:
422 return spillImpossible;
423 default:
424 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
425 }
426
427 // This is a disabled register, add up const of aliases.
428 unsigned Cost = 0;
429 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
430 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000431 if (UsedInInstr.test(Alias))
432 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000433 switch (unsigned VirtReg = PhysRegState[Alias]) {
434 case regDisabled:
435 break;
436 case regFree:
437 ++Cost;
438 break;
439 case regReserved:
440 return spillImpossible;
441 default:
442 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
443 break;
444 }
445 }
446 return Cost;
447}
448
449
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000450/// assignVirtToPhysReg - This method updates local state so that we know
451/// that PhysReg is the proper container for VirtReg now. The physical
452/// register must not be used for anything else when this is called.
453///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000454void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
455 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000456 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000457 PhysRegState[PhysReg] = LRE.first;
458 assert(!LRE.second.PhysReg && "Already assigned a physreg");
459 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000460}
461
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000462/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000463void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000464 const unsigned VirtReg = LRE.first;
465
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000466 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
467 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000468
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000469 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000470
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000471 // Ignore invalid hints.
472 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000473 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000474 Hint = 0;
475
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000476 // Take hint when possible.
477 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000478 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000479 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000480 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000481 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000482 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000483 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000484 case spillImpossible:
485 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000486 }
487 }
488
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000489 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
490 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
491
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000492 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000493 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
494 unsigned PhysReg = *I;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000495 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
496 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000497 }
498
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000499 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000500 << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000501
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000502 unsigned BestReg = 0, BestCost = spillImpossible;
503 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
504 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000505 // Cost is 0 when all aliases are already disabled.
506 if (Cost == 0)
507 return assignVirtToPhysReg(LRE, *I);
508 if (Cost < BestCost)
509 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000510 }
511
512 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000513 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000514 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000515 }
516
517 // Nothing we can do.
518 std::string msg;
519 raw_string_ostream Msg(msg);
520 Msg << "Ran out of registers during register allocation!";
521 if (MI->isInlineAsm()) {
522 Msg << "\nPlease check your inline asm statement for "
523 << "invalid constraints:\n";
524 MI->print(Msg, TM);
525 }
526 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000527}
528
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000529/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000530RAFast::LiveRegMap::iterator
531RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
532 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000533 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
534 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000535 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000536 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000537 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
538 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000539 if (New) {
540 // If there is no hint, peek at the only use of this register.
541 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
542 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000543 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000544 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000545 if (UseMI.isCopyLike())
546 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000547 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000548 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000549 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000550 // Redefining a live register - kill at the last use, unless it is this
551 // instruction defining VirtReg multiple times.
552 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
553 addKillFlag(LR);
554 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000555 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000556 LR.LastUse = MI;
557 LR.LastOpNum = OpNum;
558 LR.Dirty = true;
559 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000560 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000561}
562
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000563/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000564RAFast::LiveRegMap::iterator
565RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
566 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000567 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
568 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000569 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000570 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000571 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
572 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000573 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000574 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000575 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000576 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000577 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000578 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000579 << TRI->getName(LR.PhysReg) << "\n");
580 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000581 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000582 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000583 if (isLastUseOfLocalReg(MO)) {
584 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000585 if (MO.isUse())
586 MO.setIsKill();
587 else
588 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000589 } else if (MO.isKill()) {
590 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
591 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000592 } else if (MO.isDead()) {
593 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
594 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000595 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000596 } else if (MO.isKill()) {
597 // We must remove kill flags from uses of reloaded registers because the
598 // register would be killed immediately, and there might be a second use:
599 // %foo = OR %x<kill>, %x
600 // This would cause a second reload of %x into a different register.
601 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
602 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000603 } else if (MO.isDead()) {
604 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
605 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000606 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000607 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000608 LR.LastUse = MI;
609 LR.LastOpNum = OpNum;
610 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000611 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000612}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000613
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000614// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
615// subregs. This may invalidate any operand pointers.
616// Return true if the operand kills its register.
617bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
618 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000619 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000621 return MO.isKill() || MO.isDead();
622 }
623
624 // Handle subregister index.
625 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
626 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000627
628 // A kill flag implies killing the full register. Add corresponding super
629 // register kill.
630 if (MO.isKill()) {
631 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000632 return true;
633 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000634 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000635}
636
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000637// Handle special instruction operand like early clobbers and tied ops when
638// there are additional physreg defines.
639void RAFast::handleThroughOperands(MachineInstr *MI,
640 SmallVectorImpl<unsigned> &VirtDead) {
641 DEBUG(dbgs() << "Scanning for through registers:");
642 SmallSet<unsigned, 8> ThroughRegs;
643 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
644 MachineOperand &MO = MI->getOperand(i);
645 if (!MO.isReg()) continue;
646 unsigned Reg = MO.getReg();
647 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000648 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
649 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000650 if (ThroughRegs.insert(Reg))
651 DEBUG(dbgs() << " %reg" << Reg);
652 }
653 }
654
655 // If any physreg defines collide with preallocated through registers,
656 // we must spill and reallocate.
657 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
658 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
659 MachineOperand &MO = MI->getOperand(i);
660 if (!MO.isReg() || !MO.isDef()) continue;
661 unsigned Reg = MO.getReg();
662 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
663 UsedInInstr.set(Reg);
664 if (ThroughRegs.count(PhysRegState[Reg]))
665 definePhysReg(MI, Reg, regFree);
666 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
667 UsedInInstr.set(*AS);
668 if (ThroughRegs.count(PhysRegState[*AS]))
669 definePhysReg(MI, *AS, regFree);
670 }
671 }
672
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000673 SmallVector<unsigned, 8> PartialDefs;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000674 DEBUG(dbgs() << "Allocating tied uses and early clobbers.\n");
675 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
676 MachineOperand &MO = MI->getOperand(i);
677 if (!MO.isReg()) continue;
678 unsigned Reg = MO.getReg();
679 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
680 if (MO.isUse()) {
681 unsigned DefIdx = 0;
682 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
683 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
684 << DefIdx << ".\n");
685 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
686 unsigned PhysReg = LRI->second.PhysReg;
687 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000688 // Note: we don't update the def operand yet. That would cause the normal
689 // def-scan to attempt spilling.
690 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
691 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
692 // Reload the register, but don't assign to the operand just yet.
693 // That would confuse the later phys-def processing pass.
694 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
695 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000696 } else if (MO.isEarlyClobber()) {
697 // Note: defineVirtReg may invalidate MO.
698 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
699 unsigned PhysReg = LRI->second.PhysReg;
700 if (setPhysReg(MI, i, PhysReg))
701 VirtDead.push_back(Reg);
702 }
703 }
704
705 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
706 UsedInInstr.reset();
707 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
708 MachineOperand &MO = MI->getOperand(i);
709 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
710 unsigned Reg = MO.getReg();
711 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
712 UsedInInstr.set(Reg);
713 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
714 UsedInInstr.set(*AS);
715 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000716
717 // Also mark PartialDefs as used to avoid reallocation.
718 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
719 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000720}
721
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000722void RAFast::AllocateBasicBlock() {
723 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000724
725 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000726 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000727
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000728 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000729
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000730 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000731 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
732 E = MBB->livein_end(); I != E; ++I)
733 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000734
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000735 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000736 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000737
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000738 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000739 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000740 MachineInstr *MI = MII++;
741 const TargetInstrDesc &TID = MI->getDesc();
742 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000743 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000744 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
745 if (PhysRegState[Reg] == regDisabled) continue;
746 dbgs() << " " << TRI->getName(Reg);
747 switch(PhysRegState[Reg]) {
748 case regFree:
749 break;
750 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000751 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000752 break;
753 default:
754 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000755 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000756 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000757 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000758 "Bad inverse map");
759 break;
760 }
761 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000762 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000763 // Check that LiveVirtRegs is the inverse.
764 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
765 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000766 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
767 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000768 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000769 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000770 assert(PhysRegState[i->second.PhysReg] == i->first &&
771 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000772 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000773 });
774
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000775 // Debug values are not allowed to change codegen in any way.
776 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000777 bool ScanDbgValue = true;
778 while (ScanDbgValue) {
779 ScanDbgValue = false;
780 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
781 MachineOperand &MO = MI->getOperand(i);
782 if (!MO.isReg()) continue;
783 unsigned Reg = MO.getReg();
784 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Devang Patel459a36b2010-08-04 18:42:02 +0000785 LiveDbgValueMap[Reg] = MI;
Devang Patel58b81762010-07-19 23:25:39 +0000786 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
787 if (LRI != LiveVirtRegs.end())
788 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000789 else {
Devang Patel58b81762010-07-19 23:25:39 +0000790 int SS = StackSlotForVirtReg[Reg];
791 if (SS == -1)
Devang Patel7a029b62010-07-09 21:48:31 +0000792 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel58b81762010-07-19 23:25:39 +0000793 else {
794 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000795 int64_t Offset = MI->getOperand(1).getImm();
Devang Patel58b81762010-07-19 23:25:39 +0000796 const MDNode *MDPtr =
797 MI->getOperand(MI->getNumOperands()-1).getMetadata();
798 DebugLoc DL = MI->getDebugLoc();
799 if (MachineInstr *NewDV =
800 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
801 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
802 MachineBasicBlock *MBB = MI->getParent();
803 MBB->insert(MBB->erase(MI), NewDV);
804 // Scan NewDV operands from the beginning.
805 MI = NewDV;
806 ScanDbgValue = true;
807 break;
808 } else
809 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
810 }
Devang Patel7a029b62010-07-09 21:48:31 +0000811 }
812 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000813 }
814 // Next instruction.
815 continue;
816 }
817
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000818 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000819 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000820 if (MI->isCopy()) {
821 CopyDst = MI->getOperand(0).getReg();
822 CopySrc = MI->getOperand(1).getReg();
823 CopyDstSub = MI->getOperand(0).getSubReg();
824 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000825 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000826
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000827 // Track registers used by instruction.
828 UsedInInstr.reset();
829
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000830 // First scan.
831 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000832 // Find the end of the virtreg operands
833 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000834 bool hasTiedOps = false;
835 bool hasEarlyClobbers = false;
836 bool hasPartialRedefs = false;
837 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000838 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
839 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000840 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000841 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000842 if (!Reg) continue;
843 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
844 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000845 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000846 hasTiedOps = hasTiedOps ||
847 TID.getOperandConstraint(i, TOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000848 } else {
849 if (MO.isEarlyClobber())
850 hasEarlyClobbers = true;
851 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
852 hasPartialRedefs = true;
853 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000854 continue;
855 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000856 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000857 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000858 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000859 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000860 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
861 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000862 hasEarlyClobbers = true;
863 } else
864 hasPhysDefs = true;
865 }
866
867 // The instruction may have virtual register operands that must be allocated
868 // the same register at use-time and def-time: early clobbers and tied
869 // operands. If there are also physical defs, these registers must avoid
870 // both physical defs and uses, making them more constrained than normal
871 // operands.
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000872 // Similarly, if there are multiple defs and tied operands, we must make sure
873 // the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000874 // We didn't detect inline asm tied operands above, so just make this extra
875 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000876 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000877 (hasTiedOps && (hasPhysDefs || TID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000878 handleThroughOperands(MI, VirtDead);
879 // Don't attempt coalescing when we have funny stuff going on.
880 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000881 // Pretend we have early clobbers so the use operands get marked below.
882 // This is not necessary for the common case of a single tied use.
883 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000884 }
885
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000886 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000887 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000888 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000889 MachineOperand &MO = MI->getOperand(i);
890 if (!MO.isReg()) continue;
891 unsigned Reg = MO.getReg();
892 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
893 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000894 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
895 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000896 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000897 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000898 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000899 }
900 }
901
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000902 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000903
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000904 // Track registers defined by instruction - early clobbers and tied uses at
905 // this point.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000906 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000907 if (hasEarlyClobbers) {
908 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
909 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000910 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000911 unsigned Reg = MO.getReg();
912 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000913 // Look for physreg defs and tied uses.
914 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000915 UsedInInstr.set(Reg);
916 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
917 UsedInInstr.set(*AS);
918 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000919 }
920
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000921 unsigned DefOpEnd = MI->getNumOperands();
922 if (TID.isCall()) {
923 // Spill all virtregs before a call. This serves two purposes: 1. If an
924 // exception is thrown, the landing pad is going to expect to find registers
925 // in their spill slots, and 2. we don't have to wade through all the
926 // <imp-def> operands on the call instruction.
927 DefOpEnd = VirtOpEnd;
928 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
929 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000930
931 // The imp-defs are skipped below, but we still need to mark those
932 // registers as used by the function.
933 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000934 }
935
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000936 // Third scan.
937 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000938 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000939 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000940 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
941 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000942 unsigned Reg = MO.getReg();
943
944 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000945 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000946 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
947 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000948 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000949 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000950 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
951 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000952 if (setPhysReg(MI, i, PhysReg)) {
953 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000954 CopyDst = 0; // cancel coalescing;
955 } else
956 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000957 }
958
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000959 // Kill dead defs after the scan to ensure that multiple defs of the same
960 // register are allocated identically. We didn't need to do this for uses
961 // because we are crerating our own kill flags, and they are always at the
962 // last use.
963 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
964 killVirtReg(VirtDead[i]);
965 VirtDead.clear();
966
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000967 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000968
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000969 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
970 DEBUG(dbgs() << "-- coalescing: " << *MI);
971 Coalesced.push_back(MI);
972 } else {
973 DEBUG(dbgs() << "<< " << *MI);
974 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000975 }
976
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000977 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000978 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
979 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000980
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000981 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000982 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000983 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000984 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000985 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000986
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000987 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000988}
989
990/// runOnMachineFunction - Register allocate the whole function
991///
992bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000993 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
994 << "********** Function: "
995 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000996 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000997 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000998 TM = &Fn.getTarget();
999 TRI = TM->getRegisterInfo();
1000 TII = TM->getInstrInfo();
1001
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001002 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +00001003 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001004
1005 // initialize the virtual->physical register map to have a 'null'
1006 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001007 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001008 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001009
1010 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001011 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1012 MBBi != MBBe; ++MBBi) {
1013 MBB = &*MBBi;
1014 AllocateBasicBlock();
1015 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001016
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001017 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001018 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001019
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001020 // Add the clobber lists for all the instructions we skipped earlier.
1021 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
1022 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1023 if (const unsigned *Defs = (*I)->getImplicitDefs())
1024 while (*Defs)
1025 MRI->setPhysRegUsed(*Defs++);
1026
1027 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001028 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001029 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001030 return true;
1031}
1032
1033FunctionPass *llvm::createFastRegisterAllocator() {
1034 return new RAFast();
1035}