blob: 1cd5caba67b1028ee75cf445f7efc8ba7cf1e94e [file] [log] [blame]
Eugene Zelenko618c5552017-09-13 21:15:20 +00001//===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00002//
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//
Matthias Braun864cf582017-09-09 00:52:46 +000010/// \file This register allocator allocates registers to a basic block at a
11/// time, attempting to keep values in registers and reusing registers as
12/// appropriate.
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000013//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenko618c5552017-09-13 21:15:20 +000016#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/IndexedMap.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000021#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000022#include "llvm/ADT/Statistic.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000025#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000029#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/RegAllocRegistry.h"
32#include "llvm/CodeGen/RegisterClassInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000033#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000034#include "llvm/CodeGen/TargetOpcodes.h"
35#include "llvm/CodeGen/TargetRegisterInfo.h"
36#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000037#include "llvm/IR/DebugLoc.h"
38#include "llvm/IR/Metadata.h"
39#include "llvm/MC/MCInstrDesc.h"
40#include "llvm/MC/MCRegisterInfo.h"
41#include "llvm/Pass.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Support/Debug.h"
45#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000046#include "llvm/Support/raw_ostream.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000047#include <cassert>
48#include <tuple>
49#include <vector>
50
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000051using namespace llvm;
52
Chandler Carruth1b9dde02014-04-22 02:02:50 +000053#define DEBUG_TYPE "regalloc"
54
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000055STATISTIC(NumStores, "Number of stores added");
56STATISTIC(NumLoads , "Number of loads added");
Matthias Braun14af82a2018-11-07 02:04:07 +000057STATISTIC(NumCoalesced, "Number of copies coalesced");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000058
59static RegisterRegAlloc
60 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
61
62namespace {
Eugene Zelenko618c5552017-09-13 21:15:20 +000063
Matthias Braun864cf582017-09-09 00:52:46 +000064 class RegAllocFast : public MachineFunctionPass {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000065 public:
66 static char ID;
Eugene Zelenko618c5552017-09-13 21:15:20 +000067
Matthias Braun864cf582017-09-09 00:52:46 +000068 RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
Derek Schuffad154c82016-03-28 17:05:30 +000069
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000070 private:
Matthias Braun864cf582017-09-09 00:52:46 +000071 MachineFrameInfo *MFI;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +000072 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000073 const TargetRegisterInfo *TRI;
74 const TargetInstrInfo *TII;
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +000075 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000076
Matthias Braun864cf582017-09-09 00:52:46 +000077 /// Basic block currently being allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +000078 MachineBasicBlock *MBB;
79
Matthias Braun864cf582017-09-09 00:52:46 +000080 /// Maps virtual regs to the frame index where these values are spilled.
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000081 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
82
Matthias Braun864cf582017-09-09 00:52:46 +000083 /// Everything we know about a live virtual register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000084 struct LiveReg {
Eugene Zelenko618c5552017-09-13 21:15:20 +000085 MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
86 unsigned VirtReg; ///< Virtual register number.
87 MCPhysReg PhysReg = 0; ///< Currently held here.
88 unsigned short LastOpNum = 0; ///< OpNum on LastUse.
89 bool Dirty = false; ///< Register needs spill.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000090
Matthias Braunebcf5432018-11-07 02:04:11 +000091 explicit LiveReg(unsigned VirtReg) : VirtReg(VirtReg) {}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000092
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000093 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000094 return TargetRegisterInfo::virtReg2Index(VirtReg);
95 }
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000096 };
97
Eugene Zelenko618c5552017-09-13 21:15:20 +000098 using LiveRegMap = SparseSet<LiveReg>;
Matthias Braun864cf582017-09-09 00:52:46 +000099 /// This map contains entries for each virtual register that is currently
100 /// available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000101 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000102
Matthias Braunebcf5432018-11-07 02:04:11 +0000103 DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +0000104
Matthias Braunebcf5432018-11-07 02:04:11 +0000105 /// State of a physical register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000106 enum RegState {
Matthias Braun864cf582017-09-09 00:52:46 +0000107 /// A disabled register is not available for allocation, but an alias may
108 /// be in use. A register can only be moved out of the disabled state if
109 /// all aliases are disabled.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000110 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000111
Matthias Braun864cf582017-09-09 00:52:46 +0000112 /// A free register is not currently in use and can be allocated
113 /// immediately without checking aliases.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000114 regFree,
115
Matthias Braun864cf582017-09-09 00:52:46 +0000116 /// A reserved register has been assigned explicitly (e.g., setting up a
117 /// call parameter), and it remains reserved until it is used.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000118 regReserved
119
Matthias Braun864cf582017-09-09 00:52:46 +0000120 /// A register state may also be a virtual register number, indication
121 /// that the physical register is currently allocated to a virtual
122 /// register. In that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000123 };
124
Matthias Braunebcf5432018-11-07 02:04:11 +0000125 /// Maps each physical register to a RegState enum or a virtual register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000126 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000127
Matthias Brauna09d18d2017-09-09 00:52:45 +0000128 SmallVector<unsigned, 16> VirtDead;
Eugene Zelenko618c5552017-09-13 21:15:20 +0000129 SmallVector<MachineInstr *, 32> Coalesced;
Matthias Brauna09d18d2017-09-09 00:52:45 +0000130
Matthias Braunebcf5432018-11-07 02:04:11 +0000131 using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
Matthias Braun864cf582017-09-09 00:52:46 +0000132 /// Set of register units that are used in the current instruction, and so
133 /// cannot be allocated.
Matthias Braunebcf5432018-11-07 02:04:11 +0000134 RegUnitSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000135
Matthias Braun0804dca2018-11-07 06:57:00 +0000136 void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
137
Matthias Braun864cf582017-09-09 00:52:46 +0000138 /// Mark a physreg as used in this instruction.
139 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000140 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
141 UsedInInstr.insert(*Units);
142 }
143
Matthias Braun864cf582017-09-09 00:52:46 +0000144 /// Check if a physreg or any of its aliases are used in this instruction.
145 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000146 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
147 if (UsedInInstr.count(*Units))
148 return true;
149 return false;
150 }
151
Matthias Braun864cf582017-09-09 00:52:46 +0000152 /// This flag is set when LiveRegMap will be cleared completely after
153 /// spilling all live registers. LiveRegMap entries should not be erased.
154 bool isBulkSpilling = false;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000155
Alp Toker61007d82014-03-02 03:20:38 +0000156 enum : unsigned {
Matthias Braunebcf5432018-11-07 02:04:11 +0000157 spillClean = 50,
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000158 spillDirty = 100,
159 spillImpossible = ~0u
160 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000161
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000162 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000163 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000164
Craig Topper4584cd52014-03-07 09:26:03 +0000165 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000166 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000167 MachineFunctionPass::getAnalysisUsage(AU);
168 }
169
Matthias Braun90799ce2016-08-23 21:19:49 +0000170 MachineFunctionProperties getRequiredProperties() const override {
171 return MachineFunctionProperties().set(
172 MachineFunctionProperties::Property::NoPHIs);
173 }
174
Derek Schuffad154c82016-03-28 17:05:30 +0000175 MachineFunctionProperties getSetProperties() const override {
176 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000177 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000178 }
179
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000180 private:
Fangrui Songcb0bab82018-07-16 18:51:40 +0000181 bool runOnMachineFunction(MachineFunction &MF) override;
Matthias Braunebcf5432018-11-07 02:04:11 +0000182
Matthias Braun864cf582017-09-09 00:52:46 +0000183 void allocateBasicBlock(MachineBasicBlock &MBB);
184 void handleThroughOperands(MachineInstr &MI,
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000185 SmallVectorImpl<unsigned> &VirtDead);
Matthias Braun864cf582017-09-09 00:52:46 +0000186 bool isLastUseOfLocalReg(const MachineOperand &MO) const;
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000187
Matthias Braun864cf582017-09-09 00:52:46 +0000188 void addKillFlag(const LiveReg &LRI);
189 void killVirtReg(LiveRegMap::iterator LRI);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000190 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000191 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000192 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000193
Matthias Braun864cf582017-09-09 00:52:46 +0000194 void usePhysReg(MachineOperand &MO);
Quentin Colombet72f6d592018-01-29 23:42:37 +0000195 void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
196 RegState NewState);
Matthias Braun864cf582017-09-09 00:52:46 +0000197 unsigned calcSpillCost(MCPhysReg PhysReg) const;
Quentin Colombet72f6d592018-01-29 23:42:37 +0000198 void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000199
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000200 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
201 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
202 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000203
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000204 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
205 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
206 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000207
Fangrui Songcb0bab82018-07-16 18:51:40 +0000208 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000209 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000210 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000211 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000212 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000213 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000214 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000215 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000216 bool setPhysReg(MachineInstr &MI, unsigned OpNum, MCPhysReg PhysReg);
217
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000218 int getStackSpaceFor(unsigned VirtReg);
219 void spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
220 MCPhysReg AssignedReg, bool Kill);
221 void reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
222 MCPhysReg PhysReg);
223
Matthias Braun864cf582017-09-09 00:52:46 +0000224 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000225 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000226
227} // end anonymous namespace
228
229char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000230
Matthias Braun864cf582017-09-09 00:52:46 +0000231INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
232 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000233
Matthias Braun0804dca2018-11-07 06:57:00 +0000234void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
235 PhysRegState[PhysReg] = NewState;
236}
237
Matthias Braun864cf582017-09-09 00:52:46 +0000238/// This allocates space for the specified virtual register to be held on the
239/// stack.
Matthias Braunebcf5432018-11-07 02:04:11 +0000240int RegAllocFast::getStackSpaceFor(unsigned VirtReg) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000241 // Find the location Reg would belong...
242 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000243 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000244 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000245 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000246
247 // Allocate a new stack object for this spill location...
Matthias Braunebcf5432018-11-07 02:04:11 +0000248 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000249 unsigned Size = TRI->getSpillSize(RC);
250 unsigned Align = TRI->getSpillAlignment(RC);
251 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000252
253 // Assign the slot.
254 StackSlotForVirtReg[VirtReg] = FrameIdx;
255 return FrameIdx;
256}
257
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000258/// Insert spill instruction for \p AssignedReg before \p Before. Update
259/// DBG_VALUEs with \p VirtReg operands with the stack slot.
260void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
261 MCPhysReg AssignedReg, bool Kill) {
262 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
263 << " in " << printReg(AssignedReg, TRI));
264 int FI = getStackSpaceFor(VirtReg);
265 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << "\n");
266
267 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
268 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
269 ++NumStores;
270
271 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
272 // identify spilled location as the place to find corresponding variable's
273 // value.
274 SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
275 for (MachineInstr *DBG : LRIDbgValues) {
276 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
277 assert(NewDV->getParent() == MBB && "dangling parent pointer");
278 (void)NewDV;
279 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
280 }
281 // Now this register is spilled there is should not be any DBG_VALUE
282 // pointing to this register because they are all pointing to spilled value
283 // now.
284 LRIDbgValues.clear();
285}
286
287/// Insert reload instruction for \p PhysReg before \p Before.
288void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
289 MCPhysReg PhysReg) {
290 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
291 << printReg(PhysReg, TRI) << "\n");
292 int FI = getStackSpaceFor(VirtReg);
293 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
294 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
295 ++NumLoads;
296}
297
Matthias Braun864cf582017-09-09 00:52:46 +0000298/// Return true if MO is the only remaining reference to its virtual register,
299/// and it is guaranteed to be a block-local register.
300bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000301 // If the register has ever been spilled or reloaded, we conservatively assume
302 // it is a global register used in multiple blocks.
303 if (StackSlotForVirtReg[MO.getReg()] != -1)
304 return false;
305
306 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000307 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000308 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000309 return false;
310 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000311}
312
Matthias Braun864cf582017-09-09 00:52:46 +0000313/// Set kill flags on last use of a virtual register.
314void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000315 if (!LR.LastUse) return;
316 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000317 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
318 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000319 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000320 // else, don't do anything we are problably redefining a
321 // subreg of this register and given we don't track which
322 // lanes are actually dead, we cannot insert a kill flag here.
323 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000324 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000325 // ... <== Here we would allow later pass to reuse physreg:sub1
326 // which is potentially wrong.
327 // LR:sub0 = ...
328 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000329 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000330}
331
Matthias Braun864cf582017-09-09 00:52:46 +0000332/// Mark virtreg as no longer available.
333void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000334 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000335 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
336 "Broken RegState mapping");
Matthias Braun0804dca2018-11-07 06:57:00 +0000337 setPhysRegState(LRI->PhysReg, regFree);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000338 // Erase from LiveVirtRegs unless we're spilling in bulk.
339 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000340 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000341}
342
Matthias Braun864cf582017-09-09 00:52:46 +0000343/// Mark virtreg as no longer available.
344void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000345 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
346 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000347 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000348 if (LRI != LiveVirtRegs.end())
349 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000350}
351
Matthias Braun864cf582017-09-09 00:52:46 +0000352/// This method spills the value specified by VirtReg into the corresponding
353/// stack slot if needed.
354void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
355 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000356 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
357 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000358 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000359 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
360 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000361}
362
Matthias Braun864cf582017-09-09 00:52:46 +0000363/// Do the actual work of spilling.
364void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
365 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000366 LiveReg &LR = *LRI;
367 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000368
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000369 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000370 // If this physreg is used by the instruction, we want to kill it on the
371 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000372 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000373 LR.Dirty = false;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000374
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000375 spill(MI, LRI->VirtReg, LR.PhysReg, SpillKill);
376
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000377 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000378 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000379 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000380 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000381}
382
Matthias Braun864cf582017-09-09 00:52:46 +0000383/// Spill all dirty virtregs without killing them.
384void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000385 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000386 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000387 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
388 // of spilling here is deterministic, if arbitrary.
Matthias Braun864cf582017-09-09 00:52:46 +0000389 for (LiveRegMap::iterator I = LiveVirtRegs.begin(), E = LiveVirtRegs.end();
390 I != E; ++I)
391 spillVirtReg(MI, I);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000392 LiveVirtRegs.clear();
393 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000394}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000395
Matthias Braun864cf582017-09-09 00:52:46 +0000396/// Handle the direct use of a physical register. Check that the register is
397/// not used by a virtreg. Kill the physreg, marking it free. This may add
398/// implicit kills to MO->getParent() and invalidate MO.
399void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000400 // Ignore undef uses.
401 if (MO.isUndef())
402 return;
403
Matthias Braun864cf582017-09-09 00:52:46 +0000404 unsigned PhysReg = MO.getReg();
405 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
406 "Bad usePhysReg operand");
407
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000408 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000409 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000410 case regDisabled:
411 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000412 case regReserved:
413 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000414 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000415 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000416 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000417 return;
418 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000419 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000420 // wanted has been clobbered.
421 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000422 }
423
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000424 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000425 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000426 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000427 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000428 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000429 break;
430 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000431 // Either PhysReg is a subregister of Alias and we mark the
432 // whole register as free, or PhysReg is the superregister of
433 // Alias and we mark all the aliases as disabled before freeing
434 // PhysReg.
435 // In the latter case, since PhysReg was disabled, this means that
436 // its value is defined only by physical sub-registers. This check
437 // is performed by the assert of the default case in this loop.
438 // Note: The value of the superregister may only be partial
439 // defined, that is why regDisabled is a valid state for aliases.
440 assert((TRI->isSuperRegister(PhysReg, Alias) ||
441 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000442 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000443 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000444 case regFree:
445 if (TRI->isSuperRegister(PhysReg, Alias)) {
446 // Leave the superregister in the working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000447 setPhysRegState(Alias, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000448 MO.getParent()->addRegisterKilled(Alias, TRI, true);
449 return;
450 }
451 // Some other alias was in the working set - clear it.
Matthias Braun0804dca2018-11-07 06:57:00 +0000452 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000453 break;
454 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000455 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000456 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000457 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000458
459 // All aliases are disabled, bring register into working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000460 setPhysRegState(PhysReg, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000461 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000462}
463
Matthias Braun864cf582017-09-09 00:52:46 +0000464/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
465/// similar to defineVirtReg except the physreg is reserved instead of
466/// allocated.
Quentin Colombet72f6d592018-01-29 23:42:37 +0000467void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
468 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000469 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000470 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
471 case regDisabled:
472 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000473 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000474 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000475 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000476 case regFree:
477 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000478 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000479 return;
480 }
481
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000482 // This is a disabled register, disable all aliases.
Matthias Braun0804dca2018-11-07 06:57:00 +0000483 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000484 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000485 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000486 switch (unsigned VirtReg = PhysRegState[Alias]) {
487 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000488 break;
489 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000490 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000491 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000492 case regFree:
493 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000494 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000495 if (TRI->isSuperRegister(PhysReg, Alias))
496 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000497 break;
498 }
499 }
500}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000501
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000502/// Return the cost of spilling clearing out PhysReg and aliases so it is
Matthias Braun864cf582017-09-09 00:52:46 +0000503/// free for allocation. Returns 0 when PhysReg is free or disabled with all
504/// aliases disabled - it can be allocated directly.
505/// \returns spillImpossible when PhysReg or an alias can't be spilled.
506unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000507 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000508 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
509 << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000510 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000511 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000512 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
513 case regDisabled:
514 break;
515 case regFree:
516 return 0;
517 case regReserved:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000518 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
519 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000520 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000521 default: {
522 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
523 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
524 return I->Dirty ? spillDirty : spillClean;
525 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000526 }
527
Eric Christopherc3783362011-04-12 00:48:08 +0000528 // This is a disabled register, add up cost of aliases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000529 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000530 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000531 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000532 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000533 switch (unsigned VirtReg = PhysRegState[Alias]) {
534 case regDisabled:
535 break;
536 case regFree:
537 ++Cost;
538 break;
539 case regReserved:
540 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000541 default: {
542 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
543 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
544 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000545 break;
546 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000547 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000548 }
549 return Cost;
550}
551
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000552/// This method updates local state so that we know that PhysReg is the
Matthias Braun864cf582017-09-09 00:52:46 +0000553/// proper container for VirtReg now. The physical register must not be used
554/// for anything else when this is called.
555void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Matthias Braun0804dca2018-11-07 06:57:00 +0000556 unsigned VirtReg = LR.VirtReg;
557 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000558 << printReg(PhysReg, TRI) << "\n");
Matthias Braun0804dca2018-11-07 06:57:00 +0000559 assert(LR.PhysReg == 0 && "Already assigned a physreg");
560 assert(PhysReg != 0 && "Trying to assign no register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000561 LR.PhysReg = PhysReg;
Matthias Braun0804dca2018-11-07 06:57:00 +0000562 setPhysRegState(PhysReg, VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000563}
564
Matthias Braun864cf582017-09-09 00:52:46 +0000565RegAllocFast::LiveRegMap::iterator
566RegAllocFast::assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000567 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
568 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
569 assignVirtToPhysReg(*LRI, PhysReg);
570 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000571}
572
Matthias Braun864cf582017-09-09 00:52:46 +0000573/// Allocates a physical register for VirtReg.
574RegAllocFast::LiveRegMap::iterator RegAllocFast::allocVirtReg(MachineInstr &MI,
575 LiveRegMap::iterator LRI, unsigned Hint) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000576 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000577
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000578 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
579 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000580
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000581 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000582 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
583 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
584 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000585 // Ignore the hint if we would have to spill a dirty register.
586 unsigned Cost = calcSpillCost(Hint);
587 if (Cost < spillDirty) {
588 if (Cost)
589 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000590 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
591 // That invalidates LRI, so run a new lookup for VirtReg.
592 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000593 }
594 }
595
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000596 // First try to find a completely free register.
Matthias Braun864cf582017-09-09 00:52:46 +0000597 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(&RC);
598 for (MCPhysReg PhysReg : AO) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000599 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000600 assignVirtToPhysReg(*LRI, PhysReg);
601 return LRI;
602 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000603 }
604
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000605 LLVM_DEBUG(dbgs() << "Allocating " << printReg(VirtReg) << " from "
606 << TRI->getRegClassName(&RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000607
Matthias Braun864cf582017-09-09 00:52:46 +0000608 unsigned BestReg = 0;
609 unsigned BestCost = spillImpossible;
610 for (MCPhysReg PhysReg : AO) {
611 unsigned Cost = calcSpillCost(PhysReg);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000612 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << "\n");
613 LLVM_DEBUG(dbgs() << "\tCost: " << Cost << "\n");
614 LLVM_DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000615 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000616 if (Cost == 0) {
Matthias Braun864cf582017-09-09 00:52:46 +0000617 assignVirtToPhysReg(*LRI, PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000618 return LRI;
619 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000620 if (Cost < BestCost)
Matthias Braun864cf582017-09-09 00:52:46 +0000621 BestReg = PhysReg, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000622 }
623
624 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000625 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000626 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
627 // That invalidates LRI, so run a new lookup for VirtReg.
628 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000629 }
630
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000631 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000632 if (MI.isInlineAsm())
633 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000634 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000635 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000636 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000637 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000638}
639
Matthias Braun864cf582017-09-09 00:52:46 +0000640/// Allocates a register for VirtReg and mark it as dirty.
641RegAllocFast::LiveRegMap::iterator RegAllocFast::defineVirtReg(MachineInstr &MI,
642 unsigned OpNum,
643 unsigned VirtReg,
644 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000645 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
646 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000647 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000648 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000649 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000650 if (New) {
651 // If there is no hint, peek at the only use of this register.
652 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
653 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000654 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000655 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000656 if (UseMI.isCopyLike())
657 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000658 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000659 LRI = allocVirtReg(MI, LRI, Hint);
660 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000661 // Redefining a live register - kill at the last use, unless it is this
662 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000663 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000664 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000665 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000666 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000667 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000668 LRI->LastOpNum = OpNum;
669 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000670 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000671 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000672}
673
Matthias Braun864cf582017-09-09 00:52:46 +0000674/// Make sure VirtReg is available in a physreg and return it.
675RegAllocFast::LiveRegMap::iterator RegAllocFast::reloadVirtReg(MachineInstr &MI,
676 unsigned OpNum,
677 unsigned VirtReg,
678 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000679 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
680 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000681 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000682 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000683 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000684 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000685 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000686 LRI = allocVirtReg(MI, LRI, Hint);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000687 reload(MI, VirtReg, LRI->PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000688 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000689 if (isLastUseOfLocalReg(MO)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000690 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000691 if (MO.isUse())
692 MO.setIsKill();
693 else
694 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000695 } else if (MO.isKill()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000696 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000697 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000698 } else if (MO.isDead()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000699 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000700 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000701 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000702 } else if (MO.isKill()) {
703 // We must remove kill flags from uses of reloaded registers because the
704 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000705 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000706 // This would cause a second reload of %x into a different register.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000707 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000708 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000709 } else if (MO.isDead()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000710 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000711 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000712 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000713 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000714 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000715 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000716 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000717 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000718}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000719
Matthias Braun864cf582017-09-09 00:52:46 +0000720/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
721/// may invalidate any operand pointers. Return true if the operand kills its
722/// register.
723bool RegAllocFast::setPhysReg(MachineInstr &MI, unsigned OpNum,
724 MCPhysReg PhysReg) {
725 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000726 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000727 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000728 MO.setReg(PhysReg);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000729 MO.setIsRenamable(true);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000730 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000731 }
732
733 // Handle subregister index.
734 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000735 MO.setIsRenamable(true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000736 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000737
738 // A kill flag implies killing the full register. Add corresponding super
739 // register kill.
740 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000741 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000742 return true;
743 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000744
745 // A <def,read-undef> of a sub-register requires an implicit def of the full
746 // register.
747 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000748 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000749
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000750 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000751}
752
Matthias Braun864cf582017-09-09 00:52:46 +0000753// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000754// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000755void RegAllocFast::handleThroughOperands(MachineInstr &MI,
756 SmallVectorImpl<unsigned> &VirtDead) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000757 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000758 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000759 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000760 if (!MO.isReg()) continue;
761 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000762 if (!TargetRegisterInfo::isVirtualRegister(Reg))
763 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000764 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
765 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000766 if (ThroughRegs.insert(Reg).second)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000767 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000768 }
769 }
770
771 // If any physreg defines collide with preallocated through registers,
772 // we must spill and reallocate.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000773 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000774 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000775 if (!MO.isReg() || !MO.isDef()) continue;
776 unsigned Reg = MO.getReg();
777 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000778 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000779 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000780 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000781 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000782 }
783 }
784
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000785 SmallVector<unsigned, 8> PartialDefs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000786 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000787 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
788 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000789 if (!MO.isReg()) continue;
790 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000791 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000792 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000793 if (!MO.isTied()) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000794 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
795 << ") is tied to operand " << MI.findTiedOperandIdx(I)
796 << ".\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000797 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
798 MCPhysReg PhysReg = LRI->PhysReg;
799 setPhysReg(MI, I, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000800 // Note: we don't update the def operand yet. That would cause the normal
801 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000802 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000803 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000804 // Reload the register, but don't assign to the operand just yet.
805 // That would confuse the later phys-def processing pass.
Matthias Braun864cf582017-09-09 00:52:46 +0000806 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000807 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000808 }
809 }
810
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000811 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000812 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
813 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000814 if (!MO.isReg()) continue;
815 unsigned Reg = MO.getReg();
816 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
817 if (!MO.isEarlyClobber())
818 continue;
819 // Note: defineVirtReg may invalidate MO.
Matthias Braun864cf582017-09-09 00:52:46 +0000820 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, 0);
821 MCPhysReg PhysReg = LRI->PhysReg;
822 if (setPhysReg(MI, I, PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000823 VirtDead.push_back(Reg);
824 }
825
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000826 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000827 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000828 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000829 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
830 unsigned Reg = MO.getReg();
831 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000832 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
833 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000834 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000835 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000836
837 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000838 for (unsigned PartialDef : PartialDefs)
839 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000840}
841
Matthias Braun864cf582017-09-09 00:52:46 +0000842#ifndef NDEBUG
843void RegAllocFast::dumpState() {
844 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
845 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000846 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000847 switch(PhysRegState[Reg]) {
848 case regFree:
849 break;
850 case regReserved:
851 dbgs() << "*";
852 break;
853 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000854 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun864cf582017-09-09 00:52:46 +0000855 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
856 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
857 if (I->Dirty)
858 dbgs() << "*";
859 assert(I->PhysReg == Reg && "Bad inverse map");
860 break;
861 }
862 }
863 }
864 dbgs() << '\n';
865 // Check that LiveVirtRegs is the inverse.
866 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
867 e = LiveVirtRegs.end(); i != e; ++i) {
868 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
869 "Bad map key");
870 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
871 "Bad map value");
872 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
873 }
874}
875#endif
876
877void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
878 this->MBB = &MBB;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000879 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000880
881 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000882 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000883
Matthias Braun864cf582017-09-09 00:52:46 +0000884 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000885
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000886 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +0000887 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +0000888 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet72f6d592018-01-29 23:42:37 +0000889 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000890
Matthias Brauna09d18d2017-09-09 00:52:45 +0000891 VirtDead.clear();
892 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000893
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000894 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +0000895 for (MachineInstr &MI : MBB) {
896 const MCInstrDesc &MCID = MI.getDesc();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000897 LLVM_DEBUG(dbgs() << "\n>> " << MI << "Regs:"; dumpState());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000898
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000899 // Debug values are not allowed to change codegen in any way.
Matthias Braun864cf582017-09-09 00:52:46 +0000900 if (MI.isDebugValue()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000901 MachineInstr *DebugMI = &MI;
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000902 MachineOperand &MO = DebugMI->getOperand(0);
903
904 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
905 // mostly constants and frame indices.
906 if (!MO.isReg())
907 continue;
908 unsigned Reg = MO.getReg();
909 if (!TargetRegisterInfo::isVirtualRegister(Reg))
910 continue;
911
912 // See if this virtual register has already been allocated to a physical
913 // register or spilled to a stack slot.
914 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
915 if (LRI != LiveVirtRegs.end())
916 setPhysReg(*DebugMI, 0, LRI->PhysReg);
917 else {
918 int SS = StackSlotForVirtReg[Reg];
919 if (SS != -1) {
920 // Modify DBG_VALUE now that the value is in a spill slot.
921 updateDbgValueForSpill(*DebugMI, SS);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000922 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:"
923 << "\t" << *DebugMI);
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000924 continue;
Devang Patel57e72372010-07-09 21:48:31 +0000925 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000926
927 // We can't allocate a physreg for a DebugValue, sorry!
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000928 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000929 MO.setReg(0);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000930 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000931
932 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
933 // that future spills of Reg will have DBG_VALUEs.
934 LiveDbgValueMap[Reg].push_back(DebugMI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000935 continue;
936 }
937
Shiva Chen801bf7e2018-05-09 02:42:00 +0000938 if (MI.isDebugLabel())
939 continue;
940
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000941 // If this is a copy, we may be able to coalesce.
Matthias Braun864cf582017-09-09 00:52:46 +0000942 unsigned CopySrcReg = 0;
943 unsigned CopyDstReg = 0;
944 unsigned CopySrcSub = 0;
945 unsigned CopyDstSub = 0;
946 if (MI.isCopy()) {
947 CopyDstReg = MI.getOperand(0).getReg();
948 CopySrcReg = MI.getOperand(1).getReg();
949 CopyDstSub = MI.getOperand(0).getSubReg();
950 CopySrcSub = MI.getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000951 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000952
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000953 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000954 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000955
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000956 // First scan.
957 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000958 // Find the end of the virtreg operands
959 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000960 bool hasTiedOps = false;
961 bool hasEarlyClobbers = false;
962 bool hasPartialRedefs = false;
963 bool hasPhysDefs = false;
Matthias Braun864cf582017-09-09 00:52:46 +0000964 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
965 MachineOperand &MO = MI.getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000966 // Make sure MRI knows about registers clobbered by regmasks.
967 if (MO.isRegMask()) {
968 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
969 continue;
970 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000971 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000972 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000973 if (!Reg) continue;
974 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
975 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000976 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000977 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000978 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000979 } else {
980 if (MO.isEarlyClobber())
981 hasEarlyClobbers = true;
Matthias Braun864cf582017-09-09 00:52:46 +0000982 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000983 hasPartialRedefs = true;
984 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000985 continue;
986 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000987 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000988 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000989 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000990 } else if (MO.isEarlyClobber()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000991 definePhysReg(MI, Reg,
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000992 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000993 hasEarlyClobbers = true;
994 } else
995 hasPhysDefs = true;
996 }
997
998 // The instruction may have virtual register operands that must be allocated
999 // the same register at use-time and def-time: early clobbers and tied
1000 // operands. If there are also physical defs, these registers must avoid
1001 // both physical defs and uses, making them more constrained than normal
1002 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001003 // Similarly, if there are multiple defs and tied operands, we must make
1004 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001005 // We didn't detect inline asm tied operands above, so just make this extra
1006 // pass for all inline asm.
Matthias Braun864cf582017-09-09 00:52:46 +00001007 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +00001008 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001009 handleThroughOperands(MI, VirtDead);
1010 // Don't attempt coalescing when we have funny stuff going on.
Matthias Braun864cf582017-09-09 00:52:46 +00001011 CopyDstReg = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001012 // Pretend we have early clobbers so the use operands get marked below.
1013 // This is not necessary for the common case of a single tied use.
1014 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001015 }
1016
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001017 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001018 // Allocate virtreg uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001019 for (unsigned I = 0; I != VirtOpEnd; ++I) {
1020 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001021 if (!MO.isReg()) continue;
1022 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001023 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001024 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +00001025 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, CopyDstReg);
1026 MCPhysReg PhysReg = LRI->PhysReg;
1027 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
1028 if (setPhysReg(MI, I, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001029 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001030 }
1031 }
1032
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001033 // Track registers defined by instruction - early clobbers and tied uses at
1034 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001035 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001036 if (hasEarlyClobbers) {
Matthias Braun864cf582017-09-09 00:52:46 +00001037 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001038 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001039 unsigned Reg = MO.getReg();
1040 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001041 // Look for physreg defs and tied uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001042 if (!MO.isDef() && !MO.isTied()) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001043 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001044 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001045 }
1046
Matthias Braun864cf582017-09-09 00:52:46 +00001047 unsigned DefOpEnd = MI.getNumOperands();
1048 if (MI.isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001049 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001050 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001051 // registers in their spill slots.
1052 // Note: although this is appealing to just consider all definitions
1053 // as call-clobbered, this is not correct because some of those
1054 // definitions may be used later on and we do not want to reuse
1055 // those for virtual registers in between.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001056 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001057 spillAll(MI);
1058 }
1059
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001060 // Third scan.
1061 // Allocate defs and collect dead defs.
Matthias Braun864cf582017-09-09 00:52:46 +00001062 for (unsigned I = 0; I != DefOpEnd; ++I) {
1063 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001064 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1065 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001066 unsigned Reg = MO.getReg();
1067
1068 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001069 if (!MRI->isAllocatable(Reg)) continue;
Matthias Braun864cf582017-09-09 00:52:46 +00001070 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001071 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072 }
Matthias Braun864cf582017-09-09 00:52:46 +00001073 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, CopySrcReg);
1074 MCPhysReg PhysReg = LRI->PhysReg;
1075 if (setPhysReg(MI, I, PhysReg)) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001076 VirtDead.push_back(Reg);
Matthias Braun864cf582017-09-09 00:52:46 +00001077 CopyDstReg = 0; // cancel coalescing;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001078 } else
Matthias Braun864cf582017-09-09 00:52:46 +00001079 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001080 }
1081
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001082 // Kill dead defs after the scan to ensure that multiple defs of the same
1083 // register are allocated identically. We didn't need to do this for uses
1084 // because we are crerating our own kill flags, and they are always at the
1085 // last use.
Matthias Braun864cf582017-09-09 00:52:46 +00001086 for (unsigned VirtReg : VirtDead)
1087 killVirtReg(VirtReg);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001088 VirtDead.clear();
1089
Matthias Braun864cf582017-09-09 00:52:46 +00001090 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001091 LLVM_DEBUG(dbgs() << "-- coalescing: " << MI);
Matthias Braun864cf582017-09-09 00:52:46 +00001092 Coalesced.push_back(&MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001093 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001094 LLVM_DEBUG(dbgs() << "<< " << MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001095 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001096 }
1097
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001098 // Spill all physical registers holding virtual registers now.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001099 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001100 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001101
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001102 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001103 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001104 for (MachineInstr *MI : Coalesced)
1105 MBB.erase(MI);
Matthias Braun14af82a2018-11-07 02:04:07 +00001106 NumCoalesced += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001107
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001108 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001109}
1110
Matthias Braun864cf582017-09-09 00:52:46 +00001111bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001112 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1113 << "********** Function: " << MF.getName() << '\n');
Matthias Braun864cf582017-09-09 00:52:46 +00001114 MRI = &MF.getRegInfo();
1115 const TargetSubtargetInfo &STI = MF.getSubtarget();
1116 TRI = STI.getRegisterInfo();
1117 TII = STI.getInstrInfo();
1118 MFI = &MF.getFrameInfo();
1119 MRI->freezeReservedRegs(MF);
1120 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001121 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001122 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001123
1124 // initialize the virtual->physical register map to have a 'null'
1125 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001126 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1127 StackSlotForVirtReg.resize(NumVirtRegs);
1128 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001129
1130 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001131 for (MachineBasicBlock &MBB : MF)
1132 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001133
Andrew Trickda84e642012-02-21 04:51:23 +00001134 // All machine operands and other references to virtual registers have been
1135 // replaced. Remove the virtual registers.
1136 MRI->clearVirtRegs();
1137
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001138 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001139 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001140 return true;
1141}
1142
1143FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001144 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001145}