blob: 8a49609552ad159411e0c2b9812e8c0039f05c6d [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 "Spiller.h"
Andrew Tricke141a492010-11-08 18:02:08 +000020#include "VirtRegMap.h"
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000021#include "LiveRegMatrix.h"
Andrew Trick8a83d542010-11-11 17:46:29 +000022#include "llvm/Analysis/AliasAnalysis.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000023#include "llvm/PassAnalysisSupport.h"
24#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Tricke141a492010-11-08 18:02:08 +000025#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000026#include "llvm/CodeGen/LiveRangeEdit.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000027#include "llvm/CodeGen/LiveStackAnalysis.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineLoopInfo.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/Passes.h"
33#include "llvm/CodeGen/RegAllocRegistry.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000034#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetOptions.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000036#include "llvm/Target/TargetRegisterInfo.h"
Andrew Tricke141a492010-11-08 18:02:08 +000037#include "llvm/Support/Debug.h"
Andrew Tricke141a492010-11-08 18:02:08 +000038#include "llvm/Support/raw_ostream.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000039
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000040#include <cstdlib>
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000041#include <queue>
Andrew Tricke16eecc2010-10-26 18:34:01 +000042
Andrew Trick14e8d712010-10-22 23:09:15 +000043using namespace llvm;
44
45static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
46 createBasicRegisterAllocator);
47
Benjamin Kramerc62feda2010-11-25 16:42:51 +000048namespace {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000049 struct CompSpillWeight {
50 bool operator()(LiveInterval *A, LiveInterval *B) const {
51 return A->weight < B->weight;
52 }
53 };
54}
55
56namespace {
Andrew Trick14e8d712010-10-22 23:09:15 +000057/// RABasic provides a minimal implementation of the basic register allocation
58/// algorithm. It prioritizes live virtual registers by spill weight and spills
59/// whenever a register is unavailable. This is not practical in production but
60/// provides a useful baseline both for measuring other allocators and comparing
61/// the speed of the basic algorithm against other styles of allocators.
62class RABasic : public MachineFunctionPass, public RegAllocBase
63{
64 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000065 MachineFunction *MF;
Andrew Trick14e8d712010-10-22 23:09:15 +000066
Andrew Trick14e8d712010-10-22 23:09:15 +000067 // state
Andrew Trick18c57a82010-11-30 23:18:47 +000068 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000069 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
70 CompSpillWeight> Queue;
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +000071
72 // Scratch space. Allocated here to avoid repeated malloc calls in
73 // selectOrSplit().
74 BitVector UsableRegs;
75
Andrew Trick14e8d712010-10-22 23:09:15 +000076public:
77 RABasic();
78
79 /// Return the pass name.
80 virtual const char* getPassName() const {
81 return "Basic Register Allocator";
82 }
83
84 /// RABasic analysis usage.
Andrew Trick18c57a82010-11-30 23:18:47 +000085 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000086
87 virtual void releaseMemory();
88
Andrew Trick18c57a82010-11-30 23:18:47 +000089 virtual Spiller &spiller() { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000090
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000091 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
92
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000093 virtual void enqueue(LiveInterval *LI) {
94 Queue.push(LI);
95 }
96
97 virtual LiveInterval *dequeue() {
98 if (Queue.empty())
99 return 0;
100 LiveInterval *LI = Queue.top();
101 Queue.pop();
102 return LI;
103 }
104
Andrew Trick18c57a82010-11-30 23:18:47 +0000105 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
106 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000107
108 /// Perform register allocation.
109 virtual bool runOnMachineFunction(MachineFunction &mf);
110
Jakob Stoklund Olesena8bd9a62012-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,
115 SmallVectorImpl<LiveInterval*> &SplitVRegs);
116
Andrew Trick14e8d712010-10-22 23:09:15 +0000117 static char ID;
118};
119
120char RABasic::ID = 0;
121
122} // end anonymous namespace
123
Andrew Trick14e8d712010-10-22 23:09:15 +0000124RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000125 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000126 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
127 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000128 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000129 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000130 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
131 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000132 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000133 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
134 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000135 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000136}
137
Andrew Trick18c57a82010-11-30 23:18:47 +0000138void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
139 AU.setPreservesCFG();
140 AU.addRequired<AliasAnalysis>();
141 AU.addPreserved<AliasAnalysis>();
142 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000143 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000144 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000145 AU.addRequired<LiveDebugVariables>();
146 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000147 AU.addRequired<CalculateSpillWeights>();
148 AU.addRequired<LiveStacks>();
149 AU.addPreserved<LiveStacks>();
150 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 Olesen812cda92012-06-20 22:52:24 +0000156 AU.addRequired<LiveRegMatrix>();
157 AU.addPreserved<LiveRegMatrix>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000158 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000159}
160
161void RABasic::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000162 SpillerInstance.reset(0);
Andrew Trick14e8d712010-10-22 23:09:15 +0000163}
164
Jakob Stoklund Olesena8bd9a62012-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,
170 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
171 // Record each interference and determine if all are spillable before mutating
172 // either the union or live intervals.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000173 SmallVector<LiveInterval*, 8> Intfs;
174
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000175 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen812cda92012-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 Olesena8bd9a62012-01-11 22:52:14 +0000180 return false;
Jakob Stoklund Olesen812cda92012-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 Olesena8bd9a62012-01-11 22:52:14 +0000186 }
187 }
188 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
189 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000190 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000191
192 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen812cda92012-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.
205 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM);
206 spiller().spill(LRE);
207 }
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000208 return true;
209}
210
Andrew Trick14e8d712010-10-22 23:09:15 +0000211// Driver for the register assignment and splitting heuristics.
212// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000213//
Andrew Trick18c57a82010-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 Trick14e8d712010-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 Trick18c57a82010-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,
224 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000225 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000226 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000227
Andrew Trick13bdbb02010-11-20 02:43:55 +0000228 // Check for an available register in this class.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000229 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
230 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 Trick18c57a82010-11-30 23:18:47 +0000236
Jakob Stoklund Olesen812cda92012-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 Olesena94e6352012-02-08 18:54:35 +0000240 continue;
241
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000242 default:
243 // RegMask or RegUnit interference.
244 continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000245 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000246 }
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000247
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000248 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000249 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000250 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
251 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
252 continue;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000253
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000254 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000255 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000256 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000257 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000258 }
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000259
Andrew Trick18c57a82010-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 Olesenbf4e10f2011-05-06 21:58:30 +0000262 if (!VirtReg.isSpillable())
263 return ~0u;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000264 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000265 spiller().spill(LRE);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000266
Andrew Trickf4baeaf2010-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 Tricke141a492010-11-08 18:02:08 +0000270}
Andrew Trick14e8d712010-10-22 23:09:15 +0000271
Andrew Trick14e8d712010-10-22 23:09:15 +0000272bool RABasic::runOnMachineFunction(MachineFunction &mf) {
273 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
274 << "********** Function: "
David Blaikie986d76d2012-08-22 17:18:53 +0000275 << mf.getName() << '\n');
Andrew Trick14e8d712010-10-22 23:09:15 +0000276
Andrew Trick18c57a82010-11-30 23:18:47 +0000277 MF = &mf;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000278 RegAllocBase::init(getAnalysis<VirtRegMap>(),
279 getAnalysis<LiveIntervals>(),
280 getAnalysis<LiveRegMatrix>());
Jakob Stoklund Olesen84275962011-03-31 23:02:17 +0000281 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000282
Andrew Tricke16eecc2010-10-26 18:34:01 +0000283 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000284
285 // Diagnostic output before rewriting
Andrew Trick18c57a82010-11-30 23:18:47 +0000286 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000287
Andrew Tricke16eecc2010-10-26 18:34:01 +0000288 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000289 return true;
290}
291
Andrew Trick13bdbb02010-11-20 02:43:55 +0000292FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000293{
294 return new RABasic();
295}