blob: 7fcfe9e88befa24ee55e60c960b65b3563ae9e33 [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"
27#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineLoopInfo.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000031#include "llvm/CodeGen/RegAllocRegistry.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000032#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000033#include "llvm/PassAnalysisSupport.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000036#include "llvm/Target/TargetMachine.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000037#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000038#include <cstdlib>
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000039#include <queue>
Andrew Tricke16eecc2010-10-26 18:34:01 +000040
Andrew Trick14e8d712010-10-22 23:09:15 +000041using namespace llvm;
42
43static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
44 createBasicRegisterAllocator);
45
Benjamin Kramerc62feda2010-11-25 16:42:51 +000046namespace {
Jakob Stoklund Olesen98d96482011-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 Trick14e8d712010-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.
60class RABasic : public MachineFunctionPass, public RegAllocBase
61{
62 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000063 MachineFunction *MF;
Andrew Trick14e8d712010-10-22 23:09:15 +000064
Andrew Trick14e8d712010-10-22 23:09:15 +000065 // state
Andy Gibbs200241e2013-04-12 10:56:28 +000066 OwningPtr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000067 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
68 CompSpillWeight> Queue;
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +000069
70 // Scratch space. Allocated here to avoid repeated malloc calls in
71 // selectOrSplit().
72 BitVector UsableRegs;
73
Andrew Trick14e8d712010-10-22 23:09:15 +000074public:
75 RABasic();
76
77 /// Return the pass name.
78 virtual const char* getPassName() const {
79 return "Basic Register Allocator";
80 }
81
82 /// RABasic analysis usage.
Andrew Trick18c57a82010-11-30 23:18:47 +000083 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000084
85 virtual void releaseMemory();
86
Andrew Trick18c57a82010-11-30 23:18:47 +000087 virtual Spiller &spiller() { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000088
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000089 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
90
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000091 virtual void enqueue(LiveInterval *LI) {
92 Queue.push(LI);
93 }
94
95 virtual LiveInterval *dequeue() {
96 if (Queue.empty())
97 return 0;
98 LiveInterval *LI = Queue.top();
99 Queue.pop();
100 return LI;
101 }
102
Andrew Trick18c57a82010-11-30 23:18:47 +0000103 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
104 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000105
106 /// Perform register allocation.
107 virtual bool runOnMachineFunction(MachineFunction &mf);
108
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000109 // Helper for spilling all live virtual registers currently unified under preg
110 // that interfere with the most recently queried lvr. Return true if spilling
111 // was successful, and append any new spilled/split intervals to splitLVRs.
112 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
113 SmallVectorImpl<LiveInterval*> &SplitVRegs);
114
Andrew Trick14e8d712010-10-22 23:09:15 +0000115 static char ID;
116};
117
118char RABasic::ID = 0;
119
120} // end anonymous namespace
121
Andrew Trick14e8d712010-10-22 23:09:15 +0000122RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000123 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000124 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
125 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000126 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000127 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000128 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
129 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000130 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000131 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
132 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000133 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000134}
135
Andrew Trick18c57a82010-11-30 23:18:47 +0000136void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
137 AU.setPreservesCFG();
138 AU.addRequired<AliasAnalysis>();
139 AU.addPreserved<AliasAnalysis>();
140 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000141 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000142 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000143 AU.addRequired<LiveDebugVariables>();
144 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000145 AU.addRequired<CalculateSpillWeights>();
146 AU.addRequired<LiveStacks>();
147 AU.addPreserved<LiveStacks>();
148 AU.addRequiredID(MachineDominatorsID);
149 AU.addPreservedID(MachineDominatorsID);
150 AU.addRequired<MachineLoopInfo>();
151 AU.addPreserved<MachineLoopInfo>();
152 AU.addRequired<VirtRegMap>();
153 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000154 AU.addRequired<LiveRegMatrix>();
155 AU.addPreserved<LiveRegMatrix>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000156 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000157}
158
159void RABasic::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000160 SpillerInstance.reset(0);
Andrew Trick14e8d712010-10-22 23:09:15 +0000161}
162
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000163
164// Spill or split all live virtual registers currently unified under PhysReg
165// that interfere with VirtReg. The newly spilled or split live intervals are
166// returned by appending them to SplitVRegs.
167bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
168 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
169 // Record each interference and determine if all are spillable before mutating
170 // either the union or live intervals.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000171 SmallVector<LiveInterval*, 8> Intfs;
172
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000173 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000174 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
175 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
176 Q.collectInterferingVRegs();
177 if (Q.seenUnspillableVReg())
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000178 return false;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000179 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
180 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
181 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
182 return false;
183 Intfs.push_back(Intf);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000184 }
185 }
186 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
187 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000188 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000189
190 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000191 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
192 LiveInterval &Spill = *Intfs[i];
193
194 // Skip duplicates.
195 if (!VRM->hasPhys(Spill.reg))
196 continue;
197
198 // Deallocate the interfering vreg by removing it from the union.
199 // A LiveInterval instance may not be in a union during modification!
200 Matrix->unassign(Spill);
201
202 // Spill the extracted interval.
203 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM);
204 spiller().spill(LRE);
205 }
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000206 return true;
207}
208
Andrew Trick14e8d712010-10-22 23:09:15 +0000209// Driver for the register assignment and splitting heuristics.
210// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000211//
Andrew Trick18c57a82010-11-30 23:18:47 +0000212// This is a minimal implementation of register assignment and splitting that
213// spills whenever we run out of registers.
Andrew Trick14e8d712010-10-22 23:09:15 +0000214//
215// selectOrSplit can only be called once per live virtual register. We then do a
216// single interference test for each register the correct class until we find an
217// available register. So, the number of interference tests in the worst case is
218// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trick18c57a82010-11-30 23:18:47 +0000219// minimal, there is no value in caching them outside the scope of
220// selectOrSplit().
221unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
222 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000223 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000224 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000225
Andrew Trick13bdbb02010-11-20 02:43:55 +0000226 // Check for an available register in this class.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000227 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
228 while (unsigned PhysReg = Order.next()) {
229 // Check for interference in PhysReg
230 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
231 case LiveRegMatrix::IK_Free:
232 // PhysReg is available, allocate it.
233 return PhysReg;
Andrew Trick18c57a82010-11-30 23:18:47 +0000234
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000235 case LiveRegMatrix::IK_VirtReg:
236 // Only virtual registers in the way, we may be able to spill them.
237 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +0000238 continue;
239
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000240 default:
241 // RegMask or RegUnit interference.
242 continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000243 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000244 }
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000245
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000246 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000247 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000248 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
249 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
250 continue;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000251
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000252 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000253 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000254 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000255 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000256 }
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000257
Andrew Trick18c57a82010-11-30 23:18:47 +0000258 // No other spill candidates were found, so spill the current VirtReg.
259 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000260 if (!VirtReg.isSpillable())
261 return ~0u;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000262 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000263 spiller().spill(LRE);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000264
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000265 // The live virtual register requesting allocation was spilled, so tell
266 // the caller not to allocate anything during this round.
267 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000268}
Andrew Trick14e8d712010-10-22 23:09:15 +0000269
Andrew Trick14e8d712010-10-22 23:09:15 +0000270bool RABasic::runOnMachineFunction(MachineFunction &mf) {
271 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
272 << "********** Function: "
David Blaikie986d76d2012-08-22 17:18:53 +0000273 << mf.getName() << '\n');
Andrew Trick14e8d712010-10-22 23:09:15 +0000274
Andrew Trick18c57a82010-11-30 23:18:47 +0000275 MF = &mf;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000276 RegAllocBase::init(getAnalysis<VirtRegMap>(),
277 getAnalysis<LiveIntervals>(),
278 getAnalysis<LiveRegMatrix>());
Jakob Stoklund Olesen84275962011-03-31 23:02:17 +0000279 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000280
Andrew Tricke16eecc2010-10-26 18:34:01 +0000281 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000282
283 // Diagnostic output before rewriting
Andrew Trick18c57a82010-11-30 23:18:47 +0000284 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000285
Andrew Tricke16eecc2010-10-26 18:34:01 +0000286 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000287 return true;
288}
289
Andrew Trick13bdbb02010-11-20 02:43:55 +0000290FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000291{
292 return new RABasic();
293}