blob: 54e460f7b46aacaac0c2f705bf8147f920f38598 [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
Chris Lattnerbae74d92004-11-18 03:47:34 +000045// overlaps - Return true if the intersection of the two live intervals is
46// not empty.
47//
Chris Lattnerfb449b92004-07-23 17:49:16 +000048// An example for overlaps():
49//
50// 0: A = ...
51// 4: B = ...
52// 8: C = A + B ;; last use of A
53//
54// The live intervals should look like:
55//
56// A = [3, 11)
57// B = [7, x)
58// C = [11, y)
59//
60// A->overlaps(C) should return false since we want to be able to join
61// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000062//
63bool LiveInterval::overlapsFrom(const LiveInterval& other,
64 const_iterator StartPos) const {
65 const_iterator i = begin();
66 const_iterator ie = end();
67 const_iterator j = StartPos;
68 const_iterator je = other.end();
69
70 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000071 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000072
Chris Lattnerfb449b92004-07-23 17:49:16 +000073 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000074 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000075 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000076 } else if (j->start < i->start) {
Chris Lattner8c68b6a2004-11-18 04:02:11 +000077 if ((++StartPos)->start <= i->start) {
78 j = std::upper_bound(j, je, i->start);
79 if (j != other.ranges.begin()) --j;
80 }
Chris Lattneraa141472004-07-23 18:40:00 +000081 } else {
82 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000083 }
84
Chris Lattner9fddc122004-11-18 05:28:21 +000085 if (j == je) return false;
86
87 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000088 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000089 std::swap(i, j);
90 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000091 }
Chris Lattnerfb449b92004-07-23 17:49:16 +000092
93 if (i->end > j->start)
94 return true;
95 ++i;
96 }
97
98 return false;
99}
100
Chris Lattnerabf295f2004-07-24 02:52:23 +0000101/// joinable - Two intervals are joinable if the either don't overlap at all
102/// or if the destination of the copy is a single assignment value, and it
103/// only overlaps with one value in the source interval.
104bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
Chris Lattnerf5426492004-07-25 07:11:19 +0000105 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
106 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
107 assert(SourceLR && DestLR && "Not joining due to a copy?");
108 unsigned OtherValIdx = SourceLR->ValId;
109 unsigned ThisValIdx = DestLR->ValId;
110
111 Ranges::const_iterator i = ranges.begin();
112 Ranges::const_iterator ie = ranges.end();
113 Ranges::const_iterator j = other.ranges.begin();
114 Ranges::const_iterator je = other.ranges.end();
115
116 if (i->start < j->start) {
117 i = std::upper_bound(i, ie, j->start);
118 if (i != ranges.begin()) --i;
119 } else if (j->start < i->start) {
120 j = std::upper_bound(j, je, i->start);
121 if (j != other.ranges.begin()) --j;
122 }
123
124 while (i != ie && j != je) {
125 if (i->start == j->start) {
126 // If this is not the allowed value merge, we cannot join.
127 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000128 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000129 } else if (i->start < j->start) {
130 if (i->end > j->start) {
131 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000132 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000133 }
134 } else {
135 if (j->end > i->start) {
136 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000137 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000138 }
139 }
140 if (i->end < j->end)
141 ++i;
142 else
143 ++j;
144 }
145
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000146 return true;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000147}
148
149
Chris Lattnerb26c2152004-07-23 19:38:44 +0000150/// extendIntervalEndTo - This method is used when we want to extend the range
151/// specified by I to end at the specified endpoint. To do this, we should
152/// merge and eliminate all ranges that this will overlap with. The iterator is
153/// not invalidated.
154void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
155 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000156 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000157
Chris Lattnerb26c2152004-07-23 19:38:44 +0000158 // Search for the first interval that we can't merge with.
159 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000160 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
161 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
162 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000163
164 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
165 I->end = std::max(NewEnd, prior(MergeTo)->end);
166
167 // Erase any dead ranges
168 ranges.erase(next(I), MergeTo);
169}
170
171
172/// extendIntervalStartTo - This method is used when we want to extend the range
173/// specified by I to start at the specified endpoint. To do this, we should
174/// merge and eliminate all ranges that this will overlap with.
175LiveInterval::Ranges::iterator
176LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
177 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000178 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000179
180 // Search for the first interval that we can't merge with.
181 Ranges::iterator MergeTo = I;
182 do {
183 if (MergeTo == ranges.begin()) {
184 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000185 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000186 return I;
187 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000188 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000189 --MergeTo;
190 } while (NewStart <= MergeTo->start);
191
192 // If we start in the middle of another interval, just delete a range and
193 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000194 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000195 MergeTo->end = I->end;
196 } else {
197 // Otherwise, extend the interval right after.
198 ++MergeTo;
199 MergeTo->start = NewStart;
200 MergeTo->end = I->end;
201 }
202
203 ranges.erase(next(MergeTo), next(I));
204 return MergeTo;
205}
206
207LiveInterval::Ranges::iterator
208LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
209 unsigned Start = LR.start, End = LR.end;
210 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
211
212 // If the inserted interval starts in the middle or right at the end of
213 // another interval, just extend that interval to contain the range of LR.
214 if (it != ranges.begin()) {
215 Ranges::iterator B = prior(it);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000216 if (LR.ValId == B->ValId) {
217 if (B->start <= Start && B->end >= Start) {
218 extendIntervalEndTo(B, End);
219 return B;
220 }
221 } else {
222 // Check to make sure that we are not overlapping two live ranges with
223 // different ValId's.
224 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000225 "Cannot overlap two LiveRanges with differing ValID's"
226 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000227 }
228 }
229
230 // Otherwise, if this range ends in the middle of, or right next to, another
231 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000232 if (it != ranges.end())
233 if (LR.ValId == it->ValId) {
234 if (it->start <= End) {
235 it = extendIntervalStartTo(it, Start);
236
237 // If LR is a complete superset of an interval, we may need to grow its
238 // endpoint as well.
239 if (End > it->end)
240 extendIntervalEndTo(it, End);
241 return it;
242 }
243 } else {
244 // Check to make sure that we are not overlapping two live ranges with
245 // different ValId's.
246 assert(it->start >= End &&
247 "Cannot overlap two LiveRanges with differing ValID's");
248 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000249
250 // Otherwise, this is just a new range that doesn't interact with anything.
251 // Insert it.
252 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000253}
254
Chris Lattnerabf295f2004-07-24 02:52:23 +0000255
256/// removeRange - Remove the specified range from this interval. Note that
257/// the range must already be in this interval in its entirety.
258void LiveInterval::removeRange(unsigned Start, unsigned End) {
259 // Find the LiveRange containing this span.
260 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
261 assert(I != ranges.begin() && "Range is not in interval!");
262 --I;
263 assert(I->contains(Start) && I->contains(End-1) &&
264 "Range is not entirely in interval!");
265
266 // If the span we are removing is at the start of the LiveRange, adjust it.
267 if (I->start == Start) {
268 if (I->end == End)
269 ranges.erase(I); // Removed the whole LiveRange.
270 else
271 I->start = End;
272 return;
273 }
274
275 // Otherwise if the span we are removing is at the end of the LiveRange,
276 // adjust the other way.
277 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000278 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000279 return;
280 }
281
282 // Otherwise, we are splitting the LiveRange into two pieces.
283 unsigned OldEnd = I->end;
284 I->end = Start; // Trim the old interval.
285
286 // Insert the new one.
287 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
288}
289
290/// getLiveRangeContaining - Return the live range that contains the
291/// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000292const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
293 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000294 if (It != ranges.begin()) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000295 const LiveRange &LR = *prior(It);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000296 if (LR.contains(Idx))
297 return &LR;
298 }
299
300 return 0;
301}
302
303
304
305/// join - Join two live intervals (this, and other) together. This operation
306/// is the result of a copy instruction in the source program, that occurs at
307/// index 'CopyIdx' that copies from 'Other' to 'this'.
308void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000309 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
310 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000311 assert(SourceLR && DestLR && "Not joining due to a copy?");
312 unsigned MergedSrcValIdx = SourceLR->ValId;
313 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000314
Chris Lattnerdeb99712004-07-24 03:41:50 +0000315 // Try to do the least amount of work possible. In particular, if there are
316 // more liverange chunks in the other set than there are in the 'this' set,
317 // swap sets to merge the fewest chunks in possible.
318 if (Other.ranges.size() > ranges.size()) {
319 std::swap(MergedSrcValIdx, MergedDstValIdx);
320 std::swap(ranges, Other.ranges);
321 std::swap(NumValues, Other.NumValues);
322 }
323
Chris Lattnerb26c2152004-07-23 19:38:44 +0000324 // Join the ranges of other into the ranges of this interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000325 Ranges::iterator InsertPos = ranges.begin();
326 std::map<unsigned, unsigned> Dst2SrcIdxMap;
327 for (Ranges::iterator I = Other.ranges.begin(),
328 E = Other.ranges.end(); I != E; ++I) {
329 // Map the ValId in the other live range to the current live range.
330 if (I->ValId == MergedSrcValIdx)
331 I->ValId = MergedDstValIdx;
332 else {
333 unsigned &NV = Dst2SrcIdxMap[I->ValId];
334 if (NV == 0) NV = getNextValue();
335 I->ValId = NV;
336 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000337
Chris Lattnerabf295f2004-07-24 02:52:23 +0000338 InsertPos = addRangeFrom(*I, InsertPos);
339 }
340
341 weight += Other.weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000342}
343
Chris Lattnerfb449b92004-07-23 17:49:16 +0000344std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000345 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000346}
347
Chris Lattnerabf295f2004-07-24 02:52:23 +0000348void LiveRange::dump() const {
349 std::cerr << *this << "\n";
350}
351
352
Chris Lattnerfb449b92004-07-23 17:49:16 +0000353std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
354 os << "%reg" << li.reg << ',' << li.weight;
355 if (li.empty())
356 return os << "EMPTY";
357
358 os << " = ";
359 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
360 e = li.ranges.end(); i != e; ++i)
361 os << *i;
362 return os;
363}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000364
365void LiveInterval::dump() const {
366 std::cerr << *this << "\n";
367}