blob: 201fa93cc77b9737fa0167d9fe3df71edbcb73d1 [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 Olesen8b6a9332011-03-04 22:11:11 +0000119 typedef std::pair<SlotIndex, SlotIndex> IndexPair;
120
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000121 /// For every instruction in SA->UseSlots, store the previous non-copy
122 /// instruction.
123 SmallVector<SlotIndex, 8> PrevSlot;
124
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000125public:
126 RAGreedy();
127
128 /// Return the pass name.
129 virtual const char* getPassName() const {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000130 return "Greedy Register Allocator";
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000131 }
132
133 /// RAGreedy analysis usage.
134 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000135 virtual void releaseMemory();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000136 virtual Spiller &spiller() { return *SpillerInstance; }
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000137 virtual void enqueue(LiveInterval *LI);
138 virtual LiveInterval *dequeue();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000139 virtual unsigned selectOrSplit(LiveInterval&,
140 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000141
142 /// Perform register allocation.
143 virtual bool runOnMachineFunction(MachineFunction &mf);
144
145 static char ID;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000146
147private:
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000148 bool checkUncachedInterference(LiveInterval&, unsigned);
149 LiveInterval *getSingleInterference(LiveInterval&, unsigned);
Andrew Trickb853e6c2010-12-09 18:15:21 +0000150 bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg);
Jakob Stoklund Olesen8b6a9332011-03-04 22:11:11 +0000151 void mapGlobalInterference(unsigned, SmallVectorImpl<IndexPair>&);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000152 float calcInterferenceInfo(LiveInterval&, unsigned);
153 float calcGlobalSplitCost(const BitVector&);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000154 void splitAroundRegion(LiveInterval&, unsigned, const BitVector&,
155 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000156 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
157 SlotIndex getPrevMappedIndex(const MachineInstr*);
158 void calcPrevSlots();
159 unsigned nextSplitPoint(unsigned);
Jakob Stoklund Olesend17924b2011-03-04 21:32:50 +0000160 bool canEvictInterference(LiveInterval&, unsigned, float&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000161
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000162 unsigned tryReassign(LiveInterval&, AllocationOrder&,
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000163 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000164 unsigned tryEvict(LiveInterval&, AllocationOrder&,
165 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000166 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
167 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000168 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
169 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000170 unsigned trySplit(LiveInterval&, AllocationOrder&,
171 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000172};
173} // end anonymous namespace
174
175char RAGreedy::ID = 0;
176
177FunctionPass* llvm::createGreedyRegisterAllocator() {
178 return new RAGreedy();
179}
180
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000181RAGreedy::RAGreedy(): MachineFunctionPass(ID), LRStage(RS_Original) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000182 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000183 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
184 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
185 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
186 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
187 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
188 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
189 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
190 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000191 initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000192 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000193 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
194 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000195}
196
197void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
198 AU.setPreservesCFG();
199 AU.addRequired<AliasAnalysis>();
200 AU.addPreserved<AliasAnalysis>();
201 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000202 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000203 AU.addPreserved<SlotIndexes>();
204 if (StrongPHIElim)
205 AU.addRequiredID(StrongPHIEliminationID);
206 AU.addRequiredTransitive<RegisterCoalescer>();
207 AU.addRequired<CalculateSpillWeights>();
208 AU.addRequired<LiveStacks>();
209 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +0000210 AU.addRequired<MachineDominatorTree>();
211 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000212 AU.addRequired<MachineLoopInfo>();
213 AU.addPreserved<MachineLoopInfo>();
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000214 AU.addRequired<MachineLoopRanges>();
215 AU.addPreserved<MachineLoopRanges>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000216 AU.addRequired<VirtRegMap>();
217 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000218 AU.addRequired<EdgeBundles>();
219 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000220 MachineFunctionPass::getAnalysisUsage(AU);
221}
222
223void RAGreedy::releaseMemory() {
224 SpillerInstance.reset(0);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000225 LRStage.clear();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000226 RegAllocBase::releaseMemory();
227}
228
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000229void RAGreedy::enqueue(LiveInterval *LI) {
230 // Prioritize live ranges by size, assigning larger ranges first.
231 // The queue holds (size, reg) pairs.
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000232 const unsigned Size = LI->getSize();
233 const unsigned Reg = LI->reg;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000234 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
235 "Can only enqueue virtual registers");
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000236 unsigned Prio;
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000237
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000238 LRStage.grow(Reg);
239 if (LRStage[Reg] == RS_Original)
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000240 // 1st generation ranges are handled first, long -> short.
241 Prio = (1u << 31) + Size;
242 else
243 // Repeat offenders are handled second, short -> long
244 Prio = (1u << 30) - Size;
Jakob Stoklund Olesend2a50732011-02-23 00:56:56 +0000245
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000246 // Boost ranges that have a physical register hint.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000247 const unsigned Hint = VRM->getRegAllocPref(Reg);
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000248 if (TargetRegisterInfo::isPhysicalRegister(Hint))
249 Prio |= (1u << 30);
250
251 Queue.push(std::make_pair(Prio, Reg));
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000252}
253
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000254LiveInterval *RAGreedy::dequeue() {
255 if (Queue.empty())
256 return 0;
257 LiveInterval *LI = &LIS->getInterval(Queue.top().second);
258 Queue.pop();
259 return LI;
260}
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000261
262//===----------------------------------------------------------------------===//
263// Register Reassignment
264//===----------------------------------------------------------------------===//
265
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000266// Check interference without using the cache.
267bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg,
268 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000269 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
270 LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]);
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000271 if (subQ.checkInterference())
272 return true;
273 }
274 return false;
275}
276
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000277/// getSingleInterference - Return the single interfering virtual register
278/// assigned to PhysReg. Return 0 if more than one virtual register is
279/// interfering.
280LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg,
281 unsigned PhysReg) {
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000282 // Check physreg and aliases.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000283 LiveInterval *Interference = 0;
Jakob Stoklund Olesen257c5562010-12-14 23:38:19 +0000284 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000285 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
286 if (Q.checkInterference()) {
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000287 if (Interference)
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000288 return 0;
Jakob Stoklund Olesen417df012011-02-23 00:29:55 +0000289 if (Q.collectInterferingVRegs(2) > 1)
Jakob Stoklund Olesend84de8c2010-12-14 17:47:36 +0000290 return 0;
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000291 Interference = Q.interferingVRegs().front();
292 }
293 }
294 return Interference;
295}
296
Andrew Trickb853e6c2010-12-09 18:15:21 +0000297// Attempt to reassign this virtual register to a different physical register.
298//
299// FIXME: we are not yet caching these "second-level" interferences discovered
300// in the sub-queries. These interferences can change with each call to
301// selectOrSplit. However, we could implement a "may-interfere" cache that
302// could be conservatively dirtied when we reassign or split.
303//
304// FIXME: This may result in a lot of alias queries. We could summarize alias
305// live intervals in their parent register's live union, but it's messy.
306bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg,
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000307 unsigned WantedPhysReg) {
308 assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) &&
309 "Can only reassign virtual registers");
310 assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) &&
Andrew Trickb853e6c2010-12-09 18:15:21 +0000311 "inconsistent phys reg assigment");
312
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +0000313 AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs);
314 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000315 // Don't reassign to a WantedPhysReg alias.
316 if (TRI->regsOverlap(PhysReg, WantedPhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000317 continue;
318
Jakob Stoklund Olesen6ce219e2010-12-10 20:45:04 +0000319 if (checkUncachedInterference(InterferingVReg, PhysReg))
Andrew Trickb853e6c2010-12-09 18:15:21 +0000320 continue;
321
Andrew Trickb853e6c2010-12-09 18:15:21 +0000322 // Reassign the interfering virtual reg to this physical reg.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000323 unsigned OldAssign = VRM->getPhys(InterferingVReg.reg);
324 DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " <<
325 TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n');
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000326 unassign(InterferingVReg, OldAssign);
327 assign(InterferingVReg, PhysReg);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000328 ++NumReassigned;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000329 return true;
330 }
331 return false;
332}
333
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000334/// tryReassign - Try to reassign a single interference to a different physreg.
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000335/// @param VirtReg Currently unassigned virtual register.
336/// @param Order Physregs to try.
337/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000338unsigned RAGreedy::tryReassign(LiveInterval &VirtReg, AllocationOrder &Order,
339 SmallVectorImpl<LiveInterval*> &NewVRegs){
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000340 NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000341
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000342 Order.rewind();
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000343 while (unsigned PhysReg = Order.next()) {
344 LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg);
345 if (!InterferingVReg)
346 continue;
347 if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg))
348 continue;
349 if (reassignVReg(*InterferingVReg, PhysReg))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +0000350 return PhysReg;
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000351 }
352 return 0;
353}
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000354
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000355
356//===----------------------------------------------------------------------===//
357// Interference eviction
358//===----------------------------------------------------------------------===//
359
360/// canEvict - Return true if all interferences between VirtReg and PhysReg can
361/// be evicted. Set maxWeight to the maximal spill weight of an interference.
362bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
Jakob Stoklund Olesend17924b2011-03-04 21:32:50 +0000363 float &MaxWeight) {
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000364 float Weight = 0;
365 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
366 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
367 // If there is 10 or more interferences, chances are one is smaller.
368 if (Q.collectInterferingVRegs(10) >= 10)
369 return false;
370
Jakob Stoklund Olesend17924b2011-03-04 21:32:50 +0000371 // Check if any interfering live range is heavier than VirtReg.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000372 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
373 LiveInterval *Intf = Q.interferingVRegs()[i];
374 if (TargetRegisterInfo::isPhysicalRegister(Intf->reg))
375 return false;
Jakob Stoklund Olesend17924b2011-03-04 21:32:50 +0000376 if (Intf->weight >= VirtReg.weight)
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000377 return false;
378 Weight = std::max(Weight, Intf->weight);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000379 }
380 }
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000381 MaxWeight = Weight;
382 return true;
383}
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000384
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000385/// tryEvict - Try to evict all interferences for a physreg.
386/// @param VirtReg Currently unassigned virtual register.
387/// @param Order Physregs to try.
388/// @return Physreg to assign VirtReg, or 0.
389unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
390 AllocationOrder &Order,
391 SmallVectorImpl<LiveInterval*> &NewVRegs){
392 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
393
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000394 // Keep track of the lightest single interference seen so far.
395 float BestWeight = 0;
396 unsigned BestPhys = 0;
397
398 Order.rewind();
399 while (unsigned PhysReg = Order.next()) {
400 float Weight = 0;
Jakob Stoklund Olesend17924b2011-03-04 21:32:50 +0000401 if (!canEvictInterference(VirtReg, PhysReg, Weight))
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000402 continue;
403
404 // This is an eviction candidate.
405 DEBUG(dbgs() << "max " << PrintReg(PhysReg, TRI) << " interference = "
406 << Weight << '\n');
407 if (BestPhys && Weight >= BestWeight)
408 continue;
409
410 // Best so far.
411 BestPhys = PhysReg;
412 BestWeight = Weight;
Jakob Stoklund Olesen57f1e2c2011-02-25 01:04:22 +0000413 // Stop if the hint can be used.
414 if (Order.isHint(PhysReg))
415 break;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000416 }
417
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000418 if (!BestPhys)
419 return 0;
420
421 DEBUG(dbgs() << "evicting " << PrintReg(BestPhys, TRI) << " interference\n");
422 for (const unsigned *AliasI = TRI->getOverlaps(BestPhys); *AliasI; ++AliasI) {
423 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
424 assert(Q.seenAllInterferences() && "Didn't check all interfererences.");
425 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
426 LiveInterval *Intf = Q.interferingVRegs()[i];
427 unassign(*Intf, VRM->getPhys(Intf->reg));
428 ++NumEvicted;
429 NewVRegs.push_back(Intf);
430 }
431 }
432 return BestPhys;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000433}
434
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000435
436//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000437// Region Splitting
438//===----------------------------------------------------------------------===//
439
Jakob Stoklund Olesen8b6a9332011-03-04 22:11:11 +0000440/// mapGlobalInterference - Compute a map of the interference from PhysReg and
441/// its aliases in each block in SA->LiveBlocks.
442/// If LiveBlocks[i] is live-in, Ranges[i].first is the first interference.
443/// If LiveBlocks[i] is live-out, Ranges[i].second is the last interference.
444void RAGreedy::mapGlobalInterference(unsigned PhysReg,
445 SmallVectorImpl<IndexPair> &Ranges) {
446 Ranges.assign(SA->LiveBlocks.size(), IndexPair());
447 LiveInterval &VirtReg = const_cast<LiveInterval&>(SA->getParent());
448 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
449 if (!query(VirtReg, *AI).checkInterference())
450 continue;
451 LiveIntervalUnion::SegmentIter IntI =
452 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
453 if (!IntI.valid())
454 continue;
455 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
456 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
457 IndexPair &IP = Ranges[i];
458
459 // Skip interference-free blocks.
460 if (IntI.start() >= BI.Stop)
461 continue;
462
463 // First interference in block.
464 if (BI.LiveIn) {
465 IntI.advanceTo(BI.Start);
466 if (!IntI.valid())
467 break;
468 if (IntI.start() >= BI.Stop)
469 continue;
470 if (!IP.first.isValid() || IntI.start() < IP.first)
471 IP.first = IntI.start();
472 }
473
474 // Last interference in block.
475 if (BI.LiveOut) {
476 IntI.advanceTo(BI.Stop);
477 if (!IntI.valid() || IntI.start() >= BI.Stop)
478 --IntI;
479 if (IntI.stop() <= BI.Start)
480 continue;
481 if (!IP.second.isValid() || IntI.stop() > IP.second)
482 IP.second = IntI.stop();
483 }
484 }
485 }
486}
487
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000488/// calcInterferenceInfo - Compute per-block outgoing and ingoing constraints
489/// when considering interference from PhysReg. Also compute an optimistic local
490/// cost of this interference pattern.
491///
492/// The final cost of a split is the local cost + global cost of preferences
493/// broken by SpillPlacement.
494///
495float RAGreedy::calcInterferenceInfo(LiveInterval &VirtReg, unsigned PhysReg) {
496 // Reset interference dependent info.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000497 SpillConstraints.resize(SA->LiveBlocks.size());
498 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
499 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000500 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000501 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000502 BC.Entry = (BI.Uses && BI.LiveIn) ?
503 SpillPlacement::PrefReg : SpillPlacement::DontCare;
504 BC.Exit = (BI.Uses && BI.LiveOut) ?
505 SpillPlacement::PrefReg : SpillPlacement::DontCare;
506 BI.OverlapEntry = BI.OverlapExit = false;
507 }
508
509 // Add interference info from each PhysReg alias.
510 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
511 if (!query(VirtReg, *AI).checkInterference())
512 continue;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000513 LiveIntervalUnion::SegmentIter IntI =
514 PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
515 if (!IntI.valid())
516 continue;
517
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000518 // Determine which blocks have interference live in or after the last split
519 // point.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000520 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
521 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000522 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000523
524 // Skip interference-free blocks.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000525 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000526 continue;
527
528 // Is the interference live-in?
529 if (BI.LiveIn) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000530 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000531 if (!IntI.valid())
532 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000533 if (IntI.start() <= BI.Start)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000534 BC.Entry = SpillPlacement::MustSpill;
535 }
536
537 // Is the interference overlapping the last split point?
538 if (BI.LiveOut) {
539 if (IntI.stop() < BI.LastSplitPoint)
540 IntI.advanceTo(BI.LastSplitPoint.getPrevSlot());
541 if (!IntI.valid())
542 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000543 if (IntI.start() < BI.Stop)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000544 BC.Exit = SpillPlacement::MustSpill;
545 }
546 }
547
548 // Rewind iterator and check other interferences.
549 IntI.find(VirtReg.beginIndex());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000550 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
551 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000552 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000553
554 // Skip interference-free blocks.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000555 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000556 continue;
557
558 // Handle transparent blocks with interference separately.
559 // Transparent blocks never incur any fixed cost.
560 if (BI.LiveThrough && !BI.Uses) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000561 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000562 if (!IntI.valid())
563 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000564 if (IntI.start() >= BI.Stop)
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000565 continue;
566
567 if (BC.Entry != SpillPlacement::MustSpill)
568 BC.Entry = SpillPlacement::PrefSpill;
569 if (BC.Exit != SpillPlacement::MustSpill)
570 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000571 continue;
572 }
573
574 // Now we only have blocks with uses left.
575 // Check if the interference overlaps the uses.
576 assert(BI.Uses && "Non-transparent block without any uses");
577
578 // Check interference on entry.
579 if (BI.LiveIn && BC.Entry != SpillPlacement::MustSpill) {
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000580 IntI.advanceTo(BI.Start);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000581 if (!IntI.valid())
582 break;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000583 // Not live in, but before the first use.
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000584 if (IntI.start() < BI.FirstUse) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000585 BC.Entry = SpillPlacement::PrefSpill;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000586 // If the block contains a kill from an earlier split, never split
587 // again in the same block.
588 if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Kill))
589 BC.Entry = SpillPlacement::MustSpill;
590 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000591 }
592
593 // Does interference overlap the uses in the entry segment
594 // [FirstUse;Kill)?
595 if (BI.LiveIn && !BI.OverlapEntry) {
596 IntI.advanceTo(BI.FirstUse);
597 if (!IntI.valid())
598 break;
599 // A live-through interval has no kill.
600 // Check [FirstUse;LastUse) instead.
601 if (IntI.start() < (BI.LiveThrough ? BI.LastUse : BI.Kill))
602 BI.OverlapEntry = true;
603 }
604
605 // Does interference overlap the uses in the exit segment [Def;LastUse)?
606 if (BI.LiveOut && !BI.LiveThrough && !BI.OverlapExit) {
607 IntI.advanceTo(BI.Def);
608 if (!IntI.valid())
609 break;
610 if (IntI.start() < BI.LastUse)
611 BI.OverlapExit = true;
612 }
613
614 // Check interference on exit.
615 if (BI.LiveOut && BC.Exit != SpillPlacement::MustSpill) {
616 // Check interference between LastUse and Stop.
617 if (BC.Exit != SpillPlacement::PrefSpill) {
618 IntI.advanceTo(BI.LastUse);
619 if (!IntI.valid())
620 break;
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000621 if (IntI.start() < BI.Stop) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000622 BC.Exit = SpillPlacement::PrefSpill;
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000623 // Avoid splitting twice in the same block.
624 if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Def))
625 BC.Exit = SpillPlacement::MustSpill;
626 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000627 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000628 }
629 }
630 }
631
632 // Accumulate a local cost of this interference pattern.
633 float LocalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000634 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
635 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000636 if (!BI.Uses)
637 continue;
638 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
639 unsigned Inserts = 0;
640
641 // Do we need spill code for the entry segment?
642 if (BI.LiveIn)
643 Inserts += BI.OverlapEntry || BC.Entry != SpillPlacement::PrefReg;
644
645 // For the exit segment?
646 if (BI.LiveOut)
647 Inserts += BI.OverlapExit || BC.Exit != SpillPlacement::PrefReg;
648
649 // The local cost of spill code in this block is the block frequency times
650 // the number of spill instructions inserted.
651 if (Inserts)
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +0000652 LocalCost += Inserts * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000653 }
654 DEBUG(dbgs() << "Local cost of " << PrintReg(PhysReg, TRI) << " = "
655 << LocalCost << '\n');
656 return LocalCost;
657}
658
659/// calcGlobalSplitCost - Return the global split cost of following the split
660/// pattern in LiveBundles. This cost should be added to the local cost of the
661/// interference pattern in SpillConstraints.
662///
663float RAGreedy::calcGlobalSplitCost(const BitVector &LiveBundles) {
664 float GlobalCost = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000665 for (unsigned i = 0, e = SpillConstraints.size(); i != e; ++i) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000666 SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
667 unsigned Inserts = 0;
668 // Broken entry preference?
669 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 0)] !=
670 (BC.Entry == SpillPlacement::PrefReg);
671 // Broken exit preference?
672 Inserts += LiveBundles[Bundles->getBundle(BC.Number, 1)] !=
673 (BC.Exit == SpillPlacement::PrefReg);
674 if (Inserts)
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +0000675 GlobalCost += Inserts * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000676 }
Jakob Stoklund Olesenfb698102011-03-04 18:08:26 +0000677 DEBUG({
678 dbgs() << "Global cost = " << GlobalCost << " with bundles";
679 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
680 dbgs() << " EB#" << i;
681 dbgs() << ".\n";
682 });
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000683 return GlobalCost;
684}
685
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000686/// splitAroundRegion - Split VirtReg around the region determined by
687/// LiveBundles. Make an effort to avoid interference from PhysReg.
688///
689/// The 'register' interval is going to contain as many uses as possible while
690/// avoiding interference. The 'stack' interval is the complement constructed by
691/// SplitEditor. It will contain the rest.
692///
693void RAGreedy::splitAroundRegion(LiveInterval &VirtReg, unsigned PhysReg,
694 const BitVector &LiveBundles,
695 SmallVectorImpl<LiveInterval*> &NewVRegs) {
696 DEBUG({
697 dbgs() << "Splitting around region for " << PrintReg(PhysReg, TRI)
698 << " with bundles";
699 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
700 dbgs() << " EB#" << i;
701 dbgs() << ".\n";
702 });
703
704 // First compute interference ranges in the live blocks.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000705 SmallVector<IndexPair, 8> InterferenceRanges;
Jakob Stoklund Olesen8b6a9332011-03-04 22:11:11 +0000706 mapGlobalInterference(PhysReg, InterferenceRanges);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000707
708 SmallVector<LiveInterval*, 4> SpillRegs;
709 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000710 SE->reset(LREdit);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000711
712 // Create the main cross-block interval.
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000713 SE->openIntv();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000714
715 // First add all defs that are live out of a block.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000716 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
717 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000718 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
719 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
720
721 // Should the register be live out?
722 if (!BI.LiveOut || !RegOut)
723 continue;
724
725 IndexPair &IP = InterferenceRanges[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000726 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#"
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000727 << Bundles->getBundle(BI.MBB->getNumber(), 1)
728 << " intf [" << IP.first << ';' << IP.second << ')');
729
730 // The interference interval should either be invalid or overlap MBB.
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000731 assert((!IP.first.isValid() || IP.first < BI.Stop) && "Bad interference");
732 assert((!IP.second.isValid() || IP.second > BI.Start)
733 && "Bad interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000734
735 // Check interference leaving the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000736 if (!IP.second.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000737 // Block is interference-free.
738 DEBUG(dbgs() << ", no interference");
739 if (!BI.Uses) {
740 assert(BI.LiveThrough && "No uses, but not live through block?");
741 // Block is live-through without interference.
742 DEBUG(dbgs() << ", no uses"
743 << (RegIn ? ", live-through.\n" : ", stack in.\n"));
744 if (!RegIn)
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000745 SE->enterIntvAtEnd(*BI.MBB);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000746 continue;
747 }
748 if (!BI.LiveThrough) {
749 DEBUG(dbgs() << ", not live-through.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000750 SE->useIntv(SE->enterIntvBefore(BI.Def), BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000751 continue;
752 }
753 if (!RegIn) {
754 // Block is live-through, but entry bundle is on the stack.
755 // Reload just before the first use.
756 DEBUG(dbgs() << ", not live-in, enter before first use.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000757 SE->useIntv(SE->enterIntvBefore(BI.FirstUse), BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000758 continue;
759 }
760 DEBUG(dbgs() << ", live-through.\n");
761 continue;
762 }
763
764 // Block has interference.
765 DEBUG(dbgs() << ", interference to " << IP.second);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000766
767 if (!BI.LiveThrough && IP.second <= BI.Def) {
768 // The interference doesn't reach the outgoing segment.
769 DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n');
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000770 SE->useIntv(BI.Def, BI.Stop);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000771 continue;
772 }
773
774
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000775 if (!BI.Uses) {
776 // No uses in block, avoid interference by reloading as late as possible.
777 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000778 SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000779 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000780 continue;
781 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000782
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000783 if (IP.second.getBoundaryIndex() < BI.LastUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000784 // There are interference-free uses at the end of the block.
785 // Find the first use that can get the live-out register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000786 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000787 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
788 IP.second.getBoundaryIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000789 assert(UI != SA->UseSlots.end() && "Couldn't find last use");
790 SlotIndex Use = *UI;
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000791 assert(Use <= BI.LastUse && "Couldn't find last use");
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000792 // Only attempt a split befroe the last split point.
793 if (Use.getBaseIndex() <= BI.LastSplitPoint) {
794 DEBUG(dbgs() << ", free use at " << Use << ".\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000795 SlotIndex SegStart = SE->enterIntvBefore(Use);
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000796 assert(SegStart >= IP.second && "Couldn't avoid interference");
797 assert(SegStart < BI.LastSplitPoint && "Impossible split point");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000798 SE->useIntv(SegStart, BI.Stop);
Jakob Stoklund Olesen8a2bbde2011-02-08 23:26:48 +0000799 continue;
800 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000801 }
802
803 // Interference is after the last use.
804 DEBUG(dbgs() << " after last use.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000805 SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000806 assert(SegStart >= IP.second && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000807 }
808
809 // Now all defs leading to live bundles are handled, do everything else.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000810 for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
811 SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000812 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
813 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
814
815 // Is the register live-in?
816 if (!BI.LiveIn || !RegIn)
817 continue;
818
819 // We have an incoming register. Check for interference.
820 IndexPair &IP = InterferenceRanges[i];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000821
822 DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0)
823 << " -> BB#" << BI.MBB->getNumber());
824
825 // Check interference entering the block.
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000826 if (!IP.first.isValid()) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000827 // Block is interference-free.
828 DEBUG(dbgs() << ", no interference");
829 if (!BI.Uses) {
830 assert(BI.LiveThrough && "No uses, but not live through block?");
831 // Block is live-through without interference.
832 if (RegOut) {
833 DEBUG(dbgs() << ", no uses, live-through.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000834 SE->useIntv(BI.Start, BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000835 } else {
836 DEBUG(dbgs() << ", no uses, stack-out.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000837 SE->leaveIntvAtTop(*BI.MBB);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000838 }
839 continue;
840 }
841 if (!BI.LiveThrough) {
842 DEBUG(dbgs() << ", killed in block.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000843 SE->useIntv(BI.Start, SE->leaveIntvAfter(BI.Kill));
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000844 continue;
845 }
846 if (!RegOut) {
847 // Block is live-through, but exit bundle is on the stack.
848 // Spill immediately after the last use.
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000849 if (BI.LastUse < BI.LastSplitPoint) {
850 DEBUG(dbgs() << ", uses, stack-out.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000851 SE->useIntv(BI.Start, SE->leaveIntvAfter(BI.LastUse));
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000852 continue;
853 }
854 // The last use is after the last split point, it is probably an
855 // indirect jump.
856 DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point "
857 << BI.LastSplitPoint << ", stack-out.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000858 SlotIndex SegEnd = SE->leaveIntvBefore(BI.LastSplitPoint);
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000859 SE->useIntv(BI.Start, SegEnd);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000860 // Run a double interval from the split to the last use.
861 // This makes it possible to spill the complement without affecting the
862 // indirect branch.
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000863 SE->overlapIntv(SegEnd, BI.LastUse);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000864 continue;
865 }
866 // Register is live-through.
867 DEBUG(dbgs() << ", uses, live-through.\n");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000868 SE->useIntv(BI.Start, BI.Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000869 continue;
870 }
871
872 // Block has interference.
873 DEBUG(dbgs() << ", interference from " << IP.first);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000874
875 if (!BI.LiveThrough && IP.first >= BI.Kill) {
876 // The interference doesn't reach the outgoing segment.
877 DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n');
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000878 SE->useIntv(BI.Start, BI.Kill);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000879 continue;
880 }
881
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000882 if (!BI.Uses) {
883 // No uses in block, avoid interference by spilling as soon as possible.
884 DEBUG(dbgs() << ", no uses.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000885 SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000886 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000887 continue;
888 }
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000889 if (IP.first.getBaseIndex() > BI.FirstUse) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000890 // There are interference-free uses at the beginning of the block.
891 // Find the last use that can get the register.
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000892 SmallVectorImpl<SlotIndex>::const_iterator UI =
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000893 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
894 IP.first.getBaseIndex());
Jakob Stoklund Olesenc0de9952011-01-20 17:45:23 +0000895 assert(UI != SA->UseSlots.begin() && "Couldn't find first use");
896 SlotIndex Use = (--UI)->getBoundaryIndex();
897 DEBUG(dbgs() << ", free use at " << *UI << ".\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000898 SlotIndex SegEnd = SE->leaveIntvAfter(Use);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000899 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000900 SE->useIntv(BI.Start, SegEnd);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000901 continue;
902 }
903
904 // Interference is before the first use.
905 DEBUG(dbgs() << " before first use.\n");
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000906 SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB);
Jakob Stoklund Olesende710952011-02-05 01:06:36 +0000907 assert(SegEnd <= IP.first && "Couldn't avoid interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000908 }
909
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000910 SE->closeIntv();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000911
912 // FIXME: Should we be more aggressive about splitting the stack region into
913 // per-block segments? The current approach allows the stack region to
914 // separate into connected components. Some components may be allocatable.
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000915 SE->finish();
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +0000916 ++NumGlobalSplits;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000917
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000918 if (VerifyEnabled) {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000919 MF->verify(this, "After splitting live range around region");
Jakob Stoklund Olesen9b3d24b2011-02-04 19:33:07 +0000920
921#ifndef NDEBUG
922 // Make sure that at least one of the new intervals can allocate to PhysReg.
923 // That was the whole point of splitting the live range.
924 bool found = false;
925 for (LiveRangeEdit::iterator I = LREdit.begin(), E = LREdit.end(); I != E;
926 ++I)
927 if (!checkUncachedInterference(**I, PhysReg)) {
928 found = true;
929 break;
930 }
931 assert(found && "No allocatable intervals after pointless splitting");
932#endif
933 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000934}
935
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000936unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
937 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000938 BitVector LiveBundles, BestBundles;
939 float BestCost = 0;
940 unsigned BestReg = 0;
941 Order.rewind();
942 while (unsigned PhysReg = Order.next()) {
943 float Cost = calcInterferenceInfo(VirtReg, PhysReg);
944 if (BestReg && Cost >= BestCost)
945 continue;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000946
947 SpillPlacer->placeSpills(SpillConstraints, LiveBundles);
948 // No live bundles, defer to splitSingleBlocks().
949 if (!LiveBundles.any())
950 continue;
951
952 Cost += calcGlobalSplitCost(LiveBundles);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000953 if (!BestReg || Cost < BestCost) {
954 BestReg = PhysReg;
955 BestCost = Cost;
956 BestBundles.swap(LiveBundles);
957 }
958 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000959
960 if (!BestReg)
961 return 0;
962
963 splitAroundRegion(VirtReg, BestReg, BestBundles, NewVRegs);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000964 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Region);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000965 return 0;
966}
967
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000968
969//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000970// Local Splitting
971//===----------------------------------------------------------------------===//
972
973
974/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
975/// in order to use PhysReg between two entries in SA->UseSlots.
976///
977/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
978///
979void RAGreedy::calcGapWeights(unsigned PhysReg,
980 SmallVectorImpl<float> &GapWeight) {
981 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
982 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
983 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
984 const unsigned NumGaps = Uses.size()-1;
985
986 // Start and end points for the interference check.
987 SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse;
988 SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse;
989
990 GapWeight.assign(NumGaps, 0.0f);
991
992 // Add interference from each overlapping register.
993 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
994 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
995 .checkInterference())
996 continue;
997
998 // We know that VirtReg is a continuous interval from FirstUse to LastUse,
999 // so we don't need InterferenceQuery.
1000 //
1001 // Interference that overlaps an instruction is counted in both gaps
1002 // surrounding the instruction. The exception is interference before
1003 // StartIdx and after StopIdx.
1004 //
1005 LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
1006 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1007 // Skip the gaps before IntI.
1008 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1009 if (++Gap == NumGaps)
1010 break;
1011 if (Gap == NumGaps)
1012 break;
1013
1014 // Update the gaps covered by IntI.
1015 const float weight = IntI.value()->weight;
1016 for (; Gap != NumGaps; ++Gap) {
1017 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1018 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1019 break;
1020 }
1021 if (Gap == NumGaps)
1022 break;
1023 }
1024 }
1025}
1026
1027/// getPrevMappedIndex - Return the slot index of the last non-copy instruction
1028/// before MI that has a slot index. If MI is the first mapped instruction in
1029/// its block, return the block start index instead.
1030///
1031SlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) {
1032 assert(MI && "Missing MachineInstr");
1033 const MachineBasicBlock *MBB = MI->getParent();
1034 MachineBasicBlock::const_iterator B = MBB->begin(), I = MI;
1035 while (I != B)
1036 if (!(--I)->isDebugValue() && !I->isCopy())
1037 return Indexes->getInstructionIndex(I);
1038 return Indexes->getMBBStartIdx(MBB);
1039}
1040
1041/// calcPrevSlots - Fill in the PrevSlot array with the index of the previous
1042/// real non-copy instruction for each instruction in SA->UseSlots.
1043///
1044void RAGreedy::calcPrevSlots() {
1045 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1046 PrevSlot.clear();
1047 PrevSlot.reserve(Uses.size());
1048 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
1049 const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]);
1050 PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex());
1051 }
1052}
1053
1054/// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may
1055/// be beneficial to split before UseSlots[i].
1056///
1057/// 0 is always a valid split point
1058unsigned RAGreedy::nextSplitPoint(unsigned i) {
1059 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1060 const unsigned Size = Uses.size();
1061 assert(i != Size && "No split points after the end");
1062 // Allow split before i when Uses[i] is not adjacent to the previous use.
1063 while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex())
1064 ;
1065 return i;
1066}
1067
1068/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1069/// basic block.
1070///
1071unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1072 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1073 assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
1074 const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
1075
1076 // Note that it is possible to have an interval that is live-in or live-out
1077 // while only covering a single block - A phi-def can use undef values from
1078 // predecessors, and the block could be a single-block loop.
1079 // We don't bother doing anything clever about such a case, we simply assume
1080 // that the interval is continuous from FirstUse to LastUse. We should make
1081 // sure that we don't do anything illegal to such an interval, though.
1082
1083 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1084 if (Uses.size() <= 2)
1085 return 0;
1086 const unsigned NumGaps = Uses.size()-1;
1087
1088 DEBUG({
1089 dbgs() << "tryLocalSplit: ";
1090 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
1091 dbgs() << ' ' << SA->UseSlots[i];
1092 dbgs() << '\n';
1093 });
1094
1095 // For every use, find the previous mapped non-copy instruction.
1096 // We use this to detect valid split points, and to estimate new interval
1097 // sizes.
1098 calcPrevSlots();
1099
1100 unsigned BestBefore = NumGaps;
1101 unsigned BestAfter = 0;
1102 float BestDiff = 0;
1103
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +00001104 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber());
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001105 SmallVector<float, 8> GapWeight;
1106
1107 Order.rewind();
1108 while (unsigned PhysReg = Order.next()) {
1109 // Keep track of the largest spill weight that would need to be evicted in
1110 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1111 calcGapWeights(PhysReg, GapWeight);
1112
1113 // Try to find the best sequence of gaps to close.
1114 // The new spill weight must be larger than any gap interference.
1115
1116 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1117 unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1;
1118
1119 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1120 // It is the spill weight that needs to be evicted.
1121 float MaxGap = GapWeight[0];
1122 for (unsigned i = 1; i != SplitAfter; ++i)
1123 MaxGap = std::max(MaxGap, GapWeight[i]);
1124
1125 for (;;) {
1126 // Live before/after split?
1127 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1128 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1129
1130 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1131 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1132 << " i=" << MaxGap);
1133
1134 // Stop before the interval gets so big we wouldn't be making progress.
1135 if (!LiveBefore && !LiveAfter) {
1136 DEBUG(dbgs() << " all\n");
1137 break;
1138 }
1139 // Should the interval be extended or shrunk?
1140 bool Shrink = true;
1141 if (MaxGap < HUGE_VALF) {
1142 // Estimate the new spill weight.
1143 //
1144 // Each instruction reads and writes the register, except the first
1145 // instr doesn't read when !FirstLive, and the last instr doesn't write
1146 // when !LastLive.
1147 //
1148 // We will be inserting copies before and after, so the total number of
1149 // reads and writes is 2 * EstUses.
1150 //
1151 const unsigned EstUses = 2*(SplitAfter - SplitBefore) +
1152 2*(LiveBefore + LiveAfter);
1153
1154 // Try to guess the size of the new interval. This should be trivial,
1155 // but the slot index of an inserted copy can be a lot smaller than the
1156 // instruction it is inserted before if there are many dead indexes
1157 // between them.
1158 //
1159 // We measure the distance from the instruction before SplitBefore to
1160 // get a conservative estimate.
1161 //
1162 // The final distance can still be different if inserting copies
1163 // triggers a slot index renumbering.
1164 //
1165 const float EstWeight = normalizeSpillWeight(blockFreq * EstUses,
1166 PrevSlot[SplitBefore].distance(Uses[SplitAfter]));
1167 // Would this split be possible to allocate?
1168 // Never allocate all gaps, we wouldn't be making progress.
1169 float Diff = EstWeight - MaxGap;
1170 DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff);
1171 if (Diff > 0) {
1172 Shrink = false;
1173 if (Diff > BestDiff) {
1174 DEBUG(dbgs() << " (best)");
1175 BestDiff = Diff;
1176 BestBefore = SplitBefore;
1177 BestAfter = SplitAfter;
1178 }
1179 }
1180 }
1181
1182 // Try to shrink.
1183 if (Shrink) {
1184 SplitBefore = nextSplitPoint(SplitBefore);
1185 if (SplitBefore < SplitAfter) {
1186 DEBUG(dbgs() << " shrink\n");
1187 // Recompute the max when necessary.
1188 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1189 MaxGap = GapWeight[SplitBefore];
1190 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1191 MaxGap = std::max(MaxGap, GapWeight[i]);
1192 }
1193 continue;
1194 }
1195 MaxGap = 0;
1196 }
1197
1198 // Try to extend the interval.
1199 if (SplitAfter >= NumGaps) {
1200 DEBUG(dbgs() << " end\n");
1201 break;
1202 }
1203
1204 DEBUG(dbgs() << " extend\n");
1205 for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1;
1206 SplitAfter != e; ++SplitAfter)
1207 MaxGap = std::max(MaxGap, GapWeight[SplitAfter]);
1208 continue;
1209 }
1210 }
1211
1212 // Didn't find any candidates?
1213 if (BestBefore == NumGaps)
1214 return 0;
1215
1216 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1217 << '-' << Uses[BestAfter] << ", " << BestDiff
1218 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1219
1220 SmallVector<LiveInterval*, 4> SpillRegs;
1221 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001222 SE->reset(LREdit);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001223
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001224 SE->openIntv();
1225 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1226 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
1227 SE->useIntv(SegStart, SegStop);
1228 SE->closeIntv();
1229 SE->finish();
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001230 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Local);
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001231 ++NumLocalSplits;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001232
1233 return 0;
1234}
1235
1236//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001237// Live Range Splitting
1238//===----------------------------------------------------------------------===//
1239
1240/// trySplit - Try to split VirtReg or one of its interferences, making it
1241/// assignable.
1242/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1243unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1244 SmallVectorImpl<LiveInterval*>&NewVRegs) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001245 // Local intervals are handled separately.
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001246 if (LIS->intervalIsInOneMBB(VirtReg)) {
1247 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001248 SA->analyze(&VirtReg);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001249 return tryLocalSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001250 }
1251
1252 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001253
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001254 // Don't iterate global splitting.
1255 // Move straight to spilling if this range was produced by a global split.
1256 LiveRangeStage Stage = getStage(VirtReg);
1257 if (Stage >= RS_Block)
1258 return 0;
1259
1260 SA->analyze(&VirtReg);
1261
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001262 // First try to split around a region spanning multiple blocks.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001263 if (Stage < RS_Region) {
1264 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1265 if (PhysReg || !NewVRegs.empty())
1266 return PhysReg;
1267 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001268
1269 // Then isolate blocks with multiple uses.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001270 if (Stage < RS_Block) {
1271 SplitAnalysis::BlockPtrSet Blocks;
1272 if (SA->getMultiUseBlocks(Blocks)) {
1273 SmallVector<LiveInterval*, 4> SpillRegs;
1274 LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001275 SE->reset(LREdit);
1276 SE->splitSingleBlocks(Blocks);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001277 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Block);
1278 if (VerifyEnabled)
1279 MF->verify(this, "After splitting live range around basic blocks");
1280 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001281 }
1282
1283 // Don't assign any physregs.
1284 return 0;
1285}
1286
1287
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001288//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001289// Main Entry Point
1290//===----------------------------------------------------------------------===//
1291
1292unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001293 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001294 LiveRangeStage Stage = getStage(VirtReg);
1295 if (Stage == RS_Original)
1296 LRStage[VirtReg.reg] = RS_Second;
1297
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001298 // First try assigning a free register.
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +00001299 AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs);
1300 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001301 if (!checkPhysRegInterference(VirtReg, PhysReg))
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001302 return PhysReg;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001303 }
Andrew Trickb853e6c2010-12-09 18:15:21 +00001304
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +00001305 if (unsigned PhysReg = tryReassign(VirtReg, Order, NewVRegs))
1306 return PhysReg;
1307
1308 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001309 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001310
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001311 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1312
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001313 // The first time we see a live range, don't try to split or spill.
1314 // Wait until the second time, when all smaller ranges have been allocated.
1315 // This gives a better picture of the interference to split around.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001316 if (Stage == RS_Original) {
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001317 NewVRegs.push_back(&VirtReg);
1318 return 0;
1319 }
1320
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001321 assert(Stage < RS_Spill && "Cannot allocate after spilling");
1322
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001323 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001324 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1325 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +00001326 return PhysReg;
1327
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001328 // Finally spill VirtReg itself.
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001329 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001330 SmallVector<LiveInterval*, 1> pendingSpills;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001331 spiller().spill(&VirtReg, NewVRegs, pendingSpills);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001332
1333 // The live virtual register requesting allocation was spilled, so tell
1334 // the caller not to allocate anything during this round.
1335 return 0;
1336}
1337
1338bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1339 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1340 << "********** Function: "
1341 << ((Value*)mf.getFunction())->getName() << '\n');
1342
1343 MF = &mf;
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001344 if (VerifyEnabled)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +00001345 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001346
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +00001347 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001348 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +00001349 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001350 ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesenf6dff842010-12-10 22:54:44 +00001351 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001352 Loops = &getAnalysis<MachineLoopInfo>();
1353 LoopRanges = &getAnalysis<MachineLoopRanges>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001354 Bundles = &getAnalysis<EdgeBundles>();
1355 SpillPlacer = &getAnalysis<SpillPlacement>();
1356
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +00001357 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001358 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree));
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001359 LRStage.clear();
1360 LRStage.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001361
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001362 allocatePhysRegs();
1363 addMBBLiveIns(MF);
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +00001364 LIS->addKillFlags();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001365
1366 // Run rewriter
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001367 {
1368 NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +00001369 VRM->rewrite(Indexes);
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001370 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001371
1372 // The pass output is in VirtRegMap. Release all the transient data.
1373 releaseMemory();
1374
1375 return true;
1376}