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 "Support/STLExtras.h"
27 // An example for liveAt():
29 // this = [1,4), liveAt(0) will return false. The instruction defining this
30 // spans slots [0,3]. The interval belongs to an spilled definition of the
31 // variable it represents. This is because slot 1 is used (def slot) and spans
32 // up to slot 3 (store slot).
34 bool LiveInterval::liveAt(unsigned I) const {
35 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
37 if (r == ranges.begin())
41 return r->contains(I);
44 // An example for overlaps():
48 // 8: C = A + B ;; last use of A
50 // The live intervals should look like:
56 // A->overlaps(C) should return false since we want to be able to join
58 bool LiveInterval::overlaps(const LiveInterval& other) const {
59 Ranges::const_iterator i = ranges.begin();
60 Ranges::const_iterator ie = ranges.end();
61 Ranges::const_iterator j = other.ranges.begin();
62 Ranges::const_iterator je = other.ranges.end();
64 if (i->start < j->start) {
65 i = std::upper_bound(i, ie, j->start);
66 if (i != ranges.begin()) --i;
67 } else if (j->start < i->start) {
68 j = std::upper_bound(j, je, i->start);
69 if (j != other.ranges.begin()) --j;
74 while (i != ie && j != je) {
75 if (i->start == j->start)
78 if (i->start > j->start) {
82 assert(i->start < j->start);
84 if (i->end > j->start)
92 /// joinable - Two intervals are joinable if the either don't overlap at all
93 /// or if the destination of the copy is a single assignment value, and it
94 /// only overlaps with one value in the source interval.
95 bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
96 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
97 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
98 assert(SourceLR && DestLR && "Not joining due to a copy?");
99 unsigned OtherValIdx = SourceLR->ValId;
100 unsigned ThisValIdx = DestLR->ValId;
102 Ranges::const_iterator i = ranges.begin();
103 Ranges::const_iterator ie = ranges.end();
104 Ranges::const_iterator j = other.ranges.begin();
105 Ranges::const_iterator je = other.ranges.end();
107 if (i->start < j->start) {
108 i = std::upper_bound(i, ie, j->start);
109 if (i != ranges.begin()) --i;
110 } else if (j->start < i->start) {
111 j = std::upper_bound(j, je, i->start);
112 if (j != other.ranges.begin()) --j;
115 while (i != ie && j != je) {
116 if (i->start == j->start) {
117 // If this is not the allowed value merge, we cannot join.
118 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
120 } else if (i->start < j->start) {
121 if (i->end > j->start) {
122 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
126 if (j->end > i->start) {
127 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
141 /// extendIntervalEndTo - This method is used when we want to extend the range
142 /// specified by I to end at the specified endpoint. To do this, we should
143 /// merge and eliminate all ranges that this will overlap with. The iterator is
145 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
146 assert(I != ranges.end() && "Not a valid interval!");
147 unsigned ValId = I->ValId;
149 // Search for the first interval that we can't merge with.
150 Ranges::iterator MergeTo = next(I);
151 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
152 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
155 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
156 I->end = std::max(NewEnd, prior(MergeTo)->end);
158 // Erase any dead ranges
159 ranges.erase(next(I), MergeTo);
163 /// extendIntervalStartTo - This method is used when we want to extend the range
164 /// specified by I to start at the specified endpoint. To do this, we should
165 /// merge and eliminate all ranges that this will overlap with.
166 LiveInterval::Ranges::iterator
167 LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
168 assert(I != ranges.end() && "Not a valid interval!");
169 unsigned ValId = I->ValId;
171 // Search for the first interval that we can't merge with.
172 Ranges::iterator MergeTo = I;
174 if (MergeTo == ranges.begin()) {
176 ranges.erase(MergeTo, I);
179 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
181 } while (NewStart <= MergeTo->start);
183 // If we start in the middle of another interval, just delete a range and
184 // extend that interval.
185 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
186 MergeTo->end = I->end;
188 // Otherwise, extend the interval right after.
190 MergeTo->start = NewStart;
191 MergeTo->end = I->end;
194 ranges.erase(next(MergeTo), next(I));
198 LiveInterval::Ranges::iterator
199 LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
200 unsigned Start = LR.start, End = LR.end;
201 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
203 // If the inserted interval starts in the middle or right at the end of
204 // another interval, just extend that interval to contain the range of LR.
205 if (it != ranges.begin()) {
206 Ranges::iterator B = prior(it);
207 if (LR.ValId == B->ValId) {
208 if (B->start <= Start && B->end >= Start) {
209 extendIntervalEndTo(B, End);
213 // Check to make sure that we are not overlapping two live ranges with
214 // different ValId's.
215 assert(B->end <= Start &&
216 "Cannot overlap two LiveRanges with differing ValID's");
220 // Otherwise, if this range ends in the middle of, or right next to, another
221 // interval, merge it into that interval.
222 if (it != ranges.end())
223 if (LR.ValId == it->ValId) {
224 if (it->start <= End) {
225 it = extendIntervalStartTo(it, Start);
227 // If LR is a complete superset of an interval, we may need to grow its
230 extendIntervalEndTo(it, End);
234 // Check to make sure that we are not overlapping two live ranges with
235 // different ValId's.
236 assert(it->start >= End &&
237 "Cannot overlap two LiveRanges with differing ValID's");
240 // Otherwise, this is just a new range that doesn't interact with anything.
242 return ranges.insert(it, LR);
246 /// removeRange - Remove the specified range from this interval. Note that
247 /// the range must already be in this interval in its entirety.
248 void LiveInterval::removeRange(unsigned Start, unsigned End) {
249 // Find the LiveRange containing this span.
250 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
251 assert(I != ranges.begin() && "Range is not in interval!");
253 assert(I->contains(Start) && I->contains(End-1) &&
254 "Range is not entirely in interval!");
256 // If the span we are removing is at the start of the LiveRange, adjust it.
257 if (I->start == Start) {
259 ranges.erase(I); // Removed the whole LiveRange.
265 // Otherwise if the span we are removing is at the end of the LiveRange,
266 // adjust the other way.
272 // Otherwise, we are splitting the LiveRange into two pieces.
273 unsigned OldEnd = I->end;
274 I->end = Start; // Trim the old interval.
276 // Insert the new one.
277 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
280 /// getLiveRangeContaining - Return the live range that contains the
281 /// specified index, or null if there is none.
282 const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
283 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
284 if (It != ranges.begin()) {
285 const LiveRange &LR = *prior(It);
286 if (LR.contains(Idx))
295 /// join - Join two live intervals (this, and other) together. This operation
296 /// is the result of a copy instruction in the source program, that occurs at
297 /// index 'CopyIdx' that copies from 'Other' to 'this'.
298 void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
299 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
300 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
301 assert(SourceLR && DestLR && "Not joining due to a copy?");
302 unsigned MergedSrcValIdx = SourceLR->ValId;
303 unsigned MergedDstValIdx = DestLR->ValId;
305 // Try to do the least amount of work possible. In particular, if there are
306 // more liverange chunks in the other set than there are in the 'this' set,
307 // swap sets to merge the fewest chunks in possible.
308 if (Other.ranges.size() > ranges.size()) {
309 std::swap(MergedSrcValIdx, MergedDstValIdx);
310 std::swap(ranges, Other.ranges);
311 std::swap(NumValues, Other.NumValues);
314 // Join the ranges of other into the ranges of this interval.
315 Ranges::iterator InsertPos = ranges.begin();
316 std::map<unsigned, unsigned> Dst2SrcIdxMap;
317 for (Ranges::iterator I = Other.ranges.begin(),
318 E = Other.ranges.end(); I != E; ++I) {
319 // Map the ValId in the other live range to the current live range.
320 if (I->ValId == MergedSrcValIdx)
321 I->ValId = MergedDstValIdx;
323 unsigned &NV = Dst2SrcIdxMap[I->ValId];
324 if (NV == 0) NV = getNextValue();
328 InsertPos = addRangeFrom(*I, InsertPos);
331 weight += Other.weight;
334 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
335 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
338 void LiveRange::dump() const {
339 std::cerr << *this << "\n";
343 std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
344 os << "%reg" << li.reg << ',' << li.weight;
346 return os << "EMPTY";
349 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
350 e = li.ranges.end(); i != e; ++i)
355 void LiveInterval::dump() const {
356 std::cerr << *this << "\n";