blob: 7d9bc8cba21f479c463deabdc616e6438d067819 [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/ADT/DenseMap.h"
16#include "llvm/ADT/IndexedMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000018#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000020#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
Mehdi Amini47b292d2016-04-16 07:51:28 +000027#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/CodeGen/RegAllocRegistry.h"
29#include "llvm/CodeGen/RegisterClassInfo.h"
Reid Kleckner28865802016-04-14 18:29:59 +000030#include "llvm/IR/DebugInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000034#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000035#include <algorithm>
36using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "regalloc"
39
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000040STATISTIC(NumStores, "Number of stores added");
41STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +000042STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000043
44static RegisterRegAlloc
45 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
46
47namespace {
48 class RAFast : public MachineFunctionPass {
49 public:
50 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000051 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Andrew Trickd3f8fe82012-02-10 04:10:36 +000052 isBulkSpilling(false) {}
Derek Schuffad154c82016-03-28 17:05:30 +000053
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000054 private:
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000055 MachineFunction *MF;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +000056 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000057 const TargetRegisterInfo *TRI;
58 const TargetInstrInfo *TII;
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +000059 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000060
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +000061 // Basic block currently being allocated.
62 MachineBasicBlock *MBB;
63
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000064 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
65 // values are spilled.
66 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
67
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000068 // Everything we know about a live virtual register.
69 struct LiveReg {
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +000070 MachineInstr *LastUse; // Last instr to use reg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000071 unsigned VirtReg; // Virtual register number.
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +000072 unsigned PhysReg; // Currently held here.
73 unsigned short LastOpNum; // OpNum on LastUse.
74 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000075
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000076 explicit LiveReg(unsigned v)
Craig Topperc0196b12014-04-14 00:51:57 +000077 : LastUse(nullptr), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false){}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000078
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000079 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000080 return TargetRegisterInfo::virtReg2Index(VirtReg);
81 }
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000082 };
83
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000084 typedef SparseSet<LiveReg> LiveRegMap;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000085
86 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000087 // that is currently available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000088 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000089
Devang Patel0ab77672011-06-21 22:36:03 +000090 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +000091
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +000092 // RegState - Track the state of a physical register.
93 enum RegState {
94 // A disabled register is not available for allocation, but an alias may
95 // be in use. A register can only be moved out of the disabled state if
96 // all aliases are disabled.
97 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000098
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +000099 // A free register is not currently in use and can be allocated
100 // immediately without checking aliases.
101 regFree,
102
Evan Cheng8ea3af42011-04-22 01:40:20 +0000103 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000104 // call parameter), and it remains reserved until it is used.
105 regReserved
106
107 // A register state may also be a virtual register number, indication that
108 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000109 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000110 };
111
112 // PhysRegState - One of the RegState enums, or a virtreg.
113 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000114
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000115 // Set of register units.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000116 typedef SparseSet<unsigned> UsedInInstrSet;
117
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000118 // Set of register units that are used in the current instruction, and so
119 // cannot be allocated.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000120 UsedInInstrSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000121
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000122 // Mark a physreg as used in this instruction.
123 void markRegUsedInInstr(unsigned PhysReg) {
124 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
125 UsedInInstr.insert(*Units);
126 }
127
128 // Check if a physreg or any of its aliases are used in this instruction.
129 bool isRegUsedInInstr(unsigned PhysReg) const {
130 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
131 if (UsedInInstr.count(*Units))
132 return true;
133 return false;
134 }
135
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000136 // SkippedInstrs - Descriptors of instructions whose clobber list was
137 // ignored because all registers were spilled. It is still necessary to
138 // mark all the clobbered registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000139 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +0000140
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000141 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
142 // completely after spilling all live registers. LiveRegMap entries should
143 // not be erased.
144 bool isBulkSpilling;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000145
Alp Toker61007d82014-03-02 03:20:38 +0000146 enum : unsigned {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000147 spillClean = 1,
148 spillDirty = 100,
149 spillImpossible = ~0u
150 };
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000151 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000152 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000153
Craig Topper4584cd52014-03-07 09:26:03 +0000154 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000155 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000156 MachineFunctionPass::getAnalysisUsage(AU);
157 }
158
Matthias Braun90799ce2016-08-23 21:19:49 +0000159 MachineFunctionProperties getRequiredProperties() const override {
160 return MachineFunctionProperties().set(
161 MachineFunctionProperties::Property::NoPHIs);
162 }
163
Derek Schuffad154c82016-03-28 17:05:30 +0000164 MachineFunctionProperties getSetProperties() const override {
165 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000166 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000167 }
168
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000169 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000170 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000171 void AllocateBasicBlock();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000172 void handleThroughOperands(MachineInstr *MI,
173 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000174 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000175 bool isLastUseOfLocalReg(MachineOperand&);
176
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000177 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000178 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000179 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000180 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000181 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000182
183 void usePhysReg(MachineOperand&);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000184 void definePhysReg(MachineInstr &MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000185 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000186 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
187 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
188 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
189 }
190 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
191 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
192 }
193 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000194 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000195 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000196 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000197 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000198 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000199 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000200 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000201 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000202 };
203 char RAFast::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000204}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000205
Quentin Colombet81551142017-07-07 19:25:42 +0000206INITIALIZE_PASS(RAFast, "regallocfast", "Fast Register Allocator", false, false)
207
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000208/// getStackSpaceFor - This allocates space for the specified virtual register
209/// to be held on the stack.
210int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
211 // Find the location Reg would belong...
212 int SS = StackSlotForVirtReg[VirtReg];
213 if (SS != -1)
214 return SS; // Already has space allocated?
215
216 // Allocate a new stack object for this spill location...
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000217 unsigned Size = TRI->getSpillSize(*RC);
218 unsigned Align = TRI->getSpillAlignment(*RC);
219 int FrameIdx = MF->getFrameInfo().CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000220
221 // Assign the slot.
222 StackSlotForVirtReg[VirtReg] = FrameIdx;
223 return FrameIdx;
224}
225
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000226/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
227/// its virtual register, and it is guaranteed to be a block-local register.
228///
229bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000230 // If the register has ever been spilled or reloaded, we conservatively assume
231 // it is a global register used in multiple blocks.
232 if (StackSlotForVirtReg[MO.getReg()] != -1)
233 return false;
234
235 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000236 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000237 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000238 return false;
239 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000240}
241
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000242/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000243void RAFast::addKillFlag(const LiveReg &LR) {
244 if (!LR.LastUse) return;
245 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000246 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
247 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000248 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000249 else
250 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
251 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000252}
253
254/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000255void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000256 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000257 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
258 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000259 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000260 // Erase from LiveVirtRegs unless we're spilling in bulk.
261 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000262 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000263}
264
265/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000266void RAFast::killVirtReg(unsigned VirtReg) {
267 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
268 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000269 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000270 if (LRI != LiveVirtRegs.end())
271 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000272}
273
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000274/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000275/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000276void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000277 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
278 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000279 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000280 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
281 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000282}
283
284/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000285void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000286 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000287 LiveReg &LR = *LRI;
288 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000289
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000290 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000291 // If this physreg is used by the instruction, we want to kill it on the
292 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000293 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000294 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000295 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000296 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000297 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
298 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000299 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000300 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000301 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000302
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000303 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000304 // identify spilled location as the place to find corresponding variable's
305 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000306 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000307 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000308 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
309 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl6825fb62017-04-18 01:21:53 +0000310 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, MI, *DBG, FI);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000311 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000312 (void)NewDV;
313 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000314 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000315 // Now this register is spilled there is should not be any DBG_VALUE
316 // pointing to this register because they are all pointing to spilled value
317 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000318 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000319 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000320 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000321 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000322 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000323}
324
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000325/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000326void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000327 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000328 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000329 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
330 // of spilling here is deterministic, if arbitrary.
331 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
332 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000333 spillVirtReg(MI, i);
334 LiveVirtRegs.clear();
335 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000336}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000337
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000338/// usePhysReg - Handle the direct use of a physical register.
339/// Check that the register is not used by a virtreg.
340/// Kill the physreg, marking it free.
341/// This may add implicit kills to MO->getParent() and invalidate MO.
342void RAFast::usePhysReg(MachineOperand &MO) {
343 unsigned PhysReg = MO.getReg();
344 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
345 "Bad usePhysReg operand");
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000346
347 // Ignore undef uses.
348 if (MO.isUndef())
349 return;
350
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000351 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000352 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000353 case regDisabled:
354 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000355 case regReserved:
356 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000357 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000358 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000359 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000360 return;
361 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000362 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000363 // wanted has been clobbered.
364 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000365 }
366
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000367 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000368 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
369 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000370 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000371 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000372 break;
373 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000374 // Either PhysReg is a subregister of Alias and we mark the
375 // whole register as free, or PhysReg is the superregister of
376 // Alias and we mark all the aliases as disabled before freeing
377 // PhysReg.
378 // In the latter case, since PhysReg was disabled, this means that
379 // its value is defined only by physical sub-registers. This check
380 // is performed by the assert of the default case in this loop.
381 // Note: The value of the superregister may only be partial
382 // defined, that is why regDisabled is a valid state for aliases.
383 assert((TRI->isSuperRegister(PhysReg, Alias) ||
384 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000385 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000386 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000387 case regFree:
388 if (TRI->isSuperRegister(PhysReg, Alias)) {
389 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000390 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000391 MO.getParent()->addRegisterKilled(Alias, TRI, true);
392 return;
393 }
394 // Some other alias was in the working set - clear it.
395 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000396 break;
397 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000398 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000399 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000400 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000401
402 // All aliases are disabled, bring register into working set.
403 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000404 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000405}
406
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000407/// definePhysReg - Mark PhysReg as reserved or free after spilling any
408/// virtregs. This is very similar to defineVirtReg except the physreg is
409/// reserved instead of allocated.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000410void RAFast::definePhysReg(MachineInstr &MI, unsigned PhysReg,
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000411 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000412 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000413 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
414 case regDisabled:
415 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000416 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000417 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000418 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000419 case regFree:
420 case regReserved:
421 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000422 return;
423 }
424
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000425 // This is a disabled register, disable all aliases.
426 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000427 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
428 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000429 switch (unsigned VirtReg = PhysRegState[Alias]) {
430 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000431 break;
432 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000433 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000434 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000435 case regFree:
436 case regReserved:
437 PhysRegState[Alias] = regDisabled;
438 if (TRI->isSuperRegister(PhysReg, Alias))
439 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000440 break;
441 }
442 }
443}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000444
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000445
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000446// calcSpillCost - Return the cost of spilling clearing out PhysReg and
447// aliases so it is free for allocation.
448// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
449// can be allocated directly.
450// Returns spillImpossible when PhysReg or an alias can't be spilled.
451unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000452 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000453 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000454 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000455 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000456 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
457 case regDisabled:
458 break;
459 case regFree:
460 return 0;
461 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000462 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
463 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000464 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000465 default: {
466 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
467 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
468 return I->Dirty ? spillDirty : spillClean;
469 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000470 }
471
Eric Christopherc3783362011-04-12 00:48:08 +0000472 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000473 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000474 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000475 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
476 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000477 switch (unsigned VirtReg = PhysRegState[Alias]) {
478 case regDisabled:
479 break;
480 case regFree:
481 ++Cost;
482 break;
483 case regReserved:
484 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000485 default: {
486 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
487 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
488 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000489 break;
490 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000491 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000492 }
493 return Cost;
494}
495
496
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000497/// assignVirtToPhysReg - This method updates local state so that we know
498/// that PhysReg is the proper container for VirtReg now. The physical
499/// register must not be used for anything else when this is called.
500///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000501void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
502 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000503 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000504 PhysRegState[PhysReg] = LR.VirtReg;
505 assert(!LR.PhysReg && "Already assigned a physreg");
506 LR.PhysReg = PhysReg;
507}
508
509RAFast::LiveRegMap::iterator
510RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
511 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
512 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
513 assignVirtToPhysReg(*LRI, PhysReg);
514 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000515}
516
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000517/// allocVirtReg - Allocate a physical register for VirtReg.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000518RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr &MI,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000519 LiveRegMap::iterator LRI,
520 unsigned Hint) {
521 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000522
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000523 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
524 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000525
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000526 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000527
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000528 // Ignore invalid hints.
529 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000530 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000531 Hint = 0;
532
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000533 // Take hint when possible.
534 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000535 // Ignore the hint if we would have to spill a dirty register.
536 unsigned Cost = calcSpillCost(Hint);
537 if (Cost < spillDirty) {
538 if (Cost)
539 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000540 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
541 // That invalidates LRI, so run a new lookup for VirtReg.
542 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000543 }
544 }
545
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000546 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000547
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000548 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000549 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000550 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000551 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000552 assignVirtToPhysReg(*LRI, PhysReg);
553 return LRI;
554 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000555 }
556
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000557 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Craig Toppercf0444b2014-11-17 05:50:14 +0000558 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000559
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000560 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000561 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000562 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000563 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000564 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
565 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000566 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000567 if (Cost == 0) {
568 assignVirtToPhysReg(*LRI, *I);
569 return LRI;
570 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000571 if (Cost < BestCost)
572 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000573 }
574
575 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000576 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000577 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
578 // That invalidates LRI, so run a new lookup for VirtReg.
579 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000580 }
581
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000582 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000583 if (MI.isInlineAsm())
584 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000585 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000586 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000587 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000588 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000589}
590
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000591/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000592RAFast::LiveRegMap::iterator RAFast::defineVirtReg(MachineInstr &MI,
593 unsigned OpNum,
594 unsigned VirtReg,
595 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000596 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
597 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000598 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000599 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000600 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-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)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000605 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000606 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000607 if (UseMI.isCopyLike())
608 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000609 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000610 LRI = allocVirtReg(MI, LRI, Hint);
611 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-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.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000614 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000615 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000616 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000617 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000618 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000619 LRI->LastOpNum = OpNum;
620 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000621 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000622 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000623}
624
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000625/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000626RAFast::LiveRegMap::iterator RAFast::reloadVirtReg(MachineInstr &MI,
627 unsigned OpNum,
628 unsigned VirtReg,
629 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000630 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
631 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000632 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000633 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000634 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000635 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000636 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000637 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000638 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000639 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000640 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000641 << PrintReg(LRI->PhysReg, TRI) << "\n");
642 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000643 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000644 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000645 if (isLastUseOfLocalReg(MO)) {
646 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000647 if (MO.isUse())
648 MO.setIsKill();
649 else
650 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000651 } else if (MO.isKill()) {
652 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
653 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000654 } else if (MO.isDead()) {
655 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
656 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000657 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000658 } else if (MO.isKill()) {
659 // We must remove kill flags from uses of reloaded registers because the
660 // register would be killed immediately, and there might be a second use:
661 // %foo = OR %x<kill>, %x
662 // This would cause a second reload of %x into a different register.
663 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
664 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000665 } else if (MO.isDead()) {
666 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
667 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000668 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000669 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000670 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000671 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000672 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000673 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000674}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000675
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000676// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
677// subregs. This may invalidate any operand pointers.
678// Return true if the operand kills its register.
679bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
680 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000681 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000682 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000683 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000684 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000685 }
686
687 // Handle subregister index.
688 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
689 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000690
691 // A kill flag implies killing the full register. Add corresponding super
692 // register kill.
693 if (MO.isKill()) {
694 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000695 return true;
696 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000697
698 // A <def,read-undef> of a sub-register requires an implicit def of the full
699 // register.
700 if (MO.isDef() && MO.isUndef())
701 MI->addRegisterDefined(PhysReg, TRI);
702
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000703 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000704}
705
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000706// Handle special instruction operand like early clobbers and tied ops when
707// there are additional physreg defines.
708void RAFast::handleThroughOperands(MachineInstr *MI,
709 SmallVectorImpl<unsigned> &VirtDead) {
710 DEBUG(dbgs() << "Scanning for through registers:");
711 SmallSet<unsigned, 8> ThroughRegs;
712 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
713 MachineOperand &MO = MI->getOperand(i);
714 if (!MO.isReg()) continue;
715 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000716 if (!TargetRegisterInfo::isVirtualRegister(Reg))
717 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000718 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
719 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000720 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000721 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000722 }
723 }
724
725 // If any physreg defines collide with preallocated through registers,
726 // we must spill and reallocate.
727 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
728 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
729 MachineOperand &MO = MI->getOperand(i);
730 if (!MO.isReg() || !MO.isDef()) continue;
731 unsigned Reg = MO.getReg();
732 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000733 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000734 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000735 if (ThroughRegs.count(PhysRegState[*AI]))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000736 definePhysReg(*MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000737 }
738 }
739
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000740 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000741 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000742 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
743 MachineOperand &MO = MI->getOperand(i);
744 if (!MO.isReg()) continue;
745 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000746 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000747 if (MO.isUse()) {
748 unsigned DefIdx = 0;
749 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
750 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
751 << DefIdx << ".\n");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000752 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000753 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000754 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000755 // Note: we don't update the def operand yet. That would cause the normal
756 // def-scan to attempt spilling.
757 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
758 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
759 // Reload the register, but don't assign to the operand just yet.
760 // That would confuse the later phys-def processing pass.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000761 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000762 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000763 }
764 }
765
Rafael Espindola2021f382011-11-22 06:27:18 +0000766 DEBUG(dbgs() << "Allocating early clobbers.\n");
767 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
768 MachineOperand &MO = MI->getOperand(i);
769 if (!MO.isReg()) continue;
770 unsigned Reg = MO.getReg();
771 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
772 if (!MO.isEarlyClobber())
773 continue;
774 // Note: defineVirtReg may invalidate MO.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000775 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000776 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000777 if (setPhysReg(MI, i, PhysReg))
778 VirtDead.push_back(Reg);
779 }
780
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000781 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000782 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000783 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
784 MachineOperand &MO = MI->getOperand(i);
785 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
786 unsigned Reg = MO.getReg();
787 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000788 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
789 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000790 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000791 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000792
793 // Also mark PartialDefs as used to avoid reallocation.
794 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000795 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000796}
797
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000798void RAFast::AllocateBasicBlock() {
799 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000800
801 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000802 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000803
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000804 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000805
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000806 // Add live-in registers as live.
Matthias Braund9da1622015-09-09 18:08:03 +0000807 for (const auto &LI : MBB->liveins())
808 if (MRI->isAllocatable(LI.PhysReg))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000809 definePhysReg(*MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000810
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000811 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000812 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000813
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000814 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000815 while (MII != MBB->end()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000816 MachineInstr *MI = &*MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000817 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000818 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000819 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-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 Olesend74a5642010-05-13 20:43:17 +0000827 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000828 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000829 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000830 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-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 Olesenf1b30292010-05-11 18:54:45 +0000834 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000835 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000836 break;
837 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000838 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000839 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000840 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-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 Olesen9c4cd1b2012-02-22 01:02:37 +0000844 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000845 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000846 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000847 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000848 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000849 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000850 });
851
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000852 // Debug values are not allowed to change codegen in any way.
853 if (MI->isDebugValue()) {
Devang Pateld61b7352010-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 Olesen2fb5b312011-01-10 02:58:51 +0000861 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000862 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000863 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000864 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000865 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000866 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000867 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000868 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000869 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000870 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000871 }
Devang Pateld61b7352010-07-19 23:25:39 +0000872 else {
873 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000874 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000875 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000876 const MDNode *Var = MI->getDebugVariable();
877 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000878 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000879 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000880 assert(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000881 cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000882 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000883 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
884 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000885 .addFrameIndex(SS)
886 .addImm(Offset)
887 .addMetadata(Var)
888 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000889 DEBUG(dbgs() << "Modifying debug info due to spill:"
890 << "\t" << *NewDV);
891 // Scan NewDV operands from the beginning.
892 MI = NewDV;
893 ScanDbgValue = true;
894 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000895 }
Devang Patel57e72372010-07-09 21:48:31 +0000896 }
Devang Patel43bde962011-11-15 21:03:58 +0000897 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000898 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000899 }
900 // Next instruction.
901 continue;
902 }
903
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000904 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000905 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000906 if (MI->isCopy()) {
907 CopyDst = MI->getOperand(0).getReg();
908 CopySrc = MI->getOperand(1).getReg();
909 CopyDstSub = MI->getOperand(0).getSubReg();
910 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000911 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000912
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000913 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000914 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000915
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000916 // First scan.
917 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000918 // Find the end of the virtreg operands
919 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000920 bool hasTiedOps = false;
921 bool hasEarlyClobbers = false;
922 bool hasPartialRedefs = false;
923 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000924 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
925 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000926 // Make sure MRI knows about registers clobbered by regmasks.
927 if (MO.isRegMask()) {
928 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
929 continue;
930 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000931 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000932 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000933 if (!Reg) continue;
934 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
935 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000936 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000937 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000938 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000939 } else {
940 if (MO.isEarlyClobber())
941 hasEarlyClobbers = true;
942 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
943 hasPartialRedefs = true;
944 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000945 continue;
946 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000947 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000948 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000949 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000950 } else if (MO.isEarlyClobber()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000951 definePhysReg(*MI, Reg,
952 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000953 hasEarlyClobbers = true;
954 } else
955 hasPhysDefs = true;
956 }
957
958 // The instruction may have virtual register operands that must be allocated
959 // the same register at use-time and def-time: early clobbers and tied
960 // operands. If there are also physical defs, these registers must avoid
961 // both physical defs and uses, making them more constrained than normal
962 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000963 // Similarly, if there are multiple defs and tied operands, we must make
964 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000965 // We didn't detect inline asm tied operands above, so just make this extra
966 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000967 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000968 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000969 handleThroughOperands(MI, VirtDead);
970 // Don't attempt coalescing when we have funny stuff going on.
971 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000972 // Pretend we have early clobbers so the use operands get marked below.
973 // This is not necessary for the common case of a single tied use.
974 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000975 }
976
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000977 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000978 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000979 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000980 MachineOperand &MO = MI->getOperand(i);
981 if (!MO.isReg()) continue;
982 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000983 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000984 if (MO.isUse()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000985 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000986 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000987 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000988 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000989 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000990 }
991 }
992
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000993 // Track registers defined by instruction - early clobbers and tied uses at
994 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000995 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000996 if (hasEarlyClobbers) {
997 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
998 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000999 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001000 unsigned Reg = MO.getReg();
1001 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001002 // Look for physreg defs and tied uses.
1003 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001004 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001005 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001006 }
1007
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001008 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001009 if (MI->isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001010 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001011 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001012 // registers in their spill slots.
1013 // Note: although this is appealing to just consider all definitions
1014 // as call-clobbered, this is not correct because some of those
1015 // definitions may be used later on and we do not want to reuse
1016 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001017 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1018 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001019
1020 // The imp-defs are skipped below, but we still need to mark those
1021 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001022 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001023 }
1024
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001025 // Third scan.
1026 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001027 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001028 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001029 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1030 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001031 unsigned Reg = MO.getReg();
1032
1033 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001034 if (!MRI->isAllocatable(Reg)) continue;
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001035 definePhysReg(*MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001036 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001037 }
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001038 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001039 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001040 if (setPhysReg(MI, i, PhysReg)) {
1041 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001042 CopyDst = 0; // cancel coalescing;
1043 } else
1044 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001045 }
1046
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001047 // Kill dead defs after the scan to ensure that multiple defs of the same
1048 // register are allocated identically. We didn't need to do this for uses
1049 // because we are crerating our own kill flags, and they are always at the
1050 // last use.
1051 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1052 killVirtReg(VirtDead[i]);
1053 VirtDead.clear();
1054
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001055 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1056 DEBUG(dbgs() << "-- coalescing: " << *MI);
1057 Coalesced.push_back(MI);
1058 } else {
1059 DEBUG(dbgs() << "<< " << *MI);
1060 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001061 }
1062
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001063 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001064 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1065 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001066
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001067 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001068 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001069 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001070 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001071 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001072
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001073 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001074}
1075
1076/// runOnMachineFunction - Register allocate the whole function
1077///
1078bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001079 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001080 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001081 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001082 MRI = &MF->getRegInfo();
Eric Christopher60621802014-10-14 07:22:00 +00001083 TRI = MF->getSubtarget().getRegisterInfo();
1084 TII = MF->getSubtarget().getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001085 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001086 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001087 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001088 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001089
1090 // initialize the virtual->physical register map to have a 'null'
1091 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001092 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001093 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001094
1095 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001096 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1097 MBBi != MBBe; ++MBBi) {
1098 MBB = &*MBBi;
1099 AllocateBasicBlock();
1100 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001101
Andrew Trickda84e642012-02-21 04:51:23 +00001102 // All machine operands and other references to virtual registers have been
1103 // replaced. Remove the virtual registers.
1104 MRI->clearVirtRegs();
1105
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001106 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001107 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001108 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001109 return true;
1110}
1111
1112FunctionPass *llvm::createFastRegisterAllocator() {
1113 return new RAFast();
1114}