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