1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
19 //===----------------------------------------------------------------------===//
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"
33 // An example for liveAt():
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).
40 bool LiveInterval::liveAt(SlotIndex I) const {
41 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
43 if (r == ranges.begin())
47 return r->contains(I);
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.
53 bool LiveInterval::liveBeforeAndAt(SlotIndex I) const {
54 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
56 if (r == ranges.begin())
64 // I is the start of a live range. Check if the previous live range ends
66 if (r == ranges.begin())
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.
74 bool LiveInterval::killedAt(SlotIndex I) const {
75 Ranges::const_iterator r = std::lower_bound(ranges.begin(), ranges.end(), I);
77 // Now r points to the first interval with start >= I, or ranges.end().
78 if (r == ranges.begin())
82 // Now r points to the last interval with end <= I.
83 // r->end is the kill point.
87 /// killedInRange - Return true if the interval has kills in [Start,End).
88 bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
89 Ranges::const_iterator r =
90 std::lower_bound(ranges.begin(), ranges.end(), End);
92 // Now r points to the first interval with start >= End, or ranges.end().
93 if (r == ranges.begin())
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;
102 // overlaps - Return true if the intersection of the two live intervals is
105 // An example for overlaps():
109 // 8: C = A + B ;; last use of A
111 // The live intervals should look like:
117 // A->overlaps(C) should return false since we want to be able to join
120 bool LiveInterval::overlapsFrom(const LiveInterval& other,
121 const_iterator StartPos) const {
122 const_iterator i = begin();
123 const_iterator ie = end();
124 const_iterator j = StartPos;
125 const_iterator je = other.end();
127 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
128 StartPos != other.end() && "Bogus start position hint!");
130 if (i->start < j->start) {
131 i = std::upper_bound(i, ie, j->start);
132 if (i != ranges.begin()) --i;
133 } else if (j->start < i->start) {
135 if (StartPos != other.end() && StartPos->start <= i->start) {
136 assert(StartPos < other.end() && i < end());
137 j = std::upper_bound(j, je, i->start);
138 if (j != other.ranges.begin()) --j;
144 if (j == je) return false;
147 if (i->start > j->start) {
152 if (i->end > j->start)
160 /// overlaps - Return true if the live interval overlaps a range specified
162 bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
163 assert(Start < End && "Invalid range");
164 const_iterator I = begin();
165 const_iterator E = end();
166 const_iterator si = std::upper_bound(I, E, Start);
167 const_iterator ei = std::upper_bound(I, E, End);
173 return si->contains(Start);
176 /// extendIntervalEndTo - This method is used when we want to extend the range
177 /// specified by I to end at the specified endpoint. To do this, we should
178 /// merge and eliminate all ranges that this will overlap with. The iterator is
180 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
181 assert(I != ranges.end() && "Not a valid interval!");
182 VNInfo *ValNo = I->valno;
183 SlotIndex OldEnd = I->end;
185 // Search for the first interval that we can't merge with.
186 Ranges::iterator MergeTo = next(I);
187 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
188 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
191 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
192 I->end = std::max(NewEnd, prior(MergeTo)->end);
194 // Erase any dead ranges.
195 ranges.erase(next(I), MergeTo);
197 // If the newly formed range now touches the range after it and if they have
198 // the same value number, merge the two ranges into one range.
199 Ranges::iterator Next = next(I);
200 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
207 /// extendIntervalStartTo - This method is used when we want to extend the range
208 /// specified by I to start at the specified endpoint. To do this, we should
209 /// merge and eliminate all ranges that this will overlap with.
210 LiveInterval::Ranges::iterator
211 LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
212 assert(I != ranges.end() && "Not a valid interval!");
213 VNInfo *ValNo = I->valno;
215 // Search for the first interval that we can't merge with.
216 Ranges::iterator MergeTo = I;
218 if (MergeTo == ranges.begin()) {
220 ranges.erase(MergeTo, I);
223 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
225 } while (NewStart <= MergeTo->start);
227 // If we start in the middle of another interval, just delete a range and
228 // extend that interval.
229 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
230 MergeTo->end = I->end;
232 // Otherwise, extend the interval right after.
234 MergeTo->start = NewStart;
235 MergeTo->end = I->end;
238 ranges.erase(next(MergeTo), next(I));
242 LiveInterval::iterator
243 LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
244 SlotIndex Start = LR.start, End = LR.end;
245 iterator it = std::upper_bound(From, ranges.end(), Start);
247 // If the inserted interval starts in the middle or right at the end of
248 // another interval, just extend that interval to contain the range of LR.
249 if (it != ranges.begin()) {
250 iterator B = prior(it);
251 if (LR.valno == B->valno) {
252 if (B->start <= Start && B->end >= Start) {
253 extendIntervalEndTo(B, End);
257 // Check to make sure that we are not overlapping two live ranges with
258 // different valno's.
259 assert(B->end <= Start &&
260 "Cannot overlap two LiveRanges with differing ValID's"
261 " (did you def the same reg twice in a MachineInstr?)");
265 // Otherwise, if this range ends in the middle of, or right next to, another
266 // interval, merge it into that interval.
267 if (it != ranges.end()) {
268 if (LR.valno == it->valno) {
269 if (it->start <= End) {
270 it = extendIntervalStartTo(it, Start);
272 // If LR is a complete superset of an interval, we may need to grow its
275 extendIntervalEndTo(it, End);
279 // Check to make sure that we are not overlapping two live ranges with
280 // different valno's.
281 assert(it->start >= End &&
282 "Cannot overlap two LiveRanges with differing ValID's");
286 // Otherwise, this is just a new range that doesn't interact with anything.
288 return ranges.insert(it, LR);
291 /// isInOneLiveRange - Return true if the range specified is entirely in
292 /// a single LiveRange of the live interval.
293 bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
294 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
295 if (I == ranges.begin())
298 return I->containsRange(Start, End);
302 /// removeRange - Remove the specified range from this interval. Note that
303 /// the range must be in a single LiveRange in its entirety.
304 void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
305 bool RemoveDeadValNo) {
306 // Find the LiveRange containing this span.
307 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
308 assert(I != ranges.begin() && "Range is not in interval!");
310 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
312 // If the span we are removing is at the start of the LiveRange, adjust it.
313 VNInfo *ValNo = I->valno;
314 if (I->start == Start) {
316 if (RemoveDeadValNo) {
317 // Check if val# is dead.
319 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
320 if (II != I && II->valno == ValNo) {
325 // Now that ValNo is dead, remove it. If it is the largest value
326 // number, just nuke it (and any other deleted values neighboring it),
327 // otherwise mark it as ~1U so it can be nuked later.
328 if (ValNo->id == getNumValNums()-1) {
331 } while (!valnos.empty() && valnos.back()->isUnused());
333 ValNo->setIsUnused(true);
338 ranges.erase(I); // Removed the whole LiveRange.
344 // Otherwise if the span we are removing is at the end of the LiveRange,
345 // adjust the other way.
351 // Otherwise, we are splitting the LiveRange into two pieces.
352 SlotIndex OldEnd = I->end;
353 I->end = Start; // Trim the old interval.
355 // Insert the new one.
356 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
359 /// removeValNo - Remove all the ranges defined by the specified value#.
360 /// Also remove the value# from value# list.
361 void LiveInterval::removeValNo(VNInfo *ValNo) {
363 Ranges::iterator I = ranges.end();
364 Ranges::iterator E = ranges.begin();
367 if (I->valno == ValNo)
370 // Now that ValNo is dead, remove it. If it is the largest value
371 // number, just nuke it (and any other deleted values neighboring it),
372 // otherwise mark it as ~1U so it can be nuked later.
373 if (ValNo->id == getNumValNums()-1) {
376 } while (!valnos.empty() && valnos.back()->isUnused());
378 ValNo->setIsUnused(true);
382 /// getLiveRangeContaining - Return the live range that contains the
383 /// specified index, or null if there is none.
384 LiveInterval::const_iterator
385 LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
386 const_iterator It = std::upper_bound(begin(), end(), Idx);
387 if (It != ranges.begin()) {
389 if (It->contains(Idx))
396 LiveInterval::iterator
397 LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
398 iterator It = std::upper_bound(begin(), end(), Idx);
401 if (It->contains(Idx))
408 /// findDefinedVNInfo - Find the VNInfo defined by the specified
409 /// index (register interval).
410 VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
411 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
413 if ((*i)->def == Idx)
420 /// findDefinedVNInfo - Find the VNInfo defined by the specified
421 /// register (stack inteval).
422 VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
423 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
425 if ((*i)->getReg() == reg)
431 /// join - Join two live intervals (this, and other) together. This applies
432 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
433 /// the intervals are not joinable, this aborts.
434 void LiveInterval::join(LiveInterval &Other,
435 const int *LHSValNoAssignments,
436 const int *RHSValNoAssignments,
437 SmallVector<VNInfo*, 16> &NewVNInfo,
438 MachineRegisterInfo *MRI) {
439 // Determine if any of our live range values are mapped. This is uncommon, so
440 // we want to avoid the interval scan if not.
441 bool MustMapCurValNos = false;
442 unsigned NumVals = getNumValNums();
443 unsigned NumNewVals = NewVNInfo.size();
444 for (unsigned i = 0; i != NumVals; ++i) {
445 unsigned LHSValID = LHSValNoAssignments[i];
447 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
448 MustMapCurValNos = true;
451 // If we have to apply a mapping to our base interval assignment, rewrite it
453 if (MustMapCurValNos) {
454 // Map the first live range.
455 iterator OutIt = begin();
456 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
458 for (iterator I = OutIt, E = end(); I != E; ++I) {
459 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
461 // If this live range has the same value # as its immediate predecessor,
462 // and if they are neighbors, remove one LiveRange. This happens when we
463 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
464 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
465 (OutIt-1)->end = OutIt->end;
468 OutIt->start = I->start;
472 // Didn't merge, on to the next one.
477 // If we merge some live ranges, chop off the end.
478 ranges.erase(OutIt, end());
481 // Remember assignements because val# ids are changing.
482 SmallVector<unsigned, 16> OtherAssignments;
483 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
484 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
486 // Update val# info. Renumber them and make sure they all belong to this
487 // LiveInterval now. Also remove dead val#'s.
488 unsigned NumValNos = 0;
489 for (unsigned i = 0; i < NumNewVals; ++i) {
490 VNInfo *VNI = NewVNInfo[i];
492 if (NumValNos >= NumVals)
493 valnos.push_back(VNI);
495 valnos[NumValNos] = VNI;
496 VNI->id = NumValNos++; // Renumber val#.
499 if (NumNewVals < NumVals)
500 valnos.resize(NumNewVals); // shrinkify
502 // Okay, now insert the RHS live ranges into the LHS.
503 iterator InsertPos = begin();
504 unsigned RangeNo = 0;
505 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
506 // Map the valno in the other live range to the current live range.
507 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
508 assert(I->valno && "Adding a dead range?");
509 InsertPos = addRangeFrom(*I, InsertPos);
512 ComputeJoinedWeight(Other);
514 // Update regalloc hint if currently there isn't one.
515 if (TargetRegisterInfo::isVirtualRegister(reg) &&
516 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
517 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
518 if (Hint.first == 0 && Hint.second == 0) {
519 std::pair<unsigned, unsigned> OtherHint =
520 MRI->getRegAllocationHint(Other.reg);
521 if (OtherHint.first || OtherHint.second)
522 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
527 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
528 /// interval as the specified value number. The LiveRanges in RHS are
529 /// allowed to overlap with LiveRanges in the current interval, but only if
530 /// the overlapping LiveRanges have the specified value number.
531 void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
533 // TODO: Make this more efficient.
534 iterator InsertPos = begin();
535 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
536 // Map the valno in the other live range to the current live range.
538 Tmp.valno = LHSValNo;
539 InsertPos = addRangeFrom(Tmp, InsertPos);
544 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
545 /// in RHS into this live interval as the specified value number.
546 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
547 /// current interval, it will replace the value numbers of the overlaped
548 /// live ranges with the specified value number.
549 void LiveInterval::MergeValueInAsValue(
550 const LiveInterval &RHS,
551 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
552 SmallVector<VNInfo*, 4> ReplacedValNos;
553 iterator IP = begin();
554 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
555 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
556 if (I->valno != RHSValNo)
558 SlotIndex Start = I->start, End = I->end;
559 IP = std::upper_bound(IP, end(), Start);
560 // If the start of this range overlaps with an existing liverange, trim it.
561 if (IP != begin() && IP[-1].end > Start) {
562 if (IP[-1].valno != LHSValNo) {
563 ReplacedValNos.push_back(IP[-1].valno);
564 IP[-1].valno = LHSValNo; // Update val#.
567 // Trimmed away the whole range?
568 if (Start >= End) continue;
570 // If the end of this range overlaps with an existing liverange, trim it.
571 if (IP != end() && End > IP->start) {
572 if (IP->valno != LHSValNo) {
573 ReplacedValNos.push_back(IP->valno);
574 IP->valno = LHSValNo; // Update val#.
577 // If this trimmed away the whole range, ignore it.
578 if (Start == End) continue;
581 // Map the valno in the other live range to the current live range.
582 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
586 SmallSet<VNInfo*, 4> Seen;
587 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
588 VNInfo *V1 = ReplacedValNos[i];
589 if (Seen.insert(V1)) {
591 for (const_iterator I = begin(), E = end(); I != E; ++I)
592 if (I->valno == V1) {
597 // Now that V1 is dead, remove it. If it is the largest value number,
598 // just nuke it (and any other deleted values neighboring it), otherwise
599 // mark it as ~1U so it can be nuked later.
600 if (V1->id == getNumValNums()-1) {
603 } while (!valnos.empty() && valnos.back()->isUnused());
605 V1->setIsUnused(true);
613 /// MergeInClobberRanges - For any live ranges that are not defined in the
614 /// current interval, but are defined in the Clobbers interval, mark them
615 /// used with an unknown definition value.
616 void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
617 const LiveInterval &Clobbers,
618 VNInfo::Allocator &VNInfoAllocator) {
619 if (Clobbers.empty()) return;
621 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
622 VNInfo *UnusedValNo = 0;
623 iterator IP = begin();
624 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
625 // For every val# in the Clobbers interval, create a new "unknown" val#.
626 VNInfo *ClobberValNo = 0;
627 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
628 if (VI != ValNoMaps.end())
629 ClobberValNo = VI->second;
630 else if (UnusedValNo)
631 ClobberValNo = UnusedValNo;
633 UnusedValNo = ClobberValNo =
634 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
635 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
639 SlotIndex Start = I->start, End = I->end;
640 // If a clobber range starts before an existing range and ends after
641 // it, the clobber range will need to be split into multiple ranges.
642 // Loop until the entire clobber range is handled.
645 IP = std::upper_bound(IP, end(), Start);
646 SlotIndex SubRangeStart = Start;
647 SlotIndex SubRangeEnd = End;
649 // If the start of this range overlaps with an existing liverange, trim it.
650 if (IP != begin() && IP[-1].end > SubRangeStart) {
651 SubRangeStart = IP[-1].end;
652 // Trimmed away the whole range?
653 if (SubRangeStart >= SubRangeEnd) continue;
655 // If the end of this range overlaps with an existing liverange, trim it.
656 if (IP != end() && SubRangeEnd > IP->start) {
657 // If the clobber live range extends beyond the existing live range,
658 // it'll need at least another live range, so set the flag to keep
660 if (SubRangeEnd > IP->end) {
664 SubRangeEnd = IP->start;
665 // If this trimmed away the whole range, ignore it.
666 if (SubRangeStart == SubRangeEnd) continue;
669 // Insert the clobber interval.
670 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
677 // Delete the last unused val#.
682 void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
685 VNInfo::Allocator &VNInfoAllocator) {
686 // Find a value # to use for the clobber ranges. If there is already a value#
687 // for unknown values, use it.
688 VNInfo *ClobberValNo =
689 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
691 iterator IP = begin();
692 IP = std::upper_bound(IP, end(), Start);
694 // If the start of this range overlaps with an existing liverange, trim it.
695 if (IP != begin() && IP[-1].end > Start) {
697 // Trimmed away the whole range?
698 if (Start >= End) return;
700 // If the end of this range overlaps with an existing liverange, trim it.
701 if (IP != end() && End > IP->start) {
703 // If this trimmed away the whole range, ignore it.
704 if (Start == End) return;
707 // Insert the clobber interval.
708 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
711 /// MergeValueNumberInto - This method is called when two value nubmers
712 /// are found to be equivalent. This eliminates V1, replacing all
713 /// LiveRanges with the V1 value number with the V2 value number. This can
714 /// cause merging of V1/V2 values numbers and compaction of the value space.
715 VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
716 assert(V1 != V2 && "Identical value#'s are always equivalent!");
718 // This code actually merges the (numerically) larger value number into the
719 // smaller value number, which is likely to allow us to compactify the value
720 // space. The only thing we have to be careful of is to preserve the
721 // instruction that defines the result value.
723 // Make sure V2 is smaller than V1.
724 if (V1->id < V2->id) {
729 // Merge V1 live ranges into V2.
730 for (iterator I = begin(); I != end(); ) {
732 if (LR->valno != V1) continue; // Not a V1 LiveRange.
734 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
737 iterator Prev = LR-1;
738 if (Prev->valno == V2 && Prev->end == LR->start) {
741 // Erase this live-range.
748 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
749 // Ensure that it is a V2 live-range.
752 // If we can merge it into later V2 live ranges, do so now. We ignore any
753 // following V1 live ranges, as they will be merged in subsequent iterations
756 if (I->start == LR->end && I->valno == V2) {
764 // Now that V1 is dead, remove it. If it is the largest value number, just
765 // nuke it (and any other deleted values neighboring it), otherwise mark it as
766 // ~1U so it can be nuked later.
767 if (V1->id == getNumValNums()-1) {
770 } while (valnos.back()->isUnused());
772 V1->setIsUnused(true);
778 void LiveInterval::Copy(const LiveInterval &RHS,
779 MachineRegisterInfo *MRI,
780 VNInfo::Allocator &VNInfoAllocator) {
783 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
784 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
787 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
788 const VNInfo *VNI = RHS.getValNumInfo(i);
789 createValueCopy(VNI, VNInfoAllocator);
791 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
792 const LiveRange &LR = RHS.ranges[i];
793 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
797 unsigned LiveInterval::getSize() const {
799 for (const_iterator I = begin(), E = end(); I != E; ++I)
800 Sum += I->start.distance(I->end);
804 /// ComputeJoinedWeight - Set the weight of a live interval Joined
805 /// after Other has been merged into it.
806 void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
807 // If either of these intervals was spilled, the weight is the
808 // weight of the non-spilled interval. This can only happen with
809 // iterative coalescers.
811 if (Other.weight != HUGE_VALF) {
812 weight += Other.weight;
814 else if (weight == HUGE_VALF &&
815 !TargetRegisterInfo::isPhysicalRegister(reg)) {
816 // Remove this assert if you have an iterative coalescer
817 assert(0 && "Joining to spilled interval");
818 weight = Other.weight;
821 // Otherwise the weight stays the same
822 // Remove this assert if you have an iterative coalescer
823 assert(0 && "Joining from spilled interval");
827 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
828 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
831 void LiveRange::dump() const {
832 dbgs() << *this << "\n";
835 void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
837 OS << "SS#" << getStackSlotIndex();
838 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
839 OS << TRI->getName(reg);
849 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
850 E = ranges.end(); I != E; ++I) {
852 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
856 // Print value number info.
857 if (getNumValNums()) {
860 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
862 const VNInfo *vni = *i;
865 if (vni->isUnused()) {
868 if (!vni->isDefAccurate() && !vni->isPHIDef())
877 void LiveInterval::dump() const {
878 dbgs() << *this << "\n";
882 void LiveRange::print(raw_ostream &os) const {