f08041cd6e7eca33d29cf70648b764ca9d950b82
[oota-llvm.git] / lib / CodeGen / RegAllocGreedy.cpp
1 //===-- RegAllocGreedy.cpp - greedy register allocator --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the RAGreedy function pass for register allocation in
11 // optimized builds.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "regalloc"
16 #include "AllocationOrder.h"
17 #include "LiveIntervalUnion.h"
18 #include "LiveRangeEdit.h"
19 #include "RegAllocBase.h"
20 #include "Spiller.h"
21 #include "SpillPlacement.h"
22 #include "SplitKit.h"
23 #include "VirtRegMap.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AliasAnalysis.h"
26 #include "llvm/Function.h"
27 #include "llvm/PassAnalysisSupport.h"
28 #include "llvm/CodeGen/CalcSpillWeights.h"
29 #include "llvm/CodeGen/EdgeBundles.h"
30 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
31 #include "llvm/CodeGen/LiveStackAnalysis.h"
32 #include "llvm/CodeGen/MachineDominators.h"
33 #include "llvm/CodeGen/MachineFunctionPass.h"
34 #include "llvm/CodeGen/MachineLoopInfo.h"
35 #include "llvm/CodeGen/MachineLoopRanges.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/Passes.h"
38 #include "llvm/CodeGen/RegAllocRegistry.h"
39 #include "llvm/CodeGen/RegisterCoalescer.h"
40 #include "llvm/Target/TargetOptions.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/Timer.h"
45
46 #include <queue>
47
48 using namespace llvm;
49
50 STATISTIC(NumGlobalSplits, "Number of split global live ranges");
51 STATISTIC(NumLocalSplits,  "Number of split local live ranges");
52 STATISTIC(NumReassigned,   "Number of interferences reassigned");
53 STATISTIC(NumEvicted,      "Number of interferences evicted");
54
55 static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
56                                        createGreedyRegisterAllocator);
57
58 namespace {
59 class RAGreedy : public MachineFunctionPass, public RegAllocBase {
60   // context
61   MachineFunction *MF;
62   BitVector ReservedRegs;
63
64   // analyses
65   SlotIndexes *Indexes;
66   LiveStacks *LS;
67   MachineDominatorTree *DomTree;
68   MachineLoopInfo *Loops;
69   MachineLoopRanges *LoopRanges;
70   EdgeBundles *Bundles;
71   SpillPlacement *SpillPlacer;
72
73   // state
74   std::auto_ptr<Spiller> SpillerInstance;
75   std::priority_queue<std::pair<unsigned, unsigned> > Queue;
76
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   }
111
112   // splitting state.
113   std::auto_ptr<SplitAnalysis> SA;
114
115   /// All basic blocks where the current register is live.
116   SmallVector<SpillPlacement::BlockConstraint, 8> SpillConstraints;
117
118   /// For every instruction in SA->UseSlots, store the previous non-copy
119   /// instruction.
120   SmallVector<SlotIndex, 8> PrevSlot;
121
122 public:
123   RAGreedy();
124
125   /// Return the pass name.
126   virtual const char* getPassName() const {
127     return "Greedy Register Allocator";
128   }
129
130   /// RAGreedy analysis usage.
131   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
132   virtual void releaseMemory();
133   virtual Spiller &spiller() { return *SpillerInstance; }
134   virtual void enqueue(LiveInterval *LI);
135   virtual LiveInterval *dequeue();
136   virtual unsigned selectOrSplit(LiveInterval&,
137                                  SmallVectorImpl<LiveInterval*>&);
138
139   /// Perform register allocation.
140   virtual bool runOnMachineFunction(MachineFunction &mf);
141
142   static char ID;
143
144 private:
145   bool checkUncachedInterference(LiveInterval&, unsigned);
146   LiveInterval *getSingleInterference(LiveInterval&, unsigned);
147   bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg);
148   float calcInterferenceInfo(LiveInterval&, unsigned);
149   float calcGlobalSplitCost(const BitVector&);
150   void splitAroundRegion(LiveInterval&, unsigned, const BitVector&,
151                          SmallVectorImpl<LiveInterval*>&);
152   void calcGapWeights(unsigned, SmallVectorImpl<float>&);
153   SlotIndex getPrevMappedIndex(const MachineInstr*);
154   void calcPrevSlots();
155   unsigned nextSplitPoint(unsigned);
156   bool canEvictInterference(LiveInterval&, unsigned, unsigned, float&);
157
158   unsigned tryReassign(LiveInterval&, AllocationOrder&,
159                               SmallVectorImpl<LiveInterval*>&);
160   unsigned tryEvict(LiveInterval&, AllocationOrder&,
161                     SmallVectorImpl<LiveInterval*>&);
162   unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
163                           SmallVectorImpl<LiveInterval*>&);
164   unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
165     SmallVectorImpl<LiveInterval*>&);
166   unsigned trySplit(LiveInterval&, AllocationOrder&,
167                     SmallVectorImpl<LiveInterval*>&);
168 };
169 } // end anonymous namespace
170
171 char RAGreedy::ID = 0;
172
173 FunctionPass* llvm::createGreedyRegisterAllocator() {
174   return new RAGreedy();
175 }
176
177 RAGreedy::RAGreedy(): MachineFunctionPass(ID), LRStage(RS_Original) {
178   initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
179   initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
180   initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
181   initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
182   initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
183   initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
184   initializeLiveStacksPass(*PassRegistry::getPassRegistry());
185   initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
186   initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
187   initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry());
188   initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
189   initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
190   initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
191 }
192
193 void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
194   AU.setPreservesCFG();
195   AU.addRequired<AliasAnalysis>();
196   AU.addPreserved<AliasAnalysis>();
197   AU.addRequired<LiveIntervals>();
198   AU.addRequired<SlotIndexes>();
199   AU.addPreserved<SlotIndexes>();
200   if (StrongPHIElim)
201     AU.addRequiredID(StrongPHIEliminationID);
202   AU.addRequiredTransitive<RegisterCoalescer>();
203   AU.addRequired<CalculateSpillWeights>();
204   AU.addRequired<LiveStacks>();
205   AU.addPreserved<LiveStacks>();
206   AU.addRequired<MachineDominatorTree>();
207   AU.addPreserved<MachineDominatorTree>();
208   AU.addRequired<MachineLoopInfo>();
209   AU.addPreserved<MachineLoopInfo>();
210   AU.addRequired<MachineLoopRanges>();
211   AU.addPreserved<MachineLoopRanges>();
212   AU.addRequired<VirtRegMap>();
213   AU.addPreserved<VirtRegMap>();
214   AU.addRequired<EdgeBundles>();
215   AU.addRequired<SpillPlacement>();
216   MachineFunctionPass::getAnalysisUsage(AU);
217 }
218
219 void RAGreedy::releaseMemory() {
220   SpillerInstance.reset(0);
221   LRStage.clear();
222   RegAllocBase::releaseMemory();
223 }
224
225 void RAGreedy::enqueue(LiveInterval *LI) {
226   // Prioritize live ranges by size, assigning larger ranges first.
227   // The queue holds (size, reg) pairs.
228   const unsigned Size = LI->getSize();
229   const unsigned Reg = LI->reg;
230   assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
231          "Can only enqueue virtual registers");
232   unsigned Prio;
233
234   LRStage.grow(Reg);
235   if (LRStage[Reg] == RS_Original)
236     // 1st generation ranges are handled first, long -> short.
237     Prio = (1u << 31) + Size;
238   else
239     // Repeat offenders are handled second, short -> long
240     Prio = (1u << 30) - Size;
241
242   // Boost ranges that have a physical register hint.
243   const unsigned Hint = VRM->getRegAllocPref(Reg);
244   if (TargetRegisterInfo::isPhysicalRegister(Hint))
245     Prio |= (1u << 30);
246
247   Queue.push(std::make_pair(Prio, Reg));
248 }
249
250 LiveInterval *RAGreedy::dequeue() {
251   if (Queue.empty())
252     return 0;
253   LiveInterval *LI = &LIS->getInterval(Queue.top().second);
254   Queue.pop();
255   return LI;
256 }
257
258 //===----------------------------------------------------------------------===//
259 //                         Register Reassignment
260 //===----------------------------------------------------------------------===//
261
262 // Check interference without using the cache.
263 bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg,
264                                          unsigned PhysReg) {
265   for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
266     LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]);
267     if (subQ.checkInterference())
268       return true;
269   }
270   return false;
271 }
272
273 /// getSingleInterference - Return the single interfering virtual register
274 /// assigned to PhysReg. Return 0 if more than one virtual register is
275 /// interfering.
276 LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg,
277                                               unsigned PhysReg) {
278   // Check physreg and aliases.
279   LiveInterval *Interference = 0;
280   for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
281     LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
282     if (Q.checkInterference()) {
283       if (Interference)
284         return 0;
285       if (Q.collectInterferingVRegs(2) > 1)
286         return 0;
287       Interference = Q.interferingVRegs().front();
288     }
289   }
290   return Interference;
291 }
292
293 // Attempt to reassign this virtual register to a different physical register.
294 //
295 // FIXME: we are not yet caching these "second-level" interferences discovered
296 // in the sub-queries. These interferences can change with each call to
297 // selectOrSplit. However, we could implement a "may-interfere" cache that
298 // could be conservatively dirtied when we reassign or split.
299 //
300 // FIXME: This may result in a lot of alias queries. We could summarize alias
301 // live intervals in their parent register's live union, but it's messy.
302 bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg,
303                             unsigned WantedPhysReg) {
304   assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) &&
305          "Can only reassign virtual registers");
306   assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) &&
307          "inconsistent phys reg assigment");
308
309   AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs);
310   while (unsigned PhysReg = Order.next()) {
311     // Don't reassign to a WantedPhysReg alias.
312     if (TRI->regsOverlap(PhysReg, WantedPhysReg))
313       continue;
314
315     if (checkUncachedInterference(InterferingVReg, PhysReg))
316       continue;
317
318     // Reassign the interfering virtual reg to this physical reg.
319     unsigned OldAssign = VRM->getPhys(InterferingVReg.reg);
320     DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " <<
321           TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n');
322     unassign(InterferingVReg, OldAssign);
323     assign(InterferingVReg, PhysReg);
324     ++NumReassigned;
325     return true;
326   }
327   return false;
328 }
329
330 /// tryReassign - Try to reassign a single interference to a different physreg.
331 /// @param  VirtReg Currently unassigned virtual register.
332 /// @param  Order   Physregs to try.
333 /// @return         Physreg to assign VirtReg, or 0.
334 unsigned RAGreedy::tryReassign(LiveInterval &VirtReg, AllocationOrder &Order,
335                                SmallVectorImpl<LiveInterval*> &NewVRegs){
336   NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled);
337
338   Order.rewind();
339   while (unsigned PhysReg = Order.next()) {
340     LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg);
341     if (!InterferingVReg)
342       continue;
343     if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg))
344       continue;
345     if (reassignVReg(*InterferingVReg, PhysReg))
346       return PhysReg;
347   }
348   return 0;
349 }
350
351
352 //===----------------------------------------------------------------------===//
353 //                         Interference eviction
354 //===----------------------------------------------------------------------===//
355
356 /// canEvict - Return true if all interferences between VirtReg and PhysReg can
357 /// be evicted. Set maxWeight to the maximal spill weight of an interference.
358 bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
359                                     unsigned Size, float &MaxWeight) {
360   float Weight = 0;
361   for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
362     LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
363     // If there is 10 or more interferences, chances are one is smaller.
364     if (Q.collectInterferingVRegs(10) >= 10)
365       return false;
366
367     // CHeck if any interfering live range is shorter than VirtReg.
368     for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
369       LiveInterval *Intf = Q.interferingVRegs()[i];
370       if (TargetRegisterInfo::isPhysicalRegister(Intf->reg))
371         return false;
372       if (Intf->getSize() <= Size)
373         return false;
374       Weight = std::max(Weight, Intf->weight);
375     }
376   }
377   MaxWeight = Weight;
378   return true;
379 }
380
381 /// tryEvict - Try to evict all interferences for a physreg.
382 /// @param  VirtReg Currently unassigned virtual register.
383 /// @param  Order   Physregs to try.
384 /// @return         Physreg to assign VirtReg, or 0.
385 unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
386                             AllocationOrder &Order,
387                             SmallVectorImpl<LiveInterval*> &NewVRegs){
388   NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
389
390   // We can only evict interference if all interfering registers are virtual and
391   // longer than VirtReg.
392   const unsigned Size = VirtReg.getSize();
393
394   // 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;
401     if (!canEvictInterference(VirtReg, PhysReg, Size, Weight))
402       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;
413     // Stop if the hint can be used.
414     if (Order.isHint(PhysReg))
415       break;
416   }
417
418   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;
433 }
434
435
436 //===----------------------------------------------------------------------===//
437 //                              Region Splitting
438 //===----------------------------------------------------------------------===//
439
440 /// calcInterferenceInfo - Compute per-block outgoing and ingoing constraints
441 /// when considering interference from PhysReg. Also compute an optimistic local
442 /// cost of this interference pattern.
443 ///
444 /// The final cost of a split is the local cost + global cost of preferences
445 /// broken by SpillPlacement.
446 ///
447 float RAGreedy::calcInterferenceInfo(LiveInterval &VirtReg, unsigned PhysReg) {
448   // Reset interference dependent info.
449   SpillConstraints.resize(SA->LiveBlocks.size());
450   for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
451     SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
452     SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
453     BC.Number = BI.MBB->getNumber();
454     BC.Entry = (BI.Uses && BI.LiveIn) ?
455       SpillPlacement::PrefReg : SpillPlacement::DontCare;
456     BC.Exit = (BI.Uses && BI.LiveOut) ?
457       SpillPlacement::PrefReg : SpillPlacement::DontCare;
458     BI.OverlapEntry = BI.OverlapExit = false;
459   }
460
461   // Add interference info from each PhysReg alias.
462   for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
463     if (!query(VirtReg, *AI).checkInterference())
464       continue;
465     LiveIntervalUnion::SegmentIter IntI =
466       PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
467     if (!IntI.valid())
468       continue;
469
470     // Determine which blocks have interference live in or after the last split
471     // point.
472     for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
473       SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
474       SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
475       SlotIndex Start, Stop;
476       tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
477
478       // Skip interference-free blocks.
479       if (IntI.start() >= Stop)
480         continue;
481
482       // Is the interference live-in?
483       if (BI.LiveIn) {
484         IntI.advanceTo(Start);
485         if (!IntI.valid())
486           break;
487         if (IntI.start() <= Start)
488           BC.Entry = SpillPlacement::MustSpill;
489       }
490
491       // Is the interference overlapping the last split point?
492       if (BI.LiveOut) {
493         if (IntI.stop() < BI.LastSplitPoint)
494           IntI.advanceTo(BI.LastSplitPoint.getPrevSlot());
495         if (!IntI.valid())
496           break;
497         if (IntI.start() < Stop)
498           BC.Exit = SpillPlacement::MustSpill;
499       }
500     }
501
502     // Rewind iterator and check other interferences.
503     IntI.find(VirtReg.beginIndex());
504     for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
505       SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
506       SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
507       SlotIndex Start, Stop;
508       tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
509
510       // Skip interference-free blocks.
511       if (IntI.start() >= Stop)
512         continue;
513
514       // Handle transparent blocks with interference separately.
515       // Transparent blocks never incur any fixed cost.
516       if (BI.LiveThrough && !BI.Uses) {
517         IntI.advanceTo(Start);
518         if (!IntI.valid())
519           break;
520         if (IntI.start() >= Stop)
521           continue;
522
523         if (BC.Entry != SpillPlacement::MustSpill)
524           BC.Entry = SpillPlacement::PrefSpill;
525         if (BC.Exit != SpillPlacement::MustSpill)
526           BC.Exit = SpillPlacement::PrefSpill;
527         continue;
528       }
529
530       // Now we only have blocks with uses left.
531       // Check if the interference overlaps the uses.
532       assert(BI.Uses && "Non-transparent block without any uses");
533
534       // Check interference on entry.
535       if (BI.LiveIn && BC.Entry != SpillPlacement::MustSpill) {
536         IntI.advanceTo(Start);
537         if (!IntI.valid())
538           break;
539         // Not live in, but before the first use.
540         if (IntI.start() < BI.FirstUse) {
541           BC.Entry = SpillPlacement::PrefSpill;
542           // If the block contains a kill from an earlier split, never split
543           // again in the same block.
544           if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Kill))
545             BC.Entry = SpillPlacement::MustSpill;
546         }
547       }
548
549       // Does interference overlap the uses in the entry segment
550       // [FirstUse;Kill)?
551       if (BI.LiveIn && !BI.OverlapEntry) {
552         IntI.advanceTo(BI.FirstUse);
553         if (!IntI.valid())
554           break;
555         // A live-through interval has no kill.
556         // Check [FirstUse;LastUse) instead.
557         if (IntI.start() < (BI.LiveThrough ? BI.LastUse : BI.Kill))
558           BI.OverlapEntry = true;
559       }
560
561       // Does interference overlap the uses in the exit segment [Def;LastUse)?
562       if (BI.LiveOut && !BI.LiveThrough && !BI.OverlapExit) {
563         IntI.advanceTo(BI.Def);
564         if (!IntI.valid())
565           break;
566         if (IntI.start() < BI.LastUse)
567           BI.OverlapExit = true;
568       }
569
570       // Check interference on exit.
571       if (BI.LiveOut && BC.Exit != SpillPlacement::MustSpill) {
572         // Check interference between LastUse and Stop.
573         if (BC.Exit != SpillPlacement::PrefSpill) {
574           IntI.advanceTo(BI.LastUse);
575           if (!IntI.valid())
576             break;
577           if (IntI.start() < Stop) {
578             BC.Exit = SpillPlacement::PrefSpill;
579             // Avoid splitting twice in the same block.
580             if (!BI.LiveThrough && !SA->isOriginalEndpoint(BI.Def))
581               BC.Exit = SpillPlacement::MustSpill;
582           }
583         }
584       }
585     }
586   }
587
588   // Accumulate a local cost of this interference pattern.
589   float LocalCost = 0;
590   for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
591     SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
592     if (!BI.Uses)
593       continue;
594     SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
595     unsigned Inserts = 0;
596
597     // Do we need spill code for the entry segment?
598     if (BI.LiveIn)
599       Inserts += BI.OverlapEntry || BC.Entry != SpillPlacement::PrefReg;
600
601     // For the exit segment?
602     if (BI.LiveOut)
603       Inserts += BI.OverlapExit || BC.Exit != SpillPlacement::PrefReg;
604
605     // The local cost of spill code in this block is the block frequency times
606     // the number of spill instructions inserted.
607     if (Inserts)
608       LocalCost += Inserts * SpillPlacer->getBlockFrequency(BI.MBB);
609   }
610   DEBUG(dbgs() << "Local cost of " << PrintReg(PhysReg, TRI) << " = "
611                << LocalCost << '\n');
612   return LocalCost;
613 }
614
615 /// calcGlobalSplitCost - Return the global split cost of following the split
616 /// pattern in LiveBundles. This cost should be added to the local cost of the
617 /// interference pattern in SpillConstraints.
618 ///
619 float RAGreedy::calcGlobalSplitCost(const BitVector &LiveBundles) {
620   float GlobalCost = 0;
621   for (unsigned i = 0, e = SpillConstraints.size(); i != e; ++i) {
622     SpillPlacement::BlockConstraint &BC = SpillConstraints[i];
623     unsigned Inserts = 0;
624     // Broken entry preference?
625     Inserts += LiveBundles[Bundles->getBundle(BC.Number, 0)] !=
626                  (BC.Entry == SpillPlacement::PrefReg);
627     // Broken exit preference?
628     Inserts += LiveBundles[Bundles->getBundle(BC.Number, 1)] !=
629                  (BC.Exit == SpillPlacement::PrefReg);
630     if (Inserts)
631       GlobalCost +=
632         Inserts * SpillPlacer->getBlockFrequency(SA->LiveBlocks[i].MBB);
633   }
634   DEBUG(dbgs() << "Global cost = " << GlobalCost << '\n');
635   return GlobalCost;
636 }
637
638 /// splitAroundRegion - Split VirtReg around the region determined by
639 /// LiveBundles. Make an effort to avoid interference from PhysReg.
640 ///
641 /// The 'register' interval is going to contain as many uses as possible while
642 /// avoiding interference. The 'stack' interval is the complement constructed by
643 /// SplitEditor. It will contain the rest.
644 ///
645 void RAGreedy::splitAroundRegion(LiveInterval &VirtReg, unsigned PhysReg,
646                                  const BitVector &LiveBundles,
647                                  SmallVectorImpl<LiveInterval*> &NewVRegs) {
648   DEBUG({
649     dbgs() << "Splitting around region for " << PrintReg(PhysReg, TRI)
650            << " with bundles";
651     for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
652       dbgs() << " EB#" << i;
653     dbgs() << ".\n";
654   });
655
656   // First compute interference ranges in the live blocks.
657   typedef std::pair<SlotIndex, SlotIndex> IndexPair;
658   SmallVector<IndexPair, 8> InterferenceRanges;
659   InterferenceRanges.resize(SA->LiveBlocks.size());
660   for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
661     if (!query(VirtReg, *AI).checkInterference())
662       continue;
663     LiveIntervalUnion::SegmentIter IntI =
664       PhysReg2LiveUnion[*AI].find(VirtReg.beginIndex());
665     if (!IntI.valid())
666       continue;
667     for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
668       const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
669       IndexPair &IP = InterferenceRanges[i];
670       SlotIndex Start, Stop;
671       tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
672       // Skip interference-free blocks.
673       if (IntI.start() >= Stop)
674         continue;
675
676       // First interference in block.
677       if (BI.LiveIn) {
678         IntI.advanceTo(Start);
679         if (!IntI.valid())
680           break;
681         if (IntI.start() >= Stop)
682           continue;
683         if (!IP.first.isValid() || IntI.start() < IP.first)
684           IP.first = IntI.start();
685       }
686
687       // Last interference in block.
688       if (BI.LiveOut) {
689         IntI.advanceTo(Stop);
690         if (!IntI.valid() || IntI.start() >= Stop)
691           --IntI;
692         if (IntI.stop() <= Start)
693           continue;
694         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);
702   SplitEditor SE(*SA, *LIS, *VRM, *DomTree, LREdit);
703
704   // Create the main cross-block interval.
705   SE.openIntv();
706
707   // First add all defs that are live out of a block.
708   for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
709     SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
710     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];
718     SlotIndex Start, Stop;
719     tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
720
721     DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#"
722                  << Bundles->getBundle(BI.MBB->getNumber(), 1)
723                  << " intf [" << IP.first << ';' << IP.second << ')');
724
725     // The interference interval should either be invalid or overlap MBB.
726     assert((!IP.first.isValid() || IP.first < Stop) && "Bad interference");
727     assert((!IP.second.isValid() || IP.second > Start) && "Bad interference");
728
729     // Check interference leaving the block.
730     if (!IP.second.isValid()) {
731       // Block is interference-free.
732       DEBUG(dbgs() << ", no interference");
733       if (!BI.Uses) {
734         assert(BI.LiveThrough && "No uses, but not live through block?");
735         // Block is live-through without interference.
736         DEBUG(dbgs() << ", no uses"
737                      << (RegIn ? ", live-through.\n" : ", stack in.\n"));
738         if (!RegIn)
739           SE.enterIntvAtEnd(*BI.MBB);
740         continue;
741       }
742       if (!BI.LiveThrough) {
743         DEBUG(dbgs() << ", not live-through.\n");
744         SE.useIntv(SE.enterIntvBefore(BI.Def), Stop);
745         continue;
746       }
747       if (!RegIn) {
748         // Block is live-through, but entry bundle is on the stack.
749         // Reload just before the first use.
750         DEBUG(dbgs() << ", not live-in, enter before first use.\n");
751         SE.useIntv(SE.enterIntvBefore(BI.FirstUse), Stop);
752         continue;
753       }
754       DEBUG(dbgs() << ", live-through.\n");
755       continue;
756     }
757
758     // Block has interference.
759     DEBUG(dbgs() << ", interference to " << IP.second);
760
761     if (!BI.LiveThrough && IP.second <= BI.Def) {
762       // The interference doesn't reach the outgoing segment.
763       DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n');
764       SE.useIntv(BI.Def, Stop);
765       continue;
766     }
767
768
769     if (!BI.Uses) {
770       // No uses in block, avoid interference by reloading as late as possible.
771       DEBUG(dbgs() << ", no uses.\n");
772       SlotIndex SegStart = SE.enterIntvAtEnd(*BI.MBB);
773       assert(SegStart >= IP.second && "Couldn't avoid interference");
774       continue;
775     }
776
777     if (IP.second.getBoundaryIndex() < BI.LastUse) {
778       // There are interference-free uses at the end of the block.
779       // Find the first use that can get the live-out register.
780       SmallVectorImpl<SlotIndex>::const_iterator UI =
781         std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
782                          IP.second.getBoundaryIndex());
783       assert(UI != SA->UseSlots.end() && "Couldn't find last use");
784       SlotIndex Use = *UI;
785       assert(Use <= BI.LastUse && "Couldn't find last use");
786       // Only attempt a split befroe the last split point.
787       if (Use.getBaseIndex() <= BI.LastSplitPoint) {
788         DEBUG(dbgs() << ", free use at " << Use << ".\n");
789         SlotIndex SegStart = SE.enterIntvBefore(Use);
790         assert(SegStart >= IP.second && "Couldn't avoid interference");
791         assert(SegStart < BI.LastSplitPoint && "Impossible split point");
792         SE.useIntv(SegStart, Stop);
793         continue;
794       }
795     }
796
797     // Interference is after the last use.
798     DEBUG(dbgs() << " after last use.\n");
799     SlotIndex SegStart = SE.enterIntvAtEnd(*BI.MBB);
800     assert(SegStart >= IP.second && "Couldn't avoid interference");
801   }
802
803   // Now all defs leading to live bundles are handled, do everything else.
804   for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) {
805     SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i];
806     bool RegIn  = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
807     bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
808
809     // Is the register live-in?
810     if (!BI.LiveIn || !RegIn)
811       continue;
812
813     // We have an incoming register. Check for interference.
814     IndexPair &IP = InterferenceRanges[i];
815     SlotIndex Start, Stop;
816     tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
817
818     DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0)
819                  << " -> BB#" << BI.MBB->getNumber());
820
821     // Check interference entering the block.
822     if (!IP.first.isValid()) {
823       // Block is interference-free.
824       DEBUG(dbgs() << ", no interference");
825       if (!BI.Uses) {
826         assert(BI.LiveThrough && "No uses, but not live through block?");
827         // Block is live-through without interference.
828         if (RegOut) {
829           DEBUG(dbgs() << ", no uses, live-through.\n");
830           SE.useIntv(Start, Stop);
831         } else {
832           DEBUG(dbgs() << ", no uses, stack-out.\n");
833           SE.leaveIntvAtTop(*BI.MBB);
834         }
835         continue;
836       }
837       if (!BI.LiveThrough) {
838         DEBUG(dbgs() << ", killed in block.\n");
839         SE.useIntv(Start, SE.leaveIntvAfter(BI.Kill));
840         continue;
841       }
842       if (!RegOut) {
843         // Block is live-through, but exit bundle is on the stack.
844         // Spill immediately after the last use.
845         if (BI.LastUse < BI.LastSplitPoint) {
846           DEBUG(dbgs() << ", uses, stack-out.\n");
847           SE.useIntv(Start, SE.leaveIntvAfter(BI.LastUse));
848           continue;
849         }
850         // The last use is after the last split point, it is probably an
851         // indirect jump.
852         DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point "
853                      << BI.LastSplitPoint << ", stack-out.\n");
854         SlotIndex SegEnd = SE.leaveIntvBefore(BI.LastSplitPoint);
855         SE.useIntv(Start, SegEnd);
856         // Run a double interval from the split to the last use.
857         // This makes it possible to spill the complement without affecting the
858         // indirect branch.
859         SE.overlapIntv(SegEnd, BI.LastUse);
860         continue;
861       }
862       // Register is live-through.
863       DEBUG(dbgs() << ", uses, live-through.\n");
864       SE.useIntv(Start, Stop);
865       continue;
866     }
867
868     // Block has interference.
869     DEBUG(dbgs() << ", interference from " << IP.first);
870
871     if (!BI.LiveThrough && IP.first >= BI.Kill) {
872       // The interference doesn't reach the outgoing segment.
873       DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n');
874       SE.useIntv(Start, BI.Kill);
875       continue;
876     }
877
878     if (!BI.Uses) {
879       // No uses in block, avoid interference by spilling as soon as possible.
880       DEBUG(dbgs() << ", no uses.\n");
881       SlotIndex SegEnd = SE.leaveIntvAtTop(*BI.MBB);
882       assert(SegEnd <= IP.first && "Couldn't avoid interference");
883       continue;
884     }
885     if (IP.first.getBaseIndex() > BI.FirstUse) {
886       // There are interference-free uses at the beginning of the block.
887       // Find the last use that can get the register.
888       SmallVectorImpl<SlotIndex>::const_iterator UI =
889         std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(),
890                          IP.first.getBaseIndex());
891       assert(UI != SA->UseSlots.begin() && "Couldn't find first use");
892       SlotIndex Use = (--UI)->getBoundaryIndex();
893       DEBUG(dbgs() << ", free use at " << *UI << ".\n");
894       SlotIndex SegEnd = SE.leaveIntvAfter(Use);
895       assert(SegEnd <= IP.first && "Couldn't avoid interference");
896       SE.useIntv(Start, SegEnd);
897       continue;
898     }
899
900     // Interference is before the first use.
901     DEBUG(dbgs() << " before first use.\n");
902     SlotIndex SegEnd = SE.leaveIntvAtTop(*BI.MBB);
903     assert(SegEnd <= IP.first && "Couldn't avoid interference");
904   }
905
906   SE.closeIntv();
907
908   // FIXME: Should we be more aggressive about splitting the stack region into
909   // per-block segments? The current approach allows the stack region to
910   // separate into connected components. Some components may be allocatable.
911   SE.finish();
912   ++NumGlobalSplits;
913
914   if (VerifyEnabled) {
915     MF->verify(this, "After splitting live range around region");
916
917 #ifndef NDEBUG
918     // Make sure that at least one of the new intervals can allocate to PhysReg.
919     // That was the whole point of splitting the live range.
920     bool found = false;
921     for (LiveRangeEdit::iterator I = LREdit.begin(), E = LREdit.end(); I != E;
922          ++I)
923       if (!checkUncachedInterference(**I, PhysReg)) {
924         found = true;
925         break;
926       }
927     assert(found && "No allocatable intervals after pointless splitting");
928 #endif
929   }
930 }
931
932 unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
933                                   SmallVectorImpl<LiveInterval*> &NewVRegs) {
934   BitVector LiveBundles, BestBundles;
935   float BestCost = 0;
936   unsigned BestReg = 0;
937   Order.rewind();
938   while (unsigned PhysReg = Order.next()) {
939     float Cost = calcInterferenceInfo(VirtReg, PhysReg);
940     if (BestReg && Cost >= BestCost)
941       continue;
942
943     SpillPlacer->placeSpills(SpillConstraints, LiveBundles);
944     // No live bundles, defer to splitSingleBlocks().
945     if (!LiveBundles.any())
946       continue;
947
948     Cost += calcGlobalSplitCost(LiveBundles);
949     if (!BestReg || Cost < BestCost) {
950       BestReg = PhysReg;
951       BestCost = Cost;
952       BestBundles.swap(LiveBundles);
953     }
954   }
955
956   if (!BestReg)
957     return 0;
958
959   splitAroundRegion(VirtReg, BestReg, BestBundles, NewVRegs);
960   setStage(NewVRegs.begin(), NewVRegs.end(), RS_Region);
961   return 0;
962 }
963
964
965 //===----------------------------------------------------------------------===//
966 //                             Local Splitting
967 //===----------------------------------------------------------------------===//
968
969
970 /// calcGapWeights - Compute the maximum spill weight that needs to be evicted
971 /// in order to use PhysReg between two entries in SA->UseSlots.
972 ///
973 /// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
974 ///
975 void RAGreedy::calcGapWeights(unsigned PhysReg,
976                               SmallVectorImpl<float> &GapWeight) {
977   assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
978   const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
979   const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
980   const unsigned NumGaps = Uses.size()-1;
981
982   // Start and end points for the interference check.
983   SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse;
984   SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse;
985
986   GapWeight.assign(NumGaps, 0.0f);
987
988   // Add interference from each overlapping register.
989   for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
990     if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
991            .checkInterference())
992       continue;
993
994     // We know that VirtReg is a continuous interval from FirstUse to LastUse,
995     // so we don't need InterferenceQuery.
996     //
997     // Interference that overlaps an instruction is counted in both gaps
998     // surrounding the instruction. The exception is interference before
999     // StartIdx and after StopIdx.
1000     //
1001     LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
1002     for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1003       // Skip the gaps before IntI.
1004       while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1005         if (++Gap == NumGaps)
1006           break;
1007       if (Gap == NumGaps)
1008         break;
1009
1010       // Update the gaps covered by IntI.
1011       const float weight = IntI.value()->weight;
1012       for (; Gap != NumGaps; ++Gap) {
1013         GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1014         if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1015           break;
1016       }
1017       if (Gap == NumGaps)
1018         break;
1019     }
1020   }
1021 }
1022
1023 /// getPrevMappedIndex - Return the slot index of the last non-copy instruction
1024 /// before MI that has a slot index. If MI is the first mapped instruction in
1025 /// its block, return the block start index instead.
1026 ///
1027 SlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) {
1028   assert(MI && "Missing MachineInstr");
1029   const MachineBasicBlock *MBB = MI->getParent();
1030   MachineBasicBlock::const_iterator B = MBB->begin(), I = MI;
1031   while (I != B)
1032     if (!(--I)->isDebugValue() && !I->isCopy())
1033       return Indexes->getInstructionIndex(I);
1034   return Indexes->getMBBStartIdx(MBB);
1035 }
1036
1037 /// calcPrevSlots - Fill in the PrevSlot array with the index of the previous
1038 /// real non-copy instruction for each instruction in SA->UseSlots.
1039 ///
1040 void RAGreedy::calcPrevSlots() {
1041   const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1042   PrevSlot.clear();
1043   PrevSlot.reserve(Uses.size());
1044   for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
1045     const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]);
1046     PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex());
1047   }
1048 }
1049
1050 /// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may
1051 /// be beneficial to split before UseSlots[i].
1052 ///
1053 /// 0 is always a valid split point
1054 unsigned RAGreedy::nextSplitPoint(unsigned i) {
1055   const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1056   const unsigned Size = Uses.size();
1057   assert(i != Size && "No split points after the end");
1058   // Allow split before i when Uses[i] is not adjacent to the previous use.
1059   while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex())
1060     ;
1061   return i;
1062 }
1063
1064 /// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1065 /// basic block.
1066 ///
1067 unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1068                                  SmallVectorImpl<LiveInterval*> &NewVRegs) {
1069   assert(SA->LiveBlocks.size() == 1 && "Not a local interval");
1070   const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front();
1071
1072   // Note that it is possible to have an interval that is live-in or live-out
1073   // while only covering a single block - A phi-def can use undef values from
1074   // predecessors, and the block could be a single-block loop.
1075   // We don't bother doing anything clever about such a case, we simply assume
1076   // that the interval is continuous from FirstUse to LastUse. We should make
1077   // sure that we don't do anything illegal to such an interval, though.
1078
1079   const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1080   if (Uses.size() <= 2)
1081     return 0;
1082   const unsigned NumGaps = Uses.size()-1;
1083
1084   DEBUG({
1085     dbgs() << "tryLocalSplit: ";
1086     for (unsigned i = 0, e = Uses.size(); i != e; ++i)
1087       dbgs() << ' ' << SA->UseSlots[i];
1088     dbgs() << '\n';
1089   });
1090
1091   // For every use, find the previous mapped non-copy instruction.
1092   // We use this to detect valid split points, and to estimate new interval
1093   // sizes.
1094   calcPrevSlots();
1095
1096   unsigned BestBefore = NumGaps;
1097   unsigned BestAfter = 0;
1098   float BestDiff = 0;
1099
1100   const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB);
1101   SmallVector<float, 8> GapWeight;
1102
1103   Order.rewind();
1104   while (unsigned PhysReg = Order.next()) {
1105     // Keep track of the largest spill weight that would need to be evicted in
1106     // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1107     calcGapWeights(PhysReg, GapWeight);
1108
1109     // Try to find the best sequence of gaps to close.
1110     // The new spill weight must be larger than any gap interference.
1111
1112     // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1113     unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1;
1114
1115     // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1116     // It is the spill weight that needs to be evicted.
1117     float MaxGap = GapWeight[0];
1118     for (unsigned i = 1; i != SplitAfter; ++i)
1119       MaxGap = std::max(MaxGap, GapWeight[i]);
1120
1121     for (;;) {
1122       // Live before/after split?
1123       const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1124       const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1125
1126       DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1127                    << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1128                    << " i=" << MaxGap);
1129
1130       // Stop before the interval gets so big we wouldn't be making progress.
1131       if (!LiveBefore && !LiveAfter) {
1132         DEBUG(dbgs() << " all\n");
1133         break;
1134       }
1135       // Should the interval be extended or shrunk?
1136       bool Shrink = true;
1137       if (MaxGap < HUGE_VALF) {
1138         // Estimate the new spill weight.
1139         //
1140         // Each instruction reads and writes the register, except the first
1141         // instr doesn't read when !FirstLive, and the last instr doesn't write
1142         // when !LastLive.
1143         //
1144         // We will be inserting copies before and after, so the total number of
1145         // reads and writes is 2 * EstUses.
1146         //
1147         const unsigned EstUses = 2*(SplitAfter - SplitBefore) +
1148                                  2*(LiveBefore + LiveAfter);
1149
1150         // Try to guess the size of the new interval. This should be trivial,
1151         // but the slot index of an inserted copy can be a lot smaller than the
1152         // instruction it is inserted before if there are many dead indexes
1153         // between them.
1154         //
1155         // We measure the distance from the instruction before SplitBefore to
1156         // get a conservative estimate.
1157         //
1158         // The final distance can still be different if inserting copies
1159         // triggers a slot index renumbering.
1160         //
1161         const float EstWeight = normalizeSpillWeight(blockFreq * EstUses,
1162                               PrevSlot[SplitBefore].distance(Uses[SplitAfter]));
1163         // Would this split be possible to allocate?
1164         // Never allocate all gaps, we wouldn't be making progress.
1165         float Diff = EstWeight - MaxGap;
1166         DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff);
1167         if (Diff > 0) {
1168           Shrink = false;
1169           if (Diff > BestDiff) {
1170             DEBUG(dbgs() << " (best)");
1171             BestDiff = Diff;
1172             BestBefore = SplitBefore;
1173             BestAfter = SplitAfter;
1174           }
1175         }
1176       }
1177
1178       // Try to shrink.
1179       if (Shrink) {
1180         SplitBefore = nextSplitPoint(SplitBefore);
1181         if (SplitBefore < SplitAfter) {
1182           DEBUG(dbgs() << " shrink\n");
1183           // Recompute the max when necessary.
1184           if (GapWeight[SplitBefore - 1] >= MaxGap) {
1185             MaxGap = GapWeight[SplitBefore];
1186             for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1187               MaxGap = std::max(MaxGap, GapWeight[i]);
1188           }
1189           continue;
1190         }
1191         MaxGap = 0;
1192       }
1193
1194       // Try to extend the interval.
1195       if (SplitAfter >= NumGaps) {
1196         DEBUG(dbgs() << " end\n");
1197         break;
1198       }
1199
1200       DEBUG(dbgs() << " extend\n");
1201       for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1;
1202            SplitAfter != e; ++SplitAfter)
1203         MaxGap = std::max(MaxGap, GapWeight[SplitAfter]);
1204           continue;
1205     }
1206   }
1207
1208   // Didn't find any candidates?
1209   if (BestBefore == NumGaps)
1210     return 0;
1211
1212   DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1213                << '-' << Uses[BestAfter] << ", " << BestDiff
1214                << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1215
1216   SmallVector<LiveInterval*, 4> SpillRegs;
1217   LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
1218   SplitEditor SE(*SA, *LIS, *VRM, *DomTree, LREdit);
1219
1220   SE.openIntv();
1221   SlotIndex SegStart = SE.enterIntvBefore(Uses[BestBefore]);
1222   SlotIndex SegStop  = SE.leaveIntvAfter(Uses[BestAfter]);
1223   SE.useIntv(SegStart, SegStop);
1224   SE.closeIntv();
1225   SE.finish();
1226   setStage(NewVRegs.begin(), NewVRegs.end(), RS_Local);
1227   ++NumLocalSplits;
1228
1229   return 0;
1230 }
1231
1232 //===----------------------------------------------------------------------===//
1233 //                          Live Range Splitting
1234 //===----------------------------------------------------------------------===//
1235
1236 /// trySplit - Try to split VirtReg or one of its interferences, making it
1237 /// assignable.
1238 /// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1239 unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1240                             SmallVectorImpl<LiveInterval*>&NewVRegs) {
1241   // Local intervals are handled separately.
1242   if (LIS->intervalIsInOneMBB(VirtReg)) {
1243     NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
1244     SA->analyze(&VirtReg);
1245     return tryLocalSplit(VirtReg, Order, NewVRegs);
1246   }
1247
1248   NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
1249
1250   // Don't iterate global splitting.
1251   // Move straight to spilling if this range was produced by a global split.
1252   LiveRangeStage Stage = getStage(VirtReg);
1253   if (Stage >= RS_Block)
1254     return 0;
1255
1256   SA->analyze(&VirtReg);
1257
1258   // First try to split around a region spanning multiple blocks.
1259   if (Stage < RS_Region) {
1260     unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1261     if (PhysReg || !NewVRegs.empty())
1262       return PhysReg;
1263   }
1264
1265   // Then isolate blocks with multiple uses.
1266   if (Stage < RS_Block) {
1267     SplitAnalysis::BlockPtrSet Blocks;
1268     if (SA->getMultiUseBlocks(Blocks)) {
1269       SmallVector<LiveInterval*, 4> SpillRegs;
1270       LiveRangeEdit LREdit(VirtReg, NewVRegs, SpillRegs);
1271       SplitEditor(*SA, *LIS, *VRM, *DomTree, LREdit).splitSingleBlocks(Blocks);
1272       setStage(NewVRegs.begin(), NewVRegs.end(), RS_Block);
1273       if (VerifyEnabled)
1274         MF->verify(this, "After splitting live range around basic blocks");
1275     }
1276   }
1277
1278   // Don't assign any physregs.
1279   return 0;
1280 }
1281
1282
1283 //===----------------------------------------------------------------------===//
1284 //                            Main Entry Point
1285 //===----------------------------------------------------------------------===//
1286
1287 unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
1288                                  SmallVectorImpl<LiveInterval*> &NewVRegs) {
1289   LiveRangeStage Stage = getStage(VirtReg);
1290   if (Stage == RS_Original)
1291     LRStage[VirtReg.reg] = RS_Second;
1292
1293   // First try assigning a free register.
1294   AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs);
1295   while (unsigned PhysReg = Order.next()) {
1296     if (!checkPhysRegInterference(VirtReg, PhysReg))
1297       return PhysReg;
1298   }
1299
1300   if (unsigned PhysReg = tryReassign(VirtReg, Order, NewVRegs))
1301     return PhysReg;
1302
1303   if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
1304     return PhysReg;
1305
1306   assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1307
1308   // The first time we see a live range, don't try to split or spill.
1309   // Wait until the second time, when all smaller ranges have been allocated.
1310   // This gives a better picture of the interference to split around.
1311   if (Stage == RS_Original) {
1312     NewVRegs.push_back(&VirtReg);
1313     return 0;
1314   }
1315
1316   assert(Stage < RS_Spill && "Cannot allocate after spilling");
1317
1318   // Try splitting VirtReg or interferences.
1319   unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1320   if (PhysReg || !NewVRegs.empty())
1321     return PhysReg;
1322
1323   // Finally spill VirtReg itself.
1324   NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
1325   SmallVector<LiveInterval*, 1> pendingSpills;
1326   spiller().spill(&VirtReg, NewVRegs, pendingSpills);
1327
1328   // The live virtual register requesting allocation was spilled, so tell
1329   // the caller not to allocate anything during this round.
1330   return 0;
1331 }
1332
1333 bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1334   DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1335                << "********** Function: "
1336                << ((Value*)mf.getFunction())->getName() << '\n');
1337
1338   MF = &mf;
1339   if (VerifyEnabled)
1340     MF->verify(this, "Before greedy register allocator");
1341
1342   RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
1343   Indexes = &getAnalysis<SlotIndexes>();
1344   DomTree = &getAnalysis<MachineDominatorTree>();
1345   ReservedRegs = TRI->getReservedRegs(*MF);
1346   SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
1347   Loops = &getAnalysis<MachineLoopInfo>();
1348   LoopRanges = &getAnalysis<MachineLoopRanges>();
1349   Bundles = &getAnalysis<EdgeBundles>();
1350   SpillPlacer = &getAnalysis<SpillPlacement>();
1351
1352   SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
1353   LRStage.clear();
1354   LRStage.resize(MRI->getNumVirtRegs());
1355
1356   allocatePhysRegs();
1357   addMBBLiveIns(MF);
1358   LIS->addKillFlags();
1359
1360   // Run rewriter
1361   {
1362     NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
1363     VRM->rewrite(Indexes);
1364   }
1365
1366   // The pass output is in VirtRegMap. Release all the transient data.
1367   releaseMemory();
1368
1369   return true;
1370 }