blob: e8ecea98f5672ff9f92a84b4f9ee3065226a5b2a [file] [log] [blame]
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +00001//===-- RegAllocBasic.cpp - Basic Register Allocator ----------------------===//
Andrew Trick1c246052010-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
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +000016#include "AllocationOrder.h"
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +000017#include "LiveDebugVariables.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "RegAllocBase.h"
Andrew Trick1c246052010-10-22 23:09:15 +000019#include "Spiller.h"
Andrew Trickf11344d2010-11-11 17:46:29 +000020#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick1c246052010-10-22 23:09:15 +000021#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Trick35284652010-11-08 18:02:08 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000023#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000024#include "llvm/CodeGen/LiveRegMatrix.h"
Andrew Trick1c246052010-10-22 23:09:15 +000025#include "llvm/CodeGen/LiveStackAnalysis.h"
Benjamin Kramere2a1d892013-06-17 19:00:36 +000026#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Andrew Trick1c246052010-10-22 23:09:15 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineLoopInfo.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick1c246052010-10-22 23:09:15 +000031#include "llvm/CodeGen/RegAllocRegistry.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000032#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/PassAnalysisSupport.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
Andrew Trick84aef492010-10-26 18:34:01 +000036#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesendb357d72010-12-07 23:18:47 +000037#include <cstdlib>
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000038#include <queue>
Andrew Trick84aef492010-10-26 18:34:01 +000039
Andrew Trick1c246052010-10-22 23:09:15 +000040using namespace llvm;
41
Chandler Carruth1b9dde02014-04-22 02:02:50 +000042#define DEBUG_TYPE "regalloc"
43
Andrew Trick1c246052010-10-22 23:09:15 +000044static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
45 createBasicRegisterAllocator);
46
Benjamin Krameraef5bd02010-11-25 16:42:51 +000047namespace {
Jakob Stoklund Olesen2329c542011-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 Trick1c246052010-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.
61class RABasic : public MachineFunctionPass, public RegAllocBase
62{
63 // context
Andrew Trickfce64c92010-11-30 23:18:47 +000064 MachineFunction *MF;
Andrew Trick1c246052010-10-22 23:09:15 +000065
Andrew Trick1c246052010-10-22 23:09:15 +000066 // state
Ahmed Charles56440fd2014-03-06 05:51:42 +000067 std::unique_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000068 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
69 CompSpillWeight> Queue;
Jakob Stoklund Olesen0c1eea22012-02-08 18:54:35 +000070
71 // Scratch space. Allocated here to avoid repeated malloc calls in
72 // selectOrSplit().
73 BitVector UsableRegs;
74
Andrew Trick1c246052010-10-22 23:09:15 +000075public:
76 RABasic();
77
78 /// Return the pass name.
Mehdi Amini117296c2016-10-01 02:56:57 +000079 StringRef getPassName() const override { return "Basic Register Allocator"; }
Andrew Trick1c246052010-10-22 23:09:15 +000080
81 /// RABasic analysis usage.
Craig Topper4584cd52014-03-07 09:26:03 +000082 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick1c246052010-10-22 23:09:15 +000083
Craig Topper4584cd52014-03-07 09:26:03 +000084 void releaseMemory() override;
Andrew Trick1c246052010-10-22 23:09:15 +000085
Craig Topper4584cd52014-03-07 09:26:03 +000086 Spiller &spiller() override { return *SpillerInstance; }
Andrew Trick89eb6a82010-11-10 19:18:47 +000087
Craig Topper4584cd52014-03-07 09:26:03 +000088 void enqueue(LiveInterval *LI) override {
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000089 Queue.push(LI);
90 }
91
Craig Topper4584cd52014-03-07 09:26:03 +000092 LiveInterval *dequeue() override {
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000093 if (Queue.empty())
Craig Topperc0196b12014-04-14 00:51:57 +000094 return nullptr;
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000095 LiveInterval *LI = Queue.top();
96 Queue.pop();
97 return LI;
98 }
99
Craig Topper4584cd52014-03-07 09:26:03 +0000100 unsigned selectOrSplit(LiveInterval &VirtReg,
101 SmallVectorImpl<unsigned> &SplitVRegs) override;
Andrew Trick1c246052010-10-22 23:09:15 +0000102
103 /// Perform register allocation.
Craig Topper4584cd52014-03-07 09:26:03 +0000104 bool runOnMachineFunction(MachineFunction &mf) override;
Andrew Trick1c246052010-10-22 23:09:15 +0000105
Matthias Braun90799ce2016-08-23 21:19:49 +0000106 MachineFunctionProperties getRequiredProperties() const override {
107 return MachineFunctionProperties().set(
108 MachineFunctionProperties::Property::NoPHIs);
109 }
110
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000111 // Helper for spilling all live virtual registers currently unified under preg
112 // that interfere with the most recently queried lvr. Return true if spilling
113 // was successful, and append any new spilled/split intervals to splitLVRs.
114 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000115 SmallVectorImpl<unsigned> &SplitVRegs);
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000116
Andrew Trick1c246052010-10-22 23:09:15 +0000117 static char ID;
118};
119
120char RABasic::ID = 0;
121
122} // end anonymous namespace
123
Quentin Colombetebbaed62017-06-02 22:46:26 +0000124char &llvm::RABasicID = RABasic::ID;
125
126INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",
127 false, false)
128INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
129INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
130INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
131INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
132INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
133INITIALIZE_PASS_DEPENDENCY(LiveStacks)
134INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
135INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
136INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
137INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
138INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false,
139 false)
140
Andrew Trick1c246052010-10-22 23:09:15 +0000141RABasic::RABasic(): MachineFunctionPass(ID) {
Andrew Trick1c246052010-10-22 23:09:15 +0000142}
143
Andrew Trickfce64c92010-11-30 23:18:47 +0000144void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
145 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000146 AU.addRequired<AAResultsWrapperPass>();
147 AU.addPreserved<AAResultsWrapperPass>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000148 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000149 AU.addPreserved<LiveIntervals>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000150 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +0000151 AU.addRequired<LiveDebugVariables>();
152 AU.addPreserved<LiveDebugVariables>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000153 AU.addRequired<LiveStacks>();
154 AU.addPreserved<LiveStacks>();
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000155 AU.addRequired<MachineBlockFrequencyInfo>();
156 AU.addPreserved<MachineBlockFrequencyInfo>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000157 AU.addRequiredID(MachineDominatorsID);
158 AU.addPreservedID(MachineDominatorsID);
159 AU.addRequired<MachineLoopInfo>();
160 AU.addPreserved<MachineLoopInfo>();
161 AU.addRequired<VirtRegMap>();
162 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000163 AU.addRequired<LiveRegMatrix>();
164 AU.addPreserved<LiveRegMatrix>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000165 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick1c246052010-10-22 23:09:15 +0000166}
167
168void RABasic::releaseMemory() {
David Blaikieb61064e2014-07-19 01:05:11 +0000169 SpillerInstance.reset();
Andrew Trick1c246052010-10-22 23:09:15 +0000170}
171
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000172
173// Spill or split all live virtual registers currently unified under PhysReg
174// that interfere with VirtReg. The newly spilled or split live intervals are
175// returned by appending them to SplitVRegs.
176bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000177 SmallVectorImpl<unsigned> &SplitVRegs) {
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000178 // Record each interference and determine if all are spillable before mutating
179 // either the union or live intervals.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000180 SmallVector<LiveInterval*, 8> Intfs;
181
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000182 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000183 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
184 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
185 Q.collectInterferingVRegs();
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000186 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
187 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
188 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
189 return false;
190 Intfs.push_back(Intf);
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000191 }
192 }
193 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
194 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000195 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000196
197 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000198 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
199 LiveInterval &Spill = *Intfs[i];
200
201 // Skip duplicates.
202 if (!VRM->hasPhys(Spill.reg))
203 continue;
204
205 // Deallocate the interfering vreg by removing it from the union.
206 // A LiveInterval instance may not be in a union during modification!
207 Matrix->unassign(Spill);
208
209 // Spill the extracted interval.
Wei Mi9a16d652016-04-13 03:08:27 +0000210 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM, nullptr, &DeadRemats);
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000211 spiller().spill(LRE);
212 }
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000213 return true;
214}
215
Andrew Trick1c246052010-10-22 23:09:15 +0000216// Driver for the register assignment and splitting heuristics.
217// Manages iteration over the LiveIntervalUnions.
Andrew Trick799ec1c2010-11-20 02:43:55 +0000218//
Andrew Trickfce64c92010-11-30 23:18:47 +0000219// This is a minimal implementation of register assignment and splitting that
220// spills whenever we run out of registers.
Andrew Trick1c246052010-10-22 23:09:15 +0000221//
222// selectOrSplit can only be called once per live virtual register. We then do a
223// single interference test for each register the correct class until we find an
224// available register. So, the number of interference tests in the worst case is
225// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trickfce64c92010-11-30 23:18:47 +0000226// minimal, there is no value in caching them outside the scope of
227// selectOrSplit().
228unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000229 SmallVectorImpl<unsigned> &SplitVRegs) {
Andrew Trick89eb6a82010-11-10 19:18:47 +0000230 // Populate a list of physical register spill candidates.
Andrew Trickfce64c92010-11-30 23:18:47 +0000231 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Trick35284652010-11-08 18:02:08 +0000232
Andrew Trick799ec1c2010-11-20 02:43:55 +0000233 // Check for an available register in this class.
Matthias Braun5d1f12d2015-07-15 22:16:00 +0000234 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix);
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000235 while (unsigned PhysReg = Order.next()) {
236 // Check for interference in PhysReg
237 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
238 case LiveRegMatrix::IK_Free:
239 // PhysReg is available, allocate it.
240 return PhysReg;
Andrew Trickfce64c92010-11-30 23:18:47 +0000241
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000242 case LiveRegMatrix::IK_VirtReg:
243 // Only virtual registers in the way, we may be able to spill them.
244 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesen0c1eea22012-02-08 18:54:35 +0000245 continue;
246
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000247 default:
248 // RegMask or RegUnit interference.
249 continue;
Andrew Trick35284652010-11-08 18:02:08 +0000250 }
Andrew Trick1c246052010-10-22 23:09:15 +0000251 }
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000252
Andrew Trick89eb6a82010-11-10 19:18:47 +0000253 // Try to spill another interfering reg with less spill weight.
Andrew Trickfce64c92010-11-30 23:18:47 +0000254 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000255 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
256 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
257 continue;
Andrew Trick89eb6a82010-11-10 19:18:47 +0000258
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000259 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesenfb207c12010-12-07 18:51:27 +0000260 "Interference after spill.");
Andrew Trick89eb6a82010-11-10 19:18:47 +0000261 // Tell the caller to allocate to this newly freed physical register.
Andrew Trickfce64c92010-11-30 23:18:47 +0000262 return *PhysRegI;
Andrew Trick35284652010-11-08 18:02:08 +0000263 }
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +0000264
Andrew Trickfce64c92010-11-30 23:18:47 +0000265 // No other spill candidates were found, so spill the current VirtReg.
266 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +0000267 if (!VirtReg.isSpillable())
268 return ~0u;
Wei Mi9a16d652016-04-13 03:08:27 +0000269 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, nullptr, &DeadRemats);
Jakob Stoklund Olesen4d6eafa2011-03-10 01:51:42 +0000270 spiller().spill(LRE);
Andrew Trick799ec1c2010-11-20 02:43:55 +0000271
Andrew Trick89eb6a82010-11-10 19:18:47 +0000272 // The live virtual register requesting allocation was spilled, so tell
273 // the caller not to allocate anything during this round.
274 return 0;
Andrew Trick35284652010-11-08 18:02:08 +0000275}
Andrew Trick1c246052010-10-22 23:09:15 +0000276
Andrew Trick1c246052010-10-22 23:09:15 +0000277bool RABasic::runOnMachineFunction(MachineFunction &mf) {
278 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
279 << "********** Function: "
David Blaikiec8c29202012-08-22 17:18:53 +0000280 << mf.getName() << '\n');
Andrew Trick1c246052010-10-22 23:09:15 +0000281
Andrew Trickfce64c92010-11-30 23:18:47 +0000282 MF = &mf;
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +0000283 RegAllocBase::init(getAnalysis<VirtRegMap>(),
284 getAnalysis<LiveIntervals>(),
285 getAnalysis<LiveRegMatrix>());
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +0000286
Robert Lougher11a44b72015-08-10 11:59:44 +0000287 calculateSpillWeightsAndHints(*LIS, *MF, VRM,
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000288 getAnalysis<MachineLoopInfo>(),
289 getAnalysis<MachineBlockFrequencyInfo>());
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +0000290
Jakob Stoklund Olesen6e597dc2011-03-31 23:02:17 +0000291 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick799ec1c2010-11-20 02:43:55 +0000292
Andrew Trick84aef492010-10-26 18:34:01 +0000293 allocatePhysRegs();
Wei Mi9a16d652016-04-13 03:08:27 +0000294 postOptimization();
Andrew Trick1c246052010-10-22 23:09:15 +0000295
296 // Diagnostic output before rewriting
Andrew Trickfce64c92010-11-30 23:18:47 +0000297 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick1c246052010-10-22 23:09:15 +0000298
Andrew Trick84aef492010-10-26 18:34:01 +0000299 releaseMemory();
Andrew Trick1c246052010-10-22 23:09:15 +0000300 return true;
301}
302
Andrew Trick799ec1c2010-11-20 02:43:55 +0000303FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick1c246052010-10-22 23:09:15 +0000304{
305 return new RABasic();
306}