blob: 18faacf44502f16e297297309414fc35b4393132 [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
Chris Lattner3c3fe462005-09-21 04:19:09 +000021#include "llvm/CodeGen/LiveInterval.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/STLExtras.h"
Chris Lattner38135af2005-05-14 05:34:15 +000023#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000024#include <algorithm>
Chris Lattnerabf295f2004-07-24 02:52:23 +000025#include <iostream>
26#include <map>
Chris Lattnerfb449b92004-07-23 17:49:16 +000027using namespace llvm;
28
29// An example for liveAt():
30//
Chris Lattneraa141472004-07-23 18:40:00 +000031// this = [1,4), liveAt(0) will return false. The instruction defining this
32// spans slots [0,3]. The interval belongs to an spilled definition of the
33// variable it represents. This is because slot 1 is used (def slot) and spans
34// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000035//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000036bool LiveInterval::liveAt(unsigned I) const {
37 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000038
Chris Lattnerfb449b92004-07-23 17:49:16 +000039 if (r == ranges.begin())
40 return false;
41
42 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000043 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000044}
45
Chris Lattnerbae74d92004-11-18 03:47:34 +000046// overlaps - Return true if the intersection of the two live intervals is
47// not empty.
48//
Chris Lattnerfb449b92004-07-23 17:49:16 +000049// An example for overlaps():
50//
51// 0: A = ...
52// 4: B = ...
53// 8: C = A + B ;; last use of A
54//
55// The live intervals should look like:
56//
57// A = [3, 11)
58// B = [7, x)
59// C = [11, y)
60//
61// A->overlaps(C) should return false since we want to be able to join
62// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000063//
64bool LiveInterval::overlapsFrom(const LiveInterval& other,
65 const_iterator StartPos) const {
66 const_iterator i = begin();
67 const_iterator ie = end();
68 const_iterator j = StartPos;
69 const_iterator je = other.end();
70
71 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000072 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000073
Chris Lattnerfb449b92004-07-23 17:49:16 +000074 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000075 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000076 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000077 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000078 ++StartPos;
79 if (StartPos != other.end() && StartPos->start <= i->start) {
80 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000081 j = std::upper_bound(j, je, i->start);
82 if (j != other.ranges.begin()) --j;
83 }
Chris Lattneraa141472004-07-23 18:40:00 +000084 } else {
85 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000086 }
87
Chris Lattner9fddc122004-11-18 05:28:21 +000088 if (j == je) return false;
89
90 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000091 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000092 std::swap(i, j);
93 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000094 }
Chris Lattnerfb449b92004-07-23 17:49:16 +000095
96 if (i->end > j->start)
97 return true;
98 ++i;
99 }
100
101 return false;
102}
103
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000104/// NontrivialOverlap - Check to see if the two live ranges specified by i and j
105/// overlap. If so, check to see if they have value numbers that are not
106/// iIdx/jIdx respectively. If both conditions are true, return true.
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000107static inline bool NontrivialOverlap(const LiveRange &I, const LiveRange &J,
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000108 unsigned iIdx, unsigned jIdx) {
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000109 if (I.start == J.start) {
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000110 // If this is not the allowed value merge, we cannot join.
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000111 if (I.ValId != iIdx || J.ValId != jIdx)
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000112 return true;
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000113 } else if (I.start < J.start) {
114 if (I.end > J.start && I.ValId != iIdx || J.ValId != jIdx) {
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000115 return true;
116 }
117 } else {
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000118 if (J.end > I.start &&
119 I.ValId != iIdx || J.ValId != jIdx)
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000120 return true;
121 }
122
123 return false;
124}
125
Chris Lattnerabf295f2004-07-24 02:52:23 +0000126/// joinable - Two intervals are joinable if the either don't overlap at all
127/// or if the destination of the copy is a single assignment value, and it
128/// only overlaps with one value in the source interval.
129bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
Chris Lattnerf5426492004-07-25 07:11:19 +0000130 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
131 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
132 assert(SourceLR && DestLR && "Not joining due to a copy?");
133 unsigned OtherValIdx = SourceLR->ValId;
134 unsigned ThisValIdx = DestLR->ValId;
135
136 Ranges::const_iterator i = ranges.begin();
137 Ranges::const_iterator ie = ranges.end();
138 Ranges::const_iterator j = other.ranges.begin();
139 Ranges::const_iterator je = other.ranges.end();
140
141 if (i->start < j->start) {
142 i = std::upper_bound(i, ie, j->start);
143 if (i != ranges.begin()) --i;
144 } else if (j->start < i->start) {
145 j = std::upper_bound(j, je, i->start);
146 if (j != other.ranges.begin()) --j;
147 }
148
149 while (i != ie && j != je) {
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000150 if (NontrivialOverlap(*i, *j, ThisValIdx, OtherValIdx))
Chris Lattnerf5ce2672005-10-20 06:06:30 +0000151 return false;
152
Chris Lattnerf5426492004-07-25 07:11:19 +0000153 if (i->end < j->end)
154 ++i;
155 else
156 ++j;
157 }
158
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000159 return true;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000160}
161
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000162/// getOverlapingRanges - Given another live interval which is defined as a
163/// copy from this one, return a list of all of the live ranges where the
164/// two overlap and have different value numbers.
165void LiveInterval::getOverlapingRanges(const LiveInterval &other,
166 unsigned CopyIdx,
167 std::vector<LiveRange*> &Ranges) {
168 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
169 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
170 assert(SourceLR && DestLR && "Not joining due to a copy?");
171 unsigned OtherValIdx = SourceLR->ValId;
172 unsigned ThisValIdx = DestLR->ValId;
173
174 Ranges::iterator i = ranges.begin();
175 Ranges::iterator ie = ranges.end();
176 Ranges::const_iterator j = other.ranges.begin();
177 Ranges::const_iterator je = other.ranges.end();
178
179 if (i->start < j->start) {
180 i = std::upper_bound(i, ie, j->start);
181 if (i != ranges.begin()) --i;
182 } else if (j->start < i->start) {
183 j = std::upper_bound(j, je, i->start);
184 if (j != other.ranges.begin()) --j;
185 }
186
187 while (i != ie && j != je) {
188 if (NontrivialOverlap(*i, *j, ThisValIdx, OtherValIdx))
189 Ranges.push_back(&*i);
190
191 if (i->end < j->end)
192 ++i;
193 else
194 ++j;
195 }
196}
197
198
Chris Lattnerabf295f2004-07-24 02:52:23 +0000199
Chris Lattnerb26c2152004-07-23 19:38:44 +0000200/// extendIntervalEndTo - This method is used when we want to extend the range
201/// specified by I to end at the specified endpoint. To do this, we should
202/// merge and eliminate all ranges that this will overlap with. The iterator is
203/// not invalidated.
204void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
205 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000206 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000207
Chris Lattnerb26c2152004-07-23 19:38:44 +0000208 // Search for the first interval that we can't merge with.
209 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000210 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
211 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
212 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000213
214 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
215 I->end = std::max(NewEnd, prior(MergeTo)->end);
216
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000217 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000218 ranges.erase(next(I), MergeTo);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000219
220 // If the newly formed range now touches the range after it and if they have
221 // the same value number, merge the two ranges into one range.
222 if (I != ranges.end()) {
223 Ranges::iterator Next = next(I);
224 if (Next->start == I->end && Next->ValId == ValId) {
225 I->end = Next->end;
226 ranges.erase(Next);
227 }
228 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000229}
230
231
232/// extendIntervalStartTo - This method is used when we want to extend the range
233/// specified by I to start at the specified endpoint. To do this, we should
234/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000235LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000236LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
237 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000238 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000239
240 // Search for the first interval that we can't merge with.
241 Ranges::iterator MergeTo = I;
242 do {
243 if (MergeTo == ranges.begin()) {
244 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000245 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000246 return I;
247 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000248 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000249 --MergeTo;
250 } while (NewStart <= MergeTo->start);
251
252 // If we start in the middle of another interval, just delete a range and
253 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000254 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000255 MergeTo->end = I->end;
256 } else {
257 // Otherwise, extend the interval right after.
258 ++MergeTo;
259 MergeTo->start = NewStart;
260 MergeTo->end = I->end;
261 }
262
263 ranges.erase(next(MergeTo), next(I));
264 return MergeTo;
265}
266
267LiveInterval::Ranges::iterator
268LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
269 unsigned Start = LR.start, End = LR.end;
270 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
271
272 // If the inserted interval starts in the middle or right at the end of
273 // another interval, just extend that interval to contain the range of LR.
274 if (it != ranges.begin()) {
275 Ranges::iterator B = prior(it);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000276 if (LR.ValId == B->ValId) {
277 if (B->start <= Start && B->end >= Start) {
278 extendIntervalEndTo(B, End);
279 return B;
280 }
281 } else {
282 // Check to make sure that we are not overlapping two live ranges with
283 // different ValId's.
284 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000285 "Cannot overlap two LiveRanges with differing ValID's"
286 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000287 }
288 }
289
290 // Otherwise, if this range ends in the middle of, or right next to, another
291 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000292 if (it != ranges.end())
293 if (LR.ValId == it->ValId) {
294 if (it->start <= End) {
295 it = extendIntervalStartTo(it, Start);
296
297 // If LR is a complete superset of an interval, we may need to grow its
298 // endpoint as well.
299 if (End > it->end)
300 extendIntervalEndTo(it, End);
301 return it;
302 }
303 } else {
304 // Check to make sure that we are not overlapping two live ranges with
305 // different ValId's.
306 assert(it->start >= End &&
307 "Cannot overlap two LiveRanges with differing ValID's");
308 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000309
310 // Otherwise, this is just a new range that doesn't interact with anything.
311 // Insert it.
312 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000313}
314
Chris Lattnerabf295f2004-07-24 02:52:23 +0000315
316/// removeRange - Remove the specified range from this interval. Note that
317/// the range must already be in this interval in its entirety.
318void LiveInterval::removeRange(unsigned Start, unsigned End) {
319 // Find the LiveRange containing this span.
320 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
321 assert(I != ranges.begin() && "Range is not in interval!");
322 --I;
323 assert(I->contains(Start) && I->contains(End-1) &&
324 "Range is not entirely in interval!");
325
326 // If the span we are removing is at the start of the LiveRange, adjust it.
327 if (I->start == Start) {
328 if (I->end == End)
329 ranges.erase(I); // Removed the whole LiveRange.
330 else
331 I->start = End;
332 return;
333 }
334
335 // Otherwise if the span we are removing is at the end of the LiveRange,
336 // adjust the other way.
337 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000338 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000339 return;
340 }
341
342 // Otherwise, we are splitting the LiveRange into two pieces.
343 unsigned OldEnd = I->end;
344 I->end = Start; // Trim the old interval.
345
346 // Insert the new one.
347 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
348}
349
350/// getLiveRangeContaining - Return the live range that contains the
351/// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000352const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
353 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000354 if (It != ranges.begin()) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000355 const LiveRange &LR = *prior(It);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000356 if (LR.contains(Idx))
357 return &LR;
358 }
359
360 return 0;
361}
362
363
364
365/// join - Join two live intervals (this, and other) together. This operation
366/// is the result of a copy instruction in the source program, that occurs at
367/// index 'CopyIdx' that copies from 'Other' to 'this'.
368void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000369 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
370 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000371 assert(SourceLR && DestLR && "Not joining due to a copy?");
372 unsigned MergedSrcValIdx = SourceLR->ValId;
373 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000374
Chris Lattnerdeb99712004-07-24 03:41:50 +0000375 // Try to do the least amount of work possible. In particular, if there are
376 // more liverange chunks in the other set than there are in the 'this' set,
377 // swap sets to merge the fewest chunks in possible.
378 if (Other.ranges.size() > ranges.size()) {
379 std::swap(MergedSrcValIdx, MergedDstValIdx);
380 std::swap(ranges, Other.ranges);
381 std::swap(NumValues, Other.NumValues);
382 }
383
Chris Lattnerb26c2152004-07-23 19:38:44 +0000384 // Join the ranges of other into the ranges of this interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000385 Ranges::iterator InsertPos = ranges.begin();
386 std::map<unsigned, unsigned> Dst2SrcIdxMap;
387 for (Ranges::iterator I = Other.ranges.begin(),
388 E = Other.ranges.end(); I != E; ++I) {
389 // Map the ValId in the other live range to the current live range.
390 if (I->ValId == MergedSrcValIdx)
391 I->ValId = MergedDstValIdx;
392 else {
393 unsigned &NV = Dst2SrcIdxMap[I->ValId];
394 if (NV == 0) NV = getNextValue();
395 I->ValId = NV;
396 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000397
Chris Lattnerabf295f2004-07-24 02:52:23 +0000398 InsertPos = addRangeFrom(*I, InsertPos);
399 }
400
401 weight += Other.weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000402}
403
Chris Lattnerfb449b92004-07-23 17:49:16 +0000404std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000405 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000406}
407
Chris Lattnerabf295f2004-07-24 02:52:23 +0000408void LiveRange::dump() const {
409 std::cerr << *this << "\n";
410}
411
Chris Lattner38135af2005-05-14 05:34:15 +0000412void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const {
413 if (MRI && MRegisterInfo::isPhysicalRegister(reg))
414 OS << MRI->getName(reg);
415 else
416 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000417
Chris Lattner38135af2005-05-14 05:34:15 +0000418 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000419
Chris Lattner38135af2005-05-14 05:34:15 +0000420 if (empty())
421 OS << "EMPTY";
422 else {
423 OS << " = ";
424 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
425 E = ranges.end(); I != E; ++I)
426 OS << *I;
427 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000428}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000429
430void LiveInterval::dump() const {
431 std::cerr << *this << "\n";
432}