Make MachineDominators available for SplitEditor. We are going to need it for
[oota-llvm.git] / lib / CodeGen / RegAllocLinearScan.cpp
1 //===-- RegAllocLinearScan.cpp - Linear Scan 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 implements a linear scan register allocator.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "regalloc"
15 #include "VirtRegMap.h"
16 #include "VirtRegRewriter.h"
17 #include "Spiller.h"
18 #include "llvm/Function.h"
19 #include "llvm/CodeGen/CalcSpillWeights.h"
20 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineLoopInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/CodeGen/RegAllocRegistry.h"
27 #include "llvm/CodeGen/RegisterCoalescer.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/ADT/EquivalenceClasses.h"
33 #include "llvm/ADT/SmallSet.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include <algorithm>
40 #include <set>
41 #include <queue>
42 #include <memory>
43 #include <cmath>
44
45 using namespace llvm;
46
47 STATISTIC(NumIters     , "Number of iterations performed");
48 STATISTIC(NumBacktracks, "Number of times we had to backtrack");
49 STATISTIC(NumCoalesce,   "Number of copies coalesced");
50 STATISTIC(NumDowngrade,  "Number of registers downgraded");
51
52 static cl::opt<bool>
53 NewHeuristic("new-spilling-heuristic",
54              cl::desc("Use new spilling heuristic"),
55              cl::init(false), cl::Hidden);
56
57 static cl::opt<bool>
58 PreSplitIntervals("pre-alloc-split",
59                   cl::desc("Pre-register allocation live interval splitting"),
60                   cl::init(false), cl::Hidden);
61
62 static cl::opt<bool>
63 TrivCoalesceEnds("trivial-coalesce-ends",
64                   cl::desc("Attempt trivial coalescing of interval ends"),
65                   cl::init(false), cl::Hidden);
66
67 static RegisterRegAlloc
68 linearscanRegAlloc("linearscan", "linear scan register allocator",
69                    createLinearScanRegisterAllocator);
70
71 namespace {
72   // When we allocate a register, add it to a fixed-size queue of
73   // registers to skip in subsequent allocations. This trades a small
74   // amount of register pressure and increased spills for flexibility in
75   // the post-pass scheduler.
76   //
77   // Note that in a the number of registers used for reloading spills
78   // will be one greater than the value of this option.
79   //
80   // One big limitation of this is that it doesn't differentiate between
81   // different register classes. So on x86-64, if there is xmm register
82   // pressure, it can caused fewer GPRs to be held in the queue.
83   static cl::opt<unsigned>
84   NumRecentlyUsedRegs("linearscan-skip-count",
85                       cl::desc("Number of registers for linearscan to remember"
86                                "to skip."),
87                       cl::init(0),
88                       cl::Hidden);
89
90   struct RALinScan : public MachineFunctionPass {
91     static char ID;
92     RALinScan() : MachineFunctionPass(ID) {
93       initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
94       initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
95       initializeRegisterCoalescerAnalysisGroup(
96         *PassRegistry::getPassRegistry());
97       initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
98       initializePreAllocSplittingPass(*PassRegistry::getPassRegistry());
99       initializeLiveStacksPass(*PassRegistry::getPassRegistry());
100       initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
101       initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
102       initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
103       initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
104       
105       // Initialize the queue to record recently-used registers.
106       if (NumRecentlyUsedRegs > 0)
107         RecentRegs.resize(NumRecentlyUsedRegs, 0);
108       RecentNext = RecentRegs.begin();
109     }
110
111     typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
112     typedef SmallVector<IntervalPtr, 32> IntervalPtrs;
113   private:
114     /// RelatedRegClasses - This structure is built the first time a function is
115     /// compiled, and keeps track of which register classes have registers that
116     /// belong to multiple classes or have aliases that are in other classes.
117     EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
118     DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
119
120     // NextReloadMap - For each register in the map, it maps to the another
121     // register which is defined by a reload from the same stack slot and
122     // both reloads are in the same basic block.
123     DenseMap<unsigned, unsigned> NextReloadMap;
124
125     // DowngradedRegs - A set of registers which are being "downgraded", i.e.
126     // un-favored for allocation.
127     SmallSet<unsigned, 8> DowngradedRegs;
128
129     // DowngradeMap - A map from virtual registers to physical registers being
130     // downgraded for the virtual registers.
131     DenseMap<unsigned, unsigned> DowngradeMap;
132
133     MachineFunction* mf_;
134     MachineRegisterInfo* mri_;
135     const TargetMachine* tm_;
136     const TargetRegisterInfo* tri_;
137     const TargetInstrInfo* tii_;
138     BitVector allocatableRegs_;
139     BitVector reservedRegs_;
140     LiveIntervals* li_;
141     MachineLoopInfo *loopInfo;
142
143     /// handled_ - Intervals are added to the handled_ set in the order of their
144     /// start value.  This is uses for backtracking.
145     std::vector<LiveInterval*> handled_;
146
147     /// fixed_ - Intervals that correspond to machine registers.
148     ///
149     IntervalPtrs fixed_;
150
151     /// active_ - Intervals that are currently being processed, and which have a
152     /// live range active for the current point.
153     IntervalPtrs active_;
154
155     /// inactive_ - Intervals that are currently being processed, but which have
156     /// a hold at the current point.
157     IntervalPtrs inactive_;
158
159     typedef std::priority_queue<LiveInterval*,
160                                 SmallVector<LiveInterval*, 64>,
161                                 greater_ptr<LiveInterval> > IntervalHeap;
162     IntervalHeap unhandled_;
163
164     /// regUse_ - Tracks register usage.
165     SmallVector<unsigned, 32> regUse_;
166     SmallVector<unsigned, 32> regUseBackUp_;
167
168     /// vrm_ - Tracks register assignments.
169     VirtRegMap* vrm_;
170
171     std::auto_ptr<VirtRegRewriter> rewriter_;
172
173     std::auto_ptr<Spiller> spiller_;
174
175     // The queue of recently-used registers.
176     SmallVector<unsigned, 4> RecentRegs;
177     SmallVector<unsigned, 4>::iterator RecentNext;
178
179     // Record that we just picked this register.
180     void recordRecentlyUsed(unsigned reg) {
181       assert(reg != 0 && "Recently used register is NOREG!");
182       if (!RecentRegs.empty()) {
183         *RecentNext++ = reg;
184         if (RecentNext == RecentRegs.end())
185           RecentNext = RecentRegs.begin();
186       }
187     }
188
189   public:
190     virtual const char* getPassName() const {
191       return "Linear Scan Register Allocator";
192     }
193
194     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
195       AU.setPreservesCFG();
196       AU.addRequired<LiveIntervals>();
197       AU.addPreserved<SlotIndexes>();
198       if (StrongPHIElim)
199         AU.addRequiredID(StrongPHIEliminationID);
200       // Make sure PassManager knows which analyses to make available
201       // to coalescing and which analyses coalescing invalidates.
202       AU.addRequiredTransitive<RegisterCoalescer>();
203       AU.addRequired<CalculateSpillWeights>();
204       if (PreSplitIntervals)
205         AU.addRequiredID(PreAllocSplittingID);
206       AU.addRequiredID(LiveStacksID);
207       AU.addPreservedID(LiveStacksID);
208       AU.addRequired<MachineLoopInfo>();
209       AU.addPreserved<MachineLoopInfo>();
210       AU.addRequired<VirtRegMap>();
211       AU.addPreserved<VirtRegMap>();
212       AU.addRequiredID(MachineDominatorsID);
213       AU.addPreservedID(MachineDominatorsID);
214       MachineFunctionPass::getAnalysisUsage(AU);
215     }
216
217     /// runOnMachineFunction - register allocate the whole function
218     bool runOnMachineFunction(MachineFunction&);
219
220     // Determine if we skip this register due to its being recently used.
221     bool isRecentlyUsed(unsigned reg) const {
222       return std::find(RecentRegs.begin(), RecentRegs.end(), reg) !=
223              RecentRegs.end();
224     }
225
226   private:
227     /// linearScan - the linear scan algorithm
228     void linearScan();
229
230     /// initIntervalSets - initialize the interval sets.
231     ///
232     void initIntervalSets();
233
234     /// processActiveIntervals - expire old intervals and move non-overlapping
235     /// ones to the inactive list.
236     void processActiveIntervals(SlotIndex CurPoint);
237
238     /// processInactiveIntervals - expire old intervals and move overlapping
239     /// ones to the active list.
240     void processInactiveIntervals(SlotIndex CurPoint);
241
242     /// hasNextReloadInterval - Return the next liveinterval that's being
243     /// defined by a reload from the same SS as the specified one.
244     LiveInterval *hasNextReloadInterval(LiveInterval *cur);
245
246     /// DowngradeRegister - Downgrade a register for allocation.
247     void DowngradeRegister(LiveInterval *li, unsigned Reg);
248
249     /// UpgradeRegister - Upgrade a register for allocation.
250     void UpgradeRegister(unsigned Reg);
251
252     /// assignRegOrStackSlotAtInterval - assign a register if one
253     /// is available, or spill.
254     void assignRegOrStackSlotAtInterval(LiveInterval* cur);
255
256     void updateSpillWeights(std::vector<float> &Weights,
257                             unsigned reg, float weight,
258                             const TargetRegisterClass *RC);
259
260     /// findIntervalsToSpill - Determine the intervals to spill for the
261     /// specified interval. It's passed the physical registers whose spill
262     /// weight is the lowest among all the registers whose live intervals
263     /// conflict with the interval.
264     void findIntervalsToSpill(LiveInterval *cur,
265                             std::vector<std::pair<unsigned,float> > &Candidates,
266                             unsigned NumCands,
267                             SmallVector<LiveInterval*, 8> &SpillIntervals);
268
269     /// attemptTrivialCoalescing - If a simple interval is defined by a copy,
270     /// try to allocate the definition to the same register as the source,
271     /// if the register is not defined during the life time of the interval.
272     /// This eliminates a copy, and is used to coalesce copies which were not
273     /// coalesced away before allocation either due to dest and src being in
274     /// different register classes or because the coalescer was overly
275     /// conservative.
276     unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg);
277
278     ///
279     /// Register usage / availability tracking helpers.
280     ///
281
282     void initRegUses() {
283       regUse_.resize(tri_->getNumRegs(), 0);
284       regUseBackUp_.resize(tri_->getNumRegs(), 0);
285     }
286
287     void finalizeRegUses() {
288 #ifndef NDEBUG
289       // Verify all the registers are "freed".
290       bool Error = false;
291       for (unsigned i = 0, e = tri_->getNumRegs(); i != e; ++i) {
292         if (regUse_[i] != 0) {
293           dbgs() << tri_->getName(i) << " is still in use!\n";
294           Error = true;
295         }
296       }
297       if (Error)
298         llvm_unreachable(0);
299 #endif
300       regUse_.clear();
301       regUseBackUp_.clear();
302     }
303
304     void addRegUse(unsigned physReg) {
305       assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
306              "should be physical register!");
307       ++regUse_[physReg];
308       for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as)
309         ++regUse_[*as];
310     }
311
312     void delRegUse(unsigned physReg) {
313       assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
314              "should be physical register!");
315       assert(regUse_[physReg] != 0);
316       --regUse_[physReg];
317       for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) {
318         assert(regUse_[*as] != 0);
319         --regUse_[*as];
320       }
321     }
322
323     bool isRegAvail(unsigned physReg) const {
324       assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
325              "should be physical register!");
326       return regUse_[physReg] == 0;
327     }
328
329     void backUpRegUses() {
330       regUseBackUp_ = regUse_;
331     }
332
333     void restoreRegUses() {
334       regUse_ = regUseBackUp_;
335     }
336
337     ///
338     /// Register handling helpers.
339     ///
340
341     /// getFreePhysReg - return a free physical register for this virtual
342     /// register interval if we have one, otherwise return 0.
343     unsigned getFreePhysReg(LiveInterval* cur);
344     unsigned getFreePhysReg(LiveInterval* cur,
345                             const TargetRegisterClass *RC,
346                             unsigned MaxInactiveCount,
347                             SmallVector<unsigned, 256> &inactiveCounts,
348                             bool SkipDGRegs);
349
350     /// getFirstNonReservedPhysReg - return the first non-reserved physical
351     /// register in the register class.
352     unsigned getFirstNonReservedPhysReg(const TargetRegisterClass *RC) {
353         TargetRegisterClass::iterator aoe = RC->allocation_order_end(*mf_);
354         TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_);
355         while (i != aoe && reservedRegs_.test(*i))
356           ++i;
357         assert(i != aoe && "All registers reserved?!");
358         return *i;
359       }
360
361     void ComputeRelatedRegClasses();
362
363     template <typename ItTy>
364     void printIntervals(const char* const str, ItTy i, ItTy e) const {
365       DEBUG({
366           if (str)
367             dbgs() << str << " intervals:\n";
368
369           for (; i != e; ++i) {
370             dbgs() << "\t" << *i->first << " -> ";
371
372             unsigned reg = i->first->reg;
373             if (TargetRegisterInfo::isVirtualRegister(reg))
374               reg = vrm_->getPhys(reg);
375
376             dbgs() << tri_->getName(reg) << '\n';
377           }
378         });
379     }
380   };
381   char RALinScan::ID = 0;
382 }
383
384 INITIALIZE_PASS_BEGIN(RALinScan, "linearscan-regalloc",
385                 "Linear Scan Register Allocator", false, false)
386 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
387 INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination)
388 INITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights)
389 INITIALIZE_PASS_DEPENDENCY(PreAllocSplitting)
390 INITIALIZE_PASS_DEPENDENCY(LiveStacks)
391 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
392 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
393 INITIALIZE_AG_DEPENDENCY(RegisterCoalescer)
394 INITIALIZE_PASS_END(RALinScan, "linearscan-regalloc",
395                 "Linear Scan Register Allocator", false, false)
396
397 void RALinScan::ComputeRelatedRegClasses() {
398   // First pass, add all reg classes to the union, and determine at least one
399   // reg class that each register is in.
400   bool HasAliases = false;
401   for (TargetRegisterInfo::regclass_iterator RCI = tri_->regclass_begin(),
402        E = tri_->regclass_end(); RCI != E; ++RCI) {
403     RelatedRegClasses.insert(*RCI);
404     for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
405          I != E; ++I) {
406       HasAliases = HasAliases || *tri_->getAliasSet(*I) != 0;
407
408       const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
409       if (PRC) {
410         // Already processed this register.  Just make sure we know that
411         // multiple register classes share a register.
412         RelatedRegClasses.unionSets(PRC, *RCI);
413       } else {
414         PRC = *RCI;
415       }
416     }
417   }
418
419   // Second pass, now that we know conservatively what register classes each reg
420   // belongs to, add info about aliases.  We don't need to do this for targets
421   // without register aliases.
422   if (HasAliases)
423     for (DenseMap<unsigned, const TargetRegisterClass*>::iterator
424          I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
425          I != E; ++I)
426       for (const unsigned *AS = tri_->getAliasSet(I->first); *AS; ++AS)
427         RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
428 }
429
430 /// attemptTrivialCoalescing - If a simple interval is defined by a copy, try
431 /// allocate the definition the same register as the source register if the
432 /// register is not defined during live time of the interval. If the interval is
433 /// killed by a copy, try to use the destination register. This eliminates a
434 /// copy. This is used to coalesce copies which were not coalesced away before
435 /// allocation either due to dest and src being in different register classes or
436 /// because the coalescer was overly conservative.
437 unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
438   unsigned Preference = vrm_->getRegAllocPref(cur.reg);
439   if ((Preference && Preference == Reg) || !cur.containsOneValue())
440     return Reg;
441
442   // We cannot handle complicated live ranges. Simple linear stuff only.
443   if (cur.ranges.size() != 1)
444     return Reg;
445
446   const LiveRange &range = cur.ranges.front();
447
448   VNInfo *vni = range.valno;
449   if (vni->isUnused())
450     return Reg;
451
452   unsigned CandReg;
453   {
454     MachineInstr *CopyMI;
455     if ((CopyMI = li_->getInstructionFromIndex(vni->def)) && CopyMI->isCopy())
456       // Defined by a copy, try to extend SrcReg forward
457       CandReg = CopyMI->getOperand(1).getReg();
458     else if (TrivCoalesceEnds &&
459             (CopyMI = li_->getInstructionFromIndex(range.end.getBaseIndex())) &&
460              CopyMI->isCopy() && cur.reg == CopyMI->getOperand(1).getReg())
461       // Only used by a copy, try to extend DstReg backwards
462       CandReg = CopyMI->getOperand(0).getReg();
463     else
464       return Reg;
465   }
466
467   if (TargetRegisterInfo::isVirtualRegister(CandReg)) {
468     if (!vrm_->isAssignedReg(CandReg))
469       return Reg;
470     CandReg = vrm_->getPhys(CandReg);
471   }
472   if (Reg == CandReg)
473     return Reg;
474
475   const TargetRegisterClass *RC = mri_->getRegClass(cur.reg);
476   if (!RC->contains(CandReg))
477     return Reg;
478
479   if (li_->conflictsWithPhysReg(cur, *vrm_, CandReg))
480     return Reg;
481
482   // Try to coalesce.
483   DEBUG(dbgs() << "Coalescing: " << cur << " -> " << tri_->getName(CandReg)
484         << '\n');
485   vrm_->clearVirt(cur.reg);
486   vrm_->assignVirt2Phys(cur.reg, CandReg);
487
488   ++NumCoalesce;
489   return CandReg;
490 }
491
492 bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
493   mf_ = &fn;
494   mri_ = &fn.getRegInfo();
495   tm_ = &fn.getTarget();
496   tri_ = tm_->getRegisterInfo();
497   tii_ = tm_->getInstrInfo();
498   allocatableRegs_ = tri_->getAllocatableSet(fn);
499   reservedRegs_ = tri_->getReservedRegs(fn);
500   li_ = &getAnalysis<LiveIntervals>();
501   loopInfo = &getAnalysis<MachineLoopInfo>();
502
503   // We don't run the coalescer here because we have no reason to
504   // interact with it.  If the coalescer requires interaction, it
505   // won't do anything.  If it doesn't require interaction, we assume
506   // it was run as a separate pass.
507
508   // If this is the first function compiled, compute the related reg classes.
509   if (RelatedRegClasses.empty())
510     ComputeRelatedRegClasses();
511
512   // Also resize register usage trackers.
513   initRegUses();
514
515   vrm_ = &getAnalysis<VirtRegMap>();
516   if (!rewriter_.get()) rewriter_.reset(createVirtRegRewriter());
517
518   spiller_.reset(createSpiller(*this, *mf_, *vrm_));
519
520   initIntervalSets();
521
522   linearScan();
523
524   // Rewrite spill code and update the PhysRegsUsed set.
525   rewriter_->runOnMachineFunction(*mf_, *vrm_, li_);
526
527   assert(unhandled_.empty() && "Unhandled live intervals remain!");
528
529   finalizeRegUses();
530
531   fixed_.clear();
532   active_.clear();
533   inactive_.clear();
534   handled_.clear();
535   NextReloadMap.clear();
536   DowngradedRegs.clear();
537   DowngradeMap.clear();
538   spiller_.reset(0);
539
540   return true;
541 }
542
543 /// initIntervalSets - initialize the interval sets.
544 ///
545 void RALinScan::initIntervalSets()
546 {
547   assert(unhandled_.empty() && fixed_.empty() &&
548          active_.empty() && inactive_.empty() &&
549          "interval sets should be empty on initialization");
550
551   handled_.reserve(li_->getNumIntervals());
552
553   for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
554     if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) {
555       if (!i->second->empty()) {
556         mri_->setPhysRegUsed(i->second->reg);
557         fixed_.push_back(std::make_pair(i->second, i->second->begin()));
558       }
559     } else {
560       if (i->second->empty()) {
561         assignRegOrStackSlotAtInterval(i->second);
562       }
563       else
564         unhandled_.push(i->second);
565     }
566   }
567 }
568
569 void RALinScan::linearScan() {
570   // linear scan algorithm
571   DEBUG({
572       dbgs() << "********** LINEAR SCAN **********\n"
573              << "********** Function: "
574              << mf_->getFunction()->getName() << '\n';
575       printIntervals("fixed", fixed_.begin(), fixed_.end());
576     });
577
578   while (!unhandled_.empty()) {
579     // pick the interval with the earliest start point
580     LiveInterval* cur = unhandled_.top();
581     unhandled_.pop();
582     ++NumIters;
583     DEBUG(dbgs() << "\n*** CURRENT ***: " << *cur << '\n');
584
585     assert(!cur->empty() && "Empty interval in unhandled set.");
586
587     processActiveIntervals(cur->beginIndex());
588     processInactiveIntervals(cur->beginIndex());
589
590     assert(TargetRegisterInfo::isVirtualRegister(cur->reg) &&
591            "Can only allocate virtual registers!");
592
593     // Allocating a virtual register. try to find a free
594     // physical register or spill an interval (possibly this one) in order to
595     // assign it one.
596     assignRegOrStackSlotAtInterval(cur);
597
598     DEBUG({
599         printIntervals("active", active_.begin(), active_.end());
600         printIntervals("inactive", inactive_.begin(), inactive_.end());
601       });
602   }
603
604   // Expire any remaining active intervals
605   while (!active_.empty()) {
606     IntervalPtr &IP = active_.back();
607     unsigned reg = IP.first->reg;
608     DEBUG(dbgs() << "\tinterval " << *IP.first << " expired\n");
609     assert(TargetRegisterInfo::isVirtualRegister(reg) &&
610            "Can only allocate virtual registers!");
611     reg = vrm_->getPhys(reg);
612     delRegUse(reg);
613     active_.pop_back();
614   }
615
616   // Expire any remaining inactive intervals
617   DEBUG({
618       for (IntervalPtrs::reverse_iterator
619              i = inactive_.rbegin(); i != inactive_.rend(); ++i)
620         dbgs() << "\tinterval " << *i->first << " expired\n";
621     });
622   inactive_.clear();
623
624   // Add live-ins to every BB except for entry. Also perform trivial coalescing.
625   MachineFunction::iterator EntryMBB = mf_->begin();
626   SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
627   for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
628     LiveInterval &cur = *i->second;
629     unsigned Reg = 0;
630     bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg);
631     if (isPhys)
632       Reg = cur.reg;
633     else if (vrm_->isAssignedReg(cur.reg))
634       Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
635     if (!Reg)
636       continue;
637     // Ignore splited live intervals.
638     if (!isPhys && vrm_->getPreSplitReg(cur.reg))
639       continue;
640
641     for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
642          I != E; ++I) {
643       const LiveRange &LR = *I;
644       if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) {
645         for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
646           if (LiveInMBBs[i] != EntryMBB) {
647             assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
648                    "Adding a virtual register to livein set?");
649             LiveInMBBs[i]->addLiveIn(Reg);
650           }
651         LiveInMBBs.clear();
652       }
653     }
654   }
655
656   DEBUG(dbgs() << *vrm_);
657
658   // Look for physical registers that end up not being allocated even though
659   // register allocator had to spill other registers in its register class.
660   if (!vrm_->FindUnusedRegisters(li_))
661     return;
662 }
663
664 /// processActiveIntervals - expire old intervals and move non-overlapping ones
665 /// to the inactive list.
666 void RALinScan::processActiveIntervals(SlotIndex CurPoint)
667 {
668   DEBUG(dbgs() << "\tprocessing active intervals:\n");
669
670   for (unsigned i = 0, e = active_.size(); i != e; ++i) {
671     LiveInterval *Interval = active_[i].first;
672     LiveInterval::iterator IntervalPos = active_[i].second;
673     unsigned reg = Interval->reg;
674
675     IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
676
677     if (IntervalPos == Interval->end()) {     // Remove expired intervals.
678       DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n");
679       assert(TargetRegisterInfo::isVirtualRegister(reg) &&
680              "Can only allocate virtual registers!");
681       reg = vrm_->getPhys(reg);
682       delRegUse(reg);
683
684       // Pop off the end of the list.
685       active_[i] = active_.back();
686       active_.pop_back();
687       --i; --e;
688
689     } else if (IntervalPos->start > CurPoint) {
690       // Move inactive intervals to inactive list.
691       DEBUG(dbgs() << "\t\tinterval " << *Interval << " inactive\n");
692       assert(TargetRegisterInfo::isVirtualRegister(reg) &&
693              "Can only allocate virtual registers!");
694       reg = vrm_->getPhys(reg);
695       delRegUse(reg);
696       // add to inactive.
697       inactive_.push_back(std::make_pair(Interval, IntervalPos));
698
699       // Pop off the end of the list.
700       active_[i] = active_.back();
701       active_.pop_back();
702       --i; --e;
703     } else {
704       // Otherwise, just update the iterator position.
705       active_[i].second = IntervalPos;
706     }
707   }
708 }
709
710 /// processInactiveIntervals - expire old intervals and move overlapping
711 /// ones to the active list.
712 void RALinScan::processInactiveIntervals(SlotIndex CurPoint)
713 {
714   DEBUG(dbgs() << "\tprocessing inactive intervals:\n");
715
716   for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
717     LiveInterval *Interval = inactive_[i].first;
718     LiveInterval::iterator IntervalPos = inactive_[i].second;
719     unsigned reg = Interval->reg;
720
721     IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
722
723     if (IntervalPos == Interval->end()) {       // remove expired intervals.
724       DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n");
725
726       // Pop off the end of the list.
727       inactive_[i] = inactive_.back();
728       inactive_.pop_back();
729       --i; --e;
730     } else if (IntervalPos->start <= CurPoint) {
731       // move re-activated intervals in active list
732       DEBUG(dbgs() << "\t\tinterval " << *Interval << " active\n");
733       assert(TargetRegisterInfo::isVirtualRegister(reg) &&
734              "Can only allocate virtual registers!");
735       reg = vrm_->getPhys(reg);
736       addRegUse(reg);
737       // add to active
738       active_.push_back(std::make_pair(Interval, IntervalPos));
739
740       // Pop off the end of the list.
741       inactive_[i] = inactive_.back();
742       inactive_.pop_back();
743       --i; --e;
744     } else {
745       // Otherwise, just update the iterator position.
746       inactive_[i].second = IntervalPos;
747     }
748   }
749 }
750
751 /// updateSpillWeights - updates the spill weights of the specifed physical
752 /// register and its weight.
753 void RALinScan::updateSpillWeights(std::vector<float> &Weights,
754                                    unsigned reg, float weight,
755                                    const TargetRegisterClass *RC) {
756   SmallSet<unsigned, 4> Processed;
757   SmallSet<unsigned, 4> SuperAdded;
758   SmallVector<unsigned, 4> Supers;
759   Weights[reg] += weight;
760   Processed.insert(reg);
761   for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) {
762     Weights[*as] += weight;
763     Processed.insert(*as);
764     if (tri_->isSubRegister(*as, reg) &&
765         SuperAdded.insert(*as) &&
766         RC->contains(*as)) {
767       Supers.push_back(*as);
768     }
769   }
770
771   // If the alias is a super-register, and the super-register is in the
772   // register class we are trying to allocate. Then add the weight to all
773   // sub-registers of the super-register even if they are not aliases.
774   // e.g. allocating for GR32, bh is not used, updating bl spill weight.
775   //      bl should get the same spill weight otherwise it will be choosen
776   //      as a spill candidate since spilling bh doesn't make ebx available.
777   for (unsigned i = 0, e = Supers.size(); i != e; ++i) {
778     for (const unsigned *sr = tri_->getSubRegisters(Supers[i]); *sr; ++sr)
779       if (!Processed.count(*sr))
780         Weights[*sr] += weight;
781   }
782 }
783
784 static
785 RALinScan::IntervalPtrs::iterator
786 FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
787   for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
788        I != E; ++I)
789     if (I->first == LI) return I;
790   return IP.end();
791 }
792
793 static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V,
794                                     SlotIndex Point){
795   for (unsigned i = 0, e = V.size(); i != e; ++i) {
796     RALinScan::IntervalPtr &IP = V[i];
797     LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
798                                                 IP.second, Point);
799     if (I != IP.first->begin()) --I;
800     IP.second = I;
801   }
802 }
803
804 /// getConflictWeight - Return the number of conflicts between cur
805 /// live interval and defs and uses of Reg weighted by loop depthes.
806 static
807 float getConflictWeight(LiveInterval *cur, unsigned Reg, LiveIntervals *li_,
808                         MachineRegisterInfo *mri_,
809                         MachineLoopInfo *loopInfo) {
810   float Conflicts = 0;
811   for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg),
812          E = mri_->reg_end(); I != E; ++I) {
813     MachineInstr *MI = &*I;
814     if (cur->liveAt(li_->getInstructionIndex(MI))) {
815       unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent());
816       Conflicts += std::pow(10.0f, (float)loopDepth);
817     }
818   }
819   return Conflicts;
820 }
821
822 /// findIntervalsToSpill - Determine the intervals to spill for the
823 /// specified interval. It's passed the physical registers whose spill
824 /// weight is the lowest among all the registers whose live intervals
825 /// conflict with the interval.
826 void RALinScan::findIntervalsToSpill(LiveInterval *cur,
827                             std::vector<std::pair<unsigned,float> > &Candidates,
828                             unsigned NumCands,
829                             SmallVector<LiveInterval*, 8> &SpillIntervals) {
830   // We have figured out the *best* register to spill. But there are other
831   // registers that are pretty good as well (spill weight within 3%). Spill
832   // the one that has fewest defs and uses that conflict with cur.
833   float Conflicts[3] = { 0.0f, 0.0f, 0.0f };
834   SmallVector<LiveInterval*, 8> SLIs[3];
835
836   DEBUG({
837       dbgs() << "\tConsidering " << NumCands << " candidates: ";
838       for (unsigned i = 0; i != NumCands; ++i)
839         dbgs() << tri_->getName(Candidates[i].first) << " ";
840       dbgs() << "\n";
841     });
842
843   // Calculate the number of conflicts of each candidate.
844   for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
845     unsigned Reg = i->first->reg;
846     unsigned PhysReg = vrm_->getPhys(Reg);
847     if (!cur->overlapsFrom(*i->first, i->second))
848       continue;
849     for (unsigned j = 0; j < NumCands; ++j) {
850       unsigned Candidate = Candidates[j].first;
851       if (tri_->regsOverlap(PhysReg, Candidate)) {
852         if (NumCands > 1)
853           Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
854         SLIs[j].push_back(i->first);
855       }
856     }
857   }
858
859   for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
860     unsigned Reg = i->first->reg;
861     unsigned PhysReg = vrm_->getPhys(Reg);
862     if (!cur->overlapsFrom(*i->first, i->second-1))
863       continue;
864     for (unsigned j = 0; j < NumCands; ++j) {
865       unsigned Candidate = Candidates[j].first;
866       if (tri_->regsOverlap(PhysReg, Candidate)) {
867         if (NumCands > 1)
868           Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
869         SLIs[j].push_back(i->first);
870       }
871     }
872   }
873
874   // Which is the best candidate?
875   unsigned BestCandidate = 0;
876   float MinConflicts = Conflicts[0];
877   for (unsigned i = 1; i != NumCands; ++i) {
878     if (Conflicts[i] < MinConflicts) {
879       BestCandidate = i;
880       MinConflicts = Conflicts[i];
881     }
882   }
883
884   std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(),
885             std::back_inserter(SpillIntervals));
886 }
887
888 namespace {
889   struct WeightCompare {
890   private:
891     const RALinScan &Allocator;
892
893   public:
894     WeightCompare(const RALinScan &Alloc) : Allocator(Alloc) {}
895
896     typedef std::pair<unsigned, float> RegWeightPair;
897     bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const {
898       return LHS.second < RHS.second && !Allocator.isRecentlyUsed(LHS.first);
899     }
900   };
901 }
902
903 static bool weightsAreClose(float w1, float w2) {
904   if (!NewHeuristic)
905     return false;
906
907   float diff = w1 - w2;
908   if (diff <= 0.02f)  // Within 0.02f
909     return true;
910   return (diff / w2) <= 0.05f;  // Within 5%.
911 }
912
913 LiveInterval *RALinScan::hasNextReloadInterval(LiveInterval *cur) {
914   DenseMap<unsigned, unsigned>::iterator I = NextReloadMap.find(cur->reg);
915   if (I == NextReloadMap.end())
916     return 0;
917   return &li_->getInterval(I->second);
918 }
919
920 void RALinScan::DowngradeRegister(LiveInterval *li, unsigned Reg) {
921   bool isNew = DowngradedRegs.insert(Reg);
922   isNew = isNew; // Silence compiler warning.
923   assert(isNew && "Multiple reloads holding the same register?");
924   DowngradeMap.insert(std::make_pair(li->reg, Reg));
925   for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS) {
926     isNew = DowngradedRegs.insert(*AS);
927     isNew = isNew; // Silence compiler warning.
928     assert(isNew && "Multiple reloads holding the same register?");
929     DowngradeMap.insert(std::make_pair(li->reg, *AS));
930   }
931   ++NumDowngrade;
932 }
933
934 void RALinScan::UpgradeRegister(unsigned Reg) {
935   if (Reg) {
936     DowngradedRegs.erase(Reg);
937     for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS)
938       DowngradedRegs.erase(*AS);
939   }
940 }
941
942 namespace {
943   struct LISorter {
944     bool operator()(LiveInterval* A, LiveInterval* B) {
945       return A->beginIndex() < B->beginIndex();
946     }
947   };
948 }
949
950 /// assignRegOrStackSlotAtInterval - assign a register if one is available, or
951 /// spill.
952 void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) {
953   DEBUG(dbgs() << "\tallocating current interval: ");
954
955   // This is an implicitly defined live interval, just assign any register.
956   const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
957   if (cur->empty()) {
958     unsigned physReg = vrm_->getRegAllocPref(cur->reg);
959     if (!physReg)
960       physReg = getFirstNonReservedPhysReg(RC);
961     DEBUG(dbgs() <<  tri_->getName(physReg) << '\n');
962     // Note the register is not really in use.
963     vrm_->assignVirt2Phys(cur->reg, physReg);
964     return;
965   }
966
967   backUpRegUses();
968
969   std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
970   SlotIndex StartPosition = cur->beginIndex();
971   const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
972
973   // If start of this live interval is defined by a move instruction and its
974   // source is assigned a physical register that is compatible with the target
975   // register class, then we should try to assign it the same register.
976   // This can happen when the move is from a larger register class to a smaller
977   // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
978   if (!vrm_->getRegAllocPref(cur->reg) && cur->hasAtLeastOneValue()) {
979     VNInfo *vni = cur->begin()->valno;
980     if (!vni->isUnused()) {
981       MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
982       if (CopyMI && CopyMI->isCopy()) {
983         unsigned DstSubReg = CopyMI->getOperand(0).getSubReg();
984         unsigned SrcReg = CopyMI->getOperand(1).getReg();
985         unsigned SrcSubReg = CopyMI->getOperand(1).getSubReg();
986         unsigned Reg = 0;
987         if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
988           Reg = SrcReg;
989         else if (vrm_->isAssignedReg(SrcReg))
990           Reg = vrm_->getPhys(SrcReg);
991         if (Reg) {
992           if (SrcSubReg)
993             Reg = tri_->getSubReg(Reg, SrcSubReg);
994           if (DstSubReg)
995             Reg = tri_->getMatchingSuperReg(Reg, DstSubReg, RC);
996           if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
997             mri_->setRegAllocationHint(cur->reg, 0, Reg);
998         }
999       }
1000     }
1001   }
1002
1003   // For every interval in inactive we overlap with, mark the
1004   // register as not free and update spill weights.
1005   for (IntervalPtrs::const_iterator i = inactive_.begin(),
1006          e = inactive_.end(); i != e; ++i) {
1007     unsigned Reg = i->first->reg;
1008     assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
1009            "Can only allocate virtual registers!");
1010     const TargetRegisterClass *RegRC = mri_->getRegClass(Reg);
1011     // If this is not in a related reg class to the register we're allocating,
1012     // don't check it.
1013     if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
1014         cur->overlapsFrom(*i->first, i->second-1)) {
1015       Reg = vrm_->getPhys(Reg);
1016       addRegUse(Reg);
1017       SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
1018     }
1019   }
1020
1021   // Speculatively check to see if we can get a register right now.  If not,
1022   // we know we won't be able to by adding more constraints.  If so, we can
1023   // check to see if it is valid.  Doing an exhaustive search of the fixed_ list
1024   // is very bad (it contains all callee clobbered registers for any functions
1025   // with a call), so we want to avoid doing that if possible.
1026   unsigned physReg = getFreePhysReg(cur);
1027   unsigned BestPhysReg = physReg;
1028   if (physReg) {
1029     // We got a register.  However, if it's in the fixed_ list, we might
1030     // conflict with it.  Check to see if we conflict with it or any of its
1031     // aliases.
1032     SmallSet<unsigned, 8> RegAliases;
1033     for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS)
1034       RegAliases.insert(*AS);
1035
1036     bool ConflictsWithFixed = false;
1037     for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
1038       IntervalPtr &IP = fixed_[i];
1039       if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
1040         // Okay, this reg is on the fixed list.  Check to see if we actually
1041         // conflict.
1042         LiveInterval *I = IP.first;
1043         if (I->endIndex() > StartPosition) {
1044           LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
1045           IP.second = II;
1046           if (II != I->begin() && II->start > StartPosition)
1047             --II;
1048           if (cur->overlapsFrom(*I, II)) {
1049             ConflictsWithFixed = true;
1050             break;
1051           }
1052         }
1053       }
1054     }
1055
1056     // Okay, the register picked by our speculative getFreePhysReg call turned
1057     // out to be in use.  Actually add all of the conflicting fixed registers to
1058     // regUse_ so we can do an accurate query.
1059     if (ConflictsWithFixed) {
1060       // For every interval in fixed we overlap with, mark the register as not
1061       // free and update spill weights.
1062       for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
1063         IntervalPtr &IP = fixed_[i];
1064         LiveInterval *I = IP.first;
1065
1066         const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
1067         if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
1068             I->endIndex() > StartPosition) {
1069           LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
1070           IP.second = II;
1071           if (II != I->begin() && II->start > StartPosition)
1072             --II;
1073           if (cur->overlapsFrom(*I, II)) {
1074             unsigned reg = I->reg;
1075             addRegUse(reg);
1076             SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
1077           }
1078         }
1079       }
1080
1081       // Using the newly updated regUse_ object, which includes conflicts in the
1082       // future, see if there are any registers available.
1083       physReg = getFreePhysReg(cur);
1084     }
1085   }
1086
1087   // Restore the physical register tracker, removing information about the
1088   // future.
1089   restoreRegUses();
1090
1091   // If we find a free register, we are done: assign this virtual to
1092   // the free physical register and add this interval to the active
1093   // list.
1094   if (physReg) {
1095     DEBUG(dbgs() <<  tri_->getName(physReg) << '\n');
1096     vrm_->assignVirt2Phys(cur->reg, physReg);
1097     addRegUse(physReg);
1098     active_.push_back(std::make_pair(cur, cur->begin()));
1099     handled_.push_back(cur);
1100
1101     // "Upgrade" the physical register since it has been allocated.
1102     UpgradeRegister(physReg);
1103     if (LiveInterval *NextReloadLI = hasNextReloadInterval(cur)) {
1104       // "Downgrade" physReg to try to keep physReg from being allocated until
1105       // the next reload from the same SS is allocated.
1106       mri_->setRegAllocationHint(NextReloadLI->reg, 0, physReg);
1107       DowngradeRegister(cur, physReg);
1108     }
1109     return;
1110   }
1111   DEBUG(dbgs() << "no free registers\n");
1112
1113   // Compile the spill weights into an array that is better for scanning.
1114   std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f);
1115   for (std::vector<std::pair<unsigned, float> >::iterator
1116        I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
1117     updateSpillWeights(SpillWeights, I->first, I->second, RC);
1118
1119   // for each interval in active, update spill weights.
1120   for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
1121        i != e; ++i) {
1122     unsigned reg = i->first->reg;
1123     assert(TargetRegisterInfo::isVirtualRegister(reg) &&
1124            "Can only allocate virtual registers!");
1125     reg = vrm_->getPhys(reg);
1126     updateSpillWeights(SpillWeights, reg, i->first->weight, RC);
1127   }
1128
1129   DEBUG(dbgs() << "\tassigning stack slot at interval "<< *cur << ":\n");
1130
1131   // Find a register to spill.
1132   float minWeight = HUGE_VALF;
1133   unsigned minReg = 0;
1134
1135   bool Found = false;
1136   std::vector<std::pair<unsigned,float> > RegsWeights;
1137   if (!minReg || SpillWeights[minReg] == HUGE_VALF)
1138     for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
1139            e = RC->allocation_order_end(*mf_); i != e; ++i) {
1140       unsigned reg = *i;
1141       float regWeight = SpillWeights[reg];
1142       // Don't even consider reserved regs.
1143       if (reservedRegs_.test(reg))
1144         continue;
1145       // Skip recently allocated registers and reserved registers.
1146       if (minWeight > regWeight && !isRecentlyUsed(reg))
1147         Found = true;
1148       RegsWeights.push_back(std::make_pair(reg, regWeight));
1149     }
1150
1151   // If we didn't find a register that is spillable, try aliases?
1152   if (!Found) {
1153     for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
1154            e = RC->allocation_order_end(*mf_); i != e; ++i) {
1155       unsigned reg = *i;
1156       if (reservedRegs_.test(reg))
1157         continue;
1158       // No need to worry about if the alias register size < regsize of RC.
1159       // We are going to spill all registers that alias it anyway.
1160       for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as)
1161         RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as]));
1162     }
1163   }
1164
1165   // Sort all potential spill candidates by weight.
1166   std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare(*this));
1167   minReg = RegsWeights[0].first;
1168   minWeight = RegsWeights[0].second;
1169   if (minWeight == HUGE_VALF) {
1170     // All registers must have inf weight. Just grab one!
1171     minReg = BestPhysReg ? BestPhysReg : getFirstNonReservedPhysReg(RC);
1172     if (cur->weight == HUGE_VALF ||
1173         li_->getApproximateInstructionCount(*cur) == 0) {
1174       // Spill a physical register around defs and uses.
1175       if (li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_)) {
1176         // spillPhysRegAroundRegDefsUses may have invalidated iterator stored
1177         // in fixed_. Reset them.
1178         for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
1179           IntervalPtr &IP = fixed_[i];
1180           LiveInterval *I = IP.first;
1181           if (I->reg == minReg || tri_->isSubRegister(minReg, I->reg))
1182             IP.second = I->advanceTo(I->begin(), StartPosition);
1183         }
1184
1185         DowngradedRegs.clear();
1186         assignRegOrStackSlotAtInterval(cur);
1187       } else {
1188         assert(false && "Ran out of registers during register allocation!");
1189         report_fatal_error("Ran out of registers during register allocation!");
1190       }
1191       return;
1192     }
1193   }
1194
1195   // Find up to 3 registers to consider as spill candidates.
1196   unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1;
1197   while (LastCandidate > 1) {
1198     if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight))
1199       break;
1200     --LastCandidate;
1201   }
1202
1203   DEBUG({
1204       dbgs() << "\t\tregister(s) with min weight(s): ";
1205
1206       for (unsigned i = 0; i != LastCandidate; ++i)
1207         dbgs() << tri_->getName(RegsWeights[i].first)
1208                << " (" << RegsWeights[i].second << ")\n";
1209     });
1210
1211   // If the current has the minimum weight, we need to spill it and
1212   // add any added intervals back to unhandled, and restart
1213   // linearscan.
1214   if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
1215     DEBUG(dbgs() << "\t\t\tspilling(c): " << *cur << '\n');
1216     SmallVector<LiveInterval*, 8> spillIs, added;
1217     spiller_->spill(cur, added, spillIs);
1218
1219     std::sort(added.begin(), added.end(), LISorter());
1220     if (added.empty())
1221       return;  // Early exit if all spills were folded.
1222
1223     // Merge added with unhandled.  Note that we have already sorted
1224     // intervals returned by addIntervalsForSpills by their starting
1225     // point.
1226     // This also update the NextReloadMap. That is, it adds mapping from a
1227     // register defined by a reload from SS to the next reload from SS in the
1228     // same basic block.
1229     MachineBasicBlock *LastReloadMBB = 0;
1230     LiveInterval *LastReload = 0;
1231     int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
1232     for (unsigned i = 0, e = added.size(); i != e; ++i) {
1233       LiveInterval *ReloadLi = added[i];
1234       if (ReloadLi->weight == HUGE_VALF &&
1235           li_->getApproximateInstructionCount(*ReloadLi) == 0) {
1236         SlotIndex ReloadIdx = ReloadLi->beginIndex();
1237         MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
1238         int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
1239         if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
1240           // Last reload of same SS is in the same MBB. We want to try to
1241           // allocate both reloads the same register and make sure the reg
1242           // isn't clobbered in between if at all possible.
1243           assert(LastReload->beginIndex() < ReloadIdx);
1244           NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
1245         }
1246         LastReloadMBB = ReloadMBB;
1247         LastReload = ReloadLi;
1248         LastReloadSS = ReloadSS;
1249       }
1250       unhandled_.push(ReloadLi);
1251     }
1252     return;
1253   }
1254
1255   ++NumBacktracks;
1256
1257   // Push the current interval back to unhandled since we are going
1258   // to re-run at least this iteration. Since we didn't modify it it
1259   // should go back right in the front of the list
1260   unhandled_.push(cur);
1261
1262   assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
1263          "did not choose a register to spill?");
1264
1265   // We spill all intervals aliasing the register with
1266   // minimum weight, rollback to the interval with the earliest
1267   // start point and let the linear scan algorithm run again
1268   SmallVector<LiveInterval*, 8> spillIs;
1269
1270   // Determine which intervals have to be spilled.
1271   findIntervalsToSpill(cur, RegsWeights, LastCandidate, spillIs);
1272
1273   // Set of spilled vregs (used later to rollback properly)
1274   SmallSet<unsigned, 8> spilled;
1275
1276   // The earliest start of a Spilled interval indicates up to where
1277   // in handled we need to roll back
1278   assert(!spillIs.empty() && "No spill intervals?");
1279   SlotIndex earliestStart = spillIs[0]->beginIndex();
1280
1281   // Spill live intervals of virtual regs mapped to the physical register we
1282   // want to clear (and its aliases).  We only spill those that overlap with the
1283   // current interval as the rest do not affect its allocation. we also keep
1284   // track of the earliest start of all spilled live intervals since this will
1285   // mark our rollback point.
1286   SmallVector<LiveInterval*, 8> added;
1287   while (!spillIs.empty()) {
1288     LiveInterval *sli = spillIs.back();
1289     spillIs.pop_back();
1290     DEBUG(dbgs() << "\t\t\tspilling(a): " << *sli << '\n');
1291     if (sli->beginIndex() < earliestStart)
1292       earliestStart = sli->beginIndex();
1293     spiller_->spill(sli, added, spillIs);
1294     spilled.insert(sli->reg);
1295   }
1296
1297   // Include any added intervals in earliestStart.
1298   for (unsigned i = 0, e = added.size(); i != e; ++i) {
1299     SlotIndex SI = added[i]->beginIndex();
1300     if (SI < earliestStart)
1301       earliestStart = SI;
1302   }
1303
1304   DEBUG(dbgs() << "\t\trolling back to: " << earliestStart << '\n');
1305
1306   // Scan handled in reverse order up to the earliest start of a
1307   // spilled live interval and undo each one, restoring the state of
1308   // unhandled.
1309   while (!handled_.empty()) {
1310     LiveInterval* i = handled_.back();
1311     // If this interval starts before t we are done.
1312     if (!i->empty() && i->beginIndex() < earliestStart)
1313       break;
1314     DEBUG(dbgs() << "\t\t\tundo changes for: " << *i << '\n');
1315     handled_.pop_back();
1316
1317     // When undoing a live interval allocation we must know if it is active or
1318     // inactive to properly update regUse_ and the VirtRegMap.
1319     IntervalPtrs::iterator it;
1320     if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
1321       active_.erase(it);
1322       assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
1323       if (!spilled.count(i->reg))
1324         unhandled_.push(i);
1325       delRegUse(vrm_->getPhys(i->reg));
1326       vrm_->clearVirt(i->reg);
1327     } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
1328       inactive_.erase(it);
1329       assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
1330       if (!spilled.count(i->reg))
1331         unhandled_.push(i);
1332       vrm_->clearVirt(i->reg);
1333     } else {
1334       assert(TargetRegisterInfo::isVirtualRegister(i->reg) &&
1335              "Can only allocate virtual registers!");
1336       vrm_->clearVirt(i->reg);
1337       unhandled_.push(i);
1338     }
1339
1340     DenseMap<unsigned, unsigned>::iterator ii = DowngradeMap.find(i->reg);
1341     if (ii == DowngradeMap.end())
1342       // It interval has a preference, it must be defined by a copy. Clear the
1343       // preference now since the source interval allocation may have been
1344       // undone as well.
1345       mri_->setRegAllocationHint(i->reg, 0, 0);
1346     else {
1347       UpgradeRegister(ii->second);
1348     }
1349   }
1350
1351   // Rewind the iterators in the active, inactive, and fixed lists back to the
1352   // point we reverted to.
1353   RevertVectorIteratorsTo(active_, earliestStart);
1354   RevertVectorIteratorsTo(inactive_, earliestStart);
1355   RevertVectorIteratorsTo(fixed_, earliestStart);
1356
1357   // Scan the rest and undo each interval that expired after t and
1358   // insert it in active (the next iteration of the algorithm will
1359   // put it in inactive if required)
1360   for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
1361     LiveInterval *HI = handled_[i];
1362     if (!HI->expiredAt(earliestStart) &&
1363         HI->expiredAt(cur->beginIndex())) {
1364       DEBUG(dbgs() << "\t\t\tundo changes for: " << *HI << '\n');
1365       active_.push_back(std::make_pair(HI, HI->begin()));
1366       assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg));
1367       addRegUse(vrm_->getPhys(HI->reg));
1368     }
1369   }
1370
1371   // Merge added with unhandled.
1372   // This also update the NextReloadMap. That is, it adds mapping from a
1373   // register defined by a reload from SS to the next reload from SS in the
1374   // same basic block.
1375   MachineBasicBlock *LastReloadMBB = 0;
1376   LiveInterval *LastReload = 0;
1377   int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
1378   std::sort(added.begin(), added.end(), LISorter());
1379   for (unsigned i = 0, e = added.size(); i != e; ++i) {
1380     LiveInterval *ReloadLi = added[i];
1381     if (ReloadLi->weight == HUGE_VALF &&
1382         li_->getApproximateInstructionCount(*ReloadLi) == 0) {
1383       SlotIndex ReloadIdx = ReloadLi->beginIndex();
1384       MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
1385       int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
1386       if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
1387         // Last reload of same SS is in the same MBB. We want to try to
1388         // allocate both reloads the same register and make sure the reg
1389         // isn't clobbered in between if at all possible.
1390         assert(LastReload->beginIndex() < ReloadIdx);
1391         NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
1392       }
1393       LastReloadMBB = ReloadMBB;
1394       LastReload = ReloadLi;
1395       LastReloadSS = ReloadSS;
1396     }
1397     unhandled_.push(ReloadLi);
1398   }
1399 }
1400
1401 unsigned RALinScan::getFreePhysReg(LiveInterval* cur,
1402                                    const TargetRegisterClass *RC,
1403                                    unsigned MaxInactiveCount,
1404                                    SmallVector<unsigned, 256> &inactiveCounts,
1405                                    bool SkipDGRegs) {
1406   unsigned FreeReg = 0;
1407   unsigned FreeRegInactiveCount = 0;
1408
1409   std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(cur->reg);
1410   // Resolve second part of the hint (if possible) given the current allocation.
1411   unsigned physReg = Hint.second;
1412   if (physReg &&
1413       TargetRegisterInfo::isVirtualRegister(physReg) && vrm_->hasPhys(physReg))
1414     physReg = vrm_->getPhys(physReg);
1415
1416   TargetRegisterClass::iterator I, E;
1417   tie(I, E) = tri_->getAllocationOrder(RC, Hint.first, physReg, *mf_);
1418   assert(I != E && "No allocatable register in this register class!");
1419
1420   // Scan for the first available register.
1421   for (; I != E; ++I) {
1422     unsigned Reg = *I;
1423     // Ignore "downgraded" registers.
1424     if (SkipDGRegs && DowngradedRegs.count(Reg))
1425       continue;
1426     // Skip reserved registers.
1427     if (reservedRegs_.test(Reg))
1428       continue;
1429     // Skip recently allocated registers.
1430     if (isRegAvail(Reg) && !isRecentlyUsed(Reg)) {
1431       FreeReg = Reg;
1432       if (FreeReg < inactiveCounts.size())
1433         FreeRegInactiveCount = inactiveCounts[FreeReg];
1434       else
1435         FreeRegInactiveCount = 0;
1436       break;
1437     }
1438   }
1439
1440   // If there are no free regs, or if this reg has the max inactive count,
1441   // return this register.
1442   if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) {
1443     // Remember what register we picked so we can skip it next time.
1444     if (FreeReg != 0) recordRecentlyUsed(FreeReg);
1445     return FreeReg;
1446   }
1447
1448   // Continue scanning the registers, looking for the one with the highest
1449   // inactive count.  Alkis found that this reduced register pressure very
1450   // slightly on X86 (in rev 1.94 of this file), though this should probably be
1451   // reevaluated now.
1452   for (; I != E; ++I) {
1453     unsigned Reg = *I;
1454     // Ignore "downgraded" registers.
1455     if (SkipDGRegs && DowngradedRegs.count(Reg))
1456       continue;
1457     // Skip reserved registers.
1458     if (reservedRegs_.test(Reg))
1459       continue;
1460     if (isRegAvail(Reg) && Reg < inactiveCounts.size() &&
1461         FreeRegInactiveCount < inactiveCounts[Reg] && !isRecentlyUsed(Reg)) {
1462       FreeReg = Reg;
1463       FreeRegInactiveCount = inactiveCounts[Reg];
1464       if (FreeRegInactiveCount == MaxInactiveCount)
1465         break;    // We found the one with the max inactive count.
1466     }
1467   }
1468
1469   // Remember what register we picked so we can skip it next time.
1470   recordRecentlyUsed(FreeReg);
1471
1472   return FreeReg;
1473 }
1474
1475 /// getFreePhysReg - return a free physical register for this virtual register
1476 /// interval if we have one, otherwise return 0.
1477 unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
1478   SmallVector<unsigned, 256> inactiveCounts;
1479   unsigned MaxInactiveCount = 0;
1480
1481   const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
1482   const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
1483
1484   for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
1485        i != e; ++i) {
1486     unsigned reg = i->first->reg;
1487     assert(TargetRegisterInfo::isVirtualRegister(reg) &&
1488            "Can only allocate virtual registers!");
1489
1490     // If this is not in a related reg class to the register we're allocating,
1491     // don't check it.
1492     const TargetRegisterClass *RegRC = mri_->getRegClass(reg);
1493     if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
1494       reg = vrm_->getPhys(reg);
1495       if (inactiveCounts.size() <= reg)
1496         inactiveCounts.resize(reg+1);
1497       ++inactiveCounts[reg];
1498       MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
1499     }
1500   }
1501
1502   // If copy coalescer has assigned a "preferred" register, check if it's
1503   // available first.
1504   unsigned Preference = vrm_->getRegAllocPref(cur->reg);
1505   if (Preference) {
1506     DEBUG(dbgs() << "(preferred: " << tri_->getName(Preference) << ") ");
1507     if (isRegAvail(Preference) &&
1508         RC->contains(Preference))
1509       return Preference;
1510   }
1511
1512   if (!DowngradedRegs.empty()) {
1513     unsigned FreeReg = getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts,
1514                                       true);
1515     if (FreeReg)
1516       return FreeReg;
1517   }
1518   return getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, false);
1519 }
1520
1521 FunctionPass* llvm::createLinearScanRegisterAllocator() {
1522   return new RALinScan();
1523 }