blob: 7061c3ff6523c5dc829dbd59276520757434e457 [file] [log] [blame]
Eugene Zelenko618c5552017-09-13 21:15:20 +00001//===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Matthias Braun864cf582017-09-09 00:52:46 +000010/// \file This register allocator allocates registers to a basic block at a
11/// time, attempting to keep values in registers and reusing registers as
12/// appropriate.
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000013//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenko618c5552017-09-13 21:15:20 +000016#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/IndexedMap.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000021#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000022#include "llvm/ADT/Statistic.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000025#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000029#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/RegAllocRegistry.h"
32#include "llvm/CodeGen/RegisterClassInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000033#include "llvm/IR/DebugInfoMetadata.h"
34#include "llvm/IR/DebugLoc.h"
35#include "llvm/IR/Metadata.h"
36#include "llvm/MC/MCInstrDesc.h"
37#include "llvm/MC/MCRegisterInfo.h"
38#include "llvm/Pass.h"
39#include "llvm/Support/Casting.h"
40#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000043#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000045#include "llvm/Target/TargetOpcodes.h"
46#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000047#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000048#include <cassert>
49#include <tuple>
50#include <vector>
51
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000052using namespace llvm;
53
Chandler Carruth1b9dde02014-04-22 02:02:50 +000054#define DEBUG_TYPE "regalloc"
55
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000056STATISTIC(NumStores, "Number of stores added");
57STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +000058STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000059
60static RegisterRegAlloc
61 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
62
63namespace {
Eugene Zelenko618c5552017-09-13 21:15:20 +000064
Matthias Braun864cf582017-09-09 00:52:46 +000065 class RegAllocFast : public MachineFunctionPass {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000066 public:
67 static char ID;
Eugene Zelenko618c5552017-09-13 21:15:20 +000068
Matthias Braun864cf582017-09-09 00:52:46 +000069 RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
Derek Schuffad154c82016-03-28 17:05:30 +000070
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000071 private:
Matthias Braun864cf582017-09-09 00:52:46 +000072 MachineFrameInfo *MFI;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +000073 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000074 const TargetRegisterInfo *TRI;
75 const TargetInstrInfo *TII;
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +000076 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000077
Matthias Braun864cf582017-09-09 00:52:46 +000078 /// Basic block currently being allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +000079 MachineBasicBlock *MBB;
80
Matthias Braun864cf582017-09-09 00:52:46 +000081 /// Maps virtual regs to the frame index where these values are spilled.
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000082 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
83
Matthias Braun864cf582017-09-09 00:52:46 +000084 /// Everything we know about a live virtual register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000085 struct LiveReg {
Eugene Zelenko618c5552017-09-13 21:15:20 +000086 MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
87 unsigned VirtReg; ///< Virtual register number.
88 MCPhysReg PhysReg = 0; ///< Currently held here.
89 unsigned short LastOpNum = 0; ///< OpNum on LastUse.
90 bool Dirty = false; ///< Register needs spill.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000091
Eugene Zelenko618c5552017-09-13 21:15:20 +000092 explicit LiveReg(unsigned v) : VirtReg(v) {}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000093
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000094 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000095 return TargetRegisterInfo::virtReg2Index(VirtReg);
96 }
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000097 };
98
Eugene Zelenko618c5552017-09-13 21:15:20 +000099 using LiveRegMap = SparseSet<LiveReg>;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000100
Matthias Braun864cf582017-09-09 00:52:46 +0000101 /// This map contains entries for each virtual register that is currently
102 /// available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000103 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000104
Eugene Zelenko618c5552017-09-13 21:15:20 +0000105 DenseMap<unsigned, SmallVector<MachineInstr *, 4>> LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +0000106
Matthias Braun864cf582017-09-09 00:52:46 +0000107 /// Track the state of a physical register.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000108 enum RegState {
Matthias Braun864cf582017-09-09 00:52:46 +0000109 /// A disabled register is not available for allocation, but an alias may
110 /// be in use. A register can only be moved out of the disabled state if
111 /// all aliases are disabled.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000112 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000113
Matthias Braun864cf582017-09-09 00:52:46 +0000114 /// A free register is not currently in use and can be allocated
115 /// immediately without checking aliases.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000116 regFree,
117
Matthias Braun864cf582017-09-09 00:52:46 +0000118 /// A reserved register has been assigned explicitly (e.g., setting up a
119 /// call parameter), and it remains reserved until it is used.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000120 regReserved
121
Matthias Braun864cf582017-09-09 00:52:46 +0000122 /// A register state may also be a virtual register number, indication
123 /// that the physical register is currently allocated to a virtual
124 /// register. In that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000125 };
126
Matthias Braun864cf582017-09-09 00:52:46 +0000127 /// One of the RegState enums, or a virtreg.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000128 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000129
Matthias Brauna09d18d2017-09-09 00:52:45 +0000130 SmallVector<unsigned, 16> VirtDead;
Eugene Zelenko618c5552017-09-13 21:15:20 +0000131 SmallVector<MachineInstr *, 32> Coalesced;
Matthias Brauna09d18d2017-09-09 00:52:45 +0000132
Matthias Braun864cf582017-09-09 00:52:46 +0000133 /// Set of register units.
Eugene Zelenko618c5552017-09-13 21:15:20 +0000134 using UsedInInstrSet = SparseSet<unsigned>;
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000135
Matthias Braun864cf582017-09-09 00:52:46 +0000136 /// Set of register units that are used in the current instruction, and so
137 /// cannot be allocated.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000138 UsedInInstrSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000139
Matthias Braun864cf582017-09-09 00:52:46 +0000140 /// Mark a physreg as used in this instruction.
141 void markRegUsedInInstr(MCPhysReg PhysReg) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000142 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
143 UsedInInstr.insert(*Units);
144 }
145
Matthias Braun864cf582017-09-09 00:52:46 +0000146 /// Check if a physreg or any of its aliases are used in this instruction.
147 bool isRegUsedInInstr(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000148 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
149 if (UsedInInstr.count(*Units))
150 return true;
151 return false;
152 }
153
Matthias Braun864cf582017-09-09 00:52:46 +0000154 /// This flag is set when LiveRegMap will be cleared completely after
155 /// spilling all live registers. LiveRegMap entries should not be erased.
156 bool isBulkSpilling = false;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000157
Alp Toker61007d82014-03-02 03:20:38 +0000158 enum : unsigned {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000159 spillClean = 1,
160 spillDirty = 100,
161 spillImpossible = ~0u
162 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000163
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000164 public:
Mehdi Amini117296c2016-10-01 02:56:57 +0000165 StringRef getPassName() const override { return "Fast Register Allocator"; }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000166
Craig Topper4584cd52014-03-07 09:26:03 +0000167 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000168 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000169 MachineFunctionPass::getAnalysisUsage(AU);
170 }
171
Matthias Braun90799ce2016-08-23 21:19:49 +0000172 MachineFunctionProperties getRequiredProperties() const override {
173 return MachineFunctionProperties().set(
174 MachineFunctionProperties::Property::NoPHIs);
175 }
176
Derek Schuffad154c82016-03-28 17:05:30 +0000177 MachineFunctionProperties getSetProperties() const override {
178 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000179 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000180 }
181
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000182 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000183 bool runOnMachineFunction(MachineFunction &Fn) override;
Matthias Braun864cf582017-09-09 00:52:46 +0000184 void allocateBasicBlock(MachineBasicBlock &MBB);
185 void handleThroughOperands(MachineInstr &MI,
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000186 SmallVectorImpl<unsigned> &VirtDead);
Matthias Braun864cf582017-09-09 00:52:46 +0000187 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass &RC);
188 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);
191 void killVirtReg(LiveRegMap::iterator LRI);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000192 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000193 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000194 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000195
Matthias Braun864cf582017-09-09 00:52:46 +0000196 void usePhysReg(MachineOperand &MO);
197 void definePhysReg(MachineInstr &MI, MCPhysReg PhysReg, RegState NewState);
198 unsigned calcSpillCost(MCPhysReg PhysReg) const;
199 void assignVirtToPhysReg(LiveReg&, MCPhysReg PhysReg);
Eugene Zelenko618c5552017-09-13 21:15:20 +0000200
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000201 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
202 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
203 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000204
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000205 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
206 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
207 }
Eugene Zelenko618c5552017-09-13 21:15:20 +0000208
Matthias Braun864cf582017-09-09 00:52:46 +0000209 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, MCPhysReg PhysReg);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000210 LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000211 unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000212 LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000213 unsigned VirtReg, unsigned Hint);
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000214 LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000215 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000216 void spillAll(MachineBasicBlock::iterator MI);
Matthias Braun864cf582017-09-09 00:52:46 +0000217 bool setPhysReg(MachineInstr &MI, unsigned OpNum, MCPhysReg PhysReg);
218
219 void dumpState();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000220 };
Eugene Zelenko618c5552017-09-13 21:15:20 +0000221
222} // end anonymous namespace
223
224char RegAllocFast::ID = 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000225
Matthias Braun864cf582017-09-09 00:52:46 +0000226INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
227 false)
Quentin Colombet81551142017-07-07 19:25:42 +0000228
Matthias Braun864cf582017-09-09 00:52:46 +0000229/// This allocates space for the specified virtual register to be held on the
230/// stack.
231int RegAllocFast::getStackSpaceFor(unsigned VirtReg,
232 const TargetRegisterClass &RC) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000233 // Find the location Reg would belong...
234 int SS = StackSlotForVirtReg[VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000235 // Already has space allocated?
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000236 if (SS != -1)
Matthias Braun864cf582017-09-09 00:52:46 +0000237 return SS;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000238
239 // Allocate a new stack object for this spill location...
Matthias Braun864cf582017-09-09 00:52:46 +0000240 unsigned Size = TRI->getSpillSize(RC);
241 unsigned Align = TRI->getSpillAlignment(RC);
242 int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000243
244 // Assign the slot.
245 StackSlotForVirtReg[VirtReg] = FrameIdx;
246 return FrameIdx;
247}
248
Matthias Braun864cf582017-09-09 00:52:46 +0000249/// Return true if MO is the only remaining reference to its virtual register,
250/// and it is guaranteed to be a block-local register.
251bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000252 // If the register has ever been spilled or reloaded, we conservatively assume
253 // it is a global register used in multiple blocks.
254 if (StackSlotForVirtReg[MO.getReg()] != -1)
255 return false;
256
257 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000258 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000259 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000260 return false;
261 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000262}
263
Matthias Braun864cf582017-09-09 00:52:46 +0000264/// Set kill flags on last use of a virtual register.
265void RegAllocFast::addKillFlag(const LiveReg &LR) {
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000266 if (!LR.LastUse) return;
267 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000268 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
269 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000270 MO.setIsKill();
Quentin Colombet868ef842017-07-07 19:25:45 +0000271 // else, don't do anything we are problably redefining a
272 // subreg of this register and given we don't track which
273 // lanes are actually dead, we cannot insert a kill flag here.
274 // Otherwise we may end up in a situation like this:
275 // ... = (MO) physreg:sub1, physreg <implicit-use, kill>
276 // ... <== Here we would allow later pass to reuse physreg:sub1
277 // which is potentially wrong.
278 // LR:sub0 = ...
279 // ... = LR.sub1 <== This is going to use physreg:sub1
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000280 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000281}
282
Matthias Braun864cf582017-09-09 00:52:46 +0000283/// Mark virtreg as no longer available.
284void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000285 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000286 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
287 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000288 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000289 // Erase from LiveVirtRegs unless we're spilling in bulk.
290 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000291 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000292}
293
Matthias Braun864cf582017-09-09 00:52:46 +0000294/// Mark virtreg as no longer available.
295void RegAllocFast::killVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000296 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
297 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000298 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000299 if (LRI != LiveVirtRegs.end())
300 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000301}
302
Matthias Braun864cf582017-09-09 00:52:46 +0000303/// This method spills the value specified by VirtReg into the corresponding
304/// stack slot if needed.
305void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
306 unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000307 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
308 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000309 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000310 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
311 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000312}
313
Matthias Braun864cf582017-09-09 00:52:46 +0000314/// Do the actual work of spilling.
315void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
316 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000317 LiveReg &LR = *LRI;
318 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000319
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000320 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000321 // If this physreg is used by the instruction, we want to kill it on the
322 // instruction, not on the spill.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000323 bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000324 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000325 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000326 << " in " << PrintReg(LR.PhysReg, TRI));
Matthias Braun864cf582017-09-09 00:52:46 +0000327 const TargetRegisterClass &RC = *MRI->getRegClass(LRI->VirtReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000328 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000329 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000330 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, &RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000331 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000332
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000333 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000334 // identify spilled location as the place to find corresponding variable's
335 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000336 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000337 LiveDbgValueMap[LRI->VirtReg];
Matthias Braun864cf582017-09-09 00:52:46 +0000338 for (MachineInstr *DBG : LRIDbgValues) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000339 MachineInstr *NewDV = buildDbgValueForSpill(*MBB, MI, *DBG, FI);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000340 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000341 (void)NewDV;
342 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000343 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000344 // Now this register is spilled there is should not be any DBG_VALUE
345 // pointing to this register because they are all pointing to spilled value
346 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000347 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000348 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000349 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000350 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000351 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000352}
353
Matthias Braun864cf582017-09-09 00:52:46 +0000354/// Spill all dirty virtregs without killing them.
355void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000356 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000357 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000358 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
359 // of spilling here is deterministic, if arbitrary.
Matthias Braun864cf582017-09-09 00:52:46 +0000360 for (LiveRegMap::iterator I = LiveVirtRegs.begin(), E = LiveVirtRegs.end();
361 I != E; ++I)
362 spillVirtReg(MI, I);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000363 LiveVirtRegs.clear();
364 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000365}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000366
Matthias Braun864cf582017-09-09 00:52:46 +0000367/// Handle the direct use of a physical register. Check that the register is
368/// not used by a virtreg. Kill the physreg, marking it free. This may add
369/// implicit kills to MO->getParent() and invalidate MO.
370void RegAllocFast::usePhysReg(MachineOperand &MO) {
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000371 // Ignore undef uses.
372 if (MO.isUndef())
373 return;
374
Matthias Braun864cf582017-09-09 00:52:46 +0000375 unsigned PhysReg = MO.getReg();
376 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
377 "Bad usePhysReg operand");
378
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000379 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000380 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000381 case regDisabled:
382 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000383 case regReserved:
384 PhysRegState[PhysReg] = regFree;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000385 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000386 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000387 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000388 return;
389 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000390 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000391 // wanted has been clobbered.
392 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000393 }
394
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000395 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000396 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000397 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000398 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000399 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000400 break;
401 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000402 // Either PhysReg is a subregister of Alias and we mark the
403 // whole register as free, or PhysReg is the superregister of
404 // Alias and we mark all the aliases as disabled before freeing
405 // PhysReg.
406 // In the latter case, since PhysReg was disabled, this means that
407 // its value is defined only by physical sub-registers. This check
408 // is performed by the assert of the default case in this loop.
409 // Note: The value of the superregister may only be partial
410 // defined, that is why regDisabled is a valid state for aliases.
411 assert((TRI->isSuperRegister(PhysReg, Alias) ||
412 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000413 "Instruction is not using a subregister of a reserved register");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000414 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000415 case regFree:
416 if (TRI->isSuperRegister(PhysReg, Alias)) {
417 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000418 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000419 MO.getParent()->addRegisterKilled(Alias, TRI, true);
420 return;
421 }
422 // Some other alias was in the working set - clear it.
423 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000424 break;
425 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000426 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000427 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000428 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000429
430 // All aliases are disabled, bring register into working set.
431 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000432 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000433}
434
Matthias Braun864cf582017-09-09 00:52:46 +0000435/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
436/// similar to defineVirtReg except the physreg is reserved instead of
437/// allocated.
438void RegAllocFast::definePhysReg(MachineInstr &MI, MCPhysReg PhysReg,
439 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000440 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000441 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
442 case regDisabled:
443 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000444 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000445 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000446 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000447 case regFree:
448 case regReserved:
449 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000450 return;
451 }
452
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000453 // This is a disabled register, disable all aliases.
454 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000455 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000456 MCPhysReg Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000457 switch (unsigned VirtReg = PhysRegState[Alias]) {
458 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000459 break;
460 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000461 spillVirtReg(MI, VirtReg);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000462 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000463 case regFree:
464 case regReserved:
465 PhysRegState[Alias] = regDisabled;
466 if (TRI->isSuperRegister(PhysReg, Alias))
467 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000468 break;
469 }
470 }
471}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000472
Matthias Braun864cf582017-09-09 00:52:46 +0000473/// \brief Return the cost of spilling clearing out PhysReg and aliases so it is
474/// free for allocation. Returns 0 when PhysReg is free or disabled with all
475/// aliases disabled - it can be allocated directly.
476/// \returns spillImpossible when PhysReg or an alias can't be spilled.
477unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000478 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000479 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000480 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000481 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000482 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
483 case regDisabled:
484 break;
485 case regFree:
486 return 0;
487 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000488 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
489 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000490 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000491 default: {
492 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
493 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
494 return I->Dirty ? spillDirty : spillClean;
495 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000496 }
497
Eric Christopherc3783362011-04-12 00:48:08 +0000498 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000499 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000500 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000501 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
Matthias Braun864cf582017-09-09 00:52:46 +0000502 MCPhysReg Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000503 switch (unsigned VirtReg = PhysRegState[Alias]) {
504 case regDisabled:
505 break;
506 case regFree:
507 ++Cost;
508 break;
509 case regReserved:
510 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000511 default: {
512 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
513 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
514 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000515 break;
516 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000517 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000518 }
519 return Cost;
520}
521
Matthias Braun864cf582017-09-09 00:52:46 +0000522/// \brief This method updates local state so that we know that PhysReg is the
523/// proper container for VirtReg now. The physical register must not be used
524/// for anything else when this is called.
525void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000526 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000527 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000528 PhysRegState[PhysReg] = LR.VirtReg;
529 assert(!LR.PhysReg && "Already assigned a physreg");
530 LR.PhysReg = PhysReg;
531}
532
Matthias Braun864cf582017-09-09 00:52:46 +0000533RegAllocFast::LiveRegMap::iterator
534RegAllocFast::assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000535 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
536 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
537 assignVirtToPhysReg(*LRI, PhysReg);
538 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000539}
540
Matthias Braun864cf582017-09-09 00:52:46 +0000541/// Allocates a physical register for VirtReg.
542RegAllocFast::LiveRegMap::iterator RegAllocFast::allocVirtReg(MachineInstr &MI,
543 LiveRegMap::iterator LRI, unsigned Hint) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000544 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000545
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000546 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
547 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000548
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000549 // Take hint when possible.
Matthias Braun864cf582017-09-09 00:52:46 +0000550 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
551 if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
552 MRI->isAllocatable(Hint) && RC.contains(Hint)) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000553 // Ignore the hint if we would have to spill a dirty register.
554 unsigned Cost = calcSpillCost(Hint);
555 if (Cost < spillDirty) {
556 if (Cost)
557 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000558 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
559 // That invalidates LRI, so run a new lookup for VirtReg.
560 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000561 }
562 }
563
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000564 // First try to find a completely free register.
Matthias Braun864cf582017-09-09 00:52:46 +0000565 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(&RC);
566 for (MCPhysReg PhysReg : AO) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000567 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000568 assignVirtToPhysReg(*LRI, PhysReg);
569 return LRI;
570 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000571 }
572
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000573 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Matthias Braun864cf582017-09-09 00:52:46 +0000574 << TRI->getRegClassName(&RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000575
Matthias Braun864cf582017-09-09 00:52:46 +0000576 unsigned BestReg = 0;
577 unsigned BestCost = spillImpossible;
578 for (MCPhysReg PhysReg : AO) {
579 unsigned Cost = calcSpillCost(PhysReg);
580 DEBUG(dbgs() << "\tRegister: " << PrintReg(PhysReg, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000581 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
582 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000583 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000584 if (Cost == 0) {
Matthias Braun864cf582017-09-09 00:52:46 +0000585 assignVirtToPhysReg(*LRI, PhysReg);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000586 return LRI;
587 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000588 if (Cost < BestCost)
Matthias Braun864cf582017-09-09 00:52:46 +0000589 BestReg = PhysReg, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000590 }
591
592 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000593 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000594 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
595 // That invalidates LRI, so run a new lookup for VirtReg.
596 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000597 }
598
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000599 // Nothing we can do. Report an error and keep going with a bad allocation.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000600 if (MI.isInlineAsm())
601 MI.emitError("inline assembly requires more registers than available");
Benjamin Kramer7200a462013-10-05 19:33:37 +0000602 else
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000603 MI.emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000604 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000605 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000606}
607
Matthias Braun864cf582017-09-09 00:52:46 +0000608/// Allocates a register for VirtReg and mark it as dirty.
609RegAllocFast::LiveRegMap::iterator RegAllocFast::defineVirtReg(MachineInstr &MI,
610 unsigned OpNum,
611 unsigned VirtReg,
612 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000613 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
614 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000615 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000616 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000617 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000618 if (New) {
619 // If there is no hint, peek at the only use of this register.
620 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
621 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000622 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000623 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000624 if (UseMI.isCopyLike())
625 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000626 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000627 LRI = allocVirtReg(MI, LRI, Hint);
628 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000629 // Redefining a live register - kill at the last use, unless it is this
630 // instruction defining VirtReg multiple times.
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000631 if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000632 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000633 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000634 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000635 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000636 LRI->LastOpNum = OpNum;
637 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000638 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000639 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000640}
641
Matthias Braun864cf582017-09-09 00:52:46 +0000642/// Make sure VirtReg is available in a physreg and return it.
643RegAllocFast::LiveRegMap::iterator RegAllocFast::reloadVirtReg(MachineInstr &MI,
644 unsigned OpNum,
645 unsigned VirtReg,
646 unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000647 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
648 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000649 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000650 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000651 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000652 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000653 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000654 LRI = allocVirtReg(MI, LRI, Hint);
Matthias Braun864cf582017-09-09 00:52:46 +0000655 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000656 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000657 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000658 << PrintReg(LRI->PhysReg, TRI) << "\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000659 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, &RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000660 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000661 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000662 if (isLastUseOfLocalReg(MO)) {
663 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000664 if (MO.isUse())
665 MO.setIsKill();
666 else
667 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000668 } else if (MO.isKill()) {
669 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
670 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000671 } else if (MO.isDead()) {
672 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
673 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000674 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000675 } else if (MO.isKill()) {
676 // We must remove kill flags from uses of reloaded registers because the
677 // register would be killed immediately, and there might be a second use:
678 // %foo = OR %x<kill>, %x
679 // This would cause a second reload of %x into a different register.
680 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
681 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000682 } else if (MO.isDead()) {
683 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
684 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000685 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000686 assert(LRI->PhysReg && "Register not assigned");
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000687 LRI->LastUse = &MI;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000688 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000689 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000690 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000691}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000692
Matthias Braun864cf582017-09-09 00:52:46 +0000693/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
694/// may invalidate any operand pointers. Return true if the operand kills its
695/// register.
696bool RegAllocFast::setPhysReg(MachineInstr &MI, unsigned OpNum,
697 MCPhysReg PhysReg) {
698 MachineOperand &MO = MI.getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000699 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000700 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000701 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000702 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000703 }
704
705 // Handle subregister index.
706 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
707 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000708
709 // A kill flag implies killing the full register. Add corresponding super
710 // register kill.
711 if (MO.isKill()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000712 MI.addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000713 return true;
714 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000715
716 // A <def,read-undef> of a sub-register requires an implicit def of the full
717 // register.
718 if (MO.isDef() && MO.isUndef())
Matthias Braun864cf582017-09-09 00:52:46 +0000719 MI.addRegisterDefined(PhysReg, TRI);
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000720
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000721 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000722}
723
Matthias Braun864cf582017-09-09 00:52:46 +0000724// Handles special instruction operand like early clobbers and tied ops when
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000725// there are additional physreg defines.
Matthias Braun864cf582017-09-09 00:52:46 +0000726void RegAllocFast::handleThroughOperands(MachineInstr &MI,
727 SmallVectorImpl<unsigned> &VirtDead) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000728 DEBUG(dbgs() << "Scanning for through registers:");
729 SmallSet<unsigned, 8> ThroughRegs;
Matthias Braun864cf582017-09-09 00:52:46 +0000730 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000731 if (!MO.isReg()) continue;
732 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000733 if (!TargetRegisterInfo::isVirtualRegister(Reg))
734 continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000735 if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
736 (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000737 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000738 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000739 }
740 }
741
742 // If any physreg defines collide with preallocated through registers,
743 // we must spill and reallocate.
744 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000745 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000746 if (!MO.isReg() || !MO.isDef()) continue;
747 unsigned Reg = MO.getReg();
748 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000749 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000750 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000751 if (ThroughRegs.count(PhysRegState[*AI]))
Matthias Braun864cf582017-09-09 00:52:46 +0000752 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000753 }
754 }
755
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000756 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000757 DEBUG(dbgs() << "Allocating tied uses.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000758 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
759 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000760 if (!MO.isReg()) continue;
761 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000762 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000763 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000764 if (!MO.isTied()) continue;
Matthias Braun864cf582017-09-09 00:52:46 +0000765 DEBUG(dbgs() << "Operand " << I << "("<< MO << ") is tied to operand "
Matthias Braun6b2b88b2017-09-09 01:16:59 +0000766 << MI.findTiedOperandIdx(I) << ".\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000767 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
768 MCPhysReg PhysReg = LRI->PhysReg;
769 setPhysReg(MI, I, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000770 // Note: we don't update the def operand yet. That would cause the normal
771 // def-scan to attempt spilling.
Matthias Braun864cf582017-09-09 00:52:46 +0000772 } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000773 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
774 // Reload the register, but don't assign to the operand just yet.
775 // That would confuse the later phys-def processing pass.
Matthias Braun864cf582017-09-09 00:52:46 +0000776 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000777 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000778 }
779 }
780
Rafael Espindola2021f382011-11-22 06:27:18 +0000781 DEBUG(dbgs() << "Allocating early clobbers.\n");
Matthias Braun864cf582017-09-09 00:52:46 +0000782 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
783 const MachineOperand &MO = MI.getOperand(I);
Rafael Espindola2021f382011-11-22 06:27:18 +0000784 if (!MO.isReg()) continue;
785 unsigned Reg = MO.getReg();
786 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
787 if (!MO.isEarlyClobber())
788 continue;
789 // Note: defineVirtReg may invalidate MO.
Matthias Braun864cf582017-09-09 00:52:46 +0000790 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, 0);
791 MCPhysReg PhysReg = LRI->PhysReg;
792 if (setPhysReg(MI, I, PhysReg))
Rafael Espindola2021f382011-11-22 06:27:18 +0000793 VirtDead.push_back(Reg);
794 }
795
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000796 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000797 UsedInInstr.clear();
Matthias Braun864cf582017-09-09 00:52:46 +0000798 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000799 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
800 unsigned Reg = MO.getReg();
801 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000802 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
803 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000804 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000805 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000806
807 // Also mark PartialDefs as used to avoid reallocation.
Matthias Braun864cf582017-09-09 00:52:46 +0000808 for (unsigned PartialDef : PartialDefs)
809 markRegUsedInInstr(PartialDef);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000810}
811
Matthias Braun864cf582017-09-09 00:52:46 +0000812#ifndef NDEBUG
813void RegAllocFast::dumpState() {
814 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
815 if (PhysRegState[Reg] == regDisabled) continue;
816 dbgs() << " " << TRI->getName(Reg);
817 switch(PhysRegState[Reg]) {
818 case regFree:
819 break;
820 case regReserved:
821 dbgs() << "*";
822 break;
823 default: {
824 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
825 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
826 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
827 if (I->Dirty)
828 dbgs() << "*";
829 assert(I->PhysReg == Reg && "Bad inverse map");
830 break;
831 }
832 }
833 }
834 dbgs() << '\n';
835 // Check that LiveVirtRegs is the inverse.
836 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
837 e = LiveVirtRegs.end(); i != e; ++i) {
838 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
839 "Bad map key");
840 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
841 "Bad map value");
842 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
843 }
844}
845#endif
846
847void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
848 this->MBB = &MBB;
849 DEBUG(dbgs() << "\nAllocating " << MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000850
851 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000852 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000853
Matthias Braun864cf582017-09-09 00:52:46 +0000854 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000855
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000856 // Add live-in registers as live.
Matthias Braun864cf582017-09-09 00:52:46 +0000857 for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
Matthias Braund9da1622015-09-09 18:08:03 +0000858 if (MRI->isAllocatable(LI.PhysReg))
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000859 definePhysReg(*MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000860
Matthias Brauna09d18d2017-09-09 00:52:45 +0000861 VirtDead.clear();
862 Coalesced.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000863
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000864 // Otherwise, sequentially allocate each instruction in the MBB.
Matthias Braun864cf582017-09-09 00:52:46 +0000865 for (MachineInstr &MI : MBB) {
866 const MCInstrDesc &MCID = MI.getDesc();
867 DEBUG(
868 dbgs() << "\n>> " << MI << "Regs:";
869 dumpState()
870 );
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000871
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000872 // Debug values are not allowed to change codegen in any way.
Matthias Braun864cf582017-09-09 00:52:46 +0000873 if (MI.isDebugValue()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000874 MachineInstr *DebugMI = &MI;
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000875 MachineOperand &MO = DebugMI->getOperand(0);
876
877 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
878 // mostly constants and frame indices.
879 if (!MO.isReg())
880 continue;
881 unsigned Reg = MO.getReg();
882 if (!TargetRegisterInfo::isVirtualRegister(Reg))
883 continue;
884
885 // See if this virtual register has already been allocated to a physical
886 // register or spilled to a stack slot.
887 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
888 if (LRI != LiveVirtRegs.end())
889 setPhysReg(*DebugMI, 0, LRI->PhysReg);
890 else {
891 int SS = StackSlotForVirtReg[Reg];
892 if (SS != -1) {
893 // Modify DBG_VALUE now that the value is in a spill slot.
894 updateDbgValueForSpill(*DebugMI, SS);
895 DEBUG(dbgs() << "Modifying debug info due to spill:"
896 << "\t" << *DebugMI);
897 continue;
Devang Patel57e72372010-07-09 21:48:31 +0000898 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000899
900 // We can't allocate a physreg for a DebugValue, sorry!
901 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
902 MO.setReg(0);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000903 }
Reid Kleckner9e6c3092017-09-15 21:49:56 +0000904
905 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
906 // that future spills of Reg will have DBG_VALUEs.
907 LiveDbgValueMap[Reg].push_back(DebugMI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000908 continue;
909 }
910
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000911 // If this is a copy, we may be able to coalesce.
Matthias Braun864cf582017-09-09 00:52:46 +0000912 unsigned CopySrcReg = 0;
913 unsigned CopyDstReg = 0;
914 unsigned CopySrcSub = 0;
915 unsigned CopyDstSub = 0;
916 if (MI.isCopy()) {
917 CopyDstReg = MI.getOperand(0).getReg();
918 CopySrcReg = MI.getOperand(1).getReg();
919 CopyDstSub = MI.getOperand(0).getSubReg();
920 CopySrcSub = MI.getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000921 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000922
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000923 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000924 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000925
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000926 // First scan.
927 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000928 // Find the end of the virtreg operands
929 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000930 bool hasTiedOps = false;
931 bool hasEarlyClobbers = false;
932 bool hasPartialRedefs = false;
933 bool hasPhysDefs = false;
Matthias Braun864cf582017-09-09 00:52:46 +0000934 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
935 MachineOperand &MO = MI.getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000936 // Make sure MRI knows about registers clobbered by regmasks.
937 if (MO.isRegMask()) {
938 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
939 continue;
940 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000941 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000942 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000943 if (!Reg) continue;
944 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
945 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000946 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000947 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000948 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000949 } else {
950 if (MO.isEarlyClobber())
951 hasEarlyClobbers = true;
Matthias Braun864cf582017-09-09 00:52:46 +0000952 if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000953 hasPartialRedefs = true;
954 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000955 continue;
956 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000957 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000958 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000959 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000960 } else if (MO.isEarlyClobber()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000961 definePhysReg(MI, Reg,
Duncan P. N. Exon Smith44ed0de2016-07-01 15:03:37 +0000962 (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000963 hasEarlyClobbers = true;
964 } else
965 hasPhysDefs = true;
966 }
967
968 // The instruction may have virtual register operands that must be allocated
969 // the same register at use-time and def-time: early clobbers and tied
970 // operands. If there are also physical defs, these registers must avoid
971 // both physical defs and uses, making them more constrained than normal
972 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000973 // Similarly, if there are multiple defs and tied operands, we must make
974 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000975 // We didn't detect inline asm tied operands above, so just make this extra
976 // pass for all inline asm.
Matthias Braun864cf582017-09-09 00:52:46 +0000977 if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000978 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000979 handleThroughOperands(MI, VirtDead);
980 // Don't attempt coalescing when we have funny stuff going on.
Matthias Braun864cf582017-09-09 00:52:46 +0000981 CopyDstReg = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000982 // Pretend we have early clobbers so the use operands get marked below.
983 // This is not necessary for the common case of a single tied use.
984 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000985 }
986
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000987 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000988 // Allocate virtreg uses.
Matthias Braun864cf582017-09-09 00:52:46 +0000989 for (unsigned I = 0; I != VirtOpEnd; ++I) {
990 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000991 if (!MO.isReg()) continue;
992 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000993 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000994 if (MO.isUse()) {
Matthias Braun864cf582017-09-09 00:52:46 +0000995 LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, CopyDstReg);
996 MCPhysReg PhysReg = LRI->PhysReg;
997 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
998 if (setPhysReg(MI, I, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000999 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001000 }
1001 }
1002
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001003 // Track registers defined by instruction - early clobbers and tied uses at
1004 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001005 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001006 if (hasEarlyClobbers) {
Matthias Braun864cf582017-09-09 00:52:46 +00001007 for (const MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001008 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001009 unsigned Reg = MO.getReg();
1010 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001011 // Look for physreg defs and tied uses.
Matthias Braun864cf582017-09-09 00:52:46 +00001012 if (!MO.isDef() && !MO.isTied()) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001013 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001014 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001015 }
1016
Matthias Braun864cf582017-09-09 00:52:46 +00001017 unsigned DefOpEnd = MI.getNumOperands();
1018 if (MI.isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001019 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001020 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001021 // registers in their spill slots.
1022 // Note: although this is appealing to just consider all definitions
1023 // as call-clobbered, this is not correct because some of those
1024 // definitions may be used later on and we do not want to reuse
1025 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001026 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1027 spillAll(MI);
1028 }
1029
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001030 // Third scan.
1031 // Allocate defs and collect dead defs.
Matthias Braun864cf582017-09-09 00:52:46 +00001032 for (unsigned I = 0; I != DefOpEnd; ++I) {
1033 const MachineOperand &MO = MI.getOperand(I);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001034 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1035 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001036 unsigned Reg = MO.getReg();
1037
1038 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001039 if (!MRI->isAllocatable(Reg)) continue;
Matthias Braun864cf582017-09-09 00:52:46 +00001040 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001041 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001042 }
Matthias Braun864cf582017-09-09 00:52:46 +00001043 LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, CopySrcReg);
1044 MCPhysReg PhysReg = LRI->PhysReg;
1045 if (setPhysReg(MI, I, PhysReg)) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001046 VirtDead.push_back(Reg);
Matthias Braun864cf582017-09-09 00:52:46 +00001047 CopyDstReg = 0; // cancel coalescing;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001048 } else
Matthias Braun864cf582017-09-09 00:52:46 +00001049 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001050 }
1051
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001052 // Kill dead defs after the scan to ensure that multiple defs of the same
1053 // register are allocated identically. We didn't need to do this for uses
1054 // because we are crerating our own kill flags, and they are always at the
1055 // last use.
Matthias Braun864cf582017-09-09 00:52:46 +00001056 for (unsigned VirtReg : VirtDead)
1057 killVirtReg(VirtReg);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001058 VirtDead.clear();
1059
Matthias Braun864cf582017-09-09 00:52:46 +00001060 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1061 DEBUG(dbgs() << "-- coalescing: " << MI);
1062 Coalesced.push_back(&MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001063 } else {
Matthias Braun864cf582017-09-09 00:52:46 +00001064 DEBUG(dbgs() << "<< " << MI);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001065 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001066 }
1067
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001068 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001069 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
Matthias Braun864cf582017-09-09 00:52:46 +00001070 spillAll(MBB.getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001071
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001072 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001073 // LiveVirtRegs might refer to the instrs.
Matthias Braun864cf582017-09-09 00:52:46 +00001074 for (MachineInstr *MI : Coalesced)
1075 MBB.erase(MI);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001076 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001077
Matthias Braun864cf582017-09-09 00:52:46 +00001078 DEBUG(MBB.dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001079}
1080
Matthias Braun864cf582017-09-09 00:52:46 +00001081/// Allocates registers for a function.
1082bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001083 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
Matthias Braun864cf582017-09-09 00:52:46 +00001084 << "********** Function: " << MF.getName() << '\n');
1085 MRI = &MF.getRegInfo();
1086 const TargetSubtargetInfo &STI = MF.getSubtarget();
1087 TRI = STI.getRegisterInfo();
1088 TII = STI.getInstrInfo();
1089 MFI = &MF.getFrameInfo();
1090 MRI->freezeReservedRegs(MF);
1091 RegClassInfo.runOnMachineFunction(MF);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001092 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001093 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001094
1095 // initialize the virtual->physical register map to have a 'null'
1096 // mapping for all virtual registers
Matthias Braun864cf582017-09-09 00:52:46 +00001097 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1098 StackSlotForVirtReg.resize(NumVirtRegs);
1099 LiveVirtRegs.setUniverse(NumVirtRegs);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001100
1101 // Loop over all of the basic blocks, eliminating virtual register references
Matthias Braun864cf582017-09-09 00:52:46 +00001102 for (MachineBasicBlock &MBB : MF)
1103 allocateBasicBlock(MBB);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001104
Andrew Trickda84e642012-02-21 04:51:23 +00001105 // All machine operands and other references to virtual registers have been
1106 // replaced. Remove the virtual registers.
1107 MRI->clearVirtRegs();
1108
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001109 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001110 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001111 return true;
1112}
1113
1114FunctionPass *llvm::createFastRegisterAllocator() {
Matthias Braun864cf582017-09-09 00:52:46 +00001115 return new RegAllocFast();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001116}