blob: a711db1c0eb03013922d97cd97de26b47099845f [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00006//
7//===----------------------------------------------------------------------===//
8//
Matthias Braun864cf582017-09-09 00:52:46 +00009/// \file This register allocator allocates registers to a basic block at a
10/// time, attempting to keep values in registers and reusing registers as
11/// appropriate.
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000012//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko618c5552017-09-13 21:15:20 +000015#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/IndexedMap.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000020#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000021#include "llvm/ADT/Statistic.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000024#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000028#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/CodeGen/RegAllocRegistry.h"
31#include "llvm/CodeGen/RegisterClassInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000032#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000033#include "llvm/CodeGen/TargetOpcodes.h"
34#include "llvm/CodeGen/TargetRegisterInfo.h"
35#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000036#include "llvm/IR/DebugLoc.h"
37#include "llvm/IR/Metadata.h"
38#include "llvm/MC/MCInstrDesc.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Pass.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000046#include <cassert>
47#include <tuple>
48#include <vector>
49
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000050using namespace llvm;
51
Chandler Carruth1b9dde02014-04-22 02:02:50 +000052#define DEBUG_TYPE "regalloc"
53
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000054STATISTIC(NumStores, "Number of stores added");
55STATISTIC(NumLoads , "Number of loads added");
Matthias Braun14af82a2018-11-07 02:04:07 +000056STATISTIC(NumCoalesced, "Number of copies coalesced");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000057
58static RegisterRegAlloc
59 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
60
61namespace {
Eugene Zelenko618c5552017-09-13 21:15:20 +000062
Matthias Braun864cf582017-09-09 00:52:46 +000063 class RegAllocFast : public MachineFunctionPass {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000064 public:
65 static char ID;
Eugene Zelenko618c5552017-09-13 21:15:20 +000066
Matthias Braun864cf582017-09-09 00:52:46 +000067 RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
Derek Schuffad154c82016-03-28 17:05:30 +000068
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000069 private:
Matthias Braun864cf582017-09-09 00:52:46 +000070 MachineFrameInfo *MFI;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +000071 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000072 const TargetRegisterInfo *TRI;
73 const TargetInstrInfo *TII;
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +000074 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000075
Matthias Braun864cf582017-09-09 00:52:46 +000076 /// Basic block currently being allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +000077 MachineBasicBlock *MBB;
78
Matthias Braun864cf582017-09-09 00:52:46 +000079 /// Maps virtual regs to the frame index where these values are spilled.
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000080 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
81
Matthias Braun864cf582017-09-09 00:52:46 +000082 /// Everything we know about a live virtual register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000083 struct LiveReg {
Eugene Zelenko618c5552017-09-13 21:15:20 +000084 MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
85 unsigned VirtReg; ///< Virtual register number.
86 MCPhysReg PhysReg = 0; ///< Currently held here.
87 unsigned short LastOpNum = 0; ///< OpNum on LastUse.
88 bool Dirty = false; ///< Register needs spill.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000089
Matthias Braunebcf5432018-11-07 02:04:11 +000090 explicit LiveReg(unsigned VirtReg) : VirtReg(VirtReg) {}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000091
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000092 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000093 return TargetRegisterInfo::virtReg2Index(VirtReg);
94 }
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000095 };
96
Eugene Zelenko618c5552017-09-13 21:15:20 +000097 using LiveRegMap = SparseSet<LiveReg>;
Matthias Braun864cf582017-09-09 00:52:46 +000098 /// This map contains entries for each virtual register that is currently
99 /// available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000100 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000101
Matthias Braunebcf5432018-11-07 02:04:11 +0000102 DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +0000103
Matthias Braunebcf5432018-11-07 02:04:11 +0000104 /// State of a physical register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000105 enum RegState {
Matthias Braun864cf582017-09-09 00:52:46 +0000106 /// A disabled register is not available for allocation, but an alias may
107 /// be in use. A register can only be moved out of the disabled state if
108 /// all aliases are disabled.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000109 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000110
Matthias Braun864cf582017-09-09 00:52:46 +0000111 /// A free register is not currently in use and can be allocated
112 /// immediately without checking aliases.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000113 regFree,
114
Matthias Braun864cf582017-09-09 00:52:46 +0000115 /// A reserved register has been assigned explicitly (e.g., setting up a
116 /// call parameter), and it remains reserved until it is used.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000117 regReserved
118
Matthias Braun864cf582017-09-09 00:52:46 +0000119 /// A register state may also be a virtual register number, indication
120 /// that the physical register is currently allocated to a virtual
121 /// register. In that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000122 };
123
Matthias Braunebcf5432018-11-07 02:04:11 +0000124 /// Maps each physical register to a RegState enum or a virtual register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000125 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000126
Matthias Brauna09d18d2017-09-09 00:52:45 +0000127 SmallVector<unsigned, 16> VirtDead;
Eugene Zelenko618c5552017-09-13 21:15:20 +0000128 SmallVector<MachineInstr *, 32> Coalesced;
Matthias Brauna09d18d2017-09-09 00:52:45 +0000129
Matthias Braunebcf5432018-11-07 02:04:11 +0000130 using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
Matthias Braun864cf582017-09-09 00:52:46 +0000131 /// Set of register units that are used in the current instruction, and so
132 /// cannot be allocated.
Matthias Braunebcf5432018-11-07 02:04:11 +0000133 RegUnitSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000134
Matthias Braun0804dca2018-11-07 06:57:00 +0000135 void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
136
Matthias Braun864cf582017-09-09 00:52:46 +0000137 /// Mark a physreg as used in this instruction.
138 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000139 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
140 UsedInInstr.insert(*Units);
141 }
142
Matthias Braun864cf582017-09-09 00:52:46 +0000143 /// Check if a physreg or any of its aliases are used in this instruction.
144 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000145 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
146 if (UsedInInstr.count(*Units))
147 return true;
148 return false;
149 }
150
Alp Toker61007d82014-03-02 03:20:38 +0000151 enum : unsigned {
Matthias Braunebcf5432018-11-07 02:04:11 +0000152 spillClean = 50,
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000153 spillDirty = 100,
154 spillImpossible = ~0u
155 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000156
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000157 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000158 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000159
Craig Topper4584cd52014-03-07 09:26:03 +0000160 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000161 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000162 MachineFunctionPass::getAnalysisUsage(AU);
163 }
164
Matthias Braun90799ce2016-08-23 21:19:49 +0000165 MachineFunctionProperties getRequiredProperties() const override {
166 return MachineFunctionProperties().set(
167 MachineFunctionProperties::Property::NoPHIs);
168 }
169
Derek Schuffad154c82016-03-28 17:05:30 +0000170 MachineFunctionProperties getSetProperties() const override {
171 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000172 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000173 }
174
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000175 private:
Fangrui Songcb0bab82018-07-16 18:51:40 +0000176 bool runOnMachineFunction(MachineFunction &MF) override;
Matthias Braunebcf5432018-11-07 02:04:11 +0000177
Matthias Braun864cf582017-09-09 00:52:46 +0000178 void allocateBasicBlock(MachineBasicBlock &MBB);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000179 void allocateInstruction(MachineInstr &MI);
180 void handleDebugValue(MachineInstr &MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000181 void handleThroughOperands(MachineInstr &MI,
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000182 SmallVectorImpl<unsigned> &VirtDead);
Matthias Braun864cf582017-09-09 00:52:46 +0000183 bool isLastUseOfLocalReg(const MachineOperand &MO) const;
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000184
Matthias Braun864cf582017-09-09 00:52:46 +0000185 void addKillFlag(const LiveReg &LRI);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000186 void killVirtReg(LiveReg &LR);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000187 void killVirtReg(unsigned VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000188 void spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000189 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000190
Matthias Braun864cf582017-09-09 00:52:46 +0000191 void usePhysReg(MachineOperand &MO);
Quentin Colombet72f6d592018-01-29 23:42:37 +0000192 void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
193 RegState NewState);
Matthias Braun864cf582017-09-09 00:52:46 +0000194 unsigned calcSpillCost(MCPhysReg PhysReg) const;
Quentin Colombet72f6d592018-01-29 23:42:37 +0000195 void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000196
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000197 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
198 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
199 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000200
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000201 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
202 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
203 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000204
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000205 void allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint);
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000206 void allocVirtRegUndef(MachineOperand &MO);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000207 MCPhysReg defineVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
208 unsigned Hint);
209 LiveReg &reloadVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
210 unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000211 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000212 bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000213
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000214 int getStackSpaceFor(unsigned VirtReg);
215 void spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
216 MCPhysReg AssignedReg, bool Kill);
217 void reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
218 MCPhysReg PhysReg);
219
Matthias Braun864cf582017-09-09 00:52:46 +0000220 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000221 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000222
223} // end anonymous namespace
224
225char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000226
Matthias Braun864cf582017-09-09 00:52:46 +0000227INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
228 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000229
Matthias Braun0804dca2018-11-07 06:57:00 +0000230void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
231 PhysRegState[PhysReg] = NewState;
232}
233
Matthias Braun864cf582017-09-09 00:52:46 +0000234/// This allocates space for the specified virtual register to be held on the
235/// stack.
Matthias Braunebcf5432018-11-07 02:04:11 +0000236int RegAllocFast::getStackSpaceFor(unsigned VirtReg) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000237 // Find the location Reg would belong...
238 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000239 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000240 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000241 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000242
243 // Allocate a new stack object for this spill location...
Matthias Braunebcf5432018-11-07 02:04:11 +0000244 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000245 unsigned Size = TRI->getSpillSize(RC);
246 unsigned Align = TRI->getSpillAlignment(RC);
247 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000248
249 // Assign the slot.
250 StackSlotForVirtReg[VirtReg] = FrameIdx;
251 return FrameIdx;
252}
253
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000254/// Insert spill instruction for \p AssignedReg before \p Before. Update
255/// DBG_VALUEs with \p VirtReg operands with the stack slot.
256void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
257 MCPhysReg AssignedReg, bool Kill) {
258 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
259 << " in " << printReg(AssignedReg, TRI));
260 int FI = getStackSpaceFor(VirtReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000261 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000262
263 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
264 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
265 ++NumStores;
266
267 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
268 // identify spilled location as the place to find corresponding variable's
269 // value.
270 SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
271 for (MachineInstr *DBG : LRIDbgValues) {
272 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
273 assert(NewDV->getParent() == MBB && "dangling parent pointer");
274 (void)NewDV;
275 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
276 }
277 // Now this register is spilled there is should not be any DBG_VALUE
278 // pointing to this register because they are all pointing to spilled value
279 // now.
280 LRIDbgValues.clear();
281}
282
283/// Insert reload instruction for \p PhysReg before \p Before.
284void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
285 MCPhysReg PhysReg) {
286 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000287 << printReg(PhysReg, TRI) << '\n');
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000288 int FI = getStackSpaceFor(VirtReg);
289 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
290 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
291 ++NumLoads;
292}
293
Matthias Braun864cf582017-09-09 00:52:46 +0000294/// Return true if MO is the only remaining reference to its virtual register,
295/// and it is guaranteed to be a block-local register.
296bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000297 // If the register has ever been spilled or reloaded, we conservatively assume
298 // it is a global register used in multiple blocks.
299 if (StackSlotForVirtReg[MO.getReg()] != -1)
300 return false;
301
302 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000303 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000304 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000305 return false;
306 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000307}
308
Matthias Braun864cf582017-09-09 00:52:46 +0000309/// Set kill flags on last use of a virtual register.
310void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000311 if (!LR.LastUse) return;
312 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000313 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
314 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000315 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000316 // else, don't do anything we are problably redefining a
317 // subreg of this register and given we don't track which
318 // lanes are actually dead, we cannot insert a kill flag here.
319 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000320 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000321 // ... <== Here we would allow later pass to reuse physreg:sub1
322 // which is potentially wrong.
323 // LR:sub0 = ...
324 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000325 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000326}
327
Matthias Braun864cf582017-09-09 00:52:46 +0000328/// Mark virtreg as no longer available.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000329void RegAllocFast::killVirtReg(LiveReg &LR) {
330 addKillFlag(LR);
331 assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000332 "Broken RegState mapping");
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000333 setPhysRegState(LR.PhysReg, regFree);
334 LR.PhysReg = 0;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000335}
336
Matthias Braun864cf582017-09-09 00:52:46 +0000337/// Mark virtreg as no longer available.
338void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000339 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
340 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000341 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000342 if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
343 killVirtReg(*LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000344}
345
Matthias Braun864cf582017-09-09 00:52:46 +0000346/// This method spills the value specified by VirtReg into the corresponding
347/// stack slot if needed.
348void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
349 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000350 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
351 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000352 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000353 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
354 "Spilling unmapped virtual register");
355 spillVirtReg(MI, *LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000356}
357
Matthias Braun864cf582017-09-09 00:52:46 +0000358/// Do the actual work of spilling.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000359void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR) {
360 assert(PhysRegState[LR.PhysReg] == LR.VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000361
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000362 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000363 // If this physreg is used by the instruction, we want to kill it on the
364 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000365 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000366 LR.Dirty = false;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000367
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000368 spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000369
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000370 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000371 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000372 }
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000373 killVirtReg(LR);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000374}
375
Matthias Braun864cf582017-09-09 00:52:46 +0000376/// Spill all dirty virtregs without killing them.
377void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000378 if (LiveVirtRegs.empty())
379 return;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000380 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
381 // of spilling here is deterministic, if arbitrary.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000382 for (LiveReg &LR : LiveVirtRegs) {
383 if (!LR.PhysReg)
384 continue;
385 spillVirtReg(MI, LR);
386 }
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000387 LiveVirtRegs.clear();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000388}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000389
Matthias Braun864cf582017-09-09 00:52:46 +0000390/// Handle the direct use of a physical register. Check that the register is
391/// not used by a virtreg. Kill the physreg, marking it free. This may add
392/// implicit kills to MO->getParent() and invalidate MO.
393void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000394 // Ignore undef uses.
395 if (MO.isUndef())
396 return;
397
Matthias Braun864cf582017-09-09 00:52:46 +0000398 unsigned PhysReg = MO.getReg();
399 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
400 "Bad usePhysReg operand");
401
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000402 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000403 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000404 case regDisabled:
405 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000406 case regReserved:
407 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000408 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000409 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000410 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000411 return;
412 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000413 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000414 // wanted has been clobbered.
415 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000416 }
417
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000418 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000419 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000420 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000421 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000422 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000423 break;
424 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000425 // Either PhysReg is a subregister of Alias and we mark the
426 // whole register as free, or PhysReg is the superregister of
427 // Alias and we mark all the aliases as disabled before freeing
428 // PhysReg.
429 // In the latter case, since PhysReg was disabled, this means that
430 // its value is defined only by physical sub-registers. This check
431 // is performed by the assert of the default case in this loop.
432 // Note: The value of the superregister may only be partial
433 // defined, that is why regDisabled is a valid state for aliases.
434 assert((TRI->isSuperRegister(PhysReg, Alias) ||
435 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000436 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000437 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000438 case regFree:
439 if (TRI->isSuperRegister(PhysReg, Alias)) {
440 // Leave the superregister in the working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000441 setPhysRegState(Alias, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000442 MO.getParent()->addRegisterKilled(Alias, TRI, true);
443 return;
444 }
445 // Some other alias was in the working set - clear it.
Matthias Braun0804dca2018-11-07 06:57:00 +0000446 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000447 break;
448 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000449 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000450 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000451 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000452
453 // All aliases are disabled, bring register into working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000454 setPhysRegState(PhysReg, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000455 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000456}
457
Matthias Braun864cf582017-09-09 00:52:46 +0000458/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
459/// similar to defineVirtReg except the physreg is reserved instead of
460/// allocated.
Quentin Colombet72f6d592018-01-29 23:42:37 +0000461void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
462 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000463 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000464 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
465 case regDisabled:
466 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000467 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000468 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000469 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000470 case regFree:
471 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000472 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000473 return;
474 }
475
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000476 // This is a disabled register, disable all aliases.
Matthias Braun0804dca2018-11-07 06:57:00 +0000477 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000478 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000479 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000480 switch (unsigned VirtReg = PhysRegState[Alias]) {
481 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000482 break;
483 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000484 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000485 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000486 case regFree:
487 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000488 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000489 if (TRI->isSuperRegister(PhysReg, Alias))
490 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000491 break;
492 }
493 }
494}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000495
Matthias Braunfb93aec2018-11-10 00:36:27 +0000496/// Return the cost of spilling clearing out PhysReg and aliases so it is free
497/// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
498/// disabled - it can be allocated directly.
Matthias Braun864cf582017-09-09 00:52:46 +0000499/// \returns spillImpossible when PhysReg or an alias can't be spilled.
500unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000501 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000502 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
503 << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000504 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000505 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000506 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
507 case regDisabled:
508 break;
509 case regFree:
510 return 0;
511 case regReserved:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000512 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
513 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000514 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000515 default: {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000516 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
517 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
518 "Missing VirtReg entry");
519 return LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000520 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000521 }
522
Eric Christopherc3783362011-04-12 00:48:08 +0000523 // This is a disabled register, add up cost of aliases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000524 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000525 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000526 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000527 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000528 switch (unsigned VirtReg = PhysRegState[Alias]) {
529 case regDisabled:
530 break;
531 case regFree:
532 ++Cost;
533 break;
534 case regReserved:
535 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000536 default: {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000537 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
538 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
539 "Missing VirtReg entry");
540 Cost += LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000541 break;
542 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000543 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000544 }
545 return Cost;
546}
547
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000548/// This method updates local state so that we know that PhysReg is the
Matthias Braun864cf582017-09-09 00:52:46 +0000549/// proper container for VirtReg now. The physical register must not be used
550/// for anything else when this is called.
551void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Matthias Braun0804dca2018-11-07 06:57:00 +0000552 unsigned VirtReg = LR.VirtReg;
553 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000554 << printReg(PhysReg, TRI) << '\n');
Matthias Braun0804dca2018-11-07 06:57:00 +0000555 assert(LR.PhysReg == 0 && "Already assigned a physreg");
556 assert(PhysReg != 0 && "Trying to assign no register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000557 LR.PhysReg = PhysReg;
Matthias Braun0804dca2018-11-07 06:57:00 +0000558 setPhysRegState(PhysReg, VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000559}
560
Matthias Braun864cf582017-09-09 00:52:46 +0000561/// Allocates a physical register for VirtReg.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000562void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint) {
563 const unsigned VirtReg = LR.VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000564
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000565 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
566 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000567
Matthias Braun864cf582017-09-09 00:52:46 +0000568 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000569 LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
Matt Arsenault884a18d2019-03-17 21:31:40 +0000570 << " in class " << TRI->getRegClassName(&RC)
571 << " with hint " << printReg(Hint, TRI) << '\n');
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000572
573 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000574 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
575 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000576 // Ignore the hint if we would have to spill a dirty register.
577 unsigned Cost = calcSpillCost(Hint);
578 if (Cost < spillDirty) {
579 if (Cost)
580 definePhysReg(MI, Hint, regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000581 assignVirtToPhysReg(LR, Hint);
582 return;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000583 }
584 }
585
Matthias Braunfb93aec2018-11-10 00:36:27 +0000586 MCPhysReg BestReg = 0;
Matthias Braun864cf582017-09-09 00:52:46 +0000587 unsigned BestCost = spillImpossible;
Matt Arsenaultc2e35a62019-03-19 19:01:34 +0000588 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000589 for (MCPhysReg PhysReg : AllocationOrder) {
590 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
Matthias Braun864cf582017-09-09 00:52:46 +0000591 unsigned Cost = calcSpillCost(PhysReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000592 LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
Matthias Braunfb93aec2018-11-10 00:36:27 +0000593 // Immediate take a register with cost 0.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000594 if (Cost == 0) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000595 assignVirtToPhysReg(LR, PhysReg);
596 return;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000597 }
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000598 if (Cost < BestCost) {
599 BestReg = PhysReg;
600 BestCost = Cost;
601 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000602 }
603
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000604 if (!BestReg) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000605 // Nothing we can do: Report an error and keep going with an invalid
606 // allocation.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000607 if (MI.isInlineAsm())
608 MI.emitError("inline assembly requires more registers than available");
609 else
610 MI.emitError("ran out of registers during register allocation");
611 definePhysReg(MI, *AllocationOrder.begin(), regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000612 assignVirtToPhysReg(LR, *AllocationOrder.begin());
613 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000614 }
615
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000616 definePhysReg(MI, BestReg, regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000617 assignVirtToPhysReg(LR, BestReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000618}
619
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000620void RegAllocFast::allocVirtRegUndef(MachineOperand &MO) {
621 assert(MO.isUndef() && "expected undef use");
622 unsigned VirtReg = MO.getReg();
623 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Expected virtreg");
624
625 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
626 MCPhysReg PhysReg;
627 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
628 PhysReg = LRI->PhysReg;
629 } else {
630 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
631 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
632 assert(!AllocationOrder.empty() && "Allocation order must not be empty");
633 PhysReg = AllocationOrder[0];
634 }
635
636 unsigned SubRegIdx = MO.getSubReg();
637 if (SubRegIdx != 0) {
638 PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
639 MO.setSubReg(0);
640 }
641 MO.setReg(PhysReg);
642 MO.setIsRenamable(true);
643}
644
Matthias Braun864cf582017-09-09 00:52:46 +0000645/// Allocates a register for VirtReg and mark it as dirty.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000646MCPhysReg RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
647 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000648 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
649 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000650 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000651 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000652 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000653 if (!LRI->PhysReg) {
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000654 // If there is no hint, peek at the only use of this register.
655 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
656 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000657 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000658 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000659 if (UseMI.isCopyLike())
660 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000661 }
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000662 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000663 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000664 // Redefining a live register - kill at the last use, unless it is this
665 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000666 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000667 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000668 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000669 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000670 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000671 LRI->LastOpNum = OpNum;
672 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000673 markRegUsedInInstr(LRI->PhysReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000674 return LRI->PhysReg;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000675}
676
Matthias Braun864cf582017-09-09 00:52:46 +0000677/// Make sure VirtReg is available in a physreg and return it.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000678RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(MachineInstr &MI,
679 unsigned OpNum,
680 unsigned VirtReg,
681 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000682 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
683 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000684 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000685 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000686 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000687 MachineOperand &MO = MI.getOperand(OpNum);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000688 if (!LRI->PhysReg) {
689 allocVirtReg(MI, *LRI, Hint);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000690 reload(MI, VirtReg, LRI->PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000691 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000692 if (isLastUseOfLocalReg(MO)) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000693 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000694 if (MO.isUse())
695 MO.setIsKill();
696 else
697 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000698 } else if (MO.isKill()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000699 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << '\n');
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000700 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000701 } else if (MO.isDead()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000702 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000703 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000704 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000705 } else if (MO.isKill()) {
706 // We must remove kill flags from uses of reloaded registers because the
707 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000708 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000709 // This would cause a second reload of %x into a different register.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000710 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << '\n');
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000711 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000712 } else if (MO.isDead()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000713 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000714 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000715 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000716 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000717 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000718 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000719 markRegUsedInInstr(LRI->PhysReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000720 return *LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000721}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000722
Matthias Braun864cf582017-09-09 00:52:46 +0000723/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
724/// may invalidate any operand pointers. Return true if the operand kills its
725/// register.
Matthias Braunfb93aec2018-11-10 00:36:27 +0000726bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
Matthias Braun864cf582017-09-09 00:52:46 +0000727 MCPhysReg PhysReg) {
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000728 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000729 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000730 MO.setReg(PhysReg);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000731 MO.setIsRenamable(true);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000732 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000733 }
734
735 // Handle subregister index.
736 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000737 MO.setIsRenamable(true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000738 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000739
740 // A kill flag implies killing the full register. Add corresponding super
741 // register kill.
742 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000743 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000744 return true;
745 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000746
747 // A <def,read-undef> of a sub-register requires an implicit def of the full
748 // register.
749 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000750 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000751
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000752 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000753}
754
Matthias Braun864cf582017-09-09 00:52:46 +0000755// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000756// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000757void RegAllocFast::handleThroughOperands(MachineInstr &MI,
758 SmallVectorImpl<unsigned> &VirtDead) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000759 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000760 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000761 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000762 if (!MO.isReg()) continue;
763 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000764 if (!TargetRegisterInfo::isVirtualRegister(Reg))
765 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000766 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
767 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000768 if (ThroughRegs.insert(Reg).second)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000769 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000770 }
771 }
772
773 // If any physreg defines collide with preallocated through registers,
774 // we must spill and reallocate.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000775 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000776 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000777 if (!MO.isReg() || !MO.isDef()) continue;
778 unsigned Reg = MO.getReg();
779 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000780 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000781 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000782 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000783 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000784 }
785 }
786
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000787 SmallVector<unsigned, 8> PartialDefs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000788 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000789 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000790 MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000791 if (!MO.isReg()) continue;
792 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000793 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000794 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000795 if (!MO.isTied()) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000796 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
797 << ") is tied to operand " << MI.findTiedOperandIdx(I)
798 << ".\n");
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000799 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
800 MCPhysReg PhysReg = LR.PhysReg;
Matthias Braunfb93aec2018-11-10 00:36:27 +0000801 setPhysReg(MI, MO, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000802 // Note: we don't update the def operand yet. That would cause the normal
803 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000804 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000805 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000806 // Reload the register, but don't assign to the operand just yet.
807 // That would confuse the later phys-def processing pass.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000808 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
809 PartialDefs.push_back(LR.PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000810 }
811 }
812
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000813 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000814 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
815 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000816 if (!MO.isReg()) continue;
817 unsigned Reg = MO.getReg();
818 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
819 if (!MO.isEarlyClobber())
820 continue;
821 // Note: defineVirtReg may invalidate MO.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000822 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, 0);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000823 if (setPhysReg(MI, MI.getOperand(I), PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000824 VirtDead.push_back(Reg);
825 }
826
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000827 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000828 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000829 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000830 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
831 unsigned Reg = MO.getReg();
832 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000833 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
834 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000835 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000836 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000837
838 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000839 for (unsigned PartialDef : PartialDefs)
840 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000841}
842
Matthias Braun864cf582017-09-09 00:52:46 +0000843#ifndef NDEBUG
844void RegAllocFast::dumpState() {
845 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
846 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000847 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000848 switch(PhysRegState[Reg]) {
849 case regFree:
850 break;
851 case regReserved:
852 dbgs() << "*";
853 break;
854 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000855 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000856 LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
857 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
858 "Missing VirtReg entry");
859 if (LRI->Dirty)
Matthias Braun864cf582017-09-09 00:52:46 +0000860 dbgs() << "*";
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000861 assert(LRI->PhysReg == Reg && "Bad inverse map");
Matthias Braun864cf582017-09-09 00:52:46 +0000862 break;
863 }
864 }
865 }
866 dbgs() << '\n';
867 // Check that LiveVirtRegs is the inverse.
868 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
869 e = LiveVirtRegs.end(); i != e; ++i) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000870 if (!i->PhysReg)
871 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000872 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
873 "Bad map key");
874 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
875 "Bad map value");
876 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
877 }
878}
879#endif
880
Matthias Braunfb93aec2018-11-10 00:36:27 +0000881void RegAllocFast::allocateInstruction(MachineInstr &MI) {
882 const MCInstrDesc &MCID = MI.getDesc();
883
884 // If this is a copy, we may be able to coalesce.
885 unsigned CopySrcReg = 0;
886 unsigned CopyDstReg = 0;
887 unsigned CopySrcSub = 0;
888 unsigned CopyDstSub = 0;
889 if (MI.isCopy()) {
890 CopyDstReg = MI.getOperand(0).getReg();
891 CopySrcReg = MI.getOperand(1).getReg();
892 CopyDstSub = MI.getOperand(0).getSubReg();
893 CopySrcSub = MI.getOperand(1).getSubReg();
894 }
895
896 // Track registers used by instruction.
897 UsedInInstr.clear();
898
899 // First scan.
900 // Mark physreg uses and early clobbers as used.
901 // Find the end of the virtreg operands
902 unsigned VirtOpEnd = 0;
903 bool hasTiedOps = false;
904 bool hasEarlyClobbers = false;
905 bool hasPartialRedefs = false;
906 bool hasPhysDefs = false;
907 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
908 MachineOperand &MO = MI.getOperand(i);
909 // Make sure MRI knows about registers clobbered by regmasks.
910 if (MO.isRegMask()) {
911 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
912 continue;
913 }
914 if (!MO.isReg()) continue;
915 unsigned Reg = MO.getReg();
916 if (!Reg) continue;
917 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
918 VirtOpEnd = i+1;
919 if (MO.isUse()) {
920 hasTiedOps = hasTiedOps ||
921 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
922 } else {
923 if (MO.isEarlyClobber())
924 hasEarlyClobbers = true;
925 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
926 hasPartialRedefs = true;
927 }
928 continue;
929 }
930 if (!MRI->isAllocatable(Reg)) continue;
931 if (MO.isUse()) {
932 usePhysReg(MO);
933 } else if (MO.isEarlyClobber()) {
934 definePhysReg(MI, Reg,
935 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
936 hasEarlyClobbers = true;
937 } else
938 hasPhysDefs = true;
939 }
940
941 // The instruction may have virtual register operands that must be allocated
942 // the same register at use-time and def-time: early clobbers and tied
943 // operands. If there are also physical defs, these registers must avoid
944 // both physical defs and uses, making them more constrained than normal
945 // operands.
946 // Similarly, if there are multiple defs and tied operands, we must make
947 // sure the same register is allocated to uses and defs.
948 // We didn't detect inline asm tied operands above, so just make this extra
949 // pass for all inline asm.
950 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
951 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
952 handleThroughOperands(MI, VirtDead);
953 // Don't attempt coalescing when we have funny stuff going on.
954 CopyDstReg = 0;
955 // Pretend we have early clobbers so the use operands get marked below.
956 // This is not necessary for the common case of a single tied use.
957 hasEarlyClobbers = true;
958 }
959
960 // Second scan.
961 // Allocate virtreg uses.
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000962 bool HasUndefUse = false;
Matthias Braunfb93aec2018-11-10 00:36:27 +0000963 for (unsigned I = 0; I != VirtOpEnd; ++I) {
964 MachineOperand &MO = MI.getOperand(I);
965 if (!MO.isReg()) continue;
966 unsigned Reg = MO.getReg();
967 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
968 if (MO.isUse()) {
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000969 if (MO.isUndef()) {
970 HasUndefUse = true;
971 // There is no need to allocate a register for an undef use.
972 continue;
973 }
Matthias Braunfb93aec2018-11-10 00:36:27 +0000974 LiveReg &LR = reloadVirtReg(MI, I, Reg, CopyDstReg);
975 MCPhysReg PhysReg = LR.PhysReg;
976 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
977 if (setPhysReg(MI, MO, PhysReg))
978 killVirtReg(LR);
979 }
980 }
981
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000982 // Allocate undef operands. This is a separate step because in a situation
983 // like ` = OP undef %X, %X` both operands need the same register assign
984 // so we should perform the normal assignment first.
985 if (HasUndefUse) {
986 for (MachineOperand &MO : MI.uses()) {
987 if (!MO.isReg() || !MO.isUse())
988 continue;
989 unsigned Reg = MO.getReg();
990 if (!TargetRegisterInfo::isVirtualRegister(Reg))
991 continue;
992
993 assert(MO.isUndef() && "Should only have undef virtreg uses left");
994 allocVirtRegUndef(MO);
995 }
996 }
997
Matthias Braunfb93aec2018-11-10 00:36:27 +0000998 // Track registers defined by instruction - early clobbers and tied uses at
999 // this point.
1000 UsedInInstr.clear();
1001 if (hasEarlyClobbers) {
1002 for (const MachineOperand &MO : MI.operands()) {
1003 if (!MO.isReg()) continue;
1004 unsigned Reg = MO.getReg();
1005 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
1006 // Look for physreg defs and tied uses.
1007 if (!MO.isDef() && !MO.isTied()) continue;
1008 markRegUsedInInstr(Reg);
1009 }
1010 }
1011
1012 unsigned DefOpEnd = MI.getNumOperands();
1013 if (MI.isCall()) {
1014 // Spill all virtregs before a call. This serves one purpose: If an
1015 // exception is thrown, the landing pad is going to expect to find
1016 // registers in their spill slots.
1017 // Note: although this is appealing to just consider all definitions
1018 // as call-clobbered, this is not correct because some of those
1019 // definitions may be used later on and we do not want to reuse
1020 // those for virtual registers in between.
1021 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1022 spillAll(MI);
1023 }
1024
1025 // Third scan.
1026 // Allocate defs and collect dead defs.
1027 for (unsigned I = 0; I != DefOpEnd; ++I) {
1028 const MachineOperand &MO = MI.getOperand(I);
1029 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1030 continue;
1031 unsigned Reg = MO.getReg();
1032
1033 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1034 if (!MRI->isAllocatable(Reg)) continue;
1035 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
1036 continue;
1037 }
1038 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
1039 if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
1040 VirtDead.push_back(Reg);
1041 CopyDstReg = 0; // cancel coalescing;
1042 } else
1043 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1044 }
1045
1046 // Kill dead defs after the scan to ensure that multiple defs of the same
1047 // register are allocated identically. We didn't need to do this for uses
1048 // because we are crerating our own kill flags, and they are always at the
1049 // last use.
1050 for (unsigned VirtReg : VirtDead)
1051 killVirtReg(VirtReg);
1052 VirtDead.clear();
1053
1054 LLVM_DEBUG(dbgs() << "<< " << MI);
1055 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1056 LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1057 Coalesced.push_back(&MI);
1058 }
1059}
1060
1061void RegAllocFast::handleDebugValue(MachineInstr &MI) {
1062 MachineOperand &MO = MI.getOperand(0);
1063
1064 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1065 // mostly constants and frame indices.
1066 if (!MO.isReg())
1067 return;
1068 unsigned Reg = MO.getReg();
1069 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1070 return;
1071
1072 // See if this virtual register has already been allocated to a physical
1073 // register or spilled to a stack slot.
1074 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1075 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1076 setPhysReg(MI, MO, LRI->PhysReg);
1077 } else {
1078 int SS = StackSlotForVirtReg[Reg];
1079 if (SS != -1) {
1080 // Modify DBG_VALUE now that the value is in a spill slot.
1081 updateDbgValueForSpill(MI, SS);
1082 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << MI);
1083 return;
1084 }
1085
1086 // We can't allocate a physreg for a DebugValue, sorry!
1087 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
1088 MO.setReg(0);
1089 }
1090
1091 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1092 // that future spills of Reg will have DBG_VALUEs.
1093 LiveDbgValueMap[Reg].push_back(&MI);
1094}
1095
Matthias Braun864cf582017-09-09 00:52:46 +00001096void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1097 this->MBB = &MBB;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001098 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001099
1100 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001101 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001102
Matthias Braun864cf582017-09-09 00:52:46 +00001103 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001104
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001105 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +00001106 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +00001107 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet72f6d592018-01-29 23:42:37 +00001108 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001109
Matthias Brauna09d18d2017-09-09 00:52:45 +00001110 VirtDead.clear();
1111 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001112
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001113 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +00001114 for (MachineInstr &MI : MBB) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001115 LLVM_DEBUG(
1116 dbgs() << "\n>> " << MI << "Regs:";
1117 dumpState()
1118 );
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001119
Matthias Braunfb93aec2018-11-10 00:36:27 +00001120 // Special handling for debug values. Note that they are not allowed to
1121 // affect codegen of the other instructions in any way.
Matthias Braun864cf582017-09-09 00:52:46 +00001122 if (MI.isDebugValue()) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001123 handleDebugValue(MI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001124 continue;
1125 }
1126
Matthias Braunfb93aec2018-11-10 00:36:27 +00001127 allocateInstruction(MI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001128 }
1129
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001130 // Spill all physical registers holding virtual registers now.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001131 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001132 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001133
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001134 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001135 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001136 for (MachineInstr *MI : Coalesced)
1137 MBB.erase(MI);
Matthias Braun14af82a2018-11-07 02:04:07 +00001138 NumCoalesced += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001139
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001140 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001141}
1142
Matthias Braun864cf582017-09-09 00:52:46 +00001143bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001144 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1145 << "********** Function: " << MF.getName() << '\n');
Matthias Braun864cf582017-09-09 00:52:46 +00001146 MRI = &MF.getRegInfo();
1147 const TargetSubtargetInfo &STI = MF.getSubtarget();
1148 TRI = STI.getRegisterInfo();
1149 TII = STI.getInstrInfo();
1150 MFI = &MF.getFrameInfo();
1151 MRI->freezeReservedRegs(MF);
1152 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001153 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001154 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001155
1156 // initialize the virtual->physical register map to have a 'null'
1157 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001158 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1159 StackSlotForVirtReg.resize(NumVirtRegs);
1160 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001161
1162 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001163 for (MachineBasicBlock &MBB : MF)
1164 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001165
Andrew Trickda84e642012-02-21 04:51:23 +00001166 // All machine operands and other references to virtual registers have been
1167 // replaced. Remove the virtual registers.
1168 MRI->clearVirtRegs();
1169
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001170 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001171 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001172 return true;
1173}
1174
1175FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001176 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001177}