blob: febb29c9dd47e4d9c43507714cbe342f65009a02 [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
Matthias Braun90799ce2016-08-23 21:19:49 +0000161 MachineFunctionProperties getRequiredProperties() const override {
162 return MachineFunctionProperties().set(
163 MachineFunctionProperties::Property::NoPHIs);
164 }
165
Derek Schuffad154c82016-03-28 17:05:30 +0000166 MachineFunctionProperties getSetProperties() const override {
167 return MachineFunctionProperties().set(
168 MachineFunctionProperties::Property::AllVRegsAllocated);
169 }
170
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000171 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000172 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000173 void AllocateBasicBlock();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000174 void handleThroughOperands(MachineInstr *MI,
175 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000176 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000177 bool isLastUseOfLocalReg(MachineOperand&);
178
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000179 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000180 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000181 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000182 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000183 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000184
185 void usePhysReg(MachineOperand&);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000186 void definePhysReg(MachineInstr &MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000187 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000188 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
189 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
190 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
191 }
192 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
193 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
194 }
195 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000196 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000197 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000198 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000199 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000200 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000201 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000202 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000203 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000204 };
205 char RAFast::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000206}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000207
208/// getStackSpaceFor - This allocates space for the specified virtual register
209/// to be held on the stack.
210int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
211 // Find the location Reg would belong...
212 int SS = StackSlotForVirtReg[VirtReg];
213 if (SS != -1)
214 return SS; // Already has space allocated?
215
216 // Allocate a new stack object for this spill location...
Matthias Braun941a7052016-07-28 18:40:00 +0000217 int FrameIdx = MF->getFrameInfo().CreateSpillStackObject(RC->getSize(),
218 RC->getAlignment());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000219
220 // Assign the slot.
221 StackSlotForVirtReg[VirtReg] = FrameIdx;
222 return FrameIdx;
223}
224
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000225/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
226/// its virtual register, and it is guaranteed to be a block-local register.
227///
228bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000229 // If the register has ever been spilled or reloaded, we conservatively assume
230 // it is a global register used in multiple blocks.
231 if (StackSlotForVirtReg[MO.getReg()] != -1)
232 return false;
233
234 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000235 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000236 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000237 return false;
238 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000239}
240
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000241/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000242void RAFast::addKillFlag(const LiveReg &LR) {
243 if (!LR.LastUse) return;
244 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000245 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
246 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000247 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000248 else
249 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
250 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000251}
252
253/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000254void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000255 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000256 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
257 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000258 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000259 // Erase from LiveVirtRegs unless we're spilling in bulk.
260 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000261 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000262}
263
264/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000265void RAFast::killVirtReg(unsigned VirtReg) {
266 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
267 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000268 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000269 if (LRI != LiveVirtRegs.end())
270 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000271}
272
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000273/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000274/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000275void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000276 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
277 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000278 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000279 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
280 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000281}
282
283/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000284void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000285 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000286 LiveReg &LR = *LRI;
287 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000288
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000289 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000290 // If this physreg is used by the instruction, we want to kill it on the
291 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000292 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000293 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000294 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000295 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000296 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
297 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000298 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000299 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000300 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000301
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000302 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000303 // identify spilled location as the place to find corresponding variable's
304 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000305 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000306 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000307 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
308 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000309 const MDNode *Var = DBG->getDebugVariable();
310 const MDNode *Expr = DBG->getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000311 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000312 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000313 DebugLoc DL = DBG->getDebugLoc();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000314 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000315 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000316 MachineInstr *NewDV =
317 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000318 .addFrameIndex(FI)
319 .addImm(Offset)
320 .addMetadata(Var)
321 .addMetadata(Expr);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000322 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000323 (void)NewDV;
324 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000325 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000326 // Now this register is spilled there is should not be any DBG_VALUE
327 // pointing to this register because they are all pointing to spilled value
328 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000329 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000330 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000331 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000332 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000333 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000334}
335
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000336/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000337void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000338 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000339 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000340 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
341 // of spilling here is deterministic, if arbitrary.
342 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
343 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000344 spillVirtReg(MI, i);
345 LiveVirtRegs.clear();
346 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000347}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000348
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000349/// usePhysReg - Handle the direct use of a physical register.
350/// Check that the register is not used by a virtreg.
351/// Kill the physreg, marking it free.
352/// This may add implicit kills to MO->getParent() and invalidate MO.
353void RAFast::usePhysReg(MachineOperand &MO) {
354 unsigned PhysReg = MO.getReg();
355 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
356 "Bad usePhysReg operand");
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000357
358 // Ignore undef uses.
359 if (MO.isUndef())
360 return;
361
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000362 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000363 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000364 case regDisabled:
365 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000366 case regReserved:
367 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000368 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000369 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000370 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000371 return;
372 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000373 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000374 // wanted has been clobbered.
375 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000376 }
377
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000378 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000379 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
380 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000381 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000382 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000383 break;
384 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000385 // Either PhysReg is a subregister of Alias and we mark the
386 // whole register as free, or PhysReg is the superregister of
387 // Alias and we mark all the aliases as disabled before freeing
388 // PhysReg.
389 // In the latter case, since PhysReg was disabled, this means that
390 // its value is defined only by physical sub-registers. This check
391 // is performed by the assert of the default case in this loop.
392 // Note: The value of the superregister may only be partial
393 // defined, that is why regDisabled is a valid state for aliases.
394 assert((TRI->isSuperRegister(PhysReg, Alias) ||
395 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000396 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000397 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000398 case regFree:
399 if (TRI->isSuperRegister(PhysReg, Alias)) {
400 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000401 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000402 MO.getParent()->addRegisterKilled(Alias, TRI, true);
403 return;
404 }
405 // Some other alias was in the working set - clear it.
406 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000407 break;
408 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000409 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000410 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000411 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000412
413 // All aliases are disabled, bring register into working set.
414 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000415 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000416}
417
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000418/// definePhysReg - Mark PhysReg as reserved or free after spilling any
419/// virtregs. This is very similar to defineVirtReg except the physreg is
420/// reserved instead of allocated.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000421void RAFast::definePhysReg(MachineInstr &MI, unsigned PhysReg,
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000422 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000423 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000424 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
425 case regDisabled:
426 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000427 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000428 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000429 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000430 case regFree:
431 case regReserved:
432 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000433 return;
434 }
435
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000436 // This is a disabled register, disable all aliases.
437 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000438 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
439 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000440 switch (unsigned VirtReg = PhysRegState[Alias]) {
441 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000442 break;
443 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000444 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000445 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000446 case regFree:
447 case regReserved:
448 PhysRegState[Alias] = regDisabled;
449 if (TRI->isSuperRegister(PhysReg, Alias))
450 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000451 break;
452 }
453 }
454}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000455
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000456
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000457// calcSpillCost - Return the cost of spilling clearing out PhysReg and
458// aliases so it is free for allocation.
459// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
460// can be allocated directly.
461// Returns spillImpossible when PhysReg or an alias can't be spilled.
462unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000463 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000464 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000465 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000466 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000467 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
468 case regDisabled:
469 break;
470 case regFree:
471 return 0;
472 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000473 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
474 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000475 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000476 default: {
477 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
478 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
479 return I->Dirty ? spillDirty : spillClean;
480 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000481 }
482
Eric Christopherc3783362011-04-12 00:48:08 +0000483 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000484 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000485 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000486 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
487 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000488 switch (unsigned VirtReg = PhysRegState[Alias]) {
489 case regDisabled:
490 break;
491 case regFree:
492 ++Cost;
493 break;
494 case regReserved:
495 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000496 default: {
497 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
498 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
499 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000500 break;
501 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000502 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000503 }
504 return Cost;
505}
506
507
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000508/// assignVirtToPhysReg - This method updates local state so that we know
509/// that PhysReg is the proper container for VirtReg now. The physical
510/// register must not be used for anything else when this is called.
511///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000512void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
513 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000514 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000515 PhysRegState[PhysReg] = LR.VirtReg;
516 assert(!LR.PhysReg && "Already assigned a physreg");
517 LR.PhysReg = PhysReg;
518}
519
520RAFast::LiveRegMap::iterator
521RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
522 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
523 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
524 assignVirtToPhysReg(*LRI, PhysReg);
525 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000526}
527
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000528/// allocVirtReg - Allocate a physical register for VirtReg.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000529RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr &MI,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000530 LiveRegMap::iterator LRI,
531 unsigned Hint) {
532 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000533
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000534 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
535 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000536
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000537 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000538
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000539 // Ignore invalid hints.
540 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000541 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000542 Hint = 0;
543
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000544 // Take hint when possible.
545 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000546 // Ignore the hint if we would have to spill a dirty register.
547 unsigned Cost = calcSpillCost(Hint);
548 if (Cost < spillDirty) {
549 if (Cost)
550 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000551 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
552 // That invalidates LRI, so run a new lookup for VirtReg.
553 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000554 }
555 }
556
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000557 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000558
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000559 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000560 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000561 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000562 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000563 assignVirtToPhysReg(*LRI, PhysReg);
564 return LRI;
565 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000566 }
567
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000568 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Craig Toppercf0444b2014-11-17 05:50:14 +0000569 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000570
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000571 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000572 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000573 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000574 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000575 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
576 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000577 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000578 if (Cost == 0) {
579 assignVirtToPhysReg(*LRI, *I);
580 return LRI;
581 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000582 if (Cost < BestCost)
583 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000584 }
585
586 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000587 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000588 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
589 // That invalidates LRI, so run a new lookup for VirtReg.
590 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000591 }
592
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000593 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000594 if (MI.isInlineAsm())
595 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000596 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000597 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000598 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000599 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000600}
601
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000602/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000603RAFast::LiveRegMap::iterator RAFast::defineVirtReg(MachineInstr &MI,
604 unsigned OpNum,
605 unsigned VirtReg,
606 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000607 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
608 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000609 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000610 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000611 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000612 if (New) {
613 // If there is no hint, peek at the only use of this register.
614 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
615 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000616 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000617 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000618 if (UseMI.isCopyLike())
619 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000620 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000621 LRI = allocVirtReg(MI, LRI, Hint);
622 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000623 // Redefining a live register - kill at the last use, unless it is this
624 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000625 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000626 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000627 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000628 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000629 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000630 LRI->LastOpNum = OpNum;
631 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000632 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000633 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000634}
635
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000636/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000637RAFast::LiveRegMap::iterator RAFast::reloadVirtReg(MachineInstr &MI,
638 unsigned OpNum,
639 unsigned VirtReg,
640 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000641 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
642 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000643 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000644 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000645 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000646 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000647 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000648 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000649 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000650 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000651 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000652 << PrintReg(LRI->PhysReg, TRI) << "\n");
653 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000654 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000655 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000656 if (isLastUseOfLocalReg(MO)) {
657 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000658 if (MO.isUse())
659 MO.setIsKill();
660 else
661 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000662 } else if (MO.isKill()) {
663 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
664 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000665 } else if (MO.isDead()) {
666 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
667 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000668 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000669 } else if (MO.isKill()) {
670 // We must remove kill flags from uses of reloaded registers because the
671 // register would be killed immediately, and there might be a second use:
672 // %foo = OR %x<kill>, %x
673 // This would cause a second reload of %x into a different register.
674 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
675 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000676 } else if (MO.isDead()) {
677 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
678 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000679 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000680 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000681 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000682 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000683 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000684 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000685}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000686
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000687// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
688// subregs. This may invalidate any operand pointers.
689// Return true if the operand kills its register.
690bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
691 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000692 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000693 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000694 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000695 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000696 }
697
698 // Handle subregister index.
699 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
700 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000701
702 // A kill flag implies killing the full register. Add corresponding super
703 // register kill.
704 if (MO.isKill()) {
705 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000706 return true;
707 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000708
709 // A <def,read-undef> of a sub-register requires an implicit def of the full
710 // register.
711 if (MO.isDef() && MO.isUndef())
712 MI->addRegisterDefined(PhysReg, TRI);
713
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000714 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000715}
716
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000717// Handle special instruction operand like early clobbers and tied ops when
718// there are additional physreg defines.
719void RAFast::handleThroughOperands(MachineInstr *MI,
720 SmallVectorImpl<unsigned> &VirtDead) {
721 DEBUG(dbgs() << "Scanning for through registers:");
722 SmallSet<unsigned, 8> ThroughRegs;
723 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
724 MachineOperand &MO = MI->getOperand(i);
725 if (!MO.isReg()) continue;
726 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000727 if (!TargetRegisterInfo::isVirtualRegister(Reg))
728 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000729 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
730 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000731 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000732 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000733 }
734 }
735
736 // If any physreg defines collide with preallocated through registers,
737 // we must spill and reallocate.
738 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
739 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
740 MachineOperand &MO = MI->getOperand(i);
741 if (!MO.isReg() || !MO.isDef()) continue;
742 unsigned Reg = MO.getReg();
743 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000744 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000745 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000746 if (ThroughRegs.count(PhysRegState[*AI]))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000747 definePhysReg(*MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000748 }
749 }
750
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000751 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000752 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000753 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
754 MachineOperand &MO = MI->getOperand(i);
755 if (!MO.isReg()) continue;
756 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000757 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000758 if (MO.isUse()) {
759 unsigned DefIdx = 0;
760 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
761 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
762 << DefIdx << ".\n");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000763 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000764 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000765 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000766 // Note: we don't update the def operand yet. That would cause the normal
767 // def-scan to attempt spilling.
768 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
769 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
770 // Reload the register, but don't assign to the operand just yet.
771 // That would confuse the later phys-def processing pass.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000772 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000773 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000774 }
775 }
776
Rafael Espindola2021f382011-11-22 06:27:18 +0000777 DEBUG(dbgs() << "Allocating early clobbers.\n");
778 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
779 MachineOperand &MO = MI->getOperand(i);
780 if (!MO.isReg()) continue;
781 unsigned Reg = MO.getReg();
782 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
783 if (!MO.isEarlyClobber())
784 continue;
785 // Note: defineVirtReg may invalidate MO.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000786 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000787 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000788 if (setPhysReg(MI, i, PhysReg))
789 VirtDead.push_back(Reg);
790 }
791
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000792 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000793 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000794 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
795 MachineOperand &MO = MI->getOperand(i);
796 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
797 unsigned Reg = MO.getReg();
798 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000799 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
800 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000801 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000802 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000803
804 // Also mark PartialDefs as used to avoid reallocation.
805 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000806 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000807}
808
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000809void RAFast::AllocateBasicBlock() {
810 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000811
812 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000813 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000814
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000815 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000816
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000817 // Add live-in registers as live.
Matthias Braund9da1622015-09-09 18:08:03 +0000818 for (const auto &LI : MBB->liveins())
819 if (MRI->isAllocatable(LI.PhysReg))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000820 definePhysReg(*MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000821
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000822 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000823 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000824
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000825 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000826 while (MII != MBB->end()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000827 MachineInstr *MI = &*MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000828 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000829 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000830 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000831 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
832 if (PhysRegState[Reg] == regDisabled) continue;
833 dbgs() << " " << TRI->getName(Reg);
834 switch(PhysRegState[Reg]) {
835 case regFree:
836 break;
837 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000838 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000839 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000840 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000841 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000842 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
843 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
844 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000845 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000846 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000847 break;
848 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000849 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000850 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000851 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000852 // Check that LiveVirtRegs is the inverse.
853 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
854 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000855 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000856 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000857 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000858 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000859 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000860 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000861 });
862
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000863 // Debug values are not allowed to change codegen in any way.
864 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000865 bool ScanDbgValue = true;
866 while (ScanDbgValue) {
867 ScanDbgValue = false;
868 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
869 MachineOperand &MO = MI->getOperand(i);
870 if (!MO.isReg()) continue;
871 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000872 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000873 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000874 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000875 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000876 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000877 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000878 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000879 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000880 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000881 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000882 }
Devang Pateld61b7352010-07-19 23:25:39 +0000883 else {
884 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000885 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000886 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000887 const MDNode *Var = MI->getDebugVariable();
888 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000889 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000890 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000891 assert(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000892 cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000893 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000894 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
895 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000896 .addFrameIndex(SS)
897 .addImm(Offset)
898 .addMetadata(Var)
899 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000900 DEBUG(dbgs() << "Modifying debug info due to spill:"
901 << "\t" << *NewDV);
902 // Scan NewDV operands from the beginning.
903 MI = NewDV;
904 ScanDbgValue = true;
905 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000906 }
Devang Patel57e72372010-07-09 21:48:31 +0000907 }
Devang Patel43bde962011-11-15 21:03:58 +0000908 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000909 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000910 }
911 // Next instruction.
912 continue;
913 }
914
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000915 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000916 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000917 if (MI->isCopy()) {
918 CopyDst = MI->getOperand(0).getReg();
919 CopySrc = MI->getOperand(1).getReg();
920 CopyDstSub = MI->getOperand(0).getSubReg();
921 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000922 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000923
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000924 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000925 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000926
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000927 // First scan.
928 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000929 // Find the end of the virtreg operands
930 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000931 bool hasTiedOps = false;
932 bool hasEarlyClobbers = false;
933 bool hasPartialRedefs = false;
934 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000935 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
936 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000937 // Make sure MRI knows about registers clobbered by regmasks.
938 if (MO.isRegMask()) {
939 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
940 continue;
941 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000942 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000943 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000944 if (!Reg) continue;
945 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
946 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000947 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000948 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000949 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000950 } else {
951 if (MO.isEarlyClobber())
952 hasEarlyClobbers = true;
953 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
954 hasPartialRedefs = true;
955 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000956 continue;
957 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000958 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000959 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000960 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000961 } else if (MO.isEarlyClobber()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000962 definePhysReg(*MI, Reg,
963 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000964 hasEarlyClobbers = true;
965 } else
966 hasPhysDefs = true;
967 }
968
969 // The instruction may have virtual register operands that must be allocated
970 // the same register at use-time and def-time: early clobbers and tied
971 // operands. If there are also physical defs, these registers must avoid
972 // both physical defs and uses, making them more constrained than normal
973 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000974 // Similarly, if there are multiple defs and tied operands, we must make
975 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000976 // We didn't detect inline asm tied operands above, so just make this extra
977 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000978 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000979 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000980 handleThroughOperands(MI, VirtDead);
981 // Don't attempt coalescing when we have funny stuff going on.
982 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000983 // Pretend we have early clobbers so the use operands get marked below.
984 // This is not necessary for the common case of a single tied use.
985 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000986 }
987
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000988 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000989 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000990 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000991 MachineOperand &MO = MI->getOperand(i);
992 if (!MO.isReg()) continue;
993 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000994 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000995 if (MO.isUse()) {
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000996 LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000997 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000998 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000999 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001000 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001001 }
1002 }
1003
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001004 // Track registers defined by instruction - early clobbers and tied uses at
1005 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001006 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001007 if (hasEarlyClobbers) {
1008 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1009 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001010 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001011 unsigned Reg = MO.getReg();
1012 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001013 // Look for physreg defs and tied uses.
1014 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001015 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001016 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001017 }
1018
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001019 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001020 if (MI->isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001021 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001022 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001023 // registers in their spill slots.
1024 // Note: although this is appealing to just consider all definitions
1025 // as call-clobbered, this is not correct because some of those
1026 // definitions may be used later on and we do not want to reuse
1027 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001028 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1029 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001030
1031 // The imp-defs are skipped below, but we still need to mark those
1032 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001033 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001034 }
1035
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001036 // Third scan.
1037 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001038 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001039 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001040 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1041 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001042 unsigned Reg = MO.getReg();
1043
1044 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001045 if (!MRI->isAllocatable(Reg)) continue;
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001046 definePhysReg(*MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001047 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001048 }
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +00001049 LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001050 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001051 if (setPhysReg(MI, i, PhysReg)) {
1052 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001053 CopyDst = 0; // cancel coalescing;
1054 } else
1055 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001056 }
1057
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001058 // Kill dead defs after the scan to ensure that multiple defs of the same
1059 // register are allocated identically. We didn't need to do this for uses
1060 // because we are crerating our own kill flags, and they are always at the
1061 // last use.
1062 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1063 killVirtReg(VirtDead[i]);
1064 VirtDead.clear();
1065
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001066 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1067 DEBUG(dbgs() << "-- coalescing: " << *MI);
1068 Coalesced.push_back(MI);
1069 } else {
1070 DEBUG(dbgs() << "<< " << *MI);
1071 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072 }
1073
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001074 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001075 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1076 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001077
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001078 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001079 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001080 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001081 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001082 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001083
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001084 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001085}
1086
1087/// runOnMachineFunction - Register allocate the whole function
1088///
1089bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001090 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001091 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001092 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001093 MRI = &MF->getRegInfo();
Eric Christopher60621802014-10-14 07:22:00 +00001094 TRI = MF->getSubtarget().getRegisterInfo();
1095 TII = MF->getSubtarget().getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001096 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001097 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001098 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001099 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001100
1101 // initialize the virtual->physical register map to have a 'null'
1102 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001103 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001104 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001105
1106 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001107 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1108 MBBi != MBBe; ++MBBi) {
1109 MBB = &*MBBi;
1110 AllocateBasicBlock();
1111 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001112
Andrew Trickda84e642012-02-21 04:51:23 +00001113 // All machine operands and other references to virtual registers have been
1114 // replaced. Remove the virtual registers.
1115 MRI->clearVirtRegs();
1116
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001117 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001118 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001119 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001120 return true;
1121}
1122
1123FunctionPass *llvm::createFastRegisterAllocator() {
1124 return new RAFast();
1125}