blob: 1a07145636985ba19709c630044211c2bbffdd8b [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"
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
80 unsigned getSparseSetKey() const {
81 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) {
204 // Check for non-debug uses or defs following MO.
205 // This is the most likely way to fail - fast path it.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000206 MachineOperand *Next = &MO;
207 while ((Next = Next->getNextOperandForReg()))
208 if (!Next->isDebug())
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000209 return false;
210
211 // If the register has ever been spilled or reloaded, we conservatively assume
212 // it is a global register used in multiple blocks.
213 if (StackSlotForVirtReg[MO.getReg()] != -1)
214 return false;
215
216 // Check that the use/def chain has exactly one operand - MO.
217 return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO;
218}
219
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000220/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000221void RAFast::addKillFlag(const LiveReg &LR) {
222 if (!LR.LastUse) return;
223 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000224 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
225 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000226 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000227 else
228 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
229 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000230}
231
232/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000233void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000234 addKillFlag(*LRI);
235 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg && "Broken RegState mapping");
236 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000237 // Erase from LiveVirtRegs unless we're spilling in bulk.
238 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000239 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000240}
241
242/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000243void RAFast::killVirtReg(unsigned VirtReg) {
244 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
245 "killVirtReg needs a virtual register");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000246 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000247 if (LRI != LiveVirtRegs.end())
248 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000249}
250
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000251/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000252/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000253void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000254 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
255 "Spilling a physical register is illegal!");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000256 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000257 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
258 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000259}
260
261/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000262void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000263 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000264 LiveReg &LR = *LRI;
265 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000266
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000267 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000268 // If this physreg is used by the instruction, we want to kill it on the
269 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000270 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000271 LR.Dirty = false;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000272 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000273 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000274 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
275 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000276 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000277 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000278 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000279
Jim Grosbach07cb6892010-09-01 19:16:29 +0000280 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000281 // identify spilled location as the place to find corresponding variable's
282 // value.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000283 SmallVector<MachineInstr *, 4> &LRIDbgValues =
284 LiveDbgValueMap[LRI->VirtReg];
Devang Patel72d9b0e2011-06-21 22:36:03 +0000285 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
286 MachineInstr *DBG = LRIDbgValues[li];
Jim Grosbach07cb6892010-09-01 19:16:29 +0000287 const MDNode *MDPtr =
Devang Patel459a36b2010-08-04 18:42:02 +0000288 DBG->getOperand(DBG->getNumOperands()-1).getMetadata();
289 int64_t Offset = 0;
290 if (DBG->getOperand(1).isImm())
291 Offset = DBG->getOperand(1).getImm();
Devang Patel31defcf2010-08-06 00:26:18 +0000292 DebugLoc DL;
293 if (MI == MBB->end()) {
294 // If MI is at basic block end then use last instruction's location.
295 MachineBasicBlock::iterator EI = MI;
296 DL = (--EI)->getDebugLoc();
297 }
298 else
299 DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000300 if (MachineInstr *NewDV =
Devang Patel459a36b2010-08-04 18:42:02 +0000301 TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) {
302 MachineBasicBlock *MBB = DBG->getParent();
303 MBB->insert(MI, NewDV);
304 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Patel459a36b2010-08-04 18:42:02 +0000305 }
306 }
Devang Patel6f373a82011-06-21 23:02:36 +0000307 // Now this register is spilled there is should not be any DBG_VALUE pointing
308 // to this register because they are all pointing to spilled value now.
309 LRIDbgValues.clear();
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000310 if (SpillKill)
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000311 LR.LastUse = 0; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000312 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000313 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000314}
315
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000316/// spillAll - Spill all dirty virtregs without killing them.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000317void RAFast::spillAll(MachineInstr *MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000318 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000319 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000320 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
321 // of spilling here is deterministic, if arbitrary.
322 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
323 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000324 spillVirtReg(MI, i);
325 LiveVirtRegs.clear();
326 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000327}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000328
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000329/// usePhysReg - Handle the direct use of a physical register.
330/// Check that the register is not used by a virtreg.
331/// Kill the physreg, marking it free.
332/// This may add implicit kills to MO->getParent() and invalidate MO.
333void RAFast::usePhysReg(MachineOperand &MO) {
334 unsigned PhysReg = MO.getReg();
335 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
336 "Bad usePhysReg operand");
337
338 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000339 case regDisabled:
340 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000341 case regReserved:
342 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000343 // Fall through
344 case regFree:
345 UsedInInstr.set(PhysReg);
346 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000347 return;
348 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000349 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000350 // wanted has been clobbered.
351 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000352 }
353
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000354 // Maybe a superregister is reserved?
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000355 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
356 unsigned Alias = *AS; ++AS) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000357 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000358 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000359 break;
360 case regReserved:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000361 assert(TRI->isSuperRegister(PhysReg, Alias) &&
362 "Instruction is not using a subregister of a reserved register");
363 // Leave the superregister in the working set.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000364 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000365 UsedInInstr.set(Alias);
366 MO.getParent()->addRegisterKilled(Alias, TRI, true);
367 return;
368 case regFree:
369 if (TRI->isSuperRegister(PhysReg, Alias)) {
370 // Leave the superregister in the working set.
371 UsedInInstr.set(Alias);
372 MO.getParent()->addRegisterKilled(Alias, TRI, true);
373 return;
374 }
375 // Some other alias was in the working set - clear it.
376 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000377 break;
378 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000379 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000380 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000381 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000382
383 // All aliases are disabled, bring register into working set.
384 PhysRegState[PhysReg] = regFree;
385 UsedInInstr.set(PhysReg);
386 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000387}
388
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000389/// definePhysReg - Mark PhysReg as reserved or free after spilling any
390/// virtregs. This is very similar to defineVirtReg except the physreg is
391/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000392void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
393 RegState NewState) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000394 UsedInInstr.set(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000395 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
396 case regDisabled:
397 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000398 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000399 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000400 // Fall through.
401 case regFree:
402 case regReserved:
403 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000404 return;
405 }
406
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000407 // This is a disabled register, disable all aliases.
408 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000409 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
410 unsigned Alias = *AS; ++AS) {
411 switch (unsigned VirtReg = PhysRegState[Alias]) {
412 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000413 break;
414 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000415 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000416 // Fall through.
417 case regFree:
418 case regReserved:
419 PhysRegState[Alias] = regDisabled;
420 if (TRI->isSuperRegister(PhysReg, Alias))
421 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000422 break;
423 }
424 }
425}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000426
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000427
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000428// calcSpillCost - Return the cost of spilling clearing out PhysReg and
429// aliases so it is free for allocation.
430// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
431// can be allocated directly.
432// Returns spillImpossible when PhysReg or an alias can't be spilled.
433unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Eric Christopher0b756342011-04-12 22:17:44 +0000434 if (UsedInInstr.test(PhysReg)) {
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000435 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000436 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000437 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000438 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
439 case regDisabled:
440 break;
441 case regFree:
442 return 0;
443 case regReserved:
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000444 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
445 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000446 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000447 default: {
448 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
449 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
450 return I->Dirty ? spillDirty : spillClean;
451 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000452 }
453
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000454 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000455 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000456 unsigned Cost = 0;
457 for (const unsigned *AS = TRI->getAliasSet(PhysReg);
458 unsigned Alias = *AS; ++AS) {
Eric Christopherd31df872011-04-13 00:20:59 +0000459 if (UsedInInstr.test(Alias))
460 return spillImpossible;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000461 switch (unsigned VirtReg = PhysRegState[Alias]) {
462 case regDisabled:
463 break;
464 case regFree:
465 ++Cost;
466 break;
467 case regReserved:
468 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000469 default: {
470 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
471 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
472 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000473 break;
474 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000475 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000476 }
477 return Cost;
478}
479
480
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000481/// assignVirtToPhysReg - This method updates local state so that we know
482/// that PhysReg is the proper container for VirtReg now. The physical
483/// register must not be used for anything else when this is called.
484///
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000485void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
486 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000487 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000488 PhysRegState[PhysReg] = LR.VirtReg;
489 assert(!LR.PhysReg && "Already assigned a physreg");
490 LR.PhysReg = PhysReg;
491}
492
493RAFast::LiveRegMap::iterator
494RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
495 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
496 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
497 assignVirtToPhysReg(*LRI, PhysReg);
498 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000499}
500
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000501/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000502RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
503 LiveRegMap::iterator LRI,
504 unsigned Hint) {
505 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000506
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000507 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
508 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000509
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000510 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000511
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000512 // Ignore invalid hints.
513 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000514 !RC->contains(Hint) || !RegClassInfo.isAllocatable(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000515 Hint = 0;
516
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000517 // Take hint when possible.
518 if (Hint) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000519 // Ignore the hint if we would have to spill a dirty register.
520 unsigned Cost = calcSpillCost(Hint);
521 if (Cost < spillDirty) {
522 if (Cost)
523 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000524 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
525 // That invalidates LRI, so run a new lookup for VirtReg.
526 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000527 }
528 }
529
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000530 ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000531
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000532 // First try to find a completely free register.
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000533 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000534 unsigned PhysReg = *I;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000535 if (PhysRegState[PhysReg] == regFree && !UsedInInstr.test(PhysReg)) {
536 assignVirtToPhysReg(*LRI, PhysReg);
537 return LRI;
538 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000539 }
540
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000541 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
542 << RC->getName() << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000543
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000544 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +0000545 for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000546 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000547 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopher0b756342011-04-12 22:17:44 +0000548 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
549 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000550 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000551 if (Cost == 0) {
552 assignVirtToPhysReg(*LRI, *I);
553 return LRI;
554 }
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000555 if (Cost < BestCost)
556 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000557 }
558
559 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000560 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000561 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
562 // That invalidates LRI, so run a new lookup for VirtReg.
563 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000564 }
565
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000566 // Nothing we can do. Report an error and keep going with a bad allocation.
567 MI->emitError("ran out of registers during register allocation");
568 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000569 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000570}
571
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000572/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000573RAFast::LiveRegMap::iterator
574RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
575 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000576 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
577 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000578 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000579 bool New;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000580 tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000581 if (New) {
582 // If there is no hint, peek at the only use of this register.
583 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
584 MRI->hasOneNonDBGUse(VirtReg)) {
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000585 const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000586 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000587 if (UseMI.isCopyLike())
588 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000589 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000590 LRI = allocVirtReg(MI, LRI, Hint);
591 } else if (LRI->LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000592 // Redefining a live register - kill at the last use, unless it is this
593 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000594 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
595 addKillFlag(*LRI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000596 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000597 assert(LRI->PhysReg && "Register not assigned");
598 LRI->LastUse = MI;
599 LRI->LastOpNum = OpNum;
600 LRI->Dirty = true;
601 UsedInInstr.set(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000602 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000603}
604
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000605/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000606RAFast::LiveRegMap::iterator
607RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
608 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000609 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
610 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000611 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000612 bool New;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000613 tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000614 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000615 if (New) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000616 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000617 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000618 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000619 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000620 << PrintReg(LRI->PhysReg, TRI) << "\n");
621 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000622 ++NumLoads;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000623 } else if (LRI->Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000624 if (isLastUseOfLocalReg(MO)) {
625 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000626 if (MO.isUse())
627 MO.setIsKill();
628 else
629 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000630 } else if (MO.isKill()) {
631 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
632 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000633 } else if (MO.isDead()) {
634 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
635 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000636 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000637 } else if (MO.isKill()) {
638 // We must remove kill flags from uses of reloaded registers because the
639 // register would be killed immediately, and there might be a second use:
640 // %foo = OR %x<kill>, %x
641 // This would cause a second reload of %x into a different register.
642 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
643 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000644 } else if (MO.isDead()) {
645 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
646 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000647 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000648 assert(LRI->PhysReg && "Register not assigned");
649 LRI->LastUse = MI;
650 LRI->LastOpNum = OpNum;
651 UsedInInstr.set(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000652 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000653}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000654
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000655// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
656// subregs. This may invalidate any operand pointers.
657// Return true if the operand kills its register.
658bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
659 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000660 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000661 MO.setReg(PhysReg);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000662 return MO.isKill() || MO.isDead();
663 }
664
665 // Handle subregister index.
666 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
667 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000668
669 // A kill flag implies killing the full register. Add corresponding super
670 // register kill.
671 if (MO.isKill()) {
672 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000673 return true;
674 }
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000675 return MO.isDead();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000676}
677
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000678// Handle special instruction operand like early clobbers and tied ops when
679// there are additional physreg defines.
680void RAFast::handleThroughOperands(MachineInstr *MI,
681 SmallVectorImpl<unsigned> &VirtDead) {
682 DEBUG(dbgs() << "Scanning for through registers:");
683 SmallSet<unsigned, 8> ThroughRegs;
684 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
685 MachineOperand &MO = MI->getOperand(i);
686 if (!MO.isReg()) continue;
687 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000688 if (!TargetRegisterInfo::isVirtualRegister(Reg))
689 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000690 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
691 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000692 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000693 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000694 }
695 }
696
697 // If any physreg defines collide with preallocated through registers,
698 // we must spill and reallocate.
699 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
700 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
701 MachineOperand &MO = MI->getOperand(i);
702 if (!MO.isReg() || !MO.isDef()) continue;
703 unsigned Reg = MO.getReg();
704 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
705 UsedInInstr.set(Reg);
706 if (ThroughRegs.count(PhysRegState[Reg]))
707 definePhysReg(MI, Reg, regFree);
708 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
709 UsedInInstr.set(*AS);
710 if (ThroughRegs.count(PhysRegState[*AS]))
711 definePhysReg(MI, *AS, regFree);
712 }
713 }
714
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000715 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola254a1322011-11-22 06:27:18 +0000716 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000717 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
718 MachineOperand &MO = MI->getOperand(i);
719 if (!MO.isReg()) continue;
720 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000721 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000722 if (MO.isUse()) {
723 unsigned DefIdx = 0;
724 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
725 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
726 << DefIdx << ".\n");
727 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000728 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000729 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000730 // Note: we don't update the def operand yet. That would cause the normal
731 // def-scan to attempt spilling.
732 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
733 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
734 // Reload the register, but don't assign to the operand just yet.
735 // That would confuse the later phys-def processing pass.
736 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000737 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000738 }
739 }
740
Rafael Espindola254a1322011-11-22 06:27:18 +0000741 DEBUG(dbgs() << "Allocating early clobbers.\n");
742 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
743 MachineOperand &MO = MI->getOperand(i);
744 if (!MO.isReg()) continue;
745 unsigned Reg = MO.getReg();
746 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
747 if (!MO.isEarlyClobber())
748 continue;
749 // Note: defineVirtReg may invalidate MO.
750 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000751 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola254a1322011-11-22 06:27:18 +0000752 if (setPhysReg(MI, i, PhysReg))
753 VirtDead.push_back(Reg);
754 }
755
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000756 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jim Grosbachee726512010-09-03 21:45:15 +0000757 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000758 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
759 MachineOperand &MO = MI->getOperand(i);
760 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
761 unsigned Reg = MO.getReg();
762 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000763 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
764 << " as used in instr\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000765 UsedInInstr.set(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000766 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000767
768 // Also mark PartialDefs as used to avoid reallocation.
769 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
770 UsedInInstr.set(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000771}
772
Andrew Trickb3d58472012-01-31 05:55:32 +0000773/// addRetOperand - ensure that a return instruction has an operand for each
774/// value live out of the function.
775///
776/// Things marked both call and return are tail calls; do not do this for them.
777/// The tail callee need not take the same registers as input that it produces
778/// as output, and there are dependencies for its input registers elsewhere.
779///
780/// FIXME: This should be done as part of instruction selection, and this helper
781/// should be deleted. Until then, we use custom logic here to create the proper
782/// operand under all circumstances. We can't use addRegisterKilled because that
783/// doesn't make sense for undefined values. We can't simply avoid calling it
784/// for undefined values, because we must ensure that the operand always exists.
785void RAFast::addRetOperands(MachineBasicBlock *MBB) {
786 if (MBB->empty() || !MBB->back().isReturn() || MBB->back().isCall())
787 return;
788
789 MachineInstr *MI = &MBB->back();
790
791 for (MachineRegisterInfo::liveout_iterator
792 I = MBB->getParent()->getRegInfo().liveout_begin(),
793 E = MBB->getParent()->getRegInfo().liveout_end(); I != E; ++I) {
794 unsigned Reg = *I;
795 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
796 "Cannot have a live-out virtual register.");
797
798 bool hasDef = PhysRegState[Reg] == regReserved;
799
800 // Check if this register already has an operand.
801 bool Found = false;
802 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
803 MachineOperand &MO = MI->getOperand(i);
804 if (!MO.isReg() || !MO.isUse())
805 continue;
806
807 unsigned OperReg = MO.getReg();
Andrew Trickab78e202012-01-31 18:54:19 +0000808 if (!TargetRegisterInfo::isPhysicalRegister(OperReg))
809 continue;
810
811 if (OperReg == Reg || TRI->isSuperRegister(OperReg, Reg)) {
812 // If the ret already has an operand for this physreg or a superset,
813 // don't duplicate it. Set the kill flag if the value is defined.
814 if (hasDef && !MO.isKill())
815 MO.setIsKill();
816 Found = true;
817 break;
Andrew Trickb3d58472012-01-31 05:55:32 +0000818 }
819 }
820 if (!Found)
821 MI->addOperand(MachineOperand::CreateReg(Reg,
822 false /*IsDef*/,
823 true /*IsImp*/,
824 hasDef/*IsKill*/));
825 }
826}
827
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000828void RAFast::AllocateBasicBlock() {
829 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000830
831 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000832 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000833
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000834 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000835
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000836 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000837 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
838 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000839 if (RegClassInfo.isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000840 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000841
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000842 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000843 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000844
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000845 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000846 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000847 MachineInstr *MI = MII++;
Evan Chenge837dea2011-06-28 19:10:37 +0000848 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000849 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000850 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000851 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
852 if (PhysRegState[Reg] == regDisabled) continue;
853 dbgs() << " " << TRI->getName(Reg);
854 switch(PhysRegState[Reg]) {
855 case regFree:
856 break;
857 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000858 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000859 break;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000860 default: {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000861 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000862 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
863 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
864 if (I->Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000865 dbgs() << "*";
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000866 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000867 break;
868 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000869 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000870 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000871 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000872 // Check that LiveVirtRegs is the inverse.
873 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
874 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000875 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000876 "Bad map key");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000877 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000878 "Bad map value");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000879 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000880 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000881 });
882
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000883 // Debug values are not allowed to change codegen in any way.
884 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000885 bool ScanDbgValue = true;
886 while (ScanDbgValue) {
887 ScanDbgValue = false;
888 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
889 MachineOperand &MO = MI->getOperand(i);
890 if (!MO.isReg()) continue;
891 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000892 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000893 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Patel58b81762010-07-19 23:25:39 +0000894 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000895 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000896 else {
Devang Patel58b81762010-07-19 23:25:39 +0000897 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000898 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000899 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000900 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000901 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000902 }
Devang Patel58b81762010-07-19 23:25:39 +0000903 else {
904 // Modify DBG_VALUE now that the value is in a spill slot.
Devang Patel459a36b2010-08-04 18:42:02 +0000905 int64_t Offset = MI->getOperand(1).getImm();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000906 const MDNode *MDPtr =
Devang Patel58b81762010-07-19 23:25:39 +0000907 MI->getOperand(MI->getNumOperands()-1).getMetadata();
908 DebugLoc DL = MI->getDebugLoc();
Jim Grosbach07cb6892010-09-01 19:16:29 +0000909 if (MachineInstr *NewDV =
Devang Patel58b81762010-07-19 23:25:39 +0000910 TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000911 DEBUG(dbgs() << "Modifying debug info due to spill:" <<
912 "\t" << *MI);
Devang Patel58b81762010-07-19 23:25:39 +0000913 MachineBasicBlock *MBB = MI->getParent();
914 MBB->insert(MBB->erase(MI), NewDV);
915 // Scan NewDV operands from the beginning.
916 MI = NewDV;
917 ScanDbgValue = true;
918 break;
Devang Patel4bafda92010-09-10 20:32:09 +0000919 } else {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000920 // We can't allocate a physreg for a DebugValue; sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000921 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000922 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000923 }
Devang Patel58b81762010-07-19 23:25:39 +0000924 }
Devang Patel7a029b62010-07-09 21:48:31 +0000925 }
Devang Pateld2df64f2011-11-15 21:03:58 +0000926 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel7a029b62010-07-09 21:48:31 +0000927 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000928 }
929 // Next instruction.
930 continue;
931 }
932
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000933 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000934 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000935 if (MI->isCopy()) {
936 CopyDst = MI->getOperand(0).getReg();
937 CopySrc = MI->getOperand(1).getReg();
938 CopyDstSub = MI->getOperand(0).getSubReg();
939 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000940 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000941
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000942 // Track registers used by instruction.
Jim Grosbachee726512010-09-03 21:45:15 +0000943 UsedInInstr.reset();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000944
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000945 // First scan.
946 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000947 // Find the end of the virtreg operands
948 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000949 bool hasTiedOps = false;
950 bool hasEarlyClobbers = false;
951 bool hasPartialRedefs = false;
952 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000953 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
954 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000955 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000956 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000957 if (!Reg) continue;
958 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
959 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000960 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000961 hasTiedOps = hasTiedOps ||
Evan Chenge837dea2011-06-28 19:10:37 +0000962 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000963 } else {
964 if (MO.isEarlyClobber())
965 hasEarlyClobbers = true;
966 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
967 hasPartialRedefs = true;
968 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000969 continue;
970 }
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +0000971 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000972 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000973 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000974 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000975 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
976 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000977 hasEarlyClobbers = true;
978 } else
979 hasPhysDefs = true;
980 }
981
982 // The instruction may have virtual register operands that must be allocated
983 // the same register at use-time and def-time: early clobbers and tied
984 // operands. If there are also physical defs, these registers must avoid
985 // both physical defs and uses, making them more constrained than normal
986 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000987 // Similarly, if there are multiple defs and tied operands, we must make
988 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000989 // We didn't detect inline asm tied operands above, so just make this extra
990 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000991 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Chenge837dea2011-06-28 19:10:37 +0000992 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000993 handleThroughOperands(MI, VirtDead);
994 // Don't attempt coalescing when we have funny stuff going on.
995 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000996 // Pretend we have early clobbers so the use operands get marked below.
997 // This is not necessary for the common case of a single tied use.
998 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000999 }
1000
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001001 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001002 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +00001003 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001004 MachineOperand &MO = MI->getOperand(i);
1005 if (!MO.isReg()) continue;
1006 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001007 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001008 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001009 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001010 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001011 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001012 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001013 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001014 }
1015 }
1016
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001017 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +00001018
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001019 // Track registers defined by instruction - early clobbers and tied uses at
1020 // this point.
Jim Grosbachee726512010-09-03 21:45:15 +00001021 UsedInInstr.reset();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001022 if (hasEarlyClobbers) {
1023 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1024 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001025 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001026 unsigned Reg = MO.getReg();
1027 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001028 // Look for physreg defs and tied uses.
1029 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001030 UsedInInstr.set(Reg);
1031 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
1032 UsedInInstr.set(*AS);
1033 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001034 }
1035
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001036 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001037 if (MI->isCall()) {
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001038 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +00001039 // exception is thrown, the landing pad is going to expect to find
1040 // registers in their spill slots, and 2. we don't have to wade through
1041 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001042 DefOpEnd = VirtOpEnd;
1043 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1044 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001045
1046 // The imp-defs are skipped below, but we still need to mark those
1047 // registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +00001048 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001049 }
1050
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001051 // Third scan.
1052 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001053 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001054 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +00001055 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1056 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001057 unsigned Reg = MO.getReg();
1058
1059 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen448ab3a2011-06-02 23:41:40 +00001060 if (!RegClassInfo.isAllocatable(Reg)) continue;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001061 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1062 regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001063 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001064 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001065 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001066 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001067 if (setPhysReg(MI, i, PhysReg)) {
1068 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001069 CopyDst = 0; // cancel coalescing;
1070 } else
1071 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001072 }
1073
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001074 // Kill dead defs after the scan to ensure that multiple defs of the same
1075 // register are allocated identically. We didn't need to do this for uses
1076 // because we are crerating our own kill flags, and they are always at the
1077 // last use.
1078 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1079 killVirtReg(VirtDead[i]);
1080 VirtDead.clear();
1081
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001082 MRI->addPhysRegsUsed(UsedInInstr);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001083
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001084 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1085 DEBUG(dbgs() << "-- coalescing: " << *MI);
1086 Coalesced.push_back(MI);
1087 } else {
1088 DEBUG(dbgs() << "<< " << *MI);
1089 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001090 }
1091
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001092 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001093 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1094 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001095
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001096 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001097 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001098 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001099 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001100 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001101
Andrew Trickb3d58472012-01-31 05:55:32 +00001102 // addRetOperands must run after we've seen all defs in this block.
1103 addRetOperands(MBB);
1104
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001105 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001106}
1107
1108/// runOnMachineFunction - Register allocate the whole function
1109///
1110bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001111 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1112 << "********** Function: "
1113 << ((Value*)Fn.getFunction())->getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001114 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001115 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001116 TM = &Fn.getTarget();
1117 TRI = TM->getRegisterInfo();
1118 TII = TM->getInstrInfo();
Jakob Stoklund Olesend9e5c762012-01-05 00:26:49 +00001119 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001120 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001121 UsedInInstr.resize(TRI->getNumRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001122
Andrew Trick8dd26252012-02-10 04:10:36 +00001123 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1124
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001125 // initialize the virtual->physical register map to have a 'null'
1126 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001127 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001128 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001129
1130 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001131 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1132 MBBi != MBBe; ++MBBi) {
1133 MBB = &*MBBi;
1134 AllocateBasicBlock();
1135 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001136
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001137 // Add the clobber lists for all the instructions we skipped earlier.
Evan Chenge837dea2011-06-28 19:10:37 +00001138 for (SmallPtrSet<const MCInstrDesc*, 4>::const_iterator
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001139 I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I)
1140 if (const unsigned *Defs = (*I)->getImplicitDefs())
1141 while (*Defs)
1142 MRI->setPhysRegUsed(*Defs++);
1143
Andrew Trick19273ae2012-02-21 04:51:23 +00001144 // All machine operands and other references to virtual registers have been
1145 // replaced. Remove the virtual registers.
1146 MRI->clearVirtRegs();
1147
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001148 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001149 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001150 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001151 return true;
1152}
1153
1154FunctionPass *llvm::createFastRegisterAllocator() {
1155 return new RAFast();
1156}