blob: 29bee9bd6bf640b4a710a90822652404739497c7 [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"
22#include "Support/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();
63 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000064 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000065 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000066 } else if (j->start < i->start) {
67 j = std::upper_bound(j, je, i->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000068 if (j != other.ranges.begin()) --j;
Chris Lattneraa141472004-07-23 18:40:00 +000069 } else {
70 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000071 }
72
73 while (i != ie && j != je) {
74 if (i->start == j->start)
75 return true;
76
77 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000078 std::swap(i, j);
79 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000080 }
81 assert(i->start < j->start);
82
83 if (i->end > j->start)
84 return true;
85 ++i;
86 }
87
88 return false;
89}
90
Chris Lattnerabf295f2004-07-24 02:52:23 +000091/// joinable - Two intervals are joinable if the either don't overlap at all
92/// or if the destination of the copy is a single assignment value, and it
93/// only overlaps with one value in the source interval.
94bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
95 return overlaps(other);
96}
97
98
Chris Lattnerb26c2152004-07-23 19:38:44 +000099/// extendIntervalEndTo - This method is used when we want to extend the range
100/// specified by I to end at the specified endpoint. To do this, we should
101/// merge and eliminate all ranges that this will overlap with. The iterator is
102/// not invalidated.
103void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
104 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000105 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000106
Chris Lattnerb26c2152004-07-23 19:38:44 +0000107 // Search for the first interval that we can't merge with.
108 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000109 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
110 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
111 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000112
113 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
114 I->end = std::max(NewEnd, prior(MergeTo)->end);
115
116 // Erase any dead ranges
117 ranges.erase(next(I), MergeTo);
118}
119
120
121/// extendIntervalStartTo - This method is used when we want to extend the range
122/// specified by I to start at the specified endpoint. To do this, we should
123/// merge and eliminate all ranges that this will overlap with.
124LiveInterval::Ranges::iterator
125LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
126 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000127 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000128
129 // Search for the first interval that we can't merge with.
130 Ranges::iterator MergeTo = I;
131 do {
132 if (MergeTo == ranges.begin()) {
133 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000134 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000135 return I;
136 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000137 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000138 --MergeTo;
139 } while (NewStart <= MergeTo->start);
140
141 // If we start in the middle of another interval, just delete a range and
142 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000143 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000144 MergeTo->end = I->end;
145 } else {
146 // Otherwise, extend the interval right after.
147 ++MergeTo;
148 MergeTo->start = NewStart;
149 MergeTo->end = I->end;
150 }
151
152 ranges.erase(next(MergeTo), next(I));
153 return MergeTo;
154}
155
156LiveInterval::Ranges::iterator
157LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
158 unsigned Start = LR.start, End = LR.end;
159 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
160
161 // If the inserted interval starts in the middle or right at the end of
162 // another interval, just extend that interval to contain the range of LR.
163 if (it != ranges.begin()) {
164 Ranges::iterator B = prior(it);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000165 if (LR.ValId == B->ValId) {
166 if (B->start <= Start && B->end >= Start) {
167 extendIntervalEndTo(B, End);
168 return B;
169 }
170 } else {
171 // Check to make sure that we are not overlapping two live ranges with
172 // different ValId's.
173 assert(B->end <= Start &&
174 "Cannot overlap two LiveRanges with differing ValID's");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000175 }
176 }
177
178 // Otherwise, if this range ends in the middle of, or right next to, another
179 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000180 if (it != ranges.end())
181 if (LR.ValId == it->ValId) {
182 if (it->start <= End) {
183 it = extendIntervalStartTo(it, Start);
184
185 // If LR is a complete superset of an interval, we may need to grow its
186 // endpoint as well.
187 if (End > it->end)
188 extendIntervalEndTo(it, End);
189 return it;
190 }
191 } else {
192 // Check to make sure that we are not overlapping two live ranges with
193 // different ValId's.
194 assert(it->start >= End &&
195 "Cannot overlap two LiveRanges with differing ValID's");
196 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000197
198 // Otherwise, this is just a new range that doesn't interact with anything.
199 // Insert it.
200 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000201}
202
Chris Lattnerabf295f2004-07-24 02:52:23 +0000203
204/// removeRange - Remove the specified range from this interval. Note that
205/// the range must already be in this interval in its entirety.
206void LiveInterval::removeRange(unsigned Start, unsigned End) {
207 // Find the LiveRange containing this span.
208 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
209 assert(I != ranges.begin() && "Range is not in interval!");
210 --I;
211 assert(I->contains(Start) && I->contains(End-1) &&
212 "Range is not entirely in interval!");
213
214 // If the span we are removing is at the start of the LiveRange, adjust it.
215 if (I->start == Start) {
216 if (I->end == End)
217 ranges.erase(I); // Removed the whole LiveRange.
218 else
219 I->start = End;
220 return;
221 }
222
223 // Otherwise if the span we are removing is at the end of the LiveRange,
224 // adjust the other way.
225 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000226 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000227 return;
228 }
229
230 // Otherwise, we are splitting the LiveRange into two pieces.
231 unsigned OldEnd = I->end;
232 I->end = Start; // Trim the old interval.
233
234 // Insert the new one.
235 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
236}
237
238/// getLiveRangeContaining - Return the live range that contains the
239/// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000240const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
241 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000242 if (It != ranges.begin()) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000243 const LiveRange &LR = *prior(It);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000244 if (LR.contains(Idx))
245 return &LR;
246 }
247
248 return 0;
249}
250
251
252
253/// join - Join two live intervals (this, and other) together. This operation
254/// is the result of a copy instruction in the source program, that occurs at
255/// index 'CopyIdx' that copies from 'Other' to 'this'.
256void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000257 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
258 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000259 assert(SourceLR && DestLR && "Not joining due to a copy?");
260 unsigned MergedSrcValIdx = SourceLR->ValId;
261 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000262
Chris Lattnerdeb99712004-07-24 03:41:50 +0000263 // Try to do the least amount of work possible. In particular, if there are
264 // more liverange chunks in the other set than there are in the 'this' set,
265 // swap sets to merge the fewest chunks in possible.
266 if (Other.ranges.size() > ranges.size()) {
267 std::swap(MergedSrcValIdx, MergedDstValIdx);
268 std::swap(ranges, Other.ranges);
269 std::swap(NumValues, Other.NumValues);
270 }
271
Chris Lattnerb26c2152004-07-23 19:38:44 +0000272 // Join the ranges of other into the ranges of this interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000273 Ranges::iterator InsertPos = ranges.begin();
274 std::map<unsigned, unsigned> Dst2SrcIdxMap;
275 for (Ranges::iterator I = Other.ranges.begin(),
276 E = Other.ranges.end(); I != E; ++I) {
277 // Map the ValId in the other live range to the current live range.
278 if (I->ValId == MergedSrcValIdx)
279 I->ValId = MergedDstValIdx;
280 else {
281 unsigned &NV = Dst2SrcIdxMap[I->ValId];
282 if (NV == 0) NV = getNextValue();
283 I->ValId = NV;
284 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000285
Chris Lattnerabf295f2004-07-24 02:52:23 +0000286 InsertPos = addRangeFrom(*I, InsertPos);
287 }
288
289 weight += Other.weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000290}
291
Chris Lattnerfb449b92004-07-23 17:49:16 +0000292std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000293 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000294}
295
Chris Lattnerabf295f2004-07-24 02:52:23 +0000296void LiveRange::dump() const {
297 std::cerr << *this << "\n";
298}
299
300
Chris Lattnerfb449b92004-07-23 17:49:16 +0000301std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
302 os << "%reg" << li.reg << ',' << li.weight;
303 if (li.empty())
304 return os << "EMPTY";
305
306 os << " = ";
307 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
308 e = li.ranges.end(); i != e; ++i)
309 os << *i;
310 return os;
311}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000312
313void LiveInterval::dump() const {
314 std::cerr << *this << "\n";
315}