LLVM API Documentation

LiveInterval.cpp

Go to the documentation of this file.
00001 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the LiveRange and LiveInterval classes.  Given some
00011 // numbering of each the machine instructions an interval [i, j) is said to be a
00012 // live interval for register v if there is no instruction with number j' > j
00013 // such that v is live at j' abd there is no instruction with number i' < i such
00014 // that v is live at i'. In this implementation intervals can have holes,
00015 // i.e. an interval might look like [1,20), [50,65), [1000,1001).  Each
00016 // individual range is represented as an instance of LiveRange, and the whole
00017 // interval is represented as an instance of LiveInterval.
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #include "llvm/CodeGen/LiveInterval.h"
00022 #include "llvm/ADT/SmallSet.h"
00023 #include "llvm/ADT/STLExtras.h"
00024 #include "llvm/Support/Streams.h"
00025 #include "llvm/Target/TargetRegisterInfo.h"
00026 #include <algorithm>
00027 #include <ostream>
00028 using namespace llvm;
00029 
00030 // An example for liveAt():
00031 //
00032 // this = [1,4), liveAt(0) will return false. The instruction defining this
00033 // spans slots [0,3]. The interval belongs to an spilled definition of the
00034 // variable it represents. This is because slot 1 is used (def slot) and spans
00035 // up to slot 3 (store slot).
00036 //
00037 bool LiveInterval::liveAt(unsigned I) const {
00038   Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
00039 
00040   if (r == ranges.begin())
00041     return false;
00042 
00043   --r;
00044   return r->contains(I);
00045 }
00046 
00047 // liveBeforeAndAt - Check if the interval is live at the index and the index
00048 // just before it. If index is liveAt, check if it starts a new live range.
00049 // If it does, then check if the previous live range ends at index-1.
00050 bool LiveInterval::liveBeforeAndAt(unsigned I) const {
00051   Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
00052 
00053   if (r == ranges.begin())
00054     return false;
00055 
00056   --r;
00057   if (!r->contains(I))
00058     return false;
00059   if (I != r->start)
00060     return true;
00061   // I is the start of a live range. Check if the previous live range ends
00062   // at I-1.
00063   if (r == ranges.begin())
00064     return false;
00065   return r->end == I;
00066 }
00067 
00068 // overlaps - Return true if the intersection of the two live intervals is
00069 // not empty.
00070 //
00071 // An example for overlaps():
00072 //
00073 // 0: A = ...
00074 // 4: B = ...
00075 // 8: C = A + B ;; last use of A
00076 //
00077 // The live intervals should look like:
00078 //
00079 // A = [3, 11)
00080 // B = [7, x)
00081 // C = [11, y)
00082 //
00083 // A->overlaps(C) should return false since we want to be able to join
00084 // A and C.
00085 //
00086 bool LiveInterval::overlapsFrom(const LiveInterval& other,
00087                                 const_iterator StartPos) const {
00088   const_iterator i = begin();
00089   const_iterator ie = end();
00090   const_iterator j = StartPos;
00091   const_iterator je = other.end();
00092 
00093   assert((StartPos->start <= i->start || StartPos == other.begin()) &&
00094          StartPos != other.end() && "Bogus start position hint!");
00095 
00096   if (i->start < j->start) {
00097     i = std::upper_bound(i, ie, j->start);
00098     if (i != ranges.begin()) --i;
00099   } else if (j->start < i->start) {
00100     ++StartPos;
00101     if (StartPos != other.end() && StartPos->start <= i->start) {
00102       assert(StartPos < other.end() && i < end());
00103       j = std::upper_bound(j, je, i->start);
00104       if (j != other.ranges.begin()) --j;
00105     }
00106   } else {
00107     return true;
00108   }
00109 
00110   if (j == je) return false;
00111 
00112   while (i != ie) {
00113     if (i->start > j->start) {
00114       std::swap(i, j);
00115       std::swap(ie, je);
00116     }
00117 
00118     if (i->end > j->start)
00119       return true;
00120     ++i;
00121   }
00122 
00123   return false;
00124 }
00125 
00126 /// extendIntervalEndTo - This method is used when we want to extend the range
00127 /// specified by I to end at the specified endpoint.  To do this, we should
00128 /// merge and eliminate all ranges that this will overlap with.  The iterator is
00129 /// not invalidated.
00130 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
00131   assert(I != ranges.end() && "Not a valid interval!");
00132   VNInfo *ValNo = I->valno;
00133   unsigned OldEnd = I->end;
00134 
00135   // Search for the first interval that we can't merge with.
00136   Ranges::iterator MergeTo = next(I);
00137   for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
00138     assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
00139   }
00140 
00141   // If NewEnd was in the middle of an interval, make sure to get its endpoint.
00142   I->end = std::max(NewEnd, prior(MergeTo)->end);
00143 
00144   // Erase any dead ranges.
00145   ranges.erase(next(I), MergeTo);
00146 
00147   // Update kill info.
00148   removeKills(ValNo, OldEnd, I->end-1);
00149 
00150   // If the newly formed range now touches the range after it and if they have
00151   // the same value number, merge the two ranges into one range.
00152   Ranges::iterator Next = next(I);
00153   if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
00154     I->end = Next->end;
00155     ranges.erase(Next);
00156   }
00157 }
00158 
00159 
00160 /// extendIntervalStartTo - This method is used when we want to extend the range
00161 /// specified by I to start at the specified endpoint.  To do this, we should
00162 /// merge and eliminate all ranges that this will overlap with.
00163 LiveInterval::Ranges::iterator
00164 LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
00165   assert(I != ranges.end() && "Not a valid interval!");
00166   VNInfo *ValNo = I->valno;
00167 
00168   // Search for the first interval that we can't merge with.
00169   Ranges::iterator MergeTo = I;
00170   do {
00171     if (MergeTo == ranges.begin()) {
00172       I->start = NewStart;
00173       ranges.erase(MergeTo, I);
00174       return I;
00175     }
00176     assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
00177     --MergeTo;
00178   } while (NewStart <= MergeTo->start);
00179 
00180   // If we start in the middle of another interval, just delete a range and
00181   // extend that interval.
00182   if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
00183     MergeTo->end = I->end;
00184   } else {
00185     // Otherwise, extend the interval right after.
00186     ++MergeTo;
00187     MergeTo->start = NewStart;
00188     MergeTo->end = I->end;
00189   }
00190 
00191   ranges.erase(next(MergeTo), next(I));
00192   return MergeTo;
00193 }
00194 
00195 LiveInterval::iterator
00196 LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
00197   unsigned Start = LR.start, End = LR.end;
00198   iterator it = std::upper_bound(From, ranges.end(), Start);
00199 
00200   // If the inserted interval starts in the middle or right at the end of
00201   // another interval, just extend that interval to contain the range of LR.
00202   if (it != ranges.begin()) {
00203     iterator B = prior(it);
00204     if (LR.valno == B->valno) {
00205       if (B->start <= Start && B->end >= Start) {
00206         extendIntervalEndTo(B, End);
00207         return B;
00208       }
00209     } else {
00210       // Check to make sure that we are not overlapping two live ranges with
00211       // different valno's.
00212       assert(B->end <= Start &&
00213              "Cannot overlap two LiveRanges with differing ValID's"
00214              " (did you def the same reg twice in a MachineInstr?)");
00215     }
00216   }
00217 
00218   // Otherwise, if this range ends in the middle of, or right next to, another
00219   // interval, merge it into that interval.
00220   if (it != ranges.end()) {
00221     if (LR.valno == it->valno) {
00222       if (it->start <= End) {
00223         it = extendIntervalStartTo(it, Start);
00224 
00225         // If LR is a complete superset of an interval, we may need to grow its
00226         // endpoint as well.
00227         if (End > it->end)
00228           extendIntervalEndTo(it, End);
00229         else if (End < it->end)
00230           // Overlapping intervals, there might have been a kill here.
00231           removeKill(it->valno, End);
00232         return it;
00233       }
00234     } else {
00235       // Check to make sure that we are not overlapping two live ranges with
00236       // different valno's.
00237       assert(it->start >= End &&
00238              "Cannot overlap two LiveRanges with differing ValID's");
00239     }
00240   }
00241 
00242   // Otherwise, this is just a new range that doesn't interact with anything.
00243   // Insert it.
00244   return ranges.insert(it, LR);
00245 }
00246 
00247 /// isInOneLiveRange - Return true if the range specified is entirely in the
00248 /// a single LiveRange of the live interval.
00249 bool LiveInterval::isInOneLiveRange(unsigned Start, unsigned End) {
00250   Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
00251   if (I == ranges.begin())
00252     return false;
00253   --I;
00254   return I->contains(Start) && I->contains(End-1);
00255 }
00256 
00257 
00258 /// removeRange - Remove the specified range from this interval.  Note that
00259 /// the range must be in a single LiveRange in its entirety.
00260 void LiveInterval::removeRange(unsigned Start, unsigned End,
00261                                bool RemoveDeadValNo) {
00262   // Find the LiveRange containing this span.
00263   Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
00264   assert(I != ranges.begin() && "Range is not in interval!");
00265   --I;
00266   assert(I->contains(Start) && I->contains(End-1) &&
00267          "Range is not entirely in interval!");
00268 
00269   // If the span we are removing is at the start of the LiveRange, adjust it.
00270   VNInfo *ValNo = I->valno;
00271   if (I->start == Start) {
00272     if (I->end == End) {
00273       removeKills(I->valno, Start, End);
00274       if (RemoveDeadValNo) {
00275         // Check if val# is dead.
00276         bool isDead = true;
00277         for (const_iterator II = begin(), EE = end(); II != EE; ++II)
00278           if (II != I && II->valno == ValNo) {
00279             isDead = false;
00280             break;
00281           }          
00282         if (isDead) {
00283           // Now that ValNo is dead, remove it.  If it is the largest value
00284           // number, just nuke it (and any other deleted values neighboring it),
00285           // otherwise mark it as ~1U so it can be nuked later.
00286           if (ValNo->id == getNumValNums()-1) {
00287             do {
00288               VNInfo *VNI = valnos.back();
00289               valnos.pop_back();
00290               VNI->~VNInfo();
00291             } while (!valnos.empty() && valnos.back()->def == ~1U);
00292           } else {
00293             ValNo->def = ~1U;
00294           }
00295         }
00296       }
00297 
00298       ranges.erase(I);  // Removed the whole LiveRange.
00299     } else
00300       I->start = End;
00301     return;
00302   }
00303 
00304   // Otherwise if the span we are removing is at the end of the LiveRange,
00305   // adjust the other way.
00306   if (I->end == End) {
00307     removeKills(ValNo, Start, End);
00308     I->end = Start;
00309     return;
00310   }
00311 
00312   // Otherwise, we are splitting the LiveRange into two pieces.
00313   unsigned OldEnd = I->end;
00314   I->end = Start;   // Trim the old interval.
00315 
00316   // Insert the new one.
00317   ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
00318 }
00319 
00320 /// removeValNo - Remove all the ranges defined by the specified value#.
00321 /// Also remove the value# from value# list.
00322 void LiveInterval::removeValNo(VNInfo *ValNo) {
00323   if (empty()) return;
00324   Ranges::iterator I = ranges.end();
00325   Ranges::iterator E = ranges.begin();
00326   do {
00327     --I;
00328     if (I->valno == ValNo)
00329       ranges.erase(I);
00330   } while (I != E);
00331   // Now that ValNo is dead, remove it.  If it is the largest value
00332   // number, just nuke it (and any other deleted values neighboring it),
00333   // otherwise mark it as ~1U so it can be nuked later.
00334   if (ValNo->id == getNumValNums()-1) {
00335     do {
00336       VNInfo *VNI = valnos.back();
00337       valnos.pop_back();
00338       VNI->~VNInfo();
00339     } while (!valnos.empty() && valnos.back()->def == ~1U);
00340   } else {
00341     ValNo->def = ~1U;
00342   }
00343 }
00344  
00345 /// getLiveRangeContaining - Return the live range that contains the
00346 /// specified index, or null if there is none.
00347 LiveInterval::const_iterator 
00348 LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
00349   const_iterator It = std::upper_bound(begin(), end(), Idx);
00350   if (It != ranges.begin()) {
00351     --It;
00352     if (It->contains(Idx))
00353       return It;
00354   }
00355 
00356   return end();
00357 }
00358 
00359 LiveInterval::iterator 
00360 LiveInterval::FindLiveRangeContaining(unsigned Idx) {
00361   iterator It = std::upper_bound(begin(), end(), Idx);
00362   if (It != begin()) {
00363     --It;
00364     if (It->contains(Idx))
00365       return It;
00366   }
00367   
00368   return end();
00369 }
00370 
00371 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified index
00372 /// (register interval) or defined by the specified register (stack inteval).
00373 VNInfo *LiveInterval::findDefinedVNInfo(unsigned DefIdxOrReg) const {
00374   VNInfo *VNI = NULL;
00375   for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
00376        i != e; ++i)
00377     if ((*i)->def == DefIdxOrReg) {
00378       VNI = *i;
00379       break;
00380     }
00381   return VNI;
00382 }
00383 
00384 
00385 /// join - Join two live intervals (this, and other) together.  This applies
00386 /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
00387 /// the intervals are not joinable, this aborts.
00388 void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
00389                         const int *RHSValNoAssignments, 
00390                         SmallVector<VNInfo*, 16> &NewVNInfo) {
00391   // Determine if any of our live range values are mapped.  This is uncommon, so
00392   // we want to avoid the interval scan if not. 
00393   bool MustMapCurValNos = false;
00394   unsigned NumVals = getNumValNums();
00395   unsigned NumNewVals = NewVNInfo.size();
00396   for (unsigned i = 0; i != NumVals; ++i) {
00397     unsigned LHSValID = LHSValNoAssignments[i];
00398     if (i != LHSValID ||
00399         (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
00400       MustMapCurValNos = true;
00401   }
00402 
00403   // If we have to apply a mapping to our base interval assignment, rewrite it
00404   // now.
00405   if (MustMapCurValNos) {
00406     // Map the first live range.
00407     iterator OutIt = begin();
00408     OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
00409     ++OutIt;
00410     for (iterator I = OutIt, E = end(); I != E; ++I) {
00411       OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
00412       
00413       // If this live range has the same value # as its immediate predecessor,
00414       // and if they are neighbors, remove one LiveRange.  This happens when we
00415       // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
00416       if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
00417         (OutIt-1)->end = OutIt->end;
00418       } else {
00419         if (I != OutIt) {
00420           OutIt->start = I->start;
00421           OutIt->end = I->end;
00422         }
00423         
00424         // Didn't merge, on to the next one.
00425         ++OutIt;
00426       }
00427     }
00428     
00429     // If we merge some live ranges, chop off the end.
00430     ranges.erase(OutIt, end());
00431   }
00432 
00433   // Remember assignements because val# ids are changing.
00434   SmallVector<unsigned, 16> OtherAssignments;
00435   for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
00436     OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
00437 
00438   // Update val# info. Renumber them and make sure they all belong to this
00439   // LiveInterval now. Also remove dead val#'s.
00440   unsigned NumValNos = 0;
00441   for (unsigned i = 0; i < NumNewVals; ++i) {
00442     VNInfo *VNI = NewVNInfo[i];
00443     if (VNI) {
00444       if (i >= NumVals)
00445         valnos.push_back(VNI);
00446       else 
00447         valnos[NumValNos] = VNI;
00448       VNI->id = NumValNos++;  // Renumber val#.
00449     }
00450   }
00451   if (NumNewVals < NumVals)
00452     valnos.resize(NumNewVals);  // shrinkify
00453 
00454   // Okay, now insert the RHS live ranges into the LHS.
00455   iterator InsertPos = begin();
00456   unsigned RangeNo = 0;
00457   for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
00458     // Map the valno in the other live range to the current live range.
00459     I->valno = NewVNInfo[OtherAssignments[RangeNo]];
00460     assert(I->valno && "Adding a dead range?");
00461     InsertPos = addRangeFrom(*I, InsertPos);
00462   }
00463 
00464   weight += Other.weight;
00465   if (Other.preference && !preference)
00466     preference = Other.preference;
00467 }
00468 
00469 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
00470 /// interval as the specified value number.  The LiveRanges in RHS are
00471 /// allowed to overlap with LiveRanges in the current interval, but only if
00472 /// the overlapping LiveRanges have the specified value number.
00473 void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, 
00474                                         VNInfo *LHSValNo) {
00475   // TODO: Make this more efficient.
00476   iterator InsertPos = begin();
00477   for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
00478     // Map the valno in the other live range to the current live range.
00479     LiveRange Tmp = *I;
00480     Tmp.valno = LHSValNo;
00481     InsertPos = addRangeFrom(Tmp, InsertPos);
00482   }
00483 }
00484 
00485 
00486 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
00487 /// in RHS into this live interval as the specified value number.
00488 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
00489 /// current interval, it will replace the value numbers of the overlaped
00490 /// live ranges with the specified value number.
00491 void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
00492                                      const VNInfo *RHSValNo, VNInfo *LHSValNo) {
00493   SmallVector<VNInfo*, 4> ReplacedValNos;
00494   iterator IP = begin();
00495   for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
00496     if (I->valno != RHSValNo)
00497       continue;
00498     unsigned Start = I->start, End = I->end;
00499     IP = std::upper_bound(IP, end(), Start);
00500     // If the start of this range overlaps with an existing liverange, trim it.
00501     if (IP != begin() && IP[-1].end > Start) {
00502       if (IP[-1].valno != LHSValNo) {
00503         ReplacedValNos.push_back(IP[-1].valno);
00504         IP[-1].valno = LHSValNo; // Update val#.
00505       }
00506       Start = IP[-1].end;
00507       // Trimmed away the whole range?
00508       if (Start >= End) continue;
00509     }
00510     // If the end of this range overlaps with an existing liverange, trim it.
00511     if (IP != end() && End > IP->start) {
00512       if (IP->valno != LHSValNo) {
00513         ReplacedValNos.push_back(IP->valno);
00514         IP->valno = LHSValNo;  // Update val#.
00515       }
00516       End = IP->start;
00517       // If this trimmed away the whole range, ignore it.
00518       if (Start == End) continue;
00519     }
00520     
00521     // Map the valno in the other live range to the current live range.
00522     IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
00523   }
00524 
00525 
00526   SmallSet<VNInfo*, 4> Seen;
00527   for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
00528     VNInfo *V1 = ReplacedValNos[i];
00529     if (Seen.insert(V1)) {
00530       bool isDead = true;
00531       for (const_iterator I = begin(), E = end(); I != E; ++I)
00532         if (I->valno == V1) {
00533           isDead = false;
00534           break;
00535         }          
00536       if (isDead) {
00537         // Now that V1 is dead, remove it.  If it is the largest value number,
00538         // just nuke it (and any other deleted values neighboring it), otherwise
00539         // mark it as ~1U so it can be nuked later.
00540         if (V1->id == getNumValNums()-1) {
00541           do {
00542             VNInfo *VNI = valnos.back();
00543             valnos.pop_back();
00544             VNI->~VNInfo();
00545           } while (!valnos.empty() && valnos.back()->def == ~1U);
00546         } else {
00547           V1->def = ~1U;
00548         }
00549       }
00550     }
00551   }
00552 }
00553 
00554 
00555 /// MergeInClobberRanges - For any live ranges that are not defined in the
00556 /// current interval, but are defined in the Clobbers interval, mark them
00557 /// used with an unknown definition value.
00558 void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
00559                                         BumpPtrAllocator &VNInfoAllocator) {
00560   if (Clobbers.empty()) return;
00561   
00562   // Find a value # to use for the clobber ranges.  If there is already a value#
00563   // for unknown values, use it.
00564   // FIXME: Use a single sentinal number for these!
00565   VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
00566   
00567   iterator IP = begin();
00568   for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
00569     unsigned Start = I->start, End = I->end;
00570     IP = std::upper_bound(IP, end(), Start);
00571     
00572     // If the start of this range overlaps with an existing liverange, trim it.
00573     if (IP != begin() && IP[-1].end > Start) {
00574       Start = IP[-1].end;
00575       // Trimmed away the whole range?
00576       if (Start >= End) continue;
00577     }
00578     // If the end of this range overlaps with an existing liverange, trim it.
00579     if (IP != end() && End > IP->start) {
00580       End = IP->start;
00581       // If this trimmed away the whole range, ignore it.
00582       if (Start == End) continue;
00583     }
00584     
00585     // Insert the clobber interval.
00586     IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
00587   }
00588 }
00589 
00590 /// MergeValueNumberInto - This method is called when two value nubmers
00591 /// are found to be equivalent.  This eliminates V1, replacing all
00592 /// LiveRanges with the V1 value number with the V2 value number.  This can
00593 /// cause merging of V1/V2 values numbers and compaction of the value space.
00594 VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
00595   assert(V1 != V2 && "Identical value#'s are always equivalent!");
00596 
00597   // This code actually merges the (numerically) larger value number into the
00598   // smaller value number, which is likely to allow us to compactify the value
00599   // space.  The only thing we have to be careful of is to preserve the
00600   // instruction that defines the result value.
00601 
00602   // Make sure V2 is smaller than V1.
00603   if (V1->id < V2->id) {
00604     copyValNumInfo(V1, V2);
00605     std::swap(V1, V2);
00606   }
00607 
00608   // Merge V1 live ranges into V2.
00609   for (iterator I = begin(); I != end(); ) {
00610     iterator LR = I++;
00611     if (LR->valno != V1) continue;  // Not a V1 LiveRange.
00612     
00613     // Okay, we found a V1 live range.  If it had a previous, touching, V2 live
00614     // range, extend it.
00615     if (LR != begin()) {
00616       iterator Prev = LR-1;
00617       if (Prev->valno == V2 && Prev->end == LR->start) {
00618         Prev->end = LR->end;
00619 
00620         // Erase this live-range.
00621         ranges.erase(LR);
00622         I = Prev+1;
00623         LR = Prev;
00624       }
00625     }
00626     
00627     // Okay, now we have a V1 or V2 live range that is maximally merged forward.
00628     // Ensure that it is a V2 live-range.
00629     LR->valno = V2;
00630     
00631     // If we can merge it into later V2 live ranges, do so now.  We ignore any
00632     // following V1 live ranges, as they will be merged in subsequent iterations
00633     // of the loop.
00634     if (I != end()) {
00635       if (I->start == LR->end && I->valno == V2) {
00636         LR->end = I->end;
00637         ranges.erase(I);
00638         I = LR+1;
00639       }
00640     }
00641   }
00642   
00643   // Now that V1 is dead, remove it.  If it is the largest value number, just
00644   // nuke it (and any other deleted values neighboring it), otherwise mark it as
00645   // ~1U so it can be nuked later.
00646   if (V1->id == getNumValNums()-1) {
00647     do {
00648       VNInfo *VNI = valnos.back();
00649       valnos.pop_back();
00650       VNI->~VNInfo();
00651     } while (valnos.back()->def == ~1U);
00652   } else {
00653     V1->def = ~1U;
00654   }
00655   
00656   return V2;
00657 }
00658 
00659 void LiveInterval::Copy(const LiveInterval &RHS,
00660                         BumpPtrAllocator &VNInfoAllocator) {
00661   ranges.clear();
00662   valnos.clear();
00663   preference = RHS.preference;
00664   weight = RHS.weight;
00665   for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
00666     const VNInfo *VNI = RHS.getValNumInfo(i);
00667     VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
00668     copyValNumInfo(NewVNI, VNI);
00669   }
00670   for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
00671     const LiveRange &LR = RHS.ranges[i];
00672     addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
00673   }
00674 }
00675 
00676 unsigned LiveInterval::getSize() const {
00677   unsigned Sum = 0;
00678   for (const_iterator I = begin(), E = end(); I != E; ++I)
00679     Sum += I->end - I->start;
00680   return Sum;
00681 }
00682 
00683 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
00684   return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
00685 }
00686 
00687 void LiveRange::dump() const {
00688   cerr << *this << "\n";
00689 }
00690 
00691 void LiveInterval::print(std::ostream &OS,
00692                          const TargetRegisterInfo *TRI) const {
00693   if (isStackSlot())
00694     OS << "SS#" << getStackSlotIndex();
00695   else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
00696     OS << TRI->getName(reg);
00697   else
00698     OS << "%reg" << reg;
00699 
00700   OS << ',' << weight;
00701 
00702   if (empty())
00703     OS << " EMPTY";
00704   else {
00705     OS << " = ";
00706     for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
00707            E = ranges.end(); I != E; ++I)
00708     OS << *I;
00709   }
00710   
00711   // Print value number info.
00712   if (getNumValNums()) {
00713     OS << "  ";
00714     unsigned vnum = 0;
00715     for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
00716          ++i, ++vnum) {
00717       const VNInfo *vni = *i;
00718       if (vnum) OS << " ";
00719       OS << vnum << "@";
00720       if (vni->def == ~1U) {
00721         OS << "x";
00722       } else {
00723         if (vni->def == ~0U)
00724           OS << "?";
00725         else
00726           OS << vni->def;
00727         unsigned ee = vni->kills.size();
00728         if (ee || vni->hasPHIKill) {
00729           OS << "-(";
00730           for (unsigned j = 0; j != ee; ++j) {
00731             OS << vni->kills[j];
00732             if (j != ee-1)
00733               OS << " ";
00734           }
00735           if (vni->hasPHIKill) {
00736             if (ee)
00737               OS << " ";
00738             OS << "phi";
00739           }
00740           OS << ")";
00741         }
00742       }
00743     }
00744   }
00745 }
00746 
00747 void LiveInterval::dump() const {
00748   cerr << *this << "\n";
00749 }
00750 
00751 
00752 void LiveRange::print(std::ostream &os) const {
00753   os << *this;
00754 }