blob: 378219b97bef54327e51eb8ff1255cb697c6d291 [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"
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000024#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000025#include "llvm/Analysis/AliasAnalysis.h"
26#include "llvm/Function.h"
27#include "llvm/PassAnalysisSupport.h"
28#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000029#include "llvm/CodeGen/EdgeBundles.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000030#include "llvm/CodeGen/LiveIntervalAnalysis.h"
31#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000032#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000034#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000035#include "llvm/CodeGen/MachineLoopRanges.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/Passes.h"
38#include "llvm/CodeGen/RegAllocRegistry.h"
39#include "llvm/CodeGen/RegisterCoalescer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000040#include "llvm/Target/TargetOptions.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000044#include "llvm/Support/Timer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000045
46using namespace llvm;
47
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000048STATISTIC(NumGlobalSplits, "Number of split global live ranges");
49STATISTIC(NumLocalSplits, "Number of split local live ranges");
50STATISTIC(NumReassigned, "Number of interferences reassigned");
51STATISTIC(NumEvicted, "Number of interferences evicted");
52
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000053static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
54 createGreedyRegisterAllocator);
55
56namespace {
57class RAGreedy : public MachineFunctionPass, public RegAllocBase {
58 // context
59 MachineFunction *MF;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000060 BitVector ReservedRegs;
61
62 // analyses
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000063 SlotIndexes *Indexes;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000064 LiveStacks *LS;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000065 MachineDominatorTree *DomTree;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000066 MachineLoopInfo *Loops;
67 MachineLoopRanges *LoopRanges;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000068 EdgeBundles *Bundles;
69 SpillPlacement *SpillPlacer;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000070
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000071 // state
72 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000073 std::auto_ptr<SplitAnalysis> SA;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000074
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000075 // splitting state.
76
77 /// All basic blocks where the current register is live.
78 SmallVector<SpillPlacement::BlockConstraint, 8> SpillConstraints;
79
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +000080 /// For every instruction in SA->UseSlots, store the previous non-copy
81 /// instruction.
82 SmallVector<SlotIndex, 8> PrevSlot;
83
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000084public:
85 RAGreedy();
86
87 /// Return the pass name.
88 virtual const char* getPassName() const {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000089 return "Greedy Register Allocator";
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000090 }
91
92 /// RAGreedy analysis usage.
93 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
94
95 virtual void releaseMemory();
96
97 virtual Spiller &spiller() { return *SpillerInstance; }
98
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +000099 virtual float getPriority(LiveInterval *LI);
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000100
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000101 virtual unsigned selectOrSplit(LiveInterval&,
102 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000103
104 /// Perform register allocation.
105 virtual bool runOnMachineFunction(MachineFunction &mf);
106
107 static char ID;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000108
109private:
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000110 bool checkUncachedInterference(LiveInterval&, unsigned);
111 LiveInterval *getSingleInterference(LiveInterval&, unsigned);
Andrew Trickb853e6c2010-12-09 18:15:21 +0000112 bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000113 float calcInterferenceWeight(LiveInterval&, unsigned);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000114 float calcInterferenceInfo(LiveInterval&, unsigned);
115 float calcGlobalSplitCost(const BitVector&);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000116 void splitAroundRegion(LiveInterval&, unsigned, const BitVector&,
117 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000118 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
119 SlotIndex getPrevMappedIndex(const MachineInstr*);
120 void calcPrevSlots();
121 unsigned nextSplitPoint(unsigned);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000122
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000123 unsigned tryReassignOrEvict(LiveInterval&, AllocationOrder&,
124 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000125 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
126 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000127 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
128 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000129 unsigned trySplit(LiveInterval&, AllocationOrder&,
130 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000131 unsigned trySpillInterferences(LiveInterval&, AllocationOrder&,
132 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000133};
134} // end anonymous namespace
135
136char RAGreedy::ID = 0;
137
138FunctionPass* llvm::createGreedyRegisterAllocator() {
139 return new RAGreedy();
140}
141
142RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000143 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000144 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
145 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
146 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
147 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
148 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
149 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
150 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
151 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000152 initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000153 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000154 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
155 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000156}
157
158void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
159 AU.setPreservesCFG();
160 AU.addRequired<AliasAnalysis>();
161 AU.addPreserved<AliasAnalysis>();
162 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000163 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000164 AU.addPreserved<SlotIndexes>();
165 if (StrongPHIElim)
166 AU.addRequiredID(StrongPHIEliminationID);
167 AU.addRequiredTransitive<RegisterCoalescer>();
168 AU.addRequired<CalculateSpillWeights>();
169 AU.addRequired<LiveStacks>();
170 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +0000171 AU.addRequired<MachineDominatorTree>();
172 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000173 AU.addRequired<MachineLoopInfo>();
174 AU.addPreserved<MachineLoopInfo>();
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000175 AU.addRequired<MachineLoopRanges>();
176 AU.addPreserved<MachineLoopRanges>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000177 AU.addRequired<VirtRegMap>();
178 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000179 AU.addRequired<EdgeBundles>();
180 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000181 MachineFunctionPass::getAnalysisUsage(AU);
182}
183
184void RAGreedy::releaseMemory() {
185 SpillerInstance.reset(0);
186 RegAllocBase::releaseMemory();
187}
188
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000189float RAGreedy::getPriority(LiveInterval *LI) {
190 float Priority = LI->weight;
191
192 // Prioritize hinted registers so they are allocated first.
193 std::pair<unsigned, unsigned> Hint;
194 if (Hint.first || Hint.second) {
195 // The hint can be target specific, a virtual register, or a physreg.
196 Priority *= 2;
197
198 // Prefer physreg hints above anything else.
199 if (Hint.first == 0 && TargetRegisterInfo::isPhysicalRegister(Hint.second))
200 Priority *= 2;
201 }
202 return Priority;
203}
204
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000205
206//===----------------------------------------------------------------------===//
207// Register Reassignment
208//===----------------------------------------------------------------------===//
209
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000210// Check interference without using the cache.
211bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg,
212 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000213 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
214 LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]);
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000215 if (subQ.checkInterference())
216 return true;
217 }
218 return false;
219}
220
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000221/// getSingleInterference - Return the single interfering virtual register
222/// assigned to PhysReg. Return 0 if more than one virtual register is
223/// interfering.
224LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg,
225 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000226 // Check physreg and aliases.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000227 LiveInterval *Interference = 0;
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000228 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000229 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
230 if (Q.checkInterference()) {
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000231 if (Interference)
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000232 return 0;
233 Q.collectInterferingVRegs(1);
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000234 if (!Q.seenAllInterferences())
235 return 0;
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000236 Interference = Q.interferingVRegs().front();
237 }
238 }
239 return Interference;
240}
241
Andrew Trickb853e6c2010-12-09 18:15:21 +0000242// Attempt to reassign this virtual register to a different physical register.
243//
244// FIXME: we are not yet caching these "second-level" interferences discovered
245// in the sub-queries. These interferences can change with each call to
246// selectOrSplit. However, we could implement a "may-interfere" cache that
247// could be conservatively dirtied when we reassign or split.
248//
249// FIXME: This may result in a lot of alias queries. We could summarize alias
250// live intervals in their parent register's live union, but it's messy.
251bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg,
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000252 unsigned WantedPhysReg) {
253 assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) &&
254 "Can only reassign virtual registers");
255 assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) &&
Andrew Trickb853e6c2010-12-09 18:15:21 +0000256 "inconsistent phys reg assigment");
257
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +0000258 AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs);
259 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000260 // Don't reassign to a WantedPhysReg alias.
261 if (TRI->regsOverlap(PhysReg, WantedPhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000262 continue;
263
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000264 if (checkUncachedInterference(InterferingVReg, PhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000265 continue;
266
Andrew Trickb853e6c2010-12-09 18:15:21 +0000267 // Reassign the interfering virtual reg to this physical reg.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000268 unsigned OldAssign = VRM->getPhys(InterferingVReg.reg);
269 DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " <<
270 TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n');
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000271 unassign(InterferingVReg, OldAssign);
272 assign(InterferingVReg, PhysReg);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000273 ++NumReassigned;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000274 return true;
275 }
276 return false;
277}
278
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000279/// tryReassignOrEvict - Try to reassign a single interferences to a different
280/// physreg, or evict a single interference with a lower spill weight.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000281/// @param VirtReg Currently unassigned virtual register.
282/// @param Order Physregs to try.
283/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000284unsigned RAGreedy::tryReassignOrEvict(LiveInterval &VirtReg,
285 AllocationOrder &Order,
286 SmallVectorImpl<LiveInterval*> &NewVRegs){
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000287 NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000288
289 // Keep track of the lightest single interference seen so far.
290 float BestWeight = VirtReg.weight;
291 LiveInterval *BestVirt = 0;
292 unsigned BestPhys = 0;
293
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000294 Order.rewind();
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000295 while (unsigned PhysReg = Order.next()) {
296 LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg);
297 if (!InterferingVReg)
298 continue;
299 if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg))
300 continue;
301 if (reassignVReg(*InterferingVReg, PhysReg))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000302 return PhysReg;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000303
304 // Cannot reassign, is this an eviction candidate?
305 if (InterferingVReg->weight < BestWeight) {
306 BestVirt = InterferingVReg;
307 BestPhys = PhysReg;
308 BestWeight = InterferingVReg->weight;
309 }
310 }
311
312 // Nothing reassigned, can we evict a lighter single interference?
313 if (BestVirt) {
314 DEBUG(dbgs() << "evicting lighter " << *BestVirt << '\n');
315 unassign(*BestVirt, VRM->getPhys(BestVirt->reg));
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000316 ++NumEvicted;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000317 NewVRegs.push_back(BestVirt);
318 return BestPhys;
319 }
320
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000321 return 0;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000322}
323
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000324
325//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000326// Region Splitting
327//===----------------------------------------------------------------------===//
328
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000329/// calcInterferenceInfo - Compute per-block outgoing and ingoing constraints
330/// when considering interference from PhysReg. Also compute an optimistic local
331/// cost of this interference pattern.
332///
333/// The final cost of a split is the local cost + global cost of preferences
334/// broken by SpillPlacement.
335///
336float RAGreedy::calcInterferenceInfo(LiveInterval &VirtReg, unsigned PhysReg) {
337 // Reset interference dependent info.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000338 SpillConstraints.resize(SA->LiveBlocks.size());
339 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
340 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000341 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000342 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000343 BC.Entry = (BI.Uses && BI.LiveIn) ?
344 SpillPlacement::PrefReg : SpillPlacement::DontCare;
345 BC.Exit = (BI.Uses && BI.LiveOut) ?
346 SpillPlacement::PrefReg : SpillPlacement::DontCare;
347 BI.OverlapEntry = BI.OverlapExit = false;
348 }
349
350 // Add interference info from each PhysReg alias.
351 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
352 if (!query(VirtReg, *AI).checkInterference())
353 continue;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000354 LiveIntervalUnion::SegmentIter IntI =
355 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
356 if (!IntI.valid())
357 continue;
358
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000359 // Determine which blocks have interference live in or after the last split
360 // point.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000361 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
362 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000363 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
364 SlotIndex Start, Stop;
365 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
366
367 // Skip interference-free blocks.
368 if (IntI.start() >= Stop)
369 continue;
370
371 // Is the interference live-in?
372 if (BI.LiveIn) {
373 IntI.advanceTo(Start);
374 if (!IntI.valid())
375 break;
376 if (IntI.start() <= Start)
377 BC.Entry = SpillPlacement::MustSpill;
378 }
379
380 // Is the interference overlapping the last split point?
381 if (BI.LiveOut) {
382 if (IntI.stop() < BI.LastSplitPoint)
383 IntI.advanceTo(BI.LastSplitPoint.getPrevSlot());
384 if (!IntI.valid())
385 break;
386 if (IntI.start() < Stop)
387 BC.Exit = SpillPlacement::MustSpill;
388 }
389 }
390
391 // Rewind iterator and check other interferences.
392 IntI.find(VirtReg.beginIndex());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000393 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
394 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000395 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
396 SlotIndex Start, Stop;
397 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
398
399 // Skip interference-free blocks.
400 if (IntI.start() >= Stop)
401 continue;
402
403 // Handle transparent blocks with interference separately.
404 // Transparent blocks never incur any fixed cost.
405 if (BI.LiveThrough && !BI.Uses) {
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000406 IntI.advanceTo(Start);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000407 if (!IntI.valid())
408 break;
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000409 if (IntI.start() >= Stop)
410 continue;
411
412 if (BC.Entry != SpillPlacement::MustSpill)
413 BC.Entry = SpillPlacement::PrefSpill;
414 if (BC.Exit != SpillPlacement::MustSpill)
415 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000416 continue;
417 }
418
419 // Now we only have blocks with uses left.
420 // Check if the interference overlaps the uses.
421 assert(BI.Uses && "Non-transparent block without any uses");
422
423 // Check interference on entry.
424 if (BI.LiveIn && BC.Entry != SpillPlacement::MustSpill) {
425 IntI.advanceTo(Start);
426 if (!IntI.valid())
427 break;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000428 // Not live in, but before the first use.
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000429 if (IntI.start() < BI.FirstUse) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000430 BC.Entry = SpillPlacement::PrefSpill;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000431 // If the block contains a kill from an earlier split, never split
432 // again in the same block.
433 if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Kill))
434 BC.Entry = SpillPlacement::MustSpill;
435 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000436 }
437
438 // Does interference overlap the uses in the entry segment
439 // [FirstUse;Kill)?
440 if (BI.LiveIn && !BI.OverlapEntry) {
441 IntI.advanceTo(BI.FirstUse);
442 if (!IntI.valid())
443 break;
444 // A live-through interval has no kill.
445 // Check [FirstUse;LastUse) instead.
446 if (IntI.start() < (BI.LiveThrough ? BI.LastUse : BI.Kill))
447 BI.OverlapEntry = true;
448 }
449
450 // Does interference overlap the uses in the exit segment [Def;LastUse)?
451 if (BI.LiveOut && !BI.LiveThrough && !BI.OverlapExit) {
452 IntI.advanceTo(BI.Def);
453 if (!IntI.valid())
454 break;
455 if (IntI.start() < BI.LastUse)
456 BI.OverlapExit = true;
457 }
458
459 // Check interference on exit.
460 if (BI.LiveOut && BC.Exit != SpillPlacement::MustSpill) {
461 // Check interference between LastUse and Stop.
462 if (BC.Exit != SpillPlacement::PrefSpill) {
463 IntI.advanceTo(BI.LastUse);
464 if (!IntI.valid())
465 break;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000466 if (IntI.start() < Stop) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000467 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000468 // Avoid splitting twice in the same block.
469 if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Def))
470 BC.Exit = SpillPlacement::MustSpill;
471 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000472 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000473 }
474 }
475 }
476
477 // Accumulate a local cost of this interference pattern.
478 float LocalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000479 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
480 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000481 if (!BI.Uses)
482 continue;
483 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
484 unsigned Inserts = 0;
485
486 // Do we need spill code for the entry segment?
487 if (BI.LiveIn)
488 Inserts += BI.OverlapEntry || BC.Entry != SpillPlacement::PrefReg;
489
490 // For the exit segment?
491 if (BI.LiveOut)
492 Inserts += BI.OverlapExit || BC.Exit != SpillPlacement::PrefReg;
493
494 // The local cost of spill code in this block is the block frequency times
495 // the number of spill instructions inserted.
496 if (Inserts)
497 LocalCost += Inserts * SpillPlacer->getBlockFrequency(BI.MBB);
498 }
499 DEBUG(dbgs() << "Local cost of " << PrintReg(PhysReg, TRI) << " = "
500 << LocalCost << '\n');
501 return LocalCost;
502}
503
504/// calcGlobalSplitCost - Return the global split cost of following the split
505/// pattern in LiveBundles. This cost should be added to the local cost of the
506/// interference pattern in SpillConstraints.
507///
508float RAGreedy::calcGlobalSplitCost(const BitVector &LiveBundles) {
509 float GlobalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000510 for (unsigned i = 0, e = SpillConstraints.size(); i != e; ++i) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000511 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
512 unsigned Inserts = 0;
513 // Broken entry preference?
514 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 0)] !=
515 (BC.Entry == SpillPlacement::PrefReg);
516 // Broken exit preference?
517 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 1)] !=
518 (BC.Exit == SpillPlacement::PrefReg);
519 if (Inserts)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000520 GlobalCost +=
521 Inserts * SpillPlacer->getBlockFrequency(SA->LiveBlocks[i].MBB);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000522 }
523 DEBUG(dbgs() << "Global cost = " << GlobalCost << '\n');
524 return GlobalCost;
525}
526
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000527/// splitAroundRegion - Split VirtReg around the region determined by
528/// LiveBundles. Make an effort to avoid interference from PhysReg.
529///
530/// The 'register' interval is going to contain as many uses as possible while
531/// avoiding interference. The 'stack' interval is the complement constructed by
532/// SplitEditor. It will contain the rest.
533///
534void RAGreedy::splitAroundRegion(LiveInterval &VirtReg, unsigned PhysReg,
535 const BitVector &LiveBundles,
536 SmallVectorImpl<LiveInterval*> &NewVRegs) {
537 DEBUG({
538 dbgs() << "Splitting around region for " << PrintReg(PhysReg, TRI)
539 << " with bundles";
540 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
541 dbgs() << " EB#" << i;
542 dbgs() << ".\n";
543 });
544
545 // First compute interference ranges in the live blocks.
546 typedef std::pair<SlotIndex, SlotIndex> IndexPair;
547 SmallVector<IndexPair, 8> InterferenceRanges;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000548 InterferenceRanges.resize(SA->LiveBlocks.size());
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000549 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
550 if (!query(VirtReg, *AI).checkInterference())
551 continue;
552 LiveIntervalUnion::SegmentIter IntI =
553 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
554 if (!IntI.valid())
555 continue;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000556 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
557 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000558 IndexPair &IP = InterferenceRanges[i];
559 SlotIndex Start, Stop;
560 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
561 // Skip interference-free blocks.
562 if (IntI.start() >= Stop)
563 continue;
564
565 // First interference in block.
566 if (BI.LiveIn) {
567 IntI.advanceTo(Start);
568 if (!IntI.valid())
569 break;
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000570 if (IntI.start() >= Stop)
571 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000572 if (!IP.first.isValid() || IntI.start() < IP.first)
573 IP.first = IntI.start();
574 }
575
576 // Last interference in block.
577 if (BI.LiveOut) {
578 IntI.advanceTo(Stop);
579 if (!IntI.valid() || IntI.start() >= Stop)
580 --IntI;
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000581 if (IntI.stop() <= Start)
582 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000583 if (!IP.second.isValid() || IntI.stop() > IP.second)
584 IP.second = IntI.stop();
585 }
586 }
587 }
588
589 SmallVector<LiveInterval*, 4> SpillRegs;
590 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
591 SplitEditor SE(*SA, *LIS, *VRM, *DomTree, LREdit);
592
593 // Create the main cross-block interval.
594 SE.openIntv();
595
596 // First add all defs that are live out of a block.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000597 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
598 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000599 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
600 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
601
602 // Should the register be live out?
603 if (!BI.LiveOut || !RegOut)
604 continue;
605
606 IndexPair &IP = InterferenceRanges[i];
607 SlotIndex Start, Stop;
608 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
609
610 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#"
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000611 << Bundles->getBundle(BI.MBB->getNumber(), 1)
612 << " intf [" << IP.first << ';' << IP.second << ')');
613
614 // The interference interval should either be invalid or overlap MBB.
615 assert((!IP.first.isValid() || IP.first < Stop) && "Bad interference");
616 assert((!IP.second.isValid() || IP.second > Start) && "Bad interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000617
618 // Check interference leaving the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000619 if (!IP.second.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000620 // Block is interference-free.
621 DEBUG(dbgs() << ", no interference");
622 if (!BI.Uses) {
623 assert(BI.LiveThrough && "No uses, but not live through block?");
624 // Block is live-through without interference.
625 DEBUG(dbgs() << ", no uses"
626 << (RegIn ? ", live-through.\n" : ", stack in.\n"));
627 if (!RegIn)
628 SE.enterIntvAtEnd(*BI.MBB);
629 continue;
630 }
631 if (!BI.LiveThrough) {
632 DEBUG(dbgs() << ", not live-through.\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000633 SE.useIntv(SE.enterIntvBefore(BI.Def), Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000634 continue;
635 }
636 if (!RegIn) {
637 // Block is live-through, but entry bundle is on the stack.
638 // Reload just before the first use.
639 DEBUG(dbgs() << ", not live-in, enter before first use.\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000640 SE.useIntv(SE.enterIntvBefore(BI.FirstUse), Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000641 continue;
642 }
643 DEBUG(dbgs() << ", live-through.\n");
644 continue;
645 }
646
647 // Block has interference.
648 DEBUG(dbgs() << ", interference to " << IP.second);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000649
650 if (!BI.LiveThrough && IP.second <= BI.Def) {
651 // The interference doesn't reach the outgoing segment.
652 DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n');
653 SE.useIntv(BI.Def, Stop);
654 continue;
655 }
656
657
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000658 if (!BI.Uses) {
659 // No uses in block, avoid interference by reloading as late as possible.
660 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000661 SlotIndex SegStart = SE.enterIntvAtEnd(*BI.MBB);
662 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000663 continue;
664 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000665
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000666 if (IP.second.getBoundaryIndex() < BI.LastUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000667 // There are interference-free uses at the end of the block.
668 // Find the first use that can get the live-out register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000669 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000670 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
671 IP.second.getBoundaryIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000672 assert(UI != SA->UseSlots.end() && "Couldn't find last use");
673 SlotIndex Use = *UI;
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000674 assert(Use <= BI.LastUse && "Couldn't find last use");
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000675 // Only attempt a split befroe the last split point.
676 if (Use.getBaseIndex() <= BI.LastSplitPoint) {
677 DEBUG(dbgs() << ", free use at " << Use << ".\n");
678 SlotIndex SegStart = SE.enterIntvBefore(Use);
679 assert(SegStart >= IP.second && "Couldn't avoid interference");
680 assert(SegStart < BI.LastSplitPoint && "Impossible split point");
681 SE.useIntv(SegStart, Stop);
682 continue;
683 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000684 }
685
686 // Interference is after the last use.
687 DEBUG(dbgs() << " after last use.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000688 SlotIndex SegStart = SE.enterIntvAtEnd(*BI.MBB);
689 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000690 }
691
692 // Now all defs leading to live bundles are handled, do everything else.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000693 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
694 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000695 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
696 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
697
698 // Is the register live-in?
699 if (!BI.LiveIn || !RegIn)
700 continue;
701
702 // We have an incoming register. Check for interference.
703 IndexPair &IP = InterferenceRanges[i];
704 SlotIndex Start, Stop;
705 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
706
707 DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0)
708 << " -> BB#" << BI.MBB->getNumber());
709
710 // Check interference entering the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000711 if (!IP.first.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000712 // Block is interference-free.
713 DEBUG(dbgs() << ", no interference");
714 if (!BI.Uses) {
715 assert(BI.LiveThrough && "No uses, but not live through block?");
716 // Block is live-through without interference.
717 if (RegOut) {
718 DEBUG(dbgs() << ", no uses, live-through.\n");
719 SE.useIntv(Start, Stop);
720 } else {
721 DEBUG(dbgs() << ", no uses, stack-out.\n");
722 SE.leaveIntvAtTop(*BI.MBB);
723 }
724 continue;
725 }
726 if (!BI.LiveThrough) {
727 DEBUG(dbgs() << ", killed in block.\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000728 SE.useIntv(Start, SE.leaveIntvAfter(BI.Kill));
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000729 continue;
730 }
731 if (!RegOut) {
732 // Block is live-through, but exit bundle is on the stack.
733 // Spill immediately after the last use.
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000734 if (BI.LastUse < BI.LastSplitPoint) {
735 DEBUG(dbgs() << ", uses, stack-out.\n");
736 SE.useIntv(Start, SE.leaveIntvAfter(BI.LastUse));
737 continue;
738 }
739 // The last use is after the last split point, it is probably an
740 // indirect jump.
741 DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point "
742 << BI.LastSplitPoint << ", stack-out.\n");
Jakob Stoklund Olesen23cd57c2011-02-09 23:33:02 +0000743 SlotIndex SegEnd = SE.leaveIntvBefore(BI.LastSplitPoint);
744 SE.useIntv(Start, SegEnd);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000745 // Run a double interval from the split to the last use.
746 // This makes it possible to spill the complement without affecting the
747 // indirect branch.
748 SE.overlapIntv(SegEnd, BI.LastUse);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000749 continue;
750 }
751 // Register is live-through.
752 DEBUG(dbgs() << ", uses, live-through.\n");
753 SE.useIntv(Start, Stop);
754 continue;
755 }
756
757 // Block has interference.
758 DEBUG(dbgs() << ", interference from " << IP.first);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000759
760 if (!BI.LiveThrough && IP.first >= BI.Kill) {
761 // The interference doesn't reach the outgoing segment.
762 DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n');
763 SE.useIntv(Start, BI.Kill);
764 continue;
765 }
766
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000767 if (!BI.Uses) {
768 // No uses in block, avoid interference by spilling as soon as possible.
769 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000770 SlotIndex SegEnd = SE.leaveIntvAtTop(*BI.MBB);
771 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000772 continue;
773 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000774 if (IP.first.getBaseIndex() > BI.FirstUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000775 // There are interference-free uses at the beginning of the block.
776 // Find the last use that can get the register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000777 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000778 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
779 IP.first.getBaseIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000780 assert(UI != SA->UseSlots.begin() && "Couldn't find first use");
781 SlotIndex Use = (--UI)->getBoundaryIndex();
782 DEBUG(dbgs() << ", free use at " << *UI << ".\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000783 SlotIndex SegEnd = SE.leaveIntvAfter(Use);
784 assert(SegEnd <= IP.first && "Couldn't avoid interference");
785 SE.useIntv(Start, SegEnd);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000786 continue;
787 }
788
789 // Interference is before the first use.
790 DEBUG(dbgs() << " before first use.\n");
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000791 SlotIndex SegEnd = SE.leaveIntvAtTop(*BI.MBB);
792 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000793 }
794
795 SE.closeIntv();
796
797 // FIXME: Should we be more aggressive about splitting the stack region into
798 // per-block segments? The current approach allows the stack region to
799 // separate into connected components. Some components may be allocatable.
800 SE.finish();
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000801 ++NumGlobalSplits;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000802
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000803 if (VerifyEnabled) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000804 MF->verify(this, "After splitting live range around region");
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000805
806#ifndef NDEBUG
807 // Make sure that at least one of the new intervals can allocate to PhysReg.
808 // That was the whole point of splitting the live range.
809 bool found = false;
810 for (LiveRangeEdit::iterator I = LREdit.begin(), E = LREdit.end(); I != E;
811 ++I)
812 if (!checkUncachedInterference(**I, PhysReg)) {
813 found = true;
814 break;
815 }
816 assert(found && "No allocatable intervals after pointless splitting");
817#endif
818 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000819}
820
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000821unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
822 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000823 BitVector LiveBundles, BestBundles;
824 float BestCost = 0;
825 unsigned BestReg = 0;
826 Order.rewind();
827 while (unsigned PhysReg = Order.next()) {
828 float Cost = calcInterferenceInfo(VirtReg, PhysReg);
829 if (BestReg && Cost >= BestCost)
830 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000831
832 SpillPlacer->placeSpills(SpillConstraints, LiveBundles);
833 // No live bundles, defer to splitSingleBlocks().
834 if (!LiveBundles.any())
835 continue;
836
837 Cost += calcGlobalSplitCost(LiveBundles);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000838 if (!BestReg || Cost < BestCost) {
839 BestReg = PhysReg;
840 BestCost = Cost;
841 BestBundles.swap(LiveBundles);
842 }
843 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000844
845 if (!BestReg)
846 return 0;
847
848 splitAroundRegion(VirtReg, BestReg, BestBundles, NewVRegs);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000849 return 0;
850}
851
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000852
853//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000854// Local Splitting
855//===----------------------------------------------------------------------===//
856
857
858/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
859/// in order to use PhysReg between two entries in SA->UseSlots.
860///
861/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
862///
863void RAGreedy::calcGapWeights(unsigned PhysReg,
864 SmallVectorImpl<float> &GapWeight) {
865 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
866 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
867 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
868 const unsigned NumGaps = Uses.size()-1;
869
870 // Start and end points for the interference check.
871 SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse;
872 SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse;
873
874 GapWeight.assign(NumGaps, 0.0f);
875
876 // Add interference from each overlapping register.
877 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
878 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
879 .checkInterference())
880 continue;
881
882 // We know that VirtReg is a continuous interval from FirstUse to LastUse,
883 // so we don't need InterferenceQuery.
884 //
885 // Interference that overlaps an instruction is counted in both gaps
886 // surrounding the instruction. The exception is interference before
887 // StartIdx and after StopIdx.
888 //
889 LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
890 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
891 // Skip the gaps before IntI.
892 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
893 if (++Gap == NumGaps)
894 break;
895 if (Gap == NumGaps)
896 break;
897
898 // Update the gaps covered by IntI.
899 const float weight = IntI.value()->weight;
900 for (; Gap != NumGaps; ++Gap) {
901 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
902 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
903 break;
904 }
905 if (Gap == NumGaps)
906 break;
907 }
908 }
909}
910
911/// getPrevMappedIndex - Return the slot index of the last non-copy instruction
912/// before MI that has a slot index. If MI is the first mapped instruction in
913/// its block, return the block start index instead.
914///
915SlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) {
916 assert(MI && "Missing MachineInstr");
917 const MachineBasicBlock *MBB = MI->getParent();
918 MachineBasicBlock::const_iterator B = MBB->begin(), I = MI;
919 while (I != B)
920 if (!(--I)->isDebugValue() && !I->isCopy())
921 return Indexes->getInstructionIndex(I);
922 return Indexes->getMBBStartIdx(MBB);
923}
924
925/// calcPrevSlots - Fill in the PrevSlot array with the index of the previous
926/// real non-copy instruction for each instruction in SA->UseSlots.
927///
928void RAGreedy::calcPrevSlots() {
929 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
930 PrevSlot.clear();
931 PrevSlot.reserve(Uses.size());
932 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
933 const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]);
934 PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex());
935 }
936}
937
938/// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may
939/// be beneficial to split before UseSlots[i].
940///
941/// 0 is always a valid split point
942unsigned RAGreedy::nextSplitPoint(unsigned i) {
943 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
944 const unsigned Size = Uses.size();
945 assert(i != Size && "No split points after the end");
946 // Allow split before i when Uses[i] is not adjacent to the previous use.
947 while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex())
948 ;
949 return i;
950}
951
952/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
953/// basic block.
954///
955unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
956 SmallVectorImpl<LiveInterval*> &NewVRegs) {
957 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
958 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
959
960 // Note that it is possible to have an interval that is live-in or live-out
961 // while only covering a single block - A phi-def can use undef values from
962 // predecessors, and the block could be a single-block loop.
963 // We don't bother doing anything clever about such a case, we simply assume
964 // that the interval is continuous from FirstUse to LastUse. We should make
965 // sure that we don't do anything illegal to such an interval, though.
966
967 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
968 if (Uses.size() <= 2)
969 return 0;
970 const unsigned NumGaps = Uses.size()-1;
971
972 DEBUG({
973 dbgs() << "tryLocalSplit: ";
974 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
975 dbgs() << ' ' << SA->UseSlots[i];
976 dbgs() << '\n';
977 });
978
979 // For every use, find the previous mapped non-copy instruction.
980 // We use this to detect valid split points, and to estimate new interval
981 // sizes.
982 calcPrevSlots();
983
984 unsigned BestBefore = NumGaps;
985 unsigned BestAfter = 0;
986 float BestDiff = 0;
987
988 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB);
989 SmallVector<float, 8> GapWeight;
990
991 Order.rewind();
992 while (unsigned PhysReg = Order.next()) {
993 // Keep track of the largest spill weight that would need to be evicted in
994 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
995 calcGapWeights(PhysReg, GapWeight);
996
997 // Try to find the best sequence of gaps to close.
998 // The new spill weight must be larger than any gap interference.
999
1000 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1001 unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1;
1002
1003 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1004 // It is the spill weight that needs to be evicted.
1005 float MaxGap = GapWeight[0];
1006 for (unsigned i = 1; i != SplitAfter; ++i)
1007 MaxGap = std::max(MaxGap, GapWeight[i]);
1008
1009 for (;;) {
1010 // Live before/after split?
1011 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1012 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1013
1014 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1015 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1016 << " i=" << MaxGap);
1017
1018 // Stop before the interval gets so big we wouldn't be making progress.
1019 if (!LiveBefore && !LiveAfter) {
1020 DEBUG(dbgs() << " all\n");
1021 break;
1022 }
1023 // Should the interval be extended or shrunk?
1024 bool Shrink = true;
1025 if (MaxGap < HUGE_VALF) {
1026 // Estimate the new spill weight.
1027 //
1028 // Each instruction reads and writes the register, except the first
1029 // instr doesn't read when !FirstLive, and the last instr doesn't write
1030 // when !LastLive.
1031 //
1032 // We will be inserting copies before and after, so the total number of
1033 // reads and writes is 2 * EstUses.
1034 //
1035 const unsigned EstUses = 2*(SplitAfter - SplitBefore) +
1036 2*(LiveBefore + LiveAfter);
1037
1038 // Try to guess the size of the new interval. This should be trivial,
1039 // but the slot index of an inserted copy can be a lot smaller than the
1040 // instruction it is inserted before if there are many dead indexes
1041 // between them.
1042 //
1043 // We measure the distance from the instruction before SplitBefore to
1044 // get a conservative estimate.
1045 //
1046 // The final distance can still be different if inserting copies
1047 // triggers a slot index renumbering.
1048 //
1049 const float EstWeight = normalizeSpillWeight(blockFreq * EstUses,
1050 PrevSlot[SplitBefore].distance(Uses[SplitAfter]));
1051 // Would this split be possible to allocate?
1052 // Never allocate all gaps, we wouldn't be making progress.
1053 float Diff = EstWeight - MaxGap;
1054 DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff);
1055 if (Diff > 0) {
1056 Shrink = false;
1057 if (Diff > BestDiff) {
1058 DEBUG(dbgs() << " (best)");
1059 BestDiff = Diff;
1060 BestBefore = SplitBefore;
1061 BestAfter = SplitAfter;
1062 }
1063 }
1064 }
1065
1066 // Try to shrink.
1067 if (Shrink) {
1068 SplitBefore = nextSplitPoint(SplitBefore);
1069 if (SplitBefore < SplitAfter) {
1070 DEBUG(dbgs() << " shrink\n");
1071 // Recompute the max when necessary.
1072 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1073 MaxGap = GapWeight[SplitBefore];
1074 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1075 MaxGap = std::max(MaxGap, GapWeight[i]);
1076 }
1077 continue;
1078 }
1079 MaxGap = 0;
1080 }
1081
1082 // Try to extend the interval.
1083 if (SplitAfter >= NumGaps) {
1084 DEBUG(dbgs() << " end\n");
1085 break;
1086 }
1087
1088 DEBUG(dbgs() << " extend\n");
1089 for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1;
1090 SplitAfter != e; ++SplitAfter)
1091 MaxGap = std::max(MaxGap, GapWeight[SplitAfter]);
1092 continue;
1093 }
1094 }
1095
1096 // Didn't find any candidates?
1097 if (BestBefore == NumGaps)
1098 return 0;
1099
1100 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1101 << '-' << Uses[BestAfter] << ", " << BestDiff
1102 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1103
1104 SmallVector<LiveInterval*, 4> SpillRegs;
1105 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
1106 SplitEditor SE(*SA, *LIS, *VRM, *DomTree, LREdit);
1107
1108 SE.openIntv();
1109 SlotIndex SegStart = SE.enterIntvBefore(Uses[BestBefore]);
1110 SlotIndex SegStop = SE.leaveIntvAfter(Uses[BestAfter]);
1111 SE.useIntv(SegStart, SegStop);
1112 SE.closeIntv();
1113 SE.finish();
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001114 ++NumLocalSplits;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001115
1116 return 0;
1117}
1118
1119//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001120// Live Range Splitting
1121//===----------------------------------------------------------------------===//
1122
1123/// trySplit - Try to split VirtReg or one of its interferences, making it
1124/// assignable.
1125/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1126unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1127 SmallVectorImpl<LiveInterval*>&NewVRegs) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001128 SA->analyze(&VirtReg);
1129
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001130 // Local intervals are handled separately.
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001131 if (LIS->intervalIsInOneMBB(VirtReg)) {
1132 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001133 return tryLocalSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001134 }
1135
1136 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001137
1138 // First try to split around a region spanning multiple blocks.
1139 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1140 if (PhysReg || !NewVRegs.empty())
1141 return PhysReg;
1142
1143 // Then isolate blocks with multiple uses.
1144 SplitAnalysis::BlockPtrSet Blocks;
1145 if (SA->getMultiUseBlocks(Blocks)) {
1146 SmallVector<LiveInterval*, 4> SpillRegs;
1147 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
1148 SplitEditor(*SA, *LIS, *VRM, *DomTree, LREdit).splitSingleBlocks(Blocks);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +00001149 if (VerifyEnabled)
1150 MF->verify(this, "After splitting live range around basic blocks");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001151 }
1152
1153 // Don't assign any physregs.
1154 return 0;
1155}
1156
1157
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001158//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001159// Spilling
1160//===----------------------------------------------------------------------===//
1161
1162/// calcInterferenceWeight - Calculate the combined spill weight of
1163/// interferences when assigning VirtReg to PhysReg.
1164float RAGreedy::calcInterferenceWeight(LiveInterval &VirtReg, unsigned PhysReg){
1165 float Sum = 0;
1166 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
1167 LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
1168 Q.collectInterferingVRegs();
1169 if (Q.seenUnspillableVReg())
1170 return HUGE_VALF;
1171 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i)
1172 Sum += Q.interferingVRegs()[i]->weight;
1173 }
1174 return Sum;
1175}
1176
1177/// trySpillInterferences - Try to spill interfering registers instead of the
1178/// current one. Only do it if the accumulated spill weight is smaller than the
1179/// current spill weight.
1180unsigned RAGreedy::trySpillInterferences(LiveInterval &VirtReg,
1181 AllocationOrder &Order,
1182 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1183 NamedRegionTimer T("Spill Interference", TimerGroupName, TimePassesIsEnabled);
1184 unsigned BestPhys = 0;
Duncan Sands2aea4902010-12-28 10:07:15 +00001185 float BestWeight = 0;
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001186
1187 Order.rewind();
1188 while (unsigned PhysReg = Order.next()) {
1189 float Weight = calcInterferenceWeight(VirtReg, PhysReg);
1190 if (Weight == HUGE_VALF || Weight >= VirtReg.weight)
1191 continue;
1192 if (!BestPhys || Weight < BestWeight)
1193 BestPhys = PhysReg, BestWeight = Weight;
1194 }
1195
1196 // No candidates found.
1197 if (!BestPhys)
1198 return 0;
1199
1200 // Collect all interfering registers.
1201 SmallVector<LiveInterval*, 8> Spills;
1202 for (const unsigned *AI = TRI->getOverlaps(BestPhys); *AI; ++AI) {
1203 LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
1204 Spills.append(Q.interferingVRegs().begin(), Q.interferingVRegs().end());
1205 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
1206 LiveInterval *VReg = Q.interferingVRegs()[i];
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +00001207 unassign(*VReg, *AI);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001208 }
1209 }
1210
1211 // Spill them all.
1212 DEBUG(dbgs() << "spilling " << Spills.size() << " interferences with weight "
1213 << BestWeight << '\n');
1214 for (unsigned i = 0, e = Spills.size(); i != e; ++i)
1215 spiller().spill(Spills[i], NewVRegs, Spills);
1216 return BestPhys;
1217}
1218
1219
1220//===----------------------------------------------------------------------===//
1221// Main Entry Point
1222//===----------------------------------------------------------------------===//
1223
1224unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001225 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001226 // First try assigning a free register.
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +00001227 AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs);
1228 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001229 if (!checkPhysRegInterference(VirtReg, PhysReg))
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001230 return PhysReg;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001231 }
Andrew Trickb853e6c2010-12-09 18:15:21 +00001232
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001233 // Try to reassign interferences.
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +00001234 if (unsigned PhysReg = tryReassignOrEvict(VirtReg, Order, NewVRegs))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001235 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001236
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001237 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1238
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001239 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001240 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1241 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +00001242 return PhysReg;
1243
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001244 // Try to spill another interfering reg with less spill weight.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001245 PhysReg = trySpillInterferences(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001246 if (PhysReg)
1247 return PhysReg;
1248
1249 // Finally spill VirtReg itself.
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001250 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001251 SmallVector<LiveInterval*, 1> pendingSpills;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001252 spiller().spill(&VirtReg, NewVRegs, pendingSpills);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001253
1254 // The live virtual register requesting allocation was spilled, so tell
1255 // the caller not to allocate anything during this round.
1256 return 0;
1257}
1258
1259bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1260 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1261 << "********** Function: "
1262 << ((Value*)mf.getFunction())->getName() << '\n');
1263
1264 MF = &mf;
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001265 if (VerifyEnabled)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +00001266 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001267
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +00001268 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001269 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +00001270 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001271 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesenf6dff842010-12-10 22:54:44 +00001272 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001273 Loops = &getAnalysis<MachineLoopInfo>();
1274 LoopRanges = &getAnalysis<MachineLoopRanges>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001275 Bundles = &getAnalysis<EdgeBundles>();
1276 SpillPlacer = &getAnalysis<SpillPlacement>();
1277
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +00001278 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001279
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001280 allocatePhysRegs();
1281 addMBBLiveIns(MF);
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +00001282 LIS->addKillFlags();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001283
1284 // Run rewriter
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001285 {
1286 NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +00001287 VRM->rewrite(Indexes);
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001288 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001289
1290 // The pass output is in VirtRegMap. Release all the transient data.
1291 releaseMemory();
1292
1293 return true;
1294}