blob: e2980d175e774032678a3060d5fc39ffe445d290 [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");
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +000057STATISTIC(NumCopies, "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
Eugene Zelenko618c5552017-09-13 21:15:20 +000091 explicit LiveReg(unsigned v) : VirtReg(v) {}
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>;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000099
Matthias Braun864cf582017-09-09 00:52:46 +0000100 /// This map contains entries for each virtual register that is currently
101 /// available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000102 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000103
Eugene Zelenko618c5552017-09-13 21:15:20 +0000104 DenseMap<unsigned, SmallVector<MachineInstr *, 4>> LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +0000105
Matthias Braun864cf582017-09-09 00:52:46 +0000106 /// Track the state of a physical register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000107 enum RegState {
Matthias Braun864cf582017-09-09 00:52:46 +0000108 /// A disabled register is not available for allocation, but an alias may
109 /// be in use. A register can only be moved out of the disabled state if
110 /// all aliases are disabled.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000111 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000112
Matthias Braun864cf582017-09-09 00:52:46 +0000113 /// A free register is not currently in use and can be allocated
114 /// immediately without checking aliases.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000115 regFree,
116
Matthias Braun864cf582017-09-09 00:52:46 +0000117 /// A reserved register has been assigned explicitly (e.g., setting up a
118 /// call parameter), and it remains reserved until it is used.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000119 regReserved
120
Matthias Braun864cf582017-09-09 00:52:46 +0000121 /// A register state may also be a virtual register number, indication
122 /// that the physical register is currently allocated to a virtual
123 /// register. In that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000124 };
125
Matthias Braun864cf582017-09-09 00:52:46 +0000126 /// One of the RegState enums, or a virtreg.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000127 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000128
Matthias Brauna09d18d2017-09-09 00:52:45 +0000129 SmallVector<unsigned, 16> VirtDead;
Eugene Zelenko618c5552017-09-13 21:15:20 +0000130 SmallVector<MachineInstr *, 32> Coalesced;
Matthias Brauna09d18d2017-09-09 00:52:45 +0000131
Matthias Braun864cf582017-09-09 00:52:46 +0000132 /// Set of register units.
Eugene Zelenko618c5552017-09-13 21:15:20 +0000133 using UsedInInstrSet = SparseSet<unsigned>;
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000134
Matthias Braun864cf582017-09-09 00:52:46 +0000135 /// Set of register units that are used in the current instruction, and so
136 /// cannot be allocated.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000137 UsedInInstrSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000138
Matthias Braun864cf582017-09-09 00:52:46 +0000139 /// Mark a physreg as used in this instruction.
140 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000141 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
142 UsedInInstr.insert(*Units);
143 }
144
Matthias Braun864cf582017-09-09 00:52:46 +0000145 /// Check if a physreg or any of its aliases are used in this instruction.
146 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000147 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
148 if (UsedInInstr.count(*Units))
149 return true;
150 return false;
151 }
152
Matthias Braun864cf582017-09-09 00:52:46 +0000153 /// This flag is set when LiveRegMap will be cleared completely after
154 /// spilling all live registers. LiveRegMap entries should not be erased.
155 bool isBulkSpilling = false;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000156
Alp Toker61007d82014-03-02 03:20:38 +0000157 enum : unsigned {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000158 spillClean = 1,
159 spillDirty = 100,
160 spillImpossible = ~0u
161 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000162
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000163 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000164 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000165
Craig Topper4584cd52014-03-07 09:26:03 +0000166 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000167 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000168 MachineFunctionPass::getAnalysisUsage(AU);
169 }
170
Matthias Braun90799ce2016-08-23 21:19:49 +0000171 MachineFunctionProperties getRequiredProperties() const override {
172 return MachineFunctionProperties().set(
173 MachineFunctionProperties::Property::NoPHIs);
174 }
175
Derek Schuffad154c82016-03-28 17:05:30 +0000176 MachineFunctionProperties getSetProperties() const override {
177 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000178 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000179 }
180
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000181 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000182 bool runOnMachineFunction(MachineFunction &Fn) override;
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 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass &RC);
187 bool isLastUseOfLocalReg(const MachineOperand &MO) const;
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000188
Matthias Braun864cf582017-09-09 00:52:46 +0000189 void addKillFlag(const LiveReg &LRI);
190 void killVirtReg(LiveRegMap::iterator LRI);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000191 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000192 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000193 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000194
Matthias Braun864cf582017-09-09 00:52:46 +0000195 void usePhysReg(MachineOperand &MO);
Quentin Colombet72f6d592018-01-29 23:42:37 +0000196 void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
197 RegState NewState);
Matthias Braun864cf582017-09-09 00:52:46 +0000198 unsigned calcSpillCost(MCPhysReg PhysReg) const;
Quentin Colombet72f6d592018-01-29 23:42:37 +0000199 void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000200
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000201 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
202 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
203 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000204
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000205 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
206 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
207 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000208
Matthias Braun864cf582017-09-09 00:52:46 +0000209 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, MCPhysReg PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000210 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000211 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000212 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000213 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000214 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000215 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000216 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000217 bool setPhysReg(MachineInstr &MI, unsigned OpNum, MCPhysReg PhysReg);
218
219 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000220 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000221
222} // end anonymous namespace
223
224char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000225
Matthias Braun864cf582017-09-09 00:52:46 +0000226INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
227 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000228
Matthias Braun864cf582017-09-09 00:52:46 +0000229/// This allocates space for the specified virtual register to be held on the
230/// stack.
231int RegAllocFast::getStackSpaceFor(unsigned VirtReg,
232 const TargetRegisterClass &RC) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000233 // Find the location Reg would belong...
234 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000235 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000236 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000237 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000238
239 // Allocate a new stack object for this spill location...
Matthias Braun864cf582017-09-09 00:52:46 +0000240 unsigned Size = TRI->getSpillSize(RC);
241 unsigned Align = TRI->getSpillAlignment(RC);
242 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000243
244 // Assign the slot.
245 StackSlotForVirtReg[VirtReg] = FrameIdx;
246 return FrameIdx;
247}
248
Matthias Braun864cf582017-09-09 00:52:46 +0000249/// Return true if MO is the only remaining reference to its virtual register,
250/// and it is guaranteed to be a block-local register.
251bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000252 // If the register has ever been spilled or reloaded, we conservatively assume
253 // it is a global register used in multiple blocks.
254 if (StackSlotForVirtReg[MO.getReg()] != -1)
255 return false;
256
257 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000258 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000259 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000260 return false;
261 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000262}
263
Matthias Braun864cf582017-09-09 00:52:46 +0000264/// Set kill flags on last use of a virtual register.
265void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000266 if (!LR.LastUse) return;
267 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000268 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
269 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000270 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000271 // else, don't do anything we are problably redefining a
272 // subreg of this register and given we don't track which
273 // lanes are actually dead, we cannot insert a kill flag here.
274 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000275 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000276 // ... <== Here we would allow later pass to reuse physreg:sub1
277 // which is potentially wrong.
278 // LR:sub0 = ...
279 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000280 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000281}
282
Matthias Braun864cf582017-09-09 00:52:46 +0000283/// Mark virtreg as no longer available.
284void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000285 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000286 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
287 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000288 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000289 // Erase from LiveVirtRegs unless we're spilling in bulk.
290 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000291 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000292}
293
Matthias Braun864cf582017-09-09 00:52:46 +0000294/// Mark virtreg as no longer available.
295void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000296 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
297 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000298 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000299 if (LRI != LiveVirtRegs.end())
300 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000301}
302
Matthias Braun864cf582017-09-09 00:52:46 +0000303/// This method spills the value specified by VirtReg into the corresponding
304/// stack slot if needed.
305void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
306 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000307 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
308 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000309 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000310 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
311 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000312}
313
Matthias Braun864cf582017-09-09 00:52:46 +0000314/// Do the actual work of spilling.
315void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
316 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000317 LiveReg &LR = *LRI;
318 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000319
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000320 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000321 // If this physreg is used by the instruction, we want to kill it on the
322 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000323 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000324 LR.Dirty = false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000325 LLVM_DEBUG(dbgs() << "Spilling " << printReg(LRI->VirtReg, TRI) << " in "
326 << printReg(LR.PhysReg, TRI));
Matthias Braun864cf582017-09-09 00:52:46 +0000327 const TargetRegisterClass &RC = *MRI->getRegClass(LRI->VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000328 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000329 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000330 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, &RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000331 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000332
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000333 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000334 // identify spilled location as the place to find corresponding variable's
335 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000336 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000337 LiveDbgValueMap[LRI->VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000338 for (MachineInstr *DBG : LRIDbgValues) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000339 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, MI, *DBG, FI);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000340 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000341 (void)NewDV;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000342 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:"
343 << "\n"
344 << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000345 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000346 // Now this register is spilled there is should not be any DBG_VALUE
347 // pointing to this register because they are all pointing to spilled value
348 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000349 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000350 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000351 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000352 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000353 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000354}
355
Matthias Braun864cf582017-09-09 00:52:46 +0000356/// Spill all dirty virtregs without killing them.
357void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000358 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000359 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000360 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
361 // of spilling here is deterministic, if arbitrary.
Matthias Braun864cf582017-09-09 00:52:46 +0000362 for (LiveRegMap::iterator I = LiveVirtRegs.begin(), E = LiveVirtRegs.end();
363 I != E; ++I)
364 spillVirtReg(MI, I);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000365 LiveVirtRegs.clear();
366 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000367}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000368
Matthias Braun864cf582017-09-09 00:52:46 +0000369/// Handle the direct use of a physical register. Check that the register is
370/// not used by a virtreg. Kill the physreg, marking it free. This may add
371/// implicit kills to MO->getParent() and invalidate MO.
372void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000373 // Ignore undef uses.
374 if (MO.isUndef())
375 return;
376
Matthias Braun864cf582017-09-09 00:52:46 +0000377 unsigned PhysReg = MO.getReg();
378 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
379 "Bad usePhysReg operand");
380
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000381 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000382 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000383 case regDisabled:
384 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000385 case regReserved:
386 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000387 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000388 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000389 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000390 return;
391 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000392 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000393 // wanted has been clobbered.
394 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000395 }
396
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000397 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000398 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000399 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000400 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000401 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000402 break;
403 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000404 // Either PhysReg is a subregister of Alias and we mark the
405 // whole register as free, or PhysReg is the superregister of
406 // Alias and we mark all the aliases as disabled before freeing
407 // PhysReg.
408 // In the latter case, since PhysReg was disabled, this means that
409 // its value is defined only by physical sub-registers. This check
410 // is performed by the assert of the default case in this loop.
411 // Note: The value of the superregister may only be partial
412 // defined, that is why regDisabled is a valid state for aliases.
413 assert((TRI->isSuperRegister(PhysReg, Alias) ||
414 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000415 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000416 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000417 case regFree:
418 if (TRI->isSuperRegister(PhysReg, Alias)) {
419 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000420 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000421 MO.getParent()->addRegisterKilled(Alias, TRI, true);
422 return;
423 }
424 // Some other alias was in the working set - clear it.
425 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000426 break;
427 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000428 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000429 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000430 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000431
432 // All aliases are disabled, bring register into working set.
433 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000434 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000435}
436
Matthias Braun864cf582017-09-09 00:52:46 +0000437/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
438/// similar to defineVirtReg except the physreg is reserved instead of
439/// allocated.
Quentin Colombet72f6d592018-01-29 23:42:37 +0000440void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
441 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000442 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000443 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
444 case regDisabled:
445 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000446 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000447 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000448 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000449 case regFree:
450 case regReserved:
451 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000452 return;
453 }
454
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000455 // This is a disabled register, disable all aliases.
456 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000457 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000458 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000459 switch (unsigned VirtReg = PhysRegState[Alias]) {
460 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000461 break;
462 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000463 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000464 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000465 case regFree:
466 case regReserved:
467 PhysRegState[Alias] = regDisabled;
468 if (TRI->isSuperRegister(PhysReg, Alias))
469 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000470 break;
471 }
472 }
473}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000474
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000475/// Return the cost of spilling clearing out PhysReg and aliases so it is
Matthias Braun864cf582017-09-09 00:52:46 +0000476/// free for allocation. Returns 0 when PhysReg is free or disabled with all
477/// aliases disabled - it can be allocated directly.
478/// \returns spillImpossible when PhysReg or an alias can't be spilled.
479unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000480 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000481 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
482 << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000483 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000484 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000485 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
486 case regDisabled:
487 break;
488 case regFree:
489 return 0;
490 case regReserved:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000491 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
492 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000493 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000494 default: {
495 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
496 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
497 return I->Dirty ? spillDirty : spillClean;
498 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000499 }
500
Eric Christopherc3783362011-04-12 00:48:08 +0000501 // This is a disabled register, add up cost of aliases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000502 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000503 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000504 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000505 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000506 switch (unsigned VirtReg = PhysRegState[Alias]) {
507 case regDisabled:
508 break;
509 case regFree:
510 ++Cost;
511 break;
512 case regReserved:
513 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000514 default: {
515 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
516 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
517 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000518 break;
519 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000520 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000521 }
522 return Cost;
523}
524
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000525/// This method updates local state so that we know that PhysReg is the
Matthias Braun864cf582017-09-09 00:52:46 +0000526/// proper container for VirtReg now. The physical register must not be used
527/// for anything else when this is called.
528void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000529 LLVM_DEBUG(dbgs() << "Assigning " << printReg(LR.VirtReg, TRI) << " to "
530 << printReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000531 PhysRegState[PhysReg] = LR.VirtReg;
532 assert(!LR.PhysReg && "Already assigned a physreg");
533 LR.PhysReg = PhysReg;
534}
535
Matthias Braun864cf582017-09-09 00:52:46 +0000536RegAllocFast::LiveRegMap::iterator
537RegAllocFast::assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000538 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
539 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
540 assignVirtToPhysReg(*LRI, PhysReg);
541 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000542}
543
Matthias Braun864cf582017-09-09 00:52:46 +0000544/// Allocates a physical register for VirtReg.
545RegAllocFast::LiveRegMap::iterator RegAllocFast::allocVirtReg(MachineInstr &MI,
546 LiveRegMap::iterator LRI, unsigned Hint) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000547 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000548
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000549 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
550 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000551
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000552 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000553 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
554 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
555 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000556 // Ignore the hint if we would have to spill a dirty register.
557 unsigned Cost = calcSpillCost(Hint);
558 if (Cost < spillDirty) {
559 if (Cost)
560 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000561 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
562 // That invalidates LRI, so run a new lookup for VirtReg.
563 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000564 }
565 }
566
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000567 // First try to find a completely free register.
Matthias Braun864cf582017-09-09 00:52:46 +0000568 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(&RC);
569 for (MCPhysReg PhysReg : AO) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000570 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000571 assignVirtToPhysReg(*LRI, PhysReg);
572 return LRI;
573 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000574 }
575
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000576 LLVM_DEBUG(dbgs() << "Allocating " << printReg(VirtReg) << " from "
577 << TRI->getRegClassName(&RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000578
Matthias Braun864cf582017-09-09 00:52:46 +0000579 unsigned BestReg = 0;
580 unsigned BestCost = spillImpossible;
581 for (MCPhysReg PhysReg : AO) {
582 unsigned Cost = calcSpillCost(PhysReg);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000583 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << "\n");
584 LLVM_DEBUG(dbgs() << "\tCost: " << Cost << "\n");
585 LLVM_DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000586 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000587 if (Cost == 0) {
Matthias Braun864cf582017-09-09 00:52:46 +0000588 assignVirtToPhysReg(*LRI, PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000589 return LRI;
590 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000591 if (Cost < BestCost)
Matthias Braun864cf582017-09-09 00:52:46 +0000592 BestReg = PhysReg, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000593 }
594
595 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000596 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000597 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
598 // That invalidates LRI, so run a new lookup for VirtReg.
599 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000600 }
601
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000602 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000603 if (MI.isInlineAsm())
604 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000605 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000606 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000607 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000608 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000609}
610
Matthias Braun864cf582017-09-09 00:52:46 +0000611/// Allocates a register for VirtReg and mark it as dirty.
612RegAllocFast::LiveRegMap::iterator RegAllocFast::defineVirtReg(MachineInstr &MI,
613 unsigned OpNum,
614 unsigned VirtReg,
615 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000616 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
617 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000618 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000619 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000620 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000621 if (New) {
622 // If there is no hint, peek at the only use of this register.
623 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
624 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000625 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000626 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000627 if (UseMI.isCopyLike())
628 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000629 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000630 LRI = allocVirtReg(MI, LRI, Hint);
631 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000632 // Redefining a live register - kill at the last use, unless it is this
633 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000634 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000635 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000636 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000637 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000638 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000639 LRI->LastOpNum = OpNum;
640 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000641 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000642 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000643}
644
Matthias Braun864cf582017-09-09 00:52:46 +0000645/// Make sure VirtReg is available in a physreg and return it.
646RegAllocFast::LiveRegMap::iterator RegAllocFast::reloadVirtReg(MachineInstr &MI,
647 unsigned OpNum,
648 unsigned VirtReg,
649 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000650 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
651 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000652 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000653 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000654 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000655 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000656 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000657 LRI = allocVirtReg(MI, LRI, Hint);
Matthias Braun864cf582017-09-09 00:52:46 +0000658 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000659 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000660 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
661 << printReg(LRI->PhysReg, TRI) << "\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000662 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, &RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000663 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000664 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000665 if (isLastUseOfLocalReg(MO)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000666 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000667 if (MO.isUse())
668 MO.setIsKill();
669 else
670 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000671 } else if (MO.isKill()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000672 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000673 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000674 } else if (MO.isDead()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000675 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000676 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000677 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000678 } else if (MO.isKill()) {
679 // We must remove kill flags from uses of reloaded registers because the
680 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000681 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000682 // This would cause a second reload of %x into a different register.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000683 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000684 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000685 } else if (MO.isDead()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000686 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000687 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000688 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000689 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000690 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000691 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000692 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000693 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000694}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000695
Matthias Braun864cf582017-09-09 00:52:46 +0000696/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
697/// may invalidate any operand pointers. Return true if the operand kills its
698/// register.
699bool RegAllocFast::setPhysReg(MachineInstr &MI, unsigned OpNum,
700 MCPhysReg PhysReg) {
701 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000702 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000703 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000704 MO.setReg(PhysReg);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000705 MO.setIsRenamable(true);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000706 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000707 }
708
709 // Handle subregister index.
710 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000711 MO.setIsRenamable(true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000712 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000713
714 // A kill flag implies killing the full register. Add corresponding super
715 // register kill.
716 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000717 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000718 return true;
719 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000720
721 // A <def,read-undef> of a sub-register requires an implicit def of the full
722 // register.
723 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000724 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000725
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000726 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000727}
728
Matthias Braun864cf582017-09-09 00:52:46 +0000729// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000730// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000731void RegAllocFast::handleThroughOperands(MachineInstr &MI,
732 SmallVectorImpl<unsigned> &VirtDead) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000733 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000734 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000735 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000736 if (!MO.isReg()) continue;
737 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000738 if (!TargetRegisterInfo::isVirtualRegister(Reg))
739 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000740 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
741 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000742 if (ThroughRegs.insert(Reg).second)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000743 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000744 }
745 }
746
747 // If any physreg defines collide with preallocated through registers,
748 // we must spill and reallocate.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000749 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000750 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000751 if (!MO.isReg() || !MO.isDef()) continue;
752 unsigned Reg = MO.getReg();
753 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000754 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000755 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000756 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000757 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000758 }
759 }
760
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000761 SmallVector<unsigned, 8> PartialDefs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000762 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000763 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
764 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000765 if (!MO.isReg()) continue;
766 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000767 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000768 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000769 if (!MO.isTied()) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000770 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
771 << ") is tied to operand " << MI.findTiedOperandIdx(I)
772 << ".\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000773 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
774 MCPhysReg PhysReg = LRI->PhysReg;
775 setPhysReg(MI, I, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000776 // Note: we don't update the def operand yet. That would cause the normal
777 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000778 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000779 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000780 // Reload the register, but don't assign to the operand just yet.
781 // That would confuse the later phys-def processing pass.
Matthias Braun864cf582017-09-09 00:52:46 +0000782 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000783 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000784 }
785 }
786
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000787 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000788 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
789 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000790 if (!MO.isReg()) continue;
791 unsigned Reg = MO.getReg();
792 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
793 if (!MO.isEarlyClobber())
794 continue;
795 // Note: defineVirtReg may invalidate MO.
Matthias Braun864cf582017-09-09 00:52:46 +0000796 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, 0);
797 MCPhysReg PhysReg = LRI->PhysReg;
798 if (setPhysReg(MI, I, PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000799 VirtDead.push_back(Reg);
800 }
801
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000802 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000803 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000804 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000805 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
806 unsigned Reg = MO.getReg();
807 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000808 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
809 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000810 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000811 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000812
813 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000814 for (unsigned PartialDef : PartialDefs)
815 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000816}
817
Matthias Braun864cf582017-09-09 00:52:46 +0000818#ifndef NDEBUG
819void RegAllocFast::dumpState() {
820 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
821 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000822 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000823 switch(PhysRegState[Reg]) {
824 case regFree:
825 break;
826 case regReserved:
827 dbgs() << "*";
828 break;
829 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000830 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun864cf582017-09-09 00:52:46 +0000831 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
832 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
833 if (I->Dirty)
834 dbgs() << "*";
835 assert(I->PhysReg == Reg && "Bad inverse map");
836 break;
837 }
838 }
839 }
840 dbgs() << '\n';
841 // Check that LiveVirtRegs is the inverse.
842 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
843 e = LiveVirtRegs.end(); i != e; ++i) {
844 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
845 "Bad map key");
846 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
847 "Bad map value");
848 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
849 }
850}
851#endif
852
853void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
854 this->MBB = &MBB;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000855 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000856
857 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000858 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000859
Matthias Braun864cf582017-09-09 00:52:46 +0000860 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000861
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000862 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +0000863 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +0000864 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet72f6d592018-01-29 23:42:37 +0000865 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000866
Matthias Brauna09d18d2017-09-09 00:52:45 +0000867 VirtDead.clear();
868 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000869
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000870 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +0000871 for (MachineInstr &MI : MBB) {
872 const MCInstrDesc &MCID = MI.getDesc();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000873 LLVM_DEBUG(dbgs() << "\n>> " << MI << "Regs:"; dumpState());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000874
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000875 // Debug values are not allowed to change codegen in any way.
Matthias Braun864cf582017-09-09 00:52:46 +0000876 if (MI.isDebugValue()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000877 MachineInstr *DebugMI = &MI;
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000878 MachineOperand &MO = DebugMI->getOperand(0);
879
880 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
881 // mostly constants and frame indices.
882 if (!MO.isReg())
883 continue;
884 unsigned Reg = MO.getReg();
885 if (!TargetRegisterInfo::isVirtualRegister(Reg))
886 continue;
887
888 // See if this virtual register has already been allocated to a physical
889 // register or spilled to a stack slot.
890 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
891 if (LRI != LiveVirtRegs.end())
892 setPhysReg(*DebugMI, 0, LRI->PhysReg);
893 else {
894 int SS = StackSlotForVirtReg[Reg];
895 if (SS != -1) {
896 // Modify DBG_VALUE now that the value is in a spill slot.
897 updateDbgValueForSpill(*DebugMI, SS);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000898 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:"
899 << "\t" << *DebugMI);
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000900 continue;
Devang Patel57e72372010-07-09 21:48:31 +0000901 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000902
903 // We can't allocate a physreg for a DebugValue, sorry!
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000904 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000905 MO.setReg(0);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000906 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000907
908 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
909 // that future spills of Reg will have DBG_VALUEs.
910 LiveDbgValueMap[Reg].push_back(DebugMI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000911 continue;
912 }
913
Shiva Chen801bf7e2018-05-09 02:42:00 +0000914 if (MI.isDebugLabel())
915 continue;
916
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000917 // If this is a copy, we may be able to coalesce.
Matthias Braun864cf582017-09-09 00:52:46 +0000918 unsigned CopySrcReg = 0;
919 unsigned CopyDstReg = 0;
920 unsigned CopySrcSub = 0;
921 unsigned CopyDstSub = 0;
922 if (MI.isCopy()) {
923 CopyDstReg = MI.getOperand(0).getReg();
924 CopySrcReg = MI.getOperand(1).getReg();
925 CopyDstSub = MI.getOperand(0).getSubReg();
926 CopySrcSub = MI.getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000927 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000928
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000929 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000930 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000931
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000932 // First scan.
933 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000934 // Find the end of the virtreg operands
935 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000936 bool hasTiedOps = false;
937 bool hasEarlyClobbers = false;
938 bool hasPartialRedefs = false;
939 bool hasPhysDefs = false;
Matthias Braun864cf582017-09-09 00:52:46 +0000940 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
941 MachineOperand &MO = MI.getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000942 // Make sure MRI knows about registers clobbered by regmasks.
943 if (MO.isRegMask()) {
944 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
945 continue;
946 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000947 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000948 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000949 if (!Reg) continue;
950 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
951 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000952 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000953 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000954 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000955 } else {
956 if (MO.isEarlyClobber())
957 hasEarlyClobbers = true;
Matthias Braun864cf582017-09-09 00:52:46 +0000958 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000959 hasPartialRedefs = true;
960 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000961 continue;
962 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000963 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000964 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000965 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000966 } else if (MO.isEarlyClobber()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000967 definePhysReg(MI, Reg,
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000968 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000969 hasEarlyClobbers = true;
970 } else
971 hasPhysDefs = true;
972 }
973
974 // The instruction may have virtual register operands that must be allocated
975 // the same register at use-time and def-time: early clobbers and tied
976 // operands. If there are also physical defs, these registers must avoid
977 // both physical defs and uses, making them more constrained than normal
978 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000979 // Similarly, if there are multiple defs and tied operands, we must make
980 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000981 // We didn't detect inline asm tied operands above, so just make this extra
982 // pass for all inline asm.
Matthias Braun864cf582017-09-09 00:52:46 +0000983 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000984 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000985 handleThroughOperands(MI, VirtDead);
986 // Don't attempt coalescing when we have funny stuff going on.
Matthias Braun864cf582017-09-09 00:52:46 +0000987 CopyDstReg = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000988 // Pretend we have early clobbers so the use operands get marked below.
989 // This is not necessary for the common case of a single tied use.
990 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000991 }
992
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000993 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000994 // Allocate virtreg uses.
Matthias Braun864cf582017-09-09 00:52:46 +0000995 for (unsigned I = 0; I != VirtOpEnd; ++I) {
996 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000997 if (!MO.isReg()) continue;
998 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000999 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001000 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +00001001 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, CopyDstReg);
1002 MCPhysReg PhysReg = LRI->PhysReg;
1003 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
1004 if (setPhysReg(MI, I, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001005 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001006 }
1007 }
1008
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001009 // Track registers defined by instruction - early clobbers and tied uses at
1010 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001011 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001012 if (hasEarlyClobbers) {
Matthias Braun864cf582017-09-09 00:52:46 +00001013 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001014 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001015 unsigned Reg = MO.getReg();
1016 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001017 // Look for physreg defs and tied uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001018 if (!MO.isDef() && !MO.isTied()) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001019 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001020 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001021 }
1022
Matthias Braun864cf582017-09-09 00:52:46 +00001023 unsigned DefOpEnd = MI.getNumOperands();
1024 if (MI.isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001025 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001026 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001027 // registers in their spill slots.
1028 // Note: although this is appealing to just consider all definitions
1029 // as call-clobbered, this is not correct because some of those
1030 // definitions may be used later on and we do not want to reuse
1031 // those for virtual registers in between.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001032 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001033 spillAll(MI);
1034 }
1035
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001036 // Third scan.
1037 // Allocate defs and collect dead defs.
Matthias Braun864cf582017-09-09 00:52:46 +00001038 for (unsigned I = 0; I != DefOpEnd; ++I) {
1039 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001040 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1041 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001042 unsigned Reg = MO.getReg();
1043
1044 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001045 if (!MRI->isAllocatable(Reg)) continue;
Matthias Braun864cf582017-09-09 00:52:46 +00001046 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001047 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001048 }
Matthias Braun864cf582017-09-09 00:52:46 +00001049 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, CopySrcReg);
1050 MCPhysReg PhysReg = LRI->PhysReg;
1051 if (setPhysReg(MI, I, PhysReg)) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001052 VirtDead.push_back(Reg);
Matthias Braun864cf582017-09-09 00:52:46 +00001053 CopyDstReg = 0; // cancel coalescing;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001054 } else
Matthias Braun864cf582017-09-09 00:52:46 +00001055 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001056 }
1057
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001058 // Kill dead defs after the scan to ensure that multiple defs of the same
1059 // register are allocated identically. We didn't need to do this for uses
1060 // because we are crerating our own kill flags, and they are always at the
1061 // last use.
Matthias Braun864cf582017-09-09 00:52:46 +00001062 for (unsigned VirtReg : VirtDead)
1063 killVirtReg(VirtReg);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001064 VirtDead.clear();
1065
Matthias Braun864cf582017-09-09 00:52:46 +00001066 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001067 LLVM_DEBUG(dbgs() << "-- coalescing: " << MI);
Matthias Braun864cf582017-09-09 00:52:46 +00001068 Coalesced.push_back(&MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001069 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001070 LLVM_DEBUG(dbgs() << "<< " << MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001071 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072 }
1073
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001074 // Spill all physical registers holding virtual registers now.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001075 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001076 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001077
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001078 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001079 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001080 for (MachineInstr *MI : Coalesced)
1081 MBB.erase(MI);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001082 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001083
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001084 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001085}
1086
Matthias Braun864cf582017-09-09 00:52:46 +00001087/// Allocates registers for a function.
1088bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001089 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1090 << "********** Function: " << MF.getName() << '\n');
Matthias Braun864cf582017-09-09 00:52:46 +00001091 MRI = &MF.getRegInfo();
1092 const TargetSubtargetInfo &STI = MF.getSubtarget();
1093 TRI = STI.getRegisterInfo();
1094 TII = STI.getInstrInfo();
1095 MFI = &MF.getFrameInfo();
1096 MRI->freezeReservedRegs(MF);
1097 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001098 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001099 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001100
1101 // initialize the virtual->physical register map to have a 'null'
1102 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001103 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1104 StackSlotForVirtReg.resize(NumVirtRegs);
1105 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001106
1107 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001108 for (MachineBasicBlock &MBB : MF)
1109 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001110
Andrew Trickda84e642012-02-21 04:51:23 +00001111 // All machine operands and other references to virtual registers have been
1112 // replaced. Remove the virtual registers.
1113 MRI->clearVirtRegs();
1114
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001115 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001116 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001117 return true;
1118}
1119
1120FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001121 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001122}