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 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())
40 size_t Len = ranges.size();
42 size_t Mid = Len >> 1;
46 I += Mid + 1, Len -= Mid + 1;
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);
56 // Now r points to the first interval with start >= End, or ranges.end().
57 if (r == ranges.begin())
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;
66 // overlaps - Return true if the intersection of the two live intervals is
69 // An example for overlaps():
73 // 8: C = A + B ;; last use of A
75 // The live intervals should look like:
81 // A->overlaps(C) should return false since we want to be able to join
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();
92 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
93 StartPos != other.end() && "Bogus start position hint!");
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) {
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;
109 if (j == je) return false;
112 if (i->start > j->start) {
117 if (i->end > j->start)
125 /// overlaps - Return true if the live interval overlaps a range specified
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;
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) {
141 } while (!valnos.empty() && valnos.back()->isUnused());
143 ValNo->setIsUnused(true);
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;
152 for (const_iterator I = begin(), E = end(); I != E; ++I) {
153 VNInfo *VNI = I->valno;
154 if (!Seen.insert(VNI))
156 assert(!VNI->isUnused() && "Unused valno used by live range");
157 VNI->id = (unsigned)valnos.size();
158 valnos.push_back(VNI);
162 /// extendIntervalEndTo - This method is used when we want to extend the range
163 /// specified by I to end at the specified endpoint. To do this, we should
164 /// merge and eliminate all ranges that this will overlap with. The iterator is
166 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
167 assert(I != ranges.end() && "Not a valid interval!");
168 VNInfo *ValNo = I->valno;
170 // Search for the first interval that we can't merge with.
171 Ranges::iterator MergeTo = llvm::next(I);
172 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
173 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
176 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
177 I->end = std::max(NewEnd, prior(MergeTo)->end);
179 // Erase any dead ranges.
180 ranges.erase(llvm::next(I), MergeTo);
182 // If the newly formed range now touches the range after it and if they have
183 // the same value number, merge the two ranges into one range.
184 Ranges::iterator Next = llvm::next(I);
185 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
192 /// extendIntervalStartTo - This method is used when we want to extend the range
193 /// specified by I to start at the specified endpoint. To do this, we should
194 /// merge and eliminate all ranges that this will overlap with.
195 LiveInterval::Ranges::iterator
196 LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
197 assert(I != ranges.end() && "Not a valid interval!");
198 VNInfo *ValNo = I->valno;
200 // Search for the first interval that we can't merge with.
201 Ranges::iterator MergeTo = I;
203 if (MergeTo == ranges.begin()) {
205 ranges.erase(MergeTo, I);
208 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
210 } while (NewStart <= MergeTo->start);
212 // If we start in the middle of another interval, just delete a range and
213 // extend that interval.
214 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
215 MergeTo->end = I->end;
217 // Otherwise, extend the interval right after.
219 MergeTo->start = NewStart;
220 MergeTo->end = I->end;
223 ranges.erase(llvm::next(MergeTo), llvm::next(I));
227 LiveInterval::iterator
228 LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
229 SlotIndex Start = LR.start, End = LR.end;
230 iterator it = std::upper_bound(From, ranges.end(), Start);
232 // If the inserted interval starts in the middle or right at the end of
233 // another interval, just extend that interval to contain the range of LR.
234 if (it != ranges.begin()) {
235 iterator B = prior(it);
236 if (LR.valno == B->valno) {
237 if (B->start <= Start && B->end >= Start) {
238 extendIntervalEndTo(B, End);
242 // Check to make sure that we are not overlapping two live ranges with
243 // different valno's.
244 assert(B->end <= Start &&
245 "Cannot overlap two LiveRanges with differing ValID's"
246 " (did you def the same reg twice in a MachineInstr?)");
250 // Otherwise, if this range ends in the middle of, or right next to, another
251 // interval, merge it into that interval.
252 if (it != ranges.end()) {
253 if (LR.valno == it->valno) {
254 if (it->start <= End) {
255 it = extendIntervalStartTo(it, Start);
257 // If LR is a complete superset of an interval, we may need to grow its
260 extendIntervalEndTo(it, End);
264 // Check to make sure that we are not overlapping two live ranges with
265 // different valno's.
266 assert(it->start >= End &&
267 "Cannot overlap two LiveRanges with differing ValID's");
271 // Otherwise, this is just a new range that doesn't interact with anything.
273 return ranges.insert(it, LR);
276 /// extendInBlock - If this interval is live before Kill in the basic
277 /// block that starts at StartIdx, extend it to be live up to Kill and return
278 /// the value. If there is no live range before Kill, return NULL.
279 VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
282 iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot());
286 if (I->end <= StartIdx)
289 extendIntervalEndTo(I, Kill);
293 /// removeRange - Remove the specified range from this interval. Note that
294 /// the range must be in a single LiveRange in its entirety.
295 void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
296 bool RemoveDeadValNo) {
297 // Find the LiveRange containing this span.
298 Ranges::iterator I = find(Start);
299 assert(I != ranges.end() && "Range is not in interval!");
300 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
302 // If the span we are removing is at the start of the LiveRange, adjust it.
303 VNInfo *ValNo = I->valno;
304 if (I->start == Start) {
306 if (RemoveDeadValNo) {
307 // Check if val# is dead.
309 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
310 if (II != I && II->valno == ValNo) {
315 // Now that ValNo is dead, remove it.
316 markValNoForDeletion(ValNo);
320 ranges.erase(I); // Removed the whole LiveRange.
326 // Otherwise if the span we are removing is at the end of the LiveRange,
327 // adjust the other way.
333 // Otherwise, we are splitting the LiveRange into two pieces.
334 SlotIndex OldEnd = I->end;
335 I->end = Start; // Trim the old interval.
337 // Insert the new one.
338 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
341 /// removeValNo - Remove all the ranges defined by the specified value#.
342 /// Also remove the value# from value# list.
343 void LiveInterval::removeValNo(VNInfo *ValNo) {
345 Ranges::iterator I = ranges.end();
346 Ranges::iterator E = ranges.begin();
349 if (I->valno == ValNo)
352 // Now that ValNo is dead, remove it.
353 markValNoForDeletion(ValNo);
356 /// findDefinedVNInfo - Find the VNInfo defined by the specified
357 /// index (register interval).
358 VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
359 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
361 if ((*i)->def == Idx)
368 /// join - Join two live intervals (this, and other) together. This applies
369 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
370 /// the intervals are not joinable, this aborts.
371 void LiveInterval::join(LiveInterval &Other,
372 const int *LHSValNoAssignments,
373 const int *RHSValNoAssignments,
374 SmallVector<VNInfo*, 16> &NewVNInfo,
375 MachineRegisterInfo *MRI) {
376 // Determine if any of our live range values are mapped. This is uncommon, so
377 // we want to avoid the interval scan if not.
378 bool MustMapCurValNos = false;
379 unsigned NumVals = getNumValNums();
380 unsigned NumNewVals = NewVNInfo.size();
381 for (unsigned i = 0; i != NumVals; ++i) {
382 unsigned LHSValID = LHSValNoAssignments[i];
384 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
385 MustMapCurValNos = true;
388 // If we have to apply a mapping to our base interval assignment, rewrite it
390 if (MustMapCurValNos) {
391 // Map the first live range.
392 iterator OutIt = begin();
393 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
395 for (iterator I = OutIt, E = end(); I != E; ++I) {
396 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
398 // If this live range has the same value # as its immediate predecessor,
399 // and if they are neighbors, remove one LiveRange. This happens when we
400 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
401 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
402 (OutIt-1)->end = OutIt->end;
405 OutIt->start = I->start;
409 // Didn't merge, on to the next one.
414 // If we merge some live ranges, chop off the end.
415 ranges.erase(OutIt, end());
418 // Remember assignements because val# ids are changing.
419 SmallVector<unsigned, 16> OtherAssignments;
420 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
421 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
423 // Update val# info. Renumber them and make sure they all belong to this
424 // LiveInterval now. Also remove dead val#'s.
425 unsigned NumValNos = 0;
426 for (unsigned i = 0; i < NumNewVals; ++i) {
427 VNInfo *VNI = NewVNInfo[i];
429 if (NumValNos >= NumVals)
430 valnos.push_back(VNI);
432 valnos[NumValNos] = VNI;
433 VNI->id = NumValNos++; // Renumber val#.
436 if (NumNewVals < NumVals)
437 valnos.resize(NumNewVals); // shrinkify
439 // Okay, now insert the RHS live ranges into the LHS.
440 iterator InsertPos = begin();
441 unsigned RangeNo = 0;
442 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
443 // Map the valno in the other live range to the current live range.
444 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
445 assert(I->valno && "Adding a dead range?");
446 InsertPos = addRangeFrom(*I, InsertPos);
449 ComputeJoinedWeight(Other);
452 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
453 /// interval as the specified value number. The LiveRanges in RHS are
454 /// allowed to overlap with LiveRanges in the current interval, but only if
455 /// the overlapping LiveRanges have the specified value number.
456 void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
458 // TODO: Make this more efficient.
459 iterator InsertPos = begin();
460 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
461 // Map the valno in the other live range to the current live range.
463 Tmp.valno = LHSValNo;
464 InsertPos = addRangeFrom(Tmp, InsertPos);
469 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
470 /// in RHS into this live interval as the specified value number.
471 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
472 /// current interval, it will replace the value numbers of the overlaped
473 /// live ranges with the specified value number.
474 void LiveInterval::MergeValueInAsValue(
475 const LiveInterval &RHS,
476 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
477 // TODO: Make this more efficient.
478 iterator InsertPos = begin();
479 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
480 if (I->valno != RHSValNo)
482 // Map the valno in the other live range to the current live range.
484 Tmp.valno = LHSValNo;
485 InsertPos = addRangeFrom(Tmp, InsertPos);
490 /// MergeValueNumberInto - This method is called when two value nubmers
491 /// are found to be equivalent. This eliminates V1, replacing all
492 /// LiveRanges with the V1 value number with the V2 value number. This can
493 /// cause merging of V1/V2 values numbers and compaction of the value space.
494 VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
495 assert(V1 != V2 && "Identical value#'s are always equivalent!");
497 // This code actually merges the (numerically) larger value number into the
498 // smaller value number, which is likely to allow us to compactify the value
499 // space. The only thing we have to be careful of is to preserve the
500 // instruction that defines the result value.
502 // Make sure V2 is smaller than V1.
503 if (V1->id < V2->id) {
508 // Merge V1 live ranges into V2.
509 for (iterator I = begin(); I != end(); ) {
511 if (LR->valno != V1) continue; // Not a V1 LiveRange.
513 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
516 iterator Prev = LR-1;
517 if (Prev->valno == V2 && Prev->end == LR->start) {
520 // Erase this live-range.
527 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
528 // Ensure that it is a V2 live-range.
531 // If we can merge it into later V2 live ranges, do so now. We ignore any
532 // following V1 live ranges, as they will be merged in subsequent iterations
535 if (I->start == LR->end && I->valno == V2) {
543 // Merge the relevant flags.
546 // Now that V1 is dead, remove it.
547 markValNoForDeletion(V1);
552 void LiveInterval::Copy(const LiveInterval &RHS,
553 MachineRegisterInfo *MRI,
554 VNInfo::Allocator &VNInfoAllocator) {
557 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
558 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
561 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
562 const VNInfo *VNI = RHS.getValNumInfo(i);
563 createValueCopy(VNI, VNInfoAllocator);
565 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
566 const LiveRange &LR = RHS.ranges[i];
567 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
571 unsigned LiveInterval::getSize() const {
573 for (const_iterator I = begin(), E = end(); I != E; ++I)
574 Sum += I->start.distance(I->end);
578 /// ComputeJoinedWeight - Set the weight of a live interval Joined
579 /// after Other has been merged into it.
580 void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
581 // If either of these intervals was spilled, the weight is the
582 // weight of the non-spilled interval. This can only happen with
583 // iterative coalescers.
585 if (Other.weight != HUGE_VALF) {
586 weight += Other.weight;
588 else if (weight == HUGE_VALF &&
589 !TargetRegisterInfo::isPhysicalRegister(reg)) {
590 // Remove this assert if you have an iterative coalescer
591 assert(0 && "Joining to spilled interval");
592 weight = Other.weight;
595 // Otherwise the weight stays the same
596 // Remove this assert if you have an iterative coalescer
597 assert(0 && "Joining from spilled interval");
601 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
602 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
605 void LiveRange::dump() const {
606 dbgs() << *this << "\n";
609 void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
610 OS << PrintReg(reg, TRI);
618 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
619 E = ranges.end(); I != E; ++I) {
621 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
625 // Print value number info.
626 if (getNumValNums()) {
629 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
631 const VNInfo *vni = *i;
634 if (vni->isUnused()) {
640 if (vni->hasPHIKill())
642 if (vni->hasRedefByEC())
649 void LiveInterval::dump() const {
650 dbgs() << *this << "\n";
654 void LiveRange::print(raw_ostream &os) const {
658 unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
659 // Create initial equivalence classes.
661 EqClass.grow(LI->getNumValNums());
663 const VNInfo *used = 0, *unused = 0;
665 // Determine connections.
666 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
668 const VNInfo *VNI = *I;
669 // Group all unused values into one class.
670 if (VNI->isUnused()) {
672 EqClass.join(unused->id, VNI->id);
677 if (VNI->isPHIDef()) {
678 const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
679 assert(MBB && "Phi-def has no defining MBB");
680 // Connect to values live out of predecessors.
681 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
682 PE = MBB->pred_end(); PI != PE; ++PI)
683 if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
684 EqClass.join(VNI->id, PVNI->id);
686 // Normal value defined by an instruction. Check for two-addr redef.
687 // FIXME: This could be coincidental. Should we really check for a tied
688 // operand constraint?
689 // Note that VNI->def may be a use slot for an early clobber def.
690 if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def))
691 EqClass.join(VNI->id, UVNI->id);
695 // Lump all the unused values in with the last used value.
697 EqClass.join(used->id, unused->id);
700 return EqClass.getNumClasses();
703 void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
704 MachineRegisterInfo &MRI) {
705 assert(LIV[0] && "LIV[0] must be set");
706 LiveInterval &LI = *LIV[0];
708 // Rewrite instructions.
709 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
710 RE = MRI.reg_end(); RI != RE;) {
711 MachineOperand &MO = RI.getOperand();
712 MachineInstr *MI = MO.getParent();
714 if (MO.isUse() && MO.isUndef())
716 // DBG_VALUE instructions should have been eliminated earlier.
717 SlotIndex Idx = LIS.getInstructionIndex(MI);
718 Idx = Idx.getRegSlot(MO.isUse());
719 const VNInfo *VNI = LI.getVNInfoAt(Idx);
720 assert(VNI && "Interval not live at use.");
721 MO.setReg(LIV[getEqClass(VNI)]->reg);
724 // Move runs to new intervals.
725 LiveInterval::iterator J = LI.begin(), E = LI.end();
726 while (J != E && EqClass[J->valno->id] == 0)
728 for (LiveInterval::iterator I = J; I != E; ++I) {
729 if (unsigned eq = EqClass[I->valno->id]) {
730 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
731 "New intervals should be empty");
732 LIV[eq]->ranges.push_back(*I);
736 LI.ranges.erase(J, E);
738 // Transfer VNInfos to their new owners and renumber them.
739 unsigned j = 0, e = LI.getNumValNums();
740 while (j != e && EqClass[j] == 0)
742 for (unsigned i = j; i != e; ++i) {
743 VNInfo *VNI = LI.getValNumInfo(i);
744 if (unsigned eq = EqClass[i]) {
745 VNI->id = LIV[eq]->getNumValNums();
746 LIV[eq]->valnos.push_back(VNI);
749 LI.valnos[j++] = VNI;