blob: 58739ff31d31302f39184f803500599416dbef44 [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 Patel459a36b2010-08-04 18:42:02 +000089 DenseMap<unsigned, MachineInstr *> LiveDbgValueMap;
90
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
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000118 // Allocatable - vector of allocatable physical registers.
119 BitVector Allocatable;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000120
Jim Grosbach07cb6892010-09-01 19:16:29 +0000121 // SkippedInstrs - Descriptors of instructions whose clobber list was
122 // ignored because all registers were spilled. It is still necessary to
123 // mark all the clobbered registers as used by the function.
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000124 SmallPtrSet<const TargetInstrDesc*, 4> SkippedInstrs;
125
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000126 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
127 // completely after spilling all live registers. LiveRegMap entries should
128 // not be erased.
129 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000130
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000131 enum {
132 spillClean = 1,
133 spillDirty = 100,
134 spillImpossible = ~0u
135 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000136 public:
137 virtual const char *getPassName() const {
138 return "Fast Register Allocator";
139 }
140
141 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
142 AU.setPreservesCFG();
143 AU.addRequiredID(PHIEliminationID);
144 AU.addRequiredID(TwoAddressInstructionPassID);
145 MachineFunctionPass::getAnalysisUsage(AU);
146 }
147
148 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000149 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000150 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000151 void handleThroughOperands(MachineInstr *MI,
152 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000153 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000154 bool isLastUseOfLocalReg(MachineOperand&);
155
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000156 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000157 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000158 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000159 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000160 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000161
162 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000163 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000164 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000165 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
166 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000167 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
168 unsigned VirtReg, unsigned Hint);
169 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
170 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000171 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000172 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000173 };
174 char RAFast::ID = 0;
175}
176
177/// getStackSpaceFor - This allocates space for the specified virtual register
178/// to be held on the stack.
179int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
180 // Find the location Reg would belong...
181 int SS = StackSlotForVirtReg[VirtReg];
182 if (SS != -1)
183 return SS; // Already has space allocated?
184
185 // Allocate a new stack object for this spill location...
186 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
187 RC->getAlignment());
188
189 // Assign the slot.
190 StackSlotForVirtReg[VirtReg] = FrameIdx;
191 return FrameIdx;
192}
193
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000194/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
195/// its virtual register, and it is guaranteed to be a block-local register.
196///
197bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
198 // Check for non-debug uses or defs following MO.
199 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000200 MachineOperand *Next = &MO;
201 while ((Next = Next->getNextOperandForReg()))
202 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000203 return false;
204
205 // If the register has ever been spilled or reloaded, we conservatively assume
206 // it is a global register used in multiple blocks.
207 if (StackSlotForVirtReg[MO.getReg()] != -1)
208 return false;
209
210 // Check that the use/def chain has exactly one operand - MO.
211 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
212}
213
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000214/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000215void RAFast::addKillFlag(const LiveReg &LR) {
216 if (!LR.LastUse) return;
217 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000218 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
219 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000220 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000221 else
222 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
223 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000224}
225
226/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000227void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
228 addKillFlag(LRI->second);
229 const LiveReg &LR = LRI->second;
230 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000231 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000232 // Erase from LiveVirtRegs unless we're spilling in bulk.
233 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000234 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000235}
236
237/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000238void RAFast::killVirtReg(unsigned VirtReg) {
239 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
240 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000241 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
242 if (LRI != LiveVirtRegs.end())
243 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000244}
245
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000246/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000247/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000248void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000249 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
250 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000251 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
252 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
253 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000254}
255
256/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000257void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000258 LiveRegMap::iterator LRI) {
259 LiveReg &LR = LRI->second;
260 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000261
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000262 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000263 // If this physreg is used by the instruction, we want to kill it on the
264 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000265 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000266 LR.Dirty = false;
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000267 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->first, TRI)
268 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000269 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
270 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000271 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000272 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000273 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000274
Jim Grosbach07cb6892010-09-01 19:16:29 +0000275 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000276 // identify spilled location as the place to find corresponding variable's
277 // value.
278 if (MachineInstr *DBG = LiveDbgValueMap.lookup(LRI->first)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000279 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000280 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
281 int64_t Offset = 0;
282 if (DBG->getOperand(1).isImm())
283 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000284 DebugLoc DL;
285 if (MI == MBB->end()) {
286 // If MI is at basic block end then use last instruction's location.
287 MachineBasicBlock::iterator EI = MI;
288 DL = (--EI)->getDebugLoc();
289 }
290 else
291 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000292 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000293 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
294 MachineBasicBlock *MBB = DBG->getParent();
295 MBB->insert(MI, NewDV);
296 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
297 LiveDbgValueMap[LRI->first] = NewDV;
298 }
299 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000300 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000301 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000302 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000303 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000304}
305
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000306/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000307void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000308 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000309 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000310 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
311 // of spilling here is deterministic, if arbitrary.
312 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
313 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000314 spillVirtReg(MI, i);
315 LiveVirtRegs.clear();
316 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000317}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000318
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000319/// usePhysReg - Handle the direct use of a physical register.
320/// Check that the register is not used by a virtreg.
321/// Kill the physreg, marking it free.
322/// This may add implicit kills to MO->getParent() and invalidate MO.
323void RAFast::usePhysReg(MachineOperand &MO) {
324 unsigned PhysReg = MO.getReg();
325 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
326 "Bad usePhysReg operand");
327
328 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000329 case regDisabled:
330 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000331 case regReserved:
332 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000333 // Fall through
334 case regFree:
335 UsedInInstr.set(PhysReg);
336 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000337 return;
338 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000339 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000340 // wanted has been clobbered.
341 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000342 }
343
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000344 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000345 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
346 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000347 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000348 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000349 break;
350 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000351 assert(TRI->isSuperRegister(PhysReg, Alias) &&
352 "Instruction is not using a subregister of a reserved register");
353 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000354 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000355 UsedInInstr.set(Alias);
356 MO.getParent()->addRegisterKilled(Alias, TRI, true);
357 return;
358 case regFree:
359 if (TRI->isSuperRegister(PhysReg, Alias)) {
360 // Leave the superregister in the working set.
361 UsedInInstr.set(Alias);
362 MO.getParent()->addRegisterKilled(Alias, TRI, true);
363 return;
364 }
365 // Some other alias was in the working set - clear it.
366 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000367 break;
368 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000369 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000370 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000371 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000372
373 // All aliases are disabled, bring register into working set.
374 PhysRegState[PhysReg] = regFree;
375 UsedInInstr.set(PhysReg);
376 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000377}
378
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379/// definePhysReg - Mark PhysReg as reserved or free after spilling any
380/// virtregs. This is very similar to defineVirtReg except the physreg is
381/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000382void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
383 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000384 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000385 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
386 case regDisabled:
387 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000388 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000389 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000390 // Fall through.
391 case regFree:
392 case regReserved:
393 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000394 return;
395 }
396
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000397 // This is a disabled register, disable all aliases.
398 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000399 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
400 unsigned Alias = *AS; ++AS) {
401 switch (unsigned VirtReg = PhysRegState[Alias]) {
402 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000403 break;
404 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000405 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000406 // Fall through.
407 case regFree:
408 case regReserved:
409 PhysRegState[Alias] = regDisabled;
410 if (TRI->isSuperRegister(PhysReg, Alias))
411 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000412 break;
413 }
414 }
415}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000416
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000417
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000418// calcSpillCost - Return the cost of spilling clearing out PhysReg and
419// aliases so it is free for allocation.
420// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
421// can be allocated directly.
422// Returns spillImpossible when PhysReg or an alias can't be spilled.
423unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000424 if (UsedInInstr.test(PhysReg)) {
425 DEBUG(dbgs() << "PhysReg: " << PhysReg << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000426 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000427 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000428 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
429 case regDisabled:
430 break;
431 case regFree:
432 return 0;
433 case regReserved:
Eric Christopher0b756342011-04-12 22:17:44 +0000434 DEBUG(dbgs() << "VirtReg: " << VirtReg << " corresponding to PhysReg: "
435 << PhysReg << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000436 return spillImpossible;
437 default:
438 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
439 }
440
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000441 // This is a disabled register, add up cost of aliases.
Eric Christopher0b756342011-04-12 22:17:44 +0000442 DEBUG(dbgs() << "\tRegister: " << PhysReg << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000443 unsigned Cost = 0;
444 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
445 unsigned Alias = *AS; ++AS) {
Eric Christopherd31df872011-04-13 00:20:59 +0000446 if (UsedInInstr.test(Alias))
447 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000448 switch (unsigned VirtReg = PhysRegState[Alias]) {
449 case regDisabled:
450 break;
451 case regFree:
452 ++Cost;
453 break;
454 case regReserved:
455 return spillImpossible;
456 default:
457 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
458 break;
459 }
460 }
461 return Cost;
462}
463
464
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000465/// assignVirtToPhysReg - This method updates local state so that we know
466/// that PhysReg is the proper container for VirtReg now. The physical
467/// register must not be used for anything else when this is called.
468///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000469void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000470 DEBUG(dbgs() << "Assigning " << PrintReg(LRE.first, TRI) << " to "
471 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000472 PhysRegState[PhysReg] = LRE.first;
473 assert(!LRE.second.PhysReg && "Already assigned a physreg");
474 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000475}
476
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000477/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000478void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000479 const unsigned VirtReg = LRE.first;
480
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000481 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
482 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000483
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000484 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000485
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000486 // Ignore invalid hints.
487 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000488 !RC->contains(Hint) || !Allocatable.test(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000489 Hint = 0;
490
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000491 // Take hint when possible.
492 if (Hint) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000493 switch(calcSpillCost(Hint)) {
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000494 default:
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000495 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000496 // Fall through.
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000497 case 0:
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000498 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000499 case spillImpossible:
500 break;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000501 }
502 }
503
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000504 ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000505
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000506 // First try to find a completely free register.
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000507 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000508 unsigned PhysReg = *I;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000509 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000510 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000511 }
512
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000513 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
514 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000515
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000516 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000517 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000518 unsigned Cost = calcSpillCost(*I);
Eric Christopher0b756342011-04-12 22:17:44 +0000519 DEBUG(dbgs() << "\tRegister: " << *I << "\n");
520 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
521 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000522 // Cost is 0 when all aliases are already disabled.
523 if (Cost == 0)
524 return assignVirtToPhysReg(LRE, *I);
525 if (Cost < BestCost)
526 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000527 }
528
529 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000530 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000531 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000532 }
533
534 // Nothing we can do.
535 std::string msg;
536 raw_string_ostream Msg(msg);
537 Msg << "Ran out of registers during register allocation!";
538 if (MI->isInlineAsm()) {
539 Msg << "\nPlease check your inline asm statement for "
540 << "invalid constraints:\n";
541 MI->print(Msg, TM);
542 }
543 report_fatal_error(Msg.str());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000544}
545
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000546/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000547RAFast::LiveRegMap::iterator
548RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
549 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000550 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
551 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000552 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000553 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000554 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
555 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000556 if (New) {
557 // If there is no hint, peek at the only use of this register.
558 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
559 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000560 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000561 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000562 if (UseMI.isCopyLike())
563 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000564 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000565 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000566 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000567 // Redefining a live register - kill at the last use, unless it is this
568 // instruction defining VirtReg multiple times.
569 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
570 addKillFlag(LR);
571 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000572 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000573 LR.LastUse = MI;
574 LR.LastOpNum = OpNum;
575 LR.Dirty = true;
576 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000577 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000578}
579
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000580/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000581RAFast::LiveRegMap::iterator
582RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
583 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000584 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
585 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000586 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000587 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000588 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
589 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000590 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000591 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000592 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000593 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000594 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000595 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
596 << PrintReg(LR.PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000597 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000598 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000599 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000600 if (isLastUseOfLocalReg(MO)) {
601 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000602 if (MO.isUse())
603 MO.setIsKill();
604 else
605 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000606 } else if (MO.isKill()) {
607 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
608 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000609 } else if (MO.isDead()) {
610 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
611 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000612 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000613 } else if (MO.isKill()) {
614 // We must remove kill flags from uses of reloaded registers because the
615 // register would be killed immediately, and there might be a second use:
616 // %foo = OR %x<kill>, %x
617 // This would cause a second reload of %x into a different register.
618 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
619 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000620 } else if (MO.isDead()) {
621 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
622 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000623 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000624 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000625 LR.LastUse = MI;
626 LR.LastOpNum = OpNum;
627 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000628 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000629}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000630
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000631// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
632// subregs. This may invalidate any operand pointers.
633// Return true if the operand kills its register.
634bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
635 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000636 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000637 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000638 return MO.isKill() || MO.isDead();
639 }
640
641 // Handle subregister index.
642 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
643 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000644
645 // A kill flag implies killing the full register. Add corresponding super
646 // register kill.
647 if (MO.isKill()) {
648 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000649 return true;
650 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000651 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000652}
653
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000654// Handle special instruction operand like early clobbers and tied ops when
655// there are additional physreg defines.
656void RAFast::handleThroughOperands(MachineInstr *MI,
657 SmallVectorImpl<unsigned> &VirtDead) {
658 DEBUG(dbgs() << "Scanning for through registers:");
659 SmallSet<unsigned, 8> ThroughRegs;
660 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
661 MachineOperand &MO = MI->getOperand(i);
662 if (!MO.isReg()) continue;
663 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000664 if (!TargetRegisterInfo::isVirtualRegister(Reg))
665 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000666 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
667 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000668 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000669 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000670 }
671 }
672
673 // If any physreg defines collide with preallocated through registers,
674 // we must spill and reallocate.
675 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
676 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
677 MachineOperand &MO = MI->getOperand(i);
678 if (!MO.isReg() || !MO.isDef()) continue;
679 unsigned Reg = MO.getReg();
680 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
681 UsedInInstr.set(Reg);
682 if (ThroughRegs.count(PhysRegState[Reg]))
683 definePhysReg(MI, Reg, regFree);
684 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
685 UsedInInstr.set(*AS);
686 if (ThroughRegs.count(PhysRegState[*AS]))
687 definePhysReg(MI, *AS, regFree);
688 }
689 }
690
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000691 SmallVector<unsigned, 8> PartialDefs;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000692 DEBUG(dbgs() << "Allocating tied uses and early clobbers.\n");
693 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
694 MachineOperand &MO = MI->getOperand(i);
695 if (!MO.isReg()) continue;
696 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000697 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000698 if (MO.isUse()) {
699 unsigned DefIdx = 0;
700 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
701 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
702 << DefIdx << ".\n");
703 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
704 unsigned PhysReg = LRI->second.PhysReg;
705 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000706 // Note: we don't update the def operand yet. That would cause the normal
707 // def-scan to attempt spilling.
708 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
709 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
710 // Reload the register, but don't assign to the operand just yet.
711 // That would confuse the later phys-def processing pass.
712 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
713 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000714 } else if (MO.isEarlyClobber()) {
715 // Note: defineVirtReg may invalidate MO.
716 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
717 unsigned PhysReg = LRI->second.PhysReg;
718 if (setPhysReg(MI, i, PhysReg))
719 VirtDead.push_back(Reg);
720 }
721 }
722
723 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000724 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000725 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
726 MachineOperand &MO = MI->getOperand(i);
727 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
728 unsigned Reg = MO.getReg();
729 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Eric Christopher0b756342011-04-12 22:17:44 +0000730 DEBUG(dbgs() << "\tSetting reg " << Reg << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000731 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000732 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000733
734 // Also mark PartialDefs as used to avoid reallocation.
735 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
736 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000737}
738
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000739void RAFast::AllocateBasicBlock() {
740 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000741
Nick Lewyckyc57ef562011-02-04 22:44:08 +0000742 // FIXME: This should probably be added by instruction selection instead?
743 // If the last instruction in the block is a return, make sure to mark it as
744 // using all of the live-out values in the function. Things marked both call
745 // and return are tail calls; do not do this for them. The tail callee need
746 // not take the same registers as input that it produces as output, and there
747 // are dependencies for its input registers elsewhere.
748 if (!MBB->empty() && MBB->back().getDesc().isReturn() &&
749 !MBB->back().getDesc().isCall()) {
750 MachineInstr *Ret = &MBB->back();
751
752 for (MachineRegisterInfo::liveout_iterator
753 I = MF->getRegInfo().liveout_begin(),
754 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
755 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
756 "Cannot have a live-out virtual register.");
757
758 // Add live-out registers as implicit uses.
759 Ret->addRegisterKilled(*I, TRI, true);
760 }
761 }
762
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000763 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000764 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000765
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000766 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000767
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000768 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000769 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
770 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000771 if (Allocatable.test(*I))
772 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000773
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000774 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000775 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000776
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000777 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000778 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000779 MachineInstr *MI = MII++;
780 const TargetInstrDesc &TID = MI->getDesc();
781 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000782 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000783 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
784 if (PhysRegState[Reg] == regDisabled) continue;
785 dbgs() << " " << TRI->getName(Reg);
786 switch(PhysRegState[Reg]) {
787 case regFree:
788 break;
789 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000790 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000791 break;
792 default:
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000793 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000794 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000795 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000796 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000797 "Bad inverse map");
798 break;
799 }
800 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000801 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000802 // Check that LiveVirtRegs is the inverse.
803 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
804 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000805 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
806 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000807 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000808 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000809 assert(PhysRegState[i->second.PhysReg] == i->first &&
810 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000811 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000812 });
813
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000814 // Debug values are not allowed to change codegen in any way.
815 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000816 bool ScanDbgValue = true;
817 while (ScanDbgValue) {
818 ScanDbgValue = false;
819 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
820 MachineOperand &MO = MI->getOperand(i);
821 if (!MO.isReg()) continue;
822 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000823 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Devang Patel459a36b2010-08-04 18:42:02 +0000824 LiveDbgValueMap[Reg] = MI;
Devang Patel58b81762010-07-19 23:25:39 +0000825 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
826 if (LRI != LiveVirtRegs.end())
827 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000828 else {
Devang Patel58b81762010-07-19 23:25:39 +0000829 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000830 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000831 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000832 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000833 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000834 }
Devang Patel58b81762010-07-19 23:25:39 +0000835 else {
836 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000837 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000838 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000839 MI->getOperand(MI->getNumOperands()-1).getMetadata();
840 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000841 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000842 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000843 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
844 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000845 MachineBasicBlock *MBB = MI->getParent();
846 MBB->insert(MBB->erase(MI), NewDV);
847 // Scan NewDV operands from the beginning.
848 MI = NewDV;
849 ScanDbgValue = true;
850 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000851 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000852 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000853 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000854 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000855 }
Devang Patel58b81762010-07-19 23:25:39 +0000856 }
Devang Patel7a029b62010-07-09 21:48:31 +0000857 }
858 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000859 }
860 // Next instruction.
861 continue;
862 }
863
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000864 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000865 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000866 if (MI->isCopy()) {
867 CopyDst = MI->getOperand(0).getReg();
868 CopySrc = MI->getOperand(1).getReg();
869 CopyDstSub = MI->getOperand(0).getSubReg();
870 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000871 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000872
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000873 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000874 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000875
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000876 // First scan.
877 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000878 // Find the end of the virtreg operands
879 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000880 bool hasTiedOps = false;
881 bool hasEarlyClobbers = false;
882 bool hasPartialRedefs = false;
883 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000884 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
885 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000886 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000887 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000888 if (!Reg) continue;
889 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
890 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000891 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000892 hasTiedOps = hasTiedOps ||
893 TID.getOperandConstraint(i, TOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000894 } else {
895 if (MO.isEarlyClobber())
896 hasEarlyClobbers = true;
897 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
898 hasPartialRedefs = true;
899 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000900 continue;
901 }
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000902 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000903 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000904 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000905 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000906 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
907 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000908 hasEarlyClobbers = true;
909 } else
910 hasPhysDefs = true;
911 }
912
913 // The instruction may have virtual register operands that must be allocated
914 // the same register at use-time and def-time: early clobbers and tied
915 // operands. If there are also physical defs, these registers must avoid
916 // both physical defs and uses, making them more constrained than normal
917 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000918 // Similarly, if there are multiple defs and tied operands, we must make
919 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000920 // We didn't detect inline asm tied operands above, so just make this extra
921 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000922 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000923 (hasTiedOps && (hasPhysDefs || TID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000924 handleThroughOperands(MI, VirtDead);
925 // Don't attempt coalescing when we have funny stuff going on.
926 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000927 // Pretend we have early clobbers so the use operands get marked below.
928 // This is not necessary for the common case of a single tied use.
929 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000930 }
931
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000932 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000933 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000934 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000935 MachineOperand &MO = MI->getOperand(i);
936 if (!MO.isReg()) continue;
937 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000938 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000939 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000940 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
941 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000942 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000943 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000944 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000945 }
946 }
947
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000948 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000949
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000950 // Track registers defined by instruction - early clobbers and tied uses at
951 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +0000952 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000953 if (hasEarlyClobbers) {
954 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
955 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000956 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000957 unsigned Reg = MO.getReg();
958 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000959 // Look for physreg defs and tied uses.
960 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000961 UsedInInstr.set(Reg);
962 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
963 UsedInInstr.set(*AS);
964 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000965 }
966
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000967 unsigned DefOpEnd = MI->getNumOperands();
968 if (TID.isCall()) {
969 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +0000970 // exception is thrown, the landing pad is going to expect to find
971 // registers in their spill slots, and 2. we don't have to wade through
972 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000973 DefOpEnd = VirtOpEnd;
974 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
975 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000976
977 // The imp-defs are skipped below, but we still need to mark those
978 // registers as used by the function.
979 SkippedInstrs.insert(&TID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000980 }
981
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000982 // Third scan.
983 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +0000984 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000985 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000986 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
987 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000988 unsigned Reg = MO.getReg();
989
990 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +0000991 if (!Allocatable.test(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000992 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
993 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000994 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000995 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000996 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
997 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000998 if (setPhysReg(MI, i, PhysReg)) {
999 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001000 CopyDst = 0; // cancel coalescing;
1001 } else
1002 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001003 }
1004
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001005 // Kill dead defs after the scan to ensure that multiple defs of the same
1006 // register are allocated identically. We didn't need to do this for uses
1007 // because we are crerating our own kill flags, and they are always at the
1008 // last use.
1009 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1010 killVirtReg(VirtDead[i]);
1011 VirtDead.clear();
1012
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001013 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001014
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001015 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1016 DEBUG(dbgs() << "-- coalescing: " << *MI);
1017 Coalesced.push_back(MI);
1018 } else {
1019 DEBUG(dbgs() << "<< " << *MI);
1020 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001021 }
1022
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001023 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001024 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1025 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001026
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001027 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001028 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001029 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001030 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001031 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001032
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001033 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001034}
1035
1036/// runOnMachineFunction - Register allocate the whole function
1037///
1038bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001039 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1040 << "********** Function: "
1041 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001042 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001043 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001044 TM = &Fn.getTarget();
1045 TRI = TM->getRegisterInfo();
1046 TII = TM->getInstrInfo();
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001047 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001048
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001049 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesenefa155f2010-05-14 22:02:56 +00001050 Allocatable = TRI->getAllocatableSet(*MF);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001051
1052 // initialize the virtual->physical register map to have a 'null'
1053 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001054 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001055
1056 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001057 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1058 MBBi != MBBe; ++MBBi) {
1059 MBB = &*MBBi;
1060 AllocateBasicBlock();
1061 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001062
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001063 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001064 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001065
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001066 // Add the clobber lists for all the instructions we skipped earlier.
1067 for (SmallPtrSet<const TargetInstrDesc*, 4>::const_iterator
1068 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1069 if (const unsigned *Defs = (*I)->getImplicitDefs())
1070 while (*Defs)
1071 MRI->setPhysRegUsed(*Defs++);
1072
1073 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001074 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001075 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001076 return true;
1077}
1078
1079FunctionPass *llvm::createFastRegisterAllocator() {
1080 return new RAFast();
1081}