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