blob: 96e5531840b20ba87ff9ca6bd96a8c9f83c5ea0d [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
73 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
74 SmallVectorImpl<LiveInterval*> &SplitVRegs);
75
76 /// Perform register allocation.
77 virtual bool runOnMachineFunction(MachineFunction &mf);
78
79 static char ID;
80};
81} // end anonymous namespace
82
83char RAGreedy::ID = 0;
84
85FunctionPass* llvm::createGreedyRegisterAllocator() {
86 return new RAGreedy();
87}
88
89RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
90 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
91 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
92 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
93 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
94 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
95 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
96 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
97 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
98 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
99}
100
101void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
102 AU.setPreservesCFG();
103 AU.addRequired<AliasAnalysis>();
104 AU.addPreserved<AliasAnalysis>();
105 AU.addRequired<LiveIntervals>();
106 AU.addPreserved<SlotIndexes>();
107 if (StrongPHIElim)
108 AU.addRequiredID(StrongPHIEliminationID);
109 AU.addRequiredTransitive<RegisterCoalescer>();
110 AU.addRequired<CalculateSpillWeights>();
111 AU.addRequired<LiveStacks>();
112 AU.addPreserved<LiveStacks>();
113 AU.addRequiredID(MachineDominatorsID);
114 AU.addPreservedID(MachineDominatorsID);
115 AU.addRequired<MachineLoopInfo>();
116 AU.addPreserved<MachineLoopInfo>();
117 AU.addRequired<VirtRegMap>();
118 AU.addPreserved<VirtRegMap>();
119 MachineFunctionPass::getAnalysisUsage(AU);
120}
121
122void RAGreedy::releaseMemory() {
123 SpillerInstance.reset(0);
124 RegAllocBase::releaseMemory();
125}
126
127unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
128 SmallVectorImpl<LiveInterval*> &SplitVRegs) {
129 // Populate a list of physical register spill candidates.
130 SmallVector<unsigned, 8> PhysRegSpillCands;
131
132 // Check for an available register in this class.
133 const TargetRegisterClass *TRC = MRI->getRegClass(VirtReg.reg);
134 DEBUG(dbgs() << "RegClass: " << TRC->getName() << ' ');
135
136 for (TargetRegisterClass::iterator I = TRC->allocation_order_begin(*MF),
137 E = TRC->allocation_order_end(*MF);
138 I != E; ++I) {
139
140 unsigned PhysReg = *I;
141 if (ReservedRegs.test(PhysReg)) continue;
142
143 // Check interference and as a side effect, intialize queries for this
144 // VirtReg and its aliases.
145 unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg);
146 if (interfReg == 0) {
147 // Found an available register.
148 return PhysReg;
149 }
150 LiveInterval *interferingVirtReg =
151 Queries[interfReg].firstInterference().liveUnionPos().value();
152
153 // The current VirtReg must either spillable, or one of its interferences
154 // must have less spill weight.
155 if (interferingVirtReg->weight < VirtReg.weight ) {
156 PhysRegSpillCands.push_back(PhysReg);
157 }
158 }
159 // Try to spill another interfering reg with less spill weight.
160 //
161 // FIXME: RAGreedy will sort this list by spill weight.
162 for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
163 PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
164
165 if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue;
166
167 assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 &&
168 "Interference after spill.");
169 // Tell the caller to allocate to this newly freed physical register.
170 return *PhysRegI;
171 }
172 // No other spill candidates were found, so spill the current VirtReg.
173 DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
174 SmallVector<LiveInterval*, 1> pendingSpills;
175
176 spiller().spill(&VirtReg, SplitVRegs, pendingSpills);
177
178 // The live virtual register requesting allocation was spilled, so tell
179 // the caller not to allocate anything during this round.
180 return 0;
181}
182
183bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
184 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
185 << "********** Function: "
186 << ((Value*)mf.getFunction())->getName() << '\n');
187
188 MF = &mf;
189 TM = &mf.getTarget();
190 MRI = &mf.getRegInfo();
191
192 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
193 RegAllocBase::init(*TRI, getAnalysis<VirtRegMap>(),
194 getAnalysis<LiveIntervals>());
195
196 ReservedRegs = TRI->getReservedRegs(*MF);
197 SpillerInstance.reset(createSpiller(*this, *MF, *VRM));
198 allocatePhysRegs();
199 addMBBLiveIns(MF);
200
201 // Run rewriter
202 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
203 rewriter->runOnMachineFunction(*MF, *VRM, LIS);
204
205 // The pass output is in VirtRegMap. Release all the transient data.
206 releaseMemory();
207
208 return true;
209}
210