blob: a8bc1615b4113c364ef2cc462e20607634136fc3 [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 Lattneread1b3f2004-12-04 01:22:09 +000077 ++StartPos;
78 if (StartPos != other.end() && StartPos->start <= i->start) {
79 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000080 j = std::upper_bound(j, je, i->start);
81 if (j != other.ranges.begin()) --j;
82 }
Chris Lattneraa141472004-07-23 18:40:00 +000083 } else {
84 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000085 }
86
Chris Lattner9fddc122004-11-18 05:28:21 +000087 if (j == je) return false;
88
89 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000090 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000091 std::swap(i, j);
92 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000093 }
Chris Lattnerfb449b92004-07-23 17:49:16 +000094
95 if (i->end > j->start)
96 return true;
97 ++i;
98 }
99
100 return false;
101}
102
Chris Lattnerabf295f2004-07-24 02:52:23 +0000103/// joinable - Two intervals are joinable if the either don't overlap at all
104/// or if the destination of the copy is a single assignment value, and it
105/// only overlaps with one value in the source interval.
106bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
Chris Lattnerf5426492004-07-25 07:11:19 +0000107 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
108 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
109 assert(SourceLR && DestLR && "Not joining due to a copy?");
110 unsigned OtherValIdx = SourceLR->ValId;
111 unsigned ThisValIdx = DestLR->ValId;
112
113 Ranges::const_iterator i = ranges.begin();
114 Ranges::const_iterator ie = ranges.end();
115 Ranges::const_iterator j = other.ranges.begin();
116 Ranges::const_iterator je = other.ranges.end();
117
118 if (i->start < j->start) {
119 i = std::upper_bound(i, ie, j->start);
120 if (i != ranges.begin()) --i;
121 } else if (j->start < i->start) {
122 j = std::upper_bound(j, je, i->start);
123 if (j != other.ranges.begin()) --j;
124 }
125
126 while (i != ie && j != je) {
127 if (i->start == j->start) {
128 // If this is not the allowed value merge, we cannot join.
129 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000130 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000131 } else if (i->start < j->start) {
132 if (i->end > j->start) {
133 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000134 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000135 }
136 } else {
137 if (j->end > i->start) {
138 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000139 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000140 }
141 }
142 if (i->end < j->end)
143 ++i;
144 else
145 ++j;
146 }
147
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000148 return true;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000149}
150
151
Chris Lattnerb26c2152004-07-23 19:38:44 +0000152/// extendIntervalEndTo - This method is used when we want to extend the range
153/// specified by I to end at the specified endpoint. To do this, we should
154/// merge and eliminate all ranges that this will overlap with. The iterator is
155/// not invalidated.
156void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
157 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000158 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000159
Chris Lattnerb26c2152004-07-23 19:38:44 +0000160 // Search for the first interval that we can't merge with.
161 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000162 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
163 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
164 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000165
166 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
167 I->end = std::max(NewEnd, prior(MergeTo)->end);
168
169 // Erase any dead ranges
170 ranges.erase(next(I), MergeTo);
171}
172
173
174/// extendIntervalStartTo - This method is used when we want to extend the range
175/// specified by I to start at the specified endpoint. To do this, we should
176/// merge and eliminate all ranges that this will overlap with.
177LiveInterval::Ranges::iterator
178LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
179 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000180 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000181
182 // Search for the first interval that we can't merge with.
183 Ranges::iterator MergeTo = I;
184 do {
185 if (MergeTo == ranges.begin()) {
186 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000187 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000188 return I;
189 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000190 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000191 --MergeTo;
192 } while (NewStart <= MergeTo->start);
193
194 // If we start in the middle of another interval, just delete a range and
195 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000196 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000197 MergeTo->end = I->end;
198 } else {
199 // Otherwise, extend the interval right after.
200 ++MergeTo;
201 MergeTo->start = NewStart;
202 MergeTo->end = I->end;
203 }
204
205 ranges.erase(next(MergeTo), next(I));
206 return MergeTo;
207}
208
209LiveInterval::Ranges::iterator
210LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
211 unsigned Start = LR.start, End = LR.end;
212 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
213
214 // If the inserted interval starts in the middle or right at the end of
215 // another interval, just extend that interval to contain the range of LR.
216 if (it != ranges.begin()) {
217 Ranges::iterator B = prior(it);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000218 if (LR.ValId == B->ValId) {
219 if (B->start <= Start && B->end >= Start) {
220 extendIntervalEndTo(B, End);
221 return B;
222 }
223 } else {
224 // Check to make sure that we are not overlapping two live ranges with
225 // different ValId's.
226 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000227 "Cannot overlap two LiveRanges with differing ValID's"
228 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000229 }
230 }
231
232 // Otherwise, if this range ends in the middle of, or right next to, another
233 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000234 if (it != ranges.end())
235 if (LR.ValId == it->ValId) {
236 if (it->start <= End) {
237 it = extendIntervalStartTo(it, Start);
238
239 // If LR is a complete superset of an interval, we may need to grow its
240 // endpoint as well.
241 if (End > it->end)
242 extendIntervalEndTo(it, End);
243 return it;
244 }
245 } else {
246 // Check to make sure that we are not overlapping two live ranges with
247 // different ValId's.
248 assert(it->start >= End &&
249 "Cannot overlap two LiveRanges with differing ValID's");
250 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000251
252 // Otherwise, this is just a new range that doesn't interact with anything.
253 // Insert it.
254 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000255}
256
Chris Lattnerabf295f2004-07-24 02:52:23 +0000257
258/// removeRange - Remove the specified range from this interval. Note that
259/// the range must already be in this interval in its entirety.
260void LiveInterval::removeRange(unsigned Start, unsigned End) {
261 // Find the LiveRange containing this span.
262 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
263 assert(I != ranges.begin() && "Range is not in interval!");
264 --I;
265 assert(I->contains(Start) && I->contains(End-1) &&
266 "Range is not entirely in interval!");
267
268 // If the span we are removing is at the start of the LiveRange, adjust it.
269 if (I->start == Start) {
270 if (I->end == End)
271 ranges.erase(I); // Removed the whole LiveRange.
272 else
273 I->start = End;
274 return;
275 }
276
277 // Otherwise if the span we are removing is at the end of the LiveRange,
278 // adjust the other way.
279 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000280 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000281 return;
282 }
283
284 // Otherwise, we are splitting the LiveRange into two pieces.
285 unsigned OldEnd = I->end;
286 I->end = Start; // Trim the old interval.
287
288 // Insert the new one.
289 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
290}
291
292/// getLiveRangeContaining - Return the live range that contains the
293/// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000294const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
295 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000296 if (It != ranges.begin()) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000297 const LiveRange &LR = *prior(It);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000298 if (LR.contains(Idx))
299 return &LR;
300 }
301
302 return 0;
303}
304
305
306
307/// join - Join two live intervals (this, and other) together. This operation
308/// is the result of a copy instruction in the source program, that occurs at
309/// index 'CopyIdx' that copies from 'Other' to 'this'.
310void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000311 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
312 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000313 assert(SourceLR && DestLR && "Not joining due to a copy?");
314 unsigned MergedSrcValIdx = SourceLR->ValId;
315 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000316
Chris Lattnerdeb99712004-07-24 03:41:50 +0000317 // Try to do the least amount of work possible. In particular, if there are
318 // more liverange chunks in the other set than there are in the 'this' set,
319 // swap sets to merge the fewest chunks in possible.
320 if (Other.ranges.size() > ranges.size()) {
321 std::swap(MergedSrcValIdx, MergedDstValIdx);
322 std::swap(ranges, Other.ranges);
323 std::swap(NumValues, Other.NumValues);
324 }
325
Chris Lattnerb26c2152004-07-23 19:38:44 +0000326 // Join the ranges of other into the ranges of this interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000327 Ranges::iterator InsertPos = ranges.begin();
328 std::map<unsigned, unsigned> Dst2SrcIdxMap;
329 for (Ranges::iterator I = Other.ranges.begin(),
330 E = Other.ranges.end(); I != E; ++I) {
331 // Map the ValId in the other live range to the current live range.
332 if (I->ValId == MergedSrcValIdx)
333 I->ValId = MergedDstValIdx;
334 else {
335 unsigned &NV = Dst2SrcIdxMap[I->ValId];
336 if (NV == 0) NV = getNextValue();
337 I->ValId = NV;
338 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000339
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340 InsertPos = addRangeFrom(*I, InsertPos);
341 }
342
343 weight += Other.weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000344}
345
Chris Lattnerfb449b92004-07-23 17:49:16 +0000346std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000347 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000348}
349
Chris Lattnerabf295f2004-07-24 02:52:23 +0000350void LiveRange::dump() const {
351 std::cerr << *this << "\n";
352}
353
354
Chris Lattnerfb449b92004-07-23 17:49:16 +0000355std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
356 os << "%reg" << li.reg << ',' << li.weight;
357 if (li.empty())
358 return os << "EMPTY";
359
360 os << " = ";
361 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
362 e = li.ranges.end(); i != e; ++i)
363 os << *i;
364 return os;
365}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000366
367void LiveInterval::dump() const {
368 std::cerr << *this << "\n";
369}