blob: e34fcdbebba01d610c9c3ff98418fb523034c00c [file] [log] [blame]
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "llvm/BasicBlock.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/RegAllocRegistry.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/ADT/DenseMap.h"
30#include "llvm/ADT/IndexedMap.h"
31#include "llvm/ADT/SmallSet.h"
32#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
35#include <algorithm>
36using namespace llvm;
37
38STATISTIC(NumStores, "Number of stores added");
39STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000040STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000041
42static RegisterRegAlloc
43 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
44
45namespace {
46 class RAFast : public MachineFunctionPass {
47 public:
48 static char ID;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +000049 RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1),
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +000050 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000051 private:
52 const TargetMachine *TM;
53 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000054 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000055 const TargetRegisterInfo *TRI;
56 const TargetInstrInfo *TII;
57
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000058 // Basic block currently being allocated.
59 MachineBasicBlock *MBB;
60
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000061 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
62 // values are spilled.
63 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
64
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000065 // Everything we know about a live virtual register.
66 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000067 MachineInstr *LastUse; // Last instr to use reg.
68 unsigned PhysReg; // Currently held here.
69 unsigned short LastOpNum; // OpNum on LastUse.
70 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000071
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000072 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000073 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000074 };
75
76 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000077 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000078
79 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000080 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000081 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000082
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000083 // RegState - Track the state of a physical register.
84 enum RegState {
85 // A disabled register is not available for allocation, but an alias may
86 // be in use. A register can only be moved out of the disabled state if
87 // all aliases are disabled.
88 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000089
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000090 // A free register is not currently in use and can be allocated
91 // immediately without checking aliases.
92 regFree,
93
94 // A reserved register has been assigned expolicitly (e.g., setting up a
95 // call parameter), and it remains reserved until it is used.
96 regReserved
97
98 // A register state may also be a virtual register number, indication that
99 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000100 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000101 };
102
103 // PhysRegState - One of the RegState enums, or a virtreg.
104 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000105
106 // UsedInInstr - BitVector of physregs that are used in the current
107 // instruction, and so cannot be allocated.
108 BitVector UsedInInstr;
109
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000110 // Allocatable - vector of allocatable physical registers.
111 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000112
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000113 // SkippedInstrs - Descriptors of instructions whose clobber list was ignored
114 // because all registers were spilled. It is still necessary to mark all the
115 // clobbered registers as used by the function.
116 SmallPtrSet<const TargetInstrDesc*, 4> SkippedInstrs;
117
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000118 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
119 // completely after spilling all live registers. LiveRegMap entries should
120 // not be erased.
121 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000122
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000123 enum {
124 spillClean = 1,
125 spillDirty = 100,
126 spillImpossible = ~0u
127 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000128 public:
129 virtual const char *getPassName() const {
130 return "Fast Register Allocator";
131 }
132
133 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
134 AU.setPreservesCFG();
135 AU.addRequiredID(PHIEliminationID);
136 AU.addRequiredID(TwoAddressInstructionPassID);
137 MachineFunctionPass::getAnalysisUsage(AU);
138 }
139
140 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000141 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000142 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000143 void handleThroughOperands(MachineInstr *MI,
144 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000145 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000146 bool isLastUseOfLocalReg(MachineOperand&);
147
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000148 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000149 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000150 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000151 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000152 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000153
154 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000155 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000156 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000157 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
158 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000159 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
160 unsigned VirtReg, unsigned Hint);
161 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
162 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000163 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000164 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000165 };
166 char RAFast::ID = 0;
167}
168
169/// getStackSpaceFor - This allocates space for the specified virtual register
170/// to be held on the stack.
171int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
172 // Find the location Reg would belong...
173 int SS = StackSlotForVirtReg[VirtReg];
174 if (SS != -1)
175 return SS; // Already has space allocated?
176
177 // Allocate a new stack object for this spill location...
178 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
179 RC->getAlignment());
180
181 // Assign the slot.
182 StackSlotForVirtReg[VirtReg] = FrameIdx;
183 return FrameIdx;
184}
185
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000186/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
187/// its virtual register, and it is guaranteed to be a block-local register.
188///
189bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
190 // Check for non-debug uses or defs following MO.
191 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000192 MachineOperand *Next = &MO;
193 while ((Next = Next->getNextOperandForReg()))
194 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000195 return false;
196
197 // If the register has ever been spilled or reloaded, we conservatively assume
198 // it is a global register used in multiple blocks.
199 if (StackSlotForVirtReg[MO.getReg()] != -1)
200 return false;
201
202 // Check that the use/def chain has exactly one operand - MO.
203 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
204}
205
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000206/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000207void RAFast::addKillFlag(const LiveReg &LR) {
208 if (!LR.LastUse) return;
209 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000210 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
211 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000212 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000213 else
214 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
215 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000216}
217
218/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000219void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
220 addKillFlag(LRI->second);
221 const LiveReg &LR = LRI->second;
222 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000223 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000224 // Erase from LiveVirtRegs unless we're spilling in bulk.
225 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000226 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000227}
228
229/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000230void RAFast::killVirtReg(unsigned VirtReg) {
231 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
232 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000233 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
234 if (LRI != LiveVirtRegs.end())
235 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000236}
237
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000238/// spillVirtReg - This method spills the value specified by VirtReg into the
239/// corresponding stack slot if needed. If isKill is set, the register is also
240/// killed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000241void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000242 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
243 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000244 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
245 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
246 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000247}
248
249/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000250void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000251 LiveRegMap::iterator LRI) {
252 LiveReg &LR = LRI->second;
253 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000254
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000255 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000256 // If this physreg is used by the instruction, we want to kill it on the
257 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000258 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000259 LR.Dirty = false;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000260 DEBUG(dbgs() << "Spilling %reg" << LRI->first
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000261 << " in " << TRI->getName(LR.PhysReg));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000262 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
263 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000264 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000265 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000266 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000267
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000268 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000269 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000270 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000271 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000272}
273
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000274/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000275void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000276 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000277 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000278 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
279 // of spilling here is deterministic, if arbitrary.
280 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
281 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000282 spillVirtReg(MI, i);
283 LiveVirtRegs.clear();
284 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000285}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000286
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000287/// usePhysReg - Handle the direct use of a physical register.
288/// Check that the register is not used by a virtreg.
289/// Kill the physreg, marking it free.
290/// This may add implicit kills to MO->getParent() and invalidate MO.
291void RAFast::usePhysReg(MachineOperand &MO) {
292 unsigned PhysReg = MO.getReg();
293 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
294 "Bad usePhysReg operand");
295
296 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000297 case regDisabled:
298 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000299 case regReserved:
300 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000301 // Fall through
302 case regFree:
303 UsedInInstr.set(PhysReg);
304 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000305 return;
306 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000307 // The physreg was allocated to a virtual register. That means to value we
308 // wanted has been clobbered.
309 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000310 }
311
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000312 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000313 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
314 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000315 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000316 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000317 break;
318 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000319 assert(TRI->isSuperRegister(PhysReg, Alias) &&
320 "Instruction is not using a subregister of a reserved register");
321 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000322 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000323 UsedInInstr.set(Alias);
324 MO.getParent()->addRegisterKilled(Alias, TRI, true);
325 return;
326 case regFree:
327 if (TRI->isSuperRegister(PhysReg, Alias)) {
328 // Leave the superregister in the working set.
329 UsedInInstr.set(Alias);
330 MO.getParent()->addRegisterKilled(Alias, TRI, true);
331 return;
332 }
333 // Some other alias was in the working set - clear it.
334 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000335 break;
336 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000337 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000338 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000339 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000340
341 // All aliases are disabled, bring register into working set.
342 PhysRegState[PhysReg] = regFree;
343 UsedInInstr.set(PhysReg);
344 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000345}
346
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000347/// definePhysReg - Mark PhysReg as reserved or free after spilling any
348/// virtregs. This is very similar to defineVirtReg except the physreg is
349/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000350void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
351 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000352 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000353 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
354 case regDisabled:
355 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000356 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000357 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000358 // Fall through.
359 case regFree:
360 case regReserved:
361 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000362 return;
363 }
364
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000365 // This is a disabled register, disable all aliases.
366 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000367 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
368 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000369 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000370 switch (unsigned VirtReg = PhysRegState[Alias]) {
371 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000372 break;
373 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000374 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000375 // Fall through.
376 case regFree:
377 case regReserved:
378 PhysRegState[Alias] = regDisabled;
379 if (TRI->isSuperRegister(PhysReg, Alias))
380 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000381 break;
382 }
383 }
384}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000385
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000386
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000387// calcSpillCost - Return the cost of spilling clearing out PhysReg and
388// aliases so it is free for allocation.
389// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
390// can be allocated directly.
391// Returns spillImpossible when PhysReg or an alias can't be spilled.
392unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000393 if (UsedInInstr.test(PhysReg))
394 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000395 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
396 case regDisabled:
397 break;
398 case regFree:
399 return 0;
400 case regReserved:
401 return spillImpossible;
402 default:
403 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
404 }
405
406 // This is a disabled register, add up const of aliases.
407 unsigned Cost = 0;
408 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
409 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000410 if (UsedInInstr.test(Alias))
411 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000412 switch (unsigned VirtReg = PhysRegState[Alias]) {
413 case regDisabled:
414 break;
415 case regFree:
416 ++Cost;
417 break;
418 case regReserved:
419 return spillImpossible;
420 default:
421 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
422 break;
423 }
424 }
425 return Cost;
426}
427
428
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000429/// assignVirtToPhysReg - This method updates local state so that we know
430/// that PhysReg is the proper container for VirtReg now. The physical
431/// register must not be used for anything else when this is called.
432///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000433void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
434 DEBUG(dbgs() << "Assigning %reg" << LRE.first << " to "
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000435 << TRI->getName(PhysReg) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000436 PhysRegState[PhysReg] = LRE.first;
437 assert(!LRE.second.PhysReg && "Already assigned a physreg");
438 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000439}
440
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000441/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000442void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000443 const unsigned VirtReg = LRE.first;
444
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000445 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
446 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000447
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000448 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000449
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000450 // Ignore invalid hints.
451 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000452 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000453 Hint = 0;
454
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000455 // Take hint when possible.
456 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000457 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000458 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000459 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000460 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000461 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000462 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000463 case spillImpossible:
464 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000465 }
466 }
467
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000468 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
469 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
470
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000471 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000472 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
473 unsigned PhysReg = *I;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000474 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
475 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000476 }
477
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000478 DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName()
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000479 << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000480
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000481 unsigned BestReg = 0, BestCost = spillImpossible;
482 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
483 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000484 // Cost is 0 when all aliases are already disabled.
485 if (Cost == 0)
486 return assignVirtToPhysReg(LRE, *I);
487 if (Cost < BestCost)
488 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000489 }
490
491 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000492 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000493 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000494 }
495
496 // Nothing we can do.
497 std::string msg;
498 raw_string_ostream Msg(msg);
499 Msg << "Ran out of registers during register allocation!";
500 if (MI->isInlineAsm()) {
501 Msg << "\nPlease check your inline asm statement for "
502 << "invalid constraints:\n";
503 MI->print(Msg, TM);
504 }
505 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000506}
507
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000508/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000509RAFast::LiveRegMap::iterator
510RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
511 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000512 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
513 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000514 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000515 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000516 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
517 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000518 if (New) {
519 // If there is no hint, peek at the only use of this register.
520 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
521 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000522 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000523 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000524 if (UseMI.isCopyLike())
525 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000526 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000527 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000528 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000529 // Redefining a live register - kill at the last use, unless it is this
530 // instruction defining VirtReg multiple times.
531 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
532 addKillFlag(LR);
533 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000534 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000535 LR.LastUse = MI;
536 LR.LastOpNum = OpNum;
537 LR.Dirty = true;
538 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000539 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000540}
541
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000542/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000543RAFast::LiveRegMap::iterator
544RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
545 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000546 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
547 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000548 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000549 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000550 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
551 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000552 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000553 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000554 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000555 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000556 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000557 DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into "
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000558 << TRI->getName(LR.PhysReg) << "\n");
559 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000560 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000561 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000562 if (isLastUseOfLocalReg(MO)) {
563 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000564 if (MO.isUse())
565 MO.setIsKill();
566 else
567 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000568 } else if (MO.isKill()) {
569 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
570 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000571 } else if (MO.isDead()) {
572 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
573 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000574 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000575 } else if (MO.isKill()) {
576 // We must remove kill flags from uses of reloaded registers because the
577 // register would be killed immediately, and there might be a second use:
578 // %foo = OR %x<kill>, %x
579 // This would cause a second reload of %x into a different register.
580 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
581 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000582 } else if (MO.isDead()) {
583 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
584 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000585 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000586 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000587 LR.LastUse = MI;
588 LR.LastOpNum = OpNum;
589 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000590 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000591}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000592
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000593// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
594// subregs. This may invalidate any operand pointers.
595// Return true if the operand kills its register.
596bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
597 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000598 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000599 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000600 return MO.isKill() || MO.isDead();
601 }
602
603 // Handle subregister index.
604 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
605 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000606
607 // A kill flag implies killing the full register. Add corresponding super
608 // register kill.
609 if (MO.isKill()) {
610 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000611 return true;
612 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000613 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000614}
615
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000616// Handle special instruction operand like early clobbers and tied ops when
617// there are additional physreg defines.
618void RAFast::handleThroughOperands(MachineInstr *MI,
619 SmallVectorImpl<unsigned> &VirtDead) {
620 DEBUG(dbgs() << "Scanning for through registers:");
621 SmallSet<unsigned, 8> ThroughRegs;
622 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
623 MachineOperand &MO = MI->getOperand(i);
624 if (!MO.isReg()) continue;
625 unsigned Reg = MO.getReg();
626 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000627 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
628 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000629 if (ThroughRegs.insert(Reg))
630 DEBUG(dbgs() << " %reg" << Reg);
631 }
632 }
633
634 // If any physreg defines collide with preallocated through registers,
635 // we must spill and reallocate.
636 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
637 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
638 MachineOperand &MO = MI->getOperand(i);
639 if (!MO.isReg() || !MO.isDef()) continue;
640 unsigned Reg = MO.getReg();
641 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
642 UsedInInstr.set(Reg);
643 if (ThroughRegs.count(PhysRegState[Reg]))
644 definePhysReg(MI, Reg, regFree);
645 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
646 UsedInInstr.set(*AS);
647 if (ThroughRegs.count(PhysRegState[*AS]))
648 definePhysReg(MI, *AS, regFree);
649 }
650 }
651
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000652 SmallVector<unsigned, 8> PartialDefs;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000653 DEBUG(dbgs() << "Allocating tied uses and early clobbers.\n");
654 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
655 MachineOperand &MO = MI->getOperand(i);
656 if (!MO.isReg()) continue;
657 unsigned Reg = MO.getReg();
658 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
659 if (MO.isUse()) {
660 unsigned DefIdx = 0;
661 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
662 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
663 << DefIdx << ".\n");
664 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
665 unsigned PhysReg = LRI->second.PhysReg;
666 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000667 // Note: we don't update the def operand yet. That would cause the normal
668 // def-scan to attempt spilling.
669 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
670 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
671 // Reload the register, but don't assign to the operand just yet.
672 // That would confuse the later phys-def processing pass.
673 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
674 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000675 } else if (MO.isEarlyClobber()) {
676 // Note: defineVirtReg may invalidate MO.
677 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
678 unsigned PhysReg = LRI->second.PhysReg;
679 if (setPhysReg(MI, i, PhysReg))
680 VirtDead.push_back(Reg);
681 }
682 }
683
684 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
685 UsedInInstr.reset();
686 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
687 MachineOperand &MO = MI->getOperand(i);
688 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
689 unsigned Reg = MO.getReg();
690 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
691 UsedInInstr.set(Reg);
692 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
693 UsedInInstr.set(*AS);
694 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000695
696 // Also mark PartialDefs as used to avoid reallocation.
697 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
698 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000699}
700
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000701void RAFast::AllocateBasicBlock() {
702 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000703
704 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000705 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000706
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000707 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000708
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000709 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000710 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
711 E = MBB->livein_end(); I != E; ++I)
712 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000713
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000714 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000715 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000716
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000717 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000718 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000719 MachineInstr *MI = MII++;
720 const TargetInstrDesc &TID = MI->getDesc();
721 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000722 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000723 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
724 if (PhysRegState[Reg] == regDisabled) continue;
725 dbgs() << " " << TRI->getName(Reg);
726 switch(PhysRegState[Reg]) {
727 case regFree:
728 break;
729 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000730 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000731 break;
732 default:
733 dbgs() << "=%reg" << PhysRegState[Reg];
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000734 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000735 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000736 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000737 "Bad inverse map");
738 break;
739 }
740 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000741 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000742 // Check that LiveVirtRegs is the inverse.
743 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
744 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000745 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
746 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000747 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000748 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000749 assert(PhysRegState[i->second.PhysReg] == i->first &&
750 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000751 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000752 });
753
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000754 // Debug values are not allowed to change codegen in any way.
755 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000756 bool ScanDbgValue = true;
757 while (ScanDbgValue) {
758 ScanDbgValue = false;
759 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
760 MachineOperand &MO = MI->getOperand(i);
761 if (!MO.isReg()) continue;
762 unsigned Reg = MO.getReg();
763 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
764 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
765 if (LRI != LiveVirtRegs.end())
766 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000767 else {
Devang Patel58b81762010-07-19 23:25:39 +0000768 int SS = StackSlotForVirtReg[Reg];
769 if (SS == -1)
Devang Patel7a029b62010-07-09 21:48:31 +0000770 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel58b81762010-07-19 23:25:39 +0000771 else {
772 // Modify DBG_VALUE now that the value is in a spill slot.
773 uint64_t Offset = MI->getOperand(1).getImm();
774 const MDNode *MDPtr =
775 MI->getOperand(MI->getNumOperands()-1).getMetadata();
776 DebugLoc DL = MI->getDebugLoc();
777 if (MachineInstr *NewDV =
778 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
779 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
780 MachineBasicBlock *MBB = MI->getParent();
781 MBB->insert(MBB->erase(MI), NewDV);
782 // Scan NewDV operands from the beginning.
783 MI = NewDV;
784 ScanDbgValue = true;
785 break;
786 } else
787 MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry!
788 }
Devang Patel7a029b62010-07-09 21:48:31 +0000789 }
790 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000791 }
792 // Next instruction.
793 continue;
794 }
795
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000796 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000797 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000798 if (MI->isCopy()) {
799 CopyDst = MI->getOperand(0).getReg();
800 CopySrc = MI->getOperand(1).getReg();
801 CopyDstSub = MI->getOperand(0).getSubReg();
802 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000803 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000804
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000805 // Track registers used by instruction.
806 UsedInInstr.reset();
807
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000808 // First scan.
809 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000810 // Find the end of the virtreg operands
811 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000812 bool hasTiedOps = false;
813 bool hasEarlyClobbers = false;
814 bool hasPartialRedefs = false;
815 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000816 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
817 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000818 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000819 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000820 if (!Reg) continue;
821 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
822 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000823 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000824 hasTiedOps = hasTiedOps ||
825 TID.getOperandConstraint(i, TOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000826 } else {
827 if (MO.isEarlyClobber())
828 hasEarlyClobbers = true;
829 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
830 hasPartialRedefs = true;
831 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000832 continue;
833 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000834 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000835 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000836 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000837 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000838 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
839 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000840 hasEarlyClobbers = true;
841 } else
842 hasPhysDefs = true;
843 }
844
845 // The instruction may have virtual register operands that must be allocated
846 // the same register at use-time and def-time: early clobbers and tied
847 // operands. If there are also physical defs, these registers must avoid
848 // both physical defs and uses, making them more constrained than normal
849 // operands.
850 // We didn't detect inline asm tied operands above, so just make this extra
851 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000852 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
853 (hasTiedOps && hasPhysDefs)) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000854 handleThroughOperands(MI, VirtDead);
855 // Don't attempt coalescing when we have funny stuff going on.
856 CopyDst = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000857 }
858
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000859 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000860 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000861 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000862 MachineOperand &MO = MI->getOperand(i);
863 if (!MO.isReg()) continue;
864 unsigned Reg = MO.getReg();
865 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
866 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000867 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
868 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000869 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000870 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000871 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000872 }
873 }
874
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000875 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000876
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000877 // Track registers defined by instruction - early clobbers at this point.
878 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000879 if (hasEarlyClobbers) {
880 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
881 MachineOperand &MO = MI->getOperand(i);
882 if (!MO.isReg() || !MO.isDef()) continue;
883 unsigned Reg = MO.getReg();
884 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
885 UsedInInstr.set(Reg);
886 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
887 UsedInInstr.set(*AS);
888 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000889 }
890
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000891 unsigned DefOpEnd = MI->getNumOperands();
892 if (TID.isCall()) {
893 // Spill all virtregs before a call. This serves two purposes: 1. If an
894 // exception is thrown, the landing pad is going to expect to find registers
895 // in their spill slots, and 2. we don't have to wade through all the
896 // <imp-def> operands on the call instruction.
897 DefOpEnd = VirtOpEnd;
898 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
899 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000900
901 // The imp-defs are skipped below, but we still need to mark those
902 // registers as used by the function.
903 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000904 }
905
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000906 // Third scan.
907 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000908 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000909 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000910 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
911 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000912 unsigned Reg = MO.getReg();
913
914 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000915 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000916 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
917 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000918 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000919 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000920 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
921 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000922 if (setPhysReg(MI, i, PhysReg)) {
923 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000924 CopyDst = 0; // cancel coalescing;
925 } else
926 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000927 }
928
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000929 // Kill dead defs after the scan to ensure that multiple defs of the same
930 // register are allocated identically. We didn't need to do this for uses
931 // because we are crerating our own kill flags, and they are always at the
932 // last use.
933 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
934 killVirtReg(VirtDead[i]);
935 VirtDead.clear();
936
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000937 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000938
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000939 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
940 DEBUG(dbgs() << "-- coalescing: " << *MI);
941 Coalesced.push_back(MI);
942 } else {
943 DEBUG(dbgs() << "<< " << *MI);
944 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000945 }
946
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000947 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000948 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
949 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000950
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000951 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000952 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000953 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000954 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +0000955 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000956
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000957 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000958}
959
960/// runOnMachineFunction - Register allocate the whole function
961///
962bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000963 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
964 << "********** Function: "
965 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000966 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000967 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000968 TM = &Fn.getTarget();
969 TRI = TM->getRegisterInfo();
970 TII = TM->getInstrInfo();
971
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000972 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000973 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000974
975 // initialize the virtual->physical register map to have a 'null'
976 // mapping for all virtual registers
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000977 unsigned LastVirtReg = MRI->getLastVirtReg();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000978 StackSlotForVirtReg.grow(LastVirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000979
980 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000981 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
982 MBBi != MBBe; ++MBBi) {
983 MBB = &*MBBi;
984 AllocateBasicBlock();
985 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000986
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000987 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000988 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000989
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000990 // Add the clobber lists for all the instructions we skipped earlier.
991 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
992 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
993 if (const unsigned *Defs = (*I)->getImplicitDefs())
994 while (*Defs)
995 MRI->setPhysRegUsed(*Defs++);
996
997 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000998 StackSlotForVirtReg.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000999 return true;
1000}
1001
1002FunctionPass *llvm::createFastRegisterAllocator() {
1003 return new RAFast();
1004}