blob: 5f2be811aa6554207ed089ba2b8775000744df41 [file] [log] [blame]
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001//===-- RegAllocGreedy.cpp - greedy register allocator --------------------===//
2//
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 RAGreedy function pass for register allocation in
11// optimized builds.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "LiveIntervalUnion.h"
17#include "RegAllocBase.h"
18#include "Spiller.h"
19#include "VirtRegMap.h"
20#include "VirtRegRewriter.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000021#include "llvm/Analysis/AliasAnalysis.h"
22#include "llvm/Function.h"
23#include "llvm/PassAnalysisSupport.h"
24#include "llvm/CodeGen/CalcSpillWeights.h"
25#include "llvm/CodeGen/LiveIntervalAnalysis.h"
26#include "llvm/CodeGen/LiveStackAnalysis.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000028#include "llvm/CodeGen/MachineLoopInfo.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/CodeGen/Passes.h"
31#include "llvm/CodeGen/RegAllocRegistry.h"
32#include "llvm/CodeGen/RegisterCoalescer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000033#include "llvm/Target/TargetOptions.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
37
38using namespace llvm;
39
40static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
41 createGreedyRegisterAllocator);
42
43namespace {
44class RAGreedy : public MachineFunctionPass, public RegAllocBase {
45 // context
46 MachineFunction *MF;
47 const TargetMachine *TM;
48 MachineRegisterInfo *MRI;
49
50 BitVector ReservedRegs;
51
52 // analyses
53 LiveStacks *LS;
54
55 // state
56 std::auto_ptr<Spiller> SpillerInstance;
57
58public:
59 RAGreedy();
60
61 /// Return the pass name.
62 virtual const char* getPassName() const {
63 return "Basic Register Allocator";
64 }
65
66 /// RAGreedy analysis usage.
67 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
68
69 virtual void releaseMemory();
70
71 virtual Spiller &spiller() { return *SpillerInstance; }
72
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000073 virtual float getPriority(LiveInterval *LI) { return LI->weight; }
74
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000075 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
76 SmallVectorImpl<LiveInterval*> &SplitVRegs);
77
78 /// Perform register allocation.
79 virtual bool runOnMachineFunction(MachineFunction &mf);
80
81 static char ID;
82};
83} // end anonymous namespace
84
85char RAGreedy::ID = 0;
86
87FunctionPass* llvm::createGreedyRegisterAllocator() {
88 return new RAGreedy();
89}
90
91RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
92 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
93 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
94 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
95 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
96 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
97 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
98 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
99 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
100 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
101}
102
103void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
104 AU.setPreservesCFG();
105 AU.addRequired<AliasAnalysis>();
106 AU.addPreserved<AliasAnalysis>();
107 AU.addRequired<LiveIntervals>();
108 AU.addPreserved<SlotIndexes>();
109 if (StrongPHIElim)
110 AU.addRequiredID(StrongPHIEliminationID);
111 AU.addRequiredTransitive<RegisterCoalescer>();
112 AU.addRequired<CalculateSpillWeights>();
113 AU.addRequired<LiveStacks>();
114 AU.addPreserved<LiveStacks>();
115 AU.addRequiredID(MachineDominatorsID);
116 AU.addPreservedID(MachineDominatorsID);
117 AU.addRequired<MachineLoopInfo>();
118 AU.addPreserved<MachineLoopInfo>();
119 AU.addRequired<VirtRegMap>();
120 AU.addPreserved<VirtRegMap>();
121 MachineFunctionPass::getAnalysisUsage(AU);
122}
123
124void RAGreedy::releaseMemory() {
125 SpillerInstance.reset(0);
126 RegAllocBase::releaseMemory();
127}
128
129unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
130 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
131 // Populate a list of physical register spill candidates.
132 SmallVector<unsigned, 8> PhysRegSpillCands;
133
134 // Check for an available register in this class.
135 const TargetRegisterClass *TRC = MRI->getRegClass(VirtReg.reg);
136 DEBUG(dbgs() << "RegClass: " << TRC->getName() << ' ');
137
138 for (TargetRegisterClass::iterator I = TRC->allocation_order_begin(*MF),
139 E = TRC->allocation_order_end(*MF);
140 I != E; ++I) {
141
142 unsigned PhysReg = *I;
143 if (ReservedRegs.test(PhysReg)) continue;
144
145 // Check interference and as a side effect, intialize queries for this
146 // VirtReg and its aliases.
147 unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg);
148 if (interfReg == 0) {
149 // Found an available register.
150 return PhysReg;
151 }
152 LiveInterval *interferingVirtReg =
153 Queries[interfReg].firstInterference().liveUnionPos().value();
154
155 // The current VirtReg must either spillable, or one of its interferences
156 // must have less spill weight.
157 if (interferingVirtReg->weight < VirtReg.weight ) {
158 PhysRegSpillCands.push_back(PhysReg);
159 }
160 }
161 // Try to spill another interfering reg with less spill weight.
162 //
163 // FIXME: RAGreedy will sort this list by spill weight.
164 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
165 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
166
167 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue;
168
169 assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 &&
170 "Interference after spill.");
171 // Tell the caller to allocate to this newly freed physical register.
172 return *PhysRegI;
173 }
174 // No other spill candidates were found, so spill the current VirtReg.
175 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
176 SmallVector<LiveInterval*, 1> pendingSpills;
177
178 spiller().spill(&VirtReg, SplitVRegs, pendingSpills);
179
180 // The live virtual register requesting allocation was spilled, so tell
181 // the caller not to allocate anything during this round.
182 return 0;
183}
184
185bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
186 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
187 << "********** Function: "
188 << ((Value*)mf.getFunction())->getName() << '\n');
189
190 MF = &mf;
191 TM = &mf.getTarget();
192 MRI = &mf.getRegInfo();
193
194 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
195 RegAllocBase::init(*TRI, getAnalysis<VirtRegMap>(),
196 getAnalysis<LiveIntervals>());
197
198 ReservedRegs = TRI->getReservedRegs(*MF);
199 SpillerInstance.reset(createSpiller(*this, *MF, *VRM));
200 allocatePhysRegs();
201 addMBBLiveIns(MF);
202
203 // Run rewriter
204 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
205 rewriter->runOnMachineFunction(*MF, *VRM, LIS);
206
207 // The pass output is in VirtRegMap. Release all the transient data.
208 releaseMemory();
209
210 return true;
211}
212