blob: ceba05cbbd7bc3b325012b254c434c0c62cbd824 [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.
Evan Chenge837dea2011-06-28 19:10:37 +0000121 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000122
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);
Andrew Trickb3d58472012-01-31 05:55:32 +0000170 void addRetOperands(MachineBasicBlock *MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000171 };
172 char RAFast::ID = 0;
173}
174
175/// getStackSpaceFor - This allocates space for the specified virtual register
176/// to be held on the stack.
177int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
178 // Find the location Reg would belong...
179 int SS = StackSlotForVirtReg[VirtReg];
180 if (SS != -1)
181 return SS; // Already has space allocated?
182
183 // Allocate a new stack object for this spill location...
184 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
185 RC->getAlignment());
186
187 // Assign the slot.
188 StackSlotForVirtReg[VirtReg] = FrameIdx;
189 return FrameIdx;
190}
191
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000192/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
193/// its virtual register, and it is guaranteed to be a block-local register.
194///
195bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
196 // Check for non-debug uses or defs following MO.
197 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000198 MachineOperand *Next = &MO;
199 while ((Next = Next->getNextOperandForReg()))
200 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000201 return false;
202
203 // If the register has ever been spilled or reloaded, we conservatively assume
204 // it is a global register used in multiple blocks.
205 if (StackSlotForVirtReg[MO.getReg()] != -1)
206 return false;
207
208 // Check that the use/def chain has exactly one operand - MO.
209 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
210}
211
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000212/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000213void RAFast::addKillFlag(const LiveReg &LR) {
214 if (!LR.LastUse) return;
215 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000216 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
217 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000218 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000219 else
220 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
221 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000222}
223
224/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000225void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
226 addKillFlag(LRI->second);
227 const LiveReg &LR = LRI->second;
228 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000229 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000230 // Erase from LiveVirtRegs unless we're spilling in bulk.
231 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000232 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000233}
234
235/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000236void RAFast::killVirtReg(unsigned VirtReg) {
237 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
238 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000239 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
240 if (LRI != LiveVirtRegs.end())
241 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000242}
243
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000244/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000245/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000246void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000247 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
248 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000249 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
250 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
251 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000252}
253
254/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000255void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000256 LiveRegMap::iterator LRI) {
257 LiveReg &LR = LRI->second;
258 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000259
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000260 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000261 // If this physreg is used by the instruction, we want to kill it on the
262 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000263 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000264 LR.Dirty = false;
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000265 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->first, TRI)
266 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000267 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
268 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000269 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000270 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000271 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000272
Jim Grosbach07cb6892010-09-01 19:16:29 +0000273 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000274 // identify spilled location as the place to find corresponding variable's
275 // value.
Devang Patel72d9b0e2011-06-21 22:36:03 +0000276 SmallVector<MachineInstr *, 4> &LRIDbgValues = LiveDbgValueMap[LRI->first];
277 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
278 MachineInstr *DBG = LRIDbgValues[li];
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);
Devang Patel459a36b2010-08-04 18:42:02 +0000297 }
298 }
Devang Patel6f373a82011-06-21 23:02:36 +0000299 // Now this register is spilled there is should not be any DBG_VALUE pointing
300 // to this register because they are all pointing to spilled value now.
301 LRIDbgValues.clear();
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000302 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000303 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000304 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000305 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000306}
307
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000308/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000309void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000310 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000311 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000312 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
313 // of spilling here is deterministic, if arbitrary.
314 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
315 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000316 spillVirtReg(MI, i);
317 LiveVirtRegs.clear();
318 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000319}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000320
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000321/// usePhysReg - Handle the direct use of a physical register.
322/// Check that the register is not used by a virtreg.
323/// Kill the physreg, marking it free.
324/// This may add implicit kills to MO->getParent() and invalidate MO.
325void RAFast::usePhysReg(MachineOperand &MO) {
326 unsigned PhysReg = MO.getReg();
327 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
328 "Bad usePhysReg operand");
329
330 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000331 case regDisabled:
332 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000333 case regReserved:
334 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000335 // Fall through
336 case regFree:
337 UsedInInstr.set(PhysReg);
338 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000339 return;
340 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000341 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000342 // wanted has been clobbered.
343 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000344 }
345
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000346 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000347 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
348 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000349 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000350 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000351 break;
352 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000353 assert(TRI->isSuperRegister(PhysReg, Alias) &&
354 "Instruction is not using a subregister of a reserved register");
355 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000356 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000357 UsedInInstr.set(Alias);
358 MO.getParent()->addRegisterKilled(Alias, TRI, true);
359 return;
360 case regFree:
361 if (TRI->isSuperRegister(PhysReg, Alias)) {
362 // Leave the superregister in the working set.
363 UsedInInstr.set(Alias);
364 MO.getParent()->addRegisterKilled(Alias, TRI, true);
365 return;
366 }
367 // Some other alias was in the working set - clear it.
368 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000369 break;
370 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000371 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000372 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000373 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000374
375 // All aliases are disabled, bring register into working set.
376 PhysRegState[PhysReg] = regFree;
377 UsedInInstr.set(PhysReg);
378 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000379}
380
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000381/// definePhysReg - Mark PhysReg as reserved or free after spilling any
382/// virtregs. This is very similar to defineVirtReg except the physreg is
383/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000384void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
385 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000386 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000387 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
388 case regDisabled:
389 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000390 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000391 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000392 // Fall through.
393 case regFree:
394 case regReserved:
395 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000396 return;
397 }
398
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000399 // This is a disabled register, disable all aliases.
400 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000401 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
402 unsigned Alias = *AS; ++AS) {
403 switch (unsigned VirtReg = PhysRegState[Alias]) {
404 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000405 break;
406 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000407 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000408 // Fall through.
409 case regFree:
410 case regReserved:
411 PhysRegState[Alias] = regDisabled;
412 if (TRI->isSuperRegister(PhysReg, Alias))
413 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000414 break;
415 }
416 }
417}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000418
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000419
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000420// calcSpillCost - Return the cost of spilling clearing out PhysReg and
421// aliases so it is free for allocation.
422// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
423// can be allocated directly.
424// Returns spillImpossible when PhysReg or an alias can't be spilled.
425unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000426 if (UsedInInstr.test(PhysReg)) {
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000427 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000428 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000429 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000430 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
431 case regDisabled:
432 break;
433 case regFree:
434 return 0;
435 case regReserved:
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000436 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
437 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000438 return spillImpossible;
439 default:
440 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
441 }
442
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000443 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000444 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000445 unsigned Cost = 0;
446 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
447 unsigned Alias = *AS; ++AS) {
Eric Christopherd31df872011-04-13 00:20:59 +0000448 if (UsedInInstr.test(Alias))
449 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000450 switch (unsigned VirtReg = PhysRegState[Alias]) {
451 case regDisabled:
452 break;
453 case regFree:
454 ++Cost;
455 break;
456 case regReserved:
457 return spillImpossible;
458 default:
459 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
460 break;
461 }
462 }
463 return Cost;
464}
465
466
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000467/// assignVirtToPhysReg - This method updates local state so that we know
468/// that PhysReg is the proper container for VirtReg now. The physical
469/// register must not be used for anything else when this is called.
470///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000471void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000472 DEBUG(dbgs() << "Assigning " << PrintReg(LRE.first, TRI) << " to "
473 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000474 PhysRegState[PhysReg] = LRE.first;
475 assert(!LRE.second.PhysReg && "Already assigned a physreg");
476 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000477}
478
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000479/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000480void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000481 const unsigned VirtReg = LRE.first;
482
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000483 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
484 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000485
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000486 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000487
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000488 // Ignore invalid hints.
489 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000490 !RC->contains(Hint) || !RegClassInfo.isAllocatable(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000491 Hint = 0;
492
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000493 // Take hint when possible.
494 if (Hint) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000495 // Ignore the hint if we would have to spill a dirty register.
496 unsigned Cost = calcSpillCost(Hint);
497 if (Cost < spillDirty) {
498 if (Cost)
499 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000500 return assignVirtToPhysReg(LRE, Hint);
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);
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000519 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopher0b756342011-04-12 22:17:44 +0000520 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
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000534 // Nothing we can do. Report an error and keep going with a bad allocation.
535 MI->emitError("ran out of registers during register allocation");
536 definePhysReg(MI, *AO.begin(), regFree);
537 assignVirtToPhysReg(LRE, *AO.begin());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000538}
539
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000540/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000541RAFast::LiveRegMap::iterator
542RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
543 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000544 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
545 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000546 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000547 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000548 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
549 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000550 if (New) {
551 // If there is no hint, peek at the only use of this register.
552 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
553 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000554 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000555 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000556 if (UseMI.isCopyLike())
557 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000558 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000559 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000560 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000561 // Redefining a live register - kill at the last use, unless it is this
562 // instruction defining VirtReg multiple times.
563 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
564 addKillFlag(LR);
565 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000566 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000567 LR.LastUse = MI;
568 LR.LastOpNum = OpNum;
569 LR.Dirty = true;
570 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000571 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000572}
573
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000574/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000575RAFast::LiveRegMap::iterator
576RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
577 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000578 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
579 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000580 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000581 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000582 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
583 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000584 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000585 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000586 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000587 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000588 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000589 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
590 << PrintReg(LR.PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000591 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000592 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000593 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000594 if (isLastUseOfLocalReg(MO)) {
595 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000596 if (MO.isUse())
597 MO.setIsKill();
598 else
599 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000600 } else if (MO.isKill()) {
601 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
602 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000603 } else if (MO.isDead()) {
604 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
605 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000606 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000607 } else if (MO.isKill()) {
608 // We must remove kill flags from uses of reloaded registers because the
609 // register would be killed immediately, and there might be a second use:
610 // %foo = OR %x<kill>, %x
611 // This would cause a second reload of %x into a different register.
612 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
613 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000614 } else if (MO.isDead()) {
615 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
616 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000617 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000618 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000619 LR.LastUse = MI;
620 LR.LastOpNum = OpNum;
621 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000622 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000623}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000624
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000625// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
626// subregs. This may invalidate any operand pointers.
627// Return true if the operand kills its register.
628bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
629 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000630 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000631 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000632 return MO.isKill() || MO.isDead();
633 }
634
635 // Handle subregister index.
636 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
637 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000638
639 // A kill flag implies killing the full register. Add corresponding super
640 // register kill.
641 if (MO.isKill()) {
642 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000643 return true;
644 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000645 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000646}
647
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000648// Handle special instruction operand like early clobbers and tied ops when
649// there are additional physreg defines.
650void RAFast::handleThroughOperands(MachineInstr *MI,
651 SmallVectorImpl<unsigned> &VirtDead) {
652 DEBUG(dbgs() << "Scanning for through registers:");
653 SmallSet<unsigned, 8> ThroughRegs;
654 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
655 MachineOperand &MO = MI->getOperand(i);
656 if (!MO.isReg()) continue;
657 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000658 if (!TargetRegisterInfo::isVirtualRegister(Reg))
659 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000660 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
661 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000662 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000663 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000664 }
665 }
666
667 // If any physreg defines collide with preallocated through registers,
668 // we must spill and reallocate.
669 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
670 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
671 MachineOperand &MO = MI->getOperand(i);
672 if (!MO.isReg() || !MO.isDef()) continue;
673 unsigned Reg = MO.getReg();
674 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
675 UsedInInstr.set(Reg);
676 if (ThroughRegs.count(PhysRegState[Reg]))
677 definePhysReg(MI, Reg, regFree);
678 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
679 UsedInInstr.set(*AS);
680 if (ThroughRegs.count(PhysRegState[*AS]))
681 definePhysReg(MI, *AS, regFree);
682 }
683 }
684
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000685 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola254a1322011-11-22 06:27:18 +0000686 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000687 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
688 MachineOperand &MO = MI->getOperand(i);
689 if (!MO.isReg()) continue;
690 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000691 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000692 if (MO.isUse()) {
693 unsigned DefIdx = 0;
694 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
695 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
696 << DefIdx << ".\n");
697 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
698 unsigned PhysReg = LRI->second.PhysReg;
699 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000700 // Note: we don't update the def operand yet. That would cause the normal
701 // def-scan to attempt spilling.
702 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
703 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
704 // Reload the register, but don't assign to the operand just yet.
705 // That would confuse the later phys-def processing pass.
706 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
707 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000708 }
709 }
710
Rafael Espindola254a1322011-11-22 06:27:18 +0000711 DEBUG(dbgs() << "Allocating early clobbers.\n");
712 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
713 MachineOperand &MO = MI->getOperand(i);
714 if (!MO.isReg()) continue;
715 unsigned Reg = MO.getReg();
716 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
717 if (!MO.isEarlyClobber())
718 continue;
719 // Note: defineVirtReg may invalidate MO.
720 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
721 unsigned PhysReg = LRI->second.PhysReg;
722 if (setPhysReg(MI, i, PhysReg))
723 VirtDead.push_back(Reg);
724 }
725
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000726 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000727 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000728 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
729 MachineOperand &MO = MI->getOperand(i);
730 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
731 unsigned Reg = MO.getReg();
732 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000733 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
734 << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000735 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000736 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000737
738 // Also mark PartialDefs as used to avoid reallocation.
739 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
740 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000741}
742
Andrew Trickb3d58472012-01-31 05:55:32 +0000743/// addRetOperand - ensure that a return instruction has an operand for each
744/// value live out of the function.
745///
746/// Things marked both call and return are tail calls; do not do this for them.
747/// The tail callee need not take the same registers as input that it produces
748/// as output, and there are dependencies for its input registers elsewhere.
749///
750/// FIXME: This should be done as part of instruction selection, and this helper
751/// should be deleted. Until then, we use custom logic here to create the proper
752/// operand under all circumstances. We can't use addRegisterKilled because that
753/// doesn't make sense for undefined values. We can't simply avoid calling it
754/// for undefined values, because we must ensure that the operand always exists.
755void RAFast::addRetOperands(MachineBasicBlock *MBB) {
756 if (MBB->empty() || !MBB->back().isReturn() || MBB->back().isCall())
757 return;
758
759 MachineInstr *MI = &MBB->back();
760
761 for (MachineRegisterInfo::liveout_iterator
762 I = MBB->getParent()->getRegInfo().liveout_begin(),
763 E = MBB->getParent()->getRegInfo().liveout_end(); I != E; ++I) {
764 unsigned Reg = *I;
765 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
766 "Cannot have a live-out virtual register.");
767
768 bool hasDef = PhysRegState[Reg] == regReserved;
769
770 // Check if this register already has an operand.
771 bool Found = false;
772 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
773 MachineOperand &MO = MI->getOperand(i);
774 if (!MO.isReg() || !MO.isUse())
775 continue;
776
777 unsigned OperReg = MO.getReg();
778 for (const unsigned *AS = TRI->getOverlaps(Reg); *AS; ++AS) {
779 if (OperReg != *AS)
780 continue;
781 if (OperReg == Reg || TRI->isSuperRegister(OperReg, Reg)) {
782 // If the ret already has an operand for this physreg or a superset,
783 // don't duplicate it. Set the kill flag if the value is defined.
784 if (hasDef && !MO.isKill())
785 MO.setIsKill();
786 Found = true;
787 break;
788 }
789 }
790 }
791 if (!Found)
792 MI->addOperand(MachineOperand::CreateReg(Reg,
793 false /*IsDef*/,
794 true /*IsImp*/,
795 hasDef/*IsKill*/));
796 }
797}
798
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000799void RAFast::AllocateBasicBlock() {
800 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000801
802 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000803 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000804
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000805 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000806
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000807 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000808 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
809 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000810 if (RegClassInfo.isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000811 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000812
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000813 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000814 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000815
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000816 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000817 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000818 MachineInstr *MI = MII++;
Evan Chenge837dea2011-06-28 19:10:37 +0000819 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000820 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000821 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000822 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
823 if (PhysRegState[Reg] == regDisabled) continue;
824 dbgs() << " " << TRI->getName(Reg);
825 switch(PhysRegState[Reg]) {
826 case regFree:
827 break;
828 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000829 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000830 break;
831 default:
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000832 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000833 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000834 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000835 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000836 "Bad inverse map");
837 break;
838 }
839 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000840 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000841 // Check that LiveVirtRegs is the inverse.
842 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
843 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000844 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
845 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000846 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000847 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000848 assert(PhysRegState[i->second.PhysReg] == i->first &&
849 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000850 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000851 });
852
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000853 // Debug values are not allowed to change codegen in any way.
854 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000855 bool ScanDbgValue = true;
856 while (ScanDbgValue) {
857 ScanDbgValue = false;
858 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
859 MachineOperand &MO = MI->getOperand(i);
860 if (!MO.isReg()) continue;
861 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000862 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Devang Patel58b81762010-07-19 23:25:39 +0000863 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
864 if (LRI != LiveVirtRegs.end())
865 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000866 else {
Devang Patel58b81762010-07-19 23:25:39 +0000867 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000868 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000869 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000870 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000871 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000872 }
Devang Patel58b81762010-07-19 23:25:39 +0000873 else {
874 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000875 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000876 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000877 MI->getOperand(MI->getNumOperands()-1).getMetadata();
878 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000879 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000880 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000881 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
882 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000883 MachineBasicBlock *MBB = MI->getParent();
884 MBB->insert(MBB->erase(MI), NewDV);
885 // Scan NewDV operands from the beginning.
886 MI = NewDV;
887 ScanDbgValue = true;
888 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000889 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000890 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000891 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000892 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000893 }
Devang Patel58b81762010-07-19 23:25:39 +0000894 }
Devang Patel7a029b62010-07-09 21:48:31 +0000895 }
Devang Pateld2df64f2011-11-15 21:03:58 +0000896 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel7a029b62010-07-09 21:48:31 +0000897 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000898 }
899 // Next instruction.
900 continue;
901 }
902
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000903 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000904 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000905 if (MI->isCopy()) {
906 CopyDst = MI->getOperand(0).getReg();
907 CopySrc = MI->getOperand(1).getReg();
908 CopyDstSub = MI->getOperand(0).getSubReg();
909 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000910 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000911
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000912 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000913 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000914
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000915 // First scan.
916 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000917 // Find the end of the virtreg operands
918 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000919 bool hasTiedOps = false;
920 bool hasEarlyClobbers = false;
921 bool hasPartialRedefs = false;
922 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000923 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
924 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000925 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000926 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000927 if (!Reg) continue;
928 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
929 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000930 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000931 hasTiedOps = hasTiedOps ||
Evan Chenge837dea2011-06-28 19:10:37 +0000932 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000933 } else {
934 if (MO.isEarlyClobber())
935 hasEarlyClobbers = true;
936 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
937 hasPartialRedefs = true;
938 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000939 continue;
940 }
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000941 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000942 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000943 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000944 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000945 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
946 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000947 hasEarlyClobbers = true;
948 } else
949 hasPhysDefs = true;
950 }
951
952 // The instruction may have virtual register operands that must be allocated
953 // the same register at use-time and def-time: early clobbers and tied
954 // operands. If there are also physical defs, these registers must avoid
955 // both physical defs and uses, making them more constrained than normal
956 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000957 // Similarly, if there are multiple defs and tied operands, we must make
958 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000959 // We didn't detect inline asm tied operands above, so just make this extra
960 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000961 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Chenge837dea2011-06-28 19:10:37 +0000962 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000963 handleThroughOperands(MI, VirtDead);
964 // Don't attempt coalescing when we have funny stuff going on.
965 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000966 // Pretend we have early clobbers so the use operands get marked below.
967 // This is not necessary for the common case of a single tied use.
968 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000969 }
970
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000971 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000972 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000973 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000974 MachineOperand &MO = MI->getOperand(i);
975 if (!MO.isReg()) continue;
976 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000977 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000978 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000979 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
980 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000981 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000982 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000983 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000984 }
985 }
986
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000987 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000988
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000989 // Track registers defined by instruction - early clobbers and tied uses at
990 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +0000991 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000992 if (hasEarlyClobbers) {
993 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
994 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000995 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000996 unsigned Reg = MO.getReg();
997 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000998 // Look for physreg defs and tied uses.
999 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001000 UsedInInstr.set(Reg);
1001 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
1002 UsedInInstr.set(*AS);
1003 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001004 }
1005
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001006 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001007 if (MI->isCall()) {
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001008 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +00001009 // exception is thrown, the landing pad is going to expect to find
1010 // registers in their spill slots, and 2. we don't have to wade through
1011 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001012 DefOpEnd = VirtOpEnd;
1013 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1014 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001015
1016 // The imp-defs are skipped below, but we still need to mark those
1017 // registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +00001018 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001019 }
1020
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001021 // Third scan.
1022 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001023 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001024 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +00001025 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1026 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001027 unsigned Reg = MO.getReg();
1028
1029 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +00001030 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001031 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1032 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001033 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001034 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001035 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
1036 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001037 if (setPhysReg(MI, i, PhysReg)) {
1038 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001039 CopyDst = 0; // cancel coalescing;
1040 } else
1041 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001042 }
1043
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001044 // Kill dead defs after the scan to ensure that multiple defs of the same
1045 // register are allocated identically. We didn't need to do this for uses
1046 // because we are crerating our own kill flags, and they are always at the
1047 // last use.
1048 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1049 killVirtReg(VirtDead[i]);
1050 VirtDead.clear();
1051
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001052 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001053
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001054 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1055 DEBUG(dbgs() << "-- coalescing: " << *MI);
1056 Coalesced.push_back(MI);
1057 } else {
1058 DEBUG(dbgs() << "<< " << *MI);
1059 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001060 }
1061
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001062 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001063 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1064 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001065
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001066 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001067 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001068 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001069 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001070 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001071
Andrew Trickb3d58472012-01-31 05:55:32 +00001072 // addRetOperands must run after we've seen all defs in this block.
1073 addRetOperands(MBB);
1074
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001075 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001076}
1077
1078/// runOnMachineFunction - Register allocate the whole function
1079///
1080bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001081 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1082 << "********** Function: "
1083 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001084 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001085 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001086 TM = &Fn.getTarget();
1087 TRI = TM->getRegisterInfo();
1088 TII = TM->getInstrInfo();
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +00001089 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001090 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001091 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001092
1093 // initialize the virtual->physical register map to have a 'null'
1094 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001095 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001096
1097 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001098 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1099 MBBi != MBBe; ++MBBi) {
1100 MBB = &*MBBi;
1101 AllocateBasicBlock();
1102 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001103
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001104 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001105 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001106
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001107 // Add the clobber lists for all the instructions we skipped earlier.
Evan Chenge837dea2011-06-28 19:10:37 +00001108 for (SmallPtrSet<const MCInstrDesc*, 4>::const_iterator
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001109 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1110 if (const unsigned *Defs = (*I)->getImplicitDefs())
1111 while (*Defs)
1112 MRI->setPhysRegUsed(*Defs++);
1113
1114 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001115 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001116 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001117 return true;
1118}
1119
1120FunctionPass *llvm::createFastRegisterAllocator() {
1121 return new RAFast();
1122}