blob: eb3a4e481f5d37d4ba9e8d8b22c79b1d1020ef43 [file] [log] [blame]
Eugene Zelenko16ffaf82017-09-13 21:15:20 +00001//===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Matthias Braun8da30a72017-09-09 00:52:46 +000010/// \file This register allocator allocates registers to a basic block at a
11/// time, attempting to keep values in registers and reusing registers as
12/// appropriate.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000013//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000016#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/IndexedMap.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000021#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000022#include "llvm/ADT/Statistic.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000025#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000029#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/RegAllocRegistry.h"
32#include "llvm/CodeGen/RegisterClassInfo.h"
David Blaikie48319232017-11-08 01:01:31 +000033#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000034#include "llvm/CodeGen/TargetOpcodes.h"
35#include "llvm/CodeGen/TargetRegisterInfo.h"
36#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000037#include "llvm/IR/DebugLoc.h"
38#include "llvm/IR/Metadata.h"
39#include "llvm/MC/MCInstrDesc.h"
40#include "llvm/MC/MCRegisterInfo.h"
41#include "llvm/Pass.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/Compiler.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/Support/Debug.h"
45#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000046#include "llvm/Support/raw_ostream.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000047#include <cassert>
48#include <tuple>
49#include <vector>
50
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000051using namespace llvm;
52
Chandler Carruth8677f2f2014-04-22 02:02:50 +000053#define DEBUG_TYPE "regalloc"
54
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000055STATISTIC(NumStores, "Number of stores added");
56STATISTIC(NumLoads , "Number of loads added");
Matthias Braunf41ebcb2018-11-07 02:04:07 +000057STATISTIC(NumCoalesced, "Number of copies coalesced");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000058
59static RegisterRegAlloc
60 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
61
62namespace {
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000063
Matthias Braun8da30a72017-09-09 00:52:46 +000064 class RegAllocFast : public MachineFunctionPass {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000065 public:
66 static char ID;
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000067
Matthias Braun8da30a72017-09-09 00:52:46 +000068 RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
Derek Schufffadd1132016-03-28 17:05:30 +000069
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000070 private:
Matthias Braun8da30a72017-09-09 00:52:46 +000071 MachineFrameInfo *MFI;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +000072 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000073 const TargetRegisterInfo *TRI;
74 const TargetInstrInfo *TII;
Jakob Stoklund Olesen5d20c312011-06-02 18:35:30 +000075 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000076
Matthias Braun8da30a72017-09-09 00:52:46 +000077 /// Basic block currently being allocated.
Jakob Stoklund Olesen6fb69d82010-05-17 02:07:22 +000078 MachineBasicBlock *MBB;
79
Matthias Braun8da30a72017-09-09 00:52:46 +000080 /// Maps virtual regs to the frame index where these values are spilled.
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +000081 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
82
Matthias Braun8da30a72017-09-09 00:52:46 +000083 /// Everything we know about a live virtual register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000084 struct LiveReg {
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000085 MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
86 unsigned VirtReg; ///< Virtual register number.
87 MCPhysReg PhysReg = 0; ///< Currently held here.
88 unsigned short LastOpNum = 0; ///< OpNum on LastUse.
89 bool Dirty = false; ///< Register needs spill.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000090
Matthias Brauna007c002018-11-07 02:04:11 +000091 explicit LiveReg(unsigned VirtReg) : VirtReg(VirtReg) {}
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000092
Andrew Trickc0ccb8b2012-04-20 20:05:28 +000093 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +000094 return TargetRegisterInfo::virtReg2Index(VirtReg);
95 }
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +000096 };
97
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000098 using LiveRegMap = SparseSet<LiveReg>;
Matthias Braun8da30a72017-09-09 00:52:46 +000099 /// This map contains entries for each virtual register that is currently
100 /// available in a physical register.
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000101 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000102
Matthias Brauna007c002018-11-07 02:04:11 +0000103 DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap;
Devang Patel459a36b2010-08-04 18:42:02 +0000104
Matthias Brauna007c002018-11-07 02:04:11 +0000105 /// State of a physical register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000106 enum RegState {
Matthias Braun8da30a72017-09-09 00:52:46 +0000107 /// A disabled register is not available for allocation, but an alias may
108 /// be in use. A register can only be moved out of the disabled state if
109 /// all aliases are disabled.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000110 regDisabled,
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000111
Matthias Braun8da30a72017-09-09 00:52:46 +0000112 /// A free register is not currently in use and can be allocated
113 /// immediately without checking aliases.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000114 regFree,
115
Matthias Braun8da30a72017-09-09 00:52:46 +0000116 /// A reserved register has been assigned explicitly (e.g., setting up a
117 /// call parameter), and it remains reserved until it is used.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000118 regReserved
119
Matthias Braun8da30a72017-09-09 00:52:46 +0000120 /// A register state may also be a virtual register number, indication
121 /// that the physical register is currently allocated to a virtual
122 /// register. In that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000123 };
124
Matthias Brauna007c002018-11-07 02:04:11 +0000125 /// Maps each physical register to a RegState enum or a virtual register.
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000126 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000127
Matthias Braun53da3e92017-09-09 00:52:45 +0000128 SmallVector<unsigned, 16> VirtDead;
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000129 SmallVector<MachineInstr *, 32> Coalesced;
Matthias Braun53da3e92017-09-09 00:52:45 +0000130
Matthias Brauna007c002018-11-07 02:04:11 +0000131 using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
Matthias Braun8da30a72017-09-09 00:52:46 +0000132 /// Set of register units that are used in the current instruction, and so
133 /// cannot be allocated.
Matthias Brauna007c002018-11-07 02:04:11 +0000134 RegUnitSet UsedInInstr;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000135
Matthias Braun2c107122018-11-07 06:57:00 +0000136 void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
137
Matthias Braun8da30a72017-09-09 00:52:46 +0000138 /// Mark a physreg as used in this instruction.
139 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000140 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
141 UsedInInstr.insert(*Units);
142 }
143
Matthias Braun8da30a72017-09-09 00:52:46 +0000144 /// Check if a physreg or any of its aliases are used in this instruction.
145 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000146 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
147 if (UsedInInstr.count(*Units))
148 return true;
149 return false;
150 }
151
Alp Toker18510b72014-03-02 03:20:38 +0000152 enum : unsigned {
Matthias Brauna007c002018-11-07 02:04:11 +0000153 spillClean = 50,
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000154 spillDirty = 100,
155 spillImpossible = ~0u
156 };
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000157
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000158 public:
Mehdi Amini67f335d2016-10-01 02:56:57 +0000159 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000160
Craig Topper9f998de2014-03-07 09:26:03 +0000161 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000162 AU.setPreservesCFG();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000163 MachineFunctionPass::getAnalysisUsage(AU);
164 }
165
Matthias Braundb9ce2f2016-08-23 21:19:49 +0000166 MachineFunctionProperties getRequiredProperties() const override {
167 return MachineFunctionProperties().set(
168 MachineFunctionProperties::Property::NoPHIs);
169 }
170
Derek Schufffadd1132016-03-28 17:05:30 +0000171 MachineFunctionProperties getSetProperties() const override {
172 return MachineFunctionProperties().set(
Matthias Braun690a3cb2016-08-25 01:27:13 +0000173 MachineFunctionProperties::Property::NoVRegs);
Derek Schufffadd1132016-03-28 17:05:30 +0000174 }
175
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000176 private:
Fangrui Song7d882862018-07-16 18:51:40 +0000177 bool runOnMachineFunction(MachineFunction &MF) override;
Matthias Brauna007c002018-11-07 02:04:11 +0000178
Matthias Braun8da30a72017-09-09 00:52:46 +0000179 void allocateBasicBlock(MachineBasicBlock &MBB);
Matthias Braun3816d002018-11-10 00:36:27 +0000180 void allocateInstruction(MachineInstr &MI);
181 void handleDebugValue(MachineInstr &MI);
Matthias Braun8da30a72017-09-09 00:52:46 +0000182 void handleThroughOperands(MachineInstr &MI,
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000183 SmallVectorImpl<unsigned> &VirtDead);
Matthias Braun8da30a72017-09-09 00:52:46 +0000184 bool isLastUseOfLocalReg(const MachineOperand &MO) const;
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000185
Matthias Braun8da30a72017-09-09 00:52:46 +0000186 void addKillFlag(const LiveReg &LRI);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000187 void killVirtReg(LiveReg &LR);
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000188 void killVirtReg(unsigned VirtReg);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000189 void spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR);
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000190 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000191
Matthias Braun8da30a72017-09-09 00:52:46 +0000192 void usePhysReg(MachineOperand &MO);
Quentin Colombet263c46c2018-01-29 23:42:37 +0000193 void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
194 RegState NewState);
Matthias Braun8da30a72017-09-09 00:52:46 +0000195 unsigned calcSpillCost(MCPhysReg PhysReg) const;
Quentin Colombet263c46c2018-01-29 23:42:37 +0000196 void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000197
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000198 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
199 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
200 }
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000201
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000202 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
203 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
204 }
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000205
Matthias Braunb7a96d62018-11-07 06:57:03 +0000206 void allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint);
207 MCPhysReg defineVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
208 unsigned Hint);
209 LiveReg &reloadVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
210 unsigned Hint);
Akira Hatanakabab24212012-10-31 00:56:01 +0000211 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braun3816d002018-11-10 00:36:27 +0000212 bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
Matthias Braun8da30a72017-09-09 00:52:46 +0000213
Matthias Brauna2606fd2018-11-07 02:04:12 +0000214 int getStackSpaceFor(unsigned VirtReg);
215 void spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
216 MCPhysReg AssignedReg, bool Kill);
217 void reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
218 MCPhysReg PhysReg);
219
Matthias Braun8da30a72017-09-09 00:52:46 +0000220 void dumpState();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000221 };
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000222
223} // end anonymous namespace
224
225char RegAllocFast::ID = 0;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000226
Matthias Braun8da30a72017-09-09 00:52:46 +0000227INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
228 false)
Quentin Colombetb8caa092017-07-07 19:25:42 +0000229
Matthias Braun2c107122018-11-07 06:57:00 +0000230void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
231 PhysRegState[PhysReg] = NewState;
232}
233
Matthias Braun8da30a72017-09-09 00:52:46 +0000234/// This allocates space for the specified virtual register to be held on the
235/// stack.
Matthias Brauna007c002018-11-07 02:04:11 +0000236int RegAllocFast::getStackSpaceFor(unsigned VirtReg) {
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000237 // Find the location Reg would belong...
238 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun8da30a72017-09-09 00:52:46 +0000239 // Already has space allocated?
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000240 if (SS != -1)
Matthias Braun8da30a72017-09-09 00:52:46 +0000241 return SS;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000242
243 // Allocate a new stack object for this spill location...
Matthias Brauna007c002018-11-07 02:04:11 +0000244 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braun8da30a72017-09-09 00:52:46 +0000245 unsigned Size = TRI->getSpillSize(RC);
246 unsigned Align = TRI->getSpillAlignment(RC);
247 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000248
249 // Assign the slot.
250 StackSlotForVirtReg[VirtReg] = FrameIdx;
251 return FrameIdx;
252}
253
Matthias Brauna2606fd2018-11-07 02:04:12 +0000254/// Insert spill instruction for \p AssignedReg before \p Before. Update
255/// DBG_VALUEs with \p VirtReg operands with the stack slot.
256void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
257 MCPhysReg AssignedReg, bool Kill) {
258 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
259 << " in " << printReg(AssignedReg, TRI));
260 int FI = getStackSpaceFor(VirtReg);
Matthias Braunb8a4e382018-11-07 06:57:02 +0000261 LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
Matthias Brauna2606fd2018-11-07 02:04:12 +0000262
263 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
264 TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
265 ++NumStores;
266
267 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
268 // identify spilled location as the place to find corresponding variable's
269 // value.
270 SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
271 for (MachineInstr *DBG : LRIDbgValues) {
272 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
273 assert(NewDV->getParent() == MBB && "dangling parent pointer");
274 (void)NewDV;
275 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
276 }
277 // Now this register is spilled there is should not be any DBG_VALUE
278 // pointing to this register because they are all pointing to spilled value
279 // now.
280 LRIDbgValues.clear();
281}
282
283/// Insert reload instruction for \p PhysReg before \p Before.
284void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
285 MCPhysReg PhysReg) {
286 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
Matthias Braunb8a4e382018-11-07 06:57:02 +0000287 << printReg(PhysReg, TRI) << '\n');
Matthias Brauna2606fd2018-11-07 02:04:12 +0000288 int FI = getStackSpaceFor(VirtReg);
289 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
290 TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
291 ++NumLoads;
292}
293
Matthias Braun8da30a72017-09-09 00:52:46 +0000294/// Return true if MO is the only remaining reference to its virtual register,
295/// and it is guaranteed to be a block-local register.
296bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000297 // If the register has ever been spilled or reloaded, we conservatively assume
298 // it is a global register used in multiple blocks.
299 if (StackSlotForVirtReg[MO.getReg()] != -1)
300 return false;
301
302 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesen4e696622012-08-08 23:44:01 +0000303 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Andersonbf630222014-03-13 23:12:04 +0000304 if (&*I != &MO)
Jakob Stoklund Olesen4e696622012-08-08 23:44:01 +0000305 return false;
306 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000307}
308
Matthias Braun8da30a72017-09-09 00:52:46 +0000309/// Set kill flags on last use of a virtual register.
310void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000311 if (!LR.LastUse) return;
312 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000313 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
314 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000315 MO.setIsKill();
Quentin Colombet4e13bac2017-07-07 19:25:45 +0000316 // else, don't do anything we are problably redefining a
317 // subreg of this register and given we don't track which
318 // lanes are actually dead, we cannot insert a kill flag here.
319 // Otherwise we may end up in a situation like this:
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +0000320 // ... = (MO) physreg:sub1, implicit killed physreg
Quentin Colombet4e13bac2017-07-07 19:25:45 +0000321 // ... <== Here we would allow later pass to reuse physreg:sub1
322 // which is potentially wrong.
323 // LR:sub0 = ...
324 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000325 }
Jakob Stoklund Olesen804291e2010-05-12 18:46:03 +0000326}
327
Matthias Braun8da30a72017-09-09 00:52:46 +0000328/// Mark virtreg as no longer available.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000329void RegAllocFast::killVirtReg(LiveReg &LR) {
330 addKillFlag(LR);
331 assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
Jakob Stoklund Olesen91ba63d2012-02-22 16:50:46 +0000332 "Broken RegState mapping");
Matthias Braunb7a96d62018-11-07 06:57:03 +0000333 setPhysRegState(LR.PhysReg, regFree);
334 LR.PhysReg = 0;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000335}
336
Matthias Braun8da30a72017-09-09 00:52:46 +0000337/// Mark virtreg as no longer available.
338void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000339 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
340 "killVirtReg needs a virtual register");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000341 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000342 if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
343 killVirtReg(*LRI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000344}
345
Matthias Braun8da30a72017-09-09 00:52:46 +0000346/// This method spills the value specified by VirtReg into the corresponding
347/// stack slot if needed.
348void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
349 unsigned VirtReg) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000350 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
351 "Spilling a physical register is illegal!");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000352 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000353 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
354 "Spilling unmapped virtual register");
355 spillVirtReg(MI, *LRI);
Jakob Stoklund Olesen7d4f2592010-05-14 00:02:20 +0000356}
357
Matthias Braun8da30a72017-09-09 00:52:46 +0000358/// Do the actual work of spilling.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000359void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR) {
360 assert(PhysRegState[LR.PhysReg] == LR.VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000361
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000362 if (LR.Dirty) {
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000363 // If this physreg is used by the instruction, we want to kill it on the
364 // instruction, not on the spill.
Duncan P. N. Exon Smithce5fdc02016-07-01 15:03:37 +0000365 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen210e2af2010-05-11 23:24:47 +0000366 LR.Dirty = false;
Jakob Stoklund Olesen76b4d5a2010-05-11 23:24:45 +0000367
Matthias Braunb7a96d62018-11-07 06:57:03 +0000368 spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
Matthias Brauna2606fd2018-11-07 02:04:12 +0000369
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000370 if (SpillKill)
Craig Topper4ba84432014-04-14 00:51:57 +0000371 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000372 }
Matthias Braunb7a96d62018-11-07 06:57:03 +0000373 killVirtReg(LR);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000374}
375
Matthias Braun8da30a72017-09-09 00:52:46 +0000376/// Spill all dirty virtregs without killing them.
377void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Matthias Braun3816d002018-11-10 00:36:27 +0000378 if (LiveVirtRegs.empty())
379 return;
Jakob Stoklund Olesen29979852010-05-17 20:01:22 +0000380 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
381 // of spilling here is deterministic, if arbitrary.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000382 for (LiveReg &LR : LiveVirtRegs) {
383 if (!LR.PhysReg)
384 continue;
385 spillVirtReg(MI, LR);
386 }
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000387 LiveVirtRegs.clear();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000388}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000389
Matthias Braun8da30a72017-09-09 00:52:46 +0000390/// Handle the direct use of a physical register. Check that the register is
391/// not used by a virtreg. Kill the physreg, marking it free. This may add
392/// implicit kills to MO->getParent() and invalidate MO.
393void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborga9355d32016-05-18 16:10:17 +0000394 // Ignore undef uses.
395 if (MO.isUndef())
396 return;
397
Matthias Braun8da30a72017-09-09 00:52:46 +0000398 unsigned PhysReg = MO.getReg();
399 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
400 "Bad usePhysReg operand");
401
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000402 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000403 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000404 case regDisabled:
405 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000406 case regReserved:
407 PhysRegState[PhysReg] = regFree;
Justin Bogner7d7a23e2016-08-17 20:30:52 +0000408 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000409 case regFree:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000410 MO.setIsKill();
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000411 return;
412 default:
Eric Christopherf299da82010-12-08 21:35:09 +0000413 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000414 // wanted has been clobbered.
415 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000416 }
417
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000418 // Maybe a superregister is reserved?
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000419 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun8da30a72017-09-09 00:52:46 +0000420 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000421 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000422 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000423 break;
424 case regReserved:
Quentin Colombet331ec372014-12-03 23:38:08 +0000425 // Either PhysReg is a subregister of Alias and we mark the
426 // whole register as free, or PhysReg is the superregister of
427 // Alias and we mark all the aliases as disabled before freeing
428 // PhysReg.
429 // In the latter case, since PhysReg was disabled, this means that
430 // its value is defined only by physical sub-registers. This check
431 // is performed by the assert of the default case in this loop.
432 // Note: The value of the superregister may only be partial
433 // defined, that is why regDisabled is a valid state for aliases.
434 assert((TRI->isSuperRegister(PhysReg, Alias) ||
435 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000436 "Instruction is not using a subregister of a reserved register");
Justin Bogner7d7a23e2016-08-17 20:30:52 +0000437 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000438 case regFree:
439 if (TRI->isSuperRegister(PhysReg, Alias)) {
440 // Leave the superregister in the working set.
Matthias Braun2c107122018-11-07 06:57:00 +0000441 setPhysRegState(Alias, regFree);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000442 MO.getParent()->addRegisterKilled(Alias, TRI, true);
443 return;
444 }
445 // Some other alias was in the working set - clear it.
Matthias Braun2c107122018-11-07 06:57:00 +0000446 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000447 break;
448 default:
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000449 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000450 }
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000451 }
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000452
453 // All aliases are disabled, bring register into working set.
Matthias Braun2c107122018-11-07 06:57:00 +0000454 setPhysRegState(PhysReg, regFree);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000455 MO.setIsKill();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000456}
457
Matthias Braun8da30a72017-09-09 00:52:46 +0000458/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
459/// similar to defineVirtReg except the physreg is reserved instead of
460/// allocated.
Quentin Colombet263c46c2018-01-29 23:42:37 +0000461void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
462 MCPhysReg PhysReg, RegState NewState) {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000463 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000464 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
465 case regDisabled:
466 break;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000467 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000468 spillVirtReg(MI, VirtReg);
Justin Bogner7d7a23e2016-08-17 20:30:52 +0000469 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000470 case regFree:
471 case regReserved:
Matthias Braun2c107122018-11-07 06:57:00 +0000472 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000473 return;
474 }
475
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000476 // This is a disabled register, disable all aliases.
Matthias Braun2c107122018-11-07 06:57:00 +0000477 setPhysRegState(PhysReg, NewState);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000478 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun8da30a72017-09-09 00:52:46 +0000479 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000480 switch (unsigned VirtReg = PhysRegState[Alias]) {
481 case regDisabled:
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000482 break;
483 default:
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +0000484 spillVirtReg(MI, VirtReg);
Justin Bogner7d7a23e2016-08-17 20:30:52 +0000485 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000486 case regFree:
487 case regReserved:
Matthias Braun2c107122018-11-07 06:57:00 +0000488 setPhysRegState(Alias, regDisabled);
Jakob Stoklund Olesen4ed10822010-05-14 18:03:25 +0000489 if (TRI->isSuperRegister(PhysReg, Alias))
490 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000491 break;
492 }
493 }
494}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000495
Matthias Braun3816d002018-11-10 00:36:27 +0000496/// Return the cost of spilling clearing out PhysReg and aliases so it is free
497/// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
498/// disabled - it can be allocated directly.
Matthias Braun8da30a72017-09-09 00:52:46 +0000499/// \returns spillImpossible when PhysReg or an alias can't be spilled.
500unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000501 if (isRegUsedInInstr(PhysReg)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000502 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
503 << " is already used in instr.\n");
Jakob Stoklund Olesenb8acb7b2010-05-17 21:02:08 +0000504 return spillImpossible;
Eric Christopher0b756342011-04-12 22:17:44 +0000505 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000506 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
507 case regDisabled:
508 break;
509 case regFree:
510 return 0;
511 case regReserved:
Nicola Zaghen0818e782018-05-14 12:53:11 +0000512 LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
513 << printReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000514 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000515 default: {
Matthias Braunb7a96d62018-11-07 06:57:03 +0000516 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
517 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
518 "Missing VirtReg entry");
519 return LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000520 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000521 }
522
Eric Christopherbbfc3b32011-04-12 00:48:08 +0000523 // This is a disabled register, add up cost of aliases.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000524 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000525 unsigned Cost = 0;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000526 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun8da30a72017-09-09 00:52:46 +0000527 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000528 switch (unsigned VirtReg = PhysRegState[Alias]) {
529 case regDisabled:
530 break;
531 case regFree:
532 ++Cost;
533 break;
534 case regReserved:
535 return spillImpossible;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000536 default: {
Matthias Braunb7a96d62018-11-07 06:57:03 +0000537 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
538 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
539 "Missing VirtReg entry");
540 Cost += LRI->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000541 break;
542 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000543 }
Jakob Stoklund Olesen548643c2010-05-17 15:30:32 +0000544 }
545 return Cost;
546}
547
Adrian Prantl26b584c2018-05-01 15:54:18 +0000548/// This method updates local state so that we know that PhysReg is the
Matthias Braun8da30a72017-09-09 00:52:46 +0000549/// proper container for VirtReg now. The physical register must not be used
550/// for anything else when this is called.
551void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Matthias Braun2c107122018-11-07 06:57:00 +0000552 unsigned VirtReg = LR.VirtReg;
553 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
Matthias Braunb8a4e382018-11-07 06:57:02 +0000554 << printReg(PhysReg, TRI) << '\n');
Matthias Braun2c107122018-11-07 06:57:00 +0000555 assert(LR.PhysReg == 0 && "Already assigned a physreg");
556 assert(PhysReg != 0 && "Trying to assign no register");
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000557 LR.PhysReg = PhysReg;
Matthias Braun2c107122018-11-07 06:57:00 +0000558 setPhysRegState(PhysReg, VirtReg);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000559}
560
Matthias Braun8da30a72017-09-09 00:52:46 +0000561/// Allocates a physical register for VirtReg.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000562void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint) {
563 const unsigned VirtReg = LR.VirtReg;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000564
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000565 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
566 "Can only allocate virtual registers");
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000567
Matthias Braun8da30a72017-09-09 00:52:46 +0000568 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Matthias Braunb8a4e382018-11-07 06:57:02 +0000569 LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
570 << " in class " << TRI->getRegClassName(&RC) << '\n');
571
572 // Take hint when possible.
Matthias Braun8da30a72017-09-09 00:52:46 +0000573 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
574 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesen5e5ed442011-06-13 03:26:46 +0000575 // Ignore the hint if we would have to spill a dirty register.
576 unsigned Cost = calcSpillCost(Hint);
577 if (Cost < spillDirty) {
578 if (Cost)
579 definePhysReg(MI, Hint, regFree);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000580 assignVirtToPhysReg(LR, Hint);
581 return;
Jakob Stoklund Olesen4bf4baf2010-05-13 00:19:43 +0000582 }
583 }
584
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000585 // First try to find a completely free register.
Matthias Braunb8a4e382018-11-07 06:57:02 +0000586 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
587 for (MCPhysReg PhysReg : AllocationOrder) {
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000588 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Matthias Braunb7a96d62018-11-07 06:57:03 +0000589 assignVirtToPhysReg(LR, PhysReg);
590 return;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000591 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000592 }
593
Matthias Braun3816d002018-11-10 00:36:27 +0000594 MCPhysReg BestReg = 0;
Matthias Braun8da30a72017-09-09 00:52:46 +0000595 unsigned BestCost = spillImpossible;
Matthias Braunb8a4e382018-11-07 06:57:02 +0000596 for (MCPhysReg PhysReg : AllocationOrder) {
597 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
Matthias Braun8da30a72017-09-09 00:52:46 +0000598 unsigned Cost = calcSpillCost(PhysReg);
Matthias Braunb8a4e382018-11-07 06:57:02 +0000599 LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
Matthias Braun3816d002018-11-10 00:36:27 +0000600 // Immediate take a register with cost 0.
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000601 if (Cost == 0) {
Matthias Braunb7a96d62018-11-07 06:57:03 +0000602 assignVirtToPhysReg(LR, PhysReg);
603 return;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000604 }
Matthias Braunb8a4e382018-11-07 06:57:02 +0000605 if (Cost < BestCost) {
606 BestReg = PhysReg;
607 BestCost = Cost;
608 }
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000609 }
610
Matthias Braunb8a4e382018-11-07 06:57:02 +0000611 if (!BestReg) {
Matthias Braun3816d002018-11-10 00:36:27 +0000612 // Nothing we can do: Report an error and keep going with an invalid
613 // allocation.
Matthias Braunb8a4e382018-11-07 06:57:02 +0000614 if (MI.isInlineAsm())
615 MI.emitError("inline assembly requires more registers than available");
616 else
617 MI.emitError("ran out of registers during register allocation");
618 definePhysReg(MI, *AllocationOrder.begin(), regFree);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000619 assignVirtToPhysReg(LR, *AllocationOrder.begin());
620 return;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000621 }
622
Matthias Braunb8a4e382018-11-07 06:57:02 +0000623 definePhysReg(MI, BestReg, regFree);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000624 assignVirtToPhysReg(LR, BestReg);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000625}
626
Matthias Braun8da30a72017-09-09 00:52:46 +0000627/// Allocates a register for VirtReg and mark it as dirty.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000628MCPhysReg RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
629 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000630 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
631 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000632 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000633 bool New;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +0000634 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Matthias Braunb7a96d62018-11-07 06:57:03 +0000635 if (!LRI->PhysReg) {
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000636 // If there is no hint, peek at the only use of this register.
637 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
638 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Andersonbf630222014-03-13 23:12:04 +0000639 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000640 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000641 if (UseMI.isCopyLike())
642 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen0c9e4f52010-05-17 04:50:57 +0000643 }
Matthias Braunb7a96d62018-11-07 06:57:03 +0000644 allocVirtReg(MI, *LRI, Hint);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000645 } else if (LRI->LastUse) {
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000646 // Redefining a live register - kill at the last use, unless it is this
647 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smithce5fdc02016-07-01 15:03:37 +0000648 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000649 addKillFlag(*LRI);
Jakob Stoklund Olesen0eeb05c2010-05-18 21:10:50 +0000650 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000651 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smithce5fdc02016-07-01 15:03:37 +0000652 LRI->LastUse = &MI;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000653 LRI->LastOpNum = OpNum;
654 LRI->Dirty = true;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000655 markRegUsedInInstr(LRI->PhysReg);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000656 return LRI->PhysReg;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000657}
658
Matthias Braun8da30a72017-09-09 00:52:46 +0000659/// Make sure VirtReg is available in a physreg and return it.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000660RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(MachineInstr &MI,
661 unsigned OpNum,
662 unsigned VirtReg,
663 unsigned Hint) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000664 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
665 "Not a virtual register");
Jakob Stoklund Olesen844db9c2010-05-17 02:49:15 +0000666 LiveRegMap::iterator LRI;
Jakob Stoklund Olesen01dcbf82010-05-17 02:07:29 +0000667 bool New;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +0000668 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smithce5fdc02016-07-01 15:03:37 +0000669 MachineOperand &MO = MI.getOperand(OpNum);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000670 if (!LRI->PhysReg) {
671 allocVirtReg(MI, *LRI, Hint);
Matthias Brauna2606fd2018-11-07 02:04:12 +0000672 reload(MI, VirtReg, LRI->PhysReg);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000673 } else if (LRI->Dirty) {
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000674 if (isLastUseOfLocalReg(MO)) {
Matthias Braunb8a4e382018-11-07 06:57:02 +0000675 LLVM_DEBUG(dbgs() << "Killing last use: " << MO << '\n');
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000676 if (MO.isUse())
677 MO.setIsKill();
678 else
679 MO.setIsDead();
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000680 } else if (MO.isKill()) {
Matthias Braunb8a4e382018-11-07 06:57:02 +0000681 LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << '\n');
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000682 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000683 } else if (MO.isDead()) {
Matthias Braunb8a4e382018-11-07 06:57:02 +0000684 LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << '\n');
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000685 MO.setIsDead(false);
Jakob Stoklund Olesen1e03ff42010-05-15 06:09:08 +0000686 }
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000687 } else if (MO.isKill()) {
688 // We must remove kill flags from uses of reloaded registers because the
689 // register would be killed immediately, and there might be a second use:
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +0000690 // %foo = OR killed %x, %x
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000691 // This would cause a second reload of %x into a different register.
Matthias Braunb8a4e382018-11-07 06:57:02 +0000692 LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << '\n');
Jakob Stoklund Olesenac3e5292010-05-17 03:26:06 +0000693 MO.setIsKill(false);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000694 } else if (MO.isDead()) {
Matthias Braunb8a4e382018-11-07 06:57:02 +0000695 LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << '\n');
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000696 MO.setIsDead(false);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000697 }
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000698 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smithce5fdc02016-07-01 15:03:37 +0000699 LRI->LastUse = &MI;
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +0000700 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000701 markRegUsedInInstr(LRI->PhysReg);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000702 return *LRI;
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000703}
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000704
Matthias Braun8da30a72017-09-09 00:52:46 +0000705/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
706/// may invalidate any operand pointers. Return true if the operand kills its
707/// register.
Matthias Braun3816d002018-11-10 00:36:27 +0000708bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
Matthias Braun8da30a72017-09-09 00:52:46 +0000709 MCPhysReg PhysReg) {
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000710 bool Dead = MO.isDead();
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000711 if (!MO.getSubReg()) {
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +0000712 MO.setReg(PhysReg);
Geoff Berry13357c92018-02-23 18:25:08 +0000713 MO.setIsRenamable(true);
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000714 return MO.isKill() || Dead;
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000715 }
716
717 // Handle subregister index.
718 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
Geoff Berry13357c92018-02-23 18:25:08 +0000719 MO.setIsRenamable(true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000720 MO.setSubReg(0);
Jakob Stoklund Olesend32e7352010-05-19 21:36:05 +0000721
722 // A kill flag implies killing the full register. Add corresponding super
723 // register kill.
724 if (MO.isKill()) {
Matthias Braun8da30a72017-09-09 00:52:46 +0000725 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesen41e14012010-05-17 02:49:21 +0000726 return true;
727 }
Jakob Stoklund Olesen4d108292012-05-14 21:10:25 +0000728
729 // A <def,read-undef> of a sub-register requires an implicit def of the full
730 // register.
731 if (MO.isDef() && MO.isUndef())
Matthias Braun8da30a72017-09-09 00:52:46 +0000732 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesen4d108292012-05-14 21:10:25 +0000733
Jakob Stoklund Olesen6565a702012-05-14 21:30:58 +0000734 return Dead;
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +0000735}
736
Matthias Braun8da30a72017-09-09 00:52:46 +0000737// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000738// there are additional physreg defines.
Matthias Braun8da30a72017-09-09 00:52:46 +0000739void RegAllocFast::handleThroughOperands(MachineInstr &MI,
740 SmallVectorImpl<unsigned> &VirtDead) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000741 LLVM_DEBUG(dbgs() << "Scanning for through registers:");
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000742 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun8da30a72017-09-09 00:52:46 +0000743 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000744 if (!MO.isReg()) continue;
745 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000746 if (!TargetRegisterInfo::isVirtualRegister(Reg))
747 continue;
Matthias Braun8da30a72017-09-09 00:52:46 +0000748 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
749 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie5401ba72014-11-19 07:49:26 +0000750 if (ThroughRegs.insert(Reg).second)
Nicola Zaghen0818e782018-05-14 12:53:11 +0000751 LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000752 }
753 }
754
755 // If any physreg defines collide with preallocated through registers,
756 // we must spill and reallocate.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000757 LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun8da30a72017-09-09 00:52:46 +0000758 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000759 if (!MO.isReg() || !MO.isDef()) continue;
760 unsigned Reg = MO.getReg();
761 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000762 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +0000763 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen8c70ea42012-06-01 22:38:17 +0000764 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun8da30a72017-09-09 00:52:46 +0000765 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000766 }
767 }
768
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000769 SmallVector<unsigned, 8> PartialDefs;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000770 LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun8da30a72017-09-09 00:52:46 +0000771 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
Matthias Braun3816d002018-11-10 00:36:27 +0000772 MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000773 if (!MO.isReg()) continue;
774 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000775 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000776 if (MO.isUse()) {
Matthias Braun8da30a72017-09-09 00:52:46 +0000777 if (!MO.isTied()) continue;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000778 LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
779 << ") is tied to operand " << MI.findTiedOperandIdx(I)
780 << ".\n");
Matthias Braunb7a96d62018-11-07 06:57:03 +0000781 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
782 MCPhysReg PhysReg = LR.PhysReg;
Matthias Braun3816d002018-11-10 00:36:27 +0000783 setPhysReg(MI, MO, PhysReg);
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000784 // Note: we don't update the def operand yet. That would cause the normal
785 // def-scan to attempt spilling.
Matthias Braun8da30a72017-09-09 00:52:46 +0000786 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Matthias Braunb8a4e382018-11-07 06:57:02 +0000787 LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << '\n');
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000788 // Reload the register, but don't assign to the operand just yet.
789 // That would confuse the later phys-def processing pass.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000790 LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
791 PartialDefs.push_back(LR.PhysReg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000792 }
793 }
794
Nicola Zaghen0818e782018-05-14 12:53:11 +0000795 LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun8da30a72017-09-09 00:52:46 +0000796 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
797 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola254a1322011-11-22 06:27:18 +0000798 if (!MO.isReg()) continue;
799 unsigned Reg = MO.getReg();
800 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
801 if (!MO.isEarlyClobber())
802 continue;
803 // Note: defineVirtReg may invalidate MO.
Matthias Braunb7a96d62018-11-07 06:57:03 +0000804 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, 0);
Matthias Braun3816d002018-11-10 00:36:27 +0000805 if (setPhysReg(MI, MI.getOperand(I), PhysReg))
Rafael Espindola254a1322011-11-22 06:27:18 +0000806 VirtDead.push_back(Reg);
807 }
808
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000809 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +0000810 UsedInInstr.clear();
Matthias Braun8da30a72017-09-09 00:52:46 +0000811 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000812 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
813 unsigned Reg = MO.getReg();
814 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000815 LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
816 << " as used in instr\n");
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +0000817 markRegUsedInInstr(Reg);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000818 }
Jakob Stoklund Olesend1303d22010-06-29 19:15:30 +0000819
820 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun8da30a72017-09-09 00:52:46 +0000821 for (unsigned PartialDef : PartialDefs)
822 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesend843b392010-06-28 18:34:34 +0000823}
824
Matthias Braun8da30a72017-09-09 00:52:46 +0000825#ifndef NDEBUG
826void RegAllocFast::dumpState() {
827 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
828 if (PhysRegState[Reg] == regDisabled) continue;
Francis Visoiu Mistrihe6b89912017-11-30 16:12:24 +0000829 dbgs() << " " << printReg(Reg, TRI);
Matthias Braun8da30a72017-09-09 00:52:46 +0000830 switch(PhysRegState[Reg]) {
831 case regFree:
832 break;
833 case regReserved:
834 dbgs() << "*";
835 break;
836 default: {
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +0000837 dbgs() << '=' << printReg(PhysRegState[Reg]);
Matthias Braunb7a96d62018-11-07 06:57:03 +0000838 LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
839 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
840 "Missing VirtReg entry");
841 if (LRI->Dirty)
Matthias Braun8da30a72017-09-09 00:52:46 +0000842 dbgs() << "*";
Matthias Braunb7a96d62018-11-07 06:57:03 +0000843 assert(LRI->PhysReg == Reg && "Bad inverse map");
Matthias Braun8da30a72017-09-09 00:52:46 +0000844 break;
845 }
846 }
847 }
848 dbgs() << '\n';
849 // Check that LiveVirtRegs is the inverse.
850 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
851 e = LiveVirtRegs.end(); i != e; ++i) {
Matthias Braunb7a96d62018-11-07 06:57:03 +0000852 if (!i->PhysReg)
853 continue;
Matthias Braun8da30a72017-09-09 00:52:46 +0000854 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
855 "Bad map key");
856 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
857 "Bad map value");
858 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
859 }
860}
861#endif
862
Matthias Braun3816d002018-11-10 00:36:27 +0000863void RegAllocFast::allocateInstruction(MachineInstr &MI) {
864 const MCInstrDesc &MCID = MI.getDesc();
865
866 // If this is a copy, we may be able to coalesce.
867 unsigned CopySrcReg = 0;
868 unsigned CopyDstReg = 0;
869 unsigned CopySrcSub = 0;
870 unsigned CopyDstSub = 0;
871 if (MI.isCopy()) {
872 CopyDstReg = MI.getOperand(0).getReg();
873 CopySrcReg = MI.getOperand(1).getReg();
874 CopyDstSub = MI.getOperand(0).getSubReg();
875 CopySrcSub = MI.getOperand(1).getSubReg();
876 }
877
878 // Track registers used by instruction.
879 UsedInInstr.clear();
880
881 // First scan.
882 // Mark physreg uses and early clobbers as used.
883 // Find the end of the virtreg operands
884 unsigned VirtOpEnd = 0;
885 bool hasTiedOps = false;
886 bool hasEarlyClobbers = false;
887 bool hasPartialRedefs = false;
888 bool hasPhysDefs = false;
889 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
890 MachineOperand &MO = MI.getOperand(i);
891 // Make sure MRI knows about registers clobbered by regmasks.
892 if (MO.isRegMask()) {
893 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
894 continue;
895 }
896 if (!MO.isReg()) continue;
897 unsigned Reg = MO.getReg();
898 if (!Reg) continue;
899 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
900 VirtOpEnd = i+1;
901 if (MO.isUse()) {
902 hasTiedOps = hasTiedOps ||
903 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
904 } else {
905 if (MO.isEarlyClobber())
906 hasEarlyClobbers = true;
907 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
908 hasPartialRedefs = true;
909 }
910 continue;
911 }
912 if (!MRI->isAllocatable(Reg)) continue;
913 if (MO.isUse()) {
914 usePhysReg(MO);
915 } else if (MO.isEarlyClobber()) {
916 definePhysReg(MI, Reg,
917 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
918 hasEarlyClobbers = true;
919 } else
920 hasPhysDefs = true;
921 }
922
923 // The instruction may have virtual register operands that must be allocated
924 // the same register at use-time and def-time: early clobbers and tied
925 // operands. If there are also physical defs, these registers must avoid
926 // both physical defs and uses, making them more constrained than normal
927 // operands.
928 // Similarly, if there are multiple defs and tied operands, we must make
929 // sure the same register is allocated to uses and defs.
930 // We didn't detect inline asm tied operands above, so just make this extra
931 // pass for all inline asm.
932 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
933 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
934 handleThroughOperands(MI, VirtDead);
935 // Don't attempt coalescing when we have funny stuff going on.
936 CopyDstReg = 0;
937 // Pretend we have early clobbers so the use operands get marked below.
938 // This is not necessary for the common case of a single tied use.
939 hasEarlyClobbers = true;
940 }
941
942 // Second scan.
943 // Allocate virtreg uses.
944 for (unsigned I = 0; I != VirtOpEnd; ++I) {
945 MachineOperand &MO = MI.getOperand(I);
946 if (!MO.isReg()) continue;
947 unsigned Reg = MO.getReg();
948 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
949 if (MO.isUse()) {
950 LiveReg &LR = reloadVirtReg(MI, I, Reg, CopyDstReg);
951 MCPhysReg PhysReg = LR.PhysReg;
952 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
953 if (setPhysReg(MI, MO, PhysReg))
954 killVirtReg(LR);
955 }
956 }
957
958 // Track registers defined by instruction - early clobbers and tied uses at
959 // this point.
960 UsedInInstr.clear();
961 if (hasEarlyClobbers) {
962 for (const MachineOperand &MO : MI.operands()) {
963 if (!MO.isReg()) continue;
964 unsigned Reg = MO.getReg();
965 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
966 // Look for physreg defs and tied uses.
967 if (!MO.isDef() && !MO.isTied()) continue;
968 markRegUsedInInstr(Reg);
969 }
970 }
971
972 unsigned DefOpEnd = MI.getNumOperands();
973 if (MI.isCall()) {
974 // Spill all virtregs before a call. This serves one purpose: If an
975 // exception is thrown, the landing pad is going to expect to find
976 // registers in their spill slots.
977 // Note: although this is appealing to just consider all definitions
978 // as call-clobbered, this is not correct because some of those
979 // definitions may be used later on and we do not want to reuse
980 // those for virtual registers in between.
981 LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
982 spillAll(MI);
983 }
984
985 // Third scan.
986 // Allocate defs and collect dead defs.
987 for (unsigned I = 0; I != DefOpEnd; ++I) {
988 const MachineOperand &MO = MI.getOperand(I);
989 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
990 continue;
991 unsigned Reg = MO.getReg();
992
993 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
994 if (!MRI->isAllocatable(Reg)) continue;
995 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
996 continue;
997 }
998 MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
999 if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
1000 VirtDead.push_back(Reg);
1001 CopyDstReg = 0; // cancel coalescing;
1002 } else
1003 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1004 }
1005
1006 // Kill dead defs after the scan to ensure that multiple defs of the same
1007 // register are allocated identically. We didn't need to do this for uses
1008 // because we are crerating our own kill flags, and they are always at the
1009 // last use.
1010 for (unsigned VirtReg : VirtDead)
1011 killVirtReg(VirtReg);
1012 VirtDead.clear();
1013
1014 LLVM_DEBUG(dbgs() << "<< " << MI);
1015 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1016 LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1017 Coalesced.push_back(&MI);
1018 }
1019}
1020
1021void RegAllocFast::handleDebugValue(MachineInstr &MI) {
1022 MachineOperand &MO = MI.getOperand(0);
1023
1024 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1025 // mostly constants and frame indices.
1026 if (!MO.isReg())
1027 return;
1028 unsigned Reg = MO.getReg();
1029 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1030 return;
1031
1032 // See if this virtual register has already been allocated to a physical
1033 // register or spilled to a stack slot.
1034 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1035 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1036 setPhysReg(MI, MO, LRI->PhysReg);
1037 } else {
1038 int SS = StackSlotForVirtReg[Reg];
1039 if (SS != -1) {
1040 // Modify DBG_VALUE now that the value is in a spill slot.
1041 updateDbgValueForSpill(MI, SS);
1042 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << MI);
1043 return;
1044 }
1045
1046 // We can't allocate a physreg for a DebugValue, sorry!
1047 LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
1048 MO.setReg(0);
1049 }
1050
1051 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1052 // that future spills of Reg will have DBG_VALUEs.
1053 LiveDbgValueMap[Reg].push_back(&MI);
1054}
1055
Matthias Braun8da30a72017-09-09 00:52:46 +00001056void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1057 this->MBB = &MBB;
Nicola Zaghen0818e782018-05-14 12:53:11 +00001058 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001059
1060 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesena2407432012-02-22 01:02:37 +00001061 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001062
Matthias Braun8da30a72017-09-09 00:52:46 +00001063 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001064
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001065 // Add live-in registers as live.
Matthias Braun8da30a72017-09-09 00:52:46 +00001066 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braunaf5ff602015-09-09 18:08:03 +00001067 if (MRI->isAllocatable(LI.PhysReg))
Quentin Colombet263c46c2018-01-29 23:42:37 +00001068 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001069
Matthias Braun53da3e92017-09-09 00:52:45 +00001070 VirtDead.clear();
1071 Coalesced.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001072
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001073 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun8da30a72017-09-09 00:52:46 +00001074 for (MachineInstr &MI : MBB) {
Matthias Braun3816d002018-11-10 00:36:27 +00001075 LLVM_DEBUG(
1076 dbgs() << "\n>> " << MI << "Regs:";
1077 dumpState()
1078 );
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001079
Matthias Braun3816d002018-11-10 00:36:27 +00001080 // Special handling for debug values. Note that they are not allowed to
1081 // affect codegen of the other instructions in any way.
Matthias Braun8da30a72017-09-09 00:52:46 +00001082 if (MI.isDebugValue()) {
Matthias Braun3816d002018-11-10 00:36:27 +00001083 handleDebugValue(MI);
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001084 continue;
1085 }
1086
Matthias Braun3816d002018-11-10 00:36:27 +00001087 allocateInstruction(MI);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001088 }
1089
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001090 // Spill all physical registers holding virtual registers now.
Nicola Zaghen0818e782018-05-14 12:53:11 +00001091 LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun8da30a72017-09-09 00:52:46 +00001092 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenbbf33b32010-05-11 18:54:45 +00001093
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001094 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesene6aba832010-05-17 02:07:32 +00001095 // LiveVirtRegs might refer to the instrs.
Matthias Braun8da30a72017-09-09 00:52:46 +00001096 for (MachineInstr *MI : Coalesced)
1097 MBB.erase(MI);
Matthias Braunf41ebcb2018-11-07 02:04:07 +00001098 NumCoalesced += Coalesced.size();
Jakob Stoklund Olesen7ff82e12010-05-14 04:30:51 +00001099
Nicola Zaghen0818e782018-05-14 12:53:11 +00001100 LLVM_DEBUG(MBB.dump());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001101}
1102
Matthias Braun8da30a72017-09-09 00:52:46 +00001103bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001104 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1105 << "********** Function: " << MF.getName() << '\n');
Matthias Braun8da30a72017-09-09 00:52:46 +00001106 MRI = &MF.getRegInfo();
1107 const TargetSubtargetInfo &STI = MF.getSubtarget();
1108 TRI = STI.getRegisterInfo();
1109 TII = STI.getInstrInfo();
1110 MFI = &MF.getFrameInfo();
1111 MRI->freezeReservedRegs(MF);
1112 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesend7ea7d52012-10-17 01:37:59 +00001113 UsedInInstr.clear();
Jakob Stoklund Olesen601158a2013-02-21 19:35:21 +00001114 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001115
1116 // initialize the virtual->physical register map to have a 'null'
1117 // mapping for all virtual registers
Matthias Braun8da30a72017-09-09 00:52:46 +00001118 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1119 StackSlotForVirtReg.resize(NumVirtRegs);
1120 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001121
1122 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun8da30a72017-09-09 00:52:46 +00001123 for (MachineBasicBlock &MBB : MF)
1124 allocateBasicBlock(MBB);
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001125
Andrew Trick19273ae2012-02-21 04:51:23 +00001126 // All machine operands and other references to virtual registers have been
1127 // replaced. Remove the virtual registers.
1128 MRI->clearVirtRegs();
1129
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001130 StackSlotForVirtReg.clear();
Devang Patel459a36b2010-08-04 18:42:02 +00001131 LiveDbgValueMap.clear();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001132 return true;
1133}
1134
1135FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun8da30a72017-09-09 00:52:46 +00001136 return new RegAllocFast();
Jakob Stoklund Olesen00207232010-04-21 18:02:42 +00001137}