blob: c606b7b83310477bbf8f61f7497a3477343dca62 [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
206/// getStackSpaceFor - This allocates space for the specified virtual register
207/// to be held on the stack.
208int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
209 // Find the location Reg would belong...
210 int SS = StackSlotForVirtReg[VirtReg];
211 if (SS != -1)
212 return SS; // Already has space allocated?
213
214 // Allocate a new stack object for this spill location...
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000215 unsigned Size = TRI->getSpillSize(*RC);
216 unsigned Align = TRI->getSpillAlignment(*RC);
217 int FrameIdx = MF->getFrameInfo().CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000218
219 // Assign the slot.
220 StackSlotForVirtReg[VirtReg] = FrameIdx;
221 return FrameIdx;
222}
223
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000224/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
225/// its virtual register, and it is guaranteed to be a block-local register.
226///
227bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000228 // If the register has ever been spilled or reloaded, we conservatively assume
229 // it is a global register used in multiple blocks.
230 if (StackSlotForVirtReg[MO.getReg()] != -1)
231 return false;
232
233 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000234 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000235 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000236 return false;
237 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000238}
239
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000240/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000241void RAFast::addKillFlag(const LiveReg &LR) {
242 if (!LR.LastUse) return;
243 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000244 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
245 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000246 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000247 else
248 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
249 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000250}
251
252/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000253void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000254 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000255 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
256 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000257 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000258 // Erase from LiveVirtRegs unless we're spilling in bulk.
259 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000260 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000261}
262
263/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000264void RAFast::killVirtReg(unsigned VirtReg) {
265 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
266 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000267 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000268 if (LRI != LiveVirtRegs.end())
269 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000270}
271
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000272/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000273/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000274void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000275 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
276 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000277 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000278 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
279 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000280}
281
282/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000283void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000284 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000285 LiveReg &LR = *LRI;
286 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000287
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000288 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000289 // If this physreg is used by the instruction, we want to kill it on the
290 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000291 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000292 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000293 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000294 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000295 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
296 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000297 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000298 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000299 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000300
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000301 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000302 // identify spilled location as the place to find corresponding variable's
303 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000304 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000305 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000306 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
307 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl6825fb62017-04-18 01:21:53 +0000308 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, MI, *DBG, FI);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000309 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000310 (void)NewDV;
311 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000312 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000313 // Now this register is spilled there is should not be any DBG_VALUE
314 // pointing to this register because they are all pointing to spilled value
315 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000316 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000317 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000318 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000319 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000320 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000321}
322
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000323/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000324void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000325 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000326 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000327 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
328 // of spilling here is deterministic, if arbitrary.
329 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
330 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000331 spillVirtReg(MI, i);
332 LiveVirtRegs.clear();
333 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000334}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000335
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000336/// usePhysReg - Handle the direct use of a physical register.
337/// Check that the register is not used by a virtreg.
338/// Kill the physreg, marking it free.
339/// This may add implicit kills to MO->getParent() and invalidate MO.
340void RAFast::usePhysReg(MachineOperand &MO) {
341 unsigned PhysReg = MO.getReg();
342 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
343 "Bad usePhysReg operand");
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000344
345 // Ignore undef uses.
346 if (MO.isUndef())
347 return;
348
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000349 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000350 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000351 case regDisabled:
352 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000353 case regReserved:
354 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000355 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000356 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000357 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000358 return;
359 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000360 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000361 // wanted has been clobbered.
362 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000363 }
364
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000365 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000366 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
367 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000368 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000369 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000370 break;
371 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000372 // Either PhysReg is a subregister of Alias and we mark the
373 // whole register as free, or PhysReg is the superregister of
374 // Alias and we mark all the aliases as disabled before freeing
375 // PhysReg.
376 // In the latter case, since PhysReg was disabled, this means that
377 // its value is defined only by physical sub-registers. This check
378 // is performed by the assert of the default case in this loop.
379 // Note: The value of the superregister may only be partial
380 // defined, that is why regDisabled is a valid state for aliases.
381 assert((TRI->isSuperRegister(PhysReg, Alias) ||
382 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000383 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000384 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000385 case regFree:
386 if (TRI->isSuperRegister(PhysReg, Alias)) {
387 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000388 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000389 MO.getParent()->addRegisterKilled(Alias, TRI, true);
390 return;
391 }
392 // Some other alias was in the working set - clear it.
393 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000394 break;
395 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000396 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000397 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000398 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000399
400 // All aliases are disabled, bring register into working set.
401 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000402 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000403}
404
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000405/// definePhysReg - Mark PhysReg as reserved or free after spilling any
406/// virtregs. This is very similar to defineVirtReg except the physreg is
407/// reserved instead of allocated.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000408void RAFast::definePhysReg(MachineInstr &MI, unsigned PhysReg,
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000409 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000410 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000411 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
412 case regDisabled:
413 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000414 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000415 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000416 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000417 case regFree:
418 case regReserved:
419 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000420 return;
421 }
422
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000423 // This is a disabled register, disable all aliases.
424 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000425 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
426 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000427 switch (unsigned VirtReg = PhysRegState[Alias]) {
428 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000429 break;
430 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000431 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000432 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000433 case regFree:
434 case regReserved:
435 PhysRegState[Alias] = regDisabled;
436 if (TRI->isSuperRegister(PhysReg, Alias))
437 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000438 break;
439 }
440 }
441}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000442
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000443
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000444// calcSpillCost - Return the cost of spilling clearing out PhysReg and
445// aliases so it is free for allocation.
446// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
447// can be allocated directly.
448// Returns spillImpossible when PhysReg or an alias can't be spilled.
449unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000450 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000451 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000452 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000453 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000454 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
455 case regDisabled:
456 break;
457 case regFree:
458 return 0;
459 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000460 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
461 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000462 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000463 default: {
464 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
465 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
466 return I->Dirty ? spillDirty : spillClean;
467 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000468 }
469
Eric Christopherc3783362011-04-12 00:48:08 +0000470 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000471 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000472 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000473 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
474 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000475 switch (unsigned VirtReg = PhysRegState[Alias]) {
476 case regDisabled:
477 break;
478 case regFree:
479 ++Cost;
480 break;
481 case regReserved:
482 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000483 default: {
484 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
485 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
486 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000487 break;
488 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000489 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000490 }
491 return Cost;
492}
493
494
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000495/// assignVirtToPhysReg - This method updates local state so that we know
496/// that PhysReg is the proper container for VirtReg now. The physical
497/// register must not be used for anything else when this is called.
498///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000499void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
500 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000501 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000502 PhysRegState[PhysReg] = LR.VirtReg;
503 assert(!LR.PhysReg && "Already assigned a physreg");
504 LR.PhysReg = PhysReg;
505}
506
507RAFast::LiveRegMap::iterator
508RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
509 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
510 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
511 assignVirtToPhysReg(*LRI, PhysReg);
512 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000513}
514
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000515/// allocVirtReg - Allocate a physical register for VirtReg.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000516RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr &MI,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000517 LiveRegMap::iterator LRI,
518 unsigned Hint) {
519 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000520
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000521 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
522 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000523
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000524 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000525
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000526 // Ignore invalid hints.
527 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000528 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000529 Hint = 0;
530
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000531 // Take hint when possible.
532 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000533 // Ignore the hint if we would have to spill a dirty register.
534 unsigned Cost = calcSpillCost(Hint);
535 if (Cost < spillDirty) {
536 if (Cost)
537 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000538 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
539 // That invalidates LRI, so run a new lookup for VirtReg.
540 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000541 }
542 }
543
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000544 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000545
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000546 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000547 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000548 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000549 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000550 assignVirtToPhysReg(*LRI, PhysReg);
551 return LRI;
552 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000553 }
554
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000555 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Craig Toppercf0444b2014-11-17 05:50:14 +0000556 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000557
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000558 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000559 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000560 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000561 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000562 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
563 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000564 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000565 if (Cost == 0) {
566 assignVirtToPhysReg(*LRI, *I);
567 return LRI;
568 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000569 if (Cost < BestCost)
570 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000571 }
572
573 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000574 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000575 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
576 // That invalidates LRI, so run a new lookup for VirtReg.
577 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000578 }
579
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000580 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000581 if (MI.isInlineAsm())
582 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000583 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000584 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000585 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000586 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000587}
588
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000589/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000590RAFast::LiveRegMap::iterator RAFast::defineVirtReg(MachineInstr &MI,
591 unsigned OpNum,
592 unsigned VirtReg,
593 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000594 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
595 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000596 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000597 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000598 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000599 if (New) {
600 // If there is no hint, peek at the only use of this register.
601 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
602 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000603 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000604 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000605 if (UseMI.isCopyLike())
606 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000607 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000608 LRI = allocVirtReg(MI, LRI, Hint);
609 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000610 // Redefining a live register - kill at the last use, unless it is this
611 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000612 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000613 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000614 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000615 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000616 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000617 LRI->LastOpNum = OpNum;
618 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000619 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000620 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000621}
622
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000623/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000624RAFast::LiveRegMap::iterator RAFast::reloadVirtReg(MachineInstr &MI,
625 unsigned OpNum,
626 unsigned VirtReg,
627 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000628 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
629 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000630 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000631 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000632 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000633 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000634 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000635 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000636 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000637 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000638 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000639 << PrintReg(LRI->PhysReg, TRI) << "\n");
640 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000641 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000642 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000643 if (isLastUseOfLocalReg(MO)) {
644 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000645 if (MO.isUse())
646 MO.setIsKill();
647 else
648 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000649 } else if (MO.isKill()) {
650 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
651 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000652 } else if (MO.isDead()) {
653 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
654 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000655 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000656 } else if (MO.isKill()) {
657 // We must remove kill flags from uses of reloaded registers because the
658 // register would be killed immediately, and there might be a second use:
659 // %foo = OR %x<kill>, %x
660 // This would cause a second reload of %x into a different register.
661 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
662 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000663 } else if (MO.isDead()) {
664 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
665 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000666 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000667 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000668 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000669 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000670 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000671 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000672}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000673
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000674// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
675// subregs. This may invalidate any operand pointers.
676// Return true if the operand kills its register.
677bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
678 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000679 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000680 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000681 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000682 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000683 }
684
685 // Handle subregister index.
686 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
687 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000688
689 // A kill flag implies killing the full register. Add corresponding super
690 // register kill.
691 if (MO.isKill()) {
692 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000693 return true;
694 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000695
696 // A <def,read-undef> of a sub-register requires an implicit def of the full
697 // register.
698 if (MO.isDef() && MO.isUndef())
699 MI->addRegisterDefined(PhysReg, TRI);
700
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000701 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000702}
703
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000704// Handle special instruction operand like early clobbers and tied ops when
705// there are additional physreg defines.
706void RAFast::handleThroughOperands(MachineInstr *MI,
707 SmallVectorImpl<unsigned> &VirtDead) {
708 DEBUG(dbgs() << "Scanning for through registers:");
709 SmallSet<unsigned, 8> ThroughRegs;
710 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
711 MachineOperand &MO = MI->getOperand(i);
712 if (!MO.isReg()) continue;
713 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000714 if (!TargetRegisterInfo::isVirtualRegister(Reg))
715 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000716 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
717 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000718 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000719 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000720 }
721 }
722
723 // If any physreg defines collide with preallocated through registers,
724 // we must spill and reallocate.
725 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
726 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
727 MachineOperand &MO = MI->getOperand(i);
728 if (!MO.isReg() || !MO.isDef()) continue;
729 unsigned Reg = MO.getReg();
730 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000731 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000732 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000733 if (ThroughRegs.count(PhysRegState[*AI]))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000734 definePhysReg(*MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000735 }
736 }
737
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000738 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000739 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000740 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
741 MachineOperand &MO = MI->getOperand(i);
742 if (!MO.isReg()) continue;
743 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000744 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000745 if (MO.isUse()) {
746 unsigned DefIdx = 0;
747 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
748 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
749 << DefIdx << ".\n");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000750 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000751 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000752 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000753 // Note: we don't update the def operand yet. That would cause the normal
754 // def-scan to attempt spilling.
755 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
756 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
757 // Reload the register, but don't assign to the operand just yet.
758 // That would confuse the later phys-def processing pass.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000759 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000760 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000761 }
762 }
763
Rafael Espindola2021f382011-11-22 06:27:18 +0000764 DEBUG(dbgs() << "Allocating early clobbers.\n");
765 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
766 MachineOperand &MO = MI->getOperand(i);
767 if (!MO.isReg()) continue;
768 unsigned Reg = MO.getReg();
769 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
770 if (!MO.isEarlyClobber())
771 continue;
772 // Note: defineVirtReg may invalidate MO.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000773 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000774 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000775 if (setPhysReg(MI, i, PhysReg))
776 VirtDead.push_back(Reg);
777 }
778
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000779 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000780 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000781 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
782 MachineOperand &MO = MI->getOperand(i);
783 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
784 unsigned Reg = MO.getReg();
785 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000786 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
787 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000788 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000789 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000790
791 // Also mark PartialDefs as used to avoid reallocation.
792 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000793 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000794}
795
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000796void RAFast::AllocateBasicBlock() {
797 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000798
799 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000800 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000801
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000802 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000803
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000804 // Add live-in registers as live.
Matthias Braund9da1622015-09-09 18:08:03 +0000805 for (const auto &LI : MBB->liveins())
806 if (MRI->isAllocatable(LI.PhysReg))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000807 definePhysReg(*MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000808
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000809 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000810 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000811
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000812 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000813 while (MII != MBB->end()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000814 MachineInstr *MI = &*MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000815 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000816 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000817 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000818 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
819 if (PhysRegState[Reg] == regDisabled) continue;
820 dbgs() << " " << TRI->getName(Reg);
821 switch(PhysRegState[Reg]) {
822 case regFree:
823 break;
824 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000825 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000826 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000827 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000828 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000829 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
830 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
831 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000832 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000833 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000834 break;
835 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000836 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000837 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000838 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000839 // Check that LiveVirtRegs is the inverse.
840 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
841 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000842 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000843 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000844 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000845 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000846 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000847 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000848 });
849
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000850 // Debug values are not allowed to change codegen in any way.
851 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000852 bool ScanDbgValue = true;
853 while (ScanDbgValue) {
854 ScanDbgValue = false;
855 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
856 MachineOperand &MO = MI->getOperand(i);
857 if (!MO.isReg()) continue;
858 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000859 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000860 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000861 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000862 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000863 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000864 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000865 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000866 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000867 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000868 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000869 }
Devang Pateld61b7352010-07-19 23:25:39 +0000870 else {
871 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000872 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000873 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000874 const MDNode *Var = MI->getDebugVariable();
875 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000876 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000877 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000878 assert(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000879 cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000880 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000881 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
882 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000883 .addFrameIndex(SS)
884 .addImm(Offset)
885 .addMetadata(Var)
886 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000887 DEBUG(dbgs() << "Modifying debug info due to spill:"
888 << "\t" << *NewDV);
889 // Scan NewDV operands from the beginning.
890 MI = NewDV;
891 ScanDbgValue = true;
892 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000893 }
Devang Patel57e72372010-07-09 21:48:31 +0000894 }
Devang Patel43bde962011-11-15 21:03:58 +0000895 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000896 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000897 }
898 // Next instruction.
899 continue;
900 }
901
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000902 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000903 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000904 if (MI->isCopy()) {
905 CopyDst = MI->getOperand(0).getReg();
906 CopySrc = MI->getOperand(1).getReg();
907 CopyDstSub = MI->getOperand(0).getSubReg();
908 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000909 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000910
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000911 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000912 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000913
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000914 // First scan.
915 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000916 // Find the end of the virtreg operands
917 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000918 bool hasTiedOps = false;
919 bool hasEarlyClobbers = false;
920 bool hasPartialRedefs = false;
921 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000922 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
923 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000924 // Make sure MRI knows about registers clobbered by regmasks.
925 if (MO.isRegMask()) {
926 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
927 continue;
928 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000929 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000930 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000931 if (!Reg) continue;
932 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
933 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000934 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000935 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000936 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000937 } else {
938 if (MO.isEarlyClobber())
939 hasEarlyClobbers = true;
940 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
941 hasPartialRedefs = true;
942 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000943 continue;
944 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000945 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000946 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000947 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000948 } else if (MO.isEarlyClobber()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000949 definePhysReg(*MI, Reg,
950 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000951 hasEarlyClobbers = true;
952 } else
953 hasPhysDefs = true;
954 }
955
956 // The instruction may have virtual register operands that must be allocated
957 // the same register at use-time and def-time: early clobbers and tied
958 // operands. If there are also physical defs, these registers must avoid
959 // both physical defs and uses, making them more constrained than normal
960 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000961 // Similarly, if there are multiple defs and tied operands, we must make
962 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000963 // We didn't detect inline asm tied operands above, so just make this extra
964 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000965 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000966 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000967 handleThroughOperands(MI, VirtDead);
968 // Don't attempt coalescing when we have funny stuff going on.
969 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000970 // Pretend we have early clobbers so the use operands get marked below.
971 // This is not necessary for the common case of a single tied use.
972 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000973 }
974
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000975 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000976 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000977 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000978 MachineOperand &MO = MI->getOperand(i);
979 if (!MO.isReg()) continue;
980 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000981 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000982 if (MO.isUse()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000983 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000984 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000985 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000986 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000987 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000988 }
989 }
990
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000991 // Track registers defined by instruction - early clobbers and tied uses at
992 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000993 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000994 if (hasEarlyClobbers) {
995 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
996 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000997 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000998 unsigned Reg = MO.getReg();
999 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001000 // Look for physreg defs and tied uses.
1001 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001002 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001003 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001004 }
1005
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001006 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001007 if (MI->isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001008 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001009 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001010 // registers in their spill slots.
1011 // Note: although this is appealing to just consider all definitions
1012 // as call-clobbered, this is not correct because some of those
1013 // definitions may be used later on and we do not want to reuse
1014 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001015 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1016 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001017
1018 // The imp-defs are skipped below, but we still need to mark those
1019 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001020 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001021 }
1022
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001023 // Third scan.
1024 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001025 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001026 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001027 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1028 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001029 unsigned Reg = MO.getReg();
1030
1031 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001032 if (!MRI->isAllocatable(Reg)) continue;
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001033 definePhysReg(*MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001034 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001035 }
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001036 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001037 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001038 if (setPhysReg(MI, i, PhysReg)) {
1039 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001040 CopyDst = 0; // cancel coalescing;
1041 } else
1042 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001043 }
1044
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001045 // Kill dead defs after the scan to ensure that multiple defs of the same
1046 // register are allocated identically. We didn't need to do this for uses
1047 // because we are crerating our own kill flags, and they are always at the
1048 // last use.
1049 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1050 killVirtReg(VirtDead[i]);
1051 VirtDead.clear();
1052
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001053 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1054 DEBUG(dbgs() << "-- coalescing: " << *MI);
1055 Coalesced.push_back(MI);
1056 } else {
1057 DEBUG(dbgs() << "<< " << *MI);
1058 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001059 }
1060
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001061 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001062 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1063 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001064
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001065 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001066 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001067 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001068 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001069 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001070
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001071 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072}
1073
1074/// runOnMachineFunction - Register allocate the whole function
1075///
1076bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001077 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001078 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001079 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001080 MRI = &MF->getRegInfo();
Eric Christopher60621802014-10-14 07:22:00 +00001081 TRI = MF->getSubtarget().getRegisterInfo();
1082 TII = MF->getSubtarget().getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001083 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001084 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001085 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001086 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001087
1088 // initialize the virtual->physical register map to have a 'null'
1089 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001090 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001091 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001092
1093 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001094 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1095 MBBi != MBBe; ++MBBi) {
1096 MBB = &*MBBi;
1097 AllocateBasicBlock();
1098 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001099
Andrew Trickda84e642012-02-21 04:51:23 +00001100 // All machine operands and other references to virtual registers have been
1101 // replaced. Remove the virtual registers.
1102 MRI->clearVirtRegs();
1103
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001104 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001105 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001106 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001107 return true;
1108}
1109
1110FunctionPass *llvm::createFastRegisterAllocator() {
1111 return new RAFast();
1112}