blob: 6b3a48eefd95bfecf15b85221d14c6059d85c47b [file] [log] [blame]
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "llvm/BasicBlock.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
Devang Patel459a36b2010-08-04 18:42:02 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/CodeGen/RegAllocRegistry.h"
Andrew Trick15252602012-06-06 20:29:31 +000024#include "llvm/CodeGen/RegisterClassInfo.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000025#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"
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000035#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/STLExtras.h"
38#include <algorithm>
39using namespace llvm;
40
41STATISTIC(NumStores, "Number of stores added");
42STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000043STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000044
45static RegisterRegAlloc
46 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
47
48namespace {
49 class RAFast : public MachineFunctionPass {
50 public:
51 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000052 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Andrew Trick8dd26252012-02-10 04:10:36 +000053 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000054 private:
55 const TargetMachine *TM;
56 MachineFunction *MF;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000057 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000058 const TargetRegisterInfo *TRI;
59 const TargetInstrInfo *TII;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000060 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000061
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000062 // Basic block currently being allocated.
63 MachineBasicBlock *MBB;
64
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000065 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
66 // values are spilled.
67 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
68
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000069 // Everything we know about a live virtual register.
70 struct LiveReg {
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000071 MachineInstr *LastUse; // Last instr to use reg.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000072 unsigned VirtReg; // Virtual register number.
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +000073 unsigned PhysReg; // Currently held here.
74 unsigned short LastOpNum; // OpNum on LastUse.
75 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000076
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000077 explicit LiveReg(unsigned v)
78 : LastUse(0), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false) {}
79
Andrew Trickc0ccb8b2012-04-20 20:05:28 +000080 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000081 return TargetRegisterInfo::virtReg2Index(VirtReg);
82 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000083 };
84
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000085 typedef SparseSet<LiveReg> LiveRegMap;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000086
87 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000088 // that is currently available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000089 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000090
Devang Patel72d9b0e2011-06-21 22:36:03 +000091 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Patel459a36b2010-08-04 18:42:02 +000092
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +000093 // RegState - Track the state of a physical register.
94 enum RegState {
95 // A disabled register is not available for allocation, but an alias may
96 // be in use. A register can only be moved out of the disabled state if
97 // all aliases are disabled.
98 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000099
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000100 // A free register is not currently in use and can be allocated
101 // immediately without checking aliases.
102 regFree,
103
Evan Chengd8a16242011-04-22 01:40:20 +0000104 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000105 // call parameter), and it remains reserved until it is used.
106 regReserved
107
108 // A register state may also be a virtual register number, indication that
109 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000110 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000111 };
112
113 // PhysRegState - One of the RegState enums, or a virtreg.
114 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000115
116 // UsedInInstr - BitVector of physregs that are used in the current
117 // instruction, and so cannot be allocated.
118 BitVector UsedInInstr;
119
Jim Grosbach07cb6892010-09-01 19:16:29 +0000120 // SkippedInstrs - Descriptors of instructions whose clobber list was
121 // ignored because all registers were spilled. It is still necessary to
122 // mark all the clobbered registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +0000123 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000124
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000125 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
126 // completely after spilling all live registers. LiveRegMap entries should
127 // not be erased.
128 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000129
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000130 enum {
131 spillClean = 1,
132 spillDirty = 100,
133 spillImpossible = ~0u
134 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000135 public:
136 virtual const char *getPassName() const {
137 return "Fast Register Allocator";
138 }
139
140 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
141 AU.setPreservesCFG();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000142 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 Olesena2407432012-02-22 01:02:37 +0000162 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
163 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
164 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
165 }
166 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
167 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
168 }
169 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
170 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator,
171 unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000172 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
173 unsigned VirtReg, unsigned Hint);
174 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
175 unsigned VirtReg, unsigned Hint);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000176 void spillAll(MachineInstr *MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000177 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Andrew Trickb3d58472012-01-31 05:55:32 +0000178 void addRetOperands(MachineBasicBlock *MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000179 };
180 char RAFast::ID = 0;
181}
182
183/// getStackSpaceFor - This allocates space for the specified virtual register
184/// to be held on the stack.
185int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
186 // Find the location Reg would belong...
187 int SS = StackSlotForVirtReg[VirtReg];
188 if (SS != -1)
189 return SS; // Already has space allocated?
190
191 // Allocate a new stack object for this spill location...
192 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
193 RC->getAlignment());
194
195 // Assign the slot.
196 StackSlotForVirtReg[VirtReg] = FrameIdx;
197 return FrameIdx;
198}
199
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000200/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
201/// its virtual register, and it is guaranteed to be a block-local register.
202///
203bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000204 // If the register has ever been spilled or reloaded, we conservatively assume
205 // it is a global register used in multiple blocks.
206 if (StackSlotForVirtReg[MO.getReg()] != -1)
207 return false;
208
209 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesen4e696622012-08-08 23:44:01 +0000210 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
211 if (&I.getOperand() != &MO)
212 return false;
213 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000214}
215
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000216/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000217void RAFast::addKillFlag(const LiveReg &LR) {
218 if (!LR.LastUse) return;
219 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000220 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
221 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000222 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000223 else
224 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
225 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000226}
227
228/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000229void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000230 addKillFlag(*LRI);
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000231 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
232 "Broken RegState mapping");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000233 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000234 // Erase from LiveVirtRegs unless we're spilling in bulk.
235 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000236 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000237}
238
239/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000240void RAFast::killVirtReg(unsigned VirtReg) {
241 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
242 "killVirtReg needs a virtual register");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000243 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000244 if (LRI != LiveVirtRegs.end())
245 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000246}
247
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000248/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000249/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000250void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000251 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
252 "Spilling a physical register is illegal!");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000253 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000254 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
255 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000256}
257
258/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000259void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000260 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000261 LiveReg &LR = *LRI;
262 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000263
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000264 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000265 // If this physreg is used by the instruction, we want to kill it on the
266 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000267 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000268 LR.Dirty = false;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000269 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000270 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000271 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
272 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000273 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000274 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000275 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000276
Jim Grosbach07cb6892010-09-01 19:16:29 +0000277 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000278 // identify spilled location as the place to find corresponding variable's
279 // value.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000280 SmallVector<MachineInstr *, 4> &LRIDbgValues =
281 LiveDbgValueMap[LRI->VirtReg];
Devang Patel72d9b0e2011-06-21 22:36:03 +0000282 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
283 MachineInstr *DBG = LRIDbgValues[li];
Jim Grosbach07cb6892010-09-01 19:16:29 +0000284 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000285 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
286 int64_t Offset = 0;
287 if (DBG->getOperand(1).isImm())
288 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000289 DebugLoc DL;
290 if (MI == MBB->end()) {
291 // If MI is at basic block end then use last instruction's location.
292 MachineBasicBlock::iterator EI = MI;
293 DL = (--EI)->getDebugLoc();
294 }
295 else
296 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000297 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000298 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
299 MachineBasicBlock *MBB = DBG->getParent();
300 MBB->insert(MI, NewDV);
301 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Patel459a36b2010-08-04 18:42:02 +0000302 }
303 }
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000304 // Now this register is spilled there is should not be any DBG_VALUE
305 // pointing to this register because they are all pointing to spilled value
306 // now.
Devang Patel6f373a82011-06-21 23:02:36 +0000307 LRIDbgValues.clear();
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000308 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000309 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000310 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000311 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000312}
313
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000314/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000315void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000316 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000317 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000318 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
319 // of spilling here is deterministic, if arbitrary.
320 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
321 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000322 spillVirtReg(MI, i);
323 LiveVirtRegs.clear();
324 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000325}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000326
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000327/// usePhysReg - Handle the direct use of a physical register.
328/// Check that the register is not used by a virtreg.
329/// Kill the physreg, marking it free.
330/// This may add implicit kills to MO->getParent() and invalidate MO.
331void RAFast::usePhysReg(MachineOperand &MO) {
332 unsigned PhysReg = MO.getReg();
333 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
334 "Bad usePhysReg operand");
335
336 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000337 case regDisabled:
338 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000339 case regReserved:
340 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000341 // Fall through
342 case regFree:
343 UsedInInstr.set(PhysReg);
344 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000345 return;
346 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000347 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000348 // wanted has been clobbered.
349 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000350 }
351
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000352 // Maybe a superregister is reserved?
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000353 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
354 unsigned Alias = *AI;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000355 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000356 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000357 break;
358 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000359 assert(TRI->isSuperRegister(PhysReg, Alias) &&
360 "Instruction is not using a subregister of a reserved register");
361 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000362 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000363 UsedInInstr.set(Alias);
364 MO.getParent()->addRegisterKilled(Alias, TRI, true);
365 return;
366 case regFree:
367 if (TRI->isSuperRegister(PhysReg, Alias)) {
368 // Leave the superregister in the working set.
369 UsedInInstr.set(Alias);
370 MO.getParent()->addRegisterKilled(Alias, TRI, true);
371 return;
372 }
373 // Some other alias was in the working set - clear it.
374 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000375 break;
376 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000377 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000378 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000379 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000380
381 // All aliases are disabled, bring register into working set.
382 PhysRegState[PhysReg] = regFree;
383 UsedInInstr.set(PhysReg);
384 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000385}
386
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000387/// definePhysReg - Mark PhysReg as reserved or free after spilling any
388/// virtregs. This is very similar to defineVirtReg except the physreg is
389/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000390void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
391 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000392 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000393 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
394 case regDisabled:
395 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000396 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000397 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000398 // Fall through.
399 case regFree:
400 case regReserved:
401 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000402 return;
403 }
404
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000405 // This is a disabled register, disable all aliases.
406 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000407 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
408 unsigned Alias = *AI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000409 switch (unsigned VirtReg = PhysRegState[Alias]) {
410 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000411 break;
412 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000413 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000414 // Fall through.
415 case regFree:
416 case regReserved:
417 PhysRegState[Alias] = regDisabled;
418 if (TRI->isSuperRegister(PhysReg, Alias))
419 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000420 break;
421 }
422 }
423}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000424
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000425
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000426// calcSpillCost - Return the cost of spilling clearing out PhysReg and
427// aliases so it is free for allocation.
428// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
429// can be allocated directly.
430// Returns spillImpossible when PhysReg or an alias can't be spilled.
431unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000432 if (UsedInInstr.test(PhysReg)) {
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000433 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000434 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000435 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000436 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
437 case regDisabled:
438 break;
439 case regFree:
440 return 0;
441 case regReserved:
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000442 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
443 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000444 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000445 default: {
446 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
447 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
448 return I->Dirty ? spillDirty : spillClean;
449 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000450 }
451
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000452 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000453 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000454 unsigned Cost = 0;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000455 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
456 unsigned Alias = *AI;
Eric Christopherd31df872011-04-13 00:20:59 +0000457 if (UsedInInstr.test(Alias))
458 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000459 switch (unsigned VirtReg = PhysRegState[Alias]) {
460 case regDisabled:
461 break;
462 case regFree:
463 ++Cost;
464 break;
465 case regReserved:
466 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000467 default: {
468 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
469 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
470 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000471 break;
472 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000473 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000474 }
475 return Cost;
476}
477
478
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000479/// assignVirtToPhysReg - This method updates local state so that we know
480/// that PhysReg is the proper container for VirtReg now. The physical
481/// register must not be used for anything else when this is called.
482///
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000483void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
484 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000485 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000486 PhysRegState[PhysReg] = LR.VirtReg;
487 assert(!LR.PhysReg && "Already assigned a physreg");
488 LR.PhysReg = PhysReg;
489}
490
491RAFast::LiveRegMap::iterator
492RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
493 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
494 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
495 assignVirtToPhysReg(*LRI, PhysReg);
496 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000497}
498
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000499/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000500RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
501 LiveRegMap::iterator LRI,
502 unsigned Hint) {
503 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000504
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000505 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
506 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000507
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000508 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000509
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000510 // Ignore invalid hints.
511 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000512 !RC->contains(Hint) || !RegClassInfo.isAllocatable(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000513 Hint = 0;
514
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000515 // Take hint when possible.
516 if (Hint) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000517 // Ignore the hint if we would have to spill a dirty register.
518 unsigned Cost = calcSpillCost(Hint);
519 if (Cost < spillDirty) {
520 if (Cost)
521 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000522 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
523 // That invalidates LRI, so run a new lookup for VirtReg.
524 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000525 }
526 }
527
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000528 ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000529
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000530 // First try to find a completely free register.
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000531 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000532 unsigned PhysReg = *I;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000533 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg)) {
534 assignVirtToPhysReg(*LRI, PhysReg);
535 return LRI;
536 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000537 }
538
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000539 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
540 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000541
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000542 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000543 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000544 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000545 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopher0b756342011-04-12 22:17:44 +0000546 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
547 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000548 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000549 if (Cost == 0) {
550 assignVirtToPhysReg(*LRI, *I);
551 return LRI;
552 }
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000553 if (Cost < BestCost)
554 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000555 }
556
557 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000558 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000559 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
560 // That invalidates LRI, so run a new lookup for VirtReg.
561 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000562 }
563
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000564 // Nothing we can do. Report an error and keep going with a bad allocation.
565 MI->emitError("ran out of registers during register allocation");
566 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000567 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000568}
569
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000570/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000571RAFast::LiveRegMap::iterator
572RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
573 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000574 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
575 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000576 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000577 bool New;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000578 tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000579 if (New) {
580 // If there is no hint, peek at the only use of this register.
581 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
582 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000583 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000584 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000585 if (UseMI.isCopyLike())
586 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000587 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000588 LRI = allocVirtReg(MI, LRI, Hint);
589 } else if (LRI->LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000590 // Redefining a live register - kill at the last use, unless it is this
591 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000592 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
593 addKillFlag(*LRI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000594 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000595 assert(LRI->PhysReg && "Register not assigned");
596 LRI->LastUse = MI;
597 LRI->LastOpNum = OpNum;
598 LRI->Dirty = true;
599 UsedInInstr.set(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000600 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000601}
602
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000603/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000604RAFast::LiveRegMap::iterator
605RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
606 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000607 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
608 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000609 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000610 bool New;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000611 tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000612 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000613 if (New) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000614 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000615 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000616 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000617 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000618 << PrintReg(LRI->PhysReg, TRI) << "\n");
619 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000620 ++NumLoads;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000621 } else if (LRI->Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000622 if (isLastUseOfLocalReg(MO)) {
623 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000624 if (MO.isUse())
625 MO.setIsKill();
626 else
627 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000628 } else if (MO.isKill()) {
629 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
630 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000631 } else if (MO.isDead()) {
632 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
633 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000634 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000635 } else if (MO.isKill()) {
636 // We must remove kill flags from uses of reloaded registers because the
637 // register would be killed immediately, and there might be a second use:
638 // %foo = OR %x<kill>, %x
639 // This would cause a second reload of %x into a different register.
640 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
641 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000642 } else if (MO.isDead()) {
643 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
644 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000645 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000646 assert(LRI->PhysReg && "Register not assigned");
647 LRI->LastUse = MI;
648 LRI->LastOpNum = OpNum;
649 UsedInInstr.set(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000650 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000651}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000652
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000653// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
654// subregs. This may invalidate any operand pointers.
655// Return true if the operand kills its register.
656bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
657 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000658 bool Dead = MO.isDead();
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000659 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000660 MO.setReg(PhysReg);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000661 return MO.isKill() || Dead;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000662 }
663
664 // Handle subregister index.
665 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
666 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000667
668 // A kill flag implies killing the full register. Add corresponding super
669 // register kill.
670 if (MO.isKill()) {
671 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000672 return true;
673 }
Jakob Stoklund Olesen4d108292012-05-14 21:10:25 +0000674
675 // A <def,read-undef> of a sub-register requires an implicit def of the full
676 // register.
677 if (MO.isDef() && MO.isUndef())
678 MI->addRegisterDefined(PhysReg, TRI);
679
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000680 return Dead;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000681}
682
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000683// Handle special instruction operand like early clobbers and tied ops when
684// there are additional physreg defines.
685void RAFast::handleThroughOperands(MachineInstr *MI,
686 SmallVectorImpl<unsigned> &VirtDead) {
687 DEBUG(dbgs() << "Scanning for through registers:");
688 SmallSet<unsigned, 8> ThroughRegs;
689 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
690 MachineOperand &MO = MI->getOperand(i);
691 if (!MO.isReg()) continue;
692 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000693 if (!TargetRegisterInfo::isVirtualRegister(Reg))
694 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000695 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
696 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000697 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000698 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000699 }
700 }
701
702 // If any physreg defines collide with preallocated through registers,
703 // we must spill and reallocate.
704 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
705 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
706 MachineOperand &MO = MI->getOperand(i);
707 if (!MO.isReg() || !MO.isDef()) continue;
708 unsigned Reg = MO.getReg();
709 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +0000710 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
711 UsedInInstr.set(*AI);
712 if (ThroughRegs.count(PhysRegState[*AI]))
713 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000714 }
715 }
716
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000717 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola254a1322011-11-22 06:27:18 +0000718 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000719 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
720 MachineOperand &MO = MI->getOperand(i);
721 if (!MO.isReg()) continue;
722 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000723 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000724 if (MO.isUse()) {
725 unsigned DefIdx = 0;
726 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
727 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
728 << DefIdx << ".\n");
729 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000730 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000731 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000732 // Note: we don't update the def operand yet. That would cause the normal
733 // def-scan to attempt spilling.
734 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
735 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
736 // Reload the register, but don't assign to the operand just yet.
737 // That would confuse the later phys-def processing pass.
738 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000739 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000740 }
741 }
742
Rafael Espindola254a1322011-11-22 06:27:18 +0000743 DEBUG(dbgs() << "Allocating early clobbers.\n");
744 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
745 MachineOperand &MO = MI->getOperand(i);
746 if (!MO.isReg()) continue;
747 unsigned Reg = MO.getReg();
748 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
749 if (!MO.isEarlyClobber())
750 continue;
751 // Note: defineVirtReg may invalidate MO.
752 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000753 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola254a1322011-11-22 06:27:18 +0000754 if (setPhysReg(MI, i, PhysReg))
755 VirtDead.push_back(Reg);
756 }
757
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000758 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000759 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000760 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
761 MachineOperand &MO = MI->getOperand(i);
762 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
763 unsigned Reg = MO.getReg();
764 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000765 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
766 << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000767 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000768 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000769
770 // Also mark PartialDefs as used to avoid reallocation.
771 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
772 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000773}
774
Andrew Trickb3d58472012-01-31 05:55:32 +0000775/// addRetOperand - ensure that a return instruction has an operand for each
776/// value live out of the function.
777///
778/// Things marked both call and return are tail calls; do not do this for them.
779/// The tail callee need not take the same registers as input that it produces
780/// as output, and there are dependencies for its input registers elsewhere.
781///
782/// FIXME: This should be done as part of instruction selection, and this helper
783/// should be deleted. Until then, we use custom logic here to create the proper
784/// operand under all circumstances. We can't use addRegisterKilled because that
785/// doesn't make sense for undefined values. We can't simply avoid calling it
786/// for undefined values, because we must ensure that the operand always exists.
787void RAFast::addRetOperands(MachineBasicBlock *MBB) {
788 if (MBB->empty() || !MBB->back().isReturn() || MBB->back().isCall())
789 return;
790
791 MachineInstr *MI = &MBB->back();
792
793 for (MachineRegisterInfo::liveout_iterator
794 I = MBB->getParent()->getRegInfo().liveout_begin(),
795 E = MBB->getParent()->getRegInfo().liveout_end(); I != E; ++I) {
796 unsigned Reg = *I;
797 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
798 "Cannot have a live-out virtual register.");
799
800 bool hasDef = PhysRegState[Reg] == regReserved;
801
802 // Check if this register already has an operand.
803 bool Found = false;
804 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
805 MachineOperand &MO = MI->getOperand(i);
806 if (!MO.isReg() || !MO.isUse())
807 continue;
808
809 unsigned OperReg = MO.getReg();
Andrew Trickab78e202012-01-31 18:54:19 +0000810 if (!TargetRegisterInfo::isPhysicalRegister(OperReg))
811 continue;
812
813 if (OperReg == Reg || TRI->isSuperRegister(OperReg, Reg)) {
814 // If the ret already has an operand for this physreg or a superset,
815 // don't duplicate it. Set the kill flag if the value is defined.
816 if (hasDef && !MO.isKill())
817 MO.setIsKill();
818 Found = true;
819 break;
Andrew Trickb3d58472012-01-31 05:55:32 +0000820 }
821 }
822 if (!Found)
823 MI->addOperand(MachineOperand::CreateReg(Reg,
824 false /*IsDef*/,
825 true /*IsImp*/,
826 hasDef/*IsKill*/));
827 }
828}
829
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000830void RAFast::AllocateBasicBlock() {
831 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000832
833 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000834 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000835
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000836 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000837
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000838 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000839 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
840 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000841 if (RegClassInfo.isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000842 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000843
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000844 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000845 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000846
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000847 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000848 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000849 MachineInstr *MI = MII++;
Evan Chenge837dea2011-06-28 19:10:37 +0000850 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000851 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000852 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000853 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
854 if (PhysRegState[Reg] == regDisabled) continue;
855 dbgs() << " " << TRI->getName(Reg);
856 switch(PhysRegState[Reg]) {
857 case regFree:
858 break;
859 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000860 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000861 break;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000862 default: {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000863 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000864 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
865 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
866 if (I->Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000867 dbgs() << "*";
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000868 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000869 break;
870 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000871 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000872 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000873 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000874 // Check that LiveVirtRegs is the inverse.
875 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
876 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000877 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000878 "Bad map key");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000879 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000880 "Bad map value");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000881 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000882 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000883 });
884
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000885 // Debug values are not allowed to change codegen in any way.
886 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000887 bool ScanDbgValue = true;
888 while (ScanDbgValue) {
889 ScanDbgValue = false;
890 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
891 MachineOperand &MO = MI->getOperand(i);
892 if (!MO.isReg()) continue;
893 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000894 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000895 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Patel58b81762010-07-19 23:25:39 +0000896 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000897 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000898 else {
Devang Patel58b81762010-07-19 23:25:39 +0000899 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000900 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000901 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000902 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000903 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000904 }
Devang Patel58b81762010-07-19 23:25:39 +0000905 else {
906 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000907 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000908 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000909 MI->getOperand(MI->getNumOperands()-1).getMetadata();
910 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000911 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000912 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000913 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
914 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000915 MachineBasicBlock *MBB = MI->getParent();
916 MBB->insert(MBB->erase(MI), NewDV);
917 // Scan NewDV operands from the beginning.
918 MI = NewDV;
919 ScanDbgValue = true;
920 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000921 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000922 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000923 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000924 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000925 }
Devang Patel58b81762010-07-19 23:25:39 +0000926 }
Devang Patel7a029b62010-07-09 21:48:31 +0000927 }
Devang Pateld2df64f2011-11-15 21:03:58 +0000928 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel7a029b62010-07-09 21:48:31 +0000929 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000930 }
931 // Next instruction.
932 continue;
933 }
934
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000935 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000936 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000937 if (MI->isCopy()) {
938 CopyDst = MI->getOperand(0).getReg();
939 CopySrc = MI->getOperand(1).getReg();
940 CopyDstSub = MI->getOperand(0).getSubReg();
941 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000942 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000943
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000944 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000945 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000946
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000947 // First scan.
948 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000949 // Find the end of the virtreg operands
950 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000951 bool hasTiedOps = false;
952 bool hasEarlyClobbers = false;
953 bool hasPartialRedefs = false;
954 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000955 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
956 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000957 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000958 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000959 if (!Reg) continue;
960 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
961 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000962 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000963 hasTiedOps = hasTiedOps ||
Evan Chenge837dea2011-06-28 19:10:37 +0000964 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000965 } else {
966 if (MO.isEarlyClobber())
967 hasEarlyClobbers = true;
968 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
969 hasPartialRedefs = true;
970 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000971 continue;
972 }
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000973 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000974 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000975 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000976 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000977 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
978 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000979 hasEarlyClobbers = true;
980 } else
981 hasPhysDefs = true;
982 }
983
984 // The instruction may have virtual register operands that must be allocated
985 // the same register at use-time and def-time: early clobbers and tied
986 // operands. If there are also physical defs, these registers must avoid
987 // both physical defs and uses, making them more constrained than normal
988 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000989 // Similarly, if there are multiple defs and tied operands, we must make
990 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000991 // We didn't detect inline asm tied operands above, so just make this extra
992 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000993 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Chenge837dea2011-06-28 19:10:37 +0000994 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000995 handleThroughOperands(MI, VirtDead);
996 // Don't attempt coalescing when we have funny stuff going on.
997 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000998 // Pretend we have early clobbers so the use operands get marked below.
999 // This is not necessary for the common case of a single tied use.
1000 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001001 }
1002
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001003 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001004 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +00001005 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001006 MachineOperand &MO = MI->getOperand(i);
1007 if (!MO.isReg()) continue;
1008 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001009 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001010 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001011 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001012 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001013 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001014 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001015 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001016 }
1017 }
1018
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001019 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001020
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001021 // Track registers defined by instruction - early clobbers and tied uses at
1022 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +00001023 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001024 if (hasEarlyClobbers) {
1025 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1026 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001027 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001028 unsigned Reg = MO.getReg();
1029 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001030 // Look for physreg defs and tied uses.
1031 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +00001032 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
1033 UsedInInstr.set(*AI);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001034 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001035 }
1036
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001037 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001038 if (MI->isCall()) {
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001039 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +00001040 // exception is thrown, the landing pad is going to expect to find
1041 // registers in their spill slots, and 2. we don't have to wade through
1042 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001043 DefOpEnd = VirtOpEnd;
1044 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1045 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001046
1047 // The imp-defs are skipped below, but we still need to mark those
1048 // registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +00001049 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001050 }
1051
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001052 // Third scan.
1053 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001054 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001055 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +00001056 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1057 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001058 unsigned Reg = MO.getReg();
1059
1060 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +00001061 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001062 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1063 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001064 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001065 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001066 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001067 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001068 if (setPhysReg(MI, i, PhysReg)) {
1069 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001070 CopyDst = 0; // cancel coalescing;
1071 } else
1072 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001073 }
1074
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001075 // Kill dead defs after the scan to ensure that multiple defs of the same
1076 // register are allocated identically. We didn't need to do this for uses
1077 // because we are crerating our own kill flags, and they are always at the
1078 // last use.
1079 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1080 killVirtReg(VirtDead[i]);
1081 VirtDead.clear();
1082
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001083 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001084
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001085 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1086 DEBUG(dbgs() << "-- coalescing: " << *MI);
1087 Coalesced.push_back(MI);
1088 } else {
1089 DEBUG(dbgs() << "<< " << *MI);
1090 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001091 }
1092
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001093 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001094 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1095 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001096
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001097 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001098 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001099 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001100 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001101 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001102
Andrew Trickb3d58472012-01-31 05:55:32 +00001103 // addRetOperands must run after we've seen all defs in this block.
1104 addRetOperands(MBB);
1105
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001106 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001107}
1108
1109/// runOnMachineFunction - Register allocate the whole function
1110///
1111bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001112 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1113 << "********** Function: "
1114 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001115 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001116 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001117 TM = &Fn.getTarget();
1118 TRI = TM->getRegisterInfo();
1119 TII = TM->getInstrInfo();
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +00001120 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001121 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001122 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001123
Andrew Trick8dd26252012-02-10 04:10:36 +00001124 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1125
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001126 // initialize the virtual->physical register map to have a 'null'
1127 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001128 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001129 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001130
1131 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001132 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1133 MBBi != MBBe; ++MBBi) {
1134 MBB = &*MBBi;
1135 AllocateBasicBlock();
1136 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001137
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001138 // Add the clobber lists for all the instructions we skipped earlier.
Evan Chenge837dea2011-06-28 19:10:37 +00001139 for (SmallPtrSet<const MCInstrDesc*, 4>::const_iterator
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001140 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
Craig Topperfac25982012-03-08 08:22:45 +00001141 if (const uint16_t *Defs = (*I)->getImplicitDefs())
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001142 while (*Defs)
1143 MRI->setPhysRegUsed(*Defs++);
1144
Andrew Trick19273ae2012-02-21 04:51:23 +00001145 // All machine operands and other references to virtual registers have been
1146 // replaced. Remove the virtual registers.
1147 MRI->clearVirtRegs();
1148
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001149 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001150 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001151 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001152 return true;
1153}
1154
1155FunctionPass *llvm::createFastRegisterAllocator() {
1156 return new RAFast();
1157}