blob: 242e952bb2488517f985ab20277fdcba9b322825 [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 Braun864cf582017-09-09 00:52:46 +0000136 /// Mark a physreg as used in this instruction.
137 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000138 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
139 UsedInInstr.insert(*Units);
140 }
141
Matthias Braun864cf582017-09-09 00:52:46 +0000142 /// Check if a physreg or any of its aliases are used in this instruction.
143 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000144 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
145 if (UsedInInstr.count(*Units))
146 return true;
147 return false;
148 }
149
Matthias Braun864cf582017-09-09 00:52:46 +0000150 /// This flag is set when LiveRegMap will be cleared completely after
151 /// spilling all live registers. LiveRegMap entries should not be erased.
152 bool isBulkSpilling = false;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000153
Alp Toker61007d82014-03-02 03:20:38 +0000154 enum : unsigned {
Matthias Braunebcf5432018-11-07 02:04:11 +0000155 spillClean = 50,
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000156 spillDirty = 100,
157 spillImpossible = ~0u
158 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000159
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000160 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000161 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000162
Craig Topper4584cd52014-03-07 09:26:03 +0000163 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000164 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000165 MachineFunctionPass::getAnalysisUsage(AU);
166 }
167
Matthias Braun90799ce2016-08-23 21:19:49 +0000168 MachineFunctionProperties getRequiredProperties() const override {
169 return MachineFunctionProperties().set(
170 MachineFunctionProperties::Property::NoPHIs);
171 }
172
Derek Schuffad154c82016-03-28 17:05:30 +0000173 MachineFunctionProperties getSetProperties() const override {
174 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000175 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000176 }
177
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000178 private:
Fangrui Songcb0bab82018-07-16 18:51:40 +0000179 bool runOnMachineFunction(MachineFunction &MF) override;
Matthias Braunebcf5432018-11-07 02:04:11 +0000180
Matthias Braun864cf582017-09-09 00:52:46 +0000181 void allocateBasicBlock(MachineBasicBlock &MBB);
182 void handleThroughOperands(MachineInstr &MI,
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000183 SmallVectorImpl<unsigned> &VirtDead);
Matthias Braun864cf582017-09-09 00:52:46 +0000184 bool isLastUseOfLocalReg(const MachineOperand &MO) const;
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000185
Matthias Braun864cf582017-09-09 00:52:46 +0000186 void addKillFlag(const LiveReg &LRI);
187 void killVirtReg(LiveRegMap::iterator LRI);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000188 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000189 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000190 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000191
Matthias Braun864cf582017-09-09 00:52:46 +0000192 void usePhysReg(MachineOperand &MO);
Quentin Colombet72f6d592018-01-29 23:42:37 +0000193 void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
194 RegState NewState);
Matthias Braun864cf582017-09-09 00:52:46 +0000195 unsigned calcSpillCost(MCPhysReg PhysReg) const;
Quentin Colombet72f6d592018-01-29 23:42:37 +0000196 void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000197
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000198 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
199 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
200 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000201
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000202 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
203 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
204 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000205
Fangrui Songcb0bab82018-07-16 18:51:40 +0000206 LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000207 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000208 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000209 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000210 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000211 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000212 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000213 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000214 bool setPhysReg(MachineInstr &MI, unsigned OpNum, MCPhysReg PhysReg);
215
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000216 int getStackSpaceFor(unsigned VirtReg);
217 void spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
218 MCPhysReg AssignedReg, bool Kill);
219 void reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
220 MCPhysReg PhysReg);
221
Matthias Braun864cf582017-09-09 00:52:46 +0000222 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000223 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000224
225} // end anonymous namespace
226
227char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000228
Matthias Braun864cf582017-09-09 00:52:46 +0000229INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
230 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000231
Matthias Braun864cf582017-09-09 00:52:46 +0000232/// This allocates space for the specified virtual register to be held on the
233/// stack.
Matthias Braunebcf5432018-11-07 02:04:11 +0000234int RegAllocFast::getStackSpaceFor(unsigned VirtReg) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000235 // Find the location Reg would belong...
236 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000237 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000238 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000239 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000240
241 // Allocate a new stack object for this spill location...
Matthias Braunebcf5432018-11-07 02:04:11 +0000242 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000243 unsigned Size = TRI->getSpillSize(RC);
244 unsigned Align = TRI->getSpillAlignment(RC);
245 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000246
247 // Assign the slot.
248 StackSlotForVirtReg[VirtReg] = FrameIdx;
249 return FrameIdx;
250}
251
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000252/// Insert spill instruction for \p AssignedReg before \p Before. Update
253/// DBG_VALUEs with \p VirtReg operands with the stack slot.
254void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
255 MCPhysReg AssignedReg, bool Kill) {
256 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
257 << " in " << printReg(AssignedReg, TRI));
258 int FI = getStackSpaceFor(VirtReg);
259 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << "\n");
260
261 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
262 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
263 ++NumStores;
264
265 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
266 // identify spilled location as the place to find corresponding variable's
267 // value.
268 SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
269 for (MachineInstr *DBG : LRIDbgValues) {
270 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
271 assert(NewDV->getParent() == MBB && "dangling parent pointer");
272 (void)NewDV;
273 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
274 }
275 // Now this register is spilled there is should not be any DBG_VALUE
276 // pointing to this register because they are all pointing to spilled value
277 // now.
278 LRIDbgValues.clear();
279}
280
281/// Insert reload instruction for \p PhysReg before \p Before.
282void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
283 MCPhysReg PhysReg) {
284 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
285 << printReg(PhysReg, TRI) << "\n");
286 int FI = getStackSpaceFor(VirtReg);
287 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
288 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
289 ++NumLoads;
290}
291
Matthias Braun864cf582017-09-09 00:52:46 +0000292/// Return true if MO is the only remaining reference to its virtual register,
293/// and it is guaranteed to be a block-local register.
294bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000295 // If the register has ever been spilled or reloaded, we conservatively assume
296 // it is a global register used in multiple blocks.
297 if (StackSlotForVirtReg[MO.getReg()] != -1)
298 return false;
299
300 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000301 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000302 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000303 return false;
304 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000305}
306
Matthias Braun864cf582017-09-09 00:52:46 +0000307/// Set kill flags on last use of a virtual register.
308void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000309 if (!LR.LastUse) return;
310 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000311 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
312 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000313 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000314 // else, don't do anything we are problably redefining a
315 // subreg of this register and given we don't track which
316 // lanes are actually dead, we cannot insert a kill flag here.
317 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000318 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000319 // ... <== Here we would allow later pass to reuse physreg:sub1
320 // which is potentially wrong.
321 // LR:sub0 = ...
322 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000323 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000324}
325
Matthias Braun864cf582017-09-09 00:52:46 +0000326/// Mark virtreg as no longer available.
327void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000328 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000329 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
330 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000331 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000332 // Erase from LiveVirtRegs unless we're spilling in bulk.
333 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000334 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000335}
336
Matthias Braun864cf582017-09-09 00:52:46 +0000337/// Mark virtreg as no longer available.
338void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000339 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
340 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000341 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000342 if (LRI != LiveVirtRegs.end())
343 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000344}
345
Matthias Braun864cf582017-09-09 00:52:46 +0000346/// This method spills the value specified by VirtReg into the corresponding
347/// stack slot if needed.
348void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
349 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000350 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
351 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000352 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000353 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
354 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000355}
356
Matthias Braun864cf582017-09-09 00:52:46 +0000357/// Do the actual work of spilling.
358void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
359 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000360 LiveReg &LR = *LRI;
361 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000362
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000363 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000364 // If this physreg is used by the instruction, we want to kill it on the
365 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000366 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000367 LR.Dirty = false;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000368
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000369 spill(MI, LRI->VirtReg, LR.PhysReg, SpillKill);
370
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000371 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000372 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000373 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000374 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000375}
376
Matthias Braun864cf582017-09-09 00:52:46 +0000377/// Spill all dirty virtregs without killing them.
378void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000379 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000380 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000381 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
382 // of spilling here is deterministic, if arbitrary.
Matthias Braun864cf582017-09-09 00:52:46 +0000383 for (LiveRegMap::iterator I = LiveVirtRegs.begin(), E = LiveVirtRegs.end();
384 I != E; ++I)
385 spillVirtReg(MI, I);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000386 LiveVirtRegs.clear();
387 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000388}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000389
Matthias Braun864cf582017-09-09 00:52:46 +0000390/// Handle the direct use of a physical register. Check that the register is
391/// not used by a virtreg. Kill the physreg, marking it free. This may add
392/// implicit kills to MO->getParent() and invalidate MO.
393void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000394 // Ignore undef uses.
395 if (MO.isUndef())
396 return;
397
Matthias Braun864cf582017-09-09 00:52:46 +0000398 unsigned PhysReg = MO.getReg();
399 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
400 "Bad usePhysReg operand");
401
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000402 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000403 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000404 case regDisabled:
405 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000406 case regReserved:
407 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000408 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000409 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000410 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000411 return;
412 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000413 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000414 // wanted has been clobbered.
415 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000416 }
417
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000418 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000419 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000420 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000421 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000422 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000423 break;
424 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000425 // Either PhysReg is a subregister of Alias and we mark the
426 // whole register as free, or PhysReg is the superregister of
427 // Alias and we mark all the aliases as disabled before freeing
428 // PhysReg.
429 // In the latter case, since PhysReg was disabled, this means that
430 // its value is defined only by physical sub-registers. This check
431 // is performed by the assert of the default case in this loop.
432 // Note: The value of the superregister may only be partial
433 // defined, that is why regDisabled is a valid state for aliases.
434 assert((TRI->isSuperRegister(PhysReg, Alias) ||
435 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000436 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000437 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000438 case regFree:
439 if (TRI->isSuperRegister(PhysReg, Alias)) {
440 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000441 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000442 MO.getParent()->addRegisterKilled(Alias, TRI, true);
443 return;
444 }
445 // Some other alias was in the working set - clear it.
446 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000447 break;
448 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000449 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000450 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000451 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000452
453 // All aliases are disabled, bring register into working set.
454 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000455 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000456}
457
Matthias Braun864cf582017-09-09 00:52:46 +0000458/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
459/// similar to defineVirtReg except the physreg is reserved instead of
460/// allocated.
Quentin Colombet72f6d592018-01-29 23:42:37 +0000461void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
462 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000463 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000464 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
465 case regDisabled:
466 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000467 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000468 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000469 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000470 case regFree:
471 case regReserved:
472 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000473 return;
474 }
475
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000476 // This is a disabled register, disable all aliases.
477 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000478 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000479 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000480 switch (unsigned VirtReg = PhysRegState[Alias]) {
481 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000482 break;
483 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000484 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000485 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000486 case regFree:
487 case regReserved:
488 PhysRegState[Alias] = regDisabled;
489 if (TRI->isSuperRegister(PhysReg, Alias))
490 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000491 break;
492 }
493 }
494}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000495
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000496/// Return the cost of spilling clearing out PhysReg and aliases so it is
Matthias Braun864cf582017-09-09 00:52:46 +0000497/// free for allocation. Returns 0 when PhysReg is free or disabled with all
498/// aliases disabled - it can be allocated directly.
499/// \returns spillImpossible when PhysReg or an alias can't be spilled.
500unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000501 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000502 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
503 << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000504 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000505 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000506 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
507 case regDisabled:
508 break;
509 case regFree:
510 return 0;
511 case regReserved:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000512 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
513 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000514 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000515 default: {
516 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
517 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
518 return I->Dirty ? spillDirty : spillClean;
519 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000520 }
521
Eric Christopherc3783362011-04-12 00:48:08 +0000522 // This is a disabled register, add up cost of aliases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000523 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000524 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000525 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000526 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000527 switch (unsigned VirtReg = PhysRegState[Alias]) {
528 case regDisabled:
529 break;
530 case regFree:
531 ++Cost;
532 break;
533 case regReserved:
534 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000535 default: {
536 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
537 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
538 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000539 break;
540 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000541 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000542 }
543 return Cost;
544}
545
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000546/// This method updates local state so that we know that PhysReg is the
Matthias Braun864cf582017-09-09 00:52:46 +0000547/// proper container for VirtReg now. The physical register must not be used
548/// for anything else when this is called.
549void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000550 LLVM_DEBUG(dbgs() << "Assigning " << printReg(LR.VirtReg, TRI) << " to "
551 << printReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000552 PhysRegState[PhysReg] = LR.VirtReg;
553 assert(!LR.PhysReg && "Already assigned a physreg");
554 LR.PhysReg = PhysReg;
555}
556
Matthias Braun864cf582017-09-09 00:52:46 +0000557RegAllocFast::LiveRegMap::iterator
558RegAllocFast::assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000559 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
560 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
561 assignVirtToPhysReg(*LRI, PhysReg);
562 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000563}
564
Matthias Braun864cf582017-09-09 00:52:46 +0000565/// Allocates a physical register for VirtReg.
566RegAllocFast::LiveRegMap::iterator RegAllocFast::allocVirtReg(MachineInstr &MI,
567 LiveRegMap::iterator LRI, unsigned Hint) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000568 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000569
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000570 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
571 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000572
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000573 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000574 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
575 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
576 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000577 // Ignore the hint if we would have to spill a dirty register.
578 unsigned Cost = calcSpillCost(Hint);
579 if (Cost < spillDirty) {
580 if (Cost)
581 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000582 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
583 // That invalidates LRI, so run a new lookup for VirtReg.
584 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000585 }
586 }
587
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000588 // First try to find a completely free register.
Matthias Braun864cf582017-09-09 00:52:46 +0000589 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(&RC);
590 for (MCPhysReg PhysReg : AO) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000591 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000592 assignVirtToPhysReg(*LRI, PhysReg);
593 return LRI;
594 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000595 }
596
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000597 LLVM_DEBUG(dbgs() << "Allocating " << printReg(VirtReg) << " from "
598 << TRI->getRegClassName(&RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000599
Matthias Braun864cf582017-09-09 00:52:46 +0000600 unsigned BestReg = 0;
601 unsigned BestCost = spillImpossible;
602 for (MCPhysReg PhysReg : AO) {
603 unsigned Cost = calcSpillCost(PhysReg);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000604 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << "\n");
605 LLVM_DEBUG(dbgs() << "\tCost: " << Cost << "\n");
606 LLVM_DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000607 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000608 if (Cost == 0) {
Matthias Braun864cf582017-09-09 00:52:46 +0000609 assignVirtToPhysReg(*LRI, PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000610 return LRI;
611 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000612 if (Cost < BestCost)
Matthias Braun864cf582017-09-09 00:52:46 +0000613 BestReg = PhysReg, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000614 }
615
616 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000617 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000618 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
619 // That invalidates LRI, so run a new lookup for VirtReg.
620 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000621 }
622
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000623 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000624 if (MI.isInlineAsm())
625 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000626 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000627 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000628 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000629 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000630}
631
Matthias Braun864cf582017-09-09 00:52:46 +0000632/// Allocates a register for VirtReg and mark it as dirty.
633RegAllocFast::LiveRegMap::iterator RegAllocFast::defineVirtReg(MachineInstr &MI,
634 unsigned OpNum,
635 unsigned VirtReg,
636 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000637 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
638 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000639 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000640 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000641 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000642 if (New) {
643 // If there is no hint, peek at the only use of this register.
644 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
645 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000646 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000647 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000648 if (UseMI.isCopyLike())
649 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000650 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000651 LRI = allocVirtReg(MI, LRI, Hint);
652 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000653 // Redefining a live register - kill at the last use, unless it is this
654 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000655 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000656 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000657 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000658 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000659 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000660 LRI->LastOpNum = OpNum;
661 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000662 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000663 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000664}
665
Matthias Braun864cf582017-09-09 00:52:46 +0000666/// Make sure VirtReg is available in a physreg and return it.
667RegAllocFast::LiveRegMap::iterator RegAllocFast::reloadVirtReg(MachineInstr &MI,
668 unsigned OpNum,
669 unsigned VirtReg,
670 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000671 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
672 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000673 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000674 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000675 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000676 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000677 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000678 LRI = allocVirtReg(MI, LRI, Hint);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000679 reload(MI, VirtReg, LRI->PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000680 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000681 if (isLastUseOfLocalReg(MO)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000682 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000683 if (MO.isUse())
684 MO.setIsKill();
685 else
686 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000687 } else if (MO.isKill()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000688 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000689 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000690 } else if (MO.isDead()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000691 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000692 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000693 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000694 } else if (MO.isKill()) {
695 // We must remove kill flags from uses of reloaded registers because the
696 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000697 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000698 // This would cause a second reload of %x into a different register.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000699 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000700 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000701 } else if (MO.isDead()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000702 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000703 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000704 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000705 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000706 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000707 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000708 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000709 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000710}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000711
Matthias Braun864cf582017-09-09 00:52:46 +0000712/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
713/// may invalidate any operand pointers. Return true if the operand kills its
714/// register.
715bool RegAllocFast::setPhysReg(MachineInstr &MI, unsigned OpNum,
716 MCPhysReg PhysReg) {
717 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000718 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000719 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000720 MO.setReg(PhysReg);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000721 MO.setIsRenamable(true);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000722 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000723 }
724
725 // Handle subregister index.
726 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000727 MO.setIsRenamable(true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000728 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000729
730 // A kill flag implies killing the full register. Add corresponding super
731 // register kill.
732 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000733 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000734 return true;
735 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000736
737 // A <def,read-undef> of a sub-register requires an implicit def of the full
738 // register.
739 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000740 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000741
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000742 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000743}
744
Matthias Braun864cf582017-09-09 00:52:46 +0000745// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000746// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000747void RegAllocFast::handleThroughOperands(MachineInstr &MI,
748 SmallVectorImpl<unsigned> &VirtDead) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000749 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000750 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000751 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000752 if (!MO.isReg()) continue;
753 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000754 if (!TargetRegisterInfo::isVirtualRegister(Reg))
755 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000756 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
757 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000758 if (ThroughRegs.insert(Reg).second)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000759 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000760 }
761 }
762
763 // If any physreg defines collide with preallocated through registers,
764 // we must spill and reallocate.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000765 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000766 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000767 if (!MO.isReg() || !MO.isDef()) continue;
768 unsigned Reg = MO.getReg();
769 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000770 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000771 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000772 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000773 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000774 }
775 }
776
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000777 SmallVector<unsigned, 8> PartialDefs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000778 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000779 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
780 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000781 if (!MO.isReg()) continue;
782 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000783 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000784 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000785 if (!MO.isTied()) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000786 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
787 << ") is tied to operand " << MI.findTiedOperandIdx(I)
788 << ".\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000789 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
790 MCPhysReg PhysReg = LRI->PhysReg;
791 setPhysReg(MI, I, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000792 // Note: we don't update the def operand yet. That would cause the normal
793 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000794 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000795 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000796 // Reload the register, but don't assign to the operand just yet.
797 // That would confuse the later phys-def processing pass.
Matthias Braun864cf582017-09-09 00:52:46 +0000798 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000799 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000800 }
801 }
802
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000803 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000804 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
805 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000806 if (!MO.isReg()) continue;
807 unsigned Reg = MO.getReg();
808 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
809 if (!MO.isEarlyClobber())
810 continue;
811 // Note: defineVirtReg may invalidate MO.
Matthias Braun864cf582017-09-09 00:52:46 +0000812 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, 0);
813 MCPhysReg PhysReg = LRI->PhysReg;
814 if (setPhysReg(MI, I, PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000815 VirtDead.push_back(Reg);
816 }
817
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000818 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000819 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000820 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000821 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
822 unsigned Reg = MO.getReg();
823 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000824 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
825 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000826 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000827 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000828
829 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000830 for (unsigned PartialDef : PartialDefs)
831 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000832}
833
Matthias Braun864cf582017-09-09 00:52:46 +0000834#ifndef NDEBUG
835void RegAllocFast::dumpState() {
836 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
837 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000838 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000839 switch(PhysRegState[Reg]) {
840 case regFree:
841 break;
842 case regReserved:
843 dbgs() << "*";
844 break;
845 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000846 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun864cf582017-09-09 00:52:46 +0000847 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
848 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
849 if (I->Dirty)
850 dbgs() << "*";
851 assert(I->PhysReg == Reg && "Bad inverse map");
852 break;
853 }
854 }
855 }
856 dbgs() << '\n';
857 // Check that LiveVirtRegs is the inverse.
858 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
859 e = LiveVirtRegs.end(); i != e; ++i) {
860 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
861 "Bad map key");
862 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
863 "Bad map value");
864 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
865 }
866}
867#endif
868
869void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
870 this->MBB = &MBB;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000871 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000872
873 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000874 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000875
Matthias Braun864cf582017-09-09 00:52:46 +0000876 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000877
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000878 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +0000879 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +0000880 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet72f6d592018-01-29 23:42:37 +0000881 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000882
Matthias Brauna09d18d2017-09-09 00:52:45 +0000883 VirtDead.clear();
884 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000885
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000886 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +0000887 for (MachineInstr &MI : MBB) {
888 const MCInstrDesc &MCID = MI.getDesc();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000889 LLVM_DEBUG(dbgs() << "\n>> " << MI << "Regs:"; dumpState());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000890
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000891 // Debug values are not allowed to change codegen in any way.
Matthias Braun864cf582017-09-09 00:52:46 +0000892 if (MI.isDebugValue()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000893 MachineInstr *DebugMI = &MI;
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000894 MachineOperand &MO = DebugMI->getOperand(0);
895
896 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
897 // mostly constants and frame indices.
898 if (!MO.isReg())
899 continue;
900 unsigned Reg = MO.getReg();
901 if (!TargetRegisterInfo::isVirtualRegister(Reg))
902 continue;
903
904 // See if this virtual register has already been allocated to a physical
905 // register or spilled to a stack slot.
906 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
907 if (LRI != LiveVirtRegs.end())
908 setPhysReg(*DebugMI, 0, LRI->PhysReg);
909 else {
910 int SS = StackSlotForVirtReg[Reg];
911 if (SS != -1) {
912 // Modify DBG_VALUE now that the value is in a spill slot.
913 updateDbgValueForSpill(*DebugMI, SS);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000914 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:"
915 << "\t" << *DebugMI);
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000916 continue;
Devang Patel57e72372010-07-09 21:48:31 +0000917 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000918
919 // We can't allocate a physreg for a DebugValue, sorry!
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000920 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000921 MO.setReg(0);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000922 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000923
924 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
925 // that future spills of Reg will have DBG_VALUEs.
926 LiveDbgValueMap[Reg].push_back(DebugMI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000927 continue;
928 }
929
Shiva Chen801bf7e2018-05-09 02:42:00 +0000930 if (MI.isDebugLabel())
931 continue;
932
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000933 // If this is a copy, we may be able to coalesce.
Matthias Braun864cf582017-09-09 00:52:46 +0000934 unsigned CopySrcReg = 0;
935 unsigned CopyDstReg = 0;
936 unsigned CopySrcSub = 0;
937 unsigned CopyDstSub = 0;
938 if (MI.isCopy()) {
939 CopyDstReg = MI.getOperand(0).getReg();
940 CopySrcReg = MI.getOperand(1).getReg();
941 CopyDstSub = MI.getOperand(0).getSubReg();
942 CopySrcSub = MI.getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000943 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000944
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000945 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000946 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000947
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000948 // First scan.
949 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000950 // Find the end of the virtreg operands
951 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000952 bool hasTiedOps = false;
953 bool hasEarlyClobbers = false;
954 bool hasPartialRedefs = false;
955 bool hasPhysDefs = false;
Matthias Braun864cf582017-09-09 00:52:46 +0000956 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
957 MachineOperand &MO = MI.getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000958 // Make sure MRI knows about registers clobbered by regmasks.
959 if (MO.isRegMask()) {
960 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
961 continue;
962 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000963 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000964 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000965 if (!Reg) continue;
966 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
967 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000968 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000969 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000970 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000971 } else {
972 if (MO.isEarlyClobber())
973 hasEarlyClobbers = true;
Matthias Braun864cf582017-09-09 00:52:46 +0000974 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000975 hasPartialRedefs = true;
976 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000977 continue;
978 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000979 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000980 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000981 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000982 } else if (MO.isEarlyClobber()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000983 definePhysReg(MI, Reg,
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000984 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000985 hasEarlyClobbers = true;
986 } else
987 hasPhysDefs = true;
988 }
989
990 // The instruction may have virtual register operands that must be allocated
991 // the same register at use-time and def-time: early clobbers and tied
992 // operands. If there are also physical defs, these registers must avoid
993 // both physical defs and uses, making them more constrained than normal
994 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000995 // Similarly, if there are multiple defs and tied operands, we must make
996 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000997 // We didn't detect inline asm tied operands above, so just make this extra
998 // pass for all inline asm.
Matthias Braun864cf582017-09-09 00:52:46 +0000999 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +00001000 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001001 handleThroughOperands(MI, VirtDead);
1002 // Don't attempt coalescing when we have funny stuff going on.
Matthias Braun864cf582017-09-09 00:52:46 +00001003 CopyDstReg = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001004 // Pretend we have early clobbers so the use operands get marked below.
1005 // This is not necessary for the common case of a single tied use.
1006 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001007 }
1008
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001009 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001010 // Allocate virtreg uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001011 for (unsigned I = 0; I != VirtOpEnd; ++I) {
1012 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001013 if (!MO.isReg()) continue;
1014 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001015 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001016 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +00001017 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, CopyDstReg);
1018 MCPhysReg PhysReg = LRI->PhysReg;
1019 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
1020 if (setPhysReg(MI, I, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001021 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001022 }
1023 }
1024
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001025 // Track registers defined by instruction - early clobbers and tied uses at
1026 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001027 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001028 if (hasEarlyClobbers) {
Matthias Braun864cf582017-09-09 00:52:46 +00001029 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001030 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001031 unsigned Reg = MO.getReg();
1032 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001033 // Look for physreg defs and tied uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001034 if (!MO.isDef() && !MO.isTied()) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001035 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001036 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001037 }
1038
Matthias Braun864cf582017-09-09 00:52:46 +00001039 unsigned DefOpEnd = MI.getNumOperands();
1040 if (MI.isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001041 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001042 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001043 // registers in their spill slots.
1044 // Note: although this is appealing to just consider all definitions
1045 // as call-clobbered, this is not correct because some of those
1046 // definitions may be used later on and we do not want to reuse
1047 // those for virtual registers in between.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001048 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001049 spillAll(MI);
1050 }
1051
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001052 // Third scan.
1053 // Allocate defs and collect dead defs.
Matthias Braun864cf582017-09-09 00:52:46 +00001054 for (unsigned I = 0; I != DefOpEnd; ++I) {
1055 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001056 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1057 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001058 unsigned Reg = MO.getReg();
1059
1060 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001061 if (!MRI->isAllocatable(Reg)) continue;
Matthias Braun864cf582017-09-09 00:52:46 +00001062 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001063 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001064 }
Matthias Braun864cf582017-09-09 00:52:46 +00001065 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, CopySrcReg);
1066 MCPhysReg PhysReg = LRI->PhysReg;
1067 if (setPhysReg(MI, I, PhysReg)) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001068 VirtDead.push_back(Reg);
Matthias Braun864cf582017-09-09 00:52:46 +00001069 CopyDstReg = 0; // cancel coalescing;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001070 } else
Matthias Braun864cf582017-09-09 00:52:46 +00001071 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072 }
1073
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001074 // Kill dead defs after the scan to ensure that multiple defs of the same
1075 // register are allocated identically. We didn't need to do this for uses
1076 // because we are crerating our own kill flags, and they are always at the
1077 // last use.
Matthias Braun864cf582017-09-09 00:52:46 +00001078 for (unsigned VirtReg : VirtDead)
1079 killVirtReg(VirtReg);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001080 VirtDead.clear();
1081
Matthias Braun864cf582017-09-09 00:52:46 +00001082 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001083 LLVM_DEBUG(dbgs() << "-- coalescing: " << MI);
Matthias Braun864cf582017-09-09 00:52:46 +00001084 Coalesced.push_back(&MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001085 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001086 LLVM_DEBUG(dbgs() << "<< " << MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001087 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001088 }
1089
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001090 // Spill all physical registers holding virtual registers now.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001091 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001092 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001093
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001094 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001095 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001096 for (MachineInstr *MI : Coalesced)
1097 MBB.erase(MI);
Matthias Braun14af82a2018-11-07 02:04:07 +00001098 NumCoalesced += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001099
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001100 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001101}
1102
Matthias Braun864cf582017-09-09 00:52:46 +00001103bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001104 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1105 << "********** Function: " << MF.getName() << '\n');
Matthias Braun864cf582017-09-09 00:52:46 +00001106 MRI = &MF.getRegInfo();
1107 const TargetSubtargetInfo &STI = MF.getSubtarget();
1108 TRI = STI.getRegisterInfo();
1109 TII = STI.getInstrInfo();
1110 MFI = &MF.getFrameInfo();
1111 MRI->freezeReservedRegs(MF);
1112 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001113 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001114 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001115
1116 // initialize the virtual->physical register map to have a 'null'
1117 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001118 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1119 StackSlotForVirtReg.resize(NumVirtRegs);
1120 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001121
1122 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001123 for (MachineBasicBlock &MBB : MF)
1124 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001125
Andrew Trickda84e642012-02-21 04:51:23 +00001126 // All machine operands and other references to virtual registers have been
1127 // replaced. Remove the virtual registers.
1128 MRI->clearVirtRegs();
1129
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001130 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001131 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001132 return true;
1133}
1134
1135FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001136 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001137}