blob: c621414002674237d57ea8ee37bc5eee6caf986a [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
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000015#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/IndexedMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000019#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000021#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000022#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/RegAllocRegistry.h"
29#include "llvm/CodeGen/RegisterClassInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/BasicBlock.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35#include "llvm/Target/TargetInstrInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080036#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000037#include <algorithm>
38using namespace llvm;
39
Stephen Hinesdce4a402014-05-29 02:49:00 -070040#define DEBUG_TYPE "regalloc"
41
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000042STATISTIC(NumStores, "Number of stores added");
43STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +000044STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000045
46static RegisterRegAlloc
47 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
48
49namespace {
50 class RAFast : public MachineFunctionPass {
51 public:
52 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000053 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Andrew Trick8dd26252012-02-10 04:10:36 +000054 isBulkSpilling(false) {}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000055 private:
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000056 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)
Stephen Hinesdce4a402014-05-29 02:49:00 -070078 : LastUse(nullptr), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false){}
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000079
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
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000116 // Set of register units.
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000117 typedef SparseSet<unsigned> UsedInInstrSet;
118
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000119 // Set of register units that are used in the current instruction, and so
120 // cannot be allocated.
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000121 UsedInInstrSet UsedInInstr;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000122
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000123 // Mark a physreg as used in this instruction.
124 void markRegUsedInInstr(unsigned PhysReg) {
125 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
126 UsedInInstr.insert(*Units);
127 }
128
129 // Check if a physreg or any of its aliases are used in this instruction.
130 bool isRegUsedInInstr(unsigned PhysReg) const {
131 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
132 if (UsedInInstr.count(*Units))
133 return true;
134 return false;
135 }
136
Jim Grosbach07cb6892010-09-01 19:16:29 +0000137 // SkippedInstrs - Descriptors of instructions whose clobber list was
138 // ignored because all registers were spilled. It is still necessary to
139 // mark all the clobbered registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +0000140 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +0000141
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000142 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
143 // completely after spilling all live registers. LiveRegMap entries should
144 // not be erased.
145 bool isBulkSpilling;
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000146
Stephen Hines36b56882014-04-23 16:57:46 -0700147 enum : unsigned {
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000148 spillClean = 1,
149 spillDirty = 100,
150 spillImpossible = ~0u
151 };
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000152 public:
Stephen Hines36b56882014-04-23 16:57:46 -0700153 const char *getPassName() const override {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000154 return "Fast Register Allocator";
155 }
156
Stephen Hines36b56882014-04-23 16:57:46 -0700157 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000158 AU.setPreservesCFG();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000159 MachineFunctionPass::getAnalysisUsage(AU);
160 }
161
162 private:
Stephen Hines36b56882014-04-23 16:57:46 -0700163 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000164 void AllocateBasicBlock();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000165 void handleThroughOperands(MachineInstr *MI,
166 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000167 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000168 bool isLastUseOfLocalReg(MachineOperand&);
169
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000170 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000171 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000172 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000173 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000174 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000175
176 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000177 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000178 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000179 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
180 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
181 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
182 }
183 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
184 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
185 }
186 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
187 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator,
188 unsigned Hint);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000189 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
190 unsigned VirtReg, unsigned Hint);
191 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
192 unsigned VirtReg, unsigned Hint);
Akira Hatanakabab24212012-10-31 00:56:01 +0000193 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000194 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000195 };
196 char RAFast::ID = 0;
197}
198
199/// getStackSpaceFor - This allocates space for the specified virtual register
200/// to be held on the stack.
201int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
202 // Find the location Reg would belong...
203 int SS = StackSlotForVirtReg[VirtReg];
204 if (SS != -1)
205 return SS; // Already has space allocated?
206
207 // Allocate a new stack object for this spill location...
208 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
209 RC->getAlignment());
210
211 // Assign the slot.
212 StackSlotForVirtReg[VirtReg] = FrameIdx;
213 return FrameIdx;
214}
215
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000216/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
217/// its virtual register, and it is guaranteed to be a block-local register.
218///
219bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000220 // If the register has ever been spilled or reloaded, we conservatively assume
221 // it is a global register used in multiple blocks.
222 if (StackSlotForVirtReg[MO.getReg()] != -1)
223 return false;
224
225 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesen4e696622012-08-08 23:44:01 +0000226 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Stephen Hines36b56882014-04-23 16:57:46 -0700227 if (&*I != &MO)
Jakob Stoklund Olesen4e696622012-08-08 23:44:01 +0000228 return false;
229 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000230}
231
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000232/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000233void RAFast::addKillFlag(const LiveReg &LR) {
234 if (!LR.LastUse) return;
235 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000236 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
237 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000238 MO.setIsKill();
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000239 else
240 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
241 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000242}
243
244/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000245void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000246 addKillFlag(*LRI);
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000247 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
248 "Broken RegState mapping");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000249 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000250 // Erase from LiveVirtRegs unless we're spilling in bulk.
251 if (!isBulkSpilling)
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000252 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000253}
254
255/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000256void RAFast::killVirtReg(unsigned VirtReg) {
257 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
258 "killVirtReg needs a virtual register");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000259 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000260 if (LRI != LiveVirtRegs.end())
261 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000262}
263
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000264/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedman24a11822010-08-21 20:19:51 +0000265/// corresponding stack slot if needed.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000266void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000267 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
268 "Spilling a physical register is illegal!");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000269 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000270 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
271 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000272}
273
274/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000275void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000276 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000277 LiveReg &LR = *LRI;
278 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000279
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000280 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000281 // If this physreg is used by the instruction, we want to kill it on the
282 // instruction, not on the spill.
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000283 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000284 LR.Dirty = false;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000285 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000286 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000287 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
288 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000289 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000290 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000291 ++NumStores; // Update statistics
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000292
Jim Grosbach07cb6892010-09-01 19:16:29 +0000293 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Patel459a36b2010-08-04 18:42:02 +0000294 // identify spilled location as the place to find corresponding variable's
295 // value.
Craig Toppera0ec3f92013-07-14 04:42:23 +0000296 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000297 LiveDbgValueMap[LRI->VirtReg];
Devang Patel72d9b0e2011-06-21 22:36:03 +0000298 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
299 MachineInstr *DBG = LRIDbgValues[li];
Stephen Hines37ed9c12014-12-01 14:51:49 -0800300 const MDNode *Var = DBG->getDebugVariable();
301 const MDNode *Expr = DBG->getDebugExpression();
Adrian Prantl818833f2013-09-16 23:29:03 +0000302 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantl43ae5e82013-07-10 16:56:52 +0000303 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Devang Patel31defcf2010-08-06 00:26:18 +0000304 DebugLoc DL;
305 if (MI == MBB->end()) {
306 // If MI is at basic block end then use last instruction's location.
307 MachineBasicBlock::iterator EI = MI;
308 DL = (--EI)->getDebugLoc();
David Blaikie6d9dbd52013-06-16 20:34:15 +0000309 } else
Devang Patel31defcf2010-08-06 00:26:18 +0000310 DL = MI->getDebugLoc();
David Blaikie6d9dbd52013-06-16 20:34:15 +0000311 MachineInstr *NewDV =
312 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
Stephen Hines37ed9c12014-12-01 14:51:49 -0800313 .addFrameIndex(FI)
314 .addImm(Offset)
315 .addMetadata(Var)
316 .addMetadata(Expr);
317 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie6d9dbd52013-06-16 20:34:15 +0000318 (void)NewDV;
319 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Patel459a36b2010-08-04 18:42:02 +0000320 }
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000321 // Now this register is spilled there is should not be any DBG_VALUE
322 // pointing to this register because they are all pointing to spilled value
323 // now.
Devang Patel6f373a82011-06-21 23:02:36 +0000324 LRIDbgValues.clear();
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000325 if (SpillKill)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700326 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000327 }
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000328 killVirtReg(LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000329}
330
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000331/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakabab24212012-10-31 00:56:01 +0000332void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000333 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000334 isBulkSpilling = true;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000335 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
336 // of spilling here is deterministic, if arbitrary.
337 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
338 i != e; ++i)
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000339 spillVirtReg(MI, i);
340 LiveVirtRegs.clear();
341 isBulkSpilling = false;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000342}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000343
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000344/// usePhysReg - Handle the direct use of a physical register.
345/// Check that the register is not used by a virtreg.
346/// Kill the physreg, marking it free.
347/// This may add implicit kills to MO->getParent() and invalidate MO.
348void RAFast::usePhysReg(MachineOperand &MO) {
349 unsigned PhysReg = MO.getReg();
350 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
351 "Bad usePhysReg operand");
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000352 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000353 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000354 case regDisabled:
355 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000356 case regReserved:
357 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000358 // Fall through
359 case regFree:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000360 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000361 return;
362 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000363 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000364 // wanted has been clobbered.
365 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000366 }
367
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000368 // Maybe a superregister is reserved?
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000369 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
370 unsigned Alias = *AI;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000371 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000372 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000373 break;
374 case regReserved:
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700375 // Either PhysReg is a subregister of Alias and we mark the
376 // whole register as free, or PhysReg is the superregister of
377 // Alias and we mark all the aliases as disabled before freeing
378 // PhysReg.
379 // In the latter case, since PhysReg was disabled, this means that
380 // its value is defined only by physical sub-registers. This check
381 // is performed by the assert of the default case in this loop.
382 // Note: The value of the superregister may only be partial
383 // defined, that is why regDisabled is a valid state for aliases.
384 assert((TRI->isSuperRegister(PhysReg, Alias) ||
385 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000386 "Instruction is not using a subregister of a reserved register");
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700387 // Fall through.
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000388 case regFree:
389 if (TRI->isSuperRegister(PhysReg, Alias)) {
390 // Leave the superregister in the working set.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700391 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000392 MO.getParent()->addRegisterKilled(Alias, TRI, true);
393 return;
394 }
395 // Some other alias was in the working set - clear it.
396 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000397 break;
398 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000399 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000400 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000401 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000402
403 // All aliases are disabled, bring register into working set.
404 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000405 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000406}
407
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000408/// definePhysReg - Mark PhysReg as reserved or free after spilling any
409/// virtregs. This is very similar to defineVirtReg except the physreg is
410/// reserved instead of allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000411void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
412 RegState NewState) {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000413 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000414 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
415 case regDisabled:
416 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000417 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000418 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000419 // Fall through.
420 case regFree:
421 case regReserved:
422 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000423 return;
424 }
425
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000426 // This is a disabled register, disable all aliases.
427 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000428 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
429 unsigned Alias = *AI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000430 switch (unsigned VirtReg = PhysRegState[Alias]) {
431 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000432 break;
433 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000434 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000435 // Fall through.
436 case regFree:
437 case regReserved:
438 PhysRegState[Alias] = regDisabled;
439 if (TRI->isSuperRegister(PhysReg, Alias))
440 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000441 break;
442 }
443 }
444}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000445
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000446
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000447// calcSpillCost - Return the cost of spilling clearing out PhysReg and
448// aliases so it is free for allocation.
449// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
450// can be allocated directly.
451// Returns spillImpossible when PhysReg or an alias can't be spilled.
452unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000453 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000454 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000455 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000456 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000457 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
458 case regDisabled:
459 break;
460 case regFree:
461 return 0;
462 case regReserved:
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000463 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
464 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000465 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000466 default: {
467 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
468 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
469 return I->Dirty ? spillDirty : spillClean;
470 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000471 }
472
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000473 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000474 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000475 unsigned Cost = 0;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000476 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
477 unsigned Alias = *AI;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000478 switch (unsigned VirtReg = PhysRegState[Alias]) {
479 case regDisabled:
480 break;
481 case regFree:
482 ++Cost;
483 break;
484 case regReserved:
485 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000486 default: {
487 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
488 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
489 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000490 break;
491 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000492 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000493 }
494 return Cost;
495}
496
497
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000498/// assignVirtToPhysReg - This method updates local state so that we know
499/// that PhysReg is the proper container for VirtReg now. The physical
500/// register must not be used for anything else when this is called.
501///
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000502void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
503 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000504 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000505 PhysRegState[PhysReg] = LR.VirtReg;
506 assert(!LR.PhysReg && "Already assigned a physreg");
507 LR.PhysReg = PhysReg;
508}
509
510RAFast::LiveRegMap::iterator
511RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
512 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
513 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
514 assignVirtToPhysReg(*LRI, PhysReg);
515 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000516}
517
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000518/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000519RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
520 LiveRegMap::iterator LRI,
521 unsigned Hint) {
522 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000523
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000524 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
525 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000526
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000527 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000528
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000529 // Ignore invalid hints.
530 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000531 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000532 Hint = 0;
533
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000534 // Take hint when possible.
535 if (Hint) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000536 // Ignore the hint if we would have to spill a dirty register.
537 unsigned Cost = calcSpillCost(Hint);
538 if (Cost < spillDirty) {
539 if (Cost)
540 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000541 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
542 // That invalidates LRI, so run a new lookup for VirtReg.
543 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000544 }
545 }
546
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +0000547 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000548
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000549 // First try to find a completely free register.
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +0000550 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000551 unsigned PhysReg = *I;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000552 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000553 assignVirtToPhysReg(*LRI, PhysReg);
554 return LRI;
555 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000556 }
557
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000558 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Stephen Hines37ed9c12014-12-01 14:51:49 -0800559 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000560
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000561 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +0000562 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000563 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000564 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopher0b756342011-04-12 22:17:44 +0000565 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
566 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000567 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000568 if (Cost == 0) {
569 assignVirtToPhysReg(*LRI, *I);
570 return LRI;
571 }
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000572 if (Cost < BestCost)
573 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000574 }
575
576 if (BestReg) {
Jakob Stoklund Olesenf3ea06b2010-05-17 15:30:37 +0000577 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000578 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
579 // That invalidates LRI, so run a new lookup for VirtReg.
580 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000581 }
582
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000583 // Nothing we can do. Report an error and keep going with a bad allocation.
Benjamin Kramer87855d32013-10-05 19:33:37 +0000584 if (MI->isInlineAsm())
585 MI->emitError("inline assembly requires more registers than available");
586 else
587 MI->emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen9d812a22011-07-02 07:17:37 +0000588 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000589 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000590}
591
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000592/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000593RAFast::LiveRegMap::iterator
594RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
595 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000596 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
597 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000598 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000599 bool New;
Stephen Hines36b56882014-04-23 16:57:46 -0700600 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000601 if (New) {
602 // If there is no hint, peek at the only use of this register.
603 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
604 MRI->hasOneNonDBGUse(VirtReg)) {
Stephen Hines36b56882014-04-23 16:57:46 -0700605 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000606 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000607 if (UseMI.isCopyLike())
608 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000609 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000610 LRI = allocVirtReg(MI, LRI, Hint);
611 } else if (LRI->LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000612 // Redefining a live register - kill at the last use, unless it is this
613 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000614 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
615 addKillFlag(*LRI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000616 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000617 assert(LRI->PhysReg && "Register not assigned");
618 LRI->LastUse = MI;
619 LRI->LastOpNum = OpNum;
620 LRI->Dirty = true;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000621 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000622 return LRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000623}
624
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000625/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000626RAFast::LiveRegMap::iterator
627RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
628 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000629 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
630 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000631 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000632 bool New;
Stephen Hines36b56882014-04-23 16:57:46 -0700633 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000634 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000635 if (New) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000636 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000637 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000638 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000639 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000640 << PrintReg(LRI->PhysReg, TRI) << "\n");
641 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000642 ++NumLoads;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000643 } else if (LRI->Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000644 if (isLastUseOfLocalReg(MO)) {
645 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000646 if (MO.isUse())
647 MO.setIsKill();
648 else
649 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000650 } else if (MO.isKill()) {
651 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
652 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000653 } else if (MO.isDead()) {
654 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
655 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000656 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000657 } else if (MO.isKill()) {
658 // We must remove kill flags from uses of reloaded registers because the
659 // register would be killed immediately, and there might be a second use:
660 // %foo = OR %x<kill>, %x
661 // This would cause a second reload of %x into a different register.
662 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
663 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000664 } else if (MO.isDead()) {
665 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
666 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000667 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000668 assert(LRI->PhysReg && "Register not assigned");
669 LRI->LastUse = MI;
670 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000671 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000672 return LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000673}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000674
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000675// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
676// subregs. This may invalidate any operand pointers.
677// Return true if the operand kills its register.
678bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
679 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000680 bool Dead = MO.isDead();
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000681 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000682 MO.setReg(PhysReg);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000683 return MO.isKill() || Dead;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000684 }
685
686 // Handle subregister index.
687 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
688 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000689
690 // A kill flag implies killing the full register. Add corresponding super
691 // register kill.
692 if (MO.isKill()) {
693 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000694 return true;
695 }
Jakob Stoklund Olesen4d108292012-05-14 21:10:25 +0000696
697 // A <def,read-undef> of a sub-register requires an implicit def of the full
698 // register.
699 if (MO.isDef() && MO.isUndef())
700 MI->addRegisterDefined(PhysReg, TRI);
701
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000702 return Dead;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000703}
704
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000705// Handle special instruction operand like early clobbers and tied ops when
706// there are additional physreg defines.
707void RAFast::handleThroughOperands(MachineInstr *MI,
708 SmallVectorImpl<unsigned> &VirtDead) {
709 DEBUG(dbgs() << "Scanning for through registers:");
710 SmallSet<unsigned, 8> ThroughRegs;
711 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
712 MachineOperand &MO = MI->getOperand(i);
713 if (!MO.isReg()) continue;
714 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000715 if (!TargetRegisterInfo::isVirtualRegister(Reg))
716 continue;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000717 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
718 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800719 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000720 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000721 }
722 }
723
724 // If any physreg defines collide with preallocated through registers,
725 // we must spill and reallocate.
726 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
727 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
728 MachineOperand &MO = MI->getOperand(i);
729 if (!MO.isReg() || !MO.isDef()) continue;
730 unsigned Reg = MO.getReg();
731 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000732 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +0000733 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +0000734 if (ThroughRegs.count(PhysRegState[*AI]))
735 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000736 }
737 }
738
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000739 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola254a1322011-11-22 06:27:18 +0000740 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000741 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
742 MachineOperand &MO = MI->getOperand(i);
743 if (!MO.isReg()) continue;
744 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000745 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000746 if (MO.isUse()) {
747 unsigned DefIdx = 0;
748 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
749 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
750 << DefIdx << ".\n");
751 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000752 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000753 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000754 // Note: we don't update the def operand yet. That would cause the normal
755 // def-scan to attempt spilling.
756 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
757 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
758 // Reload the register, but don't assign to the operand just yet.
759 // That would confuse the later phys-def processing pass.
760 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000761 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000762 }
763 }
764
Rafael Espindola254a1322011-11-22 06:27:18 +0000765 DEBUG(dbgs() << "Allocating early clobbers.\n");
766 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
767 MachineOperand &MO = MI->getOperand(i);
768 if (!MO.isReg()) continue;
769 unsigned Reg = MO.getReg();
770 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
771 if (!MO.isEarlyClobber())
772 continue;
773 // Note: defineVirtReg may invalidate MO.
774 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000775 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola254a1322011-11-22 06:27:18 +0000776 if (setPhysReg(MI, i, PhysReg))
777 VirtDead.push_back(Reg);
778 }
779
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000780 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000781 UsedInInstr.clear();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000782 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
783 MachineOperand &MO = MI->getOperand(i);
784 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
785 unsigned Reg = MO.getReg();
786 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen27ce3b92011-06-28 17:24:32 +0000787 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
788 << " as used in instr\n");
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000789 markRegUsedInInstr(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000790 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000791
792 // Also mark PartialDefs as used to avoid reallocation.
793 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000794 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000795}
796
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000797void RAFast::AllocateBasicBlock() {
798 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000799
800 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000801 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000802
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000803 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000804
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000805 // Add live-in registers as live.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000806 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
807 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000808 if (MRI->isAllocatable(*I))
Jakob Stoklund Olesen9d4b51b2010-08-31 19:54:25 +0000809 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000810
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000811 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000812 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000813
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000814 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +0000815 while (MII != MBB->end()) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000816 MachineInstr *MI = MII++;
Evan Chenge837dea2011-06-28 19:10:37 +0000817 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000818 DEBUG({
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000819 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000820 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
821 if (PhysRegState[Reg] == regDisabled) continue;
822 dbgs() << " " << TRI->getName(Reg);
823 switch(PhysRegState[Reg]) {
824 case regFree:
825 break;
826 case regReserved:
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +0000827 dbgs() << "*";
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000828 break;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000829 default: {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000830 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000831 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
832 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
833 if (I->Dirty)
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000834 dbgs() << "*";
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000835 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000836 break;
837 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000838 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000839 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000840 dbgs() << '\n';
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000841 // Check that LiveVirtRegs is the inverse.
842 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
843 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000844 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000845 "Bad map key");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000846 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000847 "Bad map value");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000848 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000849 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000850 });
851
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000852 // Debug values are not allowed to change codegen in any way.
853 if (MI->isDebugValue()) {
Devang Patel58b81762010-07-19 23:25:39 +0000854 bool ScanDbgValue = true;
855 while (ScanDbgValue) {
856 ScanDbgValue = false;
857 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
858 MachineOperand &MO = MI->getOperand(i);
859 if (!MO.isReg()) continue;
860 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000861 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000862 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Patel58b81762010-07-19 23:25:39 +0000863 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000864 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel7a029b62010-07-09 21:48:31 +0000865 else {
Devang Patel58b81762010-07-19 23:25:39 +0000866 int SS = StackSlotForVirtReg[Reg];
Devang Patel4bafda92010-09-10 20:32:09 +0000867 if (SS == -1) {
Jim Grosbach07cb6892010-09-01 19:16:29 +0000868 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel4bafda92010-09-10 20:32:09 +0000869 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbach07cb6892010-09-01 19:16:29 +0000870 MO.setReg(0);
Devang Patel4bafda92010-09-10 20:32:09 +0000871 }
Devang Patel58b81762010-07-19 23:25:39 +0000872 else {
873 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantl818833f2013-09-16 23:29:03 +0000874 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantl43ae5e82013-07-10 16:56:52 +0000875 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800876 const MDNode *Var = MI->getDebugVariable();
877 const MDNode *Expr = MI->getDebugExpression();
Devang Patel58b81762010-07-19 23:25:39 +0000878 DebugLoc DL = MI->getDebugLoc();
David Blaikie6d9dbd52013-06-16 20:34:15 +0000879 MachineBasicBlock *MBB = MI->getParent();
880 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
881 TII->get(TargetOpcode::DBG_VALUE))
Stephen Hines37ed9c12014-12-01 14:51:49 -0800882 .addFrameIndex(SS)
883 .addImm(Offset)
884 .addMetadata(Var)
885 .addMetadata(Expr);
David Blaikie6d9dbd52013-06-16 20:34:15 +0000886 DEBUG(dbgs() << "Modifying debug info due to spill:"
887 << "\t" << *NewDV);
888 // Scan NewDV operands from the beginning.
889 MI = NewDV;
890 ScanDbgValue = true;
891 break;
Devang Patel58b81762010-07-19 23:25:39 +0000892 }
Devang Patel7a029b62010-07-09 21:48:31 +0000893 }
Devang Pateld2df64f2011-11-15 21:03:58 +0000894 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel7a029b62010-07-09 21:48:31 +0000895 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000896 }
897 // Next instruction.
898 continue;
899 }
900
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000901 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000902 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000903 if (MI->isCopy()) {
904 CopyDst = MI->getOperand(0).getReg();
905 CopySrc = MI->getOperand(1).getReg();
906 CopyDstSub = MI->getOperand(0).getSubReg();
907 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000908 }
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000909
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000910 // Track registers used by instruction.
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000911 UsedInInstr.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000912
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000913 // First scan.
914 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000915 // Find the end of the virtreg operands
916 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000917 bool hasTiedOps = false;
918 bool hasEarlyClobbers = false;
919 bool hasPartialRedefs = false;
920 bool hasPhysDefs = false;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000921 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
922 MachineOperand &MO = MI->getOperand(i);
Chad Rosier7979b242012-11-06 22:52:42 +0000923 // Make sure MRI knows about registers clobbered by regmasks.
924 if (MO.isRegMask()) {
925 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
926 continue;
927 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000928 if (!MO.isReg()) continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000929 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000930 if (!Reg) continue;
931 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
932 VirtOpEnd = i+1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000933 if (MO.isUse()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000934 hasTiedOps = hasTiedOps ||
Evan Chenge837dea2011-06-28 19:10:37 +0000935 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000936 } else {
937 if (MO.isEarlyClobber())
938 hasEarlyClobbers = true;
939 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
940 hasPartialRedefs = true;
941 }
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000942 continue;
943 }
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000944 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000945 if (MO.isUse()) {
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000946 usePhysReg(MO);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000947 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +0000948 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
949 regFree : regReserved);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000950 hasEarlyClobbers = true;
951 } else
952 hasPhysDefs = true;
953 }
954
955 // The instruction may have virtual register operands that must be allocated
956 // the same register at use-time and def-time: early clobbers and tied
957 // operands. If there are also physical defs, these registers must avoid
958 // both physical defs and uses, making them more constrained than normal
959 // operands.
Jim Grosbach07cb6892010-09-01 19:16:29 +0000960 // Similarly, if there are multiple defs and tied operands, we must make
961 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000962 // We didn't detect inline asm tied operands above, so just make this extra
963 // pass for all inline asm.
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000964 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Chenge837dea2011-06-28 19:10:37 +0000965 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000966 handleThroughOperands(MI, VirtDead);
967 // Don't attempt coalescing when we have funny stuff going on.
968 CopyDst = 0;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000969 // Pretend we have early clobbers so the use operands get marked below.
970 // This is not necessary for the common case of a single tied use.
971 hasEarlyClobbers = true;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000972 }
973
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000974 // Second scan.
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000975 // Allocate virtreg uses.
Jakob Stoklund Olesene97dda42010-05-14 21:55:52 +0000976 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000977 MachineOperand &MO = MI->getOperand(i);
978 if (!MO.isReg()) continue;
979 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000980 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000981 if (MO.isUse()) {
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000982 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000983 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +0000984 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000985 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +0000986 killVirtReg(LRI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000987 }
988 }
989
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000990 for (UsedInInstrSet::iterator
991 I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I)
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000992 MRI->setRegUnitUsed(*I);
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000993
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +0000994 // Track registers defined by instruction - early clobbers and tied uses at
995 // this point.
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000996 UsedInInstr.clear();
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000997 if (hasEarlyClobbers) {
998 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
999 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001000 if (!MO.isReg()) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001001 unsigned Reg = MO.getReg();
1002 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen4bd94f72010-07-29 00:52:19 +00001003 // Look for physreg defs and tied uses.
1004 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +00001005 markRegUsedInInstr(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +00001006 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001007 }
1008
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001009 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001010 if (MI->isCall()) {
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001011 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbach07cb6892010-09-01 19:16:29 +00001012 // exception is thrown, the landing pad is going to expect to find
1013 // registers in their spill slots, and 2. we don't have to wade through
1014 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001015 DefOpEnd = VirtOpEnd;
1016 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1017 spillAll(MI);
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001018
1019 // The imp-defs are skipped below, but we still need to mark those
1020 // registers as used by the function.
Evan Chenge837dea2011-06-28 19:10:37 +00001021 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001022 }
1023
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001024 // Third scan.
1025 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen4b6bbe82010-05-17 02:49:18 +00001026 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001027 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen75ac4d92010-06-15 16:20:57 +00001028 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1029 continue;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001030 unsigned Reg = MO.getReg();
1031
1032 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +00001033 if (!MRI->isAllocatable(Reg)) continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001034 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001035 continue;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001036 }
Jakob Stoklund Olesen646dd7c2010-05-17 03:26:09 +00001037 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001038 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001039 if (setPhysReg(MI, i, PhysReg)) {
1040 VirtDead.push_back(Reg);
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001041 CopyDst = 0; // cancel coalescing;
1042 } else
1043 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001044 }
1045
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +00001046 // Kill dead defs after the scan to ensure that multiple defs of the same
1047 // register are allocated identically. We didn't need to do this for uses
1048 // because we are crerating our own kill flags, and they are always at the
1049 // last use.
1050 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1051 killVirtReg(VirtDead[i]);
1052 VirtDead.clear();
1053
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +00001054 for (UsedInInstrSet::iterator
1055 I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I)
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +00001056 MRI->setRegUnitUsed(*I);
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001057
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001058 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1059 DEBUG(dbgs() << "-- coalescing: " << *MI);
1060 Coalesced.push_back(MI);
1061 } else {
1062 DEBUG(dbgs() << "<< " << *MI);
1063 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001064 }
1065
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001066 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001067 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1068 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001069
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001070 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001071 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001072 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001073 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen8a65c512010-05-14 21:55:50 +00001074 NumCopies += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001075
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001076 DEBUG(MBB->dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001077}
1078
1079/// runOnMachineFunction - Register allocate the whole function
1080///
1081bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesenc9c4dac2010-05-13 20:43:17 +00001082 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikie986d76d2012-08-22 17:18:53 +00001083 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001084 MF = &Fn;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +00001085 MRI = &MF->getRegInfo();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001086 TRI = MF->getSubtarget().getRegisterInfo();
1087 TII = MF->getSubtarget().getInstrInfo();
Chad Rosier18bb0542012-11-28 00:21:29 +00001088 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +00001089 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +00001090 UsedInInstr.clear();
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +00001091 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001092
Andrew Trick8dd26252012-02-10 04:10:36 +00001093 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1094
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001095 // initialize the virtual->physical register map to have a 'null'
1096 // mapping for all virtual registers
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +00001097 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001098 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001099
1100 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +00001101 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1102 MBBi != MBBe; ++MBBi) {
1103 MBB = &*MBBi;
1104 AllocateBasicBlock();
1105 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001106
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001107 // Add the clobber lists for all the instructions we skipped earlier.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001108 for (const MCInstrDesc *Desc : SkippedInstrs)
1109 if (const uint16_t *Defs = Desc->getImplicitDefs())
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001110 while (*Defs)
1111 MRI->setPhysRegUsed(*Defs++);
1112
Andrew Trick19273ae2012-02-21 04:51:23 +00001113 // All machine operands and other references to virtual registers have been
1114 // replaced. Remove the virtual registers.
1115 MRI->clearVirtRegs();
1116
Jakob Stoklund Olesen6de07172010-06-04 18:08:29 +00001117 SkippedInstrs.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001118 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001119 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001120 return true;
1121}
1122
1123FunctionPass *llvm::createFastRegisterAllocator() {
1124 return new RAFast();
1125}