blob: 3a03807ebd0eedfc1eb41af001e365b904f4ada9 [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/Function.h"
24#include "llvm/PassAnalysisSupport.h"
25#include "llvm/CodeGen/CalcSpillWeights.h"
Andrew Tricke141a492010-11-08 18:02:08 +000026#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000027#include "llvm/CodeGen/LiveRangeEdit.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000028#include "llvm/CodeGen/LiveStackAnalysis.h"
29#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/Passes.h"
34#include "llvm/CodeGen/RegAllocRegistry.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000035#include "llvm/Target/TargetMachine.h"
36#include "llvm/Target/TargetOptions.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000037#include "llvm/Target/TargetRegisterInfo.h"
Andrew Tricke141a492010-11-08 18:02:08 +000038#include "llvm/Support/Debug.h"
Andrew Tricke141a492010-11-08 18:02:08 +000039#include "llvm/Support/raw_ostream.h"
Andrew Tricke16eecc2010-10-26 18:34:01 +000040
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000041#include <cstdlib>
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000042#include <queue>
Andrew Tricke16eecc2010-10-26 18:34:01 +000043
Andrew Trick14e8d712010-10-22 23:09:15 +000044using namespace llvm;
45
46static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
47 createBasicRegisterAllocator);
48
Benjamin Kramerc62feda2010-11-25 16:42:51 +000049namespace {
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000050 struct CompSpillWeight {
51 bool operator()(LiveInterval *A, LiveInterval *B) const {
52 return A->weight < B->weight;
53 }
54 };
55}
56
57namespace {
Andrew Trick14e8d712010-10-22 23:09:15 +000058/// RABasic provides a minimal implementation of the basic register allocation
59/// algorithm. It prioritizes live virtual registers by spill weight and spills
60/// whenever a register is unavailable. This is not practical in production but
61/// provides a useful baseline both for measuring other allocators and comparing
62/// the speed of the basic algorithm against other styles of allocators.
63class RABasic : public MachineFunctionPass, public RegAllocBase
64{
65 // context
Andrew Trick18c57a82010-11-30 23:18:47 +000066 MachineFunction *MF;
Andrew Trick14e8d712010-10-22 23:09:15 +000067
Andrew Trick14e8d712010-10-22 23:09:15 +000068 // state
Andrew Trick18c57a82010-11-30 23:18:47 +000069 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000070 std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
71 CompSpillWeight> Queue;
Jakob Stoklund Olesena94e6352012-02-08 18:54:35 +000072
73 // Scratch space. Allocated here to avoid repeated malloc calls in
74 // selectOrSplit().
75 BitVector UsableRegs;
76
Andrew Trick14e8d712010-10-22 23:09:15 +000077public:
78 RABasic();
79
80 /// Return the pass name.
81 virtual const char* getPassName() const {
82 return "Basic Register Allocator";
83 }
84
85 /// RABasic analysis usage.
Andrew Trick18c57a82010-11-30 23:18:47 +000086 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000087
88 virtual void releaseMemory();
89
Andrew Trick18c57a82010-11-30 23:18:47 +000090 virtual Spiller &spiller() { return *SpillerInstance; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +000091
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000092 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
93
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000094 virtual void enqueue(LiveInterval *LI) {
95 Queue.push(LI);
96 }
97
98 virtual LiveInterval *dequeue() {
99 if (Queue.empty())
100 return 0;
101 LiveInterval *LI = Queue.top();
102 Queue.pop();
103 return LI;
104 }
105
Andrew Trick18c57a82010-11-30 23:18:47 +0000106 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
107 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000108
109 /// Perform register allocation.
110 virtual bool runOnMachineFunction(MachineFunction &mf);
111
Jakob Stoklund Olesena8bd9a62012-01-11 22:52:14 +0000112 // Helper for spilling all live virtual registers currently unified under preg
113 // that interfere with the most recently queried lvr. Return true if spilling
114 // was successful, and append any new spilled/split intervals to splitLVRs.
115 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
116 SmallVectorImpl<LiveInterval*> &SplitVRegs);
117
Andrew Trick14e8d712010-10-22 23:09:15 +0000118 static char ID;
119};
120
121char RABasic::ID = 0;
122
123} // end anonymous namespace
124
Andrew Trick14e8d712010-10-22 23:09:15 +0000125RABasic::RABasic(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000126 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000127 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
128 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000129 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000130 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000131 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
132 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen964bc252010-11-03 20:39:26 +0000133 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000134 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
135 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +0000136 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Andrew Trick14e8d712010-10-22 23:09:15 +0000137}
138
Andrew Trick18c57a82010-11-30 23:18:47 +0000139void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
140 AU.setPreservesCFG();
141 AU.addRequired<AliasAnalysis>();
142 AU.addPreserved<AliasAnalysis>();
143 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000144 AU.addPreserved<LiveIntervals>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000145 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000146 AU.addRequired<LiveDebugVariables>();
147 AU.addPreserved<LiveDebugVariables>();
Andrew Trick18c57a82010-11-30 23:18:47 +0000148 AU.addRequired<CalculateSpillWeights>();
149 AU.addRequired<LiveStacks>();
150 AU.addPreserved<LiveStacks>();
151 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,
171 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
172 // 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,
225 SmallVectorImpl<LiveInterval*> &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: "
276 << ((Value*)mf.getFunction())->getName() << '\n');
277
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}