blob: 814d24fdd456e1c01a2c97756ec34358947f1d99 [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.
Matt Arsenault0202fa32019-10-30 14:01:58 -070085 Register VirtReg; ///< Virtual register number.
Eugene Zelenko618c5552017-09-13 21:15:20 +000086 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
Matt Arsenault0202fa32019-10-30 14:01:58 -070090 explicit LiveReg(Register VirtReg) : VirtReg(VirtReg) {}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000091
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000092 unsigned getSparseSetIndex() const {
Daniel Sanders2bea69b2019-08-01 23:27:28 +000093 return Register::virtReg2Index(VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000094 }
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
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000104 /// Has a bit set for every virtual register for which it was determined
105 /// that it is alive across blocks.
106 BitVector MayLiveAcrossBlocks;
107
Matthias Braunebcf5432018-11-07 02:04:11 +0000108 /// State of a physical register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000109 enum RegState {
Matthias Braun864cf582017-09-09 00:52:46 +0000110 /// A disabled register is not available for allocation, but an alias may
111 /// be in use. A register can only be moved out of the disabled state if
112 /// all aliases are disabled.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000113 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000114
Matthias Braun864cf582017-09-09 00:52:46 +0000115 /// A free register is not currently in use and can be allocated
116 /// immediately without checking aliases.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000117 regFree,
118
Matthias Braun864cf582017-09-09 00:52:46 +0000119 /// A reserved register has been assigned explicitly (e.g., setting up a
120 /// call parameter), and it remains reserved until it is used.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000121 regReserved
122
Matthias Braun864cf582017-09-09 00:52:46 +0000123 /// A register state may also be a virtual register number, indication
124 /// that the physical register is currently allocated to a virtual
125 /// register. In that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000126 };
127
Matthias Braunebcf5432018-11-07 02:04:11 +0000128 /// Maps each physical register to a RegState enum or a virtual register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000129 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000130
Matt Arsenault0202fa32019-10-30 14:01:58 -0700131 SmallVector<Register, 16> VirtDead;
Eugene Zelenko618c5552017-09-13 21:15:20 +0000132 SmallVector<MachineInstr *, 32> Coalesced;
Matthias Brauna09d18d2017-09-09 00:52:45 +0000133
Matthias Braunebcf5432018-11-07 02:04:11 +0000134 using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
Matthias Braun864cf582017-09-09 00:52:46 +0000135 /// Set of register units that are used in the current instruction, and so
136 /// cannot be allocated.
Matthias Braunebcf5432018-11-07 02:04:11 +0000137 RegUnitSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000138
Matthias Braun0804dca2018-11-07 06:57:00 +0000139 void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
140
Matthias Braun864cf582017-09-09 00:52:46 +0000141 /// Mark a physreg as used in this instruction.
142 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000143 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
144 UsedInInstr.insert(*Units);
145 }
146
Matthias Braun864cf582017-09-09 00:52:46 +0000147 /// Check if a physreg or any of its aliases are used in this instruction.
148 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000149 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
150 if (UsedInInstr.count(*Units))
151 return true;
152 return false;
153 }
154
Alp Toker61007d82014-03-02 03:20:38 +0000155 enum : unsigned {
Matthias Braunebcf5432018-11-07 02:04:11 +0000156 spillClean = 50,
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000157 spillDirty = 100,
Matt Arsenault828b6852019-05-16 12:50:39 +0000158 spillPrefBonus = 20,
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000159 spillImpossible = ~0u
160 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000161
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000162 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000163 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000164
Craig Topper4584cd52014-03-07 09:26:03 +0000165 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000166 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000167 MachineFunctionPass::getAnalysisUsage(AU);
168 }
169
Matthias Braun90799ce2016-08-23 21:19:49 +0000170 MachineFunctionProperties getRequiredProperties() const override {
171 return MachineFunctionProperties().set(
172 MachineFunctionProperties::Property::NoPHIs);
173 }
174
Derek Schuffad154c82016-03-28 17:05:30 +0000175 MachineFunctionProperties getSetProperties() const override {
176 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000177 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000178 }
179
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000180 private:
Fangrui Songcb0bab82018-07-16 18:51:40 +0000181 bool runOnMachineFunction(MachineFunction &MF) override;
Matthias Braunebcf5432018-11-07 02:04:11 +0000182
Matthias Braun864cf582017-09-09 00:52:46 +0000183 void allocateBasicBlock(MachineBasicBlock &MBB);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000184 void allocateInstruction(MachineInstr &MI);
185 void handleDebugValue(MachineInstr &MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000186 void handleThroughOperands(MachineInstr &MI,
Matt Arsenault0202fa32019-10-30 14:01:58 -0700187 SmallVectorImpl<Register> &VirtDead);
Matthias Braun864cf582017-09-09 00:52:46 +0000188 bool isLastUseOfLocalReg(const MachineOperand &MO) const;
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000189
Matthias Braun864cf582017-09-09 00:52:46 +0000190 void addKillFlag(const LiveReg &LRI);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000191 void killVirtReg(LiveReg &LR);
Matt Arsenault0202fa32019-10-30 14:01:58 -0700192 void killVirtReg(Register VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000193 void spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR);
Matt Arsenault0202fa32019-10-30 14:01:58 -0700194 void spillVirtReg(MachineBasicBlock::iterator MI, Register VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000195
Matthias Braun864cf582017-09-09 00:52:46 +0000196 void usePhysReg(MachineOperand &MO);
Quentin Colombet72f6d592018-01-29 23:42:37 +0000197 void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
198 RegState NewState);
Matthias Braun864cf582017-09-09 00:52:46 +0000199 unsigned calcSpillCost(MCPhysReg PhysReg) const;
Quentin Colombet72f6d592018-01-29 23:42:37 +0000200 void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000201
Matt Arsenault0202fa32019-10-30 14:01:58 -0700202 LiveRegMap::iterator findLiveVirtReg(Register VirtReg) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000203 return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000204 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000205
Matt Arsenault0202fa32019-10-30 14:01:58 -0700206 LiveRegMap::const_iterator findLiveVirtReg(Register VirtReg) const {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000207 return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000208 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000209
Matt Arsenault0202fa32019-10-30 14:01:58 -0700210 void allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint);
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000211 void allocVirtRegUndef(MachineOperand &MO);
Matt Arsenault0202fa32019-10-30 14:01:58 -0700212 MCPhysReg defineVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
213 Register Hint);
214 LiveReg &reloadVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
215 Register Hint);
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000216 void spillAll(MachineBasicBlock::iterator MI, bool OnlyLiveOut);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000217 bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000218
Matt Arsenault0202fa32019-10-30 14:01:58 -0700219 Register traceCopies(Register VirtReg) const;
220 Register traceCopyChain(Register Reg) const;
Matt Arsenault828b6852019-05-16 12:50:39 +0000221
Matt Arsenault0202fa32019-10-30 14:01:58 -0700222 int getStackSpaceFor(Register VirtReg);
223 void spill(MachineBasicBlock::iterator Before, Register VirtReg,
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000224 MCPhysReg AssignedReg, bool Kill);
Matt Arsenault0202fa32019-10-30 14:01:58 -0700225 void reload(MachineBasicBlock::iterator Before, Register VirtReg,
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000226 MCPhysReg PhysReg);
227
Matt Arsenault0202fa32019-10-30 14:01:58 -0700228 bool mayLiveOut(Register VirtReg);
229 bool mayLiveIn(Register VirtReg);
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000230
Matthias Braun864cf582017-09-09 00:52:46 +0000231 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000232 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000233
234} // end anonymous namespace
235
236char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000237
Matthias Braun864cf582017-09-09 00:52:46 +0000238INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
239 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000240
Matthias Braun0804dca2018-11-07 06:57:00 +0000241void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
242 PhysRegState[PhysReg] = NewState;
243}
244
Matthias Braun864cf582017-09-09 00:52:46 +0000245/// This allocates space for the specified virtual register to be held on the
246/// stack.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700247int RegAllocFast::getStackSpaceFor(Register VirtReg) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000248 // Find the location Reg would belong...
249 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000250 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000251 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000252 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000253
254 // Allocate a new stack object for this spill location...
Matthias Braunebcf5432018-11-07 02:04:11 +0000255 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braun864cf582017-09-09 00:52:46 +0000256 unsigned Size = TRI->getSpillSize(RC);
257 unsigned Align = TRI->getSpillAlignment(RC);
258 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000259
260 // Assign the slot.
261 StackSlotForVirtReg[VirtReg] = FrameIdx;
262 return FrameIdx;
263}
264
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000265/// Returns false if \p VirtReg is known to not live out of the current block.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700266bool RegAllocFast::mayLiveOut(Register VirtReg) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000267 if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) {
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000268 // Cannot be live-out if there are no successors.
269 return !MBB->succ_empty();
270 }
271
272 // If this block loops back to itself, it would be necessary to check whether
273 // the use comes after the def.
Matt Arsenaultca84c4b2019-05-27 20:37:31 +0000274 if (MBB->isSuccessor(MBB)) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000275 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000276 return true;
Matt Arsenaultca84c4b2019-05-27 20:37:31 +0000277 }
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000278
279 // See if the first \p Limit uses of the register are all in the current
280 // block.
281 static const unsigned Limit = 8;
282 unsigned C = 0;
283 for (const MachineInstr &UseInst : MRI->reg_nodbg_instructions(VirtReg)) {
284 if (UseInst.getParent() != MBB || ++C >= Limit) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000285 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000286 // Cannot be live-out if there are no successors.
287 return !MBB->succ_empty();
288 }
289 }
290
291 return false;
292}
293
Matt Arsenaultca84c4b2019-05-27 20:37:31 +0000294/// Returns false if \p VirtReg is known to not be live into the current block.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700295bool RegAllocFast::mayLiveIn(Register VirtReg) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000296 if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg)))
Matt Arsenaultca84c4b2019-05-27 20:37:31 +0000297 return !MBB->pred_empty();
298
299 // See if the first \p Limit def of the register are all in the current block.
300 static const unsigned Limit = 8;
301 unsigned C = 0;
302 for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) {
303 if (DefInst.getParent() != MBB || ++C >= Limit) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000304 MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
Matt Arsenaultca84c4b2019-05-27 20:37:31 +0000305 return !MBB->pred_empty();
306 }
307 }
308
309 return false;
310}
311
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000312/// Insert spill instruction for \p AssignedReg before \p Before. Update
313/// DBG_VALUEs with \p VirtReg operands with the stack slot.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700314void RegAllocFast::spill(MachineBasicBlock::iterator Before, Register VirtReg,
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000315 MCPhysReg AssignedReg, bool Kill) {
316 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
317 << " in " << printReg(AssignedReg, TRI));
318 int FI = getStackSpaceFor(VirtReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000319 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000320
321 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
322 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
323 ++NumStores;
324
325 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
326 // identify spilled location as the place to find corresponding variable's
327 // value.
328 SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
329 for (MachineInstr *DBG : LRIDbgValues) {
330 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
331 assert(NewDV->getParent() == MBB && "dangling parent pointer");
332 (void)NewDV;
333 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
334 }
335 // Now this register is spilled there is should not be any DBG_VALUE
336 // pointing to this register because they are all pointing to spilled value
337 // now.
338 LRIDbgValues.clear();
339}
340
341/// Insert reload instruction for \p PhysReg before \p Before.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700342void RegAllocFast::reload(MachineBasicBlock::iterator Before, Register VirtReg,
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000343 MCPhysReg PhysReg) {
344 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000345 << printReg(PhysReg, TRI) << '\n');
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000346 int FI = getStackSpaceFor(VirtReg);
347 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
348 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
349 ++NumLoads;
350}
351
Matthias Braun864cf582017-09-09 00:52:46 +0000352/// Return true if MO is the only remaining reference to its virtual register,
353/// and it is guaranteed to be a block-local register.
354bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000355 // If the register has ever been spilled or reloaded, we conservatively assume
356 // it is a global register used in multiple blocks.
357 if (StackSlotForVirtReg[MO.getReg()] != -1)
358 return false;
359
360 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000361 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000362 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000363 return false;
364 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000365}
366
Matthias Braun864cf582017-09-09 00:52:46 +0000367/// Set kill flags on last use of a virtual register.
368void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000369 if (!LR.LastUse) return;
370 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000371 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
372 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000373 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000374 // else, don't do anything we are problably redefining a
375 // subreg of this register and given we don't track which
376 // lanes are actually dead, we cannot insert a kill flag here.
377 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000378 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet868ef842017-07-07 19:25:45 +0000379 // ... <== Here we would allow later pass to reuse physreg:sub1
380 // which is potentially wrong.
381 // LR:sub0 = ...
382 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000383 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000384}
385
Matthias Braun864cf582017-09-09 00:52:46 +0000386/// Mark virtreg as no longer available.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000387void RegAllocFast::killVirtReg(LiveReg &LR) {
388 addKillFlag(LR);
389 assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000390 "Broken RegState mapping");
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000391 setPhysRegState(LR.PhysReg, regFree);
392 LR.PhysReg = 0;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000393}
394
Matthias Braun864cf582017-09-09 00:52:46 +0000395/// Mark virtreg as no longer available.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700396void RegAllocFast::killVirtReg(Register VirtReg) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000397 assert(Register::isVirtualRegister(VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000398 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000399 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000400 if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
401 killVirtReg(*LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000402}
403
Matthias Braun864cf582017-09-09 00:52:46 +0000404/// This method spills the value specified by VirtReg into the corresponding
405/// stack slot if needed.
406void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
Matt Arsenault0202fa32019-10-30 14:01:58 -0700407 Register VirtReg) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000408 assert(Register::isVirtualRegister(VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000409 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000410 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000411 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
412 "Spilling unmapped virtual register");
413 spillVirtReg(MI, *LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000414}
415
Matthias Braun864cf582017-09-09 00:52:46 +0000416/// Do the actual work of spilling.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000417void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR) {
418 assert(PhysRegState[LR.PhysReg] == LR.VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000419
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000420 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000421 // If this physreg is used by the instruction, we want to kill it on the
422 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000423 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000424 LR.Dirty = false;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000425
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000426 spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000427
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000428 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000429 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000430 }
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000431 killVirtReg(LR);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000432}
433
Matthias Braun864cf582017-09-09 00:52:46 +0000434/// Spill all dirty virtregs without killing them.
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000435void RegAllocFast::spillAll(MachineBasicBlock::iterator MI, bool OnlyLiveOut) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000436 if (LiveVirtRegs.empty())
437 return;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000438 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
439 // of spilling here is deterministic, if arbitrary.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000440 for (LiveReg &LR : LiveVirtRegs) {
441 if (!LR.PhysReg)
442 continue;
Matt Arsenaultb6c599a2019-05-03 19:06:57 +0000443 if (OnlyLiveOut && !mayLiveOut(LR.VirtReg))
444 continue;
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000445 spillVirtReg(MI, LR);
446 }
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000447 LiveVirtRegs.clear();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000448}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000449
Matthias Braun864cf582017-09-09 00:52:46 +0000450/// Handle the direct use of a physical register. Check that the register is
451/// not used by a virtreg. Kill the physreg, marking it free. This may add
452/// implicit kills to MO->getParent() and invalidate MO.
453void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000454 // Ignore undef uses.
455 if (MO.isUndef())
456 return;
457
Daniel Sanders0c476112019-08-15 19:22:08 +0000458 Register PhysReg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -0700459 assert(PhysReg.isPhysical() && "Bad usePhysReg operand");
Matthias Braun864cf582017-09-09 00:52:46 +0000460
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000461 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000462 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000463 case regDisabled:
464 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000465 case regReserved:
466 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000467 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000468 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000469 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000470 return;
471 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000472 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000473 // wanted has been clobbered.
474 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000475 }
476
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000477 // Maybe a superregister is reserved?
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 Olesen4d5c1062010-05-14 18:03:25 +0000480 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000481 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000482 break;
483 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000484 // Either PhysReg is a subregister of Alias and we mark the
485 // whole register as free, or PhysReg is the superregister of
486 // Alias and we mark all the aliases as disabled before freeing
487 // PhysReg.
488 // In the latter case, since PhysReg was disabled, this means that
489 // its value is defined only by physical sub-registers. This check
490 // is performed by the assert of the default case in this loop.
491 // Note: The value of the superregister may only be partial
492 // defined, that is why regDisabled is a valid state for aliases.
493 assert((TRI->isSuperRegister(PhysReg, Alias) ||
494 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000495 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000496 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000497 case regFree:
498 if (TRI->isSuperRegister(PhysReg, Alias)) {
499 // Leave the superregister in the working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000500 setPhysRegState(Alias, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000501 MO.getParent()->addRegisterKilled(Alias, TRI, true);
502 return;
503 }
504 // Some other alias was in the working set - clear it.
Matthias Braun0804dca2018-11-07 06:57:00 +0000505 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000506 break;
507 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000508 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000509 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000510 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000511
512 // All aliases are disabled, bring register into working set.
Matthias Braun0804dca2018-11-07 06:57:00 +0000513 setPhysRegState(PhysReg, regFree);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000514 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000515}
516
Matthias Braun864cf582017-09-09 00:52:46 +0000517/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
518/// similar to defineVirtReg except the physreg is reserved instead of
519/// allocated.
Quentin Colombet72f6d592018-01-29 23:42:37 +0000520void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
521 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000522 markRegUsedInInstr(PhysReg);
Matt Arsenault0202fa32019-10-30 14:01:58 -0700523 switch (Register VirtReg = PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000524 case regDisabled:
525 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000526 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000527 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000528 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000529 case regFree:
530 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000531 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000532 return;
533 }
534
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000535 // This is a disabled register, disable all aliases.
Matthias Braun0804dca2018-11-07 06:57:00 +0000536 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000537 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000538 MCPhysReg Alias = *AI;
Matt Arsenault0202fa32019-10-30 14:01:58 -0700539 switch (Register VirtReg = PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000540 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000541 break;
542 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000543 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000544 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000545 case regFree:
546 case regReserved:
Matthias Braun0804dca2018-11-07 06:57:00 +0000547 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000548 if (TRI->isSuperRegister(PhysReg, Alias))
549 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000550 break;
551 }
552 }
553}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000554
Matthias Braunfb93aec2018-11-10 00:36:27 +0000555/// Return the cost of spilling clearing out PhysReg and aliases so it is free
556/// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
557/// disabled - it can be allocated directly.
Matthias Braun864cf582017-09-09 00:52:46 +0000558/// \returns spillImpossible when PhysReg or an alias can't be spilled.
559unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000560 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000561 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
562 << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000563 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000564 }
Matt Arsenault0202fa32019-10-30 14:01:58 -0700565 switch (Register VirtReg = PhysRegState[PhysReg]) {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000566 case regDisabled:
567 break;
568 case regFree:
569 return 0;
570 case regReserved:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000571 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
572 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000573 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000574 default: {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000575 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
576 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
577 "Missing VirtReg entry");
578 return LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000579 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000580 }
581
Eric Christopherc3783362011-04-12 00:48:08 +0000582 // This is a disabled register, add up cost of aliases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000583 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000584 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000585 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000586 MCPhysReg Alias = *AI;
Matt Arsenault0202fa32019-10-30 14:01:58 -0700587 switch (Register VirtReg = PhysRegState[Alias]) {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000588 case regDisabled:
589 break;
590 case regFree:
591 ++Cost;
592 break;
593 case regReserved:
594 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000595 default: {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000596 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
597 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
598 "Missing VirtReg entry");
599 Cost += LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000600 break;
601 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000602 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000603 }
604 return Cost;
605}
606
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000607/// This method updates local state so that we know that PhysReg is the
Matthias Braun864cf582017-09-09 00:52:46 +0000608/// proper container for VirtReg now. The physical register must not be used
609/// for anything else when this is called.
610void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Matt Arsenault0202fa32019-10-30 14:01:58 -0700611 Register VirtReg = LR.VirtReg;
Matthias Braun0804dca2018-11-07 06:57:00 +0000612 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000613 << printReg(PhysReg, TRI) << '\n');
Matthias Braun0804dca2018-11-07 06:57:00 +0000614 assert(LR.PhysReg == 0 && "Already assigned a physreg");
615 assert(PhysReg != 0 && "Trying to assign no register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000616 LR.PhysReg = PhysReg;
Matthias Braun0804dca2018-11-07 06:57:00 +0000617 setPhysRegState(PhysReg, VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000618}
619
Matt Arsenault828b6852019-05-16 12:50:39 +0000620static bool isCoalescable(const MachineInstr &MI) {
621 return MI.isFullCopy();
622}
623
Matt Arsenault0202fa32019-10-30 14:01:58 -0700624Register RegAllocFast::traceCopyChain(Register Reg) const {
Matt Arsenault828b6852019-05-16 12:50:39 +0000625 static const unsigned ChainLengthLimit = 3;
626 unsigned C = 0;
627 do {
Matt Arsenault0202fa32019-10-30 14:01:58 -0700628 if (Reg.isPhysical())
Matt Arsenault828b6852019-05-16 12:50:39 +0000629 return Reg;
Matt Arsenault0202fa32019-10-30 14:01:58 -0700630 assert(Reg.isVirtual());
Matt Arsenault828b6852019-05-16 12:50:39 +0000631
632 MachineInstr *VRegDef = MRI->getUniqueVRegDef(Reg);
633 if (!VRegDef || !isCoalescable(*VRegDef))
634 return 0;
635 Reg = VRegDef->getOperand(1).getReg();
636 } while (++C <= ChainLengthLimit);
637 return 0;
638}
639
640/// Check if any of \p VirtReg's definitions is a copy. If it is follow the
641/// chain of copies to check whether we reach a physical register we can
642/// coalesce with.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700643Register RegAllocFast::traceCopies(Register VirtReg) const {
Matt Arsenault828b6852019-05-16 12:50:39 +0000644 static const unsigned DefLimit = 3;
645 unsigned C = 0;
646 for (const MachineInstr &MI : MRI->def_instructions(VirtReg)) {
647 if (isCoalescable(MI)) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000648 Register Reg = MI.getOperand(1).getReg();
Matt Arsenault828b6852019-05-16 12:50:39 +0000649 Reg = traceCopyChain(Reg);
Matt Arsenault0202fa32019-10-30 14:01:58 -0700650 if (Reg.isValid())
Matt Arsenault828b6852019-05-16 12:50:39 +0000651 return Reg;
652 }
653
654 if (++C >= DefLimit)
655 break;
656 }
Matt Arsenault0202fa32019-10-30 14:01:58 -0700657 return Register();
Matt Arsenault828b6852019-05-16 12:50:39 +0000658}
659
Matthias Braun864cf582017-09-09 00:52:46 +0000660/// Allocates a physical register for VirtReg.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700661void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint0) {
662 const Register VirtReg = LR.VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000663
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000664 assert(Register::isVirtualRegister(VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000665 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000666
Matthias Braun864cf582017-09-09 00:52:46 +0000667 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000668 LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
Matt Arsenault884a18d2019-03-17 21:31:40 +0000669 << " in class " << TRI->getRegClassName(&RC)
Matt Arsenault828b6852019-05-16 12:50:39 +0000670 << " with hint " << printReg(Hint0, TRI) << '\n');
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000671
672 // Take hint when possible.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700673 if (Hint0.isPhysical() && MRI->isAllocatable(Hint0) &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000674 RC.contains(Hint0)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000675 // Ignore the hint if we would have to spill a dirty register.
Matt Arsenault828b6852019-05-16 12:50:39 +0000676 unsigned Cost = calcSpillCost(Hint0);
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000677 if (Cost < spillDirty) {
Matt Arsenault828b6852019-05-16 12:50:39 +0000678 LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
679 << '\n');
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000680 if (Cost)
Matt Arsenault828b6852019-05-16 12:50:39 +0000681 definePhysReg(MI, Hint0, regFree);
682 assignVirtToPhysReg(LR, Hint0);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000683 return;
Matt Arsenault828b6852019-05-16 12:50:39 +0000684 } else {
685 LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
686 << "occupied\n");
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000687 }
Matt Arsenault828b6852019-05-16 12:50:39 +0000688 } else {
Matt Arsenault0202fa32019-10-30 14:01:58 -0700689 Hint0 = Register();
Matt Arsenault828b6852019-05-16 12:50:39 +0000690 }
691
692 // Try other hint.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700693 Register Hint1 = traceCopies(VirtReg);
694 if (Hint1.isPhysical() && MRI->isAllocatable(Hint1) &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000695 RC.contains(Hint1) && !isRegUsedInInstr(Hint1)) {
Matt Arsenault828b6852019-05-16 12:50:39 +0000696 // Ignore the hint if we would have to spill a dirty register.
697 unsigned Cost = calcSpillCost(Hint1);
698 if (Cost < spillDirty) {
699 LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
700 << '\n');
701 if (Cost)
702 definePhysReg(MI, Hint1, regFree);
703 assignVirtToPhysReg(LR, Hint1);
704 return;
705 } else {
706 LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
707 << "occupied\n");
708 }
709 } else {
Matt Arsenault0202fa32019-10-30 14:01:58 -0700710 Hint1 = Register();
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000711 }
712
Matthias Braunfb93aec2018-11-10 00:36:27 +0000713 MCPhysReg BestReg = 0;
Matthias Braun864cf582017-09-09 00:52:46 +0000714 unsigned BestCost = spillImpossible;
Matt Arsenaultc2e35a62019-03-19 19:01:34 +0000715 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000716 for (MCPhysReg PhysReg : AllocationOrder) {
717 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
Matthias Braun864cf582017-09-09 00:52:46 +0000718 unsigned Cost = calcSpillCost(PhysReg);
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000719 LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
Matthias Braunfb93aec2018-11-10 00:36:27 +0000720 // Immediate take a register with cost 0.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000721 if (Cost == 0) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000722 assignVirtToPhysReg(LR, PhysReg);
723 return;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000724 }
Matt Arsenault828b6852019-05-16 12:50:39 +0000725
726 if (PhysReg == Hint1 || PhysReg == Hint0)
727 Cost -= spillPrefBonus;
728
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000729 if (Cost < BestCost) {
730 BestReg = PhysReg;
731 BestCost = Cost;
732 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000733 }
734
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000735 if (!BestReg) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000736 // Nothing we can do: Report an error and keep going with an invalid
737 // allocation.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000738 if (MI.isInlineAsm())
739 MI.emitError("inline assembly requires more registers than available");
740 else
741 MI.emitError("ran out of registers during register allocation");
742 definePhysReg(MI, *AllocationOrder.begin(), regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000743 assignVirtToPhysReg(LR, *AllocationOrder.begin());
744 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000745 }
746
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000747 definePhysReg(MI, BestReg, regFree);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000748 assignVirtToPhysReg(LR, BestReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000749}
750
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000751void RegAllocFast::allocVirtRegUndef(MachineOperand &MO) {
752 assert(MO.isUndef() && "expected undef use");
Daniel Sanders0c476112019-08-15 19:22:08 +0000753 Register VirtReg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000754 assert(Register::isVirtualRegister(VirtReg) && "Expected virtreg");
Matt Arsenault3c98cdd22019-03-19 19:16:04 +0000755
756 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
757 MCPhysReg PhysReg;
758 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
759 PhysReg = LRI->PhysReg;
760 } else {
761 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
762 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
763 assert(!AllocationOrder.empty() && "Allocation order must not be empty");
764 PhysReg = AllocationOrder[0];
765 }
766
767 unsigned SubRegIdx = MO.getSubReg();
768 if (SubRegIdx != 0) {
769 PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
770 MO.setSubReg(0);
771 }
772 MO.setReg(PhysReg);
773 MO.setIsRenamable(true);
774}
775
Matthias Braun864cf582017-09-09 00:52:46 +0000776/// Allocates a register for VirtReg and mark it as dirty.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000777MCPhysReg RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
Matt Arsenault0202fa32019-10-30 14:01:58 -0700778 Register VirtReg, Register Hint) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000779 assert(Register::isVirtualRegister(VirtReg) && "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000780 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000781 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000782 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000783 if (!LRI->PhysReg) {
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000784 // If there is no hint, peek at the only use of this register.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700785 if ((!Hint || !Hint.isPhysical()) &&
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000786 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000787 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000788 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000789 if (UseMI.isCopyLike())
790 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000791 }
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000792 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000793 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000794 // Redefining a live register - kill at the last use, unless it is this
795 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000796 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000797 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000798 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000799 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000800 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000801 LRI->LastOpNum = OpNum;
802 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000803 markRegUsedInInstr(LRI->PhysReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000804 return LRI->PhysReg;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000805}
806
Matthias Braun864cf582017-09-09 00:52:46 +0000807/// Make sure VirtReg is available in a physreg and return it.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000808RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(MachineInstr &MI,
809 unsigned OpNum,
Matt Arsenault0202fa32019-10-30 14:01:58 -0700810 Register VirtReg,
811 Register Hint) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000812 assert(Register::isVirtualRegister(VirtReg) && "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000813 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000814 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000815 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000816 MachineOperand &MO = MI.getOperand(OpNum);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000817 if (!LRI->PhysReg) {
818 allocVirtReg(MI, *LRI, Hint);
Matthias Braunb4c76ff72018-11-07 02:04:12 +0000819 reload(MI, VirtReg, LRI->PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000820 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000821 if (isLastUseOfLocalReg(MO)) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000822 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000823 if (MO.isUse())
824 MO.setIsKill();
825 else
826 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000827 } else if (MO.isKill()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000828 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << '\n');
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000829 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000830 } else if (MO.isDead()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000831 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000832 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000833 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000834 } else if (MO.isKill()) {
835 // We must remove kill flags from uses of reloaded registers because the
836 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000837 // %foo = OR killed %x, %x
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000838 // This would cause a second reload of %x into a different register.
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000839 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << '\n');
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000840 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000841 } else if (MO.isDead()) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000842 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000843 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000844 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000845 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000846 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000847 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000848 markRegUsedInInstr(LRI->PhysReg);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000849 return *LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000850}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000851
Matthias Braun864cf582017-09-09 00:52:46 +0000852/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
853/// may invalidate any operand pointers. Return true if the operand kills its
854/// register.
Matthias Braunfb93aec2018-11-10 00:36:27 +0000855bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
Matthias Braun864cf582017-09-09 00:52:46 +0000856 MCPhysReg PhysReg) {
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000857 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000858 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000859 MO.setReg(PhysReg);
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000860 MO.setIsRenamable(true);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000861 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000862 }
863
864 // Handle subregister index.
Daniel Sanderse7694f32019-08-02 20:23:00 +0000865 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : Register());
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000866 MO.setIsRenamable(true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000867 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000868
869 // A kill flag implies killing the full register. Add corresponding super
870 // register kill.
871 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000872 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000873 return true;
874 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000875
876 // A <def,read-undef> of a sub-register requires an implicit def of the full
877 // register.
878 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000879 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000880
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000881 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000882}
883
Matthias Braun864cf582017-09-09 00:52:46 +0000884// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000885// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000886void RegAllocFast::handleThroughOperands(MachineInstr &MI,
Matt Arsenault0202fa32019-10-30 14:01:58 -0700887 SmallVectorImpl<Register> &VirtDead) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000888 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Matt Arsenault0202fa32019-10-30 14:01:58 -0700889 SmallSet<Register, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000890 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000891 if (!MO.isReg()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000892 Register Reg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -0700893 if (!Reg.isVirtual())
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000894 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000895 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
896 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000897 if (ThroughRegs.insert(Reg).second)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000898 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000899 }
900 }
901
902 // If any physreg defines collide with preallocated through registers,
903 // we must spill and reallocate.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000904 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000905 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000906 if (!MO.isReg() || !MO.isDef()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000907 Register Reg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -0700908 if (!Reg || !Reg.isPhysical())
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000909 continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000910 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000911 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000912 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000913 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000914 }
915 }
916
Matt Arsenault0202fa32019-10-30 14:01:58 -0700917 SmallVector<Register, 8> PartialDefs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000918 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000919 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
Matthias Braunfb93aec2018-11-10 00:36:27 +0000920 MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000921 if (!MO.isReg()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000922 Register Reg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000923 if (!Register::isVirtualRegister(Reg))
924 continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000925 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000926 if (!MO.isTied()) continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000927 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
928 << ") is tied to operand " << MI.findTiedOperandIdx(I)
929 << ".\n");
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000930 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
931 MCPhysReg PhysReg = LR.PhysReg;
Matthias Braunfb93aec2018-11-10 00:36:27 +0000932 setPhysReg(MI, MO, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000933 // Note: we don't update the def operand yet. That would cause the normal
934 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000935 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Matthias Braunb0ecbef2018-11-07 06:57:02 +0000936 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << '\n');
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000937 // Reload the register, but don't assign to the operand just yet.
938 // That would confuse the later phys-def processing pass.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000939 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
940 PartialDefs.push_back(LR.PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000941 }
942 }
943
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000944 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000945 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
946 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000947 if (!MO.isReg()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000948 Register Reg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000949 if (!Register::isVirtualRegister(Reg))
950 continue;
Rafael Espindola2021f382011-11-22 06:27:18 +0000951 if (!MO.isEarlyClobber())
952 continue;
953 // Note: defineVirtReg may invalidate MO.
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000954 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, 0);
Matthias Braunfb93aec2018-11-10 00:36:27 +0000955 if (setPhysReg(MI, MI.getOperand(I), PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000956 VirtDead.push_back(Reg);
957 }
958
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000959 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000960 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000961 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000962 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000963 Register Reg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -0700964 if (!Reg || !Reg.isPhysical())
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000965 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000966 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
967 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000968 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000969 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000970
971 // Also mark PartialDefs as used to avoid reallocation.
Matt Arsenault0202fa32019-10-30 14:01:58 -0700972 for (Register PartialDef : PartialDefs)
Matthias Braun864cf582017-09-09 00:52:46 +0000973 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000974}
975
Matthias Braun864cf582017-09-09 00:52:46 +0000976#ifndef NDEBUG
977void RegAllocFast::dumpState() {
978 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
979 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000980 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun864cf582017-09-09 00:52:46 +0000981 switch(PhysRegState[Reg]) {
982 case regFree:
983 break;
984 case regReserved:
985 dbgs() << "*";
986 break;
987 default: {
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000988 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000989 LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
990 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
991 "Missing VirtReg entry");
992 if (LRI->Dirty)
Matthias Braun864cf582017-09-09 00:52:46 +0000993 dbgs() << "*";
Matthias Braun5b7c90b2018-11-07 06:57:03 +0000994 assert(LRI->PhysReg == Reg && "Bad inverse map");
Matthias Braun864cf582017-09-09 00:52:46 +0000995 break;
996 }
997 }
998 }
999 dbgs() << '\n';
1000 // Check that LiveVirtRegs is the inverse.
1001 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
1002 e = LiveVirtRegs.end(); i != e; ++i) {
Matthias Braun5b7c90b2018-11-07 06:57:03 +00001003 if (!i->PhysReg)
1004 continue;
Matt Arsenault0202fa32019-10-30 14:01:58 -07001005 assert(i->VirtReg.isVirtual() && "Bad map key");
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001006 assert(Register::isPhysicalRegister(i->PhysReg) && "Bad map value");
Matthias Braun864cf582017-09-09 00:52:46 +00001007 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
1008 }
1009}
1010#endif
1011
Matthias Braunfb93aec2018-11-10 00:36:27 +00001012void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1013 const MCInstrDesc &MCID = MI.getDesc();
1014
1015 // If this is a copy, we may be able to coalesce.
Matt Arsenault0202fa32019-10-30 14:01:58 -07001016 Register CopySrcReg;
1017 Register CopyDstReg;
Matthias Braunfb93aec2018-11-10 00:36:27 +00001018 unsigned CopySrcSub = 0;
1019 unsigned CopyDstSub = 0;
1020 if (MI.isCopy()) {
1021 CopyDstReg = MI.getOperand(0).getReg();
1022 CopySrcReg = MI.getOperand(1).getReg();
1023 CopyDstSub = MI.getOperand(0).getSubReg();
1024 CopySrcSub = MI.getOperand(1).getSubReg();
1025 }
1026
1027 // Track registers used by instruction.
1028 UsedInInstr.clear();
1029
1030 // First scan.
1031 // Mark physreg uses and early clobbers as used.
1032 // Find the end of the virtreg operands
1033 unsigned VirtOpEnd = 0;
1034 bool hasTiedOps = false;
1035 bool hasEarlyClobbers = false;
1036 bool hasPartialRedefs = false;
1037 bool hasPhysDefs = false;
1038 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1039 MachineOperand &MO = MI.getOperand(i);
1040 // Make sure MRI knows about registers clobbered by regmasks.
1041 if (MO.isRegMask()) {
1042 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
1043 continue;
1044 }
1045 if (!MO.isReg()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +00001046 Register Reg = MO.getReg();
Matthias Braunfb93aec2018-11-10 00:36:27 +00001047 if (!Reg) continue;
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001048 if (Register::isVirtualRegister(Reg)) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001049 VirtOpEnd = i+1;
1050 if (MO.isUse()) {
1051 hasTiedOps = hasTiedOps ||
1052 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
1053 } else {
1054 if (MO.isEarlyClobber())
1055 hasEarlyClobbers = true;
1056 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
1057 hasPartialRedefs = true;
1058 }
1059 continue;
1060 }
1061 if (!MRI->isAllocatable(Reg)) continue;
1062 if (MO.isUse()) {
1063 usePhysReg(MO);
1064 } else if (MO.isEarlyClobber()) {
1065 definePhysReg(MI, Reg,
1066 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
1067 hasEarlyClobbers = true;
1068 } else
1069 hasPhysDefs = true;
1070 }
1071
1072 // The instruction may have virtual register operands that must be allocated
1073 // the same register at use-time and def-time: early clobbers and tied
1074 // operands. If there are also physical defs, these registers must avoid
1075 // both physical defs and uses, making them more constrained than normal
1076 // operands.
1077 // Similarly, if there are multiple defs and tied operands, we must make
1078 // sure the same register is allocated to uses and defs.
1079 // We didn't detect inline asm tied operands above, so just make this extra
1080 // pass for all inline asm.
1081 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
1082 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
1083 handleThroughOperands(MI, VirtDead);
1084 // Don't attempt coalescing when we have funny stuff going on.
Matt Arsenault0202fa32019-10-30 14:01:58 -07001085 CopyDstReg = Register();
Matthias Braunfb93aec2018-11-10 00:36:27 +00001086 // Pretend we have early clobbers so the use operands get marked below.
1087 // This is not necessary for the common case of a single tied use.
1088 hasEarlyClobbers = true;
1089 }
1090
1091 // Second scan.
1092 // Allocate virtreg uses.
Matt Arsenault3c98cdd22019-03-19 19:16:04 +00001093 bool HasUndefUse = false;
Matthias Braunfb93aec2018-11-10 00:36:27 +00001094 for (unsigned I = 0; I != VirtOpEnd; ++I) {
1095 MachineOperand &MO = MI.getOperand(I);
1096 if (!MO.isReg()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +00001097 Register Reg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -07001098 if (!Reg.isVirtual())
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001099 continue;
Matthias Braunfb93aec2018-11-10 00:36:27 +00001100 if (MO.isUse()) {
Matt Arsenault3c98cdd22019-03-19 19:16:04 +00001101 if (MO.isUndef()) {
1102 HasUndefUse = true;
1103 // There is no need to allocate a register for an undef use.
1104 continue;
1105 }
Matt Arsenaultca84c4b2019-05-27 20:37:31 +00001106
1107 // Populate MayLiveAcrossBlocks in case the use block is allocated before
1108 // the def block (removing the vreg uses).
1109 mayLiveIn(Reg);
1110
Matthias Braunfb93aec2018-11-10 00:36:27 +00001111 LiveReg &LR = reloadVirtReg(MI, I, Reg, CopyDstReg);
1112 MCPhysReg PhysReg = LR.PhysReg;
1113 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
1114 if (setPhysReg(MI, MO, PhysReg))
1115 killVirtReg(LR);
1116 }
1117 }
1118
Matt Arsenault3c98cdd22019-03-19 19:16:04 +00001119 // Allocate undef operands. This is a separate step because in a situation
1120 // like ` = OP undef %X, %X` both operands need the same register assign
1121 // so we should perform the normal assignment first.
1122 if (HasUndefUse) {
1123 for (MachineOperand &MO : MI.uses()) {
1124 if (!MO.isReg() || !MO.isUse())
1125 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +00001126 Register Reg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -07001127 if (!Reg.isVirtual())
Matt Arsenault3c98cdd22019-03-19 19:16:04 +00001128 continue;
1129
1130 assert(MO.isUndef() && "Should only have undef virtreg uses left");
1131 allocVirtRegUndef(MO);
1132 }
1133 }
1134
Matthias Braunfb93aec2018-11-10 00:36:27 +00001135 // Track registers defined by instruction - early clobbers and tied uses at
1136 // this point.
1137 UsedInInstr.clear();
1138 if (hasEarlyClobbers) {
1139 for (const MachineOperand &MO : MI.operands()) {
1140 if (!MO.isReg()) continue;
Daniel Sanders0c476112019-08-15 19:22:08 +00001141 Register Reg = MO.getReg();
Matt Arsenault0202fa32019-10-30 14:01:58 -07001142 if (!Reg || !Reg.isPhysical())
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001143 continue;
Matthias Braunfb93aec2018-11-10 00:36:27 +00001144 // Look for physreg defs and tied uses.
1145 if (!MO.isDef() && !MO.isTied()) continue;
1146 markRegUsedInInstr(Reg);
1147 }
1148 }
1149
1150 unsigned DefOpEnd = MI.getNumOperands();
1151 if (MI.isCall()) {
1152 // Spill all virtregs before a call. This serves one purpose: If an
1153 // exception is thrown, the landing pad is going to expect to find
1154 // registers in their spill slots.
1155 // Note: although this is appealing to just consider all definitions
1156 // as call-clobbered, this is not correct because some of those
1157 // definitions may be used later on and we do not want to reuse
1158 // those for virtual registers in between.
1159 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
Matt Arsenaultb6c599a2019-05-03 19:06:57 +00001160 spillAll(MI, /*OnlyLiveOut*/ false);
Matthias Braunfb93aec2018-11-10 00:36:27 +00001161 }
1162
1163 // Third scan.
Quentin Colombet15742722019-05-08 18:30:26 +00001164 // Mark all physreg defs as used before allocating virtreg defs.
1165 for (unsigned I = 0; I != DefOpEnd; ++I) {
1166 const MachineOperand &MO = MI.getOperand(I);
1167 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1168 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +00001169 Register Reg = MO.getReg();
Quentin Colombet15742722019-05-08 18:30:26 +00001170
Matt Arsenault0202fa32019-10-30 14:01:58 -07001171 if (!Reg || !Reg.isPhysical() || !MRI->isAllocatable(Reg))
Quentin Colombet15742722019-05-08 18:30:26 +00001172 continue;
1173 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
1174 }
1175
1176 // Fourth scan.
Matthias Braunfb93aec2018-11-10 00:36:27 +00001177 // Allocate defs and collect dead defs.
1178 for (unsigned I = 0; I != DefOpEnd; ++I) {
1179 const MachineOperand &MO = MI.getOperand(I);
1180 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1181 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +00001182 Register Reg = MO.getReg();
Matthias Braunfb93aec2018-11-10 00:36:27 +00001183
Quentin Colombet15742722019-05-08 18:30:26 +00001184 // We have already dealt with phys regs in the previous scan.
Matt Arsenault0202fa32019-10-30 14:01:58 -07001185 if (Reg.isPhysical())
Matthias Braunfb93aec2018-11-10 00:36:27 +00001186 continue;
Matthias Braunfb93aec2018-11-10 00:36:27 +00001187 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
1188 if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
1189 VirtDead.push_back(Reg);
Matt Arsenault0202fa32019-10-30 14:01:58 -07001190 CopyDstReg = Register(); // cancel coalescing;
Matthias Braunfb93aec2018-11-10 00:36:27 +00001191 } else
1192 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1193 }
1194
1195 // Kill dead defs after the scan to ensure that multiple defs of the same
1196 // register are allocated identically. We didn't need to do this for uses
1197 // because we are crerating our own kill flags, and they are always at the
1198 // last use.
Matt Arsenault0202fa32019-10-30 14:01:58 -07001199 for (Register VirtReg : VirtDead)
Matthias Braunfb93aec2018-11-10 00:36:27 +00001200 killVirtReg(VirtReg);
1201 VirtDead.clear();
1202
1203 LLVM_DEBUG(dbgs() << "<< " << MI);
1204 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1205 LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1206 Coalesced.push_back(&MI);
1207 }
1208}
1209
1210void RegAllocFast::handleDebugValue(MachineInstr &MI) {
1211 MachineOperand &MO = MI.getOperand(0);
1212
1213 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1214 // mostly constants and frame indices.
1215 if (!MO.isReg())
1216 return;
Daniel Sanders0c476112019-08-15 19:22:08 +00001217 Register Reg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001218 if (!Register::isVirtualRegister(Reg))
Matthias Braunfb93aec2018-11-10 00:36:27 +00001219 return;
1220
1221 // See if this virtual register has already been allocated to a physical
1222 // register or spilled to a stack slot.
1223 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1224 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1225 setPhysReg(MI, MO, LRI->PhysReg);
1226 } else {
1227 int SS = StackSlotForVirtReg[Reg];
1228 if (SS != -1) {
1229 // Modify DBG_VALUE now that the value is in a spill slot.
1230 updateDbgValueForSpill(MI, SS);
1231 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << MI);
1232 return;
1233 }
1234
1235 // We can't allocate a physreg for a DebugValue, sorry!
1236 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Matt Arsenault0202fa32019-10-30 14:01:58 -07001237 MO.setReg(Register());
Matthias Braunfb93aec2018-11-10 00:36:27 +00001238 }
1239
1240 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1241 // that future spills of Reg will have DBG_VALUEs.
1242 LiveDbgValueMap[Reg].push_back(&MI);
1243}
1244
Matthias Braun864cf582017-09-09 00:52:46 +00001245void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1246 this->MBB = &MBB;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001247 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001248
1249 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001250 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001251
Matthias Braun864cf582017-09-09 00:52:46 +00001252 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001253
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001254 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +00001255 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +00001256 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet72f6d592018-01-29 23:42:37 +00001257 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001258
Matthias Brauna09d18d2017-09-09 00:52:45 +00001259 VirtDead.clear();
1260 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001261
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001262 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +00001263 for (MachineInstr &MI : MBB) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001264 LLVM_DEBUG(
1265 dbgs() << "\n>> " << MI << "Regs:";
1266 dumpState()
1267 );
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001268
Matthias Braunfb93aec2018-11-10 00:36:27 +00001269 // Special handling for debug values. Note that they are not allowed to
1270 // affect codegen of the other instructions in any way.
Matthias Braun864cf582017-09-09 00:52:46 +00001271 if (MI.isDebugValue()) {
Matthias Braunfb93aec2018-11-10 00:36:27 +00001272 handleDebugValue(MI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001273 continue;
1274 }
1275
Matthias Braunfb93aec2018-11-10 00:36:27 +00001276 allocateInstruction(MI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001277 }
1278
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001279 // Spill all physical registers holding virtual registers now.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001280 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matt Arsenaultb6c599a2019-05-03 19:06:57 +00001281 spillAll(MBB.getFirstTerminator(), /*OnlyLiveOut*/ true);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001282
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001283 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001284 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001285 for (MachineInstr *MI : Coalesced)
1286 MBB.erase(MI);
Matthias Braun14af82a2018-11-07 02:04:07 +00001287 NumCoalesced += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001288
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001289 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001290}
1291
Matthias Braun864cf582017-09-09 00:52:46 +00001292bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001293 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1294 << "********** Function: " << MF.getName() << '\n');
Matthias Braun864cf582017-09-09 00:52:46 +00001295 MRI = &MF.getRegInfo();
1296 const TargetSubtargetInfo &STI = MF.getSubtarget();
1297 TRI = STI.getRegisterInfo();
1298 TII = STI.getInstrInfo();
1299 MFI = &MF.getFrameInfo();
1300 MRI->freezeReservedRegs(MF);
1301 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001302 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001303 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001304
1305 // initialize the virtual->physical register map to have a 'null'
1306 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001307 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1308 StackSlotForVirtReg.resize(NumVirtRegs);
1309 LiveVirtRegs.setUniverse(NumVirtRegs);
Matt Arsenaultb6c599a2019-05-03 19:06:57 +00001310 MayLiveAcrossBlocks.clear();
1311 MayLiveAcrossBlocks.resize(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001312
1313 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001314 for (MachineBasicBlock &MBB : MF)
1315 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001316
Andrew Trickda84e642012-02-21 04:51:23 +00001317 // All machine operands and other references to virtual registers have been
1318 // replaced. Remove the virtual registers.
1319 MRI->clearVirtRegs();
1320
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001321 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001322 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001323 return true;
1324}
1325
1326FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001327 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001328}