blob: 9c893d379491e448ded70d92e04a6193764596f1 [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"
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000016#include "AllocationOrder.h"
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +000017#include "RegAllocBase.h"
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +000018#include "LiveDebugVariables.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000019#include "RenderMachineFunction.h"
20#include "Spiller.h"
Andrew Tricke141a492010-11-08 18:02:08 +000021#include "VirtRegMap.h"
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000022#include "LiveRegMatrix.h"
Andrew Trick8a83d542010-11-11 17:46:29 +000023#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000024#include "llvm/Function.h"
25#include "llvm/PassAnalysisSupport.h"
26#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Tricke141a492010-11-08 18:02:08 +000027#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000028#include "llvm/CodeGen/LiveRangeEdit.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000029#include "llvm/CodeGen/LiveStackAnalysis.h"
30#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/CodeGen/MachineLoopInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/CodeGen/Passes.h"
35#include "llvm/CodeGen/RegAllocRegistry.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000036#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000038#include "llvm/Target/TargetRegisterInfo.h"
Andrew Tricke141a492010-11-08 18:02:08 +000039#include "llvm/Support/Debug.h"
Andrew Tricke141a492010-11-08 18:02:08 +000040#include "llvm/Support/raw_ostream.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000041
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000042#include <cstdlib>
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000043#include <queue>
Andrew Tricke16eecc2010-10-26 18:34:01 +000044
Andrew Trick14e8d712010-10-22 23:09:15 +000045using namespace llvm;
46
47static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
48 createBasicRegisterAllocator);
49
Benjamin Kramerc62feda2010-11-25 16:42:51 +000050namespace {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000051 struct CompSpillWeight {
52 bool operator()(LiveInterval *A, LiveInterval *B) const {
53 return A->weight < B->weight;
54 }
55 };
56}
57
58namespace {
Andrew Trick14e8d712010-10-22 23:09:15 +000059/// RABasic provides a minimal implementation of the basic register allocation
60/// algorithm. It prioritizes live virtual registers by spill weight and spills
61/// whenever a register is unavailable. This is not practical in production but
62/// provides a useful baseline both for measuring other allocators and comparing
63/// the speed of the basic algorithm against other styles of allocators.
64class RABasic : public MachineFunctionPass, public RegAllocBase
65{
66 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000067 MachineFunction *MF;
Andrew Trick14e8d712010-10-22 23:09:15 +000068
Benjamin Kramera9783662012-06-16 21:48:13 +000069#ifndef NDEBUG
Andrew Trick14e8d712010-10-22 23:09:15 +000070 // analyses
Andrew Trick18c57a82010-11-30 23:18:47 +000071 RenderMachineFunction *RMF;
Benjamin Kramera9783662012-06-16 21:48:13 +000072#endif
Andrew Trick14e8d712010-10-22 23:09:15 +000073
74 // state
Andrew Trick18c57a82010-11-30 23:18:47 +000075 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000076 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
77 CompSpillWeight> Queue;
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +000078
79 // Scratch space. Allocated here to avoid repeated malloc calls in
80 // selectOrSplit().
81 BitVector UsableRegs;
82
Andrew Trick14e8d712010-10-22 23:09:15 +000083public:
84 RABasic();
85
86 /// Return the pass name.
87 virtual const char* getPassName() const {
88 return "Basic Register Allocator";
89 }
90
91 /// RABasic analysis usage.
Andrew Trick18c57a82010-11-30 23:18:47 +000092 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000093
94 virtual void releaseMemory();
95
Andrew Trick18c57a82010-11-30 23:18:47 +000096 virtual Spiller &spiller() { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000097
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000098 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
99
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000100 virtual void enqueue(LiveInterval *LI) {
101 Queue.push(LI);
102 }
103
104 virtual LiveInterval *dequeue() {
105 if (Queue.empty())
106 return 0;
107 LiveInterval *LI = Queue.top();
108 Queue.pop();
109 return LI;
110 }
111
Andrew Trick18c57a82010-11-30 23:18:47 +0000112 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
113 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000114
115 /// Perform register allocation.
116 virtual bool runOnMachineFunction(MachineFunction &mf);
117
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000118 // Helper for spilling all live virtual registers currently unified under preg
119 // that interfere with the most recently queried lvr. Return true if spilling
120 // was successful, and append any new spilled/split intervals to splitLVRs.
121 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
122 SmallVectorImpl<LiveInterval*> &SplitVRegs);
123
Andrew Trick14e8d712010-10-22 23:09:15 +0000124 static char ID;
125};
126
127char RABasic::ID = 0;
128
129} // end anonymous namespace
130
Andrew Trick14e8d712010-10-22 23:09:15 +0000131RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000132 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000133 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
134 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000135 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000136 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000137 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
138 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000139 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000140 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
141 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000142 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000143 initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
144}
145
Andrew Trick18c57a82010-11-30 23:18:47 +0000146void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
147 AU.setPreservesCFG();
148 AU.addRequired<AliasAnalysis>();
149 AU.addPreserved<AliasAnalysis>();
150 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000151 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000152 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000153 AU.addRequired<LiveDebugVariables>();
154 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000155 AU.addRequired<CalculateSpillWeights>();
156 AU.addRequired<LiveStacks>();
157 AU.addPreserved<LiveStacks>();
158 AU.addRequiredID(MachineDominatorsID);
159 AU.addPreservedID(MachineDominatorsID);
160 AU.addRequired<MachineLoopInfo>();
161 AU.addPreserved<MachineLoopInfo>();
162 AU.addRequired<VirtRegMap>();
163 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000164 AU.addRequired<LiveRegMatrix>();
165 AU.addPreserved<LiveRegMatrix>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000166 DEBUG(AU.addRequired<RenderMachineFunction>());
167 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000168}
169
170void RABasic::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000171 SpillerInstance.reset(0);
Andrew Trick14e8d712010-10-22 23:09:15 +0000172}
173
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000174
175// Spill or split all live virtual registers currently unified under PhysReg
176// that interfere with VirtReg. The newly spilled or split live intervals are
177// returned by appending them to SplitVRegs.
178bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
179 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
180 // Record each interference and determine if all are spillable before mutating
181 // either the union or live intervals.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000182 SmallVector<LiveInterval*, 8> Intfs;
183
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000184 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000185 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
186 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
187 Q.collectInterferingVRegs();
188 if (Q.seenUnspillableVReg())
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000189 return false;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000190 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
191 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
192 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
193 return false;
194 Intfs.push_back(Intf);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000195 }
196 }
197 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
198 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000199 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000200
201 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000202 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
203 LiveInterval &Spill = *Intfs[i];
204
205 // Skip duplicates.
206 if (!VRM->hasPhys(Spill.reg))
207 continue;
208
209 // Deallocate the interfering vreg by removing it from the union.
210 // A LiveInterval instance may not be in a union during modification!
211 Matrix->unassign(Spill);
212
213 // Spill the extracted interval.
214 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM);
215 spiller().spill(LRE);
216 }
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000217 return true;
218}
219
Andrew Trick14e8d712010-10-22 23:09:15 +0000220// Driver for the register assignment and splitting heuristics.
221// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000222//
Andrew Trick18c57a82010-11-30 23:18:47 +0000223// This is a minimal implementation of register assignment and splitting that
224// spills whenever we run out of registers.
Andrew Trick14e8d712010-10-22 23:09:15 +0000225//
226// selectOrSplit can only be called once per live virtual register. We then do a
227// single interference test for each register the correct class until we find an
228// available register. So, the number of interference tests in the worst case is
229// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trick18c57a82010-11-30 23:18:47 +0000230// minimal, there is no value in caching them outside the scope of
231// selectOrSplit().
232unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
233 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000234 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000235 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000236
Andrew Trick13bdbb02010-11-20 02:43:55 +0000237 // Check for an available register in this class.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000238 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
239 while (unsigned PhysReg = Order.next()) {
240 // Check for interference in PhysReg
241 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
242 case LiveRegMatrix::IK_Free:
243 // PhysReg is available, allocate it.
244 return PhysReg;
Andrew Trick18c57a82010-11-30 23:18:47 +0000245
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000246 case LiveRegMatrix::IK_VirtReg:
247 // Only virtual registers in the way, we may be able to spill them.
248 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +0000249 continue;
250
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000251 default:
252 // RegMask or RegUnit interference.
253 continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000254 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000255 }
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000256
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000257 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000258 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000259 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
260 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
261 continue;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000262
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000263 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000264 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000265 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000266 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000267 }
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000268
Andrew Trick18c57a82010-11-30 23:18:47 +0000269 // No other spill candidates were found, so spill the current VirtReg.
270 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000271 if (!VirtReg.isSpillable())
272 return ~0u;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000273 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000274 spiller().spill(LRE);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000275
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000276 // The live virtual register requesting allocation was spilled, so tell
277 // the caller not to allocate anything during this round.
278 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000279}
Andrew Trick14e8d712010-10-22 23:09:15 +0000280
Andrew Trick14e8d712010-10-22 23:09:15 +0000281bool RABasic::runOnMachineFunction(MachineFunction &mf) {
282 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
283 << "********** Function: "
284 << ((Value*)mf.getFunction())->getName() << '\n');
285
Andrew Trick18c57a82010-11-30 23:18:47 +0000286 MF = &mf;
Andrew Trick18c57a82010-11-30 23:18:47 +0000287 DEBUG(RMF = &getAnalysis<RenderMachineFunction>());
Andrew Trick8a83d542010-11-11 17:46:29 +0000288
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000289 RegAllocBase::init(getAnalysis<VirtRegMap>(),
290 getAnalysis<LiveIntervals>(),
291 getAnalysis<LiveRegMatrix>());
Jakob Stoklund Olesen84275962011-03-31 23:02:17 +0000292 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000293
Andrew Tricke16eecc2010-10-26 18:34:01 +0000294 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000295
296 // Diagnostic output before rewriting
Andrew Trick18c57a82010-11-30 23:18:47 +0000297 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000298
299 // optional HTML output
Andrew Trick18c57a82010-11-30 23:18:47 +0000300 DEBUG(RMF->renderMachineFunction("After basic register allocation.", VRM));
Andrew Trick14e8d712010-10-22 23:09:15 +0000301
Andrew Tricke16eecc2010-10-26 18:34:01 +0000302 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000303 return true;
304}
305
Andrew Trick13bdbb02010-11-20 02:43:55 +0000306FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000307{
308 return new RABasic();
309}