blob: 46f6946f70031143b293118b7179e35044614835 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Trick1c246052010-10-22 23:09:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the RABasic function pass, which provides a minimal
10// implementation of the basic register allocator.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +000014#include "AllocationOrder.h"
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +000015#include "LiveDebugVariables.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "RegAllocBase.h"
Andrew Trick1c246052010-10-22 23:09:15 +000017#include "Spiller.h"
Andrew Trickf11344d2010-11-11 17:46:29 +000018#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick1c246052010-10-22 23:09:15 +000019#include "llvm/CodeGen/CalcSpillWeights.h"
Matthias Braunf8422972017-12-13 02:51:04 +000020#include "llvm/CodeGen/LiveIntervals.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000021#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000022#include "llvm/CodeGen/LiveRegMatrix.h"
Matthias Braunef959692017-12-18 23:19:44 +000023#include "llvm/CodeGen/LiveStacks.h"
Benjamin Kramere2a1d892013-06-17 19:00:36 +000024#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Andrew Trick1c246052010-10-22 23:09:15 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineLoopInfo.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000029#include "llvm/CodeGen/Passes.h"
Andrew Trick1c246052010-10-22 23:09:15 +000030#include "llvm/CodeGen/RegAllocRegistry.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000031#include "llvm/CodeGen/TargetRegisterInfo.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"
Jakob Stoklund Olesendb357d72010-12-07 23:18:47 +000036#include <cstdlib>
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000037#include <queue>
Andrew Trick84aef492010-10-26 18:34:01 +000038
Andrew Trick1c246052010-10-22 23:09:15 +000039using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "regalloc"
42
Andrew Trick1c246052010-10-22 23:09:15 +000043static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
44 createBasicRegisterAllocator);
45
Benjamin Krameraef5bd02010-11-25 16:42:51 +000046namespace {
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000047 struct CompSpillWeight {
48 bool operator()(LiveInterval *A, LiveInterval *B) const {
49 return A->weight < B->weight;
50 }
51 };
52}
53
54namespace {
Andrew Trick1c246052010-10-22 23:09:15 +000055/// RABasic provides a minimal implementation of the basic register allocation
56/// algorithm. It prioritizes live virtual registers by spill weight and spills
57/// whenever a register is unavailable. This is not practical in production but
58/// provides a useful baseline both for measuring other allocators and comparing
59/// the speed of the basic algorithm against other styles of allocators.
Quentin Colombet2145cf32017-06-02 22:46:31 +000060class RABasic : public MachineFunctionPass,
61 public RegAllocBase,
62 private LiveRangeEdit::Delegate {
Andrew Trick1c246052010-10-22 23:09:15 +000063 // 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
Quentin Colombet2145cf32017-06-02 22:46:31 +000075 bool LRE_CanEraseVirtReg(unsigned) override;
76 void LRE_WillShrinkVirtReg(unsigned) override;
77
Andrew Trick1c246052010-10-22 23:09:15 +000078public:
79 RABasic();
80
81 /// Return the pass name.
Mehdi Amini117296c2016-10-01 02:56:57 +000082 StringRef getPassName() const override { return "Basic Register Allocator"; }
Andrew Trick1c246052010-10-22 23:09:15 +000083
84 /// RABasic analysis usage.
Craig Topper4584cd52014-03-07 09:26:03 +000085 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick1c246052010-10-22 23:09:15 +000086
Craig Topper4584cd52014-03-07 09:26:03 +000087 void releaseMemory() override;
Andrew Trick1c246052010-10-22 23:09:15 +000088
Craig Topper4584cd52014-03-07 09:26:03 +000089 Spiller &spiller() override { return *SpillerInstance; }
Andrew Trick89eb6a82010-11-10 19:18:47 +000090
Craig Topper4584cd52014-03-07 09:26:03 +000091 void enqueue(LiveInterval *LI) override {
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000092 Queue.push(LI);
93 }
94
Craig Topper4584cd52014-03-07 09:26:03 +000095 LiveInterval *dequeue() override {
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000096 if (Queue.empty())
Craig Topperc0196b12014-04-14 00:51:57 +000097 return nullptr;
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000098 LiveInterval *LI = Queue.top();
99 Queue.pop();
100 return LI;
101 }
102
Craig Topper4584cd52014-03-07 09:26:03 +0000103 unsigned selectOrSplit(LiveInterval &VirtReg,
104 SmallVectorImpl<unsigned> &SplitVRegs) override;
Andrew Trick1c246052010-10-22 23:09:15 +0000105
106 /// Perform register allocation.
Craig Topper4584cd52014-03-07 09:26:03 +0000107 bool runOnMachineFunction(MachineFunction &mf) override;
Andrew Trick1c246052010-10-22 23:09:15 +0000108
Matthias Braun90799ce2016-08-23 21:19:49 +0000109 MachineFunctionProperties getRequiredProperties() const override {
110 return MachineFunctionProperties().set(
111 MachineFunctionProperties::Property::NoPHIs);
112 }
113
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000114 // Helper for spilling all live virtual registers currently unified under preg
115 // that interfere with the most recently queried lvr. Return true if spilling
116 // was successful, and append any new spilled/split intervals to splitLVRs.
117 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000118 SmallVectorImpl<unsigned> &SplitVRegs);
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000119
Andrew Trick1c246052010-10-22 23:09:15 +0000120 static char ID;
121};
122
123char RABasic::ID = 0;
124
125} // end anonymous namespace
126
Quentin Colombetebbaed62017-06-02 22:46:26 +0000127char &llvm::RABasicID = RABasic::ID;
128
129INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",
130 false, false)
131INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
132INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
133INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
134INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
135INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
136INITIALIZE_PASS_DEPENDENCY(LiveStacks)
137INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
138INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
139INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
140INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
141INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false,
142 false)
143
Quentin Colombet2145cf32017-06-02 22:46:31 +0000144bool RABasic::LRE_CanEraseVirtReg(unsigned VirtReg) {
Jonas Paulsson6188f322017-09-15 07:47:38 +0000145 LiveInterval &LI = LIS->getInterval(VirtReg);
Quentin Colombet2145cf32017-06-02 22:46:31 +0000146 if (VRM->hasPhys(VirtReg)) {
Quentin Colombet2145cf32017-06-02 22:46:31 +0000147 Matrix->unassign(LI);
148 aboutToRemoveInterval(LI);
149 return true;
150 }
151 // Unassigned virtreg is probably in the priority queue.
152 // RegAllocBase will erase it after dequeueing.
Jonas Paulsson6188f322017-09-15 07:47:38 +0000153 // Nonetheless, clear the live-range so that the debug
154 // dump will show the right state for that VirtReg.
155 LI.clear();
Quentin Colombet2145cf32017-06-02 22:46:31 +0000156 return false;
157}
158
159void RABasic::LRE_WillShrinkVirtReg(unsigned VirtReg) {
160 if (!VRM->hasPhys(VirtReg))
161 return;
162
163 // Register is assigned, put it back on the queue for reassignment.
164 LiveInterval &LI = LIS->getInterval(VirtReg);
165 Matrix->unassign(LI);
166 enqueue(&LI);
167}
168
Andrew Trick1c246052010-10-22 23:09:15 +0000169RABasic::RABasic(): MachineFunctionPass(ID) {
Andrew Trick1c246052010-10-22 23:09:15 +0000170}
171
Andrew Trickfce64c92010-11-30 23:18:47 +0000172void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
173 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000174 AU.addRequired<AAResultsWrapperPass>();
175 AU.addPreserved<AAResultsWrapperPass>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000176 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000177 AU.addPreserved<LiveIntervals>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000178 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +0000179 AU.addRequired<LiveDebugVariables>();
180 AU.addPreserved<LiveDebugVariables>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000181 AU.addRequired<LiveStacks>();
182 AU.addPreserved<LiveStacks>();
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000183 AU.addRequired<MachineBlockFrequencyInfo>();
184 AU.addPreserved<MachineBlockFrequencyInfo>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000185 AU.addRequiredID(MachineDominatorsID);
186 AU.addPreservedID(MachineDominatorsID);
187 AU.addRequired<MachineLoopInfo>();
188 AU.addPreserved<MachineLoopInfo>();
189 AU.addRequired<VirtRegMap>();
190 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000191 AU.addRequired<LiveRegMatrix>();
192 AU.addPreserved<LiveRegMatrix>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000193 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick1c246052010-10-22 23:09:15 +0000194}
195
196void RABasic::releaseMemory() {
David Blaikieb61064e2014-07-19 01:05:11 +0000197 SpillerInstance.reset();
Andrew Trick1c246052010-10-22 23:09:15 +0000198}
199
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000200
201// Spill or split all live virtual registers currently unified under PhysReg
202// that interfere with VirtReg. The newly spilled or split live intervals are
203// returned by appending them to SplitVRegs.
204bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000205 SmallVectorImpl<unsigned> &SplitVRegs) {
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000206 // Record each interference and determine if all are spillable before mutating
207 // either the union or live intervals.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000208 SmallVector<LiveInterval*, 8> Intfs;
209
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000210 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000211 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
212 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
213 Q.collectInterferingVRegs();
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000214 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
215 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
216 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
217 return false;
218 Intfs.push_back(Intf);
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000219 }
220 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000221 LLVM_DEBUG(dbgs() << "spilling " << printReg(PhysReg, TRI)
222 << " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000223 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000224
225 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000226 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
227 LiveInterval &Spill = *Intfs[i];
228
229 // Skip duplicates.
230 if (!VRM->hasPhys(Spill.reg))
231 continue;
232
233 // Deallocate the interfering vreg by removing it from the union.
234 // A LiveInterval instance may not be in a union during modification!
235 Matrix->unassign(Spill);
236
237 // Spill the extracted interval.
Quentin Colombet2145cf32017-06-02 22:46:31 +0000238 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats);
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000239 spiller().spill(LRE);
240 }
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000241 return true;
242}
243
Andrew Trick1c246052010-10-22 23:09:15 +0000244// Driver for the register assignment and splitting heuristics.
245// Manages iteration over the LiveIntervalUnions.
Andrew Trick799ec1c2010-11-20 02:43:55 +0000246//
Andrew Trickfce64c92010-11-30 23:18:47 +0000247// This is a minimal implementation of register assignment and splitting that
248// spills whenever we run out of registers.
Andrew Trick1c246052010-10-22 23:09:15 +0000249//
250// selectOrSplit can only be called once per live virtual register. We then do a
251// single interference test for each register the correct class until we find an
252// available register. So, the number of interference tests in the worst case is
253// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trickfce64c92010-11-30 23:18:47 +0000254// minimal, there is no value in caching them outside the scope of
255// selectOrSplit().
256unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000257 SmallVectorImpl<unsigned> &SplitVRegs) {
Andrew Trick89eb6a82010-11-10 19:18:47 +0000258 // Populate a list of physical register spill candidates.
Andrew Trickfce64c92010-11-30 23:18:47 +0000259 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Trick35284652010-11-08 18:02:08 +0000260
Andrew Trick799ec1c2010-11-20 02:43:55 +0000261 // Check for an available register in this class.
Matthias Braun5d1f12d2015-07-15 22:16:00 +0000262 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix);
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000263 while (unsigned PhysReg = Order.next()) {
264 // Check for interference in PhysReg
265 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
266 case LiveRegMatrix::IK_Free:
267 // PhysReg is available, allocate it.
268 return PhysReg;
Andrew Trickfce64c92010-11-30 23:18:47 +0000269
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000270 case LiveRegMatrix::IK_VirtReg:
271 // Only virtual registers in the way, we may be able to spill them.
272 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesen0c1eea22012-02-08 18:54:35 +0000273 continue;
274
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000275 default:
276 // RegMask or RegUnit interference.
277 continue;
Andrew Trick35284652010-11-08 18:02:08 +0000278 }
Andrew Trick1c246052010-10-22 23:09:15 +0000279 }
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000280
Andrew Trick89eb6a82010-11-10 19:18:47 +0000281 // Try to spill another interfering reg with less spill weight.
Andrew Trickfce64c92010-11-30 23:18:47 +0000282 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000283 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
284 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
285 continue;
Andrew Trick89eb6a82010-11-10 19:18:47 +0000286
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000287 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesenfb207c12010-12-07 18:51:27 +0000288 "Interference after spill.");
Andrew Trick89eb6a82010-11-10 19:18:47 +0000289 // Tell the caller to allocate to this newly freed physical register.
Andrew Trickfce64c92010-11-30 23:18:47 +0000290 return *PhysRegI;
Andrew Trick35284652010-11-08 18:02:08 +0000291 }
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +0000292
Andrew Trickfce64c92010-11-30 23:18:47 +0000293 // No other spill candidates were found, so spill the current VirtReg.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000294 LLVM_DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +0000295 if (!VirtReg.isSpillable())
296 return ~0u;
Quentin Colombet2145cf32017-06-02 22:46:31 +0000297 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats);
Jakob Stoklund Olesen4d6eafa2011-03-10 01:51:42 +0000298 spiller().spill(LRE);
Andrew Trick799ec1c2010-11-20 02:43:55 +0000299
Andrew Trick89eb6a82010-11-10 19:18:47 +0000300 // The live virtual register requesting allocation was spilled, so tell
301 // the caller not to allocate anything during this round.
302 return 0;
Andrew Trick35284652010-11-08 18:02:08 +0000303}
Andrew Trick1c246052010-10-22 23:09:15 +0000304
Andrew Trick1c246052010-10-22 23:09:15 +0000305bool RABasic::runOnMachineFunction(MachineFunction &mf) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000306 LLVM_DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
307 << "********** Function: " << mf.getName() << '\n');
Andrew Trick1c246052010-10-22 23:09:15 +0000308
Andrew Trickfce64c92010-11-30 23:18:47 +0000309 MF = &mf;
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +0000310 RegAllocBase::init(getAnalysis<VirtRegMap>(),
311 getAnalysis<LiveIntervals>(),
312 getAnalysis<LiveRegMatrix>());
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +0000313
Robert Lougher11a44b72015-08-10 11:59:44 +0000314 calculateSpillWeightsAndHints(*LIS, *MF, VRM,
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000315 getAnalysis<MachineLoopInfo>(),
316 getAnalysis<MachineBlockFrequencyInfo>());
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +0000317
Jakob Stoklund Olesen6e597dc2011-03-31 23:02:17 +0000318 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick799ec1c2010-11-20 02:43:55 +0000319
Andrew Trick84aef492010-10-26 18:34:01 +0000320 allocatePhysRegs();
Wei Mi9a16d652016-04-13 03:08:27 +0000321 postOptimization();
Andrew Trick1c246052010-10-22 23:09:15 +0000322
323 // Diagnostic output before rewriting
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000324 LLVM_DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick1c246052010-10-22 23:09:15 +0000325
Andrew Trick84aef492010-10-26 18:34:01 +0000326 releaseMemory();
Andrew Trick1c246052010-10-22 23:09:15 +0000327 return true;
328}
329
Andrew Trick799ec1c2010-11-20 02:43:55 +0000330FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick1c246052010-10-22 23:09:15 +0000331{
332 return new RABasic();
333}