1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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' abd 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 "LiveInterval.h"
22 #include "llvm/ADT/STLExtras.h"
28 // An example for liveAt():
30 // this = [1,4), liveAt(0) will return false. The instruction defining this
31 // spans slots [0,3]. The interval belongs to an spilled definition of the
32 // variable it represents. This is because slot 1 is used (def slot) and spans
33 // up to slot 3 (store slot).
35 bool LiveInterval::liveAt(unsigned I) const {
36 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
38 if (r == ranges.begin())
42 return r->contains(I);
45 // An example for overlaps():
49 // 8: C = A + B ;; last use of A
51 // The live intervals should look like:
57 // A->overlaps(C) should return false since we want to be able to join
59 bool LiveInterval::overlaps(const LiveInterval& other) const {
60 Ranges::const_iterator i = ranges.begin();
61 Ranges::const_iterator ie = ranges.end();
62 Ranges::const_iterator j = other.ranges.begin();
63 Ranges::const_iterator je = other.ranges.end();
65 if (i->start < j->start) {
66 i = std::upper_bound(i, ie, j->start);
67 if (i != ranges.begin()) --i;
68 } else if (j->start < i->start) {
69 j = std::upper_bound(j, je, i->start);
70 if (j != other.ranges.begin()) --j;
75 while (i != ie && j != je) {
76 if (i->start == j->start)
79 if (i->start > j->start) {
83 assert(i->start < j->start);
85 if (i->end > j->start)
93 /// joinable - Two intervals are joinable if the either don't overlap at all
94 /// or if the destination of the copy is a single assignment value, and it
95 /// only overlaps with one value in the source interval.
96 bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
97 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
98 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
99 assert(SourceLR && DestLR && "Not joining due to a copy?");
100 unsigned OtherValIdx = SourceLR->ValId;
101 unsigned ThisValIdx = DestLR->ValId;
103 Ranges::const_iterator i = ranges.begin();
104 Ranges::const_iterator ie = ranges.end();
105 Ranges::const_iterator j = other.ranges.begin();
106 Ranges::const_iterator je = other.ranges.end();
108 if (i->start < j->start) {
109 i = std::upper_bound(i, ie, j->start);
110 if (i != ranges.begin()) --i;
111 } else if (j->start < i->start) {
112 j = std::upper_bound(j, je, i->start);
113 if (j != other.ranges.begin()) --j;
116 while (i != ie && j != je) {
117 if (i->start == j->start) {
118 // If this is not the allowed value merge, we cannot join.
119 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
121 } else if (i->start < j->start) {
122 if (i->end > j->start) {
123 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
127 if (j->end > i->start) {
128 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
142 /// extendIntervalEndTo - This method is used when we want to extend the range
143 /// specified by I to end at the specified endpoint. To do this, we should
144 /// merge and eliminate all ranges that this will overlap with. The iterator is
146 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
147 assert(I != ranges.end() && "Not a valid interval!");
148 unsigned ValId = I->ValId;
150 // Search for the first interval that we can't merge with.
151 Ranges::iterator MergeTo = next(I);
152 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
153 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
156 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
157 I->end = std::max(NewEnd, prior(MergeTo)->end);
159 // Erase any dead ranges
160 ranges.erase(next(I), MergeTo);
164 /// extendIntervalStartTo - This method is used when we want to extend the range
165 /// specified by I to start at the specified endpoint. To do this, we should
166 /// merge and eliminate all ranges that this will overlap with.
167 LiveInterval::Ranges::iterator
168 LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
169 assert(I != ranges.end() && "Not a valid interval!");
170 unsigned ValId = I->ValId;
172 // Search for the first interval that we can't merge with.
173 Ranges::iterator MergeTo = I;
175 if (MergeTo == ranges.begin()) {
177 ranges.erase(MergeTo, I);
180 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
182 } while (NewStart <= MergeTo->start);
184 // If we start in the middle of another interval, just delete a range and
185 // extend that interval.
186 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
187 MergeTo->end = I->end;
189 // Otherwise, extend the interval right after.
191 MergeTo->start = NewStart;
192 MergeTo->end = I->end;
195 ranges.erase(next(MergeTo), next(I));
199 LiveInterval::Ranges::iterator
200 LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
201 unsigned Start = LR.start, End = LR.end;
202 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
204 // If the inserted interval starts in the middle or right at the end of
205 // another interval, just extend that interval to contain the range of LR.
206 if (it != ranges.begin()) {
207 Ranges::iterator B = prior(it);
208 if (LR.ValId == B->ValId) {
209 if (B->start <= Start && B->end >= Start) {
210 extendIntervalEndTo(B, End);
214 // Check to make sure that we are not overlapping two live ranges with
215 // different ValId's.
216 assert(B->end <= Start &&
217 "Cannot overlap two LiveRanges with differing ValID's"
218 " (did you def the same reg twice in a MachineInstr?)");
222 // Otherwise, if this range ends in the middle of, or right next to, another
223 // interval, merge it into that interval.
224 if (it != ranges.end())
225 if (LR.ValId == it->ValId) {
226 if (it->start <= End) {
227 it = extendIntervalStartTo(it, Start);
229 // If LR is a complete superset of an interval, we may need to grow its
232 extendIntervalEndTo(it, End);
236 // Check to make sure that we are not overlapping two live ranges with
237 // different ValId's.
238 assert(it->start >= End &&
239 "Cannot overlap two LiveRanges with differing ValID's");
242 // Otherwise, this is just a new range that doesn't interact with anything.
244 return ranges.insert(it, LR);
248 /// removeRange - Remove the specified range from this interval. Note that
249 /// the range must already be in this interval in its entirety.
250 void LiveInterval::removeRange(unsigned Start, unsigned End) {
251 // Find the LiveRange containing this span.
252 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
253 assert(I != ranges.begin() && "Range is not in interval!");
255 assert(I->contains(Start) && I->contains(End-1) &&
256 "Range is not entirely in interval!");
258 // If the span we are removing is at the start of the LiveRange, adjust it.
259 if (I->start == Start) {
261 ranges.erase(I); // Removed the whole LiveRange.
267 // Otherwise if the span we are removing is at the end of the LiveRange,
268 // adjust the other way.
274 // Otherwise, we are splitting the LiveRange into two pieces.
275 unsigned OldEnd = I->end;
276 I->end = Start; // Trim the old interval.
278 // Insert the new one.
279 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
282 /// getLiveRangeContaining - Return the live range that contains the
283 /// specified index, or null if there is none.
284 const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
285 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
286 if (It != ranges.begin()) {
287 const LiveRange &LR = *prior(It);
288 if (LR.contains(Idx))
297 /// join - Join two live intervals (this, and other) together. This operation
298 /// is the result of a copy instruction in the source program, that occurs at
299 /// index 'CopyIdx' that copies from 'Other' to 'this'.
300 void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
301 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
302 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
303 assert(SourceLR && DestLR && "Not joining due to a copy?");
304 unsigned MergedSrcValIdx = SourceLR->ValId;
305 unsigned MergedDstValIdx = DestLR->ValId;
307 // Try to do the least amount of work possible. In particular, if there are
308 // more liverange chunks in the other set than there are in the 'this' set,
309 // swap sets to merge the fewest chunks in possible.
310 if (Other.ranges.size() > ranges.size()) {
311 std::swap(MergedSrcValIdx, MergedDstValIdx);
312 std::swap(ranges, Other.ranges);
313 std::swap(NumValues, Other.NumValues);
316 // Join the ranges of other into the ranges of this interval.
317 Ranges::iterator InsertPos = ranges.begin();
318 std::map<unsigned, unsigned> Dst2SrcIdxMap;
319 for (Ranges::iterator I = Other.ranges.begin(),
320 E = Other.ranges.end(); I != E; ++I) {
321 // Map the ValId in the other live range to the current live range.
322 if (I->ValId == MergedSrcValIdx)
323 I->ValId = MergedDstValIdx;
325 unsigned &NV = Dst2SrcIdxMap[I->ValId];
326 if (NV == 0) NV = getNextValue();
330 InsertPos = addRangeFrom(*I, InsertPos);
333 weight += Other.weight;
336 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
337 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
340 void LiveRange::dump() const {
341 std::cerr << *this << "\n";
345 std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
346 os << "%reg" << li.reg << ',' << li.weight;
348 return os << "EMPTY";
351 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
352 e = li.ranges.end(); i != e; ++i)
357 void LiveInterval::dump() const {
358 std::cerr << *this << "\n";