blob: daeff3fc3963fe2681088120946720273662cd20 [file] [log] [blame]
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +00001//===-- RegAllocBasic.cpp - Basic Register Allocator ----------------------===//
Andrew Trick14e8d712010-10-22 23:09:15 +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//
10// This file defines the RABasic function pass, which provides a minimal
11// implementation of the basic register allocator.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000015#include "AllocationOrder.h"
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +000016#include "LiveDebugVariables.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "RegAllocBase.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000018#include "Spiller.h"
Andrew Trick8a83d542010-11-11 17:46:29 +000019#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000020#include "llvm/CodeGen/CalcSpillWeights.h"
Matthias Braunfa621d22017-12-13 02:51:04 +000021#include "llvm/CodeGen/LiveIntervals.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000022#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000023#include "llvm/CodeGen/LiveRegMatrix.h"
Matthias Braun209f0482017-12-18 23:19:44 +000024#include "llvm/CodeGen/LiveStacks.h"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000025#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/CodeGen/MachineLoopInfo.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000030#include "llvm/CodeGen/Passes.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000031#include "llvm/CodeGen/RegAllocRegistry.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000033#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000034#include "llvm/PassAnalysisSupport.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000037#include <cstdlib>
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000038#include <queue>
Andrew Tricke16eecc2010-10-26 18:34:01 +000039
Andrew Trick14e8d712010-10-22 23:09:15 +000040using namespace llvm;
41
Chandler Carruth8677f2f2014-04-22 02:02:50 +000042#define DEBUG_TYPE "regalloc"
43
Andrew Trick14e8d712010-10-22 23:09:15 +000044static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
45 createBasicRegisterAllocator);
46
Benjamin Kramerc62feda2010-11-25 16:42:51 +000047namespace {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000048 struct CompSpillWeight {
49 bool operator()(LiveInterval *A, LiveInterval *B) const {
50 return A->weight < B->weight;
51 }
52 };
53}
54
55namespace {
Andrew Trick14e8d712010-10-22 23:09:15 +000056/// RABasic provides a minimal implementation of the basic register allocation
57/// algorithm. It prioritizes live virtual registers by spill weight and spills
58/// whenever a register is unavailable. This is not practical in production but
59/// provides a useful baseline both for measuring other allocators and comparing
60/// the speed of the basic algorithm against other styles of allocators.
Quentin Colombet1067c962017-06-02 22:46:31 +000061class RABasic : public MachineFunctionPass,
62 public RegAllocBase,
63 private LiveRangeEdit::Delegate {
Andrew Trick14e8d712010-10-22 23:09:15 +000064 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000065 MachineFunction *MF;
Andrew Trick14e8d712010-10-22 23:09:15 +000066
Andrew Trick14e8d712010-10-22 23:09:15 +000067 // state
Ahmed Charlesf4ccd112014-03-06 05:51:42 +000068 std::unique_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000069 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
70 CompSpillWeight> Queue;
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +000071
72 // Scratch space. Allocated here to avoid repeated malloc calls in
73 // selectOrSplit().
74 BitVector UsableRegs;
75
Quentin Colombet1067c962017-06-02 22:46:31 +000076 bool LRE_CanEraseVirtReg(unsigned) override;
77 void LRE_WillShrinkVirtReg(unsigned) override;
78
Andrew Trick14e8d712010-10-22 23:09:15 +000079public:
80 RABasic();
81
82 /// Return the pass name.
Mehdi Amini67f335d2016-10-01 02:56:57 +000083 StringRef getPassName() const override { return "Basic Register Allocator"; }
Andrew Trick14e8d712010-10-22 23:09:15 +000084
85 /// RABasic analysis usage.
Craig Topper9f998de2014-03-07 09:26:03 +000086 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick14e8d712010-10-22 23:09:15 +000087
Craig Topper9f998de2014-03-07 09:26:03 +000088 void releaseMemory() override;
Andrew Trick14e8d712010-10-22 23:09:15 +000089
Craig Topper9f998de2014-03-07 09:26:03 +000090 Spiller &spiller() override { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000091
Craig Topper9f998de2014-03-07 09:26:03 +000092 void enqueue(LiveInterval *LI) override {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000093 Queue.push(LI);
94 }
95
Craig Topper9f998de2014-03-07 09:26:03 +000096 LiveInterval *dequeue() override {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000097 if (Queue.empty())
Craig Topper4ba84432014-04-14 00:51:57 +000098 return nullptr;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000099 LiveInterval *LI = Queue.top();
100 Queue.pop();
101 return LI;
102 }
103
Craig Topper9f998de2014-03-07 09:26:03 +0000104 unsigned selectOrSplit(LiveInterval &VirtReg,
105 SmallVectorImpl<unsigned> &SplitVRegs) override;
Andrew Trick14e8d712010-10-22 23:09:15 +0000106
107 /// Perform register allocation.
Craig Topper9f998de2014-03-07 09:26:03 +0000108 bool runOnMachineFunction(MachineFunction &mf) override;
Andrew Trick14e8d712010-10-22 23:09:15 +0000109
Matthias Braundb9ce2f2016-08-23 21:19:49 +0000110 MachineFunctionProperties getRequiredProperties() const override {
111 return MachineFunctionProperties().set(
112 MachineFunctionProperties::Property::NoPHIs);
113 }
114
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000115 // Helper for spilling all live virtual registers currently unified under preg
116 // that interfere with the most recently queried lvr. Return true if spilling
117 // was successful, and append any new spilled/split intervals to splitLVRs.
118 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000119 SmallVectorImpl<unsigned> &SplitVRegs);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000120
Andrew Trick14e8d712010-10-22 23:09:15 +0000121 static char ID;
122};
123
124char RABasic::ID = 0;
125
126} // end anonymous namespace
127
Quentin Colombet9f43f262017-06-02 22:46:26 +0000128char &llvm::RABasicID = RABasic::ID;
129
130INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",
131 false, false)
132INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
133INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
134INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
135INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
136INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
137INITIALIZE_PASS_DEPENDENCY(LiveStacks)
138INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
139INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
140INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
141INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
142INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false,
143 false)
144
Quentin Colombet1067c962017-06-02 22:46:31 +0000145bool RABasic::LRE_CanEraseVirtReg(unsigned VirtReg) {
Jonas Paulsson79131592017-09-15 07:47:38 +0000146 LiveInterval &LI = LIS->getInterval(VirtReg);
Quentin Colombet1067c962017-06-02 22:46:31 +0000147 if (VRM->hasPhys(VirtReg)) {
Quentin Colombet1067c962017-06-02 22:46:31 +0000148 Matrix->unassign(LI);
149 aboutToRemoveInterval(LI);
150 return true;
151 }
152 // Unassigned virtreg is probably in the priority queue.
153 // RegAllocBase will erase it after dequeueing.
Jonas Paulsson79131592017-09-15 07:47:38 +0000154 // Nonetheless, clear the live-range so that the debug
155 // dump will show the right state for that VirtReg.
156 LI.clear();
Quentin Colombet1067c962017-06-02 22:46:31 +0000157 return false;
158}
159
160void RABasic::LRE_WillShrinkVirtReg(unsigned VirtReg) {
161 if (!VRM->hasPhys(VirtReg))
162 return;
163
164 // Register is assigned, put it back on the queue for reassignment.
165 LiveInterval &LI = LIS->getInterval(VirtReg);
166 Matrix->unassign(LI);
167 enqueue(&LI);
168}
169
Andrew Trick14e8d712010-10-22 23:09:15 +0000170RABasic::RABasic(): MachineFunctionPass(ID) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000171}
172
Andrew Trick18c57a82010-11-30 23:18:47 +0000173void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
174 AU.setPreservesCFG();
Chandler Carruth91468332015-09-09 17:55:00 +0000175 AU.addRequired<AAResultsWrapperPass>();
176 AU.addPreserved<AAResultsWrapperPass>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000177 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000178 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000179 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000180 AU.addRequired<LiveDebugVariables>();
181 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000182 AU.addRequired<LiveStacks>();
183 AU.addPreserved<LiveStacks>();
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000184 AU.addRequired<MachineBlockFrequencyInfo>();
185 AU.addPreserved<MachineBlockFrequencyInfo>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000186 AU.addRequiredID(MachineDominatorsID);
187 AU.addPreservedID(MachineDominatorsID);
188 AU.addRequired<MachineLoopInfo>();
189 AU.addPreserved<MachineLoopInfo>();
190 AU.addRequired<VirtRegMap>();
191 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000192 AU.addRequired<LiveRegMatrix>();
193 AU.addPreserved<LiveRegMatrix>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000194 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000195}
196
197void RABasic::releaseMemory() {
David Blaikieec31a302014-07-19 01:05:11 +0000198 SpillerInstance.reset();
Andrew Trick14e8d712010-10-22 23:09:15 +0000199}
200
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000201
202// Spill or split all live virtual registers currently unified under PhysReg
203// that interfere with VirtReg. The newly spilled or split live intervals are
204// returned by appending them to SplitVRegs.
205bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000206 SmallVectorImpl<unsigned> &SplitVRegs) {
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000207 // Record each interference and determine if all are spillable before mutating
208 // either the union or live intervals.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000209 SmallVector<LiveInterval*, 8> Intfs;
210
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000211 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000212 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
213 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
214 Q.collectInterferingVRegs();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000215 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
216 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
217 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
218 return false;
219 Intfs.push_back(Intf);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000220 }
221 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000222 LLVM_DEBUG(dbgs() << "spilling " << printReg(PhysReg, TRI)
223 << " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000224 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000225
226 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000227 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
228 LiveInterval &Spill = *Intfs[i];
229
230 // Skip duplicates.
231 if (!VRM->hasPhys(Spill.reg))
232 continue;
233
234 // Deallocate the interfering vreg by removing it from the union.
235 // A LiveInterval instance may not be in a union during modification!
236 Matrix->unassign(Spill);
237
238 // Spill the extracted interval.
Quentin Colombet1067c962017-06-02 22:46:31 +0000239 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats);
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000240 spiller().spill(LRE);
241 }
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000242 return true;
243}
244
Andrew Trick14e8d712010-10-22 23:09:15 +0000245// Driver for the register assignment and splitting heuristics.
246// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000247//
Andrew Trick18c57a82010-11-30 23:18:47 +0000248// This is a minimal implementation of register assignment and splitting that
249// spills whenever we run out of registers.
Andrew Trick14e8d712010-10-22 23:09:15 +0000250//
251// selectOrSplit can only be called once per live virtual register. We then do a
252// single interference test for each register the correct class until we find an
253// available register. So, the number of interference tests in the worst case is
254// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trick18c57a82010-11-30 23:18:47 +0000255// minimal, there is no value in caching them outside the scope of
256// selectOrSplit().
257unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000258 SmallVectorImpl<unsigned> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000259 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000260 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000261
Andrew Trick13bdbb02010-11-20 02:43:55 +0000262 // Check for an available register in this class.
Matthias Braun2aa57272015-07-15 22:16:00 +0000263 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix);
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000264 while (unsigned PhysReg = Order.next()) {
265 // Check for interference in PhysReg
266 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
267 case LiveRegMatrix::IK_Free:
268 // PhysReg is available, allocate it.
269 return PhysReg;
Andrew Trick18c57a82010-11-30 23:18:47 +0000270
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000271 case LiveRegMatrix::IK_VirtReg:
272 // Only virtual registers in the way, we may be able to spill them.
273 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +0000274 continue;
275
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000276 default:
277 // RegMask or RegUnit interference.
278 continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000279 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000280 }
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000281
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000282 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000283 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000284 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
285 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
286 continue;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000287
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000288 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000289 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000290 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000291 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000292 }
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000293
Andrew Trick18c57a82010-11-30 23:18:47 +0000294 // No other spill candidates were found, so spill the current VirtReg.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000295 LLVM_DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000296 if (!VirtReg.isSpillable())
297 return ~0u;
Quentin Colombet1067c962017-06-02 22:46:31 +0000298 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000299 spiller().spill(LRE);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000300
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000301 // The live virtual register requesting allocation was spilled, so tell
302 // the caller not to allocate anything during this round.
303 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000304}
Andrew Trick14e8d712010-10-22 23:09:15 +0000305
Andrew Trick14e8d712010-10-22 23:09:15 +0000306bool RABasic::runOnMachineFunction(MachineFunction &mf) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000307 LLVM_DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
308 << "********** Function: " << mf.getName() << '\n');
Andrew Trick14e8d712010-10-22 23:09:15 +0000309
Andrew Trick18c57a82010-11-30 23:18:47 +0000310 MF = &mf;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000311 RegAllocBase::init(getAnalysis<VirtRegMap>(),
312 getAnalysis<LiveIntervals>(),
313 getAnalysis<LiveRegMatrix>());
Arnaud A. de Grandmaisona77da052013-11-10 17:46:31 +0000314
Robert Lougher0d87d632015-08-10 11:59:44 +0000315 calculateSpillWeightsAndHints(*LIS, *MF, VRM,
Arnaud A. de Grandmaison095f9942013-11-11 19:04:45 +0000316 getAnalysis<MachineLoopInfo>(),
317 getAnalysis<MachineBlockFrequencyInfo>());
Arnaud A. de Grandmaisona77da052013-11-10 17:46:31 +0000318
Jakob Stoklund Olesen84275962011-03-31 23:02:17 +0000319 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000320
Andrew Tricke16eecc2010-10-26 18:34:01 +0000321 allocatePhysRegs();
Wei Mi815b02e2016-04-13 03:08:27 +0000322 postOptimization();
Andrew Trick14e8d712010-10-22 23:09:15 +0000323
324 // Diagnostic output before rewriting
Nicola Zaghen0818e782018-05-14 12:53:11 +0000325 LLVM_DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000326
Andrew Tricke16eecc2010-10-26 18:34:01 +0000327 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000328 return true;
329}
330
Andrew Trick13bdbb02010-11-20 02:43:55 +0000331FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000332{
333 return new RABasic();
334}