blob: df6e6fb1c5bb7831796de8ca7bd5130066bc1260 [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:
Craig Topper4584cd52014-03-07 09:26:03 +0000152 const char *getPassName() const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000153 return "Fast Register Allocator";
154 }
155
Craig Topper4584cd52014-03-07 09:26:03 +0000156 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000157 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000158 MachineFunctionPass::getAnalysisUsage(AU);
159 }
160
Derek Schuffad154c82016-03-28 17:05:30 +0000161 MachineFunctionProperties getSetProperties() const override {
162 return MachineFunctionProperties().set(
163 MachineFunctionProperties::Property::AllVRegsAllocated);
164 }
165
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000166 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000167 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000168 void AllocateBasicBlock();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000169 void handleThroughOperands(MachineInstr *MI,
170 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000171 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000172 bool isLastUseOfLocalReg(MachineOperand&);
173
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000174 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000175 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000176 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000177 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000178 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000179
180 void usePhysReg(MachineOperand&);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000181 void definePhysReg(MachineInstr &MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000182 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000183 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
184 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
185 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
186 }
187 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
188 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
189 }
190 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000191 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000192 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000193 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000194 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000195 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000196 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000197 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000198 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000199 };
200 char RAFast::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000201}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000202
203/// getStackSpaceFor - This allocates space for the specified virtual register
204/// to be held on the stack.
205int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
206 // Find the location Reg would belong...
207 int SS = StackSlotForVirtReg[VirtReg];
208 if (SS != -1)
209 return SS; // Already has space allocated?
210
211 // Allocate a new stack object for this spill location...
Matthias Braun941a7052016-07-28 18:40:00 +0000212 int FrameIdx = MF->getFrameInfo().CreateSpillStackObject(RC->getSize(),
213 RC->getAlignment());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000214
215 // Assign the slot.
216 StackSlotForVirtReg[VirtReg] = FrameIdx;
217 return FrameIdx;
218}
219
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000220/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
221/// its virtual register, and it is guaranteed to be a block-local register.
222///
223bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000224 // If the register has ever been spilled or reloaded, we conservatively assume
225 // it is a global register used in multiple blocks.
226 if (StackSlotForVirtReg[MO.getReg()] != -1)
227 return false;
228
229 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000230 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000231 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000232 return false;
233 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000234}
235
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000236/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000237void RAFast::addKillFlag(const LiveReg &LR) {
238 if (!LR.LastUse) return;
239 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000240 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
241 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000242 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000243 else
244 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
245 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000246}
247
248/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000249void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000250 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000251 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
252 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000253 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000254 // Erase from LiveVirtRegs unless we're spilling in bulk.
255 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000256 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000257}
258
259/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000260void RAFast::killVirtReg(unsigned VirtReg) {
261 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
262 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000263 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000264 if (LRI != LiveVirtRegs.end())
265 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000266}
267
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000268/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000269/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000270void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000271 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
272 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000273 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000274 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
275 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000276}
277
278/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000279void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000280 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000281 LiveReg &LR = *LRI;
282 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000283
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000284 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000285 // If this physreg is used by the instruction, we want to kill it on the
286 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000287 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000288 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000289 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000290 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000291 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
292 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000293 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000294 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000295 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000296
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000297 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000298 // identify spilled location as the place to find corresponding variable's
299 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000300 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000301 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000302 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
303 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000304 const MDNode *Var = DBG->getDebugVariable();
305 const MDNode *Expr = DBG->getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000306 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000307 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000308 DebugLoc DL = DBG->getDebugLoc();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000309 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000310 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000311 MachineInstr *NewDV =
312 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000313 .addFrameIndex(FI)
314 .addImm(Offset)
315 .addMetadata(Var)
316 .addMetadata(Expr);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000317 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000318 (void)NewDV;
319 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000320 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000321 // Now this register is spilled there is should not be any DBG_VALUE
322 // pointing to this register because they are all pointing to spilled value
323 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000324 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000325 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000326 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000327 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000328 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000329}
330
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000331/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000332void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000333 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000334 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000335 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
336 // of spilling here is deterministic, if arbitrary.
337 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
338 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000339 spillVirtReg(MI, i);
340 LiveVirtRegs.clear();
341 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000342}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000343
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000344/// usePhysReg - Handle the direct use of a physical register.
345/// Check that the register is not used by a virtreg.
346/// Kill the physreg, marking it free.
347/// This may add implicit kills to MO->getParent() and invalidate MO.
348void RAFast::usePhysReg(MachineOperand &MO) {
349 unsigned PhysReg = MO.getReg();
350 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
351 "Bad usePhysReg operand");
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000352
353 // Ignore undef uses.
354 if (MO.isUndef())
355 return;
356
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000357 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000358 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000359 case regDisabled:
360 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000361 case regReserved:
362 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000363 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000364 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000365 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000366 return;
367 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000368 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000369 // wanted has been clobbered.
370 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000371 }
372
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000373 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000374 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
375 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000376 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000377 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000378 break;
379 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000380 // Either PhysReg is a subregister of Alias and we mark the
381 // whole register as free, or PhysReg is the superregister of
382 // Alias and we mark all the aliases as disabled before freeing
383 // PhysReg.
384 // In the latter case, since PhysReg was disabled, this means that
385 // its value is defined only by physical sub-registers. This check
386 // is performed by the assert of the default case in this loop.
387 // Note: The value of the superregister may only be partial
388 // defined, that is why regDisabled is a valid state for aliases.
389 assert((TRI->isSuperRegister(PhysReg, Alias) ||
390 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000391 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000392 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000393 case regFree:
394 if (TRI->isSuperRegister(PhysReg, Alias)) {
395 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000396 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000397 MO.getParent()->addRegisterKilled(Alias, TRI, true);
398 return;
399 }
400 // Some other alias was in the working set - clear it.
401 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000402 break;
403 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000404 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000405 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000406 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000407
408 // All aliases are disabled, bring register into working set.
409 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000410 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000411}
412
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000413/// definePhysReg - Mark PhysReg as reserved or free after spilling any
414/// virtregs. This is very similar to defineVirtReg except the physreg is
415/// reserved instead of allocated.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000416void RAFast::definePhysReg(MachineInstr &MI, unsigned PhysReg,
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000417 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000418 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000419 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
420 case regDisabled:
421 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000422 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000423 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000424 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000425 case regFree:
426 case regReserved:
427 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000428 return;
429 }
430
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000431 // This is a disabled register, disable all aliases.
432 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000433 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
434 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000435 switch (unsigned VirtReg = PhysRegState[Alias]) {
436 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000437 break;
438 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000439 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000440 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000441 case regFree:
442 case regReserved:
443 PhysRegState[Alias] = regDisabled;
444 if (TRI->isSuperRegister(PhysReg, Alias))
445 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000446 break;
447 }
448 }
449}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000450
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000451
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000452// calcSpillCost - Return the cost of spilling clearing out PhysReg and
453// aliases so it is free for allocation.
454// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
455// can be allocated directly.
456// Returns spillImpossible when PhysReg or an alias can't be spilled.
457unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000458 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000459 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000460 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000461 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000462 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
463 case regDisabled:
464 break;
465 case regFree:
466 return 0;
467 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000468 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
469 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000470 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000471 default: {
472 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
473 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
474 return I->Dirty ? spillDirty : spillClean;
475 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000476 }
477
Eric Christopherc3783362011-04-12 00:48:08 +0000478 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000479 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000480 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000481 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
482 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000483 switch (unsigned VirtReg = PhysRegState[Alias]) {
484 case regDisabled:
485 break;
486 case regFree:
487 ++Cost;
488 break;
489 case regReserved:
490 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000491 default: {
492 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
493 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
494 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000495 break;
496 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000497 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000498 }
499 return Cost;
500}
501
502
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000503/// assignVirtToPhysReg - This method updates local state so that we know
504/// that PhysReg is the proper container for VirtReg now. The physical
505/// register must not be used for anything else when this is called.
506///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000507void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
508 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000509 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000510 PhysRegState[PhysReg] = LR.VirtReg;
511 assert(!LR.PhysReg && "Already assigned a physreg");
512 LR.PhysReg = PhysReg;
513}
514
515RAFast::LiveRegMap::iterator
516RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
517 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
518 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
519 assignVirtToPhysReg(*LRI, PhysReg);
520 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000521}
522
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000523/// allocVirtReg - Allocate a physical register for VirtReg.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000524RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr &MI,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000525 LiveRegMap::iterator LRI,
526 unsigned Hint) {
527 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000528
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000529 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
530 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000531
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000532 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000533
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000534 // Ignore invalid hints.
535 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000536 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000537 Hint = 0;
538
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000539 // Take hint when possible.
540 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000541 // Ignore the hint if we would have to spill a dirty register.
542 unsigned Cost = calcSpillCost(Hint);
543 if (Cost < spillDirty) {
544 if (Cost)
545 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000546 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
547 // That invalidates LRI, so run a new lookup for VirtReg.
548 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000549 }
550 }
551
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000552 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000553
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000554 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000555 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000556 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000557 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000558 assignVirtToPhysReg(*LRI, PhysReg);
559 return LRI;
560 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000561 }
562
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000563 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Craig Toppercf0444b2014-11-17 05:50:14 +0000564 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000565
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000566 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000567 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000568 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000569 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000570 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
571 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000572 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000573 if (Cost == 0) {
574 assignVirtToPhysReg(*LRI, *I);
575 return LRI;
576 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000577 if (Cost < BestCost)
578 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000579 }
580
581 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000582 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000583 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
584 // That invalidates LRI, so run a new lookup for VirtReg.
585 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000586 }
587
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000588 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000589 if (MI.isInlineAsm())
590 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000591 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000592 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000593 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000594 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000595}
596
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000597/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000598RAFast::LiveRegMap::iterator RAFast::defineVirtReg(MachineInstr &MI,
599 unsigned OpNum,
600 unsigned VirtReg,
601 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000602 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
603 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000604 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000605 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000606 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000607 if (New) {
608 // If there is no hint, peek at the only use of this register.
609 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
610 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000611 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000612 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000613 if (UseMI.isCopyLike())
614 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000615 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000616 LRI = allocVirtReg(MI, LRI, Hint);
617 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000618 // Redefining a live register - kill at the last use, unless it is this
619 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000620 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000621 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000622 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000623 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000624 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000625 LRI->LastOpNum = OpNum;
626 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000627 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000628 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000629}
630
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000631/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000632RAFast::LiveRegMap::iterator RAFast::reloadVirtReg(MachineInstr &MI,
633 unsigned OpNum,
634 unsigned VirtReg,
635 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000636 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
637 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000638 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000639 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000640 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000641 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000642 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000643 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000644 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000645 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000646 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000647 << PrintReg(LRI->PhysReg, TRI) << "\n");
648 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000649 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000650 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000651 if (isLastUseOfLocalReg(MO)) {
652 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000653 if (MO.isUse())
654 MO.setIsKill();
655 else
656 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000657 } else if (MO.isKill()) {
658 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
659 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000660 } else if (MO.isDead()) {
661 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
662 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000663 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000664 } else if (MO.isKill()) {
665 // We must remove kill flags from uses of reloaded registers because the
666 // register would be killed immediately, and there might be a second use:
667 // %foo = OR %x<kill>, %x
668 // This would cause a second reload of %x into a different register.
669 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
670 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000671 } else if (MO.isDead()) {
672 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
673 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000674 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000675 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000676 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000677 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000678 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000679 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000680}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000681
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000682// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
683// subregs. This may invalidate any operand pointers.
684// Return true if the operand kills its register.
685bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
686 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000687 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000688 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000689 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000690 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000691 }
692
693 // Handle subregister index.
694 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
695 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000696
697 // A kill flag implies killing the full register. Add corresponding super
698 // register kill.
699 if (MO.isKill()) {
700 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000701 return true;
702 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000703
704 // A <def,read-undef> of a sub-register requires an implicit def of the full
705 // register.
706 if (MO.isDef() && MO.isUndef())
707 MI->addRegisterDefined(PhysReg, TRI);
708
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000709 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000710}
711
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000712// Handle special instruction operand like early clobbers and tied ops when
713// there are additional physreg defines.
714void RAFast::handleThroughOperands(MachineInstr *MI,
715 SmallVectorImpl<unsigned> &VirtDead) {
716 DEBUG(dbgs() << "Scanning for through registers:");
717 SmallSet<unsigned, 8> ThroughRegs;
718 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
719 MachineOperand &MO = MI->getOperand(i);
720 if (!MO.isReg()) continue;
721 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000722 if (!TargetRegisterInfo::isVirtualRegister(Reg))
723 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000724 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
725 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000726 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000727 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000728 }
729 }
730
731 // If any physreg defines collide with preallocated through registers,
732 // we must spill and reallocate.
733 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
734 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
735 MachineOperand &MO = MI->getOperand(i);
736 if (!MO.isReg() || !MO.isDef()) continue;
737 unsigned Reg = MO.getReg();
738 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000739 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000740 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000741 if (ThroughRegs.count(PhysRegState[*AI]))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000742 definePhysReg(*MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000743 }
744 }
745
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000746 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000747 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000748 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
749 MachineOperand &MO = MI->getOperand(i);
750 if (!MO.isReg()) continue;
751 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000752 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000753 if (MO.isUse()) {
754 unsigned DefIdx = 0;
755 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
756 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
757 << DefIdx << ".\n");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000758 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000759 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000760 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000761 // Note: we don't update the def operand yet. That would cause the normal
762 // def-scan to attempt spilling.
763 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
764 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
765 // Reload the register, but don't assign to the operand just yet.
766 // That would confuse the later phys-def processing pass.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000767 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000768 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000769 }
770 }
771
Rafael Espindola2021f382011-11-22 06:27:18 +0000772 DEBUG(dbgs() << "Allocating early clobbers.\n");
773 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
774 MachineOperand &MO = MI->getOperand(i);
775 if (!MO.isReg()) continue;
776 unsigned Reg = MO.getReg();
777 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
778 if (!MO.isEarlyClobber())
779 continue;
780 // Note: defineVirtReg may invalidate MO.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000781 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000782 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000783 if (setPhysReg(MI, i, PhysReg))
784 VirtDead.push_back(Reg);
785 }
786
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000787 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000788 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000789 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
790 MachineOperand &MO = MI->getOperand(i);
791 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
792 unsigned Reg = MO.getReg();
793 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000794 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
795 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000796 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000797 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000798
799 // Also mark PartialDefs as used to avoid reallocation.
800 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000801 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000802}
803
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000804void RAFast::AllocateBasicBlock() {
805 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000806
807 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000808 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000809
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000810 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000811
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000812 // Add live-in registers as live.
Matthias Braund9da1622015-09-09 18:08:03 +0000813 for (const auto &LI : MBB->liveins())
814 if (MRI->isAllocatable(LI.PhysReg))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000815 definePhysReg(*MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000816
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000817 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000818 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000819
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000820 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000821 while (MII != MBB->end()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000822 MachineInstr *MI = &*MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000823 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000824 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000825 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000826 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
827 if (PhysRegState[Reg] == regDisabled) continue;
828 dbgs() << " " << TRI->getName(Reg);
829 switch(PhysRegState[Reg]) {
830 case regFree:
831 break;
832 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000833 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000834 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000835 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000836 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000837 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
838 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
839 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000840 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000841 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000842 break;
843 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000844 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000845 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000846 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000847 // Check that LiveVirtRegs is the inverse.
848 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
849 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000850 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000851 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000852 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000853 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000854 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000855 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000856 });
857
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000858 // Debug values are not allowed to change codegen in any way.
859 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000860 bool ScanDbgValue = true;
861 while (ScanDbgValue) {
862 ScanDbgValue = false;
863 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
864 MachineOperand &MO = MI->getOperand(i);
865 if (!MO.isReg()) continue;
866 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000867 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000868 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000869 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000870 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000871 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000872 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000873 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000874 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000875 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000876 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000877 }
Devang Pateld61b7352010-07-19 23:25:39 +0000878 else {
879 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000880 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000881 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000882 const MDNode *Var = MI->getDebugVariable();
883 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000884 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000885 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000886 assert(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000887 cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000888 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000889 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
890 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000891 .addFrameIndex(SS)
892 .addImm(Offset)
893 .addMetadata(Var)
894 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000895 DEBUG(dbgs() << "Modifying debug info due to spill:"
896 << "\t" << *NewDV);
897 // Scan NewDV operands from the beginning.
898 MI = NewDV;
899 ScanDbgValue = true;
900 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000901 }
Devang Patel57e72372010-07-09 21:48:31 +0000902 }
Devang Patel43bde962011-11-15 21:03:58 +0000903 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000904 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000905 }
906 // Next instruction.
907 continue;
908 }
909
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000910 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000911 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000912 if (MI->isCopy()) {
913 CopyDst = MI->getOperand(0).getReg();
914 CopySrc = MI->getOperand(1).getReg();
915 CopyDstSub = MI->getOperand(0).getSubReg();
916 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000917 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000918
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000919 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000920 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000921
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000922 // First scan.
923 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000924 // Find the end of the virtreg operands
925 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000926 bool hasTiedOps = false;
927 bool hasEarlyClobbers = false;
928 bool hasPartialRedefs = false;
929 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000930 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
931 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000932 // Make sure MRI knows about registers clobbered by regmasks.
933 if (MO.isRegMask()) {
934 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
935 continue;
936 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000937 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000938 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000939 if (!Reg) continue;
940 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
941 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000942 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000943 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000944 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000945 } else {
946 if (MO.isEarlyClobber())
947 hasEarlyClobbers = true;
948 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
949 hasPartialRedefs = true;
950 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000951 continue;
952 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000953 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000954 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000955 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000956 } else if (MO.isEarlyClobber()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000957 definePhysReg(*MI, Reg,
958 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000959 hasEarlyClobbers = true;
960 } else
961 hasPhysDefs = true;
962 }
963
964 // The instruction may have virtual register operands that must be allocated
965 // the same register at use-time and def-time: early clobbers and tied
966 // operands. If there are also physical defs, these registers must avoid
967 // both physical defs and uses, making them more constrained than normal
968 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000969 // Similarly, if there are multiple defs and tied operands, we must make
970 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000971 // We didn't detect inline asm tied operands above, so just make this extra
972 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000973 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000974 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000975 handleThroughOperands(MI, VirtDead);
976 // Don't attempt coalescing when we have funny stuff going on.
977 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000978 // Pretend we have early clobbers so the use operands get marked below.
979 // This is not necessary for the common case of a single tied use.
980 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000981 }
982
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000983 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000984 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000985 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000986 MachineOperand &MO = MI->getOperand(i);
987 if (!MO.isReg()) continue;
988 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000989 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000990 if (MO.isUse()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000991 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000992 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000993 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000994 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000995 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000996 }
997 }
998
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000999 // Track registers defined by instruction - early clobbers and tied uses at
1000 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001001 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001002 if (hasEarlyClobbers) {
1003 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1004 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001005 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001006 unsigned Reg = MO.getReg();
1007 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001008 // Look for physreg defs and tied uses.
1009 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001010 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001011 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001012 }
1013
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001014 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001015 if (MI->isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001016 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001017 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001018 // registers in their spill slots.
1019 // Note: although this is appealing to just consider all definitions
1020 // as call-clobbered, this is not correct because some of those
1021 // definitions may be used later on and we do not want to reuse
1022 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001023 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1024 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001025
1026 // The imp-defs are skipped below, but we still need to mark those
1027 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001028 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001029 }
1030
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001031 // Third scan.
1032 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001033 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001034 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001035 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1036 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001037 unsigned Reg = MO.getReg();
1038
1039 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001040 if (!MRI->isAllocatable(Reg)) continue;
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001041 definePhysReg(*MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001042 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001043 }
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001044 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001045 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001046 if (setPhysReg(MI, i, PhysReg)) {
1047 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001048 CopyDst = 0; // cancel coalescing;
1049 } else
1050 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001051 }
1052
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001053 // Kill dead defs after the scan to ensure that multiple defs of the same
1054 // register are allocated identically. We didn't need to do this for uses
1055 // because we are crerating our own kill flags, and they are always at the
1056 // last use.
1057 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1058 killVirtReg(VirtDead[i]);
1059 VirtDead.clear();
1060
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001061 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1062 DEBUG(dbgs() << "-- coalescing: " << *MI);
1063 Coalesced.push_back(MI);
1064 } else {
1065 DEBUG(dbgs() << "<< " << *MI);
1066 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001067 }
1068
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001069 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001070 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1071 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001072
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001073 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001074 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001075 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001076 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001077 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001078
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001079 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001080}
1081
1082/// runOnMachineFunction - Register allocate the whole function
1083///
1084bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001085 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001086 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001087 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001088 MRI = &MF->getRegInfo();
Eric Christopher60621802014-10-14 07:22:00 +00001089 TRI = MF->getSubtarget().getRegisterInfo();
1090 TII = MF->getSubtarget().getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001091 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001092 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001093 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001094 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001095
Andrew Trickd3f8fe82012-02-10 04:10:36 +00001096 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1097
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001098 // initialize the virtual->physical register map to have a 'null'
1099 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001100 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001101 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001102
1103 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001104 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1105 MBBi != MBBe; ++MBBi) {
1106 MBB = &*MBBi;
1107 AllocateBasicBlock();
1108 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001109
Andrew Trickda84e642012-02-21 04:51:23 +00001110 // All machine operands and other references to virtual registers have been
1111 // replaced. Remove the virtual registers.
1112 MRI->clearVirtRegs();
1113
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001114 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001115 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001116 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001117 return true;
1118}
1119
1120FunctionPass *llvm::createFastRegisterAllocator() {
1121 return new RAFast();
1122}