blob: dd5baab8d86778ae1acc090bd196884df821e09d [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"
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000016#include "RegisterClassInfo.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000017#include "llvm/BasicBlock.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
Devang Patel459a36b2010-08-04 18:42:02 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/Passes.h"
24#include "llvm/CodeGen/RegAllocRegistry.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/ADT/DenseMap.h"
32#include "llvm/ADT/IndexedMap.h"
33#include "llvm/ADT/SmallSet.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/Statistic.h"
36#include "llvm/ADT/STLExtras.h"
37#include <algorithm>
38using namespace llvm;
39
40STATISTIC(NumStores, "Number of stores added");
41STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000042STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000043
44static RegisterRegAlloc
45 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
46
47namespace {
48 class RAFast : public MachineFunctionPass {
49 public:
50 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000051 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Owen Anderson081c34b2010-10-19 17:21:58 +000052 isBulkSpilling(false) {
53 initializePHIEliminationPass(*PassRegistry::getPassRegistry());
54 initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry());
55 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000056 private:
57 const TargetMachine *TM;
58 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000059 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000060 const TargetRegisterInfo *TRI;
61 const TargetInstrInfo *TII;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000062 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000063
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000064 // Basic block currently being allocated.
65 MachineBasicBlock *MBB;
66
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000067 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
68 // values are spilled.
69 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
70
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000071 // Everything we know about a live virtual register.
72 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000073 MachineInstr *LastUse; // Last instr to use reg.
74 unsigned PhysReg; // Currently held here.
75 unsigned short LastOpNum; // OpNum on LastUse.
76 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000077
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000078 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000079 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000080 };
81
82 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000083 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000084
85 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000086 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000087 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000088
Devang Patel72d9b0e2011-06-21 22:36:03 +000089 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Patel459a36b2010-08-04 18:42:02 +000090
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000091 // RegState - Track the state of a physical register.
92 enum RegState {
93 // A disabled register is not available for allocation, but an alias may
94 // be in use. A register can only be moved out of the disabled state if
95 // all aliases are disabled.
96 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000097
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000098 // A free register is not currently in use and can be allocated
99 // immediately without checking aliases.
100 regFree,
101
Evan Chengd8a16242011-04-22 01:40:20 +0000102 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000103 // call parameter), and it remains reserved until it is used.
104 regReserved
105
106 // A register state may also be a virtual register number, indication that
107 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000108 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000109 };
110
111 // PhysRegState - One of the RegState enums, or a virtreg.
112 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000113
114 // UsedInInstr - BitVector of physregs that are used in the current
115 // instruction, and so cannot be allocated.
116 BitVector UsedInInstr;
117
Jim Grosbach07cb6892010-09-01 19:16:29 +0000118 // SkippedInstrs - Descriptors of instructions whose clobber list was
119 // ignored because all registers were spilled. It is still necessary to
120 // mark all the clobbered registers as used by the function.
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000121 SmallPtrSet<const TargetInstrDesc*, 4> SkippedInstrs;
122
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000123 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
124 // completely after spilling all live registers. LiveRegMap entries should
125 // not be erased.
126 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000127
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000128 enum {
129 spillClean = 1,
130 spillDirty = 100,
131 spillImpossible = ~0u
132 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000133 public:
134 virtual const char *getPassName() const {
135 return "Fast Register Allocator";
136 }
137
138 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
139 AU.setPreservesCFG();
140 AU.addRequiredID(PHIEliminationID);
141 AU.addRequiredID(TwoAddressInstructionPassID);
142 MachineFunctionPass::getAnalysisUsage(AU);
143 }
144
145 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000146 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000147 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000148 void handleThroughOperands(MachineInstr *MI,
149 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000150 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000151 bool isLastUseOfLocalReg(MachineOperand&);
152
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000153 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000154 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000155 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000156 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000157 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000158
159 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000160 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000161 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000162 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
163 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000164 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
165 unsigned VirtReg, unsigned Hint);
166 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
167 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000168 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000169 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000170 };
171 char RAFast::ID = 0;
172}
173
174/// getStackSpaceFor - This allocates space for the specified virtual register
175/// to be held on the stack.
176int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
177 // Find the location Reg would belong...
178 int SS = StackSlotForVirtReg[VirtReg];
179 if (SS != -1)
180 return SS; // Already has space allocated?
181
182 // Allocate a new stack object for this spill location...
183 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
184 RC->getAlignment());
185
186 // Assign the slot.
187 StackSlotForVirtReg[VirtReg] = FrameIdx;
188 return FrameIdx;
189}
190
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000191/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
192/// its virtual register, and it is guaranteed to be a block-local register.
193///
194bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
195 // Check for non-debug uses or defs following MO.
196 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000197 MachineOperand *Next = &MO;
198 while ((Next = Next->getNextOperandForReg()))
199 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000200 return false;
201
202 // If the register has ever been spilled or reloaded, we conservatively assume
203 // it is a global register used in multiple blocks.
204 if (StackSlotForVirtReg[MO.getReg()] != -1)
205 return false;
206
207 // Check that the use/def chain has exactly one operand - MO.
208 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
209}
210
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000211/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000212void RAFast::addKillFlag(const LiveReg &LR) {
213 if (!LR.LastUse) return;
214 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000215 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
216 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000217 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000218 else
219 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
220 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000221}
222
223/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000224void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
225 addKillFlag(LRI->second);
226 const LiveReg &LR = LRI->second;
227 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000228 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000229 // Erase from LiveVirtRegs unless we're spilling in bulk.
230 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000231 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000232}
233
234/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000235void RAFast::killVirtReg(unsigned VirtReg) {
236 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
237 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000238 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
239 if (LRI != LiveVirtRegs.end())
240 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000241}
242
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000243/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000244/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000245void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000246 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
247 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000248 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
249 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
250 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000251}
252
253/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000254void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000255 LiveRegMap::iterator LRI) {
256 LiveReg &LR = LRI->second;
257 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000258
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000259 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000260 // If this physreg is used by the instruction, we want to kill it on the
261 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000262 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000263 LR.Dirty = false;
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000264 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->first, TRI)
265 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000266 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
267 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000268 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000269 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000270 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000271
Jim Grosbach07cb6892010-09-01 19:16:29 +0000272 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000273 // identify spilled location as the place to find corresponding variable's
274 // value.
Devang Patel72d9b0e2011-06-21 22:36:03 +0000275 SmallVector<MachineInstr *, 4> &LRIDbgValues = LiveDbgValueMap[LRI->first];
276 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
277 MachineInstr *DBG = LRIDbgValues[li];
Jim Grosbach07cb6892010-09-01 19:16:29 +0000278 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000279 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
280 int64_t Offset = 0;
281 if (DBG->getOperand(1).isImm())
282 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000283 DebugLoc DL;
284 if (MI == MBB->end()) {
285 // If MI is at basic block end then use last instruction's location.
286 MachineBasicBlock::iterator EI = MI;
287 DL = (--EI)->getDebugLoc();
288 }
289 else
290 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000291 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000292 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
293 MachineBasicBlock *MBB = DBG->getParent();
294 MBB->insert(MI, NewDV);
295 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Patel72d9b0e2011-06-21 22:36:03 +0000296 LRIDbgValues[li] = NewDV;
Devang Patel459a36b2010-08-04 18:42:02 +0000297 }
298 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000299 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000300 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000301 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000302 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000303}
304
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000305/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000306void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000307 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000308 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000309 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
310 // of spilling here is deterministic, if arbitrary.
311 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
312 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000313 spillVirtReg(MI, i);
314 LiveVirtRegs.clear();
315 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000316}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000317
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000318/// usePhysReg - Handle the direct use of a physical register.
319/// Check that the register is not used by a virtreg.
320/// Kill the physreg, marking it free.
321/// This may add implicit kills to MO->getParent() and invalidate MO.
322void RAFast::usePhysReg(MachineOperand &MO) {
323 unsigned PhysReg = MO.getReg();
324 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
325 "Bad usePhysReg operand");
326
327 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000328 case regDisabled:
329 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000330 case regReserved:
331 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000332 // Fall through
333 case regFree:
334 UsedInInstr.set(PhysReg);
335 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000336 return;
337 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000338 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000339 // wanted has been clobbered.
340 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000341 }
342
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000343 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000344 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
345 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000346 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000347 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000348 break;
349 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000350 assert(TRI->isSuperRegister(PhysReg, Alias) &&
351 "Instruction is not using a subregister of a reserved register");
352 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000353 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000354 UsedInInstr.set(Alias);
355 MO.getParent()->addRegisterKilled(Alias, TRI, true);
356 return;
357 case regFree:
358 if (TRI->isSuperRegister(PhysReg, Alias)) {
359 // Leave the superregister in the working set.
360 UsedInInstr.set(Alias);
361 MO.getParent()->addRegisterKilled(Alias, TRI, true);
362 return;
363 }
364 // Some other alias was in the working set - clear it.
365 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000366 break;
367 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000368 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000369 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000370 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000371
372 // All aliases are disabled, bring register into working set.
373 PhysRegState[PhysReg] = regFree;
374 UsedInInstr.set(PhysReg);
375 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000376}
377
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000378/// definePhysReg - Mark PhysReg as reserved or free after spilling any
379/// virtregs. This is very similar to defineVirtReg except the physreg is
380/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000381void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
382 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000383 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000384 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
385 case regDisabled:
386 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000387 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000388 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000389 // Fall through.
390 case regFree:
391 case regReserved:
392 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000393 return;
394 }
395
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000396 // This is a disabled register, disable all aliases.
397 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000398 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
399 unsigned Alias = *AS; ++AS) {
400 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 Olesen448ab3a2011-06-02 23:41:40 +0000487 !RC->contains(Hint) || !RegClassInfo.isAllocatable(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 Olesen5e5ed442011-06-13 03:26:46 +0000492 // Ignore the hint if we would have to spill a dirty register.
493 unsigned Cost = calcSpillCost(Hint);
494 if (Cost < spillDirty) {
495 if (Cost)
496 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000497 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000498 }
499 }
500
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000501 ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000502
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 // First try to find a completely free register.
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000504 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000505 unsigned PhysReg = *I;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000506 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000507 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000508 }
509
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000510 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
511 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000512
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000513 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000514 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000515 unsigned Cost = calcSpillCost(*I);
Eric Christopher0b756342011-04-12 22:17:44 +0000516 DEBUG(dbgs() << "\tRegister: " << *I << "\n");
517 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
518 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000519 // Cost is 0 when all aliases are already disabled.
520 if (Cost == 0)
521 return assignVirtToPhysReg(LRE, *I);
522 if (Cost < BestCost)
523 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000524 }
525
526 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000527 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000528 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000529 }
530
531 // Nothing we can do.
532 std::string msg;
533 raw_string_ostream Msg(msg);
534 Msg << "Ran out of registers during register allocation!";
535 if (MI->isInlineAsm()) {
536 Msg << "\nPlease check your inline asm statement for "
537 << "invalid constraints:\n";
538 MI->print(Msg, TM);
539 }
540 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000541}
542
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000543/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000544RAFast::LiveRegMap::iterator
545RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
546 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000547 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
548 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000549 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000550 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000551 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
552 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000553 if (New) {
554 // If there is no hint, peek at the only use of this register.
555 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
556 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000557 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000558 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000559 if (UseMI.isCopyLike())
560 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000561 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000562 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000563 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000564 // Redefining a live register - kill at the last use, unless it is this
565 // instruction defining VirtReg multiple times.
566 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
567 addKillFlag(LR);
568 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000569 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000570 LR.LastUse = MI;
571 LR.LastOpNum = OpNum;
572 LR.Dirty = true;
573 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000574 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000575}
576
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000577/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000578RAFast::LiveRegMap::iterator
579RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
580 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000581 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
582 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000583 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000584 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000585 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
586 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000587 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000588 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000589 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000590 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000591 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000592 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
593 << PrintReg(LR.PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000594 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000595 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000596 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000597 if (isLastUseOfLocalReg(MO)) {
598 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000599 if (MO.isUse())
600 MO.setIsKill();
601 else
602 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000603 } else if (MO.isKill()) {
604 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
605 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000606 } else if (MO.isDead()) {
607 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
608 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000609 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000610 } else if (MO.isKill()) {
611 // We must remove kill flags from uses of reloaded registers because the
612 // register would be killed immediately, and there might be a second use:
613 // %foo = OR %x<kill>, %x
614 // This would cause a second reload of %x into a different register.
615 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
616 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000617 } else if (MO.isDead()) {
618 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
619 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000621 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000622 LR.LastUse = MI;
623 LR.LastOpNum = OpNum;
624 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000625 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000626}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000627
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000628// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
629// subregs. This may invalidate any operand pointers.
630// Return true if the operand kills its register.
631bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
632 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000633 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000634 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000635 return MO.isKill() || MO.isDead();
636 }
637
638 // Handle subregister index.
639 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
640 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000641
642 // A kill flag implies killing the full register. Add corresponding super
643 // register kill.
644 if (MO.isKill()) {
645 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000646 return true;
647 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000648 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000649}
650
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000651// Handle special instruction operand like early clobbers and tied ops when
652// there are additional physreg defines.
653void RAFast::handleThroughOperands(MachineInstr *MI,
654 SmallVectorImpl<unsigned> &VirtDead) {
655 DEBUG(dbgs() << "Scanning for through registers:");
656 SmallSet<unsigned, 8> ThroughRegs;
657 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
658 MachineOperand &MO = MI->getOperand(i);
659 if (!MO.isReg()) continue;
660 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000661 if (!TargetRegisterInfo::isVirtualRegister(Reg))
662 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000663 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
664 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000665 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000666 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000667 }
668 }
669
670 // If any physreg defines collide with preallocated through registers,
671 // we must spill and reallocate.
672 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
673 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
674 MachineOperand &MO = MI->getOperand(i);
675 if (!MO.isReg() || !MO.isDef()) continue;
676 unsigned Reg = MO.getReg();
677 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
678 UsedInInstr.set(Reg);
679 if (ThroughRegs.count(PhysRegState[Reg]))
680 definePhysReg(MI, Reg, regFree);
681 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
682 UsedInInstr.set(*AS);
683 if (ThroughRegs.count(PhysRegState[*AS]))
684 definePhysReg(MI, *AS, regFree);
685 }
686 }
687
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000688 SmallVector<unsigned, 8> PartialDefs;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000689 DEBUG(dbgs() << "Allocating tied uses and early clobbers.\n");
690 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
691 MachineOperand &MO = MI->getOperand(i);
692 if (!MO.isReg()) continue;
693 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000694 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000695 if (MO.isUse()) {
696 unsigned DefIdx = 0;
697 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
698 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
699 << DefIdx << ".\n");
700 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
701 unsigned PhysReg = LRI->second.PhysReg;
702 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000703 // Note: we don't update the def operand yet. That would cause the normal
704 // def-scan to attempt spilling.
705 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
706 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
707 // Reload the register, but don't assign to the operand just yet.
708 // That would confuse the later phys-def processing pass.
709 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
710 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000711 } else if (MO.isEarlyClobber()) {
712 // Note: defineVirtReg may invalidate MO.
713 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
714 unsigned PhysReg = LRI->second.PhysReg;
715 if (setPhysReg(MI, i, PhysReg))
716 VirtDead.push_back(Reg);
717 }
718 }
719
720 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000721 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000722 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
723 MachineOperand &MO = MI->getOperand(i);
724 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
725 unsigned Reg = MO.getReg();
726 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Eric Christopher0b756342011-04-12 22:17:44 +0000727 DEBUG(dbgs() << "\tSetting reg " << Reg << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000728 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000729 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000730
731 // Also mark PartialDefs as used to avoid reallocation.
732 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
733 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000734}
735
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000736void RAFast::AllocateBasicBlock() {
737 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000738
Nick Lewyckyc57ef562011-02-04 22:44:08 +0000739 // FIXME: This should probably be added by instruction selection instead?
740 // If the last instruction in the block is a return, make sure to mark it as
741 // using all of the live-out values in the function. Things marked both call
742 // and return are tail calls; do not do this for them. The tail callee need
743 // not take the same registers as input that it produces as output, and there
744 // are dependencies for its input registers elsewhere.
745 if (!MBB->empty() && MBB->back().getDesc().isReturn() &&
746 !MBB->back().getDesc().isCall()) {
747 MachineInstr *Ret = &MBB->back();
748
749 for (MachineRegisterInfo::liveout_iterator
750 I = MF->getRegInfo().liveout_begin(),
751 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
752 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
753 "Cannot have a live-out virtual register.");
754
755 // Add live-out registers as implicit uses.
756 Ret->addRegisterKilled(*I, TRI, true);
757 }
758 }
759
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000760 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000761 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000762
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000763 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000764
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000765 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000766 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
767 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000768 if (RegClassInfo.isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000769 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000770
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000771 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000772 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000773
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000774 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000775 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000776 MachineInstr *MI = MII++;
777 const TargetInstrDesc &TID = MI->getDesc();
778 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000779 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000780 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
781 if (PhysRegState[Reg] == regDisabled) continue;
782 dbgs() << " " << TRI->getName(Reg);
783 switch(PhysRegState[Reg]) {
784 case regFree:
785 break;
786 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000787 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000788 break;
789 default:
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000790 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000791 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000792 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000793 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000794 "Bad inverse map");
795 break;
796 }
797 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000798 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000799 // Check that LiveVirtRegs is the inverse.
800 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
801 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000802 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
803 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000804 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000805 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000806 assert(PhysRegState[i->second.PhysReg] == i->first &&
807 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000808 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000809 });
810
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000811 // Debug values are not allowed to change codegen in any way.
812 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000813 bool ScanDbgValue = true;
814 while (ScanDbgValue) {
815 ScanDbgValue = false;
816 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
817 MachineOperand &MO = MI->getOperand(i);
818 if (!MO.isReg()) continue;
819 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000820 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Devang Patel72d9b0e2011-06-21 22:36:03 +0000821 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel58b81762010-07-19 23:25:39 +0000822 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
823 if (LRI != LiveVirtRegs.end())
824 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000825 else {
Devang Patel58b81762010-07-19 23:25:39 +0000826 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000827 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000828 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000829 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000830 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000831 }
Devang Patel58b81762010-07-19 23:25:39 +0000832 else {
833 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000834 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000835 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000836 MI->getOperand(MI->getNumOperands()-1).getMetadata();
837 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000838 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000839 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000840 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
841 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000842 MachineBasicBlock *MBB = MI->getParent();
843 MBB->insert(MBB->erase(MI), NewDV);
844 // Scan NewDV operands from the beginning.
845 MI = NewDV;
846 ScanDbgValue = true;
847 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000848 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000849 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000850 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000851 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000852 }
Devang Patel58b81762010-07-19 23:25:39 +0000853 }
Devang Patel7a029b62010-07-09 21:48:31 +0000854 }
855 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000856 }
857 // Next instruction.
858 continue;
859 }
860
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000861 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000862 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000863 if (MI->isCopy()) {
864 CopyDst = MI->getOperand(0).getReg();
865 CopySrc = MI->getOperand(1).getReg();
866 CopyDstSub = MI->getOperand(0).getSubReg();
867 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000868 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000869
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000870 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000871 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000872
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000873 // First scan.
874 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000875 // Find the end of the virtreg operands
876 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000877 bool hasTiedOps = false;
878 bool hasEarlyClobbers = false;
879 bool hasPartialRedefs = false;
880 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000881 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
882 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000883 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000884 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000885 if (!Reg) continue;
886 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
887 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000888 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000889 hasTiedOps = hasTiedOps ||
890 TID.getOperandConstraint(i, TOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000891 } else {
892 if (MO.isEarlyClobber())
893 hasEarlyClobbers = true;
894 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
895 hasPartialRedefs = true;
896 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000897 continue;
898 }
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000899 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000900 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000901 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000902 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000903 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
904 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000905 hasEarlyClobbers = true;
906 } else
907 hasPhysDefs = true;
908 }
909
910 // The instruction may have virtual register operands that must be allocated
911 // the same register at use-time and def-time: early clobbers and tied
912 // operands. If there are also physical defs, these registers must avoid
913 // both physical defs and uses, making them more constrained than normal
914 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000915 // Similarly, if there are multiple defs and tied operands, we must make
916 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000917 // We didn't detect inline asm tied operands above, so just make this extra
918 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000919 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000920 (hasTiedOps && (hasPhysDefs || TID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000921 handleThroughOperands(MI, VirtDead);
922 // Don't attempt coalescing when we have funny stuff going on.
923 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000924 // Pretend we have early clobbers so the use operands get marked below.
925 // This is not necessary for the common case of a single tied use.
926 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000927 }
928
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000929 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000930 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000931 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000932 MachineOperand &MO = MI->getOperand(i);
933 if (!MO.isReg()) continue;
934 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000935 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000936 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000937 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
938 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000939 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000940 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000941 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000942 }
943 }
944
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000945 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000946
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000947 // Track registers defined by instruction - early clobbers and tied uses at
948 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +0000949 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000950 if (hasEarlyClobbers) {
951 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
952 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000953 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000954 unsigned Reg = MO.getReg();
955 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000956 // Look for physreg defs and tied uses.
957 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000958 UsedInInstr.set(Reg);
959 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
960 UsedInInstr.set(*AS);
961 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000962 }
963
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000964 unsigned DefOpEnd = MI->getNumOperands();
965 if (TID.isCall()) {
966 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +0000967 // exception is thrown, the landing pad is going to expect to find
968 // registers in their spill slots, and 2. we don't have to wade through
969 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000970 DefOpEnd = VirtOpEnd;
971 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
972 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000973
974 // The imp-defs are skipped below, but we still need to mark those
975 // registers as used by the function.
976 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000977 }
978
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000979 // Third scan.
980 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000981 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000982 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000983 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
984 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000985 unsigned Reg = MO.getReg();
986
987 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000988 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000989 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
990 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000991 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000992 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000993 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
994 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000995 if (setPhysReg(MI, i, PhysReg)) {
996 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000997 CopyDst = 0; // cancel coalescing;
998 } else
999 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001000 }
1001
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001002 // Kill dead defs after the scan to ensure that multiple defs of the same
1003 // register are allocated identically. We didn't need to do this for uses
1004 // because we are crerating our own kill flags, and they are always at the
1005 // last use.
1006 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1007 killVirtReg(VirtDead[i]);
1008 VirtDead.clear();
1009
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001010 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001011
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001012 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1013 DEBUG(dbgs() << "-- coalescing: " << *MI);
1014 Coalesced.push_back(MI);
1015 } else {
1016 DEBUG(dbgs() << "<< " << *MI);
1017 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001018 }
1019
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001020 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001021 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1022 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001023
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001024 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001025 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001026 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001027 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001028 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001029
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001030 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001031}
1032
1033/// runOnMachineFunction - Register allocate the whole function
1034///
1035bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001036 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1037 << "********** Function: "
1038 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001039 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001040 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001041 TM = &Fn.getTarget();
1042 TRI = TM->getRegisterInfo();
1043 TII = TM->getInstrInfo();
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001044 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001045 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001046
1047 // initialize the virtual->physical register map to have a 'null'
1048 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001049 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001050
1051 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001052 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1053 MBBi != MBBe; ++MBBi) {
1054 MBB = &*MBBi;
1055 AllocateBasicBlock();
1056 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001057
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001058 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001059 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001060
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001061 // Add the clobber lists for all the instructions we skipped earlier.
1062 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
1063 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1064 if (const unsigned *Defs = (*I)->getImplicitDefs())
1065 while (*Defs)
1066 MRI->setPhysRegUsed(*Defs++);
1067
1068 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001069 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001070 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001071 return true;
1072}
1073
1074FunctionPass *llvm::createFastRegisterAllocator() {
1075 return new RAFast();
1076}