blob: 6a5282cbbbffbbb80c7af4f35cb08d7c5d8d3af7 [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);
196 void definePhysReg(MachineInstr &MI, MCPhysReg PhysReg, RegState NewState);
197 unsigned calcSpillCost(MCPhysReg PhysReg) const;
198 void assignVirtToPhysReg(LiveReg&, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000199
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000200 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
201 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
202 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000203
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000204 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
205 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
206 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000207
Matthias Braun864cf582017-09-09 00:52:46 +0000208 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, MCPhysReg PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000209 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000210 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000211 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000212 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000213 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000214 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000215 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000216 bool setPhysReg(MachineInstr &MI, unsigned OpNum, MCPhysReg PhysReg);
217
218 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000219 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000220
221} // end anonymous namespace
222
223char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000224
Matthias Braun864cf582017-09-09 00:52:46 +0000225INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
226 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000227
Matthias Braun864cf582017-09-09 00:52:46 +0000228/// This allocates space for the specified virtual register to be held on the
229/// stack.
230int RegAllocFast::getStackSpaceFor(unsigned VirtReg,
231 const TargetRegisterClass &RC) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000232 // Find the location Reg would belong...
233 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000234 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000235 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000236 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000237
238 // Allocate a new stack object for this spill location...
Matthias Braun864cf582017-09-09 00:52:46 +0000239 unsigned Size = TRI->getSpillSize(RC);
240 unsigned Align = TRI->getSpillAlignment(RC);
241 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000242
243 // Assign the slot.
244 StackSlotForVirtReg[VirtReg] = FrameIdx;
245 return FrameIdx;
246}
247
Matthias Braun864cf582017-09-09 00:52:46 +0000248/// Return true if MO is the only remaining reference to its virtual register,
249/// and it is guaranteed to be a block-local register.
250bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000251 // If the register has ever been spilled or reloaded, we conservatively assume
252 // it is a global register used in multiple blocks.
253 if (StackSlotForVirtReg[MO.getReg()] != -1)
254 return false;
255
256 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000257 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000258 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000259 return false;
260 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000261}
262
Matthias Braun864cf582017-09-09 00:52:46 +0000263/// Set kill flags on last use of a virtual register.
264void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000265 if (!LR.LastUse) return;
266 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000267 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
268 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000269 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000270 // else, don't do anything we are problably redefining a
271 // subreg of this register and given we don't track which
272 // lanes are actually dead, we cannot insert a kill flag here.
273 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000274 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000275 // ... <== Here we would allow later pass to reuse physreg:sub1
276 // which is potentially wrong.
277 // LR:sub0 = ...
278 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000279 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000280}
281
Matthias Braun864cf582017-09-09 00:52:46 +0000282/// Mark virtreg as no longer available.
283void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000284 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000285 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
286 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000287 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000288 // Erase from LiveVirtRegs unless we're spilling in bulk.
289 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000290 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000291}
292
Matthias Braun864cf582017-09-09 00:52:46 +0000293/// Mark virtreg as no longer available.
294void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000295 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
296 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000297 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000298 if (LRI != LiveVirtRegs.end())
299 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000300}
301
Matthias Braun864cf582017-09-09 00:52:46 +0000302/// This method spills the value specified by VirtReg into the corresponding
303/// stack slot if needed.
304void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
305 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000306 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
307 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000308 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000309 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
310 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000311}
312
Matthias Braun864cf582017-09-09 00:52:46 +0000313/// Do the actual work of spilling.
314void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
315 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000316 LiveReg &LR = *LRI;
317 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000318
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000319 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000320 // If this physreg is used by the instruction, we want to kill it on the
321 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000322 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000323 LR.Dirty = false;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000324 DEBUG(dbgs() << "Spilling " << printReg(LRI->VirtReg, TRI)
325 << " in " << printReg(LR.PhysReg, TRI));
Matthias Braun864cf582017-09-09 00:52:46 +0000326 const TargetRegisterClass &RC = *MRI->getRegClass(LRI->VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000327 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000328 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000329 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, &RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000330 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000331
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000332 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000333 // identify spilled location as the place to find corresponding variable's
334 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000335 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000336 LiveDbgValueMap[LRI->VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000337 for (MachineInstr *DBG : LRIDbgValues) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000338 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, MI, *DBG, FI);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000339 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000340 (void)NewDV;
341 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000342 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000343 // Now this register is spilled there is should not be any DBG_VALUE
344 // pointing to this register because they are all pointing to spilled value
345 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000346 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000347 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000348 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000349 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000350 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000351}
352
Matthias Braun864cf582017-09-09 00:52:46 +0000353/// Spill all dirty virtregs without killing them.
354void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000355 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000356 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000357 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
358 // of spilling here is deterministic, if arbitrary.
Matthias Braun864cf582017-09-09 00:52:46 +0000359 for (LiveRegMap::iterator I = LiveVirtRegs.begin(), E = LiveVirtRegs.end();
360 I != E; ++I)
361 spillVirtReg(MI, I);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000362 LiveVirtRegs.clear();
363 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000364}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000365
Matthias Braun864cf582017-09-09 00:52:46 +0000366/// Handle the direct use of a physical register. Check that the register is
367/// not used by a virtreg. Kill the physreg, marking it free. This may add
368/// implicit kills to MO->getParent() and invalidate MO.
369void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000370 // Ignore undef uses.
371 if (MO.isUndef())
372 return;
373
Matthias Braun864cf582017-09-09 00:52:46 +0000374 unsigned PhysReg = MO.getReg();
375 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
376 "Bad usePhysReg operand");
377
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000378 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000379 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000380 case regDisabled:
381 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000382 case regReserved:
383 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000384 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000385 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000386 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000387 return;
388 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000389 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000390 // wanted has been clobbered.
391 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000392 }
393
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000394 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000395 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000396 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000397 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000398 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000399 break;
400 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000401 // Either PhysReg is a subregister of Alias and we mark the
402 // whole register as free, or PhysReg is the superregister of
403 // Alias and we mark all the aliases as disabled before freeing
404 // PhysReg.
405 // In the latter case, since PhysReg was disabled, this means that
406 // its value is defined only by physical sub-registers. This check
407 // is performed by the assert of the default case in this loop.
408 // Note: The value of the superregister may only be partial
409 // defined, that is why regDisabled is a valid state for aliases.
410 assert((TRI->isSuperRegister(PhysReg, Alias) ||
411 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000412 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000413 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000414 case regFree:
415 if (TRI->isSuperRegister(PhysReg, Alias)) {
416 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000417 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000418 MO.getParent()->addRegisterKilled(Alias, TRI, true);
419 return;
420 }
421 // Some other alias was in the working set - clear it.
422 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000423 break;
424 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000425 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000426 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000427 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000428
429 // All aliases are disabled, bring register into working set.
430 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000431 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000432}
433
Matthias Braun864cf582017-09-09 00:52:46 +0000434/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
435/// similar to defineVirtReg except the physreg is reserved instead of
436/// allocated.
437void RegAllocFast::definePhysReg(MachineInstr &MI, MCPhysReg PhysReg,
438 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000439 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000440 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
441 case regDisabled:
442 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000443 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000444 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000445 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000446 case regFree:
447 case regReserved:
448 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000449 return;
450 }
451
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000452 // This is a disabled register, disable all aliases.
453 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000454 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000455 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000456 switch (unsigned VirtReg = PhysRegState[Alias]) {
457 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000458 break;
459 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000460 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000461 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000462 case regFree:
463 case regReserved:
464 PhysRegState[Alias] = regDisabled;
465 if (TRI->isSuperRegister(PhysReg, Alias))
466 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000467 break;
468 }
469 }
470}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000471
Matthias Braun864cf582017-09-09 00:52:46 +0000472/// \brief Return the cost of spilling clearing out PhysReg and aliases so it is
473/// free for allocation. Returns 0 when PhysReg is free or disabled with all
474/// aliases disabled - it can be allocated directly.
475/// \returns spillImpossible when PhysReg or an alias can't be spilled.
476unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000477 if (isRegUsedInInstr(PhysReg)) {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000478 DEBUG(dbgs() << printReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000479 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000480 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000481 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
482 case regDisabled:
483 break;
484 case regFree:
485 return 0;
486 case regReserved:
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000487 DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
488 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000489 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000490 default: {
491 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
492 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
493 return I->Dirty ? spillDirty : spillClean;
494 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000495 }
496
Eric Christopherc3783362011-04-12 00:48:08 +0000497 // This is a disabled register, add up cost of aliases.
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000498 DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000499 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000500 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000501 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000502 switch (unsigned VirtReg = PhysRegState[Alias]) {
503 case regDisabled:
504 break;
505 case regFree:
506 ++Cost;
507 break;
508 case regReserved:
509 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000510 default: {
511 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
512 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
513 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000514 break;
515 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000516 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000517 }
518 return Cost;
519}
520
Matthias Braun864cf582017-09-09 00:52:46 +0000521/// \brief This method updates local state so that we know that PhysReg is the
522/// proper container for VirtReg now. The physical register must not be used
523/// for anything else when this is called.
524void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000525 DEBUG(dbgs() << "Assigning " << printReg(LR.VirtReg, TRI) << " to "
526 << printReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000527 PhysRegState[PhysReg] = LR.VirtReg;
528 assert(!LR.PhysReg && "Already assigned a physreg");
529 LR.PhysReg = PhysReg;
530}
531
Matthias Braun864cf582017-09-09 00:52:46 +0000532RegAllocFast::LiveRegMap::iterator
533RegAllocFast::assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000534 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
535 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
536 assignVirtToPhysReg(*LRI, PhysReg);
537 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000538}
539
Matthias Braun864cf582017-09-09 00:52:46 +0000540/// Allocates a physical register for VirtReg.
541RegAllocFast::LiveRegMap::iterator RegAllocFast::allocVirtReg(MachineInstr &MI,
542 LiveRegMap::iterator LRI, unsigned Hint) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000543 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000544
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000545 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
546 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000547
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000548 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000549 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
550 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
551 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000552 // Ignore the hint if we would have to spill a dirty register.
553 unsigned Cost = calcSpillCost(Hint);
554 if (Cost < spillDirty) {
555 if (Cost)
556 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000557 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
558 // That invalidates LRI, so run a new lookup for VirtReg.
559 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000560 }
561 }
562
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000563 // First try to find a completely free register.
Matthias Braun864cf582017-09-09 00:52:46 +0000564 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(&RC);
565 for (MCPhysReg PhysReg : AO) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000566 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000567 assignVirtToPhysReg(*LRI, PhysReg);
568 return LRI;
569 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000570 }
571
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000572 DEBUG(dbgs() << "Allocating " << printReg(VirtReg) << " from "
Matthias Braun864cf582017-09-09 00:52:46 +0000573 << TRI->getRegClassName(&RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000574
Matthias Braun864cf582017-09-09 00:52:46 +0000575 unsigned BestReg = 0;
576 unsigned BestCost = spillImpossible;
577 for (MCPhysReg PhysReg : AO) {
578 unsigned Cost = calcSpillCost(PhysReg);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000579 DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000580 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
581 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000582 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000583 if (Cost == 0) {
Matthias Braun864cf582017-09-09 00:52:46 +0000584 assignVirtToPhysReg(*LRI, PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000585 return LRI;
586 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000587 if (Cost < BestCost)
Matthias Braun864cf582017-09-09 00:52:46 +0000588 BestReg = PhysReg, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000589 }
590
591 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000592 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000593 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
594 // That invalidates LRI, so run a new lookup for VirtReg.
595 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000596 }
597
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000598 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000599 if (MI.isInlineAsm())
600 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000601 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000602 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000603 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000604 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000605}
606
Matthias Braun864cf582017-09-09 00:52:46 +0000607/// Allocates a register for VirtReg and mark it as dirty.
608RegAllocFast::LiveRegMap::iterator RegAllocFast::defineVirtReg(MachineInstr &MI,
609 unsigned OpNum,
610 unsigned VirtReg,
611 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000612 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
613 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000614 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000615 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000616 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000617 if (New) {
618 // If there is no hint, peek at the only use of this register.
619 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
620 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000621 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000622 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000623 if (UseMI.isCopyLike())
624 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000625 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000626 LRI = allocVirtReg(MI, LRI, Hint);
627 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000628 // Redefining a live register - kill at the last use, unless it is this
629 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000630 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000631 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000632 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000633 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000634 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000635 LRI->LastOpNum = OpNum;
636 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000637 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000638 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000639}
640
Matthias Braun864cf582017-09-09 00:52:46 +0000641/// Make sure VirtReg is available in a physreg and return it.
642RegAllocFast::LiveRegMap::iterator RegAllocFast::reloadVirtReg(MachineInstr &MI,
643 unsigned OpNum,
644 unsigned VirtReg,
645 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000646 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
647 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000648 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000649 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000650 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000651 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000652 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000653 LRI = allocVirtReg(MI, LRI, Hint);
Matthias Braun864cf582017-09-09 00:52:46 +0000654 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000655 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000656 DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
657 << printReg(LRI->PhysReg, TRI) << "\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000658 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, &RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000659 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000660 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000661 if (isLastUseOfLocalReg(MO)) {
662 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000663 if (MO.isUse())
664 MO.setIsKill();
665 else
666 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000667 } else if (MO.isKill()) {
668 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
669 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000670 } else if (MO.isDead()) {
671 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
672 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000673 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000674 } else if (MO.isKill()) {
675 // We must remove kill flags from uses of reloaded registers because the
676 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000677 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000678 // This would cause a second reload of %x into a different register.
679 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
680 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000681 } else if (MO.isDead()) {
682 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
683 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000684 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000685 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000686 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000687 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000688 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000689 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000690}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000691
Matthias Braun864cf582017-09-09 00:52:46 +0000692/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
693/// may invalidate any operand pointers. Return true if the operand kills its
694/// register.
695bool RegAllocFast::setPhysReg(MachineInstr &MI, unsigned OpNum,
696 MCPhysReg PhysReg) {
697 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000698 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000699 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000700 MO.setReg(PhysReg);
Geoff Berry60c43102017-12-12 17:53:59 +0000701 MO.setIsRenamableIfNoExtraRegAllocReq();
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000702 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000703 }
704
705 // Handle subregister index.
706 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berry60c43102017-12-12 17:53:59 +0000707 MO.setIsRenamableIfNoExtraRegAllocReq();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000708 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000709
710 // A kill flag implies killing the full register. Add corresponding super
711 // register kill.
712 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000713 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000714 return true;
715 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000716
717 // A <def,read-undef> of a sub-register requires an implicit def of the full
718 // register.
719 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000720 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000721
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000722 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000723}
724
Matthias Braun864cf582017-09-09 00:52:46 +0000725// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000726// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000727void RegAllocFast::handleThroughOperands(MachineInstr &MI,
728 SmallVectorImpl<unsigned> &VirtDead) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000729 DEBUG(dbgs() << "Scanning for through registers:");
730 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000731 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000732 if (!MO.isReg()) continue;
733 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000734 if (!TargetRegisterInfo::isVirtualRegister(Reg))
735 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000736 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
737 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000738 if (ThroughRegs.insert(Reg).second)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000739 DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000740 }
741 }
742
743 // If any physreg defines collide with preallocated through registers,
744 // we must spill and reallocate.
745 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000746 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000747 if (!MO.isReg() || !MO.isDef()) continue;
748 unsigned Reg = MO.getReg();
749 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000750 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000751 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000752 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000753 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000754 }
755 }
756
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000757 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000758 DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000759 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
760 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000761 if (!MO.isReg()) continue;
762 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000763 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000764 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000765 if (!MO.isTied()) continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000766 DEBUG(dbgs() << "Operand " << I << "("<< MO << ") is tied to operand "
Matthias Braun6b2b88b2017-09-09 01:16:59 +0000767 << MI.findTiedOperandIdx(I) << ".\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000768 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
769 MCPhysReg PhysReg = LRI->PhysReg;
770 setPhysReg(MI, I, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000771 // Note: we don't update the def operand yet. That would cause the normal
772 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000773 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000774 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
775 // Reload the register, but don't assign to the operand just yet.
776 // That would confuse the later phys-def processing pass.
Matthias Braun864cf582017-09-09 00:52:46 +0000777 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000778 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000779 }
780 }
781
Rafael Espindola2021f382011-11-22 06:27:18 +0000782 DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000783 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
784 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000785 if (!MO.isReg()) continue;
786 unsigned Reg = MO.getReg();
787 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
788 if (!MO.isEarlyClobber())
789 continue;
790 // Note: defineVirtReg may invalidate MO.
Matthias Braun864cf582017-09-09 00:52:46 +0000791 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, 0);
792 MCPhysReg PhysReg = LRI->PhysReg;
793 if (setPhysReg(MI, I, PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000794 VirtDead.push_back(Reg);
795 }
796
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000797 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000798 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000799 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000800 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
801 unsigned Reg = MO.getReg();
802 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000803 DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000804 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000805 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000806 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000807
808 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000809 for (unsigned PartialDef : PartialDefs)
810 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000811}
812
Matthias Braun864cf582017-09-09 00:52:46 +0000813#ifndef NDEBUG
814void RegAllocFast::dumpState() {
815 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
816 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000817 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000818 switch(PhysRegState[Reg]) {
819 case regFree:
820 break;
821 case regReserved:
822 dbgs() << "*";
823 break;
824 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000825 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun864cf582017-09-09 00:52:46 +0000826 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
827 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
828 if (I->Dirty)
829 dbgs() << "*";
830 assert(I->PhysReg == Reg && "Bad inverse map");
831 break;
832 }
833 }
834 }
835 dbgs() << '\n';
836 // Check that LiveVirtRegs is the inverse.
837 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
838 e = LiveVirtRegs.end(); i != e; ++i) {
839 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
840 "Bad map key");
841 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
842 "Bad map value");
843 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
844 }
845}
846#endif
847
848void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
849 this->MBB = &MBB;
850 DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000851
852 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000853 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000854
Matthias Braun864cf582017-09-09 00:52:46 +0000855 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000856
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000857 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +0000858 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +0000859 if (MRI->isAllocatable(LI.PhysReg))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000860 definePhysReg(*MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000861
Matthias Brauna09d18d2017-09-09 00:52:45 +0000862 VirtDead.clear();
863 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000864
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000865 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +0000866 for (MachineInstr &MI : MBB) {
867 const MCInstrDesc &MCID = MI.getDesc();
868 DEBUG(
869 dbgs() << "\n>> " << MI << "Regs:";
870 dumpState()
871 );
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000872
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000873 // Debug values are not allowed to change codegen in any way.
Matthias Braun864cf582017-09-09 00:52:46 +0000874 if (MI.isDebugValue()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000875 MachineInstr *DebugMI = &MI;
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000876 MachineOperand &MO = DebugMI->getOperand(0);
877
878 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
879 // mostly constants and frame indices.
880 if (!MO.isReg())
881 continue;
882 unsigned Reg = MO.getReg();
883 if (!TargetRegisterInfo::isVirtualRegister(Reg))
884 continue;
885
886 // See if this virtual register has already been allocated to a physical
887 // register or spilled to a stack slot.
888 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
889 if (LRI != LiveVirtRegs.end())
890 setPhysReg(*DebugMI, 0, LRI->PhysReg);
891 else {
892 int SS = StackSlotForVirtReg[Reg];
893 if (SS != -1) {
894 // Modify DBG_VALUE now that the value is in a spill slot.
895 updateDbgValueForSpill(*DebugMI, SS);
896 DEBUG(dbgs() << "Modifying debug info due to spill:"
897 << "\t" << *DebugMI);
898 continue;
Devang Patel57e72372010-07-09 21:48:31 +0000899 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000900
901 // We can't allocate a physreg for a DebugValue, sorry!
902 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
903 MO.setReg(0);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000904 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000905
906 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
907 // that future spills of Reg will have DBG_VALUEs.
908 LiveDbgValueMap[Reg].push_back(DebugMI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000909 continue;
910 }
911
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000912 // If this is a copy, we may be able to coalesce.
Matthias Braun864cf582017-09-09 00:52:46 +0000913 unsigned CopySrcReg = 0;
914 unsigned CopyDstReg = 0;
915 unsigned CopySrcSub = 0;
916 unsigned CopyDstSub = 0;
917 if (MI.isCopy()) {
918 CopyDstReg = MI.getOperand(0).getReg();
919 CopySrcReg = MI.getOperand(1).getReg();
920 CopyDstSub = MI.getOperand(0).getSubReg();
921 CopySrcSub = MI.getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000922 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000923
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000924 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000925 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000926
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000927 // First scan.
928 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000929 // Find the end of the virtreg operands
930 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000931 bool hasTiedOps = false;
932 bool hasEarlyClobbers = false;
933 bool hasPartialRedefs = false;
934 bool hasPhysDefs = false;
Matthias Braun864cf582017-09-09 00:52:46 +0000935 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
936 MachineOperand &MO = MI.getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000937 // Make sure MRI knows about registers clobbered by regmasks.
938 if (MO.isRegMask()) {
939 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
940 continue;
941 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000942 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000943 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000944 if (!Reg) continue;
945 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
946 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000947 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000948 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000949 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000950 } else {
951 if (MO.isEarlyClobber())
952 hasEarlyClobbers = true;
Matthias Braun864cf582017-09-09 00:52:46 +0000953 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000954 hasPartialRedefs = true;
955 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000956 continue;
957 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000958 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000959 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000960 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000961 } else if (MO.isEarlyClobber()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000962 definePhysReg(MI, Reg,
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000963 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000964 hasEarlyClobbers = true;
965 } else
966 hasPhysDefs = true;
967 }
968
969 // The instruction may have virtual register operands that must be allocated
970 // the same register at use-time and def-time: early clobbers and tied
971 // operands. If there are also physical defs, these registers must avoid
972 // both physical defs and uses, making them more constrained than normal
973 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000974 // Similarly, if there are multiple defs and tied operands, we must make
975 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000976 // We didn't detect inline asm tied operands above, so just make this extra
977 // pass for all inline asm.
Matthias Braun864cf582017-09-09 00:52:46 +0000978 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000979 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000980 handleThroughOperands(MI, VirtDead);
981 // Don't attempt coalescing when we have funny stuff going on.
Matthias Braun864cf582017-09-09 00:52:46 +0000982 CopyDstReg = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000983 // Pretend we have early clobbers so the use operands get marked below.
984 // This is not necessary for the common case of a single tied use.
985 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000986 }
987
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000988 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000989 // Allocate virtreg uses.
Matthias Braun864cf582017-09-09 00:52:46 +0000990 for (unsigned I = 0; I != VirtOpEnd; ++I) {
991 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000992 if (!MO.isReg()) continue;
993 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000994 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000995 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000996 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, CopyDstReg);
997 MCPhysReg PhysReg = LRI->PhysReg;
998 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
999 if (setPhysReg(MI, I, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001000 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001001 }
1002 }
1003
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001004 // Track registers defined by instruction - early clobbers and tied uses at
1005 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001006 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001007 if (hasEarlyClobbers) {
Matthias Braun864cf582017-09-09 00:52:46 +00001008 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001009 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001010 unsigned Reg = MO.getReg();
1011 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001012 // Look for physreg defs and tied uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001013 if (!MO.isDef() && !MO.isTied()) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001014 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001015 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001016 }
1017
Matthias Braun864cf582017-09-09 00:52:46 +00001018 unsigned DefOpEnd = MI.getNumOperands();
1019 if (MI.isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001020 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001021 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001022 // registers in their spill slots.
1023 // Note: although this is appealing to just consider all definitions
1024 // as call-clobbered, this is not correct because some of those
1025 // definitions may be used later on and we do not want to reuse
1026 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001027 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1028 spillAll(MI);
1029 }
1030
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001031 // Third scan.
1032 // Allocate defs and collect dead defs.
Matthias Braun864cf582017-09-09 00:52:46 +00001033 for (unsigned I = 0; I != DefOpEnd; ++I) {
1034 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001035 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1036 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001037 unsigned Reg = MO.getReg();
1038
1039 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001040 if (!MRI->isAllocatable(Reg)) continue;
Matthias Braun864cf582017-09-09 00:52:46 +00001041 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001042 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001043 }
Matthias Braun864cf582017-09-09 00:52:46 +00001044 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, CopySrcReg);
1045 MCPhysReg PhysReg = LRI->PhysReg;
1046 if (setPhysReg(MI, I, PhysReg)) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001047 VirtDead.push_back(Reg);
Matthias Braun864cf582017-09-09 00:52:46 +00001048 CopyDstReg = 0; // cancel coalescing;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001049 } else
Matthias Braun864cf582017-09-09 00:52:46 +00001050 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001051 }
1052
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001053 // Kill dead defs after the scan to ensure that multiple defs of the same
1054 // register are allocated identically. We didn't need to do this for uses
1055 // because we are crerating our own kill flags, and they are always at the
1056 // last use.
Matthias Braun864cf582017-09-09 00:52:46 +00001057 for (unsigned VirtReg : VirtDead)
1058 killVirtReg(VirtReg);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001059 VirtDead.clear();
1060
Matthias Braun864cf582017-09-09 00:52:46 +00001061 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1062 DEBUG(dbgs() << "-- coalescing: " << MI);
1063 Coalesced.push_back(&MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001064 } else {
Matthias Braun864cf582017-09-09 00:52:46 +00001065 DEBUG(dbgs() << "<< " << MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001066 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001067 }
1068
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001069 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001070 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001071 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001072
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001073 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001074 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001075 for (MachineInstr *MI : Coalesced)
1076 MBB.erase(MI);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001077 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001078
Matthias Braun864cf582017-09-09 00:52:46 +00001079 DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001080}
1081
Matthias Braun864cf582017-09-09 00:52:46 +00001082/// Allocates registers for a function.
1083bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001084 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
Matthias Braun864cf582017-09-09 00:52:46 +00001085 << "********** Function: " << MF.getName() << '\n');
1086 MRI = &MF.getRegInfo();
1087 const TargetSubtargetInfo &STI = MF.getSubtarget();
1088 TRI = STI.getRegisterInfo();
1089 TII = STI.getInstrInfo();
1090 MFI = &MF.getFrameInfo();
1091 MRI->freezeReservedRegs(MF);
1092 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001093 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001094 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001095
1096 // initialize the virtual->physical register map to have a 'null'
1097 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001098 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1099 StackSlotForVirtReg.resize(NumVirtRegs);
1100 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001101
1102 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001103 for (MachineBasicBlock &MBB : MF)
1104 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001105
Andrew Trickda84e642012-02-21 04:51:23 +00001106 // All machine operands and other references to virtual registers have been
1107 // replaced. Remove the virtual registers.
1108 MRI->clearVirtRegs();
1109
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001110 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001111 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001112 return true;
1113}
1114
1115FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001116 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001117}