blob: b3b57607086323290842b39587dda12803fe9fdb [file] [log] [blame]
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "llvm/BasicBlock.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/RegAllocRegistry.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/ADT/DenseMap.h"
30#include "llvm/ADT/IndexedMap.h"
31#include "llvm/ADT/SmallSet.h"
32#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
35#include <algorithm>
36using namespace llvm;
37
38STATISTIC(NumStores, "Number of stores added");
39STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000040STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000041
42static RegisterRegAlloc
43 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
44
45namespace {
46 class RAFast : public MachineFunctionPass {
47 public:
48 static char ID;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +000049 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1),
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +000050 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000051 private:
52 const TargetMachine *TM;
53 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000054 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000055 const TargetRegisterInfo *TRI;
56 const TargetInstrInfo *TII;
57
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000058 // Basic block currently being allocated.
59 MachineBasicBlock *MBB;
60
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000061 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
62 // values are spilled.
63 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
64
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000065 // Everything we know about a live virtual register.
66 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000067 MachineInstr *LastUse; // Last instr to use reg.
68 unsigned PhysReg; // Currently held here.
69 unsigned short LastOpNum; // OpNum on LastUse.
70 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000071
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000072 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000073 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000074 };
75
76 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000077 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000078
79 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000080 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000081 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000082
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000083 // RegState - Track the state of a physical register.
84 enum RegState {
85 // A disabled register is not available for allocation, but an alias may
86 // be in use. A register can only be moved out of the disabled state if
87 // all aliases are disabled.
88 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000089
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000090 // A free register is not currently in use and can be allocated
91 // immediately without checking aliases.
92 regFree,
93
94 // A reserved register has been assigned expolicitly (e.g., setting up a
95 // call parameter), and it remains reserved until it is used.
96 regReserved
97
98 // A register state may also be a virtual register number, indication that
99 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000100 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000101 };
102
103 // PhysRegState - One of the RegState enums, or a virtreg.
104 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000105
106 // UsedInInstr - BitVector of physregs that are used in the current
107 // instruction, and so cannot be allocated.
108 BitVector UsedInInstr;
109
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000110 // Allocatable - vector of allocatable physical registers.
111 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000112
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000113 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
114 // completely after spilling all live registers. LiveRegMap entries should
115 // not be erased.
116 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000117
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000118 enum {
119 spillClean = 1,
120 spillDirty = 100,
121 spillImpossible = ~0u
122 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000123 public:
124 virtual const char *getPassName() const {
125 return "Fast Register Allocator";
126 }
127
128 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
129 AU.setPreservesCFG();
130 AU.addRequiredID(PHIEliminationID);
131 AU.addRequiredID(TwoAddressInstructionPassID);
132 MachineFunctionPass::getAnalysisUsage(AU);
133 }
134
135 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000136 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000137 void AllocateBasicBlock();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000138 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000139 bool isLastUseOfLocalReg(MachineOperand&);
140
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000141 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000142 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000143 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000144 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000145 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000146
147 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000148 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000149 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000150 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
151 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000152 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
153 unsigned VirtReg, unsigned Hint);
154 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
155 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000156 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000157 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000158 };
159 char RAFast::ID = 0;
160}
161
162/// getStackSpaceFor - This allocates space for the specified virtual register
163/// to be held on the stack.
164int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
165 // Find the location Reg would belong...
166 int SS = StackSlotForVirtReg[VirtReg];
167 if (SS != -1)
168 return SS; // Already has space allocated?
169
170 // Allocate a new stack object for this spill location...
171 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
172 RC->getAlignment());
173
174 // Assign the slot.
175 StackSlotForVirtReg[VirtReg] = FrameIdx;
176 return FrameIdx;
177}
178
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000179/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
180/// its virtual register, and it is guaranteed to be a block-local register.
181///
182bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
183 // Check for non-debug uses or defs following MO.
184 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000185 MachineOperand *Next = &MO;
186 while ((Next = Next->getNextOperandForReg()))
187 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000188 return false;
189
190 // If the register has ever been spilled or reloaded, we conservatively assume
191 // it is a global register used in multiple blocks.
192 if (StackSlotForVirtReg[MO.getReg()] != -1)
193 return false;
194
195 // Check that the use/def chain has exactly one operand - MO.
196 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
197}
198
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000199/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000200void RAFast::addKillFlag(const LiveReg &LR) {
201 if (!LR.LastUse) return;
202 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000203 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
204 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000205 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000206 else
207 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
208 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000209}
210
211/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000212void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
213 addKillFlag(LRI->second);
214 const LiveReg &LR = LRI->second;
215 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000216 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000217 // Erase from LiveVirtRegs unless we're spilling in bulk.
218 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000219 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000220}
221
222/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000223void RAFast::killVirtReg(unsigned VirtReg) {
224 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
225 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000226 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
227 if (LRI != LiveVirtRegs.end())
228 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000229}
230
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000231/// spillVirtReg - This method spills the value specified by VirtReg into the
232/// corresponding stack slot if needed. If isKill is set, the register is also
233/// killed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000234void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000235 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
236 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000237 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
238 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
239 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000240}
241
242/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000243void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000244 LiveRegMap::iterator LRI) {
245 LiveReg &LR = LRI->second;
246 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000247
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000248 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000249 // If this physreg is used by the instruction, we want to kill it on the
250 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000251 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000252 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000253 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000254 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000255 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
256 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000257 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000258 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000259 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000260
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000261 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000262 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000263 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000264 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000265}
266
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000267/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000268void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000269 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000270 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000271 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
272 // of spilling here is deterministic, if arbitrary.
273 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
274 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000275 spillVirtReg(MI, i);
276 LiveVirtRegs.clear();
277 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000278}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000279
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000280/// usePhysReg - Handle the direct use of a physical register.
281/// Check that the register is not used by a virtreg.
282/// Kill the physreg, marking it free.
283/// This may add implicit kills to MO->getParent() and invalidate MO.
284void RAFast::usePhysReg(MachineOperand &MO) {
285 unsigned PhysReg = MO.getReg();
286 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
287 "Bad usePhysReg operand");
288
289 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000290 case regDisabled:
291 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000292 case regReserved:
293 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000294 // Fall through
295 case regFree:
296 UsedInInstr.set(PhysReg);
297 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000298 return;
299 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000300 // The physreg was allocated to a virtual register. That means to value we
301 // wanted has been clobbered.
302 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000303 }
304
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000305 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000306 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
307 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000308 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000309 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000310 break;
311 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000312 assert(TRI->isSuperRegister(PhysReg, Alias) &&
313 "Instruction is not using a subregister of a reserved register");
314 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000315 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000316 UsedInInstr.set(Alias);
317 MO.getParent()->addRegisterKilled(Alias, TRI, true);
318 return;
319 case regFree:
320 if (TRI->isSuperRegister(PhysReg, Alias)) {
321 // Leave the superregister in the working set.
322 UsedInInstr.set(Alias);
323 MO.getParent()->addRegisterKilled(Alias, TRI, true);
324 return;
325 }
326 // Some other alias was in the working set - clear it.
327 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000328 break;
329 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000330 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000331 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000332 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000333
334 // All aliases are disabled, bring register into working set.
335 PhysRegState[PhysReg] = regFree;
336 UsedInInstr.set(PhysReg);
337 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000338}
339
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000340/// definePhysReg - Mark PhysReg as reserved or free after spilling any
341/// virtregs. This is very similar to defineVirtReg except the physreg is
342/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000343void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
344 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000345 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000346 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
347 case regDisabled:
348 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000349 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000350 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000351 // Fall through.
352 case regFree:
353 case regReserved:
354 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000355 return;
356 }
357
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000358 // This is a disabled register, disable all aliases.
359 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000360 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
361 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000362 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000363 switch (unsigned VirtReg = PhysRegState[Alias]) {
364 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000365 break;
366 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000367 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000368 // Fall through.
369 case regFree:
370 case regReserved:
371 PhysRegState[Alias] = regDisabled;
372 if (TRI->isSuperRegister(PhysReg, Alias))
373 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000374 break;
375 }
376 }
377}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000378
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000380// calcSpillCost - Return the cost of spilling clearing out PhysReg and
381// aliases so it is free for allocation.
382// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
383// can be allocated directly.
384// Returns spillImpossible when PhysReg or an alias can't be spilled.
385unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000386 if (UsedInInstr.test(PhysReg))
387 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000388 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
389 case regDisabled:
390 break;
391 case regFree:
392 return 0;
393 case regReserved:
394 return spillImpossible;
395 default:
396 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
397 }
398
399 // This is a disabled register, add up const of aliases.
400 unsigned Cost = 0;
401 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
402 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000403 if (UsedInInstr.test(Alias))
404 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000405 switch (unsigned VirtReg = PhysRegState[Alias]) {
406 case regDisabled:
407 break;
408 case regFree:
409 ++Cost;
410 break;
411 case regReserved:
412 return spillImpossible;
413 default:
414 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
415 break;
416 }
417 }
418 return Cost;
419}
420
421
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000422/// assignVirtToPhysReg - This method updates local state so that we know
423/// that PhysReg is the proper container for VirtReg now. The physical
424/// register must not be used for anything else when this is called.
425///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000426void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
427 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000428 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000429 PhysRegState[PhysReg] = LRE.first;
430 assert(!LRE.second.PhysReg && "Already assigned a physreg");
431 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000432}
433
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000434/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000435void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000436 const unsigned VirtReg = LRE.first;
437
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000438 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
439 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000440
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000441 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000442
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000443 // Ignore invalid hints.
444 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000445 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000446 Hint = 0;
447
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000448 // Take hint when possible.
449 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000450 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000451 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000452 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000453 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000454 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000455 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000456 case spillImpossible:
457 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000458 }
459 }
460
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000461 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
462 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
463
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000464 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000465 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
466 unsigned PhysReg = *I;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000467 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
468 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000469 }
470
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000471 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000472 << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000473
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000474 unsigned BestReg = 0, BestCost = spillImpossible;
475 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
476 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000477 // Cost is 0 when all aliases are already disabled.
478 if (Cost == 0)
479 return assignVirtToPhysReg(LRE, *I);
480 if (Cost < BestCost)
481 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000482 }
483
484 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000485 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000486 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000487 }
488
489 // Nothing we can do.
490 std::string msg;
491 raw_string_ostream Msg(msg);
492 Msg << "Ran out of registers during register allocation!";
493 if (MI->isInlineAsm()) {
494 Msg << "\nPlease check your inline asm statement for "
495 << "invalid constraints:\n";
496 MI->print(Msg, TM);
497 }
498 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000499}
500
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000501/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000502RAFast::LiveRegMap::iterator
503RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
504 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000505 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
506 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000507 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000508 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000509 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
510 LiveReg &LR = LRI->second;
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000511 bool PartialRedef = MI->getOperand(OpNum).getSubReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000512 if (New) {
513 // If there is no hint, peek at the only use of this register.
514 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
515 MRI->hasOneNonDBGUse(VirtReg)) {
516 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
517 // It's a copy, use the destination register as a hint.
518 if (TII->isMoveInstr(*MRI->use_nodbg_begin(VirtReg),
519 SrcReg, DstReg, SrcSubReg, DstSubReg))
520 Hint = DstReg;
521 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000522 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000523 // If this is only a partial redefinition, we must reload the other parts.
524 if (PartialRedef && MI->readsVirtualRegister(VirtReg)) {
525 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
526 int FI = getStackSpaceFor(VirtReg, RC);
527 DEBUG(dbgs() << "Reloading for partial redef: %reg" << VirtReg << "\n");
528 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FI, RC, TRI);
529 ++NumLoads;
530 }
531 } else if (LR.LastUse && !PartialRedef) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000532 // Redefining a live register - kill at the last use, unless it is this
533 // instruction defining VirtReg multiple times.
534 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
535 addKillFlag(LR);
536 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000537 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000538 LR.LastUse = MI;
539 LR.LastOpNum = OpNum;
540 LR.Dirty = true;
541 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000542 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000543}
544
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000545/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000546RAFast::LiveRegMap::iterator
547RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
548 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000549 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
550 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000551 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000552 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000553 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
554 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000555 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000556 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000557 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000558 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000559 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000560 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000561 << TRI->getName(LR.PhysReg) << "\n");
562 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000563 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000564 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000565 if (isLastUseOfLocalReg(MO)) {
566 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
567 MO.setIsKill();
568 } else if (MO.isKill()) {
569 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
570 MO.setIsKill(false);
571 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000572 } else if (MO.isKill()) {
573 // We must remove kill flags from uses of reloaded registers because the
574 // register would be killed immediately, and there might be a second use:
575 // %foo = OR %x<kill>, %x
576 // This would cause a second reload of %x into a different register.
577 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
578 MO.setIsKill(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000579 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000580 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000581 LR.LastUse = MI;
582 LR.LastOpNum = OpNum;
583 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000584 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000585}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000586
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000587// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
588// subregs. This may invalidate any operand pointers.
589// Return true if the operand kills its register.
590bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
591 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000592 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000593 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000594 return MO.isKill() || MO.isDead();
595 }
596
597 // Handle subregister index.
598 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
599 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000600
601 // A kill flag implies killing the full register. Add corresponding super
602 // register kill.
603 if (MO.isKill()) {
604 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000605 return true;
606 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000607 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000608}
609
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000610void RAFast::AllocateBasicBlock() {
611 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000612
613 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000614 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000615
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000616 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000617
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000618 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000619 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
620 E = MBB->livein_end(); I != E; ++I)
621 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000622
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000623 SmallVector<unsigned, 8> PhysECs, VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000624 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000625
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000626 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000627 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000628 MachineInstr *MI = MII++;
629 const TargetInstrDesc &TID = MI->getDesc();
630 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000631 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000632 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
633 if (PhysRegState[Reg] == regDisabled) continue;
634 dbgs() << " " << TRI->getName(Reg);
635 switch(PhysRegState[Reg]) {
636 case regFree:
637 break;
638 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000639 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000640 break;
641 default:
642 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000643 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000644 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000645 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000646 "Bad inverse map");
647 break;
648 }
649 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000650 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000651 // Check that LiveVirtRegs is the inverse.
652 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
653 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000654 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
655 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000656 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000657 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000658 assert(PhysRegState[i->second.PhysReg] == i->first &&
659 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000660 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000661 });
662
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000663 // Debug values are not allowed to change codegen in any way.
664 if (MI->isDebugValue()) {
665 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
666 MachineOperand &MO = MI->getOperand(i);
667 if (!MO.isReg()) continue;
668 unsigned Reg = MO.getReg();
669 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000670 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
671 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000672 setPhysReg(MI, i, LRI->second.PhysReg);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000673 else
674 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000675 }
676 // Next instruction.
677 continue;
678 }
679
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000680 // If this is a copy, we may be able to coalesce.
681 unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub;
682 if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub))
683 CopySrc = CopyDst = 0;
684
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000685 // Track registers used by instruction.
686 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000687 PhysECs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000688
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000689 // First scan.
690 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000691 // Find the end of the virtreg operands
692 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000693 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
694 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000695 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000696 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000697 if (!Reg) continue;
698 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
699 VirtOpEnd = i+1;
700 continue;
701 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000702 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000703 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000704 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000705 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000706 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000707 PhysECs.push_back(Reg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000708 }
709 }
710
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000711 // Second scan.
712 // Allocate virtreg uses and early clobbers.
713 // Collect VirtKills
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000714 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000715 MachineOperand &MO = MI->getOperand(i);
716 if (!MO.isReg()) continue;
717 unsigned Reg = MO.getReg();
718 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
719 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000720 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
721 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000722 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000723 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000724 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000725 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000726 // Note: defineVirtReg may invalidate MO.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000727 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
728 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000729 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000730 PhysECs.push_back(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000731 }
732 }
733
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000734 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000735
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000736 // Track registers defined by instruction - early clobbers at this point.
737 UsedInInstr.reset();
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000738 for (unsigned i = 0, e = PhysECs.size(); i != e; ++i) {
739 unsigned PhysReg = PhysECs[i];
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000740 UsedInInstr.set(PhysReg);
741 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
742 unsigned Alias = *AS; ++AS)
743 UsedInInstr.set(Alias);
744 }
745
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000746 unsigned DefOpEnd = MI->getNumOperands();
747 if (TID.isCall()) {
748 // Spill all virtregs before a call. This serves two purposes: 1. If an
749 // exception is thrown, the landing pad is going to expect to find registers
750 // in their spill slots, and 2. we don't have to wade through all the
751 // <imp-def> operands on the call instruction.
752 DefOpEnd = VirtOpEnd;
753 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
754 spillAll(MI);
755 }
756
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000757 // Third scan.
758 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000759 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000760 MachineOperand &MO = MI->getOperand(i);
761 if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue;
762 unsigned Reg = MO.getReg();
763
764 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000765 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000766 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
767 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000768 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000769 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000770 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
771 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000772 if (setPhysReg(MI, i, PhysReg)) {
773 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000774 CopyDst = 0; // cancel coalescing;
775 } else
776 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000777 }
778
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000779 // Kill dead defs after the scan to ensure that multiple defs of the same
780 // register are allocated identically. We didn't need to do this for uses
781 // because we are crerating our own kill flags, and they are always at the
782 // last use.
783 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
784 killVirtReg(VirtDead[i]);
785 VirtDead.clear();
786
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000787 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000788
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000789 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
790 DEBUG(dbgs() << "-- coalescing: " << *MI);
791 Coalesced.push_back(MI);
792 } else {
793 DEBUG(dbgs() << "<< " << *MI);
794 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000795 }
796
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000797 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000798 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
799 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000800
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000801 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000802 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000803 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000804 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000805 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000806
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000807 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000808}
809
810/// runOnMachineFunction - Register allocate the whole function
811///
812bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000813 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
814 << "********** Function: "
815 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000816 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000817 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000818 TM = &Fn.getTarget();
819 TRI = TM->getRegisterInfo();
820 TII = TM->getInstrInfo();
821
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000822 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000823 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000824
825 // initialize the virtual->physical register map to have a 'null'
826 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000827 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000828 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000829
830 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000831 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
832 MBBi != MBBe; ++MBBi) {
833 MBB = &*MBBi;
834 AllocateBasicBlock();
835 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000836
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000837 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000838 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000839
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000840 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000841 return true;
842}
843
844FunctionPass *llvm::createFastRegisterAllocator() {
845 return new RAFast();
846}