blob: 7c35ceb1f561f6a0f270c2c9719b7bf5e6039afa [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"
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +000016#include "AllocationOrder.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000017#include "LiveIntervalUnion.h"
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000018#include "LiveRangeEdit.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000019#include "RegAllocBase.h"
20#include "Spiller.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000021#include "SpillPlacement.h"
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000022#include "SplitKit.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000023#include "VirtRegMap.h"
24#include "VirtRegRewriter.h"
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000025#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000026#include "llvm/Analysis/AliasAnalysis.h"
27#include "llvm/Function.h"
28#include "llvm/PassAnalysisSupport.h"
29#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000030#include "llvm/CodeGen/EdgeBundles.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
32#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000033#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000035#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000036#include "llvm/CodeGen/MachineLoopRanges.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
38#include "llvm/CodeGen/Passes.h"
39#include "llvm/CodeGen/RegAllocRegistry.h"
40#include "llvm/CodeGen/RegisterCoalescer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000041#include "llvm/Target/TargetOptions.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000042#include "llvm/Support/Debug.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000045#include "llvm/Support/Timer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000046
47using namespace llvm;
48
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000049STATISTIC(NumGlobalSplits, "Number of split global live ranges");
50STATISTIC(NumLocalSplits, "Number of split local live ranges");
51STATISTIC(NumReassigned, "Number of interferences reassigned");
52STATISTIC(NumEvicted, "Number of interferences evicted");
53
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000054static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
55 createGreedyRegisterAllocator);
56
57namespace {
58class RAGreedy : public MachineFunctionPass, public RegAllocBase {
59 // context
60 MachineFunction *MF;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000061 BitVector ReservedRegs;
62
63 // analyses
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000064 SlotIndexes *Indexes;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000065 LiveStacks *LS;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000066 MachineDominatorTree *DomTree;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000067 MachineLoopInfo *Loops;
68 MachineLoopRanges *LoopRanges;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000069 EdgeBundles *Bundles;
70 SpillPlacement *SpillPlacer;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000071
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000072 // state
73 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000074 std::auto_ptr<SplitAnalysis> SA;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000075
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000076 // splitting state.
77
78 /// All basic blocks where the current register is live.
79 SmallVector<SpillPlacement::BlockConstraint, 8> SpillConstraints;
80
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +000081 /// For every instruction in SA->UseSlots, store the previous non-copy
82 /// instruction.
83 SmallVector<SlotIndex, 8> PrevSlot;
84
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000085public:
86 RAGreedy();
87
88 /// Return the pass name.
89 virtual const char* getPassName() const {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000090 return "Greedy Register Allocator";
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000091 }
92
93 /// RAGreedy analysis usage.
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
95
96 virtual void releaseMemory();
97
98 virtual Spiller &spiller() { return *SpillerInstance; }
99
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000100 virtual float getPriority(LiveInterval *LI);
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000101
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000102 virtual unsigned selectOrSplit(LiveInterval&,
103 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000104
105 /// Perform register allocation.
106 virtual bool runOnMachineFunction(MachineFunction &mf);
107
108 static char ID;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000109
110private:
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000111 bool checkUncachedInterference(LiveInterval&, unsigned);
112 LiveInterval *getSingleInterference(LiveInterval&, unsigned);
Andrew Trickb853e6c2010-12-09 18:15:21 +0000113 bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000114 float calcInterferenceWeight(LiveInterval&, unsigned);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000115 float calcInterferenceInfo(LiveInterval&, unsigned);
116 float calcGlobalSplitCost(const BitVector&);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000117 void splitAroundRegion(LiveInterval&, unsigned, const BitVector&,
118 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000119 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
120 SlotIndex getPrevMappedIndex(const MachineInstr*);
121 void calcPrevSlots();
122 unsigned nextSplitPoint(unsigned);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000123
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000124 unsigned tryReassignOrEvict(LiveInterval&, AllocationOrder&,
125 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000126 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
127 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000128 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
129 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000130 unsigned trySplit(LiveInterval&, AllocationOrder&,
131 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000132 unsigned trySpillInterferences(LiveInterval&, AllocationOrder&,
133 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000134};
135} // end anonymous namespace
136
137char RAGreedy::ID = 0;
138
139FunctionPass* llvm::createGreedyRegisterAllocator() {
140 return new RAGreedy();
141}
142
143RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000144 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000145 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
146 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
147 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
148 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
149 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
150 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
151 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
152 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000153 initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000154 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000155 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
156 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000157}
158
159void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
160 AU.setPreservesCFG();
161 AU.addRequired<AliasAnalysis>();
162 AU.addPreserved<AliasAnalysis>();
163 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000164 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000165 AU.addPreserved<SlotIndexes>();
166 if (StrongPHIElim)
167 AU.addRequiredID(StrongPHIEliminationID);
168 AU.addRequiredTransitive<RegisterCoalescer>();
169 AU.addRequired<CalculateSpillWeights>();
170 AU.addRequired<LiveStacks>();
171 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +0000172 AU.addRequired<MachineDominatorTree>();
173 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000174 AU.addRequired<MachineLoopInfo>();
175 AU.addPreserved<MachineLoopInfo>();
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000176 AU.addRequired<MachineLoopRanges>();
177 AU.addPreserved<MachineLoopRanges>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000178 AU.addRequired<VirtRegMap>();
179 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000180 AU.addRequired<EdgeBundles>();
181 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000182 MachineFunctionPass::getAnalysisUsage(AU);
183}
184
185void RAGreedy::releaseMemory() {
186 SpillerInstance.reset(0);
187 RegAllocBase::releaseMemory();
188}
189
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000190float RAGreedy::getPriority(LiveInterval *LI) {
191 float Priority = LI->weight;
192
193 // Prioritize hinted registers so they are allocated first.
194 std::pair<unsigned, unsigned> Hint;
195 if (Hint.first || Hint.second) {
196 // The hint can be target specific, a virtual register, or a physreg.
197 Priority *= 2;
198
199 // Prefer physreg hints above anything else.
200 if (Hint.first == 0 && TargetRegisterInfo::isPhysicalRegister(Hint.second))
201 Priority *= 2;
202 }
203 return Priority;
204}
205
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000206
207//===----------------------------------------------------------------------===//
208// Register Reassignment
209//===----------------------------------------------------------------------===//
210
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000211// Check interference without using the cache.
212bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg,
213 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000214 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
215 LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]);
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000216 if (subQ.checkInterference())
217 return true;
218 }
219 return false;
220}
221
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000222/// getSingleInterference - Return the single interfering virtual register
223/// assigned to PhysReg. Return 0 if more than one virtual register is
224/// interfering.
225LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg,
226 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000227 // Check physreg and aliases.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000228 LiveInterval *Interference = 0;
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000229 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000230 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
231 if (Q.checkInterference()) {
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000232 if (Interference)
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000233 return 0;
234 Q.collectInterferingVRegs(1);
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000235 if (!Q.seenAllInterferences())
236 return 0;
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000237 Interference = Q.interferingVRegs().front();
238 }
239 }
240 return Interference;
241}
242
Andrew Trickb853e6c2010-12-09 18:15:21 +0000243// Attempt to reassign this virtual register to a different physical register.
244//
245// FIXME: we are not yet caching these "second-level" interferences discovered
246// in the sub-queries. These interferences can change with each call to
247// selectOrSplit. However, we could implement a "may-interfere" cache that
248// could be conservatively dirtied when we reassign or split.
249//
250// FIXME: This may result in a lot of alias queries. We could summarize alias
251// live intervals in their parent register's live union, but it's messy.
252bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg,
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000253 unsigned WantedPhysReg) {
254 assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) &&
255 "Can only reassign virtual registers");
256 assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) &&
Andrew Trickb853e6c2010-12-09 18:15:21 +0000257 "inconsistent phys reg assigment");
258
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +0000259 AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs);
260 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000261 // Don't reassign to a WantedPhysReg alias.
262 if (TRI->regsOverlap(PhysReg, WantedPhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000263 continue;
264
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000265 if (checkUncachedInterference(InterferingVReg, PhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000266 continue;
267
Andrew Trickb853e6c2010-12-09 18:15:21 +0000268 // Reassign the interfering virtual reg to this physical reg.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000269 unsigned OldAssign = VRM->getPhys(InterferingVReg.reg);
270 DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " <<
271 TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n');
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000272 unassign(InterferingVReg, OldAssign);
273 assign(InterferingVReg, PhysReg);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000274 ++NumReassigned;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000275 return true;
276 }
277 return false;
278}
279
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000280/// tryReassignOrEvict - Try to reassign a single interferences to a different
281/// physreg, or evict a single interference with a lower spill weight.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000282/// @param VirtReg Currently unassigned virtual register.
283/// @param Order Physregs to try.
284/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000285unsigned RAGreedy::tryReassignOrEvict(LiveInterval &VirtReg,
286 AllocationOrder &Order,
287 SmallVectorImpl<LiveInterval*> &NewVRegs){
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000288 NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000289
290 // Keep track of the lightest single interference seen so far.
291 float BestWeight = VirtReg.weight;
292 LiveInterval *BestVirt = 0;
293 unsigned BestPhys = 0;
294
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000295 Order.rewind();
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000296 while (unsigned PhysReg = Order.next()) {
297 LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg);
298 if (!InterferingVReg)
299 continue;
300 if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg))
301 continue;
302 if (reassignVReg(*InterferingVReg, PhysReg))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000303 return PhysReg;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000304
305 // Cannot reassign, is this an eviction candidate?
306 if (InterferingVReg->weight < BestWeight) {
307 BestVirt = InterferingVReg;
308 BestPhys = PhysReg;
309 BestWeight = InterferingVReg->weight;
310 }
311 }
312
313 // Nothing reassigned, can we evict a lighter single interference?
314 if (BestVirt) {
315 DEBUG(dbgs() << "evicting lighter " << *BestVirt << '\n');
316 unassign(*BestVirt, VRM->getPhys(BestVirt->reg));
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000317 ++NumEvicted;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000318 NewVRegs.push_back(BestVirt);
319 return BestPhys;
320 }
321
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000322 return 0;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000323}
324
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000325
326//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000327// Region Splitting
328//===----------------------------------------------------------------------===//
329
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000330/// calcInterferenceInfo - Compute per-block outgoing and ingoing constraints
331/// when considering interference from PhysReg. Also compute an optimistic local
332/// cost of this interference pattern.
333///
334/// The final cost of a split is the local cost + global cost of preferences
335/// broken by SpillPlacement.
336///
337float RAGreedy::calcInterferenceInfo(LiveInterval &VirtReg, unsigned PhysReg) {
338 // Reset interference dependent info.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000339 SpillConstraints.resize(SA->LiveBlocks.size());
340 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
341 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000342 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000343 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000344 BC.Entry = (BI.Uses && BI.LiveIn) ?
345 SpillPlacement::PrefReg : SpillPlacement::DontCare;
346 BC.Exit = (BI.Uses && BI.LiveOut) ?
347 SpillPlacement::PrefReg : SpillPlacement::DontCare;
348 BI.OverlapEntry = BI.OverlapExit = false;
349 }
350
351 // Add interference info from each PhysReg alias.
352 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
353 if (!query(VirtReg, *AI).checkInterference())
354 continue;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000355 LiveIntervalUnion::SegmentIter IntI =
356 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
357 if (!IntI.valid())
358 continue;
359
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000360 // Determine which blocks have interference live in or after the last split
361 // point.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000362 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
363 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000364 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
365 SlotIndex Start, Stop;
366 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
367
368 // Skip interference-free blocks.
369 if (IntI.start() >= Stop)
370 continue;
371
372 // Is the interference live-in?
373 if (BI.LiveIn) {
374 IntI.advanceTo(Start);
375 if (!IntI.valid())
376 break;
377 if (IntI.start() <= Start)
378 BC.Entry = SpillPlacement::MustSpill;
379 }
380
381 // Is the interference overlapping the last split point?
382 if (BI.LiveOut) {
383 if (IntI.stop() < BI.LastSplitPoint)
384 IntI.advanceTo(BI.LastSplitPoint.getPrevSlot());
385 if (!IntI.valid())
386 break;
387 if (IntI.start() < Stop)
388 BC.Exit = SpillPlacement::MustSpill;
389 }
390 }
391
392 // Rewind iterator and check other interferences.
393 IntI.find(VirtReg.beginIndex());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000394 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
395 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000396 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
397 SlotIndex Start, Stop;
398 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
399
400 // Skip interference-free blocks.
401 if (IntI.start() >= Stop)
402 continue;
403
404 // Handle transparent blocks with interference separately.
405 // Transparent blocks never incur any fixed cost.
406 if (BI.LiveThrough && !BI.Uses) {
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000407 IntI.advanceTo(Start);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000408 if (!IntI.valid())
409 break;
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000410 if (IntI.start() >= Stop)
411 continue;
412
413 if (BC.Entry != SpillPlacement::MustSpill)
414 BC.Entry = SpillPlacement::PrefSpill;
415 if (BC.Exit != SpillPlacement::MustSpill)
416 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000417 continue;
418 }
419
420 // Now we only have blocks with uses left.
421 // Check if the interference overlaps the uses.
422 assert(BI.Uses && "Non-transparent block without any uses");
423
424 // Check interference on entry.
425 if (BI.LiveIn && BC.Entry != SpillPlacement::MustSpill) {
426 IntI.advanceTo(Start);
427 if (!IntI.valid())
428 break;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000429 // Not live in, but before the first use.
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000430 if (IntI.start() < BI.FirstUse)
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000431 BC.Entry = SpillPlacement::PrefSpill;
432 }
433
434 // Does interference overlap the uses in the entry segment
435 // [FirstUse;Kill)?
436 if (BI.LiveIn && !BI.OverlapEntry) {
437 IntI.advanceTo(BI.FirstUse);
438 if (!IntI.valid())
439 break;
440 // A live-through interval has no kill.
441 // Check [FirstUse;LastUse) instead.
442 if (IntI.start() < (BI.LiveThrough ? BI.LastUse : BI.Kill))
443 BI.OverlapEntry = true;
444 }
445
446 // Does interference overlap the uses in the exit segment [Def;LastUse)?
447 if (BI.LiveOut && !BI.LiveThrough && !BI.OverlapExit) {
448 IntI.advanceTo(BI.Def);
449 if (!IntI.valid())
450 break;
451 if (IntI.start() < BI.LastUse)
452 BI.OverlapExit = true;
453 }
454
455 // Check interference on exit.
456 if (BI.LiveOut && BC.Exit != SpillPlacement::MustSpill) {
457 // Check interference between LastUse and Stop.
458 if (BC.Exit != SpillPlacement::PrefSpill) {
459 IntI.advanceTo(BI.LastUse);
460 if (!IntI.valid())
461 break;
462 if (IntI.start() < Stop)
463 BC.Exit = SpillPlacement::PrefSpill;
464 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000465 }
466 }
467 }
468
469 // Accumulate a local cost of this interference pattern.
470 float LocalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000471 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
472 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000473 if (!BI.Uses)
474 continue;
475 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
476 unsigned Inserts = 0;
477
478 // Do we need spill code for the entry segment?
479 if (BI.LiveIn)
480 Inserts += BI.OverlapEntry || BC.Entry != SpillPlacement::PrefReg;
481
482 // For the exit segment?
483 if (BI.LiveOut)
484 Inserts += BI.OverlapExit || BC.Exit != SpillPlacement::PrefReg;
485
486 // The local cost of spill code in this block is the block frequency times
487 // the number of spill instructions inserted.
488 if (Inserts)
489 LocalCost += Inserts * SpillPlacer->getBlockFrequency(BI.MBB);
490 }
491 DEBUG(dbgs() << "Local cost of " << PrintReg(PhysReg, TRI) << " = "
492 << LocalCost << '\n');
493 return LocalCost;
494}
495
496/// calcGlobalSplitCost - Return the global split cost of following the split
497/// pattern in LiveBundles. This cost should be added to the local cost of the
498/// interference pattern in SpillConstraints.
499///
500float RAGreedy::calcGlobalSplitCost(const BitVector &LiveBundles) {
501 float GlobalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000502 for (unsigned i = 0, e = SpillConstraints.size(); i != e; ++i) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000503 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
504 unsigned Inserts = 0;
505 // Broken entry preference?
506 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 0)] !=
507 (BC.Entry == SpillPlacement::PrefReg);
508 // Broken exit preference?
509 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 1)] !=
510 (BC.Exit == SpillPlacement::PrefReg);
511 if (Inserts)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000512 GlobalCost +=
513 Inserts * SpillPlacer->getBlockFrequency(SA->LiveBlocks[i].MBB);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000514 }
515 DEBUG(dbgs() << "Global cost = " << GlobalCost << '\n');
516 return GlobalCost;
517}
518
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000519/// splitAroundRegion - Split VirtReg around the region determined by
520/// LiveBundles. Make an effort to avoid interference from PhysReg.
521///
522/// The 'register' interval is going to contain as many uses as possible while
523/// avoiding interference. The 'stack' interval is the complement constructed by
524/// SplitEditor. It will contain the rest.
525///
526void RAGreedy::splitAroundRegion(LiveInterval &VirtReg, unsigned PhysReg,
527 const BitVector &LiveBundles,
528 SmallVectorImpl<LiveInterval*> &NewVRegs) {
529 DEBUG({
530 dbgs() << "Splitting around region for " << PrintReg(PhysReg, TRI)
531 << " with bundles";
532 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
533 dbgs() << " EB#" << i;
534 dbgs() << ".\n";
535 });
536
537 // First compute interference ranges in the live blocks.
538 typedef std::pair<SlotIndex, SlotIndex> IndexPair;
539 SmallVector<IndexPair, 8> InterferenceRanges;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000540 InterferenceRanges.resize(SA->LiveBlocks.size());
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000541 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
542 if (!query(VirtReg, *AI).checkInterference())
543 continue;
544 LiveIntervalUnion::SegmentIter IntI =
545 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
546 if (!IntI.valid())
547 continue;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000548 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
549 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000550 IndexPair &IP = InterferenceRanges[i];
551 SlotIndex Start, Stop;
552 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
553 // Skip interference-free blocks.
554 if (IntI.start() >= Stop)
555 continue;
556
557 // First interference in block.
558 if (BI.LiveIn) {
559 IntI.advanceTo(Start);
560 if (!IntI.valid())
561 break;
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000562 if (IntI.start() >= Stop)
563 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000564 if (!IP.first.isValid() || IntI.start() < IP.first)
565 IP.first = IntI.start();
566 }
567
568 // Last interference in block.
569 if (BI.LiveOut) {
570 IntI.advanceTo(Stop);
571 if (!IntI.valid() || IntI.start() >= Stop)
572 --IntI;
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000573 if (IntI.stop() <= Start)
574 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000575 if (!IP.second.isValid() || IntI.stop() > IP.second)
576 IP.second = IntI.stop();
577 }
578 }
579 }
580
581 SmallVector<LiveInterval*, 4> SpillRegs;
582 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
583 SplitEditor SE(*SA, *LIS, *VRM, *DomTree, LREdit);
584
585 // Create the main cross-block interval.
586 SE.openIntv();
587
588 // First add all defs that are live out of a block.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000589 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
590 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000591 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
592 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
593
594 // Should the register be live out?
595 if (!BI.LiveOut || !RegOut)
596 continue;
597
598 IndexPair &IP = InterferenceRanges[i];
599 SlotIndex Start, Stop;
600 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
601
602 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#"
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000603 << Bundles->getBundle(BI.MBB->getNumber(), 1)
604 << " intf [" << IP.first << ';' << IP.second << ')');
605
606 // The interference interval should either be invalid or overlap MBB.
607 assert((!IP.first.isValid() || IP.first < Stop) && "Bad interference");
608 assert((!IP.second.isValid() || IP.second > Start) && "Bad interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000609
610 // Check interference leaving the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000611 if (!IP.second.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000612 // Block is interference-free.
613 DEBUG(dbgs() << ", no interference");
614 if (!BI.Uses) {
615 assert(BI.LiveThrough && "No uses, but not live through block?");
616 // Block is live-through without interference.
617 DEBUG(dbgs() << ", no uses"
618 << (RegIn ? ", live-through.\n" : ", stack in.\n"));
619 if (!RegIn)
620 SE.enterIntvAtEnd(*BI.MBB);
621 continue;
622 }
623 if (!BI.LiveThrough) {
624 DEBUG(dbgs() << ", not live-through.\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000625 SE.useIntv(SE.enterIntvBefore(BI.Def), Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000626 continue;
627 }
628 if (!RegIn) {
629 // Block is live-through, but entry bundle is on the stack.
630 // Reload just before the first use.
631 DEBUG(dbgs() << ", not live-in, enter before first use.\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000632 SE.useIntv(SE.enterIntvBefore(BI.FirstUse), Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000633 continue;
634 }
635 DEBUG(dbgs() << ", live-through.\n");
636 continue;
637 }
638
639 // Block has interference.
640 DEBUG(dbgs() << ", interference to " << IP.second);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000641
642 if (!BI.LiveThrough && IP.second <= BI.Def) {
643 // The interference doesn't reach the outgoing segment.
644 DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n');
645 SE.useIntv(BI.Def, Stop);
646 continue;
647 }
648
649
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000650 if (!BI.Uses) {
651 // No uses in block, avoid interference by reloading as late as possible.
652 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000653 SlotIndex SegStart = SE.enterIntvAtEnd(*BI.MBB);
654 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000655 continue;
656 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000657
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000658 if (IP.second.getBoundaryIndex() < BI.LastUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000659 // There are interference-free uses at the end of the block.
660 // Find the first use that can get the live-out register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000661 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000662 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
663 IP.second.getBoundaryIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000664 assert(UI != SA->UseSlots.end() && "Couldn't find last use");
665 SlotIndex Use = *UI;
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000666 assert(Use <= BI.LastUse && "Couldn't find last use");
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000667 // Only attempt a split befroe the last split point.
668 if (Use.getBaseIndex() <= BI.LastSplitPoint) {
669 DEBUG(dbgs() << ", free use at " << Use << ".\n");
670 SlotIndex SegStart = SE.enterIntvBefore(Use);
671 assert(SegStart >= IP.second && "Couldn't avoid interference");
672 assert(SegStart < BI.LastSplitPoint && "Impossible split point");
673 SE.useIntv(SegStart, Stop);
674 continue;
675 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000676 }
677
678 // Interference is after the last use.
679 DEBUG(dbgs() << " after last use.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000680 SlotIndex SegStart = SE.enterIntvAtEnd(*BI.MBB);
681 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000682 }
683
684 // Now all defs leading to live bundles are handled, do everything else.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000685 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
686 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000687 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
688 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
689
690 // Is the register live-in?
691 if (!BI.LiveIn || !RegIn)
692 continue;
693
694 // We have an incoming register. Check for interference.
695 IndexPair &IP = InterferenceRanges[i];
696 SlotIndex Start, Stop;
697 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
698
699 DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0)
700 << " -> BB#" << BI.MBB->getNumber());
701
702 // Check interference entering the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000703 if (!IP.first.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000704 // Block is interference-free.
705 DEBUG(dbgs() << ", no interference");
706 if (!BI.Uses) {
707 assert(BI.LiveThrough && "No uses, but not live through block?");
708 // Block is live-through without interference.
709 if (RegOut) {
710 DEBUG(dbgs() << ", no uses, live-through.\n");
711 SE.useIntv(Start, Stop);
712 } else {
713 DEBUG(dbgs() << ", no uses, stack-out.\n");
714 SE.leaveIntvAtTop(*BI.MBB);
715 }
716 continue;
717 }
718 if (!BI.LiveThrough) {
719 DEBUG(dbgs() << ", killed in block.\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000720 SE.useIntv(Start, SE.leaveIntvAfter(BI.Kill));
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000721 continue;
722 }
723 if (!RegOut) {
724 // Block is live-through, but exit bundle is on the stack.
725 // Spill immediately after the last use.
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000726 if (BI.LastUse < BI.LastSplitPoint) {
727 DEBUG(dbgs() << ", uses, stack-out.\n");
728 SE.useIntv(Start, SE.leaveIntvAfter(BI.LastUse));
729 continue;
730 }
731 // The last use is after the last split point, it is probably an
732 // indirect jump.
733 DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point "
734 << BI.LastSplitPoint << ", stack-out.\n");
Jakob Stoklund Olesen23cd57c2011-02-09 23:33:02 +0000735 SlotIndex SegEnd = SE.leaveIntvBefore(BI.LastSplitPoint);
736 SE.useIntv(Start, SegEnd);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000737 // Run a double interval from the split to the last use.
738 // This makes it possible to spill the complement without affecting the
739 // indirect branch.
740 SE.overlapIntv(SegEnd, BI.LastUse);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000741 continue;
742 }
743 // Register is live-through.
744 DEBUG(dbgs() << ", uses, live-through.\n");
745 SE.useIntv(Start, Stop);
746 continue;
747 }
748
749 // Block has interference.
750 DEBUG(dbgs() << ", interference from " << IP.first);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000751
752 if (!BI.LiveThrough && IP.first >= BI.Kill) {
753 // The interference doesn't reach the outgoing segment.
754 DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n');
755 SE.useIntv(Start, BI.Kill);
756 continue;
757 }
758
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000759 if (!BI.Uses) {
760 // No uses in block, avoid interference by spilling as soon as possible.
761 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000762 SlotIndex SegEnd = SE.leaveIntvAtTop(*BI.MBB);
763 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000764 continue;
765 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000766 if (IP.first.getBaseIndex() > BI.FirstUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000767 // There are interference-free uses at the beginning of the block.
768 // Find the last use that can get the register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000769 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000770 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
771 IP.first.getBaseIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000772 assert(UI != SA->UseSlots.begin() && "Couldn't find first use");
773 SlotIndex Use = (--UI)->getBoundaryIndex();
774 DEBUG(dbgs() << ", free use at " << *UI << ".\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000775 SlotIndex SegEnd = SE.leaveIntvAfter(Use);
776 assert(SegEnd <= IP.first && "Couldn't avoid interference");
777 SE.useIntv(Start, SegEnd);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000778 continue;
779 }
780
781 // Interference is before the first use.
782 DEBUG(dbgs() << " before first use.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000783 SlotIndex SegEnd = SE.leaveIntvAtTop(*BI.MBB);
784 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000785 }
786
787 SE.closeIntv();
788
789 // FIXME: Should we be more aggressive about splitting the stack region into
790 // per-block segments? The current approach allows the stack region to
791 // separate into connected components. Some components may be allocatable.
792 SE.finish();
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000793 ++NumGlobalSplits;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000794
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000795 if (VerifyEnabled) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000796 MF->verify(this, "After splitting live range around region");
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000797
798#ifndef NDEBUG
799 // Make sure that at least one of the new intervals can allocate to PhysReg.
800 // That was the whole point of splitting the live range.
801 bool found = false;
802 for (LiveRangeEdit::iterator I = LREdit.begin(), E = LREdit.end(); I != E;
803 ++I)
804 if (!checkUncachedInterference(**I, PhysReg)) {
805 found = true;
806 break;
807 }
808 assert(found && "No allocatable intervals after pointless splitting");
809#endif
810 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000811}
812
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000813unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
814 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000815 BitVector LiveBundles, BestBundles;
816 float BestCost = 0;
817 unsigned BestReg = 0;
818 Order.rewind();
819 while (unsigned PhysReg = Order.next()) {
820 float Cost = calcInterferenceInfo(VirtReg, PhysReg);
821 if (BestReg && Cost >= BestCost)
822 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000823
824 SpillPlacer->placeSpills(SpillConstraints, LiveBundles);
825 // No live bundles, defer to splitSingleBlocks().
826 if (!LiveBundles.any())
827 continue;
828
829 Cost += calcGlobalSplitCost(LiveBundles);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000830 if (!BestReg || Cost < BestCost) {
831 BestReg = PhysReg;
832 BestCost = Cost;
833 BestBundles.swap(LiveBundles);
834 }
835 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000836
837 if (!BestReg)
838 return 0;
839
840 splitAroundRegion(VirtReg, BestReg, BestBundles, NewVRegs);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000841 return 0;
842}
843
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000844
845//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000846// Local Splitting
847//===----------------------------------------------------------------------===//
848
849
850/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
851/// in order to use PhysReg between two entries in SA->UseSlots.
852///
853/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
854///
855void RAGreedy::calcGapWeights(unsigned PhysReg,
856 SmallVectorImpl<float> &GapWeight) {
857 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
858 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
859 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
860 const unsigned NumGaps = Uses.size()-1;
861
862 // Start and end points for the interference check.
863 SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse;
864 SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse;
865
866 GapWeight.assign(NumGaps, 0.0f);
867
868 // Add interference from each overlapping register.
869 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
870 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
871 .checkInterference())
872 continue;
873
874 // We know that VirtReg is a continuous interval from FirstUse to LastUse,
875 // so we don't need InterferenceQuery.
876 //
877 // Interference that overlaps an instruction is counted in both gaps
878 // surrounding the instruction. The exception is interference before
879 // StartIdx and after StopIdx.
880 //
881 LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
882 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
883 // Skip the gaps before IntI.
884 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
885 if (++Gap == NumGaps)
886 break;
887 if (Gap == NumGaps)
888 break;
889
890 // Update the gaps covered by IntI.
891 const float weight = IntI.value()->weight;
892 for (; Gap != NumGaps; ++Gap) {
893 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
894 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
895 break;
896 }
897 if (Gap == NumGaps)
898 break;
899 }
900 }
901}
902
903/// getPrevMappedIndex - Return the slot index of the last non-copy instruction
904/// before MI that has a slot index. If MI is the first mapped instruction in
905/// its block, return the block start index instead.
906///
907SlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) {
908 assert(MI && "Missing MachineInstr");
909 const MachineBasicBlock *MBB = MI->getParent();
910 MachineBasicBlock::const_iterator B = MBB->begin(), I = MI;
911 while (I != B)
912 if (!(--I)->isDebugValue() && !I->isCopy())
913 return Indexes->getInstructionIndex(I);
914 return Indexes->getMBBStartIdx(MBB);
915}
916
917/// calcPrevSlots - Fill in the PrevSlot array with the index of the previous
918/// real non-copy instruction for each instruction in SA->UseSlots.
919///
920void RAGreedy::calcPrevSlots() {
921 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
922 PrevSlot.clear();
923 PrevSlot.reserve(Uses.size());
924 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
925 const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]);
926 PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex());
927 }
928}
929
930/// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may
931/// be beneficial to split before UseSlots[i].
932///
933/// 0 is always a valid split point
934unsigned RAGreedy::nextSplitPoint(unsigned i) {
935 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
936 const unsigned Size = Uses.size();
937 assert(i != Size && "No split points after the end");
938 // Allow split before i when Uses[i] is not adjacent to the previous use.
939 while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex())
940 ;
941 return i;
942}
943
944/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
945/// basic block.
946///
947unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
948 SmallVectorImpl<LiveInterval*> &NewVRegs) {
949 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
950 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
951
952 // Note that it is possible to have an interval that is live-in or live-out
953 // while only covering a single block - A phi-def can use undef values from
954 // predecessors, and the block could be a single-block loop.
955 // We don't bother doing anything clever about such a case, we simply assume
956 // that the interval is continuous from FirstUse to LastUse. We should make
957 // sure that we don't do anything illegal to such an interval, though.
958
959 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
960 if (Uses.size() <= 2)
961 return 0;
962 const unsigned NumGaps = Uses.size()-1;
963
964 DEBUG({
965 dbgs() << "tryLocalSplit: ";
966 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
967 dbgs() << ' ' << SA->UseSlots[i];
968 dbgs() << '\n';
969 });
970
971 // For every use, find the previous mapped non-copy instruction.
972 // We use this to detect valid split points, and to estimate new interval
973 // sizes.
974 calcPrevSlots();
975
976 unsigned BestBefore = NumGaps;
977 unsigned BestAfter = 0;
978 float BestDiff = 0;
979
980 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB);
981 SmallVector<float, 8> GapWeight;
982
983 Order.rewind();
984 while (unsigned PhysReg = Order.next()) {
985 // Keep track of the largest spill weight that would need to be evicted in
986 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
987 calcGapWeights(PhysReg, GapWeight);
988
989 // Try to find the best sequence of gaps to close.
990 // The new spill weight must be larger than any gap interference.
991
992 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
993 unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1;
994
995 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
996 // It is the spill weight that needs to be evicted.
997 float MaxGap = GapWeight[0];
998 for (unsigned i = 1; i != SplitAfter; ++i)
999 MaxGap = std::max(MaxGap, GapWeight[i]);
1000
1001 for (;;) {
1002 // Live before/after split?
1003 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1004 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1005
1006 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1007 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1008 << " i=" << MaxGap);
1009
1010 // Stop before the interval gets so big we wouldn't be making progress.
1011 if (!LiveBefore && !LiveAfter) {
1012 DEBUG(dbgs() << " all\n");
1013 break;
1014 }
1015 // Should the interval be extended or shrunk?
1016 bool Shrink = true;
1017 if (MaxGap < HUGE_VALF) {
1018 // Estimate the new spill weight.
1019 //
1020 // Each instruction reads and writes the register, except the first
1021 // instr doesn't read when !FirstLive, and the last instr doesn't write
1022 // when !LastLive.
1023 //
1024 // We will be inserting copies before and after, so the total number of
1025 // reads and writes is 2 * EstUses.
1026 //
1027 const unsigned EstUses = 2*(SplitAfter - SplitBefore) +
1028 2*(LiveBefore + LiveAfter);
1029
1030 // Try to guess the size of the new interval. This should be trivial,
1031 // but the slot index of an inserted copy can be a lot smaller than the
1032 // instruction it is inserted before if there are many dead indexes
1033 // between them.
1034 //
1035 // We measure the distance from the instruction before SplitBefore to
1036 // get a conservative estimate.
1037 //
1038 // The final distance can still be different if inserting copies
1039 // triggers a slot index renumbering.
1040 //
1041 const float EstWeight = normalizeSpillWeight(blockFreq * EstUses,
1042 PrevSlot[SplitBefore].distance(Uses[SplitAfter]));
1043 // Would this split be possible to allocate?
1044 // Never allocate all gaps, we wouldn't be making progress.
1045 float Diff = EstWeight - MaxGap;
1046 DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff);
1047 if (Diff > 0) {
1048 Shrink = false;
1049 if (Diff > BestDiff) {
1050 DEBUG(dbgs() << " (best)");
1051 BestDiff = Diff;
1052 BestBefore = SplitBefore;
1053 BestAfter = SplitAfter;
1054 }
1055 }
1056 }
1057
1058 // Try to shrink.
1059 if (Shrink) {
1060 SplitBefore = nextSplitPoint(SplitBefore);
1061 if (SplitBefore < SplitAfter) {
1062 DEBUG(dbgs() << " shrink\n");
1063 // Recompute the max when necessary.
1064 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1065 MaxGap = GapWeight[SplitBefore];
1066 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1067 MaxGap = std::max(MaxGap, GapWeight[i]);
1068 }
1069 continue;
1070 }
1071 MaxGap = 0;
1072 }
1073
1074 // Try to extend the interval.
1075 if (SplitAfter >= NumGaps) {
1076 DEBUG(dbgs() << " end\n");
1077 break;
1078 }
1079
1080 DEBUG(dbgs() << " extend\n");
1081 for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1;
1082 SplitAfter != e; ++SplitAfter)
1083 MaxGap = std::max(MaxGap, GapWeight[SplitAfter]);
1084 continue;
1085 }
1086 }
1087
1088 // Didn't find any candidates?
1089 if (BestBefore == NumGaps)
1090 return 0;
1091
1092 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1093 << '-' << Uses[BestAfter] << ", " << BestDiff
1094 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1095
1096 SmallVector<LiveInterval*, 4> SpillRegs;
1097 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
1098 SplitEditor SE(*SA, *LIS, *VRM, *DomTree, LREdit);
1099
1100 SE.openIntv();
1101 SlotIndex SegStart = SE.enterIntvBefore(Uses[BestBefore]);
1102 SlotIndex SegStop = SE.leaveIntvAfter(Uses[BestAfter]);
1103 SE.useIntv(SegStart, SegStop);
1104 SE.closeIntv();
1105 SE.finish();
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001106 ++NumLocalSplits;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001107
1108 return 0;
1109}
1110
1111//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001112// Live Range Splitting
1113//===----------------------------------------------------------------------===//
1114
1115/// trySplit - Try to split VirtReg or one of its interferences, making it
1116/// assignable.
1117/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1118unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1119 SmallVectorImpl<LiveInterval*>&NewVRegs) {
1120 NamedRegionTimer T("Splitter", TimerGroupName, TimePassesIsEnabled);
1121 SA->analyze(&VirtReg);
1122
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001123 // Local intervals are handled separately.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001124 if (LIS->intervalIsInOneMBB(VirtReg))
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001125 return tryLocalSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001126
1127 // First try to split around a region spanning multiple blocks.
1128 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1129 if (PhysReg || !NewVRegs.empty())
1130 return PhysReg;
1131
1132 // Then isolate blocks with multiple uses.
1133 SplitAnalysis::BlockPtrSet Blocks;
1134 if (SA->getMultiUseBlocks(Blocks)) {
1135 SmallVector<LiveInterval*, 4> SpillRegs;
1136 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
1137 SplitEditor(*SA, *LIS, *VRM, *DomTree, LREdit).splitSingleBlocks(Blocks);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +00001138 if (VerifyEnabled)
1139 MF->verify(this, "After splitting live range around basic blocks");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001140 }
1141
1142 // Don't assign any physregs.
1143 return 0;
1144}
1145
1146
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001147//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001148// Spilling
1149//===----------------------------------------------------------------------===//
1150
1151/// calcInterferenceWeight - Calculate the combined spill weight of
1152/// interferences when assigning VirtReg to PhysReg.
1153float RAGreedy::calcInterferenceWeight(LiveInterval &VirtReg, unsigned PhysReg){
1154 float Sum = 0;
1155 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
1156 LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
1157 Q.collectInterferingVRegs();
1158 if (Q.seenUnspillableVReg())
1159 return HUGE_VALF;
1160 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i)
1161 Sum += Q.interferingVRegs()[i]->weight;
1162 }
1163 return Sum;
1164}
1165
1166/// trySpillInterferences - Try to spill interfering registers instead of the
1167/// current one. Only do it if the accumulated spill weight is smaller than the
1168/// current spill weight.
1169unsigned RAGreedy::trySpillInterferences(LiveInterval &VirtReg,
1170 AllocationOrder &Order,
1171 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1172 NamedRegionTimer T("Spill Interference", TimerGroupName, TimePassesIsEnabled);
1173 unsigned BestPhys = 0;
Duncan Sands2aea4902010-12-28 10:07:15 +00001174 float BestWeight = 0;
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001175
1176 Order.rewind();
1177 while (unsigned PhysReg = Order.next()) {
1178 float Weight = calcInterferenceWeight(VirtReg, PhysReg);
1179 if (Weight == HUGE_VALF || Weight >= VirtReg.weight)
1180 continue;
1181 if (!BestPhys || Weight < BestWeight)
1182 BestPhys = PhysReg, BestWeight = Weight;
1183 }
1184
1185 // No candidates found.
1186 if (!BestPhys)
1187 return 0;
1188
1189 // Collect all interfering registers.
1190 SmallVector<LiveInterval*, 8> Spills;
1191 for (const unsigned *AI = TRI->getOverlaps(BestPhys); *AI; ++AI) {
1192 LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
1193 Spills.append(Q.interferingVRegs().begin(), Q.interferingVRegs().end());
1194 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
1195 LiveInterval *VReg = Q.interferingVRegs()[i];
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +00001196 unassign(*VReg, *AI);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001197 }
1198 }
1199
1200 // Spill them all.
1201 DEBUG(dbgs() << "spilling " << Spills.size() << " interferences with weight "
1202 << BestWeight << '\n');
1203 for (unsigned i = 0, e = Spills.size(); i != e; ++i)
1204 spiller().spill(Spills[i], NewVRegs, Spills);
1205 return BestPhys;
1206}
1207
1208
1209//===----------------------------------------------------------------------===//
1210// Main Entry Point
1211//===----------------------------------------------------------------------===//
1212
1213unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001214 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001215 // First try assigning a free register.
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +00001216 AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs);
1217 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001218 if (!checkPhysRegInterference(VirtReg, PhysReg))
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001219 return PhysReg;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001220 }
Andrew Trickb853e6c2010-12-09 18:15:21 +00001221
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001222 // Try to reassign interferences.
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +00001223 if (unsigned PhysReg = tryReassignOrEvict(VirtReg, Order, NewVRegs))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001224 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001225
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001226 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1227
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001228 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001229 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1230 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +00001231 return PhysReg;
1232
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001233 // Try to spill another interfering reg with less spill weight.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001234 PhysReg = trySpillInterferences(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001235 if (PhysReg)
1236 return PhysReg;
1237
1238 // Finally spill VirtReg itself.
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001239 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001240 SmallVector<LiveInterval*, 1> pendingSpills;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001241 spiller().spill(&VirtReg, NewVRegs, pendingSpills);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001242
1243 // The live virtual register requesting allocation was spilled, so tell
1244 // the caller not to allocate anything during this round.
1245 return 0;
1246}
1247
1248bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1249 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1250 << "********** Function: "
1251 << ((Value*)mf.getFunction())->getName() << '\n');
1252
1253 MF = &mf;
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001254 if (VerifyEnabled)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +00001255 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001256
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +00001257 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001258 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +00001259 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001260 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesenf6dff842010-12-10 22:54:44 +00001261 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001262 Loops = &getAnalysis<MachineLoopInfo>();
1263 LoopRanges = &getAnalysis<MachineLoopRanges>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001264 Bundles = &getAnalysis<EdgeBundles>();
1265 SpillPlacer = &getAnalysis<SpillPlacement>();
1266
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001267 SA.reset(new SplitAnalysis(*MF, *LIS, *Loops));
1268
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001269 allocatePhysRegs();
1270 addMBBLiveIns(MF);
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +00001271 LIS->addKillFlags();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001272
1273 // Run rewriter
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001274 {
1275 NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
1276 std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
1277 rewriter->runOnMachineFunction(*MF, *VRM, LIS);
1278 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001279
1280 // The pass output is in VirtRegMap. Release all the transient data.
1281 releaseMemory();
1282
1283 return true;
1284}