blob: 174b4830d01180068c65ad5ce4032389dfba4960 [file] [log] [blame]
Chris Lattnerfb449b92004-07-23 17:49:16 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
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' 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.
18//
19//===----------------------------------------------------------------------===//
20
21#include "LiveInterval.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000023#include <algorithm>
Chris Lattnerabf295f2004-07-24 02:52:23 +000024#include <iostream>
25#include <map>
Chris Lattnerfb449b92004-07-23 17:49:16 +000026using namespace llvm;
27
28// An example for liveAt():
29//
Chris Lattneraa141472004-07-23 18:40:00 +000030// 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).
Chris Lattnerfb449b92004-07-23 17:49:16 +000034//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000035bool LiveInterval::liveAt(unsigned I) const {
36 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
37
Chris Lattnerfb449b92004-07-23 17:49:16 +000038 if (r == ranges.begin())
39 return false;
40
41 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000042 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000043}
44
45// An example for overlaps():
46//
47// 0: A = ...
48// 4: B = ...
49// 8: C = A + B ;; last use of A
50//
51// The live intervals should look like:
52//
53// A = [3, 11)
54// B = [7, x)
55// C = [11, y)
56//
57// A->overlaps(C) should return false since we want to be able to join
58// A and C.
59bool 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();
Chris Lattnerf5426492004-07-25 07:11:19 +000064
Chris Lattnerfb449b92004-07-23 17:49:16 +000065 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000066 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000067 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000068 } else if (j->start < i->start) {
69 j = std::upper_bound(j, je, i->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000070 if (j != other.ranges.begin()) --j;
Chris Lattneraa141472004-07-23 18:40:00 +000071 } else {
72 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000073 }
74
75 while (i != ie && j != je) {
76 if (i->start == j->start)
77 return true;
78
79 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000080 std::swap(i, j);
81 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000082 }
83 assert(i->start < j->start);
84
85 if (i->end > j->start)
86 return true;
87 ++i;
88 }
89
90 return false;
91}
92
Chris Lattnerabf295f2004-07-24 02:52:23 +000093/// 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.
96bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
Chris Lattnerf5426492004-07-25 07:11:19 +000097 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;
102
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();
107
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;
114 }
115
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)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000120 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000121 } else if (i->start < j->start) {
122 if (i->end > j->start) {
123 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000124 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000125 }
126 } else {
127 if (j->end > i->start) {
128 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000129 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000130 }
131 }
132 if (i->end < j->end)
133 ++i;
134 else
135 ++j;
136 }
137
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000138 return true;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000139}
140
141
Chris Lattnerb26c2152004-07-23 19:38:44 +0000142/// 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
145/// not invalidated.
146void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
147 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000148 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000149
Chris Lattnerb26c2152004-07-23 19:38:44 +0000150 // Search for the first interval that we can't merge with.
151 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000152 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
153 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
154 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000155
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);
158
159 // Erase any dead ranges
160 ranges.erase(next(I), MergeTo);
161}
162
163
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.
167LiveInterval::Ranges::iterator
168LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
169 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000170 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000171
172 // Search for the first interval that we can't merge with.
173 Ranges::iterator MergeTo = I;
174 do {
175 if (MergeTo == ranges.begin()) {
176 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000177 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000178 return I;
179 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000180 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000181 --MergeTo;
182 } while (NewStart <= MergeTo->start);
183
184 // If we start in the middle of another interval, just delete a range and
185 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000186 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000187 MergeTo->end = I->end;
188 } else {
189 // Otherwise, extend the interval right after.
190 ++MergeTo;
191 MergeTo->start = NewStart;
192 MergeTo->end = I->end;
193 }
194
195 ranges.erase(next(MergeTo), next(I));
196 return MergeTo;
197}
198
199LiveInterval::Ranges::iterator
200LiveInterval::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);
203
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);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000208 if (LR.ValId == B->ValId) {
209 if (B->start <= Start && B->end >= Start) {
210 extendIntervalEndTo(B, End);
211 return B;
212 }
213 } else {
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");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000218 }
219 }
220
221 // Otherwise, if this range ends in the middle of, or right next to, another
222 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000223 if (it != ranges.end())
224 if (LR.ValId == it->ValId) {
225 if (it->start <= End) {
226 it = extendIntervalStartTo(it, Start);
227
228 // If LR is a complete superset of an interval, we may need to grow its
229 // endpoint as well.
230 if (End > it->end)
231 extendIntervalEndTo(it, End);
232 return it;
233 }
234 } else {
235 // Check to make sure that we are not overlapping two live ranges with
236 // different ValId's.
237 assert(it->start >= End &&
238 "Cannot overlap two LiveRanges with differing ValID's");
239 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000240
241 // Otherwise, this is just a new range that doesn't interact with anything.
242 // Insert it.
243 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000244}
245
Chris Lattnerabf295f2004-07-24 02:52:23 +0000246
247/// removeRange - Remove the specified range from this interval. Note that
248/// the range must already be in this interval in its entirety.
249void LiveInterval::removeRange(unsigned Start, unsigned End) {
250 // Find the LiveRange containing this span.
251 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
252 assert(I != ranges.begin() && "Range is not in interval!");
253 --I;
254 assert(I->contains(Start) && I->contains(End-1) &&
255 "Range is not entirely in interval!");
256
257 // If the span we are removing is at the start of the LiveRange, adjust it.
258 if (I->start == Start) {
259 if (I->end == End)
260 ranges.erase(I); // Removed the whole LiveRange.
261 else
262 I->start = End;
263 return;
264 }
265
266 // Otherwise if the span we are removing is at the end of the LiveRange,
267 // adjust the other way.
268 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000269 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000270 return;
271 }
272
273 // Otherwise, we are splitting the LiveRange into two pieces.
274 unsigned OldEnd = I->end;
275 I->end = Start; // Trim the old interval.
276
277 // Insert the new one.
278 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
279}
280
281/// getLiveRangeContaining - Return the live range that contains the
282/// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000283const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
284 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000285 if (It != ranges.begin()) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000286 const LiveRange &LR = *prior(It);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000287 if (LR.contains(Idx))
288 return &LR;
289 }
290
291 return 0;
292}
293
294
295
296/// join - Join two live intervals (this, and other) together. This operation
297/// is the result of a copy instruction in the source program, that occurs at
298/// index 'CopyIdx' that copies from 'Other' to 'this'.
299void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000300 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
301 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000302 assert(SourceLR && DestLR && "Not joining due to a copy?");
303 unsigned MergedSrcValIdx = SourceLR->ValId;
304 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000305
Chris Lattnerdeb99712004-07-24 03:41:50 +0000306 // Try to do the least amount of work possible. In particular, if there are
307 // more liverange chunks in the other set than there are in the 'this' set,
308 // swap sets to merge the fewest chunks in possible.
309 if (Other.ranges.size() > ranges.size()) {
310 std::swap(MergedSrcValIdx, MergedDstValIdx);
311 std::swap(ranges, Other.ranges);
312 std::swap(NumValues, Other.NumValues);
313 }
314
Chris Lattnerb26c2152004-07-23 19:38:44 +0000315 // Join the ranges of other into the ranges of this interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000316 Ranges::iterator InsertPos = ranges.begin();
317 std::map<unsigned, unsigned> Dst2SrcIdxMap;
318 for (Ranges::iterator I = Other.ranges.begin(),
319 E = Other.ranges.end(); I != E; ++I) {
320 // Map the ValId in the other live range to the current live range.
321 if (I->ValId == MergedSrcValIdx)
322 I->ValId = MergedDstValIdx;
323 else {
324 unsigned &NV = Dst2SrcIdxMap[I->ValId];
325 if (NV == 0) NV = getNextValue();
326 I->ValId = NV;
327 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000328
Chris Lattnerabf295f2004-07-24 02:52:23 +0000329 InsertPos = addRangeFrom(*I, InsertPos);
330 }
331
332 weight += Other.weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000333}
334
Chris Lattnerfb449b92004-07-23 17:49:16 +0000335std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000336 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000337}
338
Chris Lattnerabf295f2004-07-24 02:52:23 +0000339void LiveRange::dump() const {
340 std::cerr << *this << "\n";
341}
342
343
Chris Lattnerfb449b92004-07-23 17:49:16 +0000344std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
345 os << "%reg" << li.reg << ',' << li.weight;
346 if (li.empty())
347 return os << "EMPTY";
348
349 os << " = ";
350 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
351 e = li.ranges.end(); i != e; ++i)
352 os << *i;
353 return os;
354}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000355
356void LiveInterval::dump() const {
357 std::cerr << *this << "\n";
358}