blob: a558e371ad4c687fe8b7806180c3f4ba38f44d04 [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
Andrew Trick1c246052010-10-22 23:09:15 +0000124RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +0000125 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick1c246052010-10-22 23:09:15 +0000126 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
127 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola676c4052011-06-26 22:34:10 +0000128 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Tricke1c034f2012-01-17 06:55:03 +0000129 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick1c246052010-10-22 23:09:15 +0000130 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenc9132012010-11-03 20:39:26 +0000131 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick1c246052010-10-22 23:09:15 +0000132 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
133 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000134 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick1c246052010-10-22 23:09:15 +0000135}
136
Andrew Trickfce64c92010-11-30 23:18:47 +0000137void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
138 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000139 AU.addRequired<AAResultsWrapperPass>();
140 AU.addPreserved<AAResultsWrapperPass>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000141 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000142 AU.addPreserved<LiveIntervals>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000143 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +0000144 AU.addRequired<LiveDebugVariables>();
145 AU.addPreserved<LiveDebugVariables>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000146 AU.addRequired<LiveStacks>();
147 AU.addPreserved<LiveStacks>();
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000148 AU.addRequired<MachineBlockFrequencyInfo>();
149 AU.addPreserved<MachineBlockFrequencyInfo>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000150 AU.addRequiredID(MachineDominatorsID);
151 AU.addPreservedID(MachineDominatorsID);
152 AU.addRequired<MachineLoopInfo>();
153 AU.addPreserved<MachineLoopInfo>();
154 AU.addRequired<VirtRegMap>();
155 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000156 AU.addRequired<LiveRegMatrix>();
157 AU.addPreserved<LiveRegMatrix>();
Andrew Trickfce64c92010-11-30 23:18:47 +0000158 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick1c246052010-10-22 23:09:15 +0000159}
160
161void RABasic::releaseMemory() {
David Blaikieb61064e2014-07-19 01:05:11 +0000162 SpillerInstance.reset();
Andrew Trick1c246052010-10-22 23:09:15 +0000163}
164
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000165
166// Spill or split all live virtual registers currently unified under PhysReg
167// that interfere with VirtReg. The newly spilled or split live intervals are
168// returned by appending them to SplitVRegs.
169bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000170 SmallVectorImpl<unsigned> &SplitVRegs) {
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000171 // Record each interference and determine if all are spillable before mutating
172 // either the union or live intervals.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000173 SmallVector<LiveInterval*, 8> Intfs;
174
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000175 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000176 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
177 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
178 Q.collectInterferingVRegs();
179 if (Q.seenUnspillableVReg())
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000180 return false;
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000181 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
182 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
183 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
184 return false;
185 Intfs.push_back(Intf);
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000186 }
187 }
188 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
189 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000190 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000191
192 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000193 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
194 LiveInterval &Spill = *Intfs[i];
195
196 // Skip duplicates.
197 if (!VRM->hasPhys(Spill.reg))
198 continue;
199
200 // Deallocate the interfering vreg by removing it from the union.
201 // A LiveInterval instance may not be in a union during modification!
202 Matrix->unassign(Spill);
203
204 // Spill the extracted interval.
Wei Mi9a16d652016-04-13 03:08:27 +0000205 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM, nullptr, &DeadRemats);
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000206 spiller().spill(LRE);
207 }
Jakob Stoklund Olesen73edbf12012-01-11 22:52:14 +0000208 return true;
209}
210
Andrew Trick1c246052010-10-22 23:09:15 +0000211// Driver for the register assignment and splitting heuristics.
212// Manages iteration over the LiveIntervalUnions.
Andrew Trick799ec1c2010-11-20 02:43:55 +0000213//
Andrew Trickfce64c92010-11-30 23:18:47 +0000214// This is a minimal implementation of register assignment and splitting that
215// spills whenever we run out of registers.
Andrew Trick1c246052010-10-22 23:09:15 +0000216//
217// selectOrSplit can only be called once per live virtual register. We then do a
218// single interference test for each register the correct class until we find an
219// available register. So, the number of interference tests in the worst case is
220// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trickfce64c92010-11-30 23:18:47 +0000221// minimal, there is no value in caching them outside the scope of
222// selectOrSplit().
223unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000224 SmallVectorImpl<unsigned> &SplitVRegs) {
Andrew Trick89eb6a82010-11-10 19:18:47 +0000225 // Populate a list of physical register spill candidates.
Andrew Trickfce64c92010-11-30 23:18:47 +0000226 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Trick35284652010-11-08 18:02:08 +0000227
Andrew Trick799ec1c2010-11-20 02:43:55 +0000228 // Check for an available register in this class.
Matthias Braun5d1f12d2015-07-15 22:16:00 +0000229 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix);
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000230 while (unsigned PhysReg = Order.next()) {
231 // Check for interference in PhysReg
232 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
233 case LiveRegMatrix::IK_Free:
234 // PhysReg is available, allocate it.
235 return PhysReg;
Andrew Trickfce64c92010-11-30 23:18:47 +0000236
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000237 case LiveRegMatrix::IK_VirtReg:
238 // Only virtual registers in the way, we may be able to spill them.
239 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesen0c1eea22012-02-08 18:54:35 +0000240 continue;
241
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000242 default:
243 // RegMask or RegUnit interference.
244 continue;
Andrew Trick35284652010-11-08 18:02:08 +0000245 }
Andrew Trick1c246052010-10-22 23:09:15 +0000246 }
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000247
Andrew Trick89eb6a82010-11-10 19:18:47 +0000248 // Try to spill another interfering reg with less spill weight.
Andrew Trickfce64c92010-11-30 23:18:47 +0000249 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000250 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
251 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
252 continue;
Andrew Trick89eb6a82010-11-10 19:18:47 +0000253
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +0000254 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesenfb207c12010-12-07 18:51:27 +0000255 "Interference after spill.");
Andrew Trick89eb6a82010-11-10 19:18:47 +0000256 // Tell the caller to allocate to this newly freed physical register.
Andrew Trickfce64c92010-11-30 23:18:47 +0000257 return *PhysRegI;
Andrew Trick35284652010-11-08 18:02:08 +0000258 }
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +0000259
Andrew Trickfce64c92010-11-30 23:18:47 +0000260 // No other spill candidates were found, so spill the current VirtReg.
261 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +0000262 if (!VirtReg.isSpillable())
263 return ~0u;
Wei Mi9a16d652016-04-13 03:08:27 +0000264 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, nullptr, &DeadRemats);
Jakob Stoklund Olesen4d6eafa2011-03-10 01:51:42 +0000265 spiller().spill(LRE);
Andrew Trick799ec1c2010-11-20 02:43:55 +0000266
Andrew Trick89eb6a82010-11-10 19:18:47 +0000267 // The live virtual register requesting allocation was spilled, so tell
268 // the caller not to allocate anything during this round.
269 return 0;
Andrew Trick35284652010-11-08 18:02:08 +0000270}
Andrew Trick1c246052010-10-22 23:09:15 +0000271
Andrew Trick1c246052010-10-22 23:09:15 +0000272bool RABasic::runOnMachineFunction(MachineFunction &mf) {
273 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
274 << "********** Function: "
David Blaikiec8c29202012-08-22 17:18:53 +0000275 << mf.getName() << '\n');
Andrew Trick1c246052010-10-22 23:09:15 +0000276
Andrew Trickfce64c92010-11-30 23:18:47 +0000277 MF = &mf;
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +0000278 RegAllocBase::init(getAnalysis<VirtRegMap>(),
279 getAnalysis<LiveIntervals>(),
280 getAnalysis<LiveRegMatrix>());
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +0000281
Robert Lougher11a44b72015-08-10 11:59:44 +0000282 calculateSpillWeightsAndHints(*LIS, *MF, VRM,
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000283 getAnalysis<MachineLoopInfo>(),
284 getAnalysis<MachineBlockFrequencyInfo>());
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +0000285
Jakob Stoklund Olesen6e597dc2011-03-31 23:02:17 +0000286 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick799ec1c2010-11-20 02:43:55 +0000287
Andrew Trick84aef492010-10-26 18:34:01 +0000288 allocatePhysRegs();
Wei Mi9a16d652016-04-13 03:08:27 +0000289 postOptimization();
Andrew Trick1c246052010-10-22 23:09:15 +0000290
291 // Diagnostic output before rewriting
Andrew Trickfce64c92010-11-30 23:18:47 +0000292 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick1c246052010-10-22 23:09:15 +0000293
Andrew Trick84aef492010-10-26 18:34:01 +0000294 releaseMemory();
Andrew Trick1c246052010-10-22 23:09:15 +0000295 return true;
296}
297
Andrew Trick799ec1c2010-11-20 02:43:55 +0000298FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick1c246052010-10-22 23:09:15 +0000299{
300 return new RABasic();
301}