blob: d63cfc60fba7c417511f9a7ac7eaf729157c2292 [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"
Chris Lattnerabf295f2004-07-24 02:52:23 +000023#include <iostream>
24#include <map>
Chris Lattnerfb449b92004-07-23 17:49:16 +000025using namespace llvm;
26
27// An example for liveAt():
28//
Chris Lattneraa141472004-07-23 18:40:00 +000029// 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).
Chris Lattnerfb449b92004-07-23 17:49:16 +000033//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000034bool LiveInterval::liveAt(unsigned I) const {
35 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
36
Chris Lattnerfb449b92004-07-23 17:49:16 +000037 if (r == ranges.begin())
38 return false;
39
40 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000041 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000042}
43
44// An example for overlaps():
45//
46// 0: A = ...
47// 4: B = ...
48// 8: C = A + B ;; last use of A
49//
50// The live intervals should look like:
51//
52// A = [3, 11)
53// B = [7, x)
54// C = [11, y)
55//
56// A->overlaps(C) should return false since we want to be able to join
57// A and C.
58bool 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();
Chris Lattnerf5426492004-07-25 07:11:19 +000063
Chris Lattnerfb449b92004-07-23 17:49:16 +000064 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000065 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000066 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000067 } else if (j->start < i->start) {
68 j = std::upper_bound(j, je, i->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000069 if (j != other.ranges.begin()) --j;
Chris Lattneraa141472004-07-23 18:40:00 +000070 } else {
71 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000072 }
73
74 while (i != ie && j != je) {
75 if (i->start == j->start)
76 return true;
77
78 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000079 std::swap(i, j);
80 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000081 }
82 assert(i->start < j->start);
83
84 if (i->end > j->start)
85 return true;
86 ++i;
87 }
88
89 return false;
90}
91
Chris Lattnerabf295f2004-07-24 02:52:23 +000092/// 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.
95bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
Chris Lattnerf5426492004-07-25 07:11:19 +000096 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;
101
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();
106
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;
113 }
114
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)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000119 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000120 } else if (i->start < j->start) {
121 if (i->end > j->start) {
122 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000123 return false;
Chris Lattnerf5426492004-07-25 07:11:19 +0000124 }
125 } else {
126 if (j->end > i->start) {
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 }
130 }
131 if (i->end < j->end)
132 ++i;
133 else
134 ++j;
135 }
136
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000137 return true;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000138}
139
140
Chris Lattnerb26c2152004-07-23 19:38:44 +0000141/// 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
144/// not invalidated.
145void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
146 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000147 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000148
Chris Lattnerb26c2152004-07-23 19:38:44 +0000149 // Search for the first interval that we can't merge with.
150 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000151 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
152 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
153 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000154
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);
157
158 // Erase any dead ranges
159 ranges.erase(next(I), MergeTo);
160}
161
162
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.
166LiveInterval::Ranges::iterator
167LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
168 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000169 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000170
171 // Search for the first interval that we can't merge with.
172 Ranges::iterator MergeTo = I;
173 do {
174 if (MergeTo == ranges.begin()) {
175 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000176 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000177 return I;
178 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000179 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000180 --MergeTo;
181 } while (NewStart <= MergeTo->start);
182
183 // If we start in the middle of another interval, just delete a range and
184 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000185 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000186 MergeTo->end = I->end;
187 } else {
188 // Otherwise, extend the interval right after.
189 ++MergeTo;
190 MergeTo->start = NewStart;
191 MergeTo->end = I->end;
192 }
193
194 ranges.erase(next(MergeTo), next(I));
195 return MergeTo;
196}
197
198LiveInterval::Ranges::iterator
199LiveInterval::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);
202
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);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000207 if (LR.ValId == B->ValId) {
208 if (B->start <= Start && B->end >= Start) {
209 extendIntervalEndTo(B, End);
210 return B;
211 }
212 } else {
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");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000217 }
218 }
219
220 // Otherwise, if this range ends in the middle of, or right next to, another
221 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000222 if (it != ranges.end())
223 if (LR.ValId == it->ValId) {
224 if (it->start <= End) {
225 it = extendIntervalStartTo(it, Start);
226
227 // If LR is a complete superset of an interval, we may need to grow its
228 // endpoint as well.
229 if (End > it->end)
230 extendIntervalEndTo(it, End);
231 return it;
232 }
233 } else {
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");
238 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000239
240 // Otherwise, this is just a new range that doesn't interact with anything.
241 // Insert it.
242 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000243}
244
Chris Lattnerabf295f2004-07-24 02:52:23 +0000245
246/// removeRange - Remove the specified range from this interval. Note that
247/// the range must already be in this interval in its entirety.
248void 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!");
252 --I;
253 assert(I->contains(Start) && I->contains(End-1) &&
254 "Range is not entirely in interval!");
255
256 // If the span we are removing is at the start of the LiveRange, adjust it.
257 if (I->start == Start) {
258 if (I->end == End)
259 ranges.erase(I); // Removed the whole LiveRange.
260 else
261 I->start = End;
262 return;
263 }
264
265 // Otherwise if the span we are removing is at the end of the LiveRange,
266 // adjust the other way.
267 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000268 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000269 return;
270 }
271
272 // Otherwise, we are splitting the LiveRange into two pieces.
273 unsigned OldEnd = I->end;
274 I->end = Start; // Trim the old interval.
275
276 // Insert the new one.
277 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
278}
279
280/// getLiveRangeContaining - Return the live range that contains the
281/// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000282const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
283 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000284 if (It != ranges.begin()) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000285 const LiveRange &LR = *prior(It);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000286 if (LR.contains(Idx))
287 return &LR;
288 }
289
290 return 0;
291}
292
293
294
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'.
298void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000299 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
300 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000301 assert(SourceLR && DestLR && "Not joining due to a copy?");
302 unsigned MergedSrcValIdx = SourceLR->ValId;
303 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000304
Chris Lattnerdeb99712004-07-24 03:41:50 +0000305 // 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);
312 }
313
Chris Lattnerb26c2152004-07-23 19:38:44 +0000314 // Join the ranges of other into the ranges of this interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000315 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;
322 else {
323 unsigned &NV = Dst2SrcIdxMap[I->ValId];
324 if (NV == 0) NV = getNextValue();
325 I->ValId = NV;
326 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000327
Chris Lattnerabf295f2004-07-24 02:52:23 +0000328 InsertPos = addRangeFrom(*I, InsertPos);
329 }
330
331 weight += Other.weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000332}
333
Chris Lattnerfb449b92004-07-23 17:49:16 +0000334std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000335 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000336}
337
Chris Lattnerabf295f2004-07-24 02:52:23 +0000338void LiveRange::dump() const {
339 std::cerr << *this << "\n";
340}
341
342
Chris Lattnerfb449b92004-07-23 17:49:16 +0000343std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
344 os << "%reg" << li.reg << ',' << li.weight;
345 if (li.empty())
346 return os << "EMPTY";
347
348 os << " = ";
349 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
350 e = li.ranges.end(); i != e; ++i)
351 os << *i;
352 return os;
353}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000354
355void LiveInterval::dump() const {
356 std::cerr << *this << "\n";
357}