Home | History | Annotate | Download | only in CodeGen
      1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
      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 the LiveRange and LiveInterval classes.  Given some
     11 // numbering of each the machine instructions an interval [i, j) is said to be a
     12 // live interval for register v if there is no instruction with number j' > j
     13 // such that v is live at j' and there is no instruction with number i' < i such
     14 // that v is live at i'. In this implementation intervals can have holes,
     15 // i.e. an interval might look like [1,20), [50,65), [1000,1001).  Each
     16 // individual range is represented as an instance of LiveRange, and the whole
     17 // interval is represented as an instance of LiveInterval.
     18 //
     19 //===----------------------------------------------------------------------===//
     20 
     21 #include "llvm/CodeGen/LiveInterval.h"
     22 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
     23 #include "llvm/CodeGen/MachineRegisterInfo.h"
     24 #include "llvm/ADT/DenseMap.h"
     25 #include "llvm/ADT/SmallSet.h"
     26 #include "llvm/ADT/STLExtras.h"
     27 #include "llvm/Support/Debug.h"
     28 #include "llvm/Support/raw_ostream.h"
     29 #include "llvm/Target/TargetRegisterInfo.h"
     30 #include <algorithm>
     31 using namespace llvm;
     32 
     33 LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
     34   // This algorithm is basically std::upper_bound.
     35   // Unfortunately, std::upper_bound cannot be used with mixed types until we
     36   // adopt C++0x. Many libraries can do it, but not all.
     37   if (empty() || Pos >= endIndex())
     38     return end();
     39   iterator I = begin();
     40   size_t Len = ranges.size();
     41   do {
     42     size_t Mid = Len >> 1;
     43     if (Pos < I[Mid].end)
     44       Len = Mid;
     45     else
     46       I += Mid + 1, Len -= Mid + 1;
     47   } while (Len);
     48   return I;
     49 }
     50 
     51 /// killedInRange - Return true if the interval has kills in [Start,End).
     52 bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
     53   Ranges::const_iterator r =
     54     std::lower_bound(ranges.begin(), ranges.end(), End);
     55 
     56   // Now r points to the first interval with start >= End, or ranges.end().
     57   if (r == ranges.begin())
     58     return false;
     59 
     60   --r;
     61   // Now r points to the last interval with end <= End.
     62   // r->end is the kill point.
     63   return r->end >= Start && r->end < End;
     64 }
     65 
     66 // overlaps - Return true if the intersection of the two live intervals is
     67 // not empty.
     68 //
     69 // An example for overlaps():
     70 //
     71 // 0: A = ...
     72 // 4: B = ...
     73 // 8: C = A + B ;; last use of A
     74 //
     75 // The live intervals should look like:
     76 //
     77 // A = [3, 11)
     78 // B = [7, x)
     79 // C = [11, y)
     80 //
     81 // A->overlaps(C) should return false since we want to be able to join
     82 // A and C.
     83 //
     84 bool LiveInterval::overlapsFrom(const LiveInterval& other,
     85                                 const_iterator StartPos) const {
     86   assert(!empty() && "empty interval");
     87   const_iterator i = begin();
     88   const_iterator ie = end();
     89   const_iterator j = StartPos;
     90   const_iterator je = other.end();
     91 
     92   assert((StartPos->start <= i->start || StartPos == other.begin()) &&
     93          StartPos != other.end() && "Bogus start position hint!");
     94 
     95   if (i->start < j->start) {
     96     i = std::upper_bound(i, ie, j->start);
     97     if (i != ranges.begin()) --i;
     98   } else if (j->start < i->start) {
     99     ++StartPos;
    100     if (StartPos != other.end() && StartPos->start <= i->start) {
    101       assert(StartPos < other.end() && i < end());
    102       j = std::upper_bound(j, je, i->start);
    103       if (j != other.ranges.begin()) --j;
    104     }
    105   } else {
    106     return true;
    107   }
    108 
    109   if (j == je) return false;
    110 
    111   while (i != ie) {
    112     if (i->start > j->start) {
    113       std::swap(i, j);
    114       std::swap(ie, je);
    115     }
    116 
    117     if (i->end > j->start)
    118       return true;
    119     ++i;
    120   }
    121 
    122   return false;
    123 }
    124 
    125 /// overlaps - Return true if the live interval overlaps a range specified
    126 /// by [Start, End).
    127 bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
    128   assert(Start < End && "Invalid range");
    129   const_iterator I = std::lower_bound(begin(), end(), End);
    130   return I != begin() && (--I)->end > Start;
    131 }
    132 
    133 
    134 /// ValNo is dead, remove it.  If it is the largest value number, just nuke it
    135 /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
    136 /// it can be nuked later.
    137 void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
    138   if (ValNo->id == getNumValNums()-1) {
    139     do {
    140       valnos.pop_back();
    141     } while (!valnos.empty() && valnos.back()->isUnused());
    142   } else {
    143     ValNo->setIsUnused(true);
    144   }
    145 }
    146 
    147 /// RenumberValues - Renumber all values in order of appearance and delete the
    148 /// remaining unused values.
    149 void LiveInterval::RenumberValues(LiveIntervals &lis) {
    150   SmallPtrSet<VNInfo*, 8> Seen;
    151   bool seenPHIDef = false;
    152   valnos.clear();
    153   for (const_iterator I = begin(), E = end(); I != E; ++I) {
    154     VNInfo *VNI = I->valno;
    155     if (!Seen.insert(VNI))
    156       continue;
    157     assert(!VNI->isUnused() && "Unused valno used by live range");
    158     VNI->id = (unsigned)valnos.size();
    159     valnos.push_back(VNI);
    160     VNI->setHasPHIKill(false);
    161     if (VNI->isPHIDef())
    162       seenPHIDef = true;
    163   }
    164 
    165   // Recompute phi kill flags.
    166   if (!seenPHIDef)
    167     return;
    168   for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
    169     VNInfo *VNI = *I;
    170     if (!VNI->isPHIDef())
    171       continue;
    172     const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
    173     assert(PHIBB && "No basic block for phi-def");
    174     for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
    175          PE = PHIBB->pred_end(); PI != PE; ++PI) {
    176       VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
    177       if (KVNI)
    178         KVNI->setHasPHIKill(true);
    179     }
    180   }
    181 }
    182 
    183 /// extendIntervalEndTo - This method is used when we want to extend the range
    184 /// specified by I to end at the specified endpoint.  To do this, we should
    185 /// merge and eliminate all ranges that this will overlap with.  The iterator is
    186 /// not invalidated.
    187 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
    188   assert(I != ranges.end() && "Not a valid interval!");
    189   VNInfo *ValNo = I->valno;
    190 
    191   // Search for the first interval that we can't merge with.
    192   Ranges::iterator MergeTo = llvm::next(I);
    193   for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
    194     assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
    195   }
    196 
    197   // If NewEnd was in the middle of an interval, make sure to get its endpoint.
    198   I->end = std::max(NewEnd, prior(MergeTo)->end);
    199 
    200   // Erase any dead ranges.
    201   ranges.erase(llvm::next(I), MergeTo);
    202 
    203   // If the newly formed range now touches the range after it and if they have
    204   // the same value number, merge the two ranges into one range.
    205   Ranges::iterator Next = llvm::next(I);
    206   if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
    207     I->end = Next->end;
    208     ranges.erase(Next);
    209   }
    210 }
    211 
    212 
    213 /// extendIntervalStartTo - This method is used when we want to extend the range
    214 /// specified by I to start at the specified endpoint.  To do this, we should
    215 /// merge and eliminate all ranges that this will overlap with.
    216 LiveInterval::Ranges::iterator
    217 LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
    218   assert(I != ranges.end() && "Not a valid interval!");
    219   VNInfo *ValNo = I->valno;
    220 
    221   // Search for the first interval that we can't merge with.
    222   Ranges::iterator MergeTo = I;
    223   do {
    224     if (MergeTo == ranges.begin()) {
    225       I->start = NewStart;
    226       ranges.erase(MergeTo, I);
    227       return I;
    228     }
    229     assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
    230     --MergeTo;
    231   } while (NewStart <= MergeTo->start);
    232 
    233   // If we start in the middle of another interval, just delete a range and
    234   // extend that interval.
    235   if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
    236     MergeTo->end = I->end;
    237   } else {
    238     // Otherwise, extend the interval right after.
    239     ++MergeTo;
    240     MergeTo->start = NewStart;
    241     MergeTo->end = I->end;
    242   }
    243 
    244   ranges.erase(llvm::next(MergeTo), llvm::next(I));
    245   return MergeTo;
    246 }
    247 
    248 LiveInterval::iterator
    249 LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
    250   SlotIndex Start = LR.start, End = LR.end;
    251   iterator it = std::upper_bound(From, ranges.end(), Start);
    252 
    253   // If the inserted interval starts in the middle or right at the end of
    254   // another interval, just extend that interval to contain the range of LR.
    255   if (it != ranges.begin()) {
    256     iterator B = prior(it);
    257     if (LR.valno == B->valno) {
    258       if (B->start <= Start && B->end >= Start) {
    259         extendIntervalEndTo(B, End);
    260         return B;
    261       }
    262     } else {
    263       // Check to make sure that we are not overlapping two live ranges with
    264       // different valno's.
    265       assert(B->end <= Start &&
    266              "Cannot overlap two LiveRanges with differing ValID's"
    267              " (did you def the same reg twice in a MachineInstr?)");
    268     }
    269   }
    270 
    271   // Otherwise, if this range ends in the middle of, or right next to, another
    272   // interval, merge it into that interval.
    273   if (it != ranges.end()) {
    274     if (LR.valno == it->valno) {
    275       if (it->start <= End) {
    276         it = extendIntervalStartTo(it, Start);
    277 
    278         // If LR is a complete superset of an interval, we may need to grow its
    279         // endpoint as well.
    280         if (End > it->end)
    281           extendIntervalEndTo(it, End);
    282         return it;
    283       }
    284     } else {
    285       // Check to make sure that we are not overlapping two live ranges with
    286       // different valno's.
    287       assert(it->start >= End &&
    288              "Cannot overlap two LiveRanges with differing ValID's");
    289     }
    290   }
    291 
    292   // Otherwise, this is just a new range that doesn't interact with anything.
    293   // Insert it.
    294   return ranges.insert(it, LR);
    295 }
    296 
    297 /// extendInBlock - If this interval is live before UseIdx in the basic
    298 /// block that starts at StartIdx, extend it to be live at UseIdx and return
    299 /// the value. If there is no live range before UseIdx, return NULL.
    300 VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex UseIdx) {
    301   if (empty())
    302     return 0;
    303   iterator I = std::upper_bound(begin(), end(), UseIdx);
    304   if (I == begin())
    305     return 0;
    306   --I;
    307   if (I->end <= StartIdx)
    308     return 0;
    309   if (I->end <= UseIdx)
    310     extendIntervalEndTo(I, UseIdx.getNextSlot());
    311   return I->valno;
    312 }
    313 
    314 /// removeRange - Remove the specified range from this interval.  Note that
    315 /// the range must be in a single LiveRange in its entirety.
    316 void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
    317                                bool RemoveDeadValNo) {
    318   // Find the LiveRange containing this span.
    319   Ranges::iterator I = find(Start);
    320   assert(I != ranges.end() && "Range is not in interval!");
    321   assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
    322 
    323   // If the span we are removing is at the start of the LiveRange, adjust it.
    324   VNInfo *ValNo = I->valno;
    325   if (I->start == Start) {
    326     if (I->end == End) {
    327       if (RemoveDeadValNo) {
    328         // Check if val# is dead.
    329         bool isDead = true;
    330         for (const_iterator II = begin(), EE = end(); II != EE; ++II)
    331           if (II != I && II->valno == ValNo) {
    332             isDead = false;
    333             break;
    334           }
    335         if (isDead) {
    336           // Now that ValNo is dead, remove it.
    337           markValNoForDeletion(ValNo);
    338         }
    339       }
    340 
    341       ranges.erase(I);  // Removed the whole LiveRange.
    342     } else
    343       I->start = End;
    344     return;
    345   }
    346 
    347   // Otherwise if the span we are removing is at the end of the LiveRange,
    348   // adjust the other way.
    349   if (I->end == End) {
    350     I->end = Start;
    351     return;
    352   }
    353 
    354   // Otherwise, we are splitting the LiveRange into two pieces.
    355   SlotIndex OldEnd = I->end;
    356   I->end = Start;   // Trim the old interval.
    357 
    358   // Insert the new one.
    359   ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
    360 }
    361 
    362 /// removeValNo - Remove all the ranges defined by the specified value#.
    363 /// Also remove the value# from value# list.
    364 void LiveInterval::removeValNo(VNInfo *ValNo) {
    365   if (empty()) return;
    366   Ranges::iterator I = ranges.end();
    367   Ranges::iterator E = ranges.begin();
    368   do {
    369     --I;
    370     if (I->valno == ValNo)
    371       ranges.erase(I);
    372   } while (I != E);
    373   // Now that ValNo is dead, remove it.
    374   markValNoForDeletion(ValNo);
    375 }
    376 
    377 /// findDefinedVNInfo - Find the VNInfo defined by the specified
    378 /// index (register interval).
    379 VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
    380   for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
    381        i != e; ++i) {
    382     if ((*i)->def == Idx)
    383       return *i;
    384   }
    385 
    386   return 0;
    387 }
    388 
    389 /// join - Join two live intervals (this, and other) together.  This applies
    390 /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
    391 /// the intervals are not joinable, this aborts.
    392 void LiveInterval::join(LiveInterval &Other,
    393                         const int *LHSValNoAssignments,
    394                         const int *RHSValNoAssignments,
    395                         SmallVector<VNInfo*, 16> &NewVNInfo,
    396                         MachineRegisterInfo *MRI) {
    397   // Determine if any of our live range values are mapped.  This is uncommon, so
    398   // we want to avoid the interval scan if not.
    399   bool MustMapCurValNos = false;
    400   unsigned NumVals = getNumValNums();
    401   unsigned NumNewVals = NewVNInfo.size();
    402   for (unsigned i = 0; i != NumVals; ++i) {
    403     unsigned LHSValID = LHSValNoAssignments[i];
    404     if (i != LHSValID ||
    405         (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
    406       MustMapCurValNos = true;
    407   }
    408 
    409   // If we have to apply a mapping to our base interval assignment, rewrite it
    410   // now.
    411   if (MustMapCurValNos) {
    412     // Map the first live range.
    413     iterator OutIt = begin();
    414     OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
    415     ++OutIt;
    416     for (iterator I = OutIt, E = end(); I != E; ++I) {
    417       OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
    418 
    419       // If this live range has the same value # as its immediate predecessor,
    420       // and if they are neighbors, remove one LiveRange.  This happens when we
    421       // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
    422       if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
    423         (OutIt-1)->end = OutIt->end;
    424       } else {
    425         if (I != OutIt) {
    426           OutIt->start = I->start;
    427           OutIt->end = I->end;
    428         }
    429 
    430         // Didn't merge, on to the next one.
    431         ++OutIt;
    432       }
    433     }
    434 
    435     // If we merge some live ranges, chop off the end.
    436     ranges.erase(OutIt, end());
    437   }
    438 
    439   // Remember assignements because val# ids are changing.
    440   SmallVector<unsigned, 16> OtherAssignments;
    441   for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
    442     OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
    443 
    444   // Update val# info. Renumber them and make sure they all belong to this
    445   // LiveInterval now. Also remove dead val#'s.
    446   unsigned NumValNos = 0;
    447   for (unsigned i = 0; i < NumNewVals; ++i) {
    448     VNInfo *VNI = NewVNInfo[i];
    449     if (VNI) {
    450       if (NumValNos >= NumVals)
    451         valnos.push_back(VNI);
    452       else
    453         valnos[NumValNos] = VNI;
    454       VNI->id = NumValNos++;  // Renumber val#.
    455     }
    456   }
    457   if (NumNewVals < NumVals)
    458     valnos.resize(NumNewVals);  // shrinkify
    459 
    460   // Okay, now insert the RHS live ranges into the LHS.
    461   iterator InsertPos = begin();
    462   unsigned RangeNo = 0;
    463   for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
    464     // Map the valno in the other live range to the current live range.
    465     I->valno = NewVNInfo[OtherAssignments[RangeNo]];
    466     assert(I->valno && "Adding a dead range?");
    467     InsertPos = addRangeFrom(*I, InsertPos);
    468   }
    469 
    470   ComputeJoinedWeight(Other);
    471 }
    472 
    473 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
    474 /// interval as the specified value number.  The LiveRanges in RHS are
    475 /// allowed to overlap with LiveRanges in the current interval, but only if
    476 /// the overlapping LiveRanges have the specified value number.
    477 void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
    478                                         VNInfo *LHSValNo) {
    479   // TODO: Make this more efficient.
    480   iterator InsertPos = begin();
    481   for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
    482     // Map the valno in the other live range to the current live range.
    483     LiveRange Tmp = *I;
    484     Tmp.valno = LHSValNo;
    485     InsertPos = addRangeFrom(Tmp, InsertPos);
    486   }
    487 }
    488 
    489 
    490 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
    491 /// in RHS into this live interval as the specified value number.
    492 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
    493 /// current interval, it will replace the value numbers of the overlaped
    494 /// live ranges with the specified value number.
    495 void LiveInterval::MergeValueInAsValue(
    496                                     const LiveInterval &RHS,
    497                                     const VNInfo *RHSValNo, VNInfo *LHSValNo) {
    498   // TODO: Make this more efficient.
    499   iterator InsertPos = begin();
    500   for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
    501     if (I->valno != RHSValNo)
    502       continue;
    503     // Map the valno in the other live range to the current live range.
    504     LiveRange Tmp = *I;
    505     Tmp.valno = LHSValNo;
    506     InsertPos = addRangeFrom(Tmp, InsertPos);
    507   }
    508 }
    509 
    510 
    511 /// MergeValueNumberInto - This method is called when two value nubmers
    512 /// are found to be equivalent.  This eliminates V1, replacing all
    513 /// LiveRanges with the V1 value number with the V2 value number.  This can
    514 /// cause merging of V1/V2 values numbers and compaction of the value space.
    515 VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
    516   assert(V1 != V2 && "Identical value#'s are always equivalent!");
    517 
    518   // This code actually merges the (numerically) larger value number into the
    519   // smaller value number, which is likely to allow us to compactify the value
    520   // space.  The only thing we have to be careful of is to preserve the
    521   // instruction that defines the result value.
    522 
    523   // Make sure V2 is smaller than V1.
    524   if (V1->id < V2->id) {
    525     V1->copyFrom(*V2);
    526     std::swap(V1, V2);
    527   }
    528 
    529   // Merge V1 live ranges into V2.
    530   for (iterator I = begin(); I != end(); ) {
    531     iterator LR = I++;
    532     if (LR->valno != V1) continue;  // Not a V1 LiveRange.
    533 
    534     // Okay, we found a V1 live range.  If it had a previous, touching, V2 live
    535     // range, extend it.
    536     if (LR != begin()) {
    537       iterator Prev = LR-1;
    538       if (Prev->valno == V2 && Prev->end == LR->start) {
    539         Prev->end = LR->end;
    540 
    541         // Erase this live-range.
    542         ranges.erase(LR);
    543         I = Prev+1;
    544         LR = Prev;
    545       }
    546     }
    547 
    548     // Okay, now we have a V1 or V2 live range that is maximally merged forward.
    549     // Ensure that it is a V2 live-range.
    550     LR->valno = V2;
    551 
    552     // If we can merge it into later V2 live ranges, do so now.  We ignore any
    553     // following V1 live ranges, as they will be merged in subsequent iterations
    554     // of the loop.
    555     if (I != end()) {
    556       if (I->start == LR->end && I->valno == V2) {
    557         LR->end = I->end;
    558         ranges.erase(I);
    559         I = LR+1;
    560       }
    561     }
    562   }
    563 
    564   // Merge the relevant flags.
    565   V2->mergeFlags(V1);
    566 
    567   // Now that V1 is dead, remove it.
    568   markValNoForDeletion(V1);
    569 
    570   return V2;
    571 }
    572 
    573 void LiveInterval::Copy(const LiveInterval &RHS,
    574                         MachineRegisterInfo *MRI,
    575                         VNInfo::Allocator &VNInfoAllocator) {
    576   ranges.clear();
    577   valnos.clear();
    578   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
    579   MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
    580 
    581   weight = RHS.weight;
    582   for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
    583     const VNInfo *VNI = RHS.getValNumInfo(i);
    584     createValueCopy(VNI, VNInfoAllocator);
    585   }
    586   for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
    587     const LiveRange &LR = RHS.ranges[i];
    588     addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
    589   }
    590 }
    591 
    592 unsigned LiveInterval::getSize() const {
    593   unsigned Sum = 0;
    594   for (const_iterator I = begin(), E = end(); I != E; ++I)
    595     Sum += I->start.distance(I->end);
    596   return Sum;
    597 }
    598 
    599 /// ComputeJoinedWeight - Set the weight of a live interval Joined
    600 /// after Other has been merged into it.
    601 void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
    602   // If either of these intervals was spilled, the weight is the
    603   // weight of the non-spilled interval.  This can only happen with
    604   // iterative coalescers.
    605 
    606   if (Other.weight != HUGE_VALF) {
    607     weight += Other.weight;
    608   }
    609   else if (weight == HUGE_VALF &&
    610       !TargetRegisterInfo::isPhysicalRegister(reg)) {
    611     // Remove this assert if you have an iterative coalescer
    612     assert(0 && "Joining to spilled interval");
    613     weight = Other.weight;
    614   }
    615   else {
    616     // Otherwise the weight stays the same
    617     // Remove this assert if you have an iterative coalescer
    618     assert(0 && "Joining from spilled interval");
    619   }
    620 }
    621 
    622 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
    623   return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
    624 }
    625 
    626 void LiveRange::dump() const {
    627   dbgs() << *this << "\n";
    628 }
    629 
    630 void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
    631   OS << PrintReg(reg, TRI);
    632   if (weight != 0)
    633     OS << ',' << weight;
    634 
    635   if (empty())
    636     OS << " EMPTY";
    637   else {
    638     OS << " = ";
    639     for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
    640            E = ranges.end(); I != E; ++I) {
    641       OS << *I;
    642       assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
    643     }
    644   }
    645 
    646   // Print value number info.
    647   if (getNumValNums()) {
    648     OS << "  ";
    649     unsigned vnum = 0;
    650     for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
    651          ++i, ++vnum) {
    652       const VNInfo *vni = *i;
    653       if (vnum) OS << " ";
    654       OS << vnum << "@";
    655       if (vni->isUnused()) {
    656         OS << "x";
    657       } else {
    658         OS << vni->def;
    659         if (vni->isPHIDef())
    660           OS << "-phidef";
    661         if (vni->hasPHIKill())
    662           OS << "-phikill";
    663         if (vni->hasRedefByEC())
    664           OS << "-ec";
    665       }
    666     }
    667   }
    668 }
    669 
    670 void LiveInterval::dump() const {
    671   dbgs() << *this << "\n";
    672 }
    673 
    674 
    675 void LiveRange::print(raw_ostream &os) const {
    676   os << *this;
    677 }
    678 
    679 unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
    680   // Create initial equivalence classes.
    681   EqClass.clear();
    682   EqClass.grow(LI->getNumValNums());
    683 
    684   const VNInfo *used = 0, *unused = 0;
    685 
    686   // Determine connections.
    687   for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
    688        I != E; ++I) {
    689     const VNInfo *VNI = *I;
    690     // Group all unused values into one class.
    691     if (VNI->isUnused()) {
    692       if (unused)
    693         EqClass.join(unused->id, VNI->id);
    694       unused = VNI;
    695       continue;
    696     }
    697     used = VNI;
    698     if (VNI->isPHIDef()) {
    699       const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
    700       assert(MBB && "Phi-def has no defining MBB");
    701       // Connect to values live out of predecessors.
    702       for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
    703            PE = MBB->pred_end(); PI != PE; ++PI)
    704         if (const VNInfo *PVNI =
    705               LI->getVNInfoAt(LIS.getMBBEndIdx(*PI).getPrevSlot()))
    706           EqClass.join(VNI->id, PVNI->id);
    707     } else {
    708       // Normal value defined by an instruction. Check for two-addr redef.
    709       // FIXME: This could be coincidental. Should we really check for a tied
    710       // operand constraint?
    711       // Note that VNI->def may be a use slot for an early clobber def.
    712       if (const VNInfo *UVNI = LI->getVNInfoAt(VNI->def.getPrevSlot()))
    713         EqClass.join(VNI->id, UVNI->id);
    714     }
    715   }
    716 
    717   // Lump all the unused values in with the last used value.
    718   if (used && unused)
    719     EqClass.join(used->id, unused->id);
    720 
    721   EqClass.compress();
    722   return EqClass.getNumClasses();
    723 }
    724 
    725 void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
    726                                           MachineRegisterInfo &MRI) {
    727   assert(LIV[0] && "LIV[0] must be set");
    728   LiveInterval &LI = *LIV[0];
    729 
    730   // Rewrite instructions.
    731   for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
    732        RE = MRI.reg_end(); RI != RE;) {
    733     MachineOperand &MO = RI.getOperand();
    734     MachineInstr *MI = MO.getParent();
    735     ++RI;
    736     if (MO.isUse() && MO.isUndef())
    737       continue;
    738     // DBG_VALUE instructions should have been eliminated earlier.
    739     SlotIndex Idx = LIS.getInstructionIndex(MI);
    740     Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
    741     const VNInfo *VNI = LI.getVNInfoAt(Idx);
    742     assert(VNI && "Interval not live at use.");
    743     MO.setReg(LIV[getEqClass(VNI)]->reg);
    744   }
    745 
    746   // Move runs to new intervals.
    747   LiveInterval::iterator J = LI.begin(), E = LI.end();
    748   while (J != E && EqClass[J->valno->id] == 0)
    749     ++J;
    750   for (LiveInterval::iterator I = J; I != E; ++I) {
    751     if (unsigned eq = EqClass[I->valno->id]) {
    752       assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
    753              "New intervals should be empty");
    754       LIV[eq]->ranges.push_back(*I);
    755     } else
    756       *J++ = *I;
    757   }
    758   LI.ranges.erase(J, E);
    759 
    760   // Transfer VNInfos to their new owners and renumber them.
    761   unsigned j = 0, e = LI.getNumValNums();
    762   while (j != e && EqClass[j] == 0)
    763     ++j;
    764   for (unsigned i = j; i != e; ++i) {
    765     VNInfo *VNI = LI.getValNumInfo(i);
    766     if (unsigned eq = EqClass[i]) {
    767       VNI->id = LIV[eq]->getNumValNums();
    768       LIV[eq]->valnos.push_back(VNI);
    769     } else {
    770       VNI->id = j;
    771       LI.valnos[j++] = VNI;
    772     }
    773   }
    774   LI.valnos.resize(j);
    775 }
    776