LiveInterval.cpp revision 84315f03cb6127ac92a5b02239c6edc2a347069a
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>
31using namespace llvm;
32
33LiveInterval::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
51VNInfo *LiveInterval::createDeadDef(SlotIndex Def,
52                                    VNInfo::Allocator &VNInfoAllocator) {
53  assert(!Def.isDead() && "Cannot define a value at the dead slot");
54  iterator I = find(Def);
55  if (I == end()) {
56    VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
57    ranges.push_back(LiveRange(Def, Def.getDeadSlot(), VNI));
58    return VNI;
59  }
60  if (SlotIndex::isSameInstr(Def, I->start)) {
61    assert(I->start == Def && "Cannot insert def, already live");
62    assert(I->valno->def == Def && "Inconsistent existing value def");
63    return I->valno;
64  }
65  assert(SlotIndex::isEarlierInstr(Def, I->start) && "Already live at def");
66  VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
67  ranges.insert(I, LiveRange(Def, Def.getDeadSlot(), VNI));
68  return VNI;
69}
70
71/// killedInRange - Return true if the interval has kills in [Start,End).
72bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
73  Ranges::const_iterator r =
74    std::lower_bound(ranges.begin(), ranges.end(), End);
75
76  // Now r points to the first interval with start >= End, or ranges.end().
77  if (r == ranges.begin())
78    return false;
79
80  --r;
81  // Now r points to the last interval with end <= End.
82  // r->end is the kill point.
83  return r->end >= Start && r->end < End;
84}
85
86// overlaps - Return true if the intersection of the two live intervals is
87// not empty.
88//
89// An example for overlaps():
90//
91// 0: A = ...
92// 4: B = ...
93// 8: C = A + B ;; last use of A
94//
95// The live intervals should look like:
96//
97// A = [3, 11)
98// B = [7, x)
99// C = [11, y)
100//
101// A->overlaps(C) should return false since we want to be able to join
102// A and C.
103//
104bool LiveInterval::overlapsFrom(const LiveInterval& other,
105                                const_iterator StartPos) const {
106  assert(!empty() && "empty interval");
107  const_iterator i = begin();
108  const_iterator ie = end();
109  const_iterator j = StartPos;
110  const_iterator je = other.end();
111
112  assert((StartPos->start <= i->start || StartPos == other.begin()) &&
113         StartPos != other.end() && "Bogus start position hint!");
114
115  if (i->start < j->start) {
116    i = std::upper_bound(i, ie, j->start);
117    if (i != ranges.begin()) --i;
118  } else if (j->start < i->start) {
119    ++StartPos;
120    if (StartPos != other.end() && StartPos->start <= i->start) {
121      assert(StartPos < other.end() && i < end());
122      j = std::upper_bound(j, je, i->start);
123      if (j != other.ranges.begin()) --j;
124    }
125  } else {
126    return true;
127  }
128
129  if (j == je) return false;
130
131  while (i != ie) {
132    if (i->start > j->start) {
133      std::swap(i, j);
134      std::swap(ie, je);
135    }
136
137    if (i->end > j->start)
138      return true;
139    ++i;
140  }
141
142  return false;
143}
144
145/// overlaps - Return true if the live interval overlaps a range specified
146/// by [Start, End).
147bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
148  assert(Start < End && "Invalid range");
149  const_iterator I = std::lower_bound(begin(), end(), End);
150  return I != begin() && (--I)->end > Start;
151}
152
153
154/// ValNo is dead, remove it.  If it is the largest value number, just nuke it
155/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
156/// it can be nuked later.
157void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
158  if (ValNo->id == getNumValNums()-1) {
159    do {
160      valnos.pop_back();
161    } while (!valnos.empty() && valnos.back()->isUnused());
162  } else {
163    ValNo->setIsUnused(true);
164  }
165}
166
167/// RenumberValues - Renumber all values in order of appearance and delete the
168/// remaining unused values.
169void LiveInterval::RenumberValues(LiveIntervals &lis) {
170  SmallPtrSet<VNInfo*, 8> Seen;
171  valnos.clear();
172  for (const_iterator I = begin(), E = end(); I != E; ++I) {
173    VNInfo *VNI = I->valno;
174    if (!Seen.insert(VNI))
175      continue;
176    assert(!VNI->isUnused() && "Unused valno used by live range");
177    VNI->id = (unsigned)valnos.size();
178    valnos.push_back(VNI);
179  }
180}
181
182/// extendIntervalEndTo - This method is used when we want to extend the range
183/// specified by I to end at the specified endpoint.  To do this, we should
184/// merge and eliminate all ranges that this will overlap with.  The iterator is
185/// not invalidated.
186void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
187  assert(I != ranges.end() && "Not a valid interval!");
188  VNInfo *ValNo = I->valno;
189
190  // Search for the first interval that we can't merge with.
191  Ranges::iterator MergeTo = llvm::next(I);
192  for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
193    assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
194  }
195
196  // If NewEnd was in the middle of an interval, make sure to get its endpoint.
197  I->end = std::max(NewEnd, prior(MergeTo)->end);
198
199  // If the newly formed range now touches the range after it and if they have
200  // the same value number, merge the two ranges into one range.
201  if (MergeTo != ranges.end() && MergeTo->start <= I->end &&
202      MergeTo->valno == ValNo) {
203    I->end = MergeTo->end;
204    ++MergeTo;
205  }
206
207  // Erase any dead ranges.
208  ranges.erase(llvm::next(I), MergeTo);
209}
210
211
212/// extendIntervalStartTo - This method is used when we want to extend the range
213/// specified by I to start at the specified endpoint.  To do this, we should
214/// merge and eliminate all ranges that this will overlap with.
215LiveInterval::Ranges::iterator
216LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
217  assert(I != ranges.end() && "Not a valid interval!");
218  VNInfo *ValNo = I->valno;
219
220  // Search for the first interval that we can't merge with.
221  Ranges::iterator MergeTo = I;
222  do {
223    if (MergeTo == ranges.begin()) {
224      I->start = NewStart;
225      ranges.erase(MergeTo, I);
226      return I;
227    }
228    assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
229    --MergeTo;
230  } while (NewStart <= MergeTo->start);
231
232  // If we start in the middle of another interval, just delete a range and
233  // extend that interval.
234  if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
235    MergeTo->end = I->end;
236  } else {
237    // Otherwise, extend the interval right after.
238    ++MergeTo;
239    MergeTo->start = NewStart;
240    MergeTo->end = I->end;
241  }
242
243  ranges.erase(llvm::next(MergeTo), llvm::next(I));
244  return MergeTo;
245}
246
247LiveInterval::iterator
248LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
249  SlotIndex Start = LR.start, End = LR.end;
250  iterator it = std::upper_bound(From, ranges.end(), Start);
251
252  // If the inserted interval starts in the middle or right at the end of
253  // another interval, just extend that interval to contain the range of LR.
254  if (it != ranges.begin()) {
255    iterator B = prior(it);
256    if (LR.valno == B->valno) {
257      if (B->start <= Start && B->end >= Start) {
258        extendIntervalEndTo(B, End);
259        return B;
260      }
261    } else {
262      // Check to make sure that we are not overlapping two live ranges with
263      // different valno's.
264      assert(B->end <= Start &&
265             "Cannot overlap two LiveRanges with differing ValID's"
266             " (did you def the same reg twice in a MachineInstr?)");
267    }
268  }
269
270  // Otherwise, if this range ends in the middle of, or right next to, another
271  // interval, merge it into that interval.
272  if (it != ranges.end()) {
273    if (LR.valno == it->valno) {
274      if (it->start <= End) {
275        it = extendIntervalStartTo(it, Start);
276
277        // If LR is a complete superset of an interval, we may need to grow its
278        // endpoint as well.
279        if (End > it->end)
280          extendIntervalEndTo(it, End);
281        return it;
282      }
283    } else {
284      // Check to make sure that we are not overlapping two live ranges with
285      // different valno's.
286      assert(it->start >= End &&
287             "Cannot overlap two LiveRanges with differing ValID's");
288    }
289  }
290
291  // Otherwise, this is just a new range that doesn't interact with anything.
292  // Insert it.
293  return ranges.insert(it, LR);
294}
295
296/// extendInBlock - If this interval is live before Kill in the basic
297/// block that starts at StartIdx, extend it to be live up to Kill and return
298/// the value. If there is no live range before Kill, return NULL.
299VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
300  if (empty())
301    return 0;
302  iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot());
303  if (I == begin())
304    return 0;
305  --I;
306  if (I->end <= StartIdx)
307    return 0;
308  if (I->end < Kill)
309    extendIntervalEndTo(I, Kill);
310  return I->valno;
311}
312
313/// removeRange - Remove the specified range from this interval.  Note that
314/// the range must be in a single LiveRange in its entirety.
315void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
316                               bool RemoveDeadValNo) {
317  // Find the LiveRange containing this span.
318  Ranges::iterator I = find(Start);
319  assert(I != ranges.end() && "Range is not in interval!");
320  assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
321
322  // If the span we are removing is at the start of the LiveRange, adjust it.
323  VNInfo *ValNo = I->valno;
324  if (I->start == Start) {
325    if (I->end == End) {
326      if (RemoveDeadValNo) {
327        // Check if val# is dead.
328        bool isDead = true;
329        for (const_iterator II = begin(), EE = end(); II != EE; ++II)
330          if (II != I && II->valno == ValNo) {
331            isDead = false;
332            break;
333          }
334        if (isDead) {
335          // Now that ValNo is dead, remove it.
336          markValNoForDeletion(ValNo);
337        }
338      }
339
340      ranges.erase(I);  // Removed the whole LiveRange.
341    } else
342      I->start = End;
343    return;
344  }
345
346  // Otherwise if the span we are removing is at the end of the LiveRange,
347  // adjust the other way.
348  if (I->end == End) {
349    I->end = Start;
350    return;
351  }
352
353  // Otherwise, we are splitting the LiveRange into two pieces.
354  SlotIndex OldEnd = I->end;
355  I->end = Start;   // Trim the old interval.
356
357  // Insert the new one.
358  ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
359}
360
361/// removeValNo - Remove all the ranges defined by the specified value#.
362/// Also remove the value# from value# list.
363void LiveInterval::removeValNo(VNInfo *ValNo) {
364  if (empty()) return;
365  Ranges::iterator I = ranges.end();
366  Ranges::iterator E = ranges.begin();
367  do {
368    --I;
369    if (I->valno == ValNo)
370      ranges.erase(I);
371  } while (I != E);
372  // Now that ValNo is dead, remove it.
373  markValNoForDeletion(ValNo);
374}
375
376/// join - Join two live intervals (this, and other) together.  This applies
377/// mappings to the value numbers in the LHS/RHS intervals as specified.  If
378/// the intervals are not joinable, this aborts.
379void LiveInterval::join(LiveInterval &Other,
380                        const int *LHSValNoAssignments,
381                        const int *RHSValNoAssignments,
382                        SmallVector<VNInfo*, 16> &NewVNInfo,
383                        MachineRegisterInfo *MRI) {
384  verify();
385
386  // Determine if any of our live range values are mapped.  This is uncommon, so
387  // we want to avoid the interval scan if not.
388  bool MustMapCurValNos = false;
389  unsigned NumVals = getNumValNums();
390  unsigned NumNewVals = NewVNInfo.size();
391  for (unsigned i = 0; i != NumVals; ++i) {
392    unsigned LHSValID = LHSValNoAssignments[i];
393    if (i != LHSValID ||
394        (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
395      MustMapCurValNos = true;
396      break;
397    }
398  }
399
400  // If we have to apply a mapping to our base interval assignment, rewrite it
401  // now.
402  if (MustMapCurValNos) {
403    // Map the first live range.
404
405    iterator OutIt = begin();
406    OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
407    for (iterator I = next(OutIt), E = end(); I != E; ++I) {
408      VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
409      assert(nextValNo != 0 && "Huh?");
410
411      // If this live range has the same value # as its immediate predecessor,
412      // and if they are neighbors, remove one LiveRange.  This happens when we
413      // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
414      if (OutIt->valno == nextValNo && OutIt->end == I->start) {
415        OutIt->end = I->end;
416      } else {
417        // Didn't merge. Move OutIt to the next interval,
418        ++OutIt;
419        OutIt->valno = nextValNo;
420        if (OutIt != I) {
421          OutIt->start = I->start;
422          OutIt->end = I->end;
423        }
424      }
425    }
426    // If we merge some live ranges, chop off the end.
427    ++OutIt;
428    ranges.erase(OutIt, end());
429  }
430
431  // Remember assignements because val# ids are changing.
432  SmallVector<unsigned, 16> OtherAssignments;
433  for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
434    OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
435
436  // Update val# info. Renumber them and make sure they all belong to this
437  // LiveInterval now. Also remove dead val#'s.
438  unsigned NumValNos = 0;
439  for (unsigned i = 0; i < NumNewVals; ++i) {
440    VNInfo *VNI = NewVNInfo[i];
441    if (VNI) {
442      if (NumValNos >= NumVals)
443        valnos.push_back(VNI);
444      else
445        valnos[NumValNos] = VNI;
446      VNI->id = NumValNos++;  // Renumber val#.
447    }
448  }
449  if (NumNewVals < NumVals)
450    valnos.resize(NumNewVals);  // shrinkify
451
452  // Okay, now insert the RHS live ranges into the LHS.
453  unsigned RangeNo = 0;
454  for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
455    // Map the valno in the other live range to the current live range.
456    I->valno = NewVNInfo[OtherAssignments[RangeNo]];
457    assert(I->valno && "Adding a dead range?");
458  }
459  mergeIntervalRanges(Other);
460
461  verify();
462}
463
464/// \brief Helper function for merging in another LiveInterval's ranges.
465///
466/// This is a helper routine implementing an efficient merge of another
467/// LiveIntervals ranges into the current interval.
468///
469/// \param LHSValNo If non-NULL, set as the new value number for every range
470///                 from RHS which is merged into the LHS.
471/// \param RHSValNo If non-NULL, then only ranges in RHS whose original value
472///                 number maches this value number will be merged into LHS.
473void LiveInterval::mergeIntervalRanges(const LiveInterval &RHS,
474                                       VNInfo *LHSValNo,
475                                       const VNInfo *RHSValNo) {
476  if (RHS.empty())
477    return;
478
479  // Ensure we're starting with a valid range. Note that we don't verify RHS
480  // because it may have had its value numbers adjusted in preparation for
481  // merging.
482  verify();
483
484  // The strategy for merging these efficiently is as follows:
485  //
486  // 1) Find the beginning of the impacted ranges in the LHS.
487  // 2) Create a new, merged sub-squence of ranges merging from the position in
488  //    #1 until either LHS or RHS is exhausted. Any part of LHS between RHS
489  //    entries being merged will be copied into this new range.
490  // 3) Replace the relevant section in LHS with these newly merged ranges.
491  // 4) Append any remaning ranges from RHS if LHS is exhausted in #2.
492  //
493  // We don't follow the typical in-place merge strategy for sorted ranges of
494  // appending the new ranges to the back and then using std::inplace_merge
495  // because one step of the merge can both mutate the original elements and
496  // remove elements from the original. Essentially, because the merge includes
497  // collapsing overlapping ranges, a more complex approach is required.
498
499  // We do an initial binary search to optimize for a common pattern: a large
500  // LHS, and a very small RHS.
501  const_iterator RI = RHS.begin(), RE = RHS.end();
502  iterator LE = end(), LI = std::upper_bound(begin(), LE, *RI);
503
504  // Merge into NewRanges until one of the ranges is exhausted.
505  SmallVector<LiveRange, 4> NewRanges;
506
507  // Keep track of where to begin the replacement.
508  iterator ReplaceI = LI;
509
510  // If there are preceding ranges in the LHS, put the last one into NewRanges
511  // so we can optionally extend it. Adjust the replacement point accordingly.
512  if (LI != begin()) {
513    ReplaceI = llvm::prior(LI);
514    NewRanges.push_back(*ReplaceI);
515  }
516
517  // Now loop over the mergable portions of both LHS and RHS, merging into
518  // NewRanges.
519  while (LI != LE && RI != RE) {
520    // Skip incoming ranges with the wrong value.
521    if (RHSValNo && RI->valno != RHSValNo) {
522      ++RI;
523      continue;
524    }
525
526    // Select the first range. We pick the earliest start point, and then the
527    // largest range.
528    LiveRange R = *LI;
529    if (*RI < R) {
530      R = *RI;
531      ++RI;
532      if (LHSValNo)
533        R.valno = LHSValNo;
534    } else {
535      ++LI;
536    }
537
538    if (NewRanges.empty()) {
539      NewRanges.push_back(R);
540      continue;
541    }
542
543    LiveRange &LastR = NewRanges.back();
544    if (R.valno == LastR.valno) {
545      // Try to merge this range into the last one.
546      if (R.start <= LastR.end) {
547        LastR.end = std::max(LastR.end, R.end);
548        continue;
549      }
550    } else {
551      // We can't merge ranges across a value number.
552      assert(R.start >= LastR.end &&
553             "Cannot overlap two LiveRanges with differing ValID's");
554    }
555
556    // If all else fails, just append the range.
557    NewRanges.push_back(R);
558  }
559  assert(RI == RE || LI == LE);
560
561  // Check for being able to merge into the trailing sequence of ranges on the LHS.
562  if (!NewRanges.empty())
563    for (; LI != LE && (LI->valno == NewRanges.back().valno &&
564                        LI->start <= NewRanges.back().end);
565         ++LI)
566      NewRanges.back().end = std::max(NewRanges.back().end, LI->end);
567
568  // Replace the ranges in the LHS with the newly merged ones. It would be
569  // really nice if there were a move-supporting 'replace' directly in
570  // SmallVector, but as there is not, we pay the price of copies to avoid
571  // wasted memory allocations.
572  SmallVectorImpl<LiveRange>::iterator NRI = NewRanges.begin(),
573                                       NRE = NewRanges.end();
574  for (; ReplaceI != LI && NRI != NRE; ++ReplaceI, ++NRI)
575    *ReplaceI = *NRI;
576  if (NRI == NRE)
577    ranges.erase(ReplaceI, LI);
578  else
579    ranges.insert(LI, NRI, NRE);
580
581  // And finally insert any trailing end of RHS (if we have one).
582  for (; RI != RE; ++RI) {
583    LiveRange R = *RI;
584    if (LHSValNo)
585      R.valno = LHSValNo;
586    if (!ranges.empty() &&
587        ranges.back().valno == R.valno && R.start <= ranges.back().end)
588      ranges.back().end = std::max(ranges.back().end, R.end);
589    else
590      ranges.push_back(R);
591  }
592
593  // Ensure we finished with a valid new sequence of ranges.
594  verify();
595}
596
597/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
598/// interval as the specified value number.  The LiveRanges in RHS are
599/// allowed to overlap with LiveRanges in the current interval, but only if
600/// the overlapping LiveRanges have the specified value number.
601void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
602                                        VNInfo *LHSValNo) {
603  mergeIntervalRanges(RHS, LHSValNo);
604}
605
606/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
607/// in RHS into this live interval as the specified value number.
608/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
609/// current interval, it will replace the value numbers of the overlaped
610/// live ranges with the specified value number.
611void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
612                                       const VNInfo *RHSValNo,
613                                       VNInfo *LHSValNo) {
614  mergeIntervalRanges(RHS, LHSValNo, RHSValNo);
615}
616
617/// MergeValueNumberInto - This method is called when two value nubmers
618/// are found to be equivalent.  This eliminates V1, replacing all
619/// LiveRanges with the V1 value number with the V2 value number.  This can
620/// cause merging of V1/V2 values numbers and compaction of the value space.
621VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
622  assert(V1 != V2 && "Identical value#'s are always equivalent!");
623
624  // This code actually merges the (numerically) larger value number into the
625  // smaller value number, which is likely to allow us to compactify the value
626  // space.  The only thing we have to be careful of is to preserve the
627  // instruction that defines the result value.
628
629  // Make sure V2 is smaller than V1.
630  if (V1->id < V2->id) {
631    V1->copyFrom(*V2);
632    std::swap(V1, V2);
633  }
634
635  // Merge V1 live ranges into V2.
636  for (iterator I = begin(); I != end(); ) {
637    iterator LR = I++;
638    if (LR->valno != V1) continue;  // Not a V1 LiveRange.
639
640    // Okay, we found a V1 live range.  If it had a previous, touching, V2 live
641    // range, extend it.
642    if (LR != begin()) {
643      iterator Prev = LR-1;
644      if (Prev->valno == V2 && Prev->end == LR->start) {
645        Prev->end = LR->end;
646
647        // Erase this live-range.
648        ranges.erase(LR);
649        I = Prev+1;
650        LR = Prev;
651      }
652    }
653
654    // Okay, now we have a V1 or V2 live range that is maximally merged forward.
655    // Ensure that it is a V2 live-range.
656    LR->valno = V2;
657
658    // If we can merge it into later V2 live ranges, do so now.  We ignore any
659    // following V1 live ranges, as they will be merged in subsequent iterations
660    // of the loop.
661    if (I != end()) {
662      if (I->start == LR->end && I->valno == V2) {
663        LR->end = I->end;
664        ranges.erase(I);
665        I = LR+1;
666      }
667    }
668  }
669
670  // Merge the relevant flags.
671  V2->mergeFlags(V1);
672
673  // Now that V1 is dead, remove it.
674  markValNoForDeletion(V1);
675
676  return V2;
677}
678
679void LiveInterval::Copy(const LiveInterval &RHS,
680                        MachineRegisterInfo *MRI,
681                        VNInfo::Allocator &VNInfoAllocator) {
682  ranges.clear();
683  valnos.clear();
684  std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
685  MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
686
687  weight = RHS.weight;
688  for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
689    const VNInfo *VNI = RHS.getValNumInfo(i);
690    createValueCopy(VNI, VNInfoAllocator);
691  }
692  for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
693    const LiveRange &LR = RHS.ranges[i];
694    addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
695  }
696
697  verify();
698}
699
700unsigned LiveInterval::getSize() const {
701  unsigned Sum = 0;
702  for (const_iterator I = begin(), E = end(); I != E; ++I)
703    Sum += I->start.distance(I->end);
704  return Sum;
705}
706
707raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
708  return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
709}
710
711void LiveRange::dump() const {
712  dbgs() << *this << "\n";
713}
714
715void LiveInterval::print(raw_ostream &OS) const {
716  if (empty())
717    OS << "EMPTY";
718  else {
719    for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
720           E = ranges.end(); I != E; ++I) {
721      OS << *I;
722      assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
723    }
724  }
725
726  // Print value number info.
727  if (getNumValNums()) {
728    OS << "  ";
729    unsigned vnum = 0;
730    for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
731         ++i, ++vnum) {
732      const VNInfo *vni = *i;
733      if (vnum) OS << " ";
734      OS << vnum << "@";
735      if (vni->isUnused()) {
736        OS << "x";
737      } else {
738        OS << vni->def;
739        if (vni->isPHIDef())
740          OS << "-phidef";
741        if (vni->hasPHIKill())
742          OS << "-phikill";
743      }
744    }
745  }
746}
747
748void LiveInterval::dump() const {
749  dbgs() << *this << "\n";
750}
751
752#ifndef NDEBUG
753void LiveInterval::verify() const {
754  for (const_iterator I = begin(), E = end(); I != E; ++I) {
755    assert(I->start.isValid());
756    assert(I->end.isValid());
757    assert(I->start < I->end);
758    assert(I->valno != 0);
759    assert(I->valno == valnos[I->valno->id]);
760    if (llvm::next(I) != E) {
761      assert(I->end <= llvm::next(I)->start);
762      if (I->end == llvm::next(I)->start)
763        assert(I->valno != llvm::next(I)->valno);
764    }
765  }
766}
767#endif
768
769
770void LiveRange::print(raw_ostream &os) const {
771  os << *this;
772}
773
774unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
775  // Create initial equivalence classes.
776  EqClass.clear();
777  EqClass.grow(LI->getNumValNums());
778
779  const VNInfo *used = 0, *unused = 0;
780
781  // Determine connections.
782  for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
783       I != E; ++I) {
784    const VNInfo *VNI = *I;
785    // Group all unused values into one class.
786    if (VNI->isUnused()) {
787      if (unused)
788        EqClass.join(unused->id, VNI->id);
789      unused = VNI;
790      continue;
791    }
792    used = VNI;
793    if (VNI->isPHIDef()) {
794      const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
795      assert(MBB && "Phi-def has no defining MBB");
796      // Connect to values live out of predecessors.
797      for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
798           PE = MBB->pred_end(); PI != PE; ++PI)
799        if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
800          EqClass.join(VNI->id, PVNI->id);
801    } else {
802      // Normal value defined by an instruction. Check for two-addr redef.
803      // FIXME: This could be coincidental. Should we really check for a tied
804      // operand constraint?
805      // Note that VNI->def may be a use slot for an early clobber def.
806      if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def))
807        EqClass.join(VNI->id, UVNI->id);
808    }
809  }
810
811  // Lump all the unused values in with the last used value.
812  if (used && unused)
813    EqClass.join(used->id, unused->id);
814
815  EqClass.compress();
816  return EqClass.getNumClasses();
817}
818
819void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
820                                          MachineRegisterInfo &MRI) {
821  assert(LIV[0] && "LIV[0] must be set");
822  LiveInterval &LI = *LIV[0];
823
824  // Rewrite instructions.
825  for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
826       RE = MRI.reg_end(); RI != RE;) {
827    MachineOperand &MO = RI.getOperand();
828    MachineInstr *MI = MO.getParent();
829    ++RI;
830    // DBG_VALUE instructions should have been eliminated earlier.
831    LiveRangeQuery LRQ(LI, LIS.getInstructionIndex(MI));
832    const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined();
833    // In the case of an <undef> use that isn't tied to any def, VNI will be
834    // NULL. If the use is tied to a def, VNI will be the defined value.
835    if (!VNI)
836      continue;
837    MO.setReg(LIV[getEqClass(VNI)]->reg);
838  }
839
840  // Move runs to new intervals.
841  LiveInterval::iterator J = LI.begin(), E = LI.end();
842  while (J != E && EqClass[J->valno->id] == 0)
843    ++J;
844  for (LiveInterval::iterator I = J; I != E; ++I) {
845    if (unsigned eq = EqClass[I->valno->id]) {
846      assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
847             "New intervals should be empty");
848      LIV[eq]->ranges.push_back(*I);
849    } else
850      *J++ = *I;
851  }
852  LI.ranges.erase(J, E);
853
854  // Transfer VNInfos to their new owners and renumber them.
855  unsigned j = 0, e = LI.getNumValNums();
856  while (j != e && EqClass[j] == 0)
857    ++j;
858  for (unsigned i = j; i != e; ++i) {
859    VNInfo *VNI = LI.getValNumInfo(i);
860    if (unsigned eq = EqClass[i]) {
861      VNI->id = LIV[eq]->getNumValNums();
862      LIV[eq]->valnos.push_back(VNI);
863    } else {
864      VNI->id = j;
865      LI.valnos[j++] = VNI;
866    }
867  }
868  LI.valnos.resize(j);
869}
870