blob: 14e376db6059b764d3ea5fddd3f33abec180af0b [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
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000046#include <queue>
47
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000048using namespace llvm;
49
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000050STATISTIC(NumGlobalSplits, "Number of split global live ranges");
51STATISTIC(NumLocalSplits, "Number of split local live ranges");
52STATISTIC(NumReassigned, "Number of interferences reassigned");
53STATISTIC(NumEvicted, "Number of interferences evicted");
54
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000055static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
56 createGreedyRegisterAllocator);
57
58namespace {
59class RAGreedy : public MachineFunctionPass, public RegAllocBase {
60 // context
61 MachineFunction *MF;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000062 BitVector ReservedRegs;
63
64 // analyses
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000065 SlotIndexes *Indexes;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000066 LiveStacks *LS;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000067 MachineDominatorTree *DomTree;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000068 MachineLoopInfo *Loops;
69 MachineLoopRanges *LoopRanges;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000070 EdgeBundles *Bundles;
71 SpillPlacement *SpillPlacer;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000072
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000073 // state
74 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000075 std::priority_queue<std::pair<unsigned, unsigned> > Queue;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +000076
77 // Live ranges pass through a number of stages as we try to allocate them.
78 // Some of the stages may also create new live ranges:
79 //
80 // - Region splitting.
81 // - Per-block splitting.
82 // - Local splitting.
83 // - Spilling.
84 //
85 // Ranges produced by one of the stages skip the previous stages when they are
86 // dequeued. This improves performance because we can skip interference checks
87 // that are unlikely to give any results. It also guarantees that the live
88 // range splitting algorithm terminates, something that is otherwise hard to
89 // ensure.
90 enum LiveRangeStage {
91 RS_Original, ///< Never seen before, never split.
92 RS_Second, ///< Second time in the queue.
93 RS_Region, ///< Produced by region splitting.
94 RS_Block, ///< Produced by per-block splitting.
95 RS_Local, ///< Produced by local splitting.
96 RS_Spill ///< Produced by spilling.
97 };
98
99 IndexedMap<unsigned char, VirtReg2IndexFunctor> LRStage;
100
101 LiveRangeStage getStage(const LiveInterval &VirtReg) const {
102 return LiveRangeStage(LRStage[VirtReg.reg]);
103 }
104
105 template<typename Iterator>
106 void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
107 LRStage.resize(MRI->getNumVirtRegs());
108 for (;Begin != End; ++Begin)
109 LRStage[(*Begin)->reg] = NewStage;
110 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000111
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000112 // splitting state.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000113 std::auto_ptr<SplitAnalysis> SA;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000114 std::auto_ptr<SplitEditor> SE;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000115
116 /// All basic blocks where the current register is live.
117 SmallVector<SpillPlacement::BlockConstraint, 8> SpillConstraints;
118
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000119 /// For every instruction in SA->UseSlots, store the previous non-copy
120 /// instruction.
121 SmallVector<SlotIndex, 8> PrevSlot;
122
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000123public:
124 RAGreedy();
125
126 /// Return the pass name.
127 virtual const char* getPassName() const {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000128 return "Greedy Register Allocator";
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000129 }
130
131 /// RAGreedy analysis usage.
132 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000133 virtual void releaseMemory();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000134 virtual Spiller &spiller() { return *SpillerInstance; }
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000135 virtual void enqueue(LiveInterval *LI);
136 virtual LiveInterval *dequeue();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000137 virtual unsigned selectOrSplit(LiveInterval&,
138 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000139
140 /// Perform register allocation.
141 virtual bool runOnMachineFunction(MachineFunction &mf);
142
143 static char ID;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000144
145private:
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000146 bool checkUncachedInterference(LiveInterval&, unsigned);
147 LiveInterval *getSingleInterference(LiveInterval&, unsigned);
Andrew Trickb853e6c2010-12-09 18:15:21 +0000148 bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000149 float calcInterferenceInfo(LiveInterval&, unsigned);
150 float calcGlobalSplitCost(const BitVector&);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000151 void splitAroundRegion(LiveInterval&, unsigned, const BitVector&,
152 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000153 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
154 SlotIndex getPrevMappedIndex(const MachineInstr*);
155 void calcPrevSlots();
156 unsigned nextSplitPoint(unsigned);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000157 bool canEvictInterference(LiveInterval&, unsigned, unsigned, float&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000158
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000159 unsigned tryReassign(LiveInterval&, AllocationOrder&,
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000160 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000161 unsigned tryEvict(LiveInterval&, AllocationOrder&,
162 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000163 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
164 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000165 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
166 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000167 unsigned trySplit(LiveInterval&, AllocationOrder&,
168 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000169};
170} // end anonymous namespace
171
172char RAGreedy::ID = 0;
173
174FunctionPass* llvm::createGreedyRegisterAllocator() {
175 return new RAGreedy();
176}
177
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000178RAGreedy::RAGreedy(): MachineFunctionPass(ID), LRStage(RS_Original) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000179 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000180 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
181 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
182 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
183 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
184 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
185 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
186 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
187 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000188 initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000189 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000190 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
191 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000192}
193
194void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
195 AU.setPreservesCFG();
196 AU.addRequired<AliasAnalysis>();
197 AU.addPreserved<AliasAnalysis>();
198 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000199 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000200 AU.addPreserved<SlotIndexes>();
201 if (StrongPHIElim)
202 AU.addRequiredID(StrongPHIEliminationID);
203 AU.addRequiredTransitive<RegisterCoalescer>();
204 AU.addRequired<CalculateSpillWeights>();
205 AU.addRequired<LiveStacks>();
206 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +0000207 AU.addRequired<MachineDominatorTree>();
208 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000209 AU.addRequired<MachineLoopInfo>();
210 AU.addPreserved<MachineLoopInfo>();
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000211 AU.addRequired<MachineLoopRanges>();
212 AU.addPreserved<MachineLoopRanges>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000213 AU.addRequired<VirtRegMap>();
214 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000215 AU.addRequired<EdgeBundles>();
216 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000217 MachineFunctionPass::getAnalysisUsage(AU);
218}
219
220void RAGreedy::releaseMemory() {
221 SpillerInstance.reset(0);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000222 LRStage.clear();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000223 RegAllocBase::releaseMemory();
224}
225
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000226void RAGreedy::enqueue(LiveInterval *LI) {
227 // Prioritize live ranges by size, assigning larger ranges first.
228 // The queue holds (size, reg) pairs.
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000229 const unsigned Size = LI->getSize();
230 const unsigned Reg = LI->reg;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000231 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
232 "Can only enqueue virtual registers");
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000233 unsigned Prio;
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000234
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000235 LRStage.grow(Reg);
236 if (LRStage[Reg] == RS_Original)
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000237 // 1st generation ranges are handled first, long -> short.
238 Prio = (1u << 31) + Size;
239 else
240 // Repeat offenders are handled second, short -> long
241 Prio = (1u << 30) - Size;
Jakob Stoklund Olesend2a50732011-02-23 00:56:56 +0000242
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000243 // Boost ranges that have a physical register hint.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000244 const unsigned Hint = VRM->getRegAllocPref(Reg);
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000245 if (TargetRegisterInfo::isPhysicalRegister(Hint))
246 Prio |= (1u << 30);
247
248 Queue.push(std::make_pair(Prio, Reg));
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000249}
250
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000251LiveInterval *RAGreedy::dequeue() {
252 if (Queue.empty())
253 return 0;
254 LiveInterval *LI = &LIS->getInterval(Queue.top().second);
255 Queue.pop();
256 return LI;
257}
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000258
259//===----------------------------------------------------------------------===//
260// Register Reassignment
261//===----------------------------------------------------------------------===//
262
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000263// Check interference without using the cache.
264bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg,
265 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000266 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
267 LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]);
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000268 if (subQ.checkInterference())
269 return true;
270 }
271 return false;
272}
273
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000274/// getSingleInterference - Return the single interfering virtual register
275/// assigned to PhysReg. Return 0 if more than one virtual register is
276/// interfering.
277LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg,
278 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000279 // Check physreg and aliases.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000280 LiveInterval *Interference = 0;
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000281 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000282 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
283 if (Q.checkInterference()) {
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000284 if (Interference)
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000285 return 0;
Jakob Stoklund Olesen417df012011-02-23 00:29:55 +0000286 if (Q.collectInterferingVRegs(2) > 1)
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000287 return 0;
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000288 Interference = Q.interferingVRegs().front();
289 }
290 }
291 return Interference;
292}
293
Andrew Trickb853e6c2010-12-09 18:15:21 +0000294// Attempt to reassign this virtual register to a different physical register.
295//
296// FIXME: we are not yet caching these "second-level" interferences discovered
297// in the sub-queries. These interferences can change with each call to
298// selectOrSplit. However, we could implement a "may-interfere" cache that
299// could be conservatively dirtied when we reassign or split.
300//
301// FIXME: This may result in a lot of alias queries. We could summarize alias
302// live intervals in their parent register's live union, but it's messy.
303bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg,
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000304 unsigned WantedPhysReg) {
305 assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) &&
306 "Can only reassign virtual registers");
307 assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) &&
Andrew Trickb853e6c2010-12-09 18:15:21 +0000308 "inconsistent phys reg assigment");
309
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +0000310 AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs);
311 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000312 // Don't reassign to a WantedPhysReg alias.
313 if (TRI->regsOverlap(PhysReg, WantedPhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000314 continue;
315
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000316 if (checkUncachedInterference(InterferingVReg, PhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000317 continue;
318
Andrew Trickb853e6c2010-12-09 18:15:21 +0000319 // Reassign the interfering virtual reg to this physical reg.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000320 unsigned OldAssign = VRM->getPhys(InterferingVReg.reg);
321 DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " <<
322 TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n');
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000323 unassign(InterferingVReg, OldAssign);
324 assign(InterferingVReg, PhysReg);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000325 ++NumReassigned;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000326 return true;
327 }
328 return false;
329}
330
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000331/// tryReassign - Try to reassign a single interference to a different physreg.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000332/// @param VirtReg Currently unassigned virtual register.
333/// @param Order Physregs to try.
334/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000335unsigned RAGreedy::tryReassign(LiveInterval &VirtReg, AllocationOrder &Order,
336 SmallVectorImpl<LiveInterval*> &NewVRegs){
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000337 NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000338
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000339 Order.rewind();
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000340 while (unsigned PhysReg = Order.next()) {
341 LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg);
342 if (!InterferingVReg)
343 continue;
344 if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg))
345 continue;
346 if (reassignVReg(*InterferingVReg, PhysReg))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000347 return PhysReg;
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000348 }
349 return 0;
350}
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000351
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000352
353//===----------------------------------------------------------------------===//
354// Interference eviction
355//===----------------------------------------------------------------------===//
356
357/// canEvict - Return true if all interferences between VirtReg and PhysReg can
358/// be evicted. Set maxWeight to the maximal spill weight of an interference.
359bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
360 unsigned Size, float &MaxWeight) {
361 float Weight = 0;
362 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
363 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
364 // If there is 10 or more interferences, chances are one is smaller.
365 if (Q.collectInterferingVRegs(10) >= 10)
366 return false;
367
368 // CHeck if any interfering live range is shorter than VirtReg.
369 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
370 LiveInterval *Intf = Q.interferingVRegs()[i];
371 if (TargetRegisterInfo::isPhysicalRegister(Intf->reg))
372 return false;
373 if (Intf->getSize() <= Size)
374 return false;
375 Weight = std::max(Weight, Intf->weight);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000376 }
377 }
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000378 MaxWeight = Weight;
379 return true;
380}
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000381
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000382/// tryEvict - Try to evict all interferences for a physreg.
383/// @param VirtReg Currently unassigned virtual register.
384/// @param Order Physregs to try.
385/// @return Physreg to assign VirtReg, or 0.
386unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
387 AllocationOrder &Order,
388 SmallVectorImpl<LiveInterval*> &NewVRegs){
389 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
390
391 // We can only evict interference if all interfering registers are virtual and
392 // longer than VirtReg.
393 const unsigned Size = VirtReg.getSize();
394
395 // Keep track of the lightest single interference seen so far.
396 float BestWeight = 0;
397 unsigned BestPhys = 0;
398
399 Order.rewind();
400 while (unsigned PhysReg = Order.next()) {
401 float Weight = 0;
402 if (!canEvictInterference(VirtReg, PhysReg, Size, Weight))
403 continue;
404
405 // This is an eviction candidate.
406 DEBUG(dbgs() << "max " << PrintReg(PhysReg, TRI) << " interference = "
407 << Weight << '\n');
408 if (BestPhys && Weight >= BestWeight)
409 continue;
410
411 // Best so far.
412 BestPhys = PhysReg;
413 BestWeight = Weight;
Jakob Stoklund Olesen57f1e2c2011-02-25 01:04:22 +0000414 // Stop if the hint can be used.
415 if (Order.isHint(PhysReg))
416 break;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000417 }
418
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000419 if (!BestPhys)
420 return 0;
421
422 DEBUG(dbgs() << "evicting " << PrintReg(BestPhys, TRI) << " interference\n");
423 for (const unsigned *AliasI = TRI->getOverlaps(BestPhys); *AliasI; ++AliasI) {
424 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
425 assert(Q.seenAllInterferences() && "Didn't check all interfererences.");
426 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
427 LiveInterval *Intf = Q.interferingVRegs()[i];
428 unassign(*Intf, VRM->getPhys(Intf->reg));
429 ++NumEvicted;
430 NewVRegs.push_back(Intf);
431 }
432 }
433 return BestPhys;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000434}
435
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000436
437//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000438// Region Splitting
439//===----------------------------------------------------------------------===//
440
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000441/// calcInterferenceInfo - Compute per-block outgoing and ingoing constraints
442/// when considering interference from PhysReg. Also compute an optimistic local
443/// cost of this interference pattern.
444///
445/// The final cost of a split is the local cost + global cost of preferences
446/// broken by SpillPlacement.
447///
448float RAGreedy::calcInterferenceInfo(LiveInterval &VirtReg, unsigned PhysReg) {
449 // Reset interference dependent info.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000450 SpillConstraints.resize(SA->LiveBlocks.size());
451 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
452 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000453 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000454 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000455 BC.Entry = (BI.Uses && BI.LiveIn) ?
456 SpillPlacement::PrefReg : SpillPlacement::DontCare;
457 BC.Exit = (BI.Uses && BI.LiveOut) ?
458 SpillPlacement::PrefReg : SpillPlacement::DontCare;
459 BI.OverlapEntry = BI.OverlapExit = false;
460 }
461
462 // Add interference info from each PhysReg alias.
463 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
464 if (!query(VirtReg, *AI).checkInterference())
465 continue;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000466 LiveIntervalUnion::SegmentIter IntI =
467 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
468 if (!IntI.valid())
469 continue;
470
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000471 // Determine which blocks have interference live in or after the last split
472 // point.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000473 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
474 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000475 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000476
477 // Skip interference-free blocks.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000478 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000479 continue;
480
481 // Is the interference live-in?
482 if (BI.LiveIn) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000483 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000484 if (!IntI.valid())
485 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000486 if (IntI.start() <= BI.Start)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000487 BC.Entry = SpillPlacement::MustSpill;
488 }
489
490 // Is the interference overlapping the last split point?
491 if (BI.LiveOut) {
492 if (IntI.stop() < BI.LastSplitPoint)
493 IntI.advanceTo(BI.LastSplitPoint.getPrevSlot());
494 if (!IntI.valid())
495 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000496 if (IntI.start() < BI.Stop)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000497 BC.Exit = SpillPlacement::MustSpill;
498 }
499 }
500
501 // Rewind iterator and check other interferences.
502 IntI.find(VirtReg.beginIndex());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000503 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
504 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000505 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000506
507 // Skip interference-free blocks.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000508 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000509 continue;
510
511 // Handle transparent blocks with interference separately.
512 // Transparent blocks never incur any fixed cost.
513 if (BI.LiveThrough && !BI.Uses) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000514 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000515 if (!IntI.valid())
516 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000517 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000518 continue;
519
520 if (BC.Entry != SpillPlacement::MustSpill)
521 BC.Entry = SpillPlacement::PrefSpill;
522 if (BC.Exit != SpillPlacement::MustSpill)
523 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000524 continue;
525 }
526
527 // Now we only have blocks with uses left.
528 // Check if the interference overlaps the uses.
529 assert(BI.Uses && "Non-transparent block without any uses");
530
531 // Check interference on entry.
532 if (BI.LiveIn && BC.Entry != SpillPlacement::MustSpill) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000533 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000534 if (!IntI.valid())
535 break;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000536 // Not live in, but before the first use.
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000537 if (IntI.start() < BI.FirstUse) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000538 BC.Entry = SpillPlacement::PrefSpill;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000539 // If the block contains a kill from an earlier split, never split
540 // again in the same block.
541 if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Kill))
542 BC.Entry = SpillPlacement::MustSpill;
543 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000544 }
545
546 // Does interference overlap the uses in the entry segment
547 // [FirstUse;Kill)?
548 if (BI.LiveIn && !BI.OverlapEntry) {
549 IntI.advanceTo(BI.FirstUse);
550 if (!IntI.valid())
551 break;
552 // A live-through interval has no kill.
553 // Check [FirstUse;LastUse) instead.
554 if (IntI.start() < (BI.LiveThrough ? BI.LastUse : BI.Kill))
555 BI.OverlapEntry = true;
556 }
557
558 // Does interference overlap the uses in the exit segment [Def;LastUse)?
559 if (BI.LiveOut && !BI.LiveThrough && !BI.OverlapExit) {
560 IntI.advanceTo(BI.Def);
561 if (!IntI.valid())
562 break;
563 if (IntI.start() < BI.LastUse)
564 BI.OverlapExit = true;
565 }
566
567 // Check interference on exit.
568 if (BI.LiveOut && BC.Exit != SpillPlacement::MustSpill) {
569 // Check interference between LastUse and Stop.
570 if (BC.Exit != SpillPlacement::PrefSpill) {
571 IntI.advanceTo(BI.LastUse);
572 if (!IntI.valid())
573 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000574 if (IntI.start() < BI.Stop) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000575 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000576 // Avoid splitting twice in the same block.
577 if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Def))
578 BC.Exit = SpillPlacement::MustSpill;
579 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000580 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000581 }
582 }
583 }
584
585 // Accumulate a local cost of this interference pattern.
586 float LocalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000587 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
588 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000589 if (!BI.Uses)
590 continue;
591 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
592 unsigned Inserts = 0;
593
594 // Do we need spill code for the entry segment?
595 if (BI.LiveIn)
596 Inserts += BI.OverlapEntry || BC.Entry != SpillPlacement::PrefReg;
597
598 // For the exit segment?
599 if (BI.LiveOut)
600 Inserts += BI.OverlapExit || BC.Exit != SpillPlacement::PrefReg;
601
602 // The local cost of spill code in this block is the block frequency times
603 // the number of spill instructions inserted.
604 if (Inserts)
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +0000605 LocalCost += Inserts * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000606 }
607 DEBUG(dbgs() << "Local cost of " << PrintReg(PhysReg, TRI) << " = "
608 << LocalCost << '\n');
609 return LocalCost;
610}
611
612/// calcGlobalSplitCost - Return the global split cost of following the split
613/// pattern in LiveBundles. This cost should be added to the local cost of the
614/// interference pattern in SpillConstraints.
615///
616float RAGreedy::calcGlobalSplitCost(const BitVector &LiveBundles) {
617 float GlobalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000618 for (unsigned i = 0, e = SpillConstraints.size(); i != e; ++i) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000619 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
620 unsigned Inserts = 0;
621 // Broken entry preference?
622 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 0)] !=
623 (BC.Entry == SpillPlacement::PrefReg);
624 // Broken exit preference?
625 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 1)] !=
626 (BC.Exit == SpillPlacement::PrefReg);
627 if (Inserts)
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +0000628 GlobalCost += Inserts * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000629 }
Jakob Stoklund Olesenfb698102011-03-04 18:08:26 +0000630 DEBUG({
631 dbgs() << "Global cost = " << GlobalCost << " with bundles";
632 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
633 dbgs() << " EB#" << i;
634 dbgs() << ".\n";
635 });
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000636 return GlobalCost;
637}
638
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000639/// splitAroundRegion - Split VirtReg around the region determined by
640/// LiveBundles. Make an effort to avoid interference from PhysReg.
641///
642/// The 'register' interval is going to contain as many uses as possible while
643/// avoiding interference. The 'stack' interval is the complement constructed by
644/// SplitEditor. It will contain the rest.
645///
646void RAGreedy::splitAroundRegion(LiveInterval &VirtReg, unsigned PhysReg,
647 const BitVector &LiveBundles,
648 SmallVectorImpl<LiveInterval*> &NewVRegs) {
649 DEBUG({
650 dbgs() << "Splitting around region for " << PrintReg(PhysReg, TRI)
651 << " with bundles";
652 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
653 dbgs() << " EB#" << i;
654 dbgs() << ".\n";
655 });
656
657 // First compute interference ranges in the live blocks.
658 typedef std::pair<SlotIndex, SlotIndex> IndexPair;
659 SmallVector<IndexPair, 8> InterferenceRanges;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000660 InterferenceRanges.resize(SA->LiveBlocks.size());
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000661 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
662 if (!query(VirtReg, *AI).checkInterference())
663 continue;
664 LiveIntervalUnion::SegmentIter IntI =
665 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
666 if (!IntI.valid())
667 continue;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000668 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
669 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000670 IndexPair &IP = InterferenceRanges[i];
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000671
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000672 // Skip interference-free blocks.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000673 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000674 continue;
675
676 // First interference in block.
677 if (BI.LiveIn) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000678 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000679 if (!IntI.valid())
680 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000681 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000682 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000683 if (!IP.first.isValid() || IntI.start() < IP.first)
684 IP.first = IntI.start();
685 }
686
687 // Last interference in block.
688 if (BI.LiveOut) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000689 IntI.advanceTo(BI.Stop);
690 if (!IntI.valid() || IntI.start() >= BI.Stop)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000691 --IntI;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000692 if (IntI.stop() <= BI.Start)
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000693 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000694 if (!IP.second.isValid() || IntI.stop() > IP.second)
695 IP.second = IntI.stop();
696 }
697 }
698 }
699
700 SmallVector<LiveInterval*, 4> SpillRegs;
701 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000702 SE->reset(LREdit);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000703
704 // Create the main cross-block interval.
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000705 SE->openIntv();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000706
707 // First add all defs that are live out of a block.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000708 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
709 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000710 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
711 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
712
713 // Should the register be live out?
714 if (!BI.LiveOut || !RegOut)
715 continue;
716
717 IndexPair &IP = InterferenceRanges[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000718 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#"
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000719 << Bundles->getBundle(BI.MBB->getNumber(), 1)
720 << " intf [" << IP.first << ';' << IP.second << ')');
721
722 // The interference interval should either be invalid or overlap MBB.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000723 assert((!IP.first.isValid() || IP.first < BI.Stop) && "Bad interference");
724 assert((!IP.second.isValid() || IP.second > BI.Start)
725 && "Bad interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000726
727 // Check interference leaving the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000728 if (!IP.second.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000729 // Block is interference-free.
730 DEBUG(dbgs() << ", no interference");
731 if (!BI.Uses) {
732 assert(BI.LiveThrough && "No uses, but not live through block?");
733 // Block is live-through without interference.
734 DEBUG(dbgs() << ", no uses"
735 << (RegIn ? ", live-through.\n" : ", stack in.\n"));
736 if (!RegIn)
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000737 SE->enterIntvAtEnd(*BI.MBB);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000738 continue;
739 }
740 if (!BI.LiveThrough) {
741 DEBUG(dbgs() << ", not live-through.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000742 SE->useIntv(SE->enterIntvBefore(BI.Def), BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000743 continue;
744 }
745 if (!RegIn) {
746 // Block is live-through, but entry bundle is on the stack.
747 // Reload just before the first use.
748 DEBUG(dbgs() << ", not live-in, enter before first use.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000749 SE->useIntv(SE->enterIntvBefore(BI.FirstUse), BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000750 continue;
751 }
752 DEBUG(dbgs() << ", live-through.\n");
753 continue;
754 }
755
756 // Block has interference.
757 DEBUG(dbgs() << ", interference to " << IP.second);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000758
759 if (!BI.LiveThrough && IP.second <= BI.Def) {
760 // The interference doesn't reach the outgoing segment.
761 DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n');
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000762 SE->useIntv(BI.Def, BI.Stop);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000763 continue;
764 }
765
766
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000767 if (!BI.Uses) {
768 // No uses in block, avoid interference by reloading as late as possible.
769 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000770 SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000771 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000772 continue;
773 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000774
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000775 if (IP.second.getBoundaryIndex() < BI.LastUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000776 // There are interference-free uses at the end of the block.
777 // Find the first use that can get the live-out register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000778 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000779 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
780 IP.second.getBoundaryIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000781 assert(UI != SA->UseSlots.end() && "Couldn't find last use");
782 SlotIndex Use = *UI;
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000783 assert(Use <= BI.LastUse && "Couldn't find last use");
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000784 // Only attempt a split befroe the last split point.
785 if (Use.getBaseIndex() <= BI.LastSplitPoint) {
786 DEBUG(dbgs() << ", free use at " << Use << ".\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000787 SlotIndex SegStart = SE->enterIntvBefore(Use);
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000788 assert(SegStart >= IP.second && "Couldn't avoid interference");
789 assert(SegStart < BI.LastSplitPoint && "Impossible split point");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000790 SE->useIntv(SegStart, BI.Stop);
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000791 continue;
792 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000793 }
794
795 // Interference is after the last use.
796 DEBUG(dbgs() << " after last use.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000797 SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000798 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000799 }
800
801 // Now all defs leading to live bundles are handled, do everything else.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000802 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
803 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000804 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
805 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
806
807 // Is the register live-in?
808 if (!BI.LiveIn || !RegIn)
809 continue;
810
811 // We have an incoming register. Check for interference.
812 IndexPair &IP = InterferenceRanges[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000813
814 DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0)
815 << " -> BB#" << BI.MBB->getNumber());
816
817 // Check interference entering the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000818 if (!IP.first.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000819 // Block is interference-free.
820 DEBUG(dbgs() << ", no interference");
821 if (!BI.Uses) {
822 assert(BI.LiveThrough && "No uses, but not live through block?");
823 // Block is live-through without interference.
824 if (RegOut) {
825 DEBUG(dbgs() << ", no uses, live-through.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000826 SE->useIntv(BI.Start, BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000827 } else {
828 DEBUG(dbgs() << ", no uses, stack-out.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000829 SE->leaveIntvAtTop(*BI.MBB);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000830 }
831 continue;
832 }
833 if (!BI.LiveThrough) {
834 DEBUG(dbgs() << ", killed in block.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000835 SE->useIntv(BI.Start, SE->leaveIntvAfter(BI.Kill));
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000836 continue;
837 }
838 if (!RegOut) {
839 // Block is live-through, but exit bundle is on the stack.
840 // Spill immediately after the last use.
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000841 if (BI.LastUse < BI.LastSplitPoint) {
842 DEBUG(dbgs() << ", uses, stack-out.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000843 SE->useIntv(BI.Start, SE->leaveIntvAfter(BI.LastUse));
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000844 continue;
845 }
846 // The last use is after the last split point, it is probably an
847 // indirect jump.
848 DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point "
849 << BI.LastSplitPoint << ", stack-out.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000850 SlotIndex SegEnd = SE->leaveIntvBefore(BI.LastSplitPoint);
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000851 SE->useIntv(BI.Start, SegEnd);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000852 // Run a double interval from the split to the last use.
853 // This makes it possible to spill the complement without affecting the
854 // indirect branch.
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000855 SE->overlapIntv(SegEnd, BI.LastUse);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000856 continue;
857 }
858 // Register is live-through.
859 DEBUG(dbgs() << ", uses, live-through.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000860 SE->useIntv(BI.Start, BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000861 continue;
862 }
863
864 // Block has interference.
865 DEBUG(dbgs() << ", interference from " << IP.first);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000866
867 if (!BI.LiveThrough && IP.first >= BI.Kill) {
868 // The interference doesn't reach the outgoing segment.
869 DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n');
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000870 SE->useIntv(BI.Start, BI.Kill);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000871 continue;
872 }
873
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000874 if (!BI.Uses) {
875 // No uses in block, avoid interference by spilling as soon as possible.
876 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000877 SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000878 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000879 continue;
880 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000881 if (IP.first.getBaseIndex() > BI.FirstUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000882 // There are interference-free uses at the beginning of the block.
883 // Find the last use that can get the register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000884 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000885 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
886 IP.first.getBaseIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000887 assert(UI != SA->UseSlots.begin() && "Couldn't find first use");
888 SlotIndex Use = (--UI)->getBoundaryIndex();
889 DEBUG(dbgs() << ", free use at " << *UI << ".\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000890 SlotIndex SegEnd = SE->leaveIntvAfter(Use);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000891 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000892 SE->useIntv(BI.Start, SegEnd);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000893 continue;
894 }
895
896 // Interference is before the first use.
897 DEBUG(dbgs() << " before first use.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000898 SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000899 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000900 }
901
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000902 SE->closeIntv();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000903
904 // FIXME: Should we be more aggressive about splitting the stack region into
905 // per-block segments? The current approach allows the stack region to
906 // separate into connected components. Some components may be allocatable.
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000907 SE->finish();
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000908 ++NumGlobalSplits;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000909
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000910 if (VerifyEnabled) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000911 MF->verify(this, "After splitting live range around region");
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000912
913#ifndef NDEBUG
914 // Make sure that at least one of the new intervals can allocate to PhysReg.
915 // That was the whole point of splitting the live range.
916 bool found = false;
917 for (LiveRangeEdit::iterator I = LREdit.begin(), E = LREdit.end(); I != E;
918 ++I)
919 if (!checkUncachedInterference(**I, PhysReg)) {
920 found = true;
921 break;
922 }
923 assert(found && "No allocatable intervals after pointless splitting");
924#endif
925 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000926}
927
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000928unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
929 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000930 BitVector LiveBundles, BestBundles;
931 float BestCost = 0;
932 unsigned BestReg = 0;
933 Order.rewind();
934 while (unsigned PhysReg = Order.next()) {
935 float Cost = calcInterferenceInfo(VirtReg, PhysReg);
936 if (BestReg && Cost >= BestCost)
937 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000938
939 SpillPlacer->placeSpills(SpillConstraints, LiveBundles);
940 // No live bundles, defer to splitSingleBlocks().
941 if (!LiveBundles.any())
942 continue;
943
944 Cost += calcGlobalSplitCost(LiveBundles);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000945 if (!BestReg || Cost < BestCost) {
946 BestReg = PhysReg;
947 BestCost = Cost;
948 BestBundles.swap(LiveBundles);
949 }
950 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000951
952 if (!BestReg)
953 return 0;
954
955 splitAroundRegion(VirtReg, BestReg, BestBundles, NewVRegs);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000956 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Region);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000957 return 0;
958}
959
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000960
961//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000962// Local Splitting
963//===----------------------------------------------------------------------===//
964
965
966/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
967/// in order to use PhysReg between two entries in SA->UseSlots.
968///
969/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
970///
971void RAGreedy::calcGapWeights(unsigned PhysReg,
972 SmallVectorImpl<float> &GapWeight) {
973 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
974 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
975 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
976 const unsigned NumGaps = Uses.size()-1;
977
978 // Start and end points for the interference check.
979 SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse;
980 SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse;
981
982 GapWeight.assign(NumGaps, 0.0f);
983
984 // Add interference from each overlapping register.
985 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
986 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
987 .checkInterference())
988 continue;
989
990 // We know that VirtReg is a continuous interval from FirstUse to LastUse,
991 // so we don't need InterferenceQuery.
992 //
993 // Interference that overlaps an instruction is counted in both gaps
994 // surrounding the instruction. The exception is interference before
995 // StartIdx and after StopIdx.
996 //
997 LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
998 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
999 // Skip the gaps before IntI.
1000 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1001 if (++Gap == NumGaps)
1002 break;
1003 if (Gap == NumGaps)
1004 break;
1005
1006 // Update the gaps covered by IntI.
1007 const float weight = IntI.value()->weight;
1008 for (; Gap != NumGaps; ++Gap) {
1009 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1010 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1011 break;
1012 }
1013 if (Gap == NumGaps)
1014 break;
1015 }
1016 }
1017}
1018
1019/// getPrevMappedIndex - Return the slot index of the last non-copy instruction
1020/// before MI that has a slot index. If MI is the first mapped instruction in
1021/// its block, return the block start index instead.
1022///
1023SlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) {
1024 assert(MI && "Missing MachineInstr");
1025 const MachineBasicBlock *MBB = MI->getParent();
1026 MachineBasicBlock::const_iterator B = MBB->begin(), I = MI;
1027 while (I != B)
1028 if (!(--I)->isDebugValue() && !I->isCopy())
1029 return Indexes->getInstructionIndex(I);
1030 return Indexes->getMBBStartIdx(MBB);
1031}
1032
1033/// calcPrevSlots - Fill in the PrevSlot array with the index of the previous
1034/// real non-copy instruction for each instruction in SA->UseSlots.
1035///
1036void RAGreedy::calcPrevSlots() {
1037 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1038 PrevSlot.clear();
1039 PrevSlot.reserve(Uses.size());
1040 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
1041 const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]);
1042 PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex());
1043 }
1044}
1045
1046/// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may
1047/// be beneficial to split before UseSlots[i].
1048///
1049/// 0 is always a valid split point
1050unsigned RAGreedy::nextSplitPoint(unsigned i) {
1051 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1052 const unsigned Size = Uses.size();
1053 assert(i != Size && "No split points after the end");
1054 // Allow split before i when Uses[i] is not adjacent to the previous use.
1055 while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex())
1056 ;
1057 return i;
1058}
1059
1060/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1061/// basic block.
1062///
1063unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1064 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1065 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
1066 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
1067
1068 // Note that it is possible to have an interval that is live-in or live-out
1069 // while only covering a single block - A phi-def can use undef values from
1070 // predecessors, and the block could be a single-block loop.
1071 // We don't bother doing anything clever about such a case, we simply assume
1072 // that the interval is continuous from FirstUse to LastUse. We should make
1073 // sure that we don't do anything illegal to such an interval, though.
1074
1075 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1076 if (Uses.size() <= 2)
1077 return 0;
1078 const unsigned NumGaps = Uses.size()-1;
1079
1080 DEBUG({
1081 dbgs() << "tryLocalSplit: ";
1082 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
1083 dbgs() << ' ' << SA->UseSlots[i];
1084 dbgs() << '\n';
1085 });
1086
1087 // For every use, find the previous mapped non-copy instruction.
1088 // We use this to detect valid split points, and to estimate new interval
1089 // sizes.
1090 calcPrevSlots();
1091
1092 unsigned BestBefore = NumGaps;
1093 unsigned BestAfter = 0;
1094 float BestDiff = 0;
1095
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +00001096 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber());
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001097 SmallVector<float, 8> GapWeight;
1098
1099 Order.rewind();
1100 while (unsigned PhysReg = Order.next()) {
1101 // Keep track of the largest spill weight that would need to be evicted in
1102 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1103 calcGapWeights(PhysReg, GapWeight);
1104
1105 // Try to find the best sequence of gaps to close.
1106 // The new spill weight must be larger than any gap interference.
1107
1108 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1109 unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1;
1110
1111 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1112 // It is the spill weight that needs to be evicted.
1113 float MaxGap = GapWeight[0];
1114 for (unsigned i = 1; i != SplitAfter; ++i)
1115 MaxGap = std::max(MaxGap, GapWeight[i]);
1116
1117 for (;;) {
1118 // Live before/after split?
1119 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1120 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1121
1122 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1123 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1124 << " i=" << MaxGap);
1125
1126 // Stop before the interval gets so big we wouldn't be making progress.
1127 if (!LiveBefore && !LiveAfter) {
1128 DEBUG(dbgs() << " all\n");
1129 break;
1130 }
1131 // Should the interval be extended or shrunk?
1132 bool Shrink = true;
1133 if (MaxGap < HUGE_VALF) {
1134 // Estimate the new spill weight.
1135 //
1136 // Each instruction reads and writes the register, except the first
1137 // instr doesn't read when !FirstLive, and the last instr doesn't write
1138 // when !LastLive.
1139 //
1140 // We will be inserting copies before and after, so the total number of
1141 // reads and writes is 2 * EstUses.
1142 //
1143 const unsigned EstUses = 2*(SplitAfter - SplitBefore) +
1144 2*(LiveBefore + LiveAfter);
1145
1146 // Try to guess the size of the new interval. This should be trivial,
1147 // but the slot index of an inserted copy can be a lot smaller than the
1148 // instruction it is inserted before if there are many dead indexes
1149 // between them.
1150 //
1151 // We measure the distance from the instruction before SplitBefore to
1152 // get a conservative estimate.
1153 //
1154 // The final distance can still be different if inserting copies
1155 // triggers a slot index renumbering.
1156 //
1157 const float EstWeight = normalizeSpillWeight(blockFreq * EstUses,
1158 PrevSlot[SplitBefore].distance(Uses[SplitAfter]));
1159 // Would this split be possible to allocate?
1160 // Never allocate all gaps, we wouldn't be making progress.
1161 float Diff = EstWeight - MaxGap;
1162 DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff);
1163 if (Diff > 0) {
1164 Shrink = false;
1165 if (Diff > BestDiff) {
1166 DEBUG(dbgs() << " (best)");
1167 BestDiff = Diff;
1168 BestBefore = SplitBefore;
1169 BestAfter = SplitAfter;
1170 }
1171 }
1172 }
1173
1174 // Try to shrink.
1175 if (Shrink) {
1176 SplitBefore = nextSplitPoint(SplitBefore);
1177 if (SplitBefore < SplitAfter) {
1178 DEBUG(dbgs() << " shrink\n");
1179 // Recompute the max when necessary.
1180 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1181 MaxGap = GapWeight[SplitBefore];
1182 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1183 MaxGap = std::max(MaxGap, GapWeight[i]);
1184 }
1185 continue;
1186 }
1187 MaxGap = 0;
1188 }
1189
1190 // Try to extend the interval.
1191 if (SplitAfter >= NumGaps) {
1192 DEBUG(dbgs() << " end\n");
1193 break;
1194 }
1195
1196 DEBUG(dbgs() << " extend\n");
1197 for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1;
1198 SplitAfter != e; ++SplitAfter)
1199 MaxGap = std::max(MaxGap, GapWeight[SplitAfter]);
1200 continue;
1201 }
1202 }
1203
1204 // Didn't find any candidates?
1205 if (BestBefore == NumGaps)
1206 return 0;
1207
1208 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1209 << '-' << Uses[BestAfter] << ", " << BestDiff
1210 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1211
1212 SmallVector<LiveInterval*, 4> SpillRegs;
1213 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001214 SE->reset(LREdit);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001215
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001216 SE->openIntv();
1217 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1218 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
1219 SE->useIntv(SegStart, SegStop);
1220 SE->closeIntv();
1221 SE->finish();
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001222 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Local);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001223 ++NumLocalSplits;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001224
1225 return 0;
1226}
1227
1228//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001229// Live Range Splitting
1230//===----------------------------------------------------------------------===//
1231
1232/// trySplit - Try to split VirtReg or one of its interferences, making it
1233/// assignable.
1234/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1235unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1236 SmallVectorImpl<LiveInterval*>&NewVRegs) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001237 // Local intervals are handled separately.
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001238 if (LIS->intervalIsInOneMBB(VirtReg)) {
1239 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001240 SA->analyze(&VirtReg);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001241 return tryLocalSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001242 }
1243
1244 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001245
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001246 // Don't iterate global splitting.
1247 // Move straight to spilling if this range was produced by a global split.
1248 LiveRangeStage Stage = getStage(VirtReg);
1249 if (Stage >= RS_Block)
1250 return 0;
1251
1252 SA->analyze(&VirtReg);
1253
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001254 // First try to split around a region spanning multiple blocks.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001255 if (Stage < RS_Region) {
1256 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1257 if (PhysReg || !NewVRegs.empty())
1258 return PhysReg;
1259 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001260
1261 // Then isolate blocks with multiple uses.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001262 if (Stage < RS_Block) {
1263 SplitAnalysis::BlockPtrSet Blocks;
1264 if (SA->getMultiUseBlocks(Blocks)) {
1265 SmallVector<LiveInterval*, 4> SpillRegs;
1266 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001267 SE->reset(LREdit);
1268 SE->splitSingleBlocks(Blocks);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001269 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Block);
1270 if (VerifyEnabled)
1271 MF->verify(this, "After splitting live range around basic blocks");
1272 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001273 }
1274
1275 // Don't assign any physregs.
1276 return 0;
1277}
1278
1279
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001280//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001281// Main Entry Point
1282//===----------------------------------------------------------------------===//
1283
1284unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001285 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001286 LiveRangeStage Stage = getStage(VirtReg);
1287 if (Stage == RS_Original)
1288 LRStage[VirtReg.reg] = RS_Second;
1289
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001290 // First try assigning a free register.
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +00001291 AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs);
1292 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001293 if (!checkPhysRegInterference(VirtReg, PhysReg))
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001294 return PhysReg;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001295 }
Andrew Trickb853e6c2010-12-09 18:15:21 +00001296
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +00001297 if (unsigned PhysReg = tryReassign(VirtReg, Order, NewVRegs))
1298 return PhysReg;
1299
1300 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001301 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001302
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001303 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1304
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001305 // The first time we see a live range, don't try to split or spill.
1306 // Wait until the second time, when all smaller ranges have been allocated.
1307 // This gives a better picture of the interference to split around.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001308 if (Stage == RS_Original) {
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001309 NewVRegs.push_back(&VirtReg);
1310 return 0;
1311 }
1312
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001313 assert(Stage < RS_Spill && "Cannot allocate after spilling");
1314
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001315 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001316 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1317 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +00001318 return PhysReg;
1319
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001320 // Finally spill VirtReg itself.
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001321 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001322 SmallVector<LiveInterval*, 1> pendingSpills;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001323 spiller().spill(&VirtReg, NewVRegs, pendingSpills);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001324
1325 // The live virtual register requesting allocation was spilled, so tell
1326 // the caller not to allocate anything during this round.
1327 return 0;
1328}
1329
1330bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1331 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1332 << "********** Function: "
1333 << ((Value*)mf.getFunction())->getName() << '\n');
1334
1335 MF = &mf;
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001336 if (VerifyEnabled)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +00001337 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001338
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +00001339 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001340 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +00001341 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001342 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesenf6dff842010-12-10 22:54:44 +00001343 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001344 Loops = &getAnalysis<MachineLoopInfo>();
1345 LoopRanges = &getAnalysis<MachineLoopRanges>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001346 Bundles = &getAnalysis<EdgeBundles>();
1347 SpillPlacer = &getAnalysis<SpillPlacement>();
1348
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +00001349 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001350 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree));
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001351 LRStage.clear();
1352 LRStage.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001353
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001354 allocatePhysRegs();
1355 addMBBLiveIns(MF);
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +00001356 LIS->addKillFlags();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001357
1358 // Run rewriter
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001359 {
1360 NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +00001361 VRM->rewrite(Indexes);
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001362 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001363
1364 // The pass output is in VirtRegMap. Release all the transient data.
1365 releaseMemory();
1366
1367 return true;
1368}