blob: 4724ca2f92e477f1c0561d3f3e4c681155b5626e [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),
Andrew Trick8dd26252012-02-10 04:10:36 +000052 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000053 private:
54 const TargetMachine *TM;
55 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000056 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000057 const TargetRegisterInfo *TRI;
58 const TargetInstrInfo *TII;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000059 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000060
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000061 // Basic block currently being allocated.
62 MachineBasicBlock *MBB;
63
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000064 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
65 // values are spilled.
66 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
67
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000068 // Everything we know about a live virtual register.
69 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000070 MachineInstr *LastUse; // Last instr to use reg.
71 unsigned PhysReg; // Currently held here.
72 unsigned short LastOpNum; // OpNum on LastUse.
73 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000074
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000075 LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0),
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000076 Dirty(false) {}
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000077 };
78
79 typedef DenseMap<unsigned, LiveReg> LiveRegMap;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +000080 typedef LiveRegMap::value_type LiveRegEntry;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000081
82 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000083 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000084 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000085
Devang Patel72d9b0e2011-06-21 22:36:03 +000086 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Patel459a36b2010-08-04 18:42:02 +000087
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000088 // RegState - Track the state of a physical register.
89 enum RegState {
90 // A disabled register is not available for allocation, but an alias may
91 // be in use. A register can only be moved out of the disabled state if
92 // all aliases are disabled.
93 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000094
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000095 // A free register is not currently in use and can be allocated
96 // immediately without checking aliases.
97 regFree,
98
Evan Chengd8a16242011-04-22 01:40:20 +000099 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000100 // call parameter), and it remains reserved until it is used.
101 regReserved
102
103 // A register state may also be a virtual register number, indication that
104 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000105 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000106 };
107
108 // PhysRegState - One of the RegState enums, or a virtreg.
109 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000110
111 // UsedInInstr - BitVector of physregs that are used in the current
112 // instruction, and so cannot be allocated.
113 BitVector UsedInInstr;
114
Jim Grosbach07cb6892010-09-01 19:16:29 +0000115 // SkippedInstrs - Descriptors of instructions whose clobber list was
116 // ignored because all registers were spilled. It is still necessary to
117 // mark all the clobbered registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +0000118 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000119
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000120 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
121 // completely after spilling all live registers. LiveRegMap entries should
122 // not be erased.
123 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000124
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000125 enum {
126 spillClean = 1,
127 spillDirty = 100,
128 spillImpossible = ~0u
129 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000130 public:
131 virtual const char *getPassName() const {
132 return "Fast Register Allocator";
133 }
134
135 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
136 AU.setPreservesCFG();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000137 MachineFunctionPass::getAnalysisUsage(AU);
138 }
139
140 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000141 bool runOnMachineFunction(MachineFunction &Fn);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000142 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000143 void handleThroughOperands(MachineInstr *MI,
144 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000145 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000146 bool isLastUseOfLocalReg(MachineOperand&);
147
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000148 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000149 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000150 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000151 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000152 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000153
154 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000155 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000156 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000157 void assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg);
158 void allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000159 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
160 unsigned VirtReg, unsigned Hint);
161 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
162 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000163 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000164 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Andrew Trickb3d58472012-01-31 05:55:32 +0000165 void addRetOperands(MachineBasicBlock *MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000166 };
167 char RAFast::ID = 0;
168}
169
170/// getStackSpaceFor - This allocates space for the specified virtual register
171/// to be held on the stack.
172int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
173 // Find the location Reg would belong...
174 int SS = StackSlotForVirtReg[VirtReg];
175 if (SS != -1)
176 return SS; // Already has space allocated?
177
178 // Allocate a new stack object for this spill location...
179 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
180 RC->getAlignment());
181
182 // Assign the slot.
183 StackSlotForVirtReg[VirtReg] = FrameIdx;
184 return FrameIdx;
185}
186
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000187/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
188/// its virtual register, and it is guaranteed to be a block-local register.
189///
190bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
191 // Check for non-debug uses or defs following MO.
192 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000193 MachineOperand *Next = &MO;
194 while ((Next = Next->getNextOperandForReg()))
195 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000196 return false;
197
198 // If the register has ever been spilled or reloaded, we conservatively assume
199 // it is a global register used in multiple blocks.
200 if (StackSlotForVirtReg[MO.getReg()] != -1)
201 return false;
202
203 // Check that the use/def chain has exactly one operand - MO.
204 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
205}
206
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000207/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000208void RAFast::addKillFlag(const LiveReg &LR) {
209 if (!LR.LastUse) return;
210 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000211 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
212 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000213 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000214 else
215 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
216 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000217}
218
219/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000220void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
221 addKillFlag(LRI->second);
222 const LiveReg &LR = LRI->second;
223 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000224 PhysRegState[LR.PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000225 // Erase from LiveVirtRegs unless we're spilling in bulk.
226 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000227 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000228}
229
230/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000231void RAFast::killVirtReg(unsigned VirtReg) {
232 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
233 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000234 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
235 if (LRI != LiveVirtRegs.end())
236 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000237}
238
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000239/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000240/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000241void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000242 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
243 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000244 LiveRegMap::iterator LRI = LiveVirtRegs.find(VirtReg);
245 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
246 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000247}
248
249/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000250void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000251 LiveRegMap::iterator LRI) {
252 LiveReg &LR = LRI->second;
253 assert(PhysRegState[LR.PhysReg] == LRI->first && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000254
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000255 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000256 // If this physreg is used by the instruction, we want to kill it on the
257 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000258 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000259 LR.Dirty = false;
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000260 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->first, TRI)
261 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000262 const TargetRegisterClass *RC = MRI->getRegClass(LRI->first);
263 int FI = getStackSpaceFor(LRI->first, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000264 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000265 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000266 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000267
Jim Grosbach07cb6892010-09-01 19:16:29 +0000268 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000269 // identify spilled location as the place to find corresponding variable's
270 // value.
Devang Patel72d9b0e2011-06-21 22:36:03 +0000271 SmallVector<MachineInstr *, 4> &LRIDbgValues = LiveDbgValueMap[LRI->first];
272 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
273 MachineInstr *DBG = LRIDbgValues[li];
Jim Grosbach07cb6892010-09-01 19:16:29 +0000274 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000275 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
276 int64_t Offset = 0;
277 if (DBG->getOperand(1).isImm())
278 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000279 DebugLoc DL;
280 if (MI == MBB->end()) {
281 // If MI is at basic block end then use last instruction's location.
282 MachineBasicBlock::iterator EI = MI;
283 DL = (--EI)->getDebugLoc();
284 }
285 else
286 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000287 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000288 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
289 MachineBasicBlock *MBB = DBG->getParent();
290 MBB->insert(MI, NewDV);
291 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Patel459a36b2010-08-04 18:42:02 +0000292 }
293 }
Devang Patel6f373a82011-06-21 23:02:36 +0000294 // Now this register is spilled there is should not be any DBG_VALUE pointing
295 // to this register because they are all pointing to spilled value now.
296 LRIDbgValues.clear();
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000297 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000298 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000299 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000300 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000301}
302
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000303/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000304void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000305 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000306 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000307 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
308 // of spilling here is deterministic, if arbitrary.
309 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
310 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000311 spillVirtReg(MI, i);
312 LiveVirtRegs.clear();
313 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000314}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000315
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000316/// usePhysReg - Handle the direct use of a physical register.
317/// Check that the register is not used by a virtreg.
318/// Kill the physreg, marking it free.
319/// This may add implicit kills to MO->getParent() and invalidate MO.
320void RAFast::usePhysReg(MachineOperand &MO) {
321 unsigned PhysReg = MO.getReg();
322 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
323 "Bad usePhysReg operand");
324
325 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000326 case regDisabled:
327 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000328 case regReserved:
329 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000330 // Fall through
331 case regFree:
332 UsedInInstr.set(PhysReg);
333 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000334 return;
335 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000336 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000337 // wanted has been clobbered.
338 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000339 }
340
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000341 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000342 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
343 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000344 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000345 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000346 break;
347 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000348 assert(TRI->isSuperRegister(PhysReg, Alias) &&
349 "Instruction is not using a subregister of a reserved register");
350 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000351 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000352 UsedInInstr.set(Alias);
353 MO.getParent()->addRegisterKilled(Alias, TRI, true);
354 return;
355 case regFree:
356 if (TRI->isSuperRegister(PhysReg, Alias)) {
357 // Leave the superregister in the working set.
358 UsedInInstr.set(Alias);
359 MO.getParent()->addRegisterKilled(Alias, TRI, true);
360 return;
361 }
362 // Some other alias was in the working set - clear it.
363 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000364 break;
365 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000366 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000367 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000368 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000369
370 // All aliases are disabled, bring register into working set.
371 PhysRegState[PhysReg] = regFree;
372 UsedInInstr.set(PhysReg);
373 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000374}
375
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000376/// definePhysReg - Mark PhysReg as reserved or free after spilling any
377/// virtregs. This is very similar to defineVirtReg except the physreg is
378/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000379void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
380 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000381 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000382 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
383 case regDisabled:
384 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000385 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000386 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000387 // Fall through.
388 case regFree:
389 case regReserved:
390 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000391 return;
392 }
393
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000394 // This is a disabled register, disable all aliases.
395 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000396 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
397 unsigned Alias = *AS; ++AS) {
398 switch (unsigned VirtReg = PhysRegState[Alias]) {
399 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000400 break;
401 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000402 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000403 // Fall through.
404 case regFree:
405 case regReserved:
406 PhysRegState[Alias] = regDisabled;
407 if (TRI->isSuperRegister(PhysReg, Alias))
408 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000409 break;
410 }
411 }
412}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000413
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000414
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000415// calcSpillCost - Return the cost of spilling clearing out PhysReg and
416// aliases so it is free for allocation.
417// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
418// can be allocated directly.
419// Returns spillImpossible when PhysReg or an alias can't be spilled.
420unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000421 if (UsedInInstr.test(PhysReg)) {
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000422 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000423 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000424 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000425 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
426 case regDisabled:
427 break;
428 case regFree:
429 return 0;
430 case regReserved:
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000431 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
432 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000433 return spillImpossible;
434 default:
435 return LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
436 }
437
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000438 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000439 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000440 unsigned Cost = 0;
441 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
442 unsigned Alias = *AS; ++AS) {
Eric Christopherd31df872011-04-13 00:20:59 +0000443 if (UsedInInstr.test(Alias))
444 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000445 switch (unsigned VirtReg = PhysRegState[Alias]) {
446 case regDisabled:
447 break;
448 case regFree:
449 ++Cost;
450 break;
451 case regReserved:
452 return spillImpossible;
453 default:
454 Cost += LiveVirtRegs.lookup(VirtReg).Dirty ? spillDirty : spillClean;
455 break;
456 }
457 }
458 return Cost;
459}
460
461
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000462/// assignVirtToPhysReg - This method updates local state so that we know
463/// that PhysReg is the proper container for VirtReg now. The physical
464/// register must not be used for anything else when this is called.
465///
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000466void RAFast::assignVirtToPhysReg(LiveRegEntry &LRE, unsigned PhysReg) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000467 DEBUG(dbgs() << "Assigning " << PrintReg(LRE.first, TRI) << " to "
468 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000469 PhysRegState[PhysReg] = LRE.first;
470 assert(!LRE.second.PhysReg && "Already assigned a physreg");
471 LRE.second.PhysReg = PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000472}
473
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000474/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000475void RAFast::allocVirtReg(MachineInstr *MI, LiveRegEntry &LRE, unsigned Hint) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000476 const unsigned VirtReg = LRE.first;
477
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000478 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
479 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000480
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000481 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000482
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000483 // Ignore invalid hints.
484 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000485 !RC->contains(Hint) || !RegClassInfo.isAllocatable(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000486 Hint = 0;
487
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000488 // Take hint when possible.
489 if (Hint) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000490 // Ignore the hint if we would have to spill a dirty register.
491 unsigned Cost = calcSpillCost(Hint);
492 if (Cost < spillDirty) {
493 if (Cost)
494 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000495 return assignVirtToPhysReg(LRE, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000496 }
497 }
498
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000499 ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000500
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000501 // First try to find a completely free register.
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000502 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000503 unsigned PhysReg = *I;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000504 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg))
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000505 return assignVirtToPhysReg(LRE, PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000506 }
507
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000508 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
509 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000510
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000511 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000512 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000513 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000514 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopher0b756342011-04-12 22:17:44 +0000515 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
516 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000517 // Cost is 0 when all aliases are already disabled.
518 if (Cost == 0)
519 return assignVirtToPhysReg(LRE, *I);
520 if (Cost < BestCost)
521 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000522 }
523
524 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000525 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000526 return assignVirtToPhysReg(LRE, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000527 }
528
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000529 // Nothing we can do. Report an error and keep going with a bad allocation.
530 MI->emitError("ran out of registers during register allocation");
531 definePhysReg(MI, *AO.begin(), regFree);
532 assignVirtToPhysReg(LRE, *AO.begin());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000533}
534
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000535/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000536RAFast::LiveRegMap::iterator
537RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
538 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000539 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
540 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000541 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000542 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000543 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
544 LiveReg &LR = LRI->second;
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000545 if (New) {
546 // If there is no hint, peek at the only use of this register.
547 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
548 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000549 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000550 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000551 if (UseMI.isCopyLike())
552 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000553 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000554 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000555 } else if (LR.LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000556 // Redefining a live register - kill at the last use, unless it is this
557 // instruction defining VirtReg multiple times.
558 if (LR.LastUse != MI || LR.LastUse->getOperand(LR.LastOpNum).isUse())
559 addKillFlag(LR);
560 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000561 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000562 LR.LastUse = MI;
563 LR.LastOpNum = OpNum;
564 LR.Dirty = true;
565 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000566 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000567}
568
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000569/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000570RAFast::LiveRegMap::iterator
571RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
572 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000573 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
574 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000575 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000576 bool New;
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000577 tie(LRI, New) = LiveVirtRegs.insert(std::make_pair(VirtReg, LiveReg()));
578 LiveReg &LR = LRI->second;
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000579 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000580 if (New) {
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000581 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000582 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000583 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000584 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
585 << PrintReg(LR.PhysReg, TRI) << "\n");
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000586 TII->loadRegFromStackSlot(*MBB, MI, LR.PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000587 ++NumLoads;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000588 } else if (LR.Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000589 if (isLastUseOfLocalReg(MO)) {
590 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000591 if (MO.isUse())
592 MO.setIsKill();
593 else
594 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000595 } else if (MO.isKill()) {
596 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
597 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000598 } else if (MO.isDead()) {
599 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
600 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000601 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000602 } else if (MO.isKill()) {
603 // We must remove kill flags from uses of reloaded registers because the
604 // register would be killed immediately, and there might be a second use:
605 // %foo = OR %x<kill>, %x
606 // This would cause a second reload of %x into a different register.
607 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
608 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000609 } else if (MO.isDead()) {
610 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
611 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000612 }
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000613 assert(LR.PhysReg && "Register not assigned");
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000614 LR.LastUse = MI;
615 LR.LastOpNum = OpNum;
616 UsedInInstr.set(LR.PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000617 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000618}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000619
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000620// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
621// subregs. This may invalidate any operand pointers.
622// Return true if the operand kills its register.
623bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
624 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000625 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000626 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000627 return MO.isKill() || MO.isDead();
628 }
629
630 // Handle subregister index.
631 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
632 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000633
634 // A kill flag implies killing the full register. Add corresponding super
635 // register kill.
636 if (MO.isKill()) {
637 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000638 return true;
639 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000640 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000641}
642
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000643// Handle special instruction operand like early clobbers and tied ops when
644// there are additional physreg defines.
645void RAFast::handleThroughOperands(MachineInstr *MI,
646 SmallVectorImpl<unsigned> &VirtDead) {
647 DEBUG(dbgs() << "Scanning for through registers:");
648 SmallSet<unsigned, 8> ThroughRegs;
649 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
650 MachineOperand &MO = MI->getOperand(i);
651 if (!MO.isReg()) continue;
652 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000653 if (!TargetRegisterInfo::isVirtualRegister(Reg))
654 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000655 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
656 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000657 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000658 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000659 }
660 }
661
662 // If any physreg defines collide with preallocated through registers,
663 // we must spill and reallocate.
664 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
665 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
666 MachineOperand &MO = MI->getOperand(i);
667 if (!MO.isReg() || !MO.isDef()) continue;
668 unsigned Reg = MO.getReg();
669 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
670 UsedInInstr.set(Reg);
671 if (ThroughRegs.count(PhysRegState[Reg]))
672 definePhysReg(MI, Reg, regFree);
673 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
674 UsedInInstr.set(*AS);
675 if (ThroughRegs.count(PhysRegState[*AS]))
676 definePhysReg(MI, *AS, regFree);
677 }
678 }
679
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000680 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola254a1322011-11-22 06:27:18 +0000681 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000682 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
683 MachineOperand &MO = MI->getOperand(i);
684 if (!MO.isReg()) continue;
685 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000686 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000687 if (MO.isUse()) {
688 unsigned DefIdx = 0;
689 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
690 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
691 << DefIdx << ".\n");
692 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
693 unsigned PhysReg = LRI->second.PhysReg;
694 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000695 // Note: we don't update the def operand yet. That would cause the normal
696 // def-scan to attempt spilling.
697 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
698 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
699 // Reload the register, but don't assign to the operand just yet.
700 // That would confuse the later phys-def processing pass.
701 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
702 PartialDefs.push_back(LRI->second.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000703 }
704 }
705
Rafael Espindola254a1322011-11-22 06:27:18 +0000706 DEBUG(dbgs() << "Allocating early clobbers.\n");
707 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
708 MachineOperand &MO = MI->getOperand(i);
709 if (!MO.isReg()) continue;
710 unsigned Reg = MO.getReg();
711 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
712 if (!MO.isEarlyClobber())
713 continue;
714 // Note: defineVirtReg may invalidate MO.
715 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
716 unsigned PhysReg = LRI->second.PhysReg;
717 if (setPhysReg(MI, i, PhysReg))
718 VirtDead.push_back(Reg);
719 }
720
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000721 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000722 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000723 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
724 MachineOperand &MO = MI->getOperand(i);
725 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
726 unsigned Reg = MO.getReg();
727 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000728 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
729 << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000730 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000731 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000732
733 // Also mark PartialDefs as used to avoid reallocation.
734 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
735 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000736}
737
Andrew Trickb3d58472012-01-31 05:55:32 +0000738/// addRetOperand - ensure that a return instruction has an operand for each
739/// value live out of the function.
740///
741/// Things marked both call and return are tail calls; do not do this for them.
742/// The tail callee need not take the same registers as input that it produces
743/// as output, and there are dependencies for its input registers elsewhere.
744///
745/// FIXME: This should be done as part of instruction selection, and this helper
746/// should be deleted. Until then, we use custom logic here to create the proper
747/// operand under all circumstances. We can't use addRegisterKilled because that
748/// doesn't make sense for undefined values. We can't simply avoid calling it
749/// for undefined values, because we must ensure that the operand always exists.
750void RAFast::addRetOperands(MachineBasicBlock *MBB) {
751 if (MBB->empty() || !MBB->back().isReturn() || MBB->back().isCall())
752 return;
753
754 MachineInstr *MI = &MBB->back();
755
756 for (MachineRegisterInfo::liveout_iterator
757 I = MBB->getParent()->getRegInfo().liveout_begin(),
758 E = MBB->getParent()->getRegInfo().liveout_end(); I != E; ++I) {
759 unsigned Reg = *I;
760 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
761 "Cannot have a live-out virtual register.");
762
763 bool hasDef = PhysRegState[Reg] == regReserved;
764
765 // Check if this register already has an operand.
766 bool Found = false;
767 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
768 MachineOperand &MO = MI->getOperand(i);
769 if (!MO.isReg() || !MO.isUse())
770 continue;
771
772 unsigned OperReg = MO.getReg();
Andrew Trickab78e202012-01-31 18:54:19 +0000773 if (!TargetRegisterInfo::isPhysicalRegister(OperReg))
774 continue;
775
776 if (OperReg == Reg || TRI->isSuperRegister(OperReg, Reg)) {
777 // If the ret already has an operand for this physreg or a superset,
778 // don't duplicate it. Set the kill flag if the value is defined.
779 if (hasDef && !MO.isKill())
780 MO.setIsKill();
781 Found = true;
782 break;
Andrew Trickb3d58472012-01-31 05:55:32 +0000783 }
784 }
785 if (!Found)
786 MI->addOperand(MachineOperand::CreateReg(Reg,
787 false /*IsDef*/,
788 true /*IsImp*/,
789 hasDef/*IsKill*/));
790 }
791}
792
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000793void RAFast::AllocateBasicBlock() {
794 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000795
796 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000797 assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000798
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000799 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000800
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000801 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000802 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
803 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000804 if (RegClassInfo.isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000805 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000806
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000807 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000808 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000809
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000810 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000811 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000812 MachineInstr *MI = MII++;
Evan Chenge837dea2011-06-28 19:10:37 +0000813 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000814 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000815 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000816 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
817 if (PhysRegState[Reg] == regDisabled) continue;
818 dbgs() << " " << TRI->getName(Reg);
819 switch(PhysRegState[Reg]) {
820 case regFree:
821 break;
822 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000823 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000824 break;
825 default:
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000826 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000827 if (LiveVirtRegs[PhysRegState[Reg]].Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000828 dbgs() << "*";
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000829 assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000830 "Bad inverse map");
831 break;
832 }
833 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000834 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000835 // Check that LiveVirtRegs is the inverse.
836 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
837 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000838 assert(TargetRegisterInfo::isVirtualRegister(i->first) &&
839 "Bad map key");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000840 assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000841 "Bad map value");
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000842 assert(PhysRegState[i->second.PhysReg] == i->first &&
843 "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000844 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000845 });
846
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000847 // Debug values are not allowed to change codegen in any way.
848 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000849 bool ScanDbgValue = true;
850 while (ScanDbgValue) {
851 ScanDbgValue = false;
852 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
853 MachineOperand &MO = MI->getOperand(i);
854 if (!MO.isReg()) continue;
855 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000856 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Devang Patel58b81762010-07-19 23:25:39 +0000857 LiveRegMap::iterator LRI = LiveVirtRegs.find(Reg);
858 if (LRI != LiveVirtRegs.end())
859 setPhysReg(MI, i, LRI->second.PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000860 else {
Devang Patel58b81762010-07-19 23:25:39 +0000861 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000862 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000863 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000864 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000865 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000866 }
Devang Patel58b81762010-07-19 23:25:39 +0000867 else {
868 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000869 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000870 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000871 MI->getOperand(MI->getNumOperands()-1).getMetadata();
872 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000873 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000874 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000875 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
876 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000877 MachineBasicBlock *MBB = MI->getParent();
878 MBB->insert(MBB->erase(MI), NewDV);
879 // Scan NewDV operands from the beginning.
880 MI = NewDV;
881 ScanDbgValue = true;
882 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000883 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000884 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000885 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000886 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000887 }
Devang Patel58b81762010-07-19 23:25:39 +0000888 }
Devang Patel7a029b62010-07-09 21:48:31 +0000889 }
Devang Pateld2df64f2011-11-15 21:03:58 +0000890 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel7a029b62010-07-09 21:48:31 +0000891 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000892 }
893 // Next instruction.
894 continue;
895 }
896
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000897 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000898 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000899 if (MI->isCopy()) {
900 CopyDst = MI->getOperand(0).getReg();
901 CopySrc = MI->getOperand(1).getReg();
902 CopyDstSub = MI->getOperand(0).getSubReg();
903 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000904 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000905
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000906 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000907 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000908
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000909 // First scan.
910 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000911 // Find the end of the virtreg operands
912 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000913 bool hasTiedOps = false;
914 bool hasEarlyClobbers = false;
915 bool hasPartialRedefs = false;
916 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000917 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
918 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000919 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000920 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000921 if (!Reg) continue;
922 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
923 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000924 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000925 hasTiedOps = hasTiedOps ||
Evan Chenge837dea2011-06-28 19:10:37 +0000926 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000927 } else {
928 if (MO.isEarlyClobber())
929 hasEarlyClobbers = true;
930 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
931 hasPartialRedefs = true;
932 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000933 continue;
934 }
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000935 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000936 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000937 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000938 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000939 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
940 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000941 hasEarlyClobbers = true;
942 } else
943 hasPhysDefs = true;
944 }
945
946 // The instruction may have virtual register operands that must be allocated
947 // the same register at use-time and def-time: early clobbers and tied
948 // operands. If there are also physical defs, these registers must avoid
949 // both physical defs and uses, making them more constrained than normal
950 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000951 // Similarly, if there are multiple defs and tied operands, we must make
952 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000953 // We didn't detect inline asm tied operands above, so just make this extra
954 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000955 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Chenge837dea2011-06-28 19:10:37 +0000956 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000957 handleThroughOperands(MI, VirtDead);
958 // Don't attempt coalescing when we have funny stuff going on.
959 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000960 // Pretend we have early clobbers so the use operands get marked below.
961 // This is not necessary for the common case of a single tied use.
962 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000963 }
964
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000965 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000966 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000967 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000968 MachineOperand &MO = MI->getOperand(i);
969 if (!MO.isReg()) continue;
970 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000971 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000972 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000973 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
974 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000975 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000976 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000977 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000978 }
979 }
980
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000981 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000982
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000983 // Track registers defined by instruction - early clobbers and tied uses at
984 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +0000985 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000986 if (hasEarlyClobbers) {
987 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
988 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000989 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000990 unsigned Reg = MO.getReg();
991 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000992 // Look for physreg defs and tied uses.
993 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000994 UsedInInstr.set(Reg);
995 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
996 UsedInInstr.set(*AS);
997 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000998 }
999
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001000 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001001 if (MI->isCall()) {
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001002 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +00001003 // exception is thrown, the landing pad is going to expect to find
1004 // registers in their spill slots, and 2. we don't have to wade through
1005 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001006 DefOpEnd = VirtOpEnd;
1007 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1008 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001009
1010 // The imp-defs are skipped below, but we still need to mark those
1011 // registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +00001012 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001013 }
1014
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001015 // Third scan.
1016 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001017 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001018 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +00001019 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1020 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001021 unsigned Reg = MO.getReg();
1022
1023 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +00001024 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001025 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1026 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001027 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001028 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001029 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
1030 unsigned PhysReg = LRI->second.PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001031 if (setPhysReg(MI, i, PhysReg)) {
1032 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001033 CopyDst = 0; // cancel coalescing;
1034 } else
1035 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001036 }
1037
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001038 // Kill dead defs after the scan to ensure that multiple defs of the same
1039 // register are allocated identically. We didn't need to do this for uses
1040 // because we are crerating our own kill flags, and they are always at the
1041 // last use.
1042 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1043 killVirtReg(VirtDead[i]);
1044 VirtDead.clear();
1045
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001046 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001047
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001048 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1049 DEBUG(dbgs() << "-- coalescing: " << *MI);
1050 Coalesced.push_back(MI);
1051 } else {
1052 DEBUG(dbgs() << "<< " << *MI);
1053 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001054 }
1055
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001056 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001057 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1058 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001059
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001060 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001061 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001062 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001063 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001064 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001065
Andrew Trickb3d58472012-01-31 05:55:32 +00001066 // addRetOperands must run after we've seen all defs in this block.
1067 addRetOperands(MBB);
1068
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001069 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001070}
1071
1072/// runOnMachineFunction - Register allocate the whole function
1073///
1074bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001075 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1076 << "********** Function: "
1077 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001078 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001079 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001080 TM = &Fn.getTarget();
1081 TRI = TM->getRegisterInfo();
1082 TII = TM->getInstrInfo();
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +00001083 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001084 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001085 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001086
Andrew Trick8dd26252012-02-10 04:10:36 +00001087 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1088
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001089 // initialize the virtual->physical register map to have a 'null'
1090 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001091 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001092
1093 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001094 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1095 MBBi != MBBe; ++MBBi) {
1096 MBB = &*MBBi;
1097 AllocateBasicBlock();
1098 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001099
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001100 // Make sure the set of used physregs is closed under subreg operations.
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001101 MRI->closePhysRegsUsed(*TRI);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001102
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001103 // Add the clobber lists for all the instructions we skipped earlier.
Evan Chenge837dea2011-06-28 19:10:37 +00001104 for (SmallPtrSet<const MCInstrDesc*, 4>::const_iterator
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001105 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1106 if (const unsigned *Defs = (*I)->getImplicitDefs())
1107 while (*Defs)
1108 MRI->setPhysRegUsed(*Defs++);
1109
1110 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001111 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001112 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001113 return true;
1114}
1115
1116FunctionPass *llvm::createFastRegisterAllocator() {
1117 return new RAFast();
1118}