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