blob: 7bb849ef546a7c24f999fa160a541fffdbde4a19 [file] [log] [blame]
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "llvm/BasicBlock.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
Devang Patel459a36b2010-08-04 18:42:02 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/CodeGen/RegAllocRegistry.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/IndexedMap.h"
32#include "llvm/ADT/SmallSet.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/STLExtras.h"
36#include <algorithm>
37using namespace llvm;
38
39STATISTIC(NumStores, "Number of stores added");
40STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000041STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000042
43static RegisterRegAlloc
44 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
45
46namespace {
47 class RAFast : public MachineFunctionPass {
48 public:
49 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000050 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Owen Anderson081c34b2010-10-19 17:21:58 +000051 isBulkSpilling(false) {
52 initializePHIEliminationPass(*PassRegistry::getPassRegistry());
53 initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry());
54 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000055 private:
56 const TargetMachine *TM;
57 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000058 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000059 const TargetRegisterInfo *TRI;
60 const TargetInstrInfo *TII;
61
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000062 // Basic block currently being allocated.
63 MachineBasicBlock *MBB;
64
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000065 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
66 // values are spilled.
67 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
68
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000069 // Everything we know about a live virtual register.
70 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000071 MachineInstr *LastUse; // Last instr to use reg.
72 unsigned PhysReg; // Currently held here.
73 unsigned short LastOpNum; // OpNum on LastUse.
74 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000075
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000076 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000077 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000078 };
79
80 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000081 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000082
83 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000084 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000085 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000086
Devang Patel459a36b2010-08-04 18:42:02 +000087 DenseMap<unsigned, MachineInstr *> LiveDbgValueMap;
88
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000089 // RegState - Track the state of a physical register.
90 enum RegState {
91 // A disabled register is not available for allocation, but an alias may
92 // be in use. A register can only be moved out of the disabled state if
93 // all aliases are disabled.
94 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000095
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000096 // A free register is not currently in use and can be allocated
97 // immediately without checking aliases.
98 regFree,
99
100 // A reserved register has been assigned expolicitly (e.g., setting up a
101 // call parameter), and it remains reserved until it is used.
102 regReserved
103
104 // A register state may also be a virtual register number, indication that
105 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000106 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000107 };
108
109 // PhysRegState - One of the RegState enums, or a virtreg.
110 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000111
112 // UsedInInstr - BitVector of physregs that are used in the current
113 // instruction, and so cannot be allocated.
114 BitVector UsedInInstr;
115
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000116 // Allocatable - vector of allocatable physical registers.
117 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000118
Jim Grosbach07cb6892010-09-01 19:16:29 +0000119 // SkippedInstrs - Descriptors of instructions whose clobber list was
120 // ignored because all registers were spilled. It is still necessary to
121 // mark all the clobbered registers as used by the function.
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000122 SmallPtrSet<const TargetInstrDesc*, 4> SkippedInstrs;
123
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000124 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
125 // completely after spilling all live registers. LiveRegMap entries should
126 // not be erased.
127 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000128
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000129 enum {
130 spillClean = 1,
131 spillDirty = 100,
132 spillImpossible = ~0u
133 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000134 public:
135 virtual const char *getPassName() const {
136 return "Fast Register Allocator";
137 }
138
139 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
140 AU.setPreservesCFG();
141 AU.addRequiredID(PHIEliminationID);
142 AU.addRequiredID(TwoAddressInstructionPassID);
143 MachineFunctionPass::getAnalysisUsage(AU);
144 }
145
146 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000147 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000148 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000149 void handleThroughOperands(MachineInstr *MI,
150 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000151 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000152 bool isLastUseOfLocalReg(MachineOperand&);
153
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000154 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000155 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000156 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000157 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000158 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000159
160 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000161 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000162 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000163 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
164 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000165 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
166 unsigned VirtReg, unsigned Hint);
167 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
168 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000169 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000170 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000171 };
172 char RAFast::ID = 0;
173}
174
175/// getStackSpaceFor - This allocates space for the specified virtual register
176/// to be held on the stack.
177int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
178 // Find the location Reg would belong...
179 int SS = StackSlotForVirtReg[VirtReg];
180 if (SS != -1)
181 return SS; // Already has space allocated?
182
183 // Allocate a new stack object for this spill location...
184 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
185 RC->getAlignment());
186
187 // Assign the slot.
188 StackSlotForVirtReg[VirtReg] = FrameIdx;
189 return FrameIdx;
190}
191
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000192/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
193/// its virtual register, and it is guaranteed to be a block-local register.
194///
195bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
196 // Check for non-debug uses or defs following MO.
197 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000198 MachineOperand *Next = &MO;
199 while ((Next = Next->getNextOperandForReg()))
200 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000201 return false;
202
203 // If the register has ever been spilled or reloaded, we conservatively assume
204 // it is a global register used in multiple blocks.
205 if (StackSlotForVirtReg[MO.getReg()] != -1)
206 return false;
207
208 // Check that the use/def chain has exactly one operand - MO.
209 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
210}
211
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000212/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000213void RAFast::addKillFlag(const LiveReg &LR) {
214 if (!LR.LastUse) return;
215 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000216 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
217 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000218 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000219 else
220 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
221 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000222}
223
224/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000225void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
226 addKillFlag(LRI->second);
227 const LiveReg &LR = LRI->second;
228 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000229 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000230 // Erase from LiveVirtRegs unless we're spilling in bulk.
231 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000232 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000233}
234
235/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000236void RAFast::killVirtReg(unsigned VirtReg) {
237 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
238 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000239 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
240 if (LRI != LiveVirtRegs.end())
241 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000242}
243
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000244/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000245/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000246void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000247 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
248 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000249 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
250 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
251 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000252}
253
254/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000255void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000256 LiveRegMap::iterator LRI) {
257 LiveReg &LR = LRI->second;
258 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000259
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000260 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000261 // If this physreg is used by the instruction, we want to kill it on the
262 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000263 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000264 LR.Dirty = false;
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000265 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->first, TRI)
266 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000267 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
268 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000269 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000270 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000271 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000272
Jim Grosbach07cb6892010-09-01 19:16:29 +0000273 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000274 // identify spilled location as the place to find corresponding variable's
275 // value.
276 if (MachineInstr *DBG = LiveDbgValueMap.lookup(LRI->first)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000277 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000278 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
279 int64_t Offset = 0;
280 if (DBG->getOperand(1).isImm())
281 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000282 DebugLoc DL;
283 if (MI == MBB->end()) {
284 // If MI is at basic block end then use last instruction's location.
285 MachineBasicBlock::iterator EI = MI;
286 DL = (--EI)->getDebugLoc();
287 }
288 else
289 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000290 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000291 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
292 MachineBasicBlock *MBB = DBG->getParent();
293 MBB->insert(MI, NewDV);
294 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
295 LiveDbgValueMap[LRI->first] = NewDV;
296 }
297 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000298 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000299 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000300 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000301 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000302}
303
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000304/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000305void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000306 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000307 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000308 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
309 // of spilling here is deterministic, if arbitrary.
310 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
311 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000312 spillVirtReg(MI, i);
313 LiveVirtRegs.clear();
314 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000315}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000316
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000317/// usePhysReg - Handle the direct use of a physical register.
318/// Check that the register is not used by a virtreg.
319/// Kill the physreg, marking it free.
320/// This may add implicit kills to MO->getParent() and invalidate MO.
321void RAFast::usePhysReg(MachineOperand &MO) {
322 unsigned PhysReg = MO.getReg();
323 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
324 "Bad usePhysReg operand");
325
326 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000327 case regDisabled:
328 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000329 case regReserved:
330 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000331 // Fall through
332 case regFree:
333 UsedInInstr.set(PhysReg);
334 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000335 return;
336 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000337 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000338 // wanted has been clobbered.
339 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000340 }
341
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000342 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000343 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
344 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000345 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000346 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000347 break;
348 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000349 assert(TRI->isSuperRegister(PhysReg, Alias) &&
350 "Instruction is not using a subregister of a reserved register");
351 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000352 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000353 UsedInInstr.set(Alias);
354 MO.getParent()->addRegisterKilled(Alias, TRI, true);
355 return;
356 case regFree:
357 if (TRI->isSuperRegister(PhysReg, Alias)) {
358 // Leave the superregister in the working set.
359 UsedInInstr.set(Alias);
360 MO.getParent()->addRegisterKilled(Alias, TRI, true);
361 return;
362 }
363 // Some other alias was in the working set - clear it.
364 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000365 break;
366 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000367 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000368 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000369 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000370
371 // All aliases are disabled, bring register into working set.
372 PhysRegState[PhysReg] = regFree;
373 UsedInInstr.set(PhysReg);
374 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000375}
376
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000377/// definePhysReg - Mark PhysReg as reserved or free after spilling any
378/// virtregs. This is very similar to defineVirtReg except the physreg is
379/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000380void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
381 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000382 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000383 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
384 case regDisabled:
385 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000386 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000387 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000388 // Fall through.
389 case regFree:
390 case regReserved:
391 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000392 return;
393 }
394
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000395 // This is a disabled register, disable all aliases.
396 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000397 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
398 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000399 UsedInInstr.set(Alias);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000400 switch (unsigned VirtReg = PhysRegState[Alias]) {
401 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000402 break;
403 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000404 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000405 // Fall through.
406 case regFree:
407 case regReserved:
408 PhysRegState[Alias] = regDisabled;
409 if (TRI->isSuperRegister(PhysReg, Alias))
410 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000411 break;
412 }
413 }
414}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000415
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000416
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000417// calcSpillCost - Return the cost of spilling clearing out PhysReg and
418// aliases so it is free for allocation.
419// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
420// can be allocated directly.
421// Returns spillImpossible when PhysReg or an alias can't be spilled.
422unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000423 if (UsedInInstr.test(PhysReg)) {
424 DEBUG(dbgs() << "PhysReg: " << PhysReg << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000425 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000426 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000427 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
428 case regDisabled:
429 break;
430 case regFree:
431 return 0;
432 case regReserved:
Eric Christopher0b756342011-04-12 22:17:44 +0000433 DEBUG(dbgs() << "VirtReg: " << VirtReg << " corresponding to PhysReg: "
434 << PhysReg << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000435 return spillImpossible;
436 default:
437 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
438 }
439
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000440 // This is a disabled register, add up cost of aliases.
Eric Christopher0b756342011-04-12 22:17:44 +0000441 DEBUG(dbgs() << "\tRegister: " << PhysReg << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000442 unsigned Cost = 0;
443 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
444 unsigned Alias = *AS; ++AS) {
Eric Christopherd31df872011-04-13 00:20:59 +0000445 if (UsedInInstr.test(Alias))
446 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000447 switch (unsigned VirtReg = PhysRegState[Alias]) {
448 case regDisabled:
449 break;
450 case regFree:
451 ++Cost;
452 break;
453 case regReserved:
454 return spillImpossible;
455 default:
456 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
457 break;
458 }
459 }
460 return Cost;
461}
462
463
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000464/// assignVirtToPhysReg - This method updates local state so that we know
465/// that PhysReg is the proper container for VirtReg now. The physical
466/// register must not be used for anything else when this is called.
467///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000468void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000469 DEBUG(dbgs() << "Assigning " << PrintReg(LRE.first, TRI) << " to "
470 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000471 PhysRegState[PhysReg] = LRE.first;
472 assert(!LRE.second.PhysReg && "Already assigned a physreg");
473 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000474}
475
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000476/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000477void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000478 const unsigned VirtReg = LRE.first;
479
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000480 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
481 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000482
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000483 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000484
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000485 // Ignore invalid hints.
486 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000487 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000488 Hint = 0;
489
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000490 // Take hint when possible.
491 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000492 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000493 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000494 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000495 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000496 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000497 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000498 case spillImpossible:
499 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000500 }
501 }
502
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000503 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
504 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
505
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000506 // First try to find a completely free register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000507 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
508 unsigned PhysReg = *I;
Jim Grosbachee726512010-09-03 21:45:15 +0000509 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg) &&
510 Allocatable.test(PhysReg))
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000511 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000512 }
513
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000514 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
515 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000516
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000517 unsigned BestReg = 0, BestCost = spillImpossible;
518 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
Eric Christopher0b756342011-04-12 22:17:44 +0000519 if (!Allocatable.test(*I)) {
520 DEBUG(dbgs() << "\tRegister " << *I << " is not allocatable.\n");
Jim Grosbachee726512010-09-03 21:45:15 +0000521 continue;
Eric Christopher0b756342011-04-12 22:17:44 +0000522 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000523 unsigned Cost = calcSpillCost(*I);
Eric Christopher0b756342011-04-12 22:17:44 +0000524 DEBUG(dbgs() << "\tRegister: " << *I << "\n");
525 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
526 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000527 // Cost is 0 when all aliases are already disabled.
528 if (Cost == 0)
529 return assignVirtToPhysReg(LRE, *I);
530 if (Cost < BestCost)
531 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000532 }
533
534 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000535 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000536 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000537 }
538
539 // Nothing we can do.
540 std::string msg;
541 raw_string_ostream Msg(msg);
542 Msg << "Ran out of registers during register allocation!";
543 if (MI->isInlineAsm()) {
544 Msg << "\nPlease check your inline asm statement for "
545 << "invalid constraints:\n";
546 MI->print(Msg, TM);
547 }
548 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000549}
550
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000551/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000552RAFast::LiveRegMap::iterator
553RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
554 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000555 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
556 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000557 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000558 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000559 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
560 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000561 if (New) {
562 // If there is no hint, peek at the only use of this register.
563 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
564 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000565 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000566 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000567 if (UseMI.isCopyLike())
568 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000569 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000570 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000571 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000572 // Redefining a live register - kill at the last use, unless it is this
573 // instruction defining VirtReg multiple times.
574 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
575 addKillFlag(LR);
576 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000577 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000578 LR.LastUse = MI;
579 LR.LastOpNum = OpNum;
580 LR.Dirty = true;
581 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000582 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000583}
584
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000585/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000586RAFast::LiveRegMap::iterator
587RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
588 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000589 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
590 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000591 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000592 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000593 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
594 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000595 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000596 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000597 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000598 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000599 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000600 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
601 << PrintReg(LR.PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000602 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000603 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000604 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000605 if (isLastUseOfLocalReg(MO)) {
606 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000607 if (MO.isUse())
608 MO.setIsKill();
609 else
610 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000611 } else if (MO.isKill()) {
612 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
613 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000614 } else if (MO.isDead()) {
615 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
616 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000617 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000618 } else if (MO.isKill()) {
619 // We must remove kill flags from uses of reloaded registers because the
620 // register would be killed immediately, and there might be a second use:
621 // %foo = OR %x<kill>, %x
622 // This would cause a second reload of %x into a different register.
623 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
624 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000625 } else if (MO.isDead()) {
626 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
627 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000628 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000629 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000630 LR.LastUse = MI;
631 LR.LastOpNum = OpNum;
632 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000633 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000634}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000635
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000636// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
637// subregs. This may invalidate any operand pointers.
638// Return true if the operand kills its register.
639bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
640 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000641 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000642 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000643 return MO.isKill() || MO.isDead();
644 }
645
646 // Handle subregister index.
647 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
648 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000649
650 // A kill flag implies killing the full register. Add corresponding super
651 // register kill.
652 if (MO.isKill()) {
653 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000654 return true;
655 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000656 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000657}
658
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000659// Handle special instruction operand like early clobbers and tied ops when
660// there are additional physreg defines.
661void RAFast::handleThroughOperands(MachineInstr *MI,
662 SmallVectorImpl<unsigned> &VirtDead) {
663 DEBUG(dbgs() << "Scanning for through registers:");
664 SmallSet<unsigned, 8> ThroughRegs;
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();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000669 if (!TargetRegisterInfo::isVirtualRegister(Reg))
670 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000671 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
672 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000673 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000674 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000675 }
676 }
677
678 // If any physreg defines collide with preallocated through registers,
679 // we must spill and reallocate.
680 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
681 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
682 MachineOperand &MO = MI->getOperand(i);
683 if (!MO.isReg() || !MO.isDef()) continue;
684 unsigned Reg = MO.getReg();
685 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
686 UsedInInstr.set(Reg);
687 if (ThroughRegs.count(PhysRegState[Reg]))
688 definePhysReg(MI, Reg, regFree);
689 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
690 UsedInInstr.set(*AS);
691 if (ThroughRegs.count(PhysRegState[*AS]))
692 definePhysReg(MI, *AS, regFree);
693 }
694 }
695
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000696 SmallVector<unsigned, 8> PartialDefs;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000697 DEBUG(dbgs() << "Allocating tied uses and early clobbers.\n");
698 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
699 MachineOperand &MO = MI->getOperand(i);
700 if (!MO.isReg()) continue;
701 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000702 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000703 if (MO.isUse()) {
704 unsigned DefIdx = 0;
705 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
706 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
707 << DefIdx << ".\n");
708 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
709 unsigned PhysReg = LRI->second.PhysReg;
710 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000711 // Note: we don't update the def operand yet. That would cause the normal
712 // def-scan to attempt spilling.
713 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
714 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
715 // Reload the register, but don't assign to the operand just yet.
716 // That would confuse the later phys-def processing pass.
717 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
718 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000719 } else if (MO.isEarlyClobber()) {
720 // Note: defineVirtReg may invalidate MO.
721 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
722 unsigned PhysReg = LRI->second.PhysReg;
723 if (setPhysReg(MI, i, PhysReg))
724 VirtDead.push_back(Reg);
725 }
726 }
727
728 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000729 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000730 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
731 MachineOperand &MO = MI->getOperand(i);
732 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
733 unsigned Reg = MO.getReg();
734 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Eric Christopher0b756342011-04-12 22:17:44 +0000735 DEBUG(dbgs() << "\tSetting reg " << Reg << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000736 UsedInInstr.set(Reg);
Eric Christopher0b756342011-04-12 22:17:44 +0000737 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
738 DEBUG(dbgs() << "\tSetting alias reg " << *AS << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000739 UsedInInstr.set(*AS);
Eric Christopher0b756342011-04-12 22:17:44 +0000740 }
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000741 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000742
743 // Also mark PartialDefs as used to avoid reallocation.
744 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
745 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000746}
747
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000748void RAFast::AllocateBasicBlock() {
749 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000750
Nick Lewyckyc57ef562011-02-04 22:44:08 +0000751 // FIXME: This should probably be added by instruction selection instead?
752 // If the last instruction in the block is a return, make sure to mark it as
753 // using all of the live-out values in the function. Things marked both call
754 // and return are tail calls; do not do this for them. The tail callee need
755 // not take the same registers as input that it produces as output, and there
756 // are dependencies for its input registers elsewhere.
757 if (!MBB->empty() && MBB->back().getDesc().isReturn() &&
758 !MBB->back().getDesc().isCall()) {
759 MachineInstr *Ret = &MBB->back();
760
761 for (MachineRegisterInfo::liveout_iterator
762 I = MF->getRegInfo().liveout_begin(),
763 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
764 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
765 "Cannot have a live-out virtual register.");
766
767 // Add live-out registers as implicit uses.
768 Ret->addRegisterKilled(*I, TRI, true);
769 }
770 }
771
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000772 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000773 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000774
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000775 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000776
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000777 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000778 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
779 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000780 if (Allocatable.test(*I))
781 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000782
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000783 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000784 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000785
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000786 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000787 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000788 MachineInstr *MI = MII++;
789 const TargetInstrDesc &TID = MI->getDesc();
790 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000791 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000792 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
793 if (PhysRegState[Reg] == regDisabled) continue;
794 dbgs() << " " << TRI->getName(Reg);
795 switch(PhysRegState[Reg]) {
796 case regFree:
797 break;
798 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000799 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000800 break;
801 default:
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000802 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000803 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000804 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000805 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000806 "Bad inverse map");
807 break;
808 }
809 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000810 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000811 // Check that LiveVirtRegs is the inverse.
812 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
813 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000814 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
815 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000816 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000817 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000818 assert(PhysRegState[i->second.PhysReg] == i->first &&
819 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000820 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000821 });
822
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000823 // Debug values are not allowed to change codegen in any way.
824 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000825 bool ScanDbgValue = true;
826 while (ScanDbgValue) {
827 ScanDbgValue = false;
828 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
829 MachineOperand &MO = MI->getOperand(i);
830 if (!MO.isReg()) continue;
831 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000832 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Devang Patel459a36b2010-08-04 18:42:02 +0000833 LiveDbgValueMap[Reg] = MI;
Devang Patel58b81762010-07-19 23:25:39 +0000834 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
835 if (LRI != LiveVirtRegs.end())
836 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000837 else {
Devang Patel58b81762010-07-19 23:25:39 +0000838 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000839 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000840 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000841 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000842 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000843 }
Devang Patel58b81762010-07-19 23:25:39 +0000844 else {
845 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000846 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000847 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000848 MI->getOperand(MI->getNumOperands()-1).getMetadata();
849 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000850 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000851 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000852 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
853 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000854 MachineBasicBlock *MBB = MI->getParent();
855 MBB->insert(MBB->erase(MI), NewDV);
856 // Scan NewDV operands from the beginning.
857 MI = NewDV;
858 ScanDbgValue = true;
859 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000860 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000861 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000862 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000863 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000864 }
Devang Patel58b81762010-07-19 23:25:39 +0000865 }
Devang Patel7a029b62010-07-09 21:48:31 +0000866 }
867 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000868 }
869 // Next instruction.
870 continue;
871 }
872
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000873 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000874 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000875 if (MI->isCopy()) {
876 CopyDst = MI->getOperand(0).getReg();
877 CopySrc = MI->getOperand(1).getReg();
878 CopyDstSub = MI->getOperand(0).getSubReg();
879 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000880 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000881
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000882 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000883 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000884
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000885 // First scan.
886 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000887 // Find the end of the virtreg operands
888 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000889 bool hasTiedOps = false;
890 bool hasEarlyClobbers = false;
891 bool hasPartialRedefs = false;
892 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000893 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
894 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000895 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000896 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000897 if (!Reg) continue;
898 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
899 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000900 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000901 hasTiedOps = hasTiedOps ||
902 TID.getOperandConstraint(i, TOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000903 } else {
904 if (MO.isEarlyClobber())
905 hasEarlyClobbers = true;
906 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
907 hasPartialRedefs = true;
908 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000909 continue;
910 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000911 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000912 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000913 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000914 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000915 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
916 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000917 hasEarlyClobbers = true;
918 } else
919 hasPhysDefs = true;
920 }
921
922 // The instruction may have virtual register operands that must be allocated
923 // the same register at use-time and def-time: early clobbers and tied
924 // operands. If there are also physical defs, these registers must avoid
925 // both physical defs and uses, making them more constrained than normal
926 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000927 // Similarly, if there are multiple defs and tied operands, we must make
928 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000929 // We didn't detect inline asm tied operands above, so just make this extra
930 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000931 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000932 (hasTiedOps && (hasPhysDefs || TID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000933 handleThroughOperands(MI, VirtDead);
934 // Don't attempt coalescing when we have funny stuff going on.
935 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000936 // Pretend we have early clobbers so the use operands get marked below.
937 // This is not necessary for the common case of a single tied use.
938 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000939 }
940
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000941 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000942 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000943 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000944 MachineOperand &MO = MI->getOperand(i);
945 if (!MO.isReg()) continue;
946 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000947 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000948 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000949 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
950 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000951 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000952 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000953 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000954 }
955 }
956
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000957 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000958
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000959 // Track registers defined by instruction - early clobbers and tied uses at
960 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +0000961 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000962 if (hasEarlyClobbers) {
963 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
964 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000965 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000966 unsigned Reg = MO.getReg();
967 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000968 // Look for physreg defs and tied uses.
969 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000970 UsedInInstr.set(Reg);
971 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
972 UsedInInstr.set(*AS);
973 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000974 }
975
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000976 unsigned DefOpEnd = MI->getNumOperands();
977 if (TID.isCall()) {
978 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +0000979 // exception is thrown, the landing pad is going to expect to find
980 // registers in their spill slots, and 2. we don't have to wade through
981 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000982 DefOpEnd = VirtOpEnd;
983 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
984 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000985
986 // The imp-defs are skipped below, but we still need to mark those
987 // registers as used by the function.
988 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000989 }
990
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000991 // Third scan.
992 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000993 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000994 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000995 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
996 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000997 unsigned Reg = MO.getReg();
998
999 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +00001000 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001001 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1002 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001003 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001004 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001005 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
1006 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001007 if (setPhysReg(MI, i, PhysReg)) {
1008 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001009 CopyDst = 0; // cancel coalescing;
1010 } else
1011 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001012 }
1013
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001014 // Kill dead defs after the scan to ensure that multiple defs of the same
1015 // register are allocated identically. We didn't need to do this for uses
1016 // because we are crerating our own kill flags, and they are always at the
1017 // last use.
1018 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1019 killVirtReg(VirtDead[i]);
1020 VirtDead.clear();
1021
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001022 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001023
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001024 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1025 DEBUG(dbgs() << "-- coalescing: " << *MI);
1026 Coalesced.push_back(MI);
1027 } else {
1028 DEBUG(dbgs() << "<< " << *MI);
1029 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001030 }
1031
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001032 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001033 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1034 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001035
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001036 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001037 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001038 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001039 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001040 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001041
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001042 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001043}
1044
1045/// runOnMachineFunction - Register allocate the whole function
1046///
1047bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001048 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1049 << "********** Function: "
1050 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001051 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001052 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001053 TM = &Fn.getTarget();
1054 TRI = TM->getRegisterInfo();
1055 TII = TM->getInstrInfo();
1056
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001057 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +00001058 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001059
1060 // initialize the virtual->physical register map to have a 'null'
1061 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001062 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001063
1064 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001065 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1066 MBBi != MBBe; ++MBBi) {
1067 MBB = &*MBBi;
1068 AllocateBasicBlock();
1069 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001070
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001071 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001072 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001073
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001074 // Add the clobber lists for all the instructions we skipped earlier.
1075 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
1076 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1077 if (const unsigned *Defs = (*I)->getImplicitDefs())
1078 while (*Defs)
1079 MRI->setPhysRegUsed(*Defs++);
1080
1081 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001082 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001083 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001084 return true;
1085}
1086
1087FunctionPass *llvm::createFastRegisterAllocator() {
1088 return new RAFast();
1089}