blob: 0c958df3c5aeaead0d86a781ea703de70763fe35 [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
Andy Gibbs200241e2013-04-12 10:56:28 +000067 OwningPtr<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.
79 virtual const char* getPassName() const {
80 return "Basic Register Allocator";
81 }
82
83 /// RABasic analysis usage.
Andrew Trick18c57a82010-11-30 23:18:47 +000084 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000085
86 virtual void releaseMemory();
87
Andrew Trick18c57a82010-11-30 23:18:47 +000088 virtual Spiller &spiller() { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000089
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000090 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
91
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000092 virtual void enqueue(LiveInterval *LI) {
93 Queue.push(LI);
94 }
95
96 virtual LiveInterval *dequeue() {
97 if (Queue.empty())
98 return 0;
99 LiveInterval *LI = Queue.top();
100 Queue.pop();
101 return LI;
102 }
103
Andrew Trick18c57a82010-11-30 23:18:47 +0000104 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000105 SmallVectorImpl<unsigned> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000106
107 /// Perform register allocation.
108 virtual bool runOnMachineFunction(MachineFunction &mf);
109
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000110 // Helper for spilling all live virtual registers currently unified under preg
111 // that interfere with the most recently queried lvr. Return true if spilling
112 // was successful, and append any new spilled/split intervals to splitLVRs.
113 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000114 SmallVectorImpl<unsigned> &SplitVRegs);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000115
Andrew Trick14e8d712010-10-22 23:09:15 +0000116 static char ID;
117};
118
119char RABasic::ID = 0;
120
121} // end anonymous namespace
122
Andrew Trick14e8d712010-10-22 23:09:15 +0000123RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000124 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000125 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
126 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000127 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000128 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000129 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
130 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000131 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000132 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
133 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000134 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000135}
136
Andrew Trick18c57a82010-11-30 23:18:47 +0000137void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
138 AU.setPreservesCFG();
139 AU.addRequired<AliasAnalysis>();
140 AU.addPreserved<AliasAnalysis>();
141 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000142 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000143 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000144 AU.addRequired<LiveDebugVariables>();
145 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000146 AU.addRequired<CalculateSpillWeights>();
147 AU.addRequired<LiveStacks>();
148 AU.addPreserved<LiveStacks>();
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000149 AU.addRequired<MachineBlockFrequencyInfo>();
150 AU.addPreserved<MachineBlockFrequencyInfo>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000151 AU.addRequiredID(MachineDominatorsID);
152 AU.addPreservedID(MachineDominatorsID);
153 AU.addRequired<MachineLoopInfo>();
154 AU.addPreserved<MachineLoopInfo>();
155 AU.addRequired<VirtRegMap>();
156 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000157 AU.addRequired<LiveRegMatrix>();
158 AU.addPreserved<LiveRegMatrix>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000159 MachineFunctionPass::getAnalysisUsage(AU);
Andrew Trick14e8d712010-10-22 23:09:15 +0000160}
161
162void RABasic::releaseMemory() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000163 SpillerInstance.reset(0);
Andrew Trick14e8d712010-10-22 23:09:15 +0000164}
165
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000166
167// Spill or split all live virtual registers currently unified under PhysReg
168// that interfere with VirtReg. The newly spilled or split live intervals are
169// returned by appending them to SplitVRegs.
170bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000171 SmallVectorImpl<unsigned> &SplitVRegs) {
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000172 // Record each interference and determine if all are spillable before mutating
173 // either the union or live intervals.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000174 SmallVector<LiveInterval*, 8> Intfs;
175
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000176 // Collect interferences assigned to any alias of the physical register.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000177 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
178 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
179 Q.collectInterferingVRegs();
180 if (Q.seenUnspillableVReg())
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000181 return false;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000182 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
183 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
184 if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
185 return false;
186 Intfs.push_back(Intf);
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000187 }
188 }
189 DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
190 " interferences with " << VirtReg << "\n");
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000191 assert(!Intfs.empty() && "expected interference");
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000192
193 // Spill each interfering vreg allocated to PhysReg or an alias.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000194 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
195 LiveInterval &Spill = *Intfs[i];
196
197 // Skip duplicates.
198 if (!VRM->hasPhys(Spill.reg))
199 continue;
200
201 // Deallocate the interfering vreg by removing it from the union.
202 // A LiveInterval instance may not be in a union during modification!
203 Matrix->unassign(Spill);
204
205 // Spill the extracted interval.
206 LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM);
207 spiller().spill(LRE);
208 }
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000209 return true;
210}
211
Andrew Trick14e8d712010-10-22 23:09:15 +0000212// Driver for the register assignment and splitting heuristics.
213// Manages iteration over the LiveIntervalUnions.
Andrew Trick13bdbb02010-11-20 02:43:55 +0000214//
Andrew Trick18c57a82010-11-30 23:18:47 +0000215// This is a minimal implementation of register assignment and splitting that
216// spills whenever we run out of registers.
Andrew Trick14e8d712010-10-22 23:09:15 +0000217//
218// selectOrSplit can only be called once per live virtual register. We then do a
219// single interference test for each register the correct class until we find an
220// available register. So, the number of interference tests in the worst case is
221// |vregs| * |machineregs|. And since the number of interference tests is
Andrew Trick18c57a82010-11-30 23:18:47 +0000222// minimal, there is no value in caching them outside the scope of
223// selectOrSplit().
224unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000225 SmallVectorImpl<unsigned> &SplitVRegs) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000226 // Populate a list of physical register spill candidates.
Andrew Trick18c57a82010-11-30 23:18:47 +0000227 SmallVector<unsigned, 8> PhysRegSpillCands;
Andrew Tricke141a492010-11-08 18:02:08 +0000228
Andrew Trick13bdbb02010-11-20 02:43:55 +0000229 // Check for an available register in this class.
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000230 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
231 while (unsigned PhysReg = Order.next()) {
232 // Check for interference in PhysReg
233 switch (Matrix->checkInterference(VirtReg, PhysReg)) {
234 case LiveRegMatrix::IK_Free:
235 // PhysReg is available, allocate it.
236 return PhysReg;
Andrew Trick18c57a82010-11-30 23:18:47 +0000237
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000238 case LiveRegMatrix::IK_VirtReg:
239 // Only virtual registers in the way, we may be able to spill them.
240 PhysRegSpillCands.push_back(PhysReg);
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +0000241 continue;
242
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000243 default:
244 // RegMask or RegUnit interference.
245 continue;
Andrew Tricke141a492010-11-08 18:02:08 +0000246 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000247 }
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000248
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000249 // Try to spill another interfering reg with less spill weight.
Andrew Trick18c57a82010-11-30 23:18:47 +0000250 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000251 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
252 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
253 continue;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000254
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000255 assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
Jakob Stoklund Olesen2b38c512010-12-07 18:51:27 +0000256 "Interference after spill.");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000257 // Tell the caller to allocate to this newly freed physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000258 return *PhysRegI;
Andrew Tricke141a492010-11-08 18:02:08 +0000259 }
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000260
Andrew Trick18c57a82010-11-30 23:18:47 +0000261 // No other spill candidates were found, so spill the current VirtReg.
262 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +0000263 if (!VirtReg.isSpillable())
264 return ~0u;
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +0000265 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000266 spiller().spill(LRE);
Andrew Trick13bdbb02010-11-20 02:43:55 +0000267
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000268 // The live virtual register requesting allocation was spilled, so tell
269 // the caller not to allocate anything during this round.
270 return 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000271}
Andrew Trick14e8d712010-10-22 23:09:15 +0000272
Andrew Trick14e8d712010-10-22 23:09:15 +0000273bool RABasic::runOnMachineFunction(MachineFunction &mf) {
274 DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
275 << "********** Function: "
David Blaikie986d76d2012-08-22 17:18:53 +0000276 << mf.getName() << '\n');
Andrew Trick14e8d712010-10-22 23:09:15 +0000277
Andrew Trick18c57a82010-11-30 23:18:47 +0000278 MF = &mf;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000279 RegAllocBase::init(getAnalysis<VirtRegMap>(),
280 getAnalysis<LiveIntervals>(),
281 getAnalysis<LiveRegMatrix>());
Jakob Stoklund Olesen84275962011-03-31 23:02:17 +0000282 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Andrew Trick13bdbb02010-11-20 02:43:55 +0000283
Andrew Tricke16eecc2010-10-26 18:34:01 +0000284 allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000285
286 // Diagnostic output before rewriting
Andrew Trick18c57a82010-11-30 23:18:47 +0000287 DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
Andrew Trick14e8d712010-10-22 23:09:15 +0000288
Andrew Tricke16eecc2010-10-26 18:34:01 +0000289 releaseMemory();
Andrew Trick14e8d712010-10-22 23:09:15 +0000290 return true;
291}
292
Andrew Trick13bdbb02010-11-20 02:43:55 +0000293FunctionPass* llvm::createBasicRegisterAllocator()
Andrew Trick14e8d712010-10-22 23:09:15 +0000294{
295 return new RABasic();
296}