blob: 56a3470edbeb9c8341ca70139491806cd4b81453 [file] [log] [blame]
Jakob Stoklund Olesen8a070a52010-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 Olesen8a070a52010-04-21 18:02:42 +000015#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/IndexedMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000019#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000021#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000022#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-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 Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/BasicBlock.h"
Chandler Carruthed0881b2012-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"
Eric Christopherd9134482014-08-04 21:25:23 +000036#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000037#include <algorithm>
38using namespace llvm;
39
Chandler Carruth1b9dde02014-04-22 02:02:50 +000040#define DEBUG_TYPE "regalloc"
41
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000042STATISTIC(NumStores, "Number of stores added");
43STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +000044STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen8a070a52010-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 Andersona7aed182010-08-06 18:33:48 +000053 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Andrew Trickd3f8fe82012-02-10 04:10:36 +000054 isBulkSpilling(false) {}
Derek Schuffad154c82016-03-28 17:05:30 +000055
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000056 private:
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000057 MachineFunction *MF;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +000058 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000059 const TargetRegisterInfo *TRI;
60 const TargetInstrInfo *TII;
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +000061 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000062
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +000063 // Basic block currently being allocated.
64 MachineBasicBlock *MBB;
65
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000066 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
67 // values are spilled.
68 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
69
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000070 // Everything we know about a live virtual register.
71 struct LiveReg {
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +000072 MachineInstr *LastUse; // Last instr to use reg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000073 unsigned VirtReg; // Virtual register number.
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +000074 unsigned PhysReg; // Currently held here.
75 unsigned short LastOpNum; // OpNum on LastUse.
76 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000077
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000078 explicit LiveReg(unsigned v)
Craig Topperc0196b12014-04-14 00:51:57 +000079 : LastUse(nullptr), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false){}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000080
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000081 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000082 return TargetRegisterInfo::virtReg2Index(VirtReg);
83 }
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000084 };
85
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000086 typedef SparseSet<LiveReg> LiveRegMap;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000087
88 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000089 // that is currently available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000090 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000091
Devang Patel0ab77672011-06-21 22:36:03 +000092 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +000093
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +000094 // RegState - Track the state of a physical register.
95 enum RegState {
96 // A disabled register is not available for allocation, but an alias may
97 // be in use. A register can only be moved out of the disabled state if
98 // all aliases are disabled.
99 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000100
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000101 // A free register is not currently in use and can be allocated
102 // immediately without checking aliases.
103 regFree,
104
Evan Cheng8ea3af42011-04-22 01:40:20 +0000105 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000106 // call parameter), and it remains reserved until it is used.
107 regReserved
108
109 // A register state may also be a virtual register number, indication that
110 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000111 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000112 };
113
114 // PhysRegState - One of the RegState enums, or a virtreg.
115 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000116
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000117 // Set of register units.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000118 typedef SparseSet<unsigned> UsedInInstrSet;
119
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000120 // Set of register units that are used in the current instruction, and so
121 // cannot be allocated.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000122 UsedInInstrSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000123
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000124 // Mark a physreg as used in this instruction.
125 void markRegUsedInInstr(unsigned PhysReg) {
126 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
127 UsedInInstr.insert(*Units);
128 }
129
130 // Check if a physreg or any of its aliases are used in this instruction.
131 bool isRegUsedInInstr(unsigned PhysReg) const {
132 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
133 if (UsedInInstr.count(*Units))
134 return true;
135 return false;
136 }
137
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000138 // SkippedInstrs - Descriptors of instructions whose clobber list was
139 // ignored because all registers were spilled. It is still necessary to
140 // mark all the clobbered registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000141 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +0000142
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000143 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
144 // completely after spilling all live registers. LiveRegMap entries should
145 // not be erased.
146 bool isBulkSpilling;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000147
Alp Toker61007d82014-03-02 03:20:38 +0000148 enum : unsigned {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000149 spillClean = 1,
150 spillDirty = 100,
151 spillImpossible = ~0u
152 };
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000153 public:
Craig Topper4584cd52014-03-07 09:26:03 +0000154 const char *getPassName() const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000155 return "Fast Register Allocator";
156 }
157
Craig Topper4584cd52014-03-07 09:26:03 +0000158 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000159 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000160 MachineFunctionPass::getAnalysisUsage(AU);
161 }
162
Derek Schuffad154c82016-03-28 17:05:30 +0000163 MachineFunctionProperties getSetProperties() const override {
164 return MachineFunctionProperties().set(
165 MachineFunctionProperties::Property::AllVRegsAllocated);
166 }
167
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000168 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000169 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000170 void AllocateBasicBlock();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000171 void handleThroughOperands(MachineInstr *MI,
172 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000173 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000174 bool isLastUseOfLocalReg(MachineOperand&);
175
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000176 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000177 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000178 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000179 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000180 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000181
182 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000183 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000184 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000185 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
186 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
187 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
188 }
189 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
190 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
191 }
192 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
193 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator,
194 unsigned Hint);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000195 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
196 unsigned VirtReg, unsigned Hint);
197 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
198 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000199 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000200 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000201 };
202 char RAFast::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000203}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000204
205/// getStackSpaceFor - This allocates space for the specified virtual register
206/// to be held on the stack.
207int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
208 // Find the location Reg would belong...
209 int SS = StackSlotForVirtReg[VirtReg];
210 if (SS != -1)
211 return SS; // Already has space allocated?
212
213 // Allocate a new stack object for this spill location...
214 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
215 RC->getAlignment());
216
217 // Assign the slot.
218 StackSlotForVirtReg[VirtReg] = FrameIdx;
219 return FrameIdx;
220}
221
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000222/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
223/// its virtual register, and it is guaranteed to be a block-local register.
224///
225bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000226 // If the register has ever been spilled or reloaded, we conservatively assume
227 // it is a global register used in multiple blocks.
228 if (StackSlotForVirtReg[MO.getReg()] != -1)
229 return false;
230
231 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000232 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000233 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000234 return false;
235 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000236}
237
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000238/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000239void RAFast::addKillFlag(const LiveReg &LR) {
240 if (!LR.LastUse) return;
241 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000242 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
243 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000244 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000245 else
246 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
247 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000248}
249
250/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000251void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000252 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000253 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
254 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000255 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000256 // Erase from LiveVirtRegs unless we're spilling in bulk.
257 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000258 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000259}
260
261/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000262void RAFast::killVirtReg(unsigned VirtReg) {
263 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
264 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000265 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000266 if (LRI != LiveVirtRegs.end())
267 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000268}
269
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000270/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000271/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000272void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000273 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
274 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000275 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000276 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
277 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000278}
279
280/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000281void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000282 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000283 LiveReg &LR = *LRI;
284 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000285
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000286 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000287 // If this physreg is used by the instruction, we want to kill it on the
288 // instruction, not on the spill.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000289 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000290 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000291 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000292 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000293 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
294 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000295 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000296 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000297 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000298
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000299 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000300 // identify spilled location as the place to find corresponding variable's
301 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000302 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000303 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000304 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
305 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000306 const MDNode *Var = DBG->getDebugVariable();
307 const MDNode *Expr = DBG->getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000308 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000309 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000310 DebugLoc DL = DBG->getDebugLoc();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000311 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000312 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000313 MachineInstr *NewDV =
314 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000315 .addFrameIndex(FI)
316 .addImm(Offset)
317 .addMetadata(Var)
318 .addMetadata(Expr);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000319 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000320 (void)NewDV;
321 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000322 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000323 // Now this register is spilled there is should not be any DBG_VALUE
324 // pointing to this register because they are all pointing to spilled value
325 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000326 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000327 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000328 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000329 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000330 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000331}
332
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000333/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000334void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000335 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000336 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000337 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
338 // of spilling here is deterministic, if arbitrary.
339 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
340 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000341 spillVirtReg(MI, i);
342 LiveVirtRegs.clear();
343 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000344}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000345
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000346/// usePhysReg - Handle the direct use of a physical register.
347/// Check that the register is not used by a virtreg.
348/// Kill the physreg, marking it free.
349/// This may add implicit kills to MO->getParent() and invalidate MO.
350void RAFast::usePhysReg(MachineOperand &MO) {
351 unsigned PhysReg = MO.getReg();
352 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
353 "Bad usePhysReg operand");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000354 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000355 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000356 case regDisabled:
357 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000358 case regReserved:
359 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000360 // Fall through
361 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000362 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000363 return;
364 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000365 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000366 // wanted has been clobbered.
367 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000368 }
369
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000370 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000371 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
372 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000373 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000374 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000375 break;
376 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000377 // Either PhysReg is a subregister of Alias and we mark the
378 // whole register as free, or PhysReg is the superregister of
379 // Alias and we mark all the aliases as disabled before freeing
380 // PhysReg.
381 // In the latter case, since PhysReg was disabled, this means that
382 // its value is defined only by physical sub-registers. This check
383 // is performed by the assert of the default case in this loop.
384 // Note: The value of the superregister may only be partial
385 // defined, that is why regDisabled is a valid state for aliases.
386 assert((TRI->isSuperRegister(PhysReg, Alias) ||
387 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000388 "Instruction is not using a subregister of a reserved register");
Quentin Colombet079aba72014-12-03 23:38:08 +0000389 // Fall through.
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000390 case regFree:
391 if (TRI->isSuperRegister(PhysReg, Alias)) {
392 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000393 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000394 MO.getParent()->addRegisterKilled(Alias, TRI, true);
395 return;
396 }
397 // Some other alias was in the working set - clear it.
398 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000399 break;
400 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000401 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000402 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000403 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000404
405 // All aliases are disabled, bring register into working set.
406 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000407 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000408}
409
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000410/// definePhysReg - Mark PhysReg as reserved or free after spilling any
411/// virtregs. This is very similar to defineVirtReg except the physreg is
412/// reserved instead of allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000413void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
414 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000415 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000416 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
417 case regDisabled:
418 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000419 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000420 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000421 // Fall through.
422 case regFree:
423 case regReserved:
424 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000425 return;
426 }
427
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000428 // This is a disabled register, disable all aliases.
429 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000430 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
431 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000432 switch (unsigned VirtReg = PhysRegState[Alias]) {
433 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000434 break;
435 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000436 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000437 // Fall through.
438 case regFree:
439 case regReserved:
440 PhysRegState[Alias] = regDisabled;
441 if (TRI->isSuperRegister(PhysReg, Alias))
442 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000443 break;
444 }
445 }
446}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000447
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000448
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000449// calcSpillCost - Return the cost of spilling clearing out PhysReg and
450// aliases so it is free for allocation.
451// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
452// can be allocated directly.
453// Returns spillImpossible when PhysReg or an alias can't be spilled.
454unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000455 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000456 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000457 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000458 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000459 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
460 case regDisabled:
461 break;
462 case regFree:
463 return 0;
464 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000465 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
466 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000467 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000468 default: {
469 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
470 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
471 return I->Dirty ? spillDirty : spillClean;
472 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000473 }
474
Eric Christopherc3783362011-04-12 00:48:08 +0000475 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000476 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000477 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000478 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
479 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000480 switch (unsigned VirtReg = PhysRegState[Alias]) {
481 case regDisabled:
482 break;
483 case regFree:
484 ++Cost;
485 break;
486 case regReserved:
487 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000488 default: {
489 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
490 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
491 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000492 break;
493 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000494 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000495 }
496 return Cost;
497}
498
499
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000500/// assignVirtToPhysReg - This method updates local state so that we know
501/// that PhysReg is the proper container for VirtReg now. The physical
502/// register must not be used for anything else when this is called.
503///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000504void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
505 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000506 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000507 PhysRegState[PhysReg] = LR.VirtReg;
508 assert(!LR.PhysReg && "Already assigned a physreg");
509 LR.PhysReg = PhysReg;
510}
511
512RAFast::LiveRegMap::iterator
513RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
514 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
515 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
516 assignVirtToPhysReg(*LRI, PhysReg);
517 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000518}
519
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000520/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000521RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
522 LiveRegMap::iterator LRI,
523 unsigned Hint) {
524 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000525
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000526 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
527 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000528
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000529 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000530
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000531 // Ignore invalid hints.
532 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000533 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000534 Hint = 0;
535
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000536 // Take hint when possible.
537 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000538 // Ignore the hint if we would have to spill a dirty register.
539 unsigned Cost = calcSpillCost(Hint);
540 if (Cost < spillDirty) {
541 if (Cost)
542 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000543 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
544 // That invalidates LRI, so run a new lookup for VirtReg.
545 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000546 }
547 }
548
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000549 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000550
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000551 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000552 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000553 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000554 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000555 assignVirtToPhysReg(*LRI, PhysReg);
556 return LRI;
557 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000558 }
559
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000560 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Craig Toppercf0444b2014-11-17 05:50:14 +0000561 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000562
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000563 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000564 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000565 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000566 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000567 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
568 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000569 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000570 if (Cost == 0) {
571 assignVirtToPhysReg(*LRI, *I);
572 return LRI;
573 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000574 if (Cost < BestCost)
575 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000576 }
577
578 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000579 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000580 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
581 // That invalidates LRI, so run a new lookup for VirtReg.
582 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000583 }
584
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000585 // Nothing we can do. Report an error and keep going with a bad allocation.
Benjamin Kramer7200a462013-10-05 19:33:37 +0000586 if (MI->isInlineAsm())
587 MI->emitError("inline assembly requires more registers than available");
588 else
589 MI->emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000590 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000591 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000592}
593
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000594/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000595RAFast::LiveRegMap::iterator
596RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
597 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000598 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
599 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000600 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000601 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000602 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000603 if (New) {
604 // If there is no hint, peek at the only use of this register.
605 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
606 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000607 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000608 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000609 if (UseMI.isCopyLike())
610 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000611 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000612 LRI = allocVirtReg(MI, LRI, Hint);
613 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000614 // Redefining a live register - kill at the last use, unless it is this
615 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000616 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
617 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000618 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000619 assert(LRI->PhysReg && "Register not assigned");
620 LRI->LastUse = MI;
621 LRI->LastOpNum = OpNum;
622 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000623 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000624 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000625}
626
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000627/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000628RAFast::LiveRegMap::iterator
629RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
630 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000631 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
632 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000633 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000634 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000635 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000636 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000637 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000638 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000639 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000640 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000641 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000642 << PrintReg(LRI->PhysReg, TRI) << "\n");
643 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000644 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000645 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000646 if (isLastUseOfLocalReg(MO)) {
647 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000648 if (MO.isUse())
649 MO.setIsKill();
650 else
651 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000652 } else if (MO.isKill()) {
653 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
654 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000655 } else if (MO.isDead()) {
656 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
657 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000658 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000659 } else if (MO.isKill()) {
660 // We must remove kill flags from uses of reloaded registers because the
661 // register would be killed immediately, and there might be a second use:
662 // %foo = OR %x<kill>, %x
663 // This would cause a second reload of %x into a different register.
664 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
665 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000666 } else if (MO.isDead()) {
667 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
668 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000669 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000670 assert(LRI->PhysReg && "Register not assigned");
671 LRI->LastUse = MI;
672 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000673 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000674 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000675}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000676
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000677// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
678// subregs. This may invalidate any operand pointers.
679// Return true if the operand kills its register.
680bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
681 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000682 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000683 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000684 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000685 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000686 }
687
688 // Handle subregister index.
689 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
690 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000691
692 // A kill flag implies killing the full register. Add corresponding super
693 // register kill.
694 if (MO.isKill()) {
695 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000696 return true;
697 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000698
699 // A <def,read-undef> of a sub-register requires an implicit def of the full
700 // register.
701 if (MO.isDef() && MO.isUndef())
702 MI->addRegisterDefined(PhysReg, TRI);
703
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000704 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000705}
706
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000707// Handle special instruction operand like early clobbers and tied ops when
708// there are additional physreg defines.
709void RAFast::handleThroughOperands(MachineInstr *MI,
710 SmallVectorImpl<unsigned> &VirtDead) {
711 DEBUG(dbgs() << "Scanning for through registers:");
712 SmallSet<unsigned, 8> ThroughRegs;
713 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
714 MachineOperand &MO = MI->getOperand(i);
715 if (!MO.isReg()) continue;
716 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000717 if (!TargetRegisterInfo::isVirtualRegister(Reg))
718 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000719 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
720 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000721 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000722 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000723 }
724 }
725
726 // If any physreg defines collide with preallocated through registers,
727 // we must spill and reallocate.
728 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
729 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
730 MachineOperand &MO = MI->getOperand(i);
731 if (!MO.isReg() || !MO.isDef()) continue;
732 unsigned Reg = MO.getReg();
733 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000734 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000735 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000736 if (ThroughRegs.count(PhysRegState[*AI]))
737 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000738 }
739 }
740
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000741 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000742 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000743 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
744 MachineOperand &MO = MI->getOperand(i);
745 if (!MO.isReg()) continue;
746 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000747 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000748 if (MO.isUse()) {
749 unsigned DefIdx = 0;
750 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
751 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
752 << DefIdx << ".\n");
753 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000754 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000755 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000756 // Note: we don't update the def operand yet. That would cause the normal
757 // def-scan to attempt spilling.
758 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
759 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
760 // Reload the register, but don't assign to the operand just yet.
761 // That would confuse the later phys-def processing pass.
762 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000763 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000764 }
765 }
766
Rafael Espindola2021f382011-11-22 06:27:18 +0000767 DEBUG(dbgs() << "Allocating early clobbers.\n");
768 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
769 MachineOperand &MO = MI->getOperand(i);
770 if (!MO.isReg()) continue;
771 unsigned Reg = MO.getReg();
772 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
773 if (!MO.isEarlyClobber())
774 continue;
775 // Note: defineVirtReg may invalidate MO.
776 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000777 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000778 if (setPhysReg(MI, i, PhysReg))
779 VirtDead.push_back(Reg);
780 }
781
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000782 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000783 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000784 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
785 MachineOperand &MO = MI->getOperand(i);
786 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
787 unsigned Reg = MO.getReg();
788 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000789 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
790 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000791 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000792 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000793
794 // Also mark PartialDefs as used to avoid reallocation.
795 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000796 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000797}
798
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000799void RAFast::AllocateBasicBlock() {
800 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000801
802 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000803 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000804
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000805 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000806
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000807 // Add live-in registers as live.
Matthias Braund9da1622015-09-09 18:08:03 +0000808 for (const auto &LI : MBB->liveins())
809 if (MRI->isAllocatable(LI.PhysReg))
810 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000811
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000812 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000813 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000814
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000815 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000816 while (MII != MBB->end()) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000817 MachineInstr *MI = MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000818 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000819 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000820 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000821 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
822 if (PhysRegState[Reg] == regDisabled) continue;
823 dbgs() << " " << TRI->getName(Reg);
824 switch(PhysRegState[Reg]) {
825 case regFree:
826 break;
827 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000828 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000829 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000830 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000831 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000832 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
833 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
834 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000835 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000836 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000837 break;
838 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000839 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000840 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000841 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000842 // Check that LiveVirtRegs is the inverse.
843 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
844 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000845 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000846 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000847 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000848 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000849 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000850 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000851 });
852
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000853 // Debug values are not allowed to change codegen in any way.
854 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000855 bool ScanDbgValue = true;
856 while (ScanDbgValue) {
857 ScanDbgValue = false;
858 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
859 MachineOperand &MO = MI->getOperand(i);
860 if (!MO.isReg()) continue;
861 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000862 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000863 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000864 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000865 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000866 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000867 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000868 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000869 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000870 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000871 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000872 }
Devang Pateld61b7352010-07-19 23:25:39 +0000873 else {
874 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000875 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000876 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000877 const MDNode *Var = MI->getDebugVariable();
878 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000879 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000880 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000881 assert(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000882 cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000883 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000884 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
885 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000886 .addFrameIndex(SS)
887 .addImm(Offset)
888 .addMetadata(Var)
889 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000890 DEBUG(dbgs() << "Modifying debug info due to spill:"
891 << "\t" << *NewDV);
892 // Scan NewDV operands from the beginning.
893 MI = NewDV;
894 ScanDbgValue = true;
895 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000896 }
Devang Patel57e72372010-07-09 21:48:31 +0000897 }
Devang Patel43bde962011-11-15 21:03:58 +0000898 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000899 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000900 }
901 // Next instruction.
902 continue;
903 }
904
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000905 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000906 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000907 if (MI->isCopy()) {
908 CopyDst = MI->getOperand(0).getReg();
909 CopySrc = MI->getOperand(1).getReg();
910 CopyDstSub = MI->getOperand(0).getSubReg();
911 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000912 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000913
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000914 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000915 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000916
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000917 // First scan.
918 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000919 // Find the end of the virtreg operands
920 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000921 bool hasTiedOps = false;
922 bool hasEarlyClobbers = false;
923 bool hasPartialRedefs = false;
924 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000925 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
926 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000927 // Make sure MRI knows about registers clobbered by regmasks.
928 if (MO.isRegMask()) {
929 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
930 continue;
931 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000932 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000933 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000934 if (!Reg) continue;
935 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
936 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000937 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000938 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000939 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000940 } else {
941 if (MO.isEarlyClobber())
942 hasEarlyClobbers = true;
943 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
944 hasPartialRedefs = true;
945 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000946 continue;
947 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000948 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000949 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000950 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000951 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +0000952 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
953 regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000954 hasEarlyClobbers = true;
955 } else
956 hasPhysDefs = true;
957 }
958
959 // The instruction may have virtual register operands that must be allocated
960 // the same register at use-time and def-time: early clobbers and tied
961 // operands. If there are also physical defs, these registers must avoid
962 // both physical defs and uses, making them more constrained than normal
963 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000964 // Similarly, if there are multiple defs and tied operands, we must make
965 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000966 // We didn't detect inline asm tied operands above, so just make this extra
967 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000968 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000969 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000970 handleThroughOperands(MI, VirtDead);
971 // Don't attempt coalescing when we have funny stuff going on.
972 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000973 // Pretend we have early clobbers so the use operands get marked below.
974 // This is not necessary for the common case of a single tied use.
975 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000976 }
977
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000978 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000979 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000980 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000981 MachineOperand &MO = MI->getOperand(i);
982 if (!MO.isReg()) continue;
983 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000984 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000985 if (MO.isUse()) {
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000986 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000987 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000988 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000989 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000990 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000991 }
992 }
993
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000994 // Track registers defined by instruction - early clobbers and tied uses at
995 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000996 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-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 Olesen36cf1192010-07-29 00:52:19 +00001000 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001001 unsigned Reg = MO.getReg();
1002 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001003 // Look for physreg defs and tied uses.
1004 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001005 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001006 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001007 }
1008
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001009 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001010 if (MI->isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001011 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001012 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001013 // registers in their spill slots.
1014 // Note: although this is appealing to just consider all definitions
1015 // as call-clobbered, this is not correct because some of those
1016 // definitions may be used later on and we do not want to reuse
1017 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001018 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1019 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001020
1021 // The imp-defs are skipped below, but we still need to mark those
1022 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001023 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001024 }
1025
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001026 // Third scan.
1027 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001028 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001029 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001030 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1031 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001032 unsigned Reg = MO.getReg();
1033
1034 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001035 if (!MRI->isAllocatable(Reg)) continue;
Quentin Colombet079aba72014-12-03 23:38:08 +00001036 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001037 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001038 }
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001039 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001040 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001041 if (setPhysReg(MI, i, PhysReg)) {
1042 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001043 CopyDst = 0; // cancel coalescing;
1044 } else
1045 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001046 }
1047
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001048 // Kill dead defs after the scan to ensure that multiple defs of the same
1049 // register are allocated identically. We didn't need to do this for uses
1050 // because we are crerating our own kill flags, and they are always at the
1051 // last use.
1052 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1053 killVirtReg(VirtDead[i]);
1054 VirtDead.clear();
1055
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001056 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1057 DEBUG(dbgs() << "-- coalescing: " << *MI);
1058 Coalesced.push_back(MI);
1059 } else {
1060 DEBUG(dbgs() << "<< " << *MI);
1061 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001062 }
1063
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001064 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001065 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1066 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001067
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001068 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001069 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001070 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001071 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001072 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001073
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001074 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001075}
1076
1077/// runOnMachineFunction - Register allocate the whole function
1078///
1079bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001080 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001081 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001082 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001083 MRI = &MF->getRegInfo();
Eric Christopher60621802014-10-14 07:22:00 +00001084 TRI = MF->getSubtarget().getRegisterInfo();
1085 TII = MF->getSubtarget().getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001086 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001087 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001088 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001089 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001090
Andrew Trickd3f8fe82012-02-10 04:10:36 +00001091 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1092
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001093 // initialize the virtual->physical register map to have a 'null'
1094 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001095 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001096 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001097
1098 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001099 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1100 MBBi != MBBe; ++MBBi) {
1101 MBB = &*MBBi;
1102 AllocateBasicBlock();
1103 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001104
Andrew Trickda84e642012-02-21 04:51:23 +00001105 // All machine operands and other references to virtual registers have been
1106 // replaced. Remove the virtual registers.
1107 MRI->clearVirtRegs();
1108
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001109 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001110 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001111 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001112 return true;
1113}
1114
1115FunctionPass *llvm::createFastRegisterAllocator() {
1116 return new RAFast();
1117}