blob: b8c04fcdbf3864286bcd16412625be63ec723b96 [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
15#define DEBUG_TYPE "regalloc"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000017#include "AllocationOrder.h"
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +000018#include "LiveDebugVariables.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "RegAllocBase.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000020#include "Spiller.h"
Andrew Trick8a83d542010-11-11 17:46:29 +000021#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000022#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Tricke141a492010-11-08 18:02:08 +000023#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000024#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000025#include "llvm/CodeGen/LiveRegMatrix.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000026#include "llvm/CodeGen/LiveStackAnalysis.h"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000027#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineLoopInfo.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000032#include "llvm/CodeGen/RegAllocRegistry.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"
Andrew Trick14e8d712010-10-22 23:09:15 +000037#include "llvm/Target/TargetMachine.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000038#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000039#include <cstdlib>
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000040#include <queue>
Andrew Tricke16eecc2010-10-26 18:34:01 +000041
Andrew Trick14e8d712010-10-22 23:09:15 +000042using namespace llvm;
43
44static 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.
61class RABasic : public MachineFunctionPass, public RegAllocBase
62{
63 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000064 MachineFunction *MF;
Andrew Trick14e8d712010-10-22 23:09:15 +000065
Andrew Trick14e8d712010-10-22 23:09:15 +000066 // state
Stephen Hines36b56882014-04-23 16:57:46 -070067 std::unique_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000068 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
69 CompSpillWeight> Queue;
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +000070
71 // Scratch space. Allocated here to avoid repeated malloc calls in
72 // selectOrSplit().
73 BitVector UsableRegs;
74
Andrew Trick14e8d712010-10-22 23:09:15 +000075public:
76 RABasic();
77
78 /// Return the pass name.
Stephen Hines36b56882014-04-23 16:57:46 -070079 const char* getPassName() const override {
Andrew Trick14e8d712010-10-22 23:09:15 +000080 return "Basic Register Allocator";
81 }
82
83 /// RABasic analysis usage.
Stephen Hines36b56882014-04-23 16:57:46 -070084 void getAnalysisUsage(AnalysisUsage &AU) const override;
Andrew Trick14e8d712010-10-22 23:09:15 +000085
Stephen Hines36b56882014-04-23 16:57:46 -070086 void releaseMemory() override;
Andrew Trick14e8d712010-10-22 23:09:15 +000087
Stephen Hines36b56882014-04-23 16:57:46 -070088 Spiller &spiller() override { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000089
Stephen Hines36b56882014-04-23 16:57:46 -070090 void enqueue(LiveInterval *LI) override {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000091 Queue.push(LI);
92 }
93
Stephen Hines36b56882014-04-23 16:57:46 -070094 LiveInterval *dequeue() override {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000095 if (Queue.empty())
96 return 0;
97 LiveInterval *LI = Queue.top();
98 Queue.pop();
99 return LI;
100 }
101
Stephen Hines36b56882014-04-23 16:57:46 -0700102 unsigned selectOrSplit(LiveInterval &VirtReg,
103 SmallVectorImpl<unsigned> &SplitVRegs) override;
Andrew Trick14e8d712010-10-22 23:09:15 +0000104
105 /// Perform register allocation.
Stephen Hines36b56882014-04-23 16:57:46 -0700106 bool runOnMachineFunction(MachineFunction &mf) override;
Andrew Trick14e8d712010-10-22 23:09:15 +0000107
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000108 // Helper for spilling all live virtual registers currently unified under preg
109 // that interfere with the most recently queried lvr. Return true if spilling
110 // was successful, and append any new spilled/split intervals to splitLVRs.
111 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000112 SmallVectorImpl<unsigned> &SplitVRegs);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000113
Andrew Trick14e8d712010-10-22 23:09:15 +0000114 static char ID;
115};
116
117char RABasic::ID = 0;
118
119} // end anonymous namespace
120
Andrew Trick14e8d712010-10-22 23:09:15 +0000121RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000122 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000123 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
124 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000125 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000126 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000127 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000128 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000129 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
130 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000131 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000132}
133
Andrew Trick18c57a82010-11-30 23:18:47 +0000134void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
135 AU.setPreservesCFG();
136 AU.addRequired<AliasAnalysis>();
137 AU.addPreserved<AliasAnalysis>();
138 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000139 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000140 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000141 AU.addRequired<LiveDebugVariables>();
142 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000143 AU.addRequired<LiveStacks>();
144 AU.addPreserved<LiveStacks>();
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000145 AU.addRequired<MachineBlockFrequencyInfo>();
146 AU.addPreserved<MachineBlockFrequencyInfo>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000147 AU.addRequiredID(MachineDominatorsID);
148 AU.addPreservedID(MachineDominatorsID);
149 AU.addRequired<MachineLoopInfo>();
150 AU.addPreserved<MachineLoopInfo>();
151 AU.addRequired<VirtRegMap>();
152 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000153 AU.addRequired<LiveRegMatrix>();
154 AU.addPreserved<LiveRegMatrix>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000155 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000156}
157
158void RABasic::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000159 SpillerInstance.reset(0);
Andrew Trick14e8d712010-10-22 23:09:15 +0000160}
161
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000162
163// Spill or split all live virtual registers currently unified under PhysReg
164// that interfere with VirtReg. The newly spilled or split live intervals are
165// returned by appending them to SplitVRegs.
166bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000167 SmallVectorImpl<unsigned> &SplitVRegs) {
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000168 // Record each interference and determine if all are spillable before mutating
169 // either the union or live intervals.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000170 SmallVector<LiveInterval*, 8> Intfs;
171
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000172 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000173 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
174 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
175 Q.collectInterferingVRegs();
176 if (Q.seenUnspillableVReg())
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000177 return false;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000178 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
179 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
180 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
181 return false;
182 Intfs.push_back(Intf);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000183 }
184 }
185 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
186 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000187 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000188
189 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000190 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
191 LiveInterval &Spill = *Intfs[i];
192
193 // Skip duplicates.
194 if (!VRM->hasPhys(Spill.reg))
195 continue;
196
197 // Deallocate the interfering vreg by removing it from the union.
198 // A LiveInterval instance may not be in a union during modification!
199 Matrix->unassign(Spill);
200
201 // Spill the extracted interval.
202 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM);
203 spiller().spill(LRE);
204 }
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000205 return true;
206}
207
Andrew Trick14e8d712010-10-22 23:09:15 +0000208// Driver for the register assignment and splitting heuristics.
209// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000210//
Andrew Trick18c57a82010-11-30 23:18:47 +0000211// This is a minimal implementation of register assignment and splitting that
212// spills whenever we run out of registers.
Andrew Trick14e8d712010-10-22 23:09:15 +0000213//
214// selectOrSplit can only be called once per live virtual register. We then do a
215// single interference test for each register the correct class until we find an
216// available register. So, the number of interference tests in the worst case is
217// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trick18c57a82010-11-30 23:18:47 +0000218// minimal, there is no value in caching them outside the scope of
219// selectOrSplit().
220unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000221 SmallVectorImpl<unsigned> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000222 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000223 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000224
Andrew Trick13bdbb02010-11-20 02:43:55 +0000225 // Check for an available register in this class.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000226 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
227 while (unsigned PhysReg = Order.next()) {
228 // Check for interference in PhysReg
229 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
230 case LiveRegMatrix::IK_Free:
231 // PhysReg is available, allocate it.
232 return PhysReg;
Andrew Trick18c57a82010-11-30 23:18:47 +0000233
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000234 case LiveRegMatrix::IK_VirtReg:
235 // Only virtual registers in the way, we may be able to spill them.
236 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +0000237 continue;
238
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000239 default:
240 // RegMask or RegUnit interference.
241 continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000242 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000243 }
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000244
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000245 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000246 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000247 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
248 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
249 continue;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000250
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000251 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000252 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000253 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000254 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000255 }
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000256
Andrew Trick18c57a82010-11-30 23:18:47 +0000257 // No other spill candidates were found, so spill the current VirtReg.
258 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000259 if (!VirtReg.isSpillable())
260 return ~0u;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000261 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000262 spiller().spill(LRE);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000263
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000264 // The live virtual register requesting allocation was spilled, so tell
265 // the caller not to allocate anything during this round.
266 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000267}
Andrew Trick14e8d712010-10-22 23:09:15 +0000268
Andrew Trick14e8d712010-10-22 23:09:15 +0000269bool RABasic::runOnMachineFunction(MachineFunction &mf) {
270 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
271 << "********** Function: "
David Blaikie986d76d2012-08-22 17:18:53 +0000272 << mf.getName() << '\n');
Andrew Trick14e8d712010-10-22 23:09:15 +0000273
Andrew Trick18c57a82010-11-30 23:18:47 +0000274 MF = &mf;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000275 RegAllocBase::init(getAnalysis<VirtRegMap>(),
276 getAnalysis<LiveIntervals>(),
277 getAnalysis<LiveRegMatrix>());
Arnaud A. de Grandmaisona77da052013-11-10 17:46:31 +0000278
Arnaud A. de Grandmaison095f9942013-11-11 19:04:45 +0000279 calculateSpillWeightsAndHints(*LIS, *MF,
280 getAnalysis<MachineLoopInfo>(),
281 getAnalysis<MachineBlockFrequencyInfo>());
Arnaud A. de Grandmaisona77da052013-11-10 17:46:31 +0000282
Jakob Stoklund Olesen84275962011-03-31 23:02:17 +0000283 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000284
Andrew Tricke16eecc2010-10-26 18:34:01 +0000285 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000286
287 // Diagnostic output before rewriting
Andrew Trick18c57a82010-11-30 23:18:47 +0000288 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000289
Andrew Tricke16eecc2010-10-26 18:34:01 +0000290 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000291 return true;
292}
293
Andrew Trick13bdbb02010-11-20 02:43:55 +0000294FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000295{
296 return new RABasic();
297}