blob: ad080e9c2900923e2e518449da2dcac824994eb3 [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);
206 MCPhysReg defineVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
207 unsigned Hint);
208 LiveReg &reloadVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
209 unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000210 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000211 bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000212
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000213 int getStackSpaceFor(unsigned VirtReg);
214 void spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
215 MCPhysReg AssignedReg, bool Kill);
216 void reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
217 MCPhysReg PhysReg);
218
Matthias Braun864cf582017-09-09 00:52:46 +0000219 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000220 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000221
222} // end anonymous namespace
223
224char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000225
Matthias Braun864cf582017-09-09 00:52:46 +0000226INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
227 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000228
Matthias Braun0804dca2018-11-07 06:57:00 +0000229void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
230 PhysRegState[PhysReg] = NewState;
231}
232
Matthias Braun864cf582017-09-09 00:52:46 +0000233/// This allocates space for the specified virtual register to be held on the
234/// stack.
Matthias Braunebcf5432018-11-07 02:04:11 +0000235int RegAllocFast::getStackSpaceFor(unsigned VirtReg) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000236 // Find the location Reg would belong...
237 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000238 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000239 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000240 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000241
242 // Allocate a new stack object for this spill location...
Matthias Braunebcf5432018-11-07 02:04:11 +0000243 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000244 unsigned Size = TRI->getSpillSize(RC);
245 unsigned Align = TRI->getSpillAlignment(RC);
246 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000247
248 // Assign the slot.
249 StackSlotForVirtReg[VirtReg] = FrameIdx;
250 return FrameIdx;
251}
252
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000253/// Insert spill instruction for \p AssignedReg before \p Before. Update
254/// DBG_VALUEs with \p VirtReg operands with the stack slot.
255void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
256 MCPhysReg AssignedReg, bool Kill) {
257 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
258 << " in " << printReg(AssignedReg, TRI));
259 int FI = getStackSpaceFor(VirtReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000260 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000261
262 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
263 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
264 ++NumStores;
265
266 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
267 // identify spilled location as the place to find corresponding variable's
268 // value.
269 SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
270 for (MachineInstr *DBG : LRIDbgValues) {
271 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
272 assert(NewDV->getParent() == MBB && "dangling parent pointer");
273 (void)NewDV;
274 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
275 }
276 // Now this register is spilled there is should not be any DBG_VALUE
277 // pointing to this register because they are all pointing to spilled value
278 // now.
279 LRIDbgValues.clear();
280}
281
282/// Insert reload instruction for \p PhysReg before \p Before.
283void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
284 MCPhysReg PhysReg) {
285 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000286 << printReg(PhysReg, TRI) << '\n');
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000287 int FI = getStackSpaceFor(VirtReg);
288 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
289 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
290 ++NumLoads;
291}
292
Matthias Braun864cf582017-09-09 00:52:46 +0000293/// Return true if MO is the only remaining reference to its virtual register,
294/// and it is guaranteed to be a block-local register.
295bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000296 // If the register has ever been spilled or reloaded, we conservatively assume
297 // it is a global register used in multiple blocks.
298 if (StackSlotForVirtReg[MO.getReg()] != -1)
299 return false;
300
301 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000302 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000303 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000304 return false;
305 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000306}
307
Matthias Braun864cf582017-09-09 00:52:46 +0000308/// Set kill flags on last use of a virtual register.
309void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000310 if (!LR.LastUse) return;
311 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000312 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
313 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000314 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000315 // else, don't do anything we are problably redefining a
316 // subreg of this register and given we don't track which
317 // lanes are actually dead, we cannot insert a kill flag here.
318 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000319 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000320 // ... <== Here we would allow later pass to reuse physreg:sub1
321 // which is potentially wrong.
322 // LR:sub0 = ...
323 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000324 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000325}
326
Matthias Braun864cf582017-09-09 00:52:46 +0000327/// Mark virtreg as no longer available.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000328void RegAllocFast::killVirtReg(LiveReg &LR) {
329 addKillFlag(LR);
330 assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000331 "Broken RegState mapping");
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000332 setPhysRegState(LR.PhysReg, regFree);
333 LR.PhysReg = 0;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000334}
335
Matthias Braun864cf582017-09-09 00:52:46 +0000336/// Mark virtreg as no longer available.
337void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000338 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
339 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000340 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000341 if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
342 killVirtReg(*LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000343}
344
Matthias Braun864cf582017-09-09 00:52:46 +0000345/// This method spills the value specified by VirtReg into the corresponding
346/// stack slot if needed.
347void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
348 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000349 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
350 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000351 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000352 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
353 "Spilling unmapped virtual register");
354 spillVirtReg(MI, *LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000355}
356
Matthias Braun864cf582017-09-09 00:52:46 +0000357/// Do the actual work of spilling.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000358void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR) {
359 assert(PhysRegState[LR.PhysReg] == LR.VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000360
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000361 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000362 // If this physreg is used by the instruction, we want to kill it on the
363 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000364 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000365 LR.Dirty = false;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000366
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000367 spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000368
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000369 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000370 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000371 }
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000372 killVirtReg(LR);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000373}
374
Matthias Braun864cf582017-09-09 00:52:46 +0000375/// Spill all dirty virtregs without killing them.
376void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000377 if (LiveVirtRegs.empty())
378 return;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000379 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
380 // of spilling here is deterministic, if arbitrary.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000381 for (LiveReg &LR : LiveVirtRegs) {
382 if (!LR.PhysReg)
383 continue;
384 spillVirtReg(MI, LR);
385 }
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000386 LiveVirtRegs.clear();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000387}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000388
Matthias Braun864cf582017-09-09 00:52:46 +0000389/// Handle the direct use of a physical register. Check that the register is
390/// not used by a virtreg. Kill the physreg, marking it free. This may add
391/// implicit kills to MO->getParent() and invalidate MO.
392void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000393 // Ignore undef uses.
394 if (MO.isUndef())
395 return;
396
Matthias Braun864cf582017-09-09 00:52:46 +0000397 unsigned PhysReg = MO.getReg();
398 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
399 "Bad usePhysReg operand");
400
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000401 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000402 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000403 case regDisabled:
404 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000405 case regReserved:
406 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000407 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000408 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000409 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000410 return;
411 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000412 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000413 // wanted has been clobbered.
414 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000415 }
416
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000417 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000418 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000419 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000420 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000421 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000422 break;
423 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000424 // Either PhysReg is a subregister of Alias and we mark the
425 // whole register as free, or PhysReg is the superregister of
426 // Alias and we mark all the aliases as disabled before freeing
427 // PhysReg.
428 // In the latter case, since PhysReg was disabled, this means that
429 // its value is defined only by physical sub-registers. This check
430 // is performed by the assert of the default case in this loop.
431 // Note: The value of the superregister may only be partial
432 // defined, that is why regDisabled is a valid state for aliases.
433 assert((TRI->isSuperRegister(PhysReg, Alias) ||
434 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000435 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000436 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000437 case regFree:
438 if (TRI->isSuperRegister(PhysReg, Alias)) {
439 // Leave the superregister in the working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000440 setPhysRegState(Alias, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000441 MO.getParent()->addRegisterKilled(Alias, TRI, true);
442 return;
443 }
444 // Some other alias was in the working set - clear it.
Matthias Braun0804dca2018-11-07 06:57:00 +0000445 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000446 break;
447 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000448 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000449 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000450 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000451
452 // All aliases are disabled, bring register into working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000453 setPhysRegState(PhysReg, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000454 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000455}
456
Matthias Braun864cf582017-09-09 00:52:46 +0000457/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
458/// similar to defineVirtReg except the physreg is reserved instead of
459/// allocated.
Quentin Colombet72f6d592018-01-29 23:42:37 +0000460void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
461 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000462 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000463 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
464 case regDisabled:
465 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000466 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000467 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000468 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000469 case regFree:
470 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000471 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000472 return;
473 }
474
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000475 // This is a disabled register, disable all aliases.
Matthias Braun0804dca2018-11-07 06:57:00 +0000476 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000477 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000478 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000479 switch (unsigned VirtReg = PhysRegState[Alias]) {
480 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000481 break;
482 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000483 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000484 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000485 case regFree:
486 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000487 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000488 if (TRI->isSuperRegister(PhysReg, Alias))
489 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000490 break;
491 }
492 }
493}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000494
Matthias Braunfb93aec2018-11-10 00:36:27 +0000495/// Return the cost of spilling clearing out PhysReg and aliases so it is free
496/// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
497/// disabled - it can be allocated directly.
Matthias Braun864cf582017-09-09 00:52:46 +0000498/// \returns spillImpossible when PhysReg or an alias can't be spilled.
499unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000500 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000501 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
502 << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000503 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000504 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000505 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
506 case regDisabled:
507 break;
508 case regFree:
509 return 0;
510 case regReserved:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000511 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
512 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000513 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000514 default: {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000515 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
516 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
517 "Missing VirtReg entry");
518 return LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000519 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000520 }
521
Eric Christopherc3783362011-04-12 00:48:08 +0000522 // This is a disabled register, add up cost of aliases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000523 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000524 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000525 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000526 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000527 switch (unsigned VirtReg = PhysRegState[Alias]) {
528 case regDisabled:
529 break;
530 case regFree:
531 ++Cost;
532 break;
533 case regReserved:
534 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000535 default: {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000536 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
537 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
538 "Missing VirtReg entry");
539 Cost += LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000540 break;
541 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000542 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000543 }
544 return Cost;
545}
546
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000547/// This method updates local state so that we know that PhysReg is the
Matthias Braun864cf582017-09-09 00:52:46 +0000548/// proper container for VirtReg now. The physical register must not be used
549/// for anything else when this is called.
550void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Matthias Braun0804dca2018-11-07 06:57:00 +0000551 unsigned VirtReg = LR.VirtReg;
552 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000553 << printReg(PhysReg, TRI) << '\n');
Matthias Braun0804dca2018-11-07 06:57:00 +0000554 assert(LR.PhysReg == 0 && "Already assigned a physreg");
555 assert(PhysReg != 0 && "Trying to assign no register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000556 LR.PhysReg = PhysReg;
Matthias Braun0804dca2018-11-07 06:57:00 +0000557 setPhysRegState(PhysReg, VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000558}
559
Matthias Braun864cf582017-09-09 00:52:46 +0000560/// Allocates a physical register for VirtReg.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000561void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint) {
562 const unsigned VirtReg = LR.VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000563
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000564 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
565 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000566
Matthias Braun864cf582017-09-09 00:52:46 +0000567 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000568 LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
569 << " in class " << TRI->getRegClassName(&RC) << '\n');
570
571 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000572 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
573 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000574 // Ignore the hint if we would have to spill a dirty register.
575 unsigned Cost = calcSpillCost(Hint);
576 if (Cost < spillDirty) {
577 if (Cost)
578 definePhysReg(MI, Hint, regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000579 assignVirtToPhysReg(LR, Hint);
580 return;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000581 }
582 }
583
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000584 // First try to find a completely free register.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000585 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
586 for (MCPhysReg PhysReg : AllocationOrder) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000587 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000588 assignVirtToPhysReg(LR, PhysReg);
589 return;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000590 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000591 }
592
Matthias Braunfb93aec2018-11-10 00:36:27 +0000593 MCPhysReg BestReg = 0;
Matthias Braun864cf582017-09-09 00:52:46 +0000594 unsigned BestCost = spillImpossible;
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000595 for (MCPhysReg PhysReg : AllocationOrder) {
596 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
Matthias Braun864cf582017-09-09 00:52:46 +0000597 unsigned Cost = calcSpillCost(PhysReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000598 LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
Matthias Braunfb93aec2018-11-10 00:36:27 +0000599 // Immediate take a register with cost 0.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000600 if (Cost == 0) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000601 assignVirtToPhysReg(LR, PhysReg);
602 return;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000603 }
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000604 if (Cost < BestCost) {
605 BestReg = PhysReg;
606 BestCost = Cost;
607 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000608 }
609
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000610 if (!BestReg) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000611 // Nothing we can do: Report an error and keep going with an invalid
612 // allocation.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000613 if (MI.isInlineAsm())
614 MI.emitError("inline assembly requires more registers than available");
615 else
616 MI.emitError("ran out of registers during register allocation");
617 definePhysReg(MI, *AllocationOrder.begin(), regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000618 assignVirtToPhysReg(LR, *AllocationOrder.begin());
619 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000620 }
621
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000622 definePhysReg(MI, BestReg, regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000623 assignVirtToPhysReg(LR, BestReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000624}
625
Matthias Braun864cf582017-09-09 00:52:46 +0000626/// Allocates a register for VirtReg and mark it as dirty.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000627MCPhysReg RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
628 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000629 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
630 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000631 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000632 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000633 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000634 if (!LRI->PhysReg) {
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000635 // If there is no hint, peek at the only use of this register.
636 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
637 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000638 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000639 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000640 if (UseMI.isCopyLike())
641 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000642 }
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000643 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000644 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000645 // Redefining a live register - kill at the last use, unless it is this
646 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000647 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000648 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000649 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000650 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000651 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000652 LRI->LastOpNum = OpNum;
653 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000654 markRegUsedInInstr(LRI->PhysReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000655 return LRI->PhysReg;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000656}
657
Matthias Braun864cf582017-09-09 00:52:46 +0000658/// Make sure VirtReg is available in a physreg and return it.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000659RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(MachineInstr &MI,
660 unsigned OpNum,
661 unsigned VirtReg,
662 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000663 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
664 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000665 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000666 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000667 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000668 MachineOperand &MO = MI.getOperand(OpNum);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000669 if (!LRI->PhysReg) {
670 allocVirtReg(MI, *LRI, Hint);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000671 reload(MI, VirtReg, LRI->PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000672 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000673 if (isLastUseOfLocalReg(MO)) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000674 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000675 if (MO.isUse())
676 MO.setIsKill();
677 else
678 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000679 } else if (MO.isKill()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000680 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << '\n');
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000681 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000682 } else if (MO.isDead()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000683 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000684 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000685 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000686 } else if (MO.isKill()) {
687 // We must remove kill flags from uses of reloaded registers because the
688 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000689 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000690 // This would cause a second reload of %x into a different register.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000691 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << '\n');
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000692 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000693 } else if (MO.isDead()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000694 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000695 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000696 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000697 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000698 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000699 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000700 markRegUsedInInstr(LRI->PhysReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000701 return *LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000702}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000703
Matthias Braun864cf582017-09-09 00:52:46 +0000704/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
705/// may invalidate any operand pointers. Return true if the operand kills its
706/// register.
Matthias Braunfb93aec2018-11-10 00:36:27 +0000707bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
Matthias Braun864cf582017-09-09 00:52:46 +0000708 MCPhysReg PhysReg) {
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000709 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000710 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000711 MO.setReg(PhysReg);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000712 MO.setIsRenamable(true);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000713 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000714 }
715
716 // Handle subregister index.
717 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000718 MO.setIsRenamable(true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000719 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000720
721 // A kill flag implies killing the full register. Add corresponding super
722 // register kill.
723 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000724 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000725 return true;
726 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000727
728 // A <def,read-undef> of a sub-register requires an implicit def of the full
729 // register.
730 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000731 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000732
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000733 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000734}
735
Matthias Braun864cf582017-09-09 00:52:46 +0000736// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000737// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000738void RegAllocFast::handleThroughOperands(MachineInstr &MI,
739 SmallVectorImpl<unsigned> &VirtDead) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000740 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000741 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000742 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000743 if (!MO.isReg()) continue;
744 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000745 if (!TargetRegisterInfo::isVirtualRegister(Reg))
746 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000747 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
748 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000749 if (ThroughRegs.insert(Reg).second)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000750 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000751 }
752 }
753
754 // If any physreg defines collide with preallocated through registers,
755 // we must spill and reallocate.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000756 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000757 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000758 if (!MO.isReg() || !MO.isDef()) continue;
759 unsigned Reg = MO.getReg();
760 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000761 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000762 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000763 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000764 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000765 }
766 }
767
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000768 SmallVector<unsigned, 8> PartialDefs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000769 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000770 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000771 MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000772 if (!MO.isReg()) continue;
773 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000774 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000775 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000776 if (!MO.isTied()) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000777 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
778 << ") is tied to operand " << MI.findTiedOperandIdx(I)
779 << ".\n");
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000780 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
781 MCPhysReg PhysReg = LR.PhysReg;
Matthias Braunfb93aec2018-11-10 00:36:27 +0000782 setPhysReg(MI, MO, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000783 // Note: we don't update the def operand yet. That would cause the normal
784 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000785 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000786 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000787 // Reload the register, but don't assign to the operand just yet.
788 // That would confuse the later phys-def processing pass.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000789 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
790 PartialDefs.push_back(LR.PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000791 }
792 }
793
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000794 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000795 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
796 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000797 if (!MO.isReg()) continue;
798 unsigned Reg = MO.getReg();
799 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
800 if (!MO.isEarlyClobber())
801 continue;
802 // Note: defineVirtReg may invalidate MO.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000803 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, 0);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000804 if (setPhysReg(MI, MI.getOperand(I), PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000805 VirtDead.push_back(Reg);
806 }
807
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000808 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000809 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000810 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000811 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
812 unsigned Reg = MO.getReg();
813 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000814 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
815 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000816 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000817 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000818
819 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000820 for (unsigned PartialDef : PartialDefs)
821 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000822}
823
Matthias Braun864cf582017-09-09 00:52:46 +0000824#ifndef NDEBUG
825void RegAllocFast::dumpState() {
826 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
827 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000828 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000829 switch(PhysRegState[Reg]) {
830 case regFree:
831 break;
832 case regReserved:
833 dbgs() << "*";
834 break;
835 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000836 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000837 LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
838 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
839 "Missing VirtReg entry");
840 if (LRI->Dirty)
Matthias Braun864cf582017-09-09 00:52:46 +0000841 dbgs() << "*";
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000842 assert(LRI->PhysReg == Reg && "Bad inverse map");
Matthias Braun864cf582017-09-09 00:52:46 +0000843 break;
844 }
845 }
846 }
847 dbgs() << '\n';
848 // Check that LiveVirtRegs is the inverse.
849 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
850 e = LiveVirtRegs.end(); i != e; ++i) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000851 if (!i->PhysReg)
852 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000853 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
854 "Bad map key");
855 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
856 "Bad map value");
857 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
858 }
859}
860#endif
861
Matthias Braunfb93aec2018-11-10 00:36:27 +0000862void RegAllocFast::allocateInstruction(MachineInstr &MI) {
863 const MCInstrDesc &MCID = MI.getDesc();
864
865 // If this is a copy, we may be able to coalesce.
866 unsigned CopySrcReg = 0;
867 unsigned CopyDstReg = 0;
868 unsigned CopySrcSub = 0;
869 unsigned CopyDstSub = 0;
870 if (MI.isCopy()) {
871 CopyDstReg = MI.getOperand(0).getReg();
872 CopySrcReg = MI.getOperand(1).getReg();
873 CopyDstSub = MI.getOperand(0).getSubReg();
874 CopySrcSub = MI.getOperand(1).getSubReg();
875 }
876
877 // Track registers used by instruction.
878 UsedInInstr.clear();
879
880 // First scan.
881 // Mark physreg uses and early clobbers as used.
882 // Find the end of the virtreg operands
883 unsigned VirtOpEnd = 0;
884 bool hasTiedOps = false;
885 bool hasEarlyClobbers = false;
886 bool hasPartialRedefs = false;
887 bool hasPhysDefs = false;
888 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
889 MachineOperand &MO = MI.getOperand(i);
890 // Make sure MRI knows about registers clobbered by regmasks.
891 if (MO.isRegMask()) {
892 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
893 continue;
894 }
895 if (!MO.isReg()) continue;
896 unsigned Reg = MO.getReg();
897 if (!Reg) continue;
898 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
899 VirtOpEnd = i+1;
900 if (MO.isUse()) {
901 hasTiedOps = hasTiedOps ||
902 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
903 } else {
904 if (MO.isEarlyClobber())
905 hasEarlyClobbers = true;
906 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
907 hasPartialRedefs = true;
908 }
909 continue;
910 }
911 if (!MRI->isAllocatable(Reg)) continue;
912 if (MO.isUse()) {
913 usePhysReg(MO);
914 } else if (MO.isEarlyClobber()) {
915 definePhysReg(MI, Reg,
916 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
917 hasEarlyClobbers = true;
918 } else
919 hasPhysDefs = true;
920 }
921
922 // The instruction may have virtual register operands that must be allocated
923 // the same register at use-time and def-time: early clobbers and tied
924 // operands. If there are also physical defs, these registers must avoid
925 // both physical defs and uses, making them more constrained than normal
926 // operands.
927 // Similarly, if there are multiple defs and tied operands, we must make
928 // sure the same register is allocated to uses and defs.
929 // We didn't detect inline asm tied operands above, so just make this extra
930 // pass for all inline asm.
931 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
932 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
933 handleThroughOperands(MI, VirtDead);
934 // Don't attempt coalescing when we have funny stuff going on.
935 CopyDstReg = 0;
936 // Pretend we have early clobbers so the use operands get marked below.
937 // This is not necessary for the common case of a single tied use.
938 hasEarlyClobbers = true;
939 }
940
941 // Second scan.
942 // Allocate virtreg uses.
943 for (unsigned I = 0; I != VirtOpEnd; ++I) {
944 MachineOperand &MO = MI.getOperand(I);
945 if (!MO.isReg()) continue;
946 unsigned Reg = MO.getReg();
947 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
948 if (MO.isUse()) {
949 LiveReg &LR = reloadVirtReg(MI, I, Reg, CopyDstReg);
950 MCPhysReg PhysReg = LR.PhysReg;
951 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
952 if (setPhysReg(MI, MO, PhysReg))
953 killVirtReg(LR);
954 }
955 }
956
957 // Track registers defined by instruction - early clobbers and tied uses at
958 // this point.
959 UsedInInstr.clear();
960 if (hasEarlyClobbers) {
961 for (const MachineOperand &MO : MI.operands()) {
962 if (!MO.isReg()) continue;
963 unsigned Reg = MO.getReg();
964 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
965 // Look for physreg defs and tied uses.
966 if (!MO.isDef() && !MO.isTied()) continue;
967 markRegUsedInInstr(Reg);
968 }
969 }
970
971 unsigned DefOpEnd = MI.getNumOperands();
972 if (MI.isCall()) {
973 // Spill all virtregs before a call. This serves one purpose: If an
974 // exception is thrown, the landing pad is going to expect to find
975 // registers in their spill slots.
976 // Note: although this is appealing to just consider all definitions
977 // as call-clobbered, this is not correct because some of those
978 // definitions may be used later on and we do not want to reuse
979 // those for virtual registers in between.
980 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
981 spillAll(MI);
982 }
983
984 // Third scan.
985 // Allocate defs and collect dead defs.
986 for (unsigned I = 0; I != DefOpEnd; ++I) {
987 const MachineOperand &MO = MI.getOperand(I);
988 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
989 continue;
990 unsigned Reg = MO.getReg();
991
992 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
993 if (!MRI->isAllocatable(Reg)) continue;
994 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
995 continue;
996 }
997 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
998 if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
999 VirtDead.push_back(Reg);
1000 CopyDstReg = 0; // cancel coalescing;
1001 } else
1002 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1003 }
1004
1005 // Kill dead defs after the scan to ensure that multiple defs of the same
1006 // register are allocated identically. We didn't need to do this for uses
1007 // because we are crerating our own kill flags, and they are always at the
1008 // last use.
1009 for (unsigned VirtReg : VirtDead)
1010 killVirtReg(VirtReg);
1011 VirtDead.clear();
1012
1013 LLVM_DEBUG(dbgs() << "<< " << MI);
1014 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1015 LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1016 Coalesced.push_back(&MI);
1017 }
1018}
1019
1020void RegAllocFast::handleDebugValue(MachineInstr &MI) {
1021 MachineOperand &MO = MI.getOperand(0);
1022
1023 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1024 // mostly constants and frame indices.
1025 if (!MO.isReg())
1026 return;
1027 unsigned Reg = MO.getReg();
1028 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1029 return;
1030
1031 // See if this virtual register has already been allocated to a physical
1032 // register or spilled to a stack slot.
1033 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1034 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1035 setPhysReg(MI, MO, LRI->PhysReg);
1036 } else {
1037 int SS = StackSlotForVirtReg[Reg];
1038 if (SS != -1) {
1039 // Modify DBG_VALUE now that the value is in a spill slot.
1040 updateDbgValueForSpill(MI, SS);
1041 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << MI);
1042 return;
1043 }
1044
1045 // We can't allocate a physreg for a DebugValue, sorry!
1046 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
1047 MO.setReg(0);
1048 }
1049
1050 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1051 // that future spills of Reg will have DBG_VALUEs.
1052 LiveDbgValueMap[Reg].push_back(&MI);
1053}
1054
Matthias Braun864cf582017-09-09 00:52:46 +00001055void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1056 this->MBB = &MBB;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001057 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001058
1059 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001060 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001061
Matthias Braun864cf582017-09-09 00:52:46 +00001062 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001063
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001064 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +00001065 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +00001066 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet72f6d592018-01-29 23:42:37 +00001067 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001068
Matthias Brauna09d18d2017-09-09 00:52:45 +00001069 VirtDead.clear();
1070 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001071
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +00001073 for (MachineInstr &MI : MBB) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001074 LLVM_DEBUG(
1075 dbgs() << "\n>> " << MI << "Regs:";
1076 dumpState()
1077 );
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001078
Matthias Braunfb93aec2018-11-10 00:36:27 +00001079 // Special handling for debug values. Note that they are not allowed to
1080 // affect codegen of the other instructions in any way.
Matthias Braun864cf582017-09-09 00:52:46 +00001081 if (MI.isDebugValue()) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001082 handleDebugValue(MI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001083 continue;
1084 }
1085
Matthias Braunfb93aec2018-11-10 00:36:27 +00001086 allocateInstruction(MI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001087 }
1088
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001089 // Spill all physical registers holding virtual registers now.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001090 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001091 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001092
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001093 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001094 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001095 for (MachineInstr *MI : Coalesced)
1096 MBB.erase(MI);
Matthias Braun14af82a2018-11-07 02:04:07 +00001097 NumCoalesced += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001098
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001099 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001100}
1101
Matthias Braun864cf582017-09-09 00:52:46 +00001102bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001103 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1104 << "********** Function: " << MF.getName() << '\n');
Matthias Braun864cf582017-09-09 00:52:46 +00001105 MRI = &MF.getRegInfo();
1106 const TargetSubtargetInfo &STI = MF.getSubtarget();
1107 TRI = STI.getRegisterInfo();
1108 TII = STI.getInstrInfo();
1109 MFI = &MF.getFrameInfo();
1110 MRI->freezeReservedRegs(MF);
1111 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001112 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001113 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001114
1115 // initialize the virtual->physical register map to have a 'null'
1116 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001117 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1118 StackSlotForVirtReg.resize(NumVirtRegs);
1119 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001120
1121 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001122 for (MachineBasicBlock &MBB : MF)
1123 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001124
Andrew Trickda84e642012-02-21 04:51:23 +00001125 // All machine operands and other references to virtual registers have been
1126 // replaced. Remove the virtual registers.
1127 MRI->clearVirtRegs();
1128
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001129 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001130 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001131 return true;
1132}
1133
1134FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001135 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001136}