blob: 8306116b1a84c9f5a9687c20457d9e9b7479aa45 [file] [log] [blame]
Chris Lattner78f62e32004-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 Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenosfc59e0e2004-09-28 02:38:58 +000023#include <algorithm>
Chris Lattner038747f2004-07-24 02:52:23 +000024#include <iostream>
25#include <map>
Chris Lattner78f62e32004-07-23 17:49:16 +000026using namespace llvm;
27
28// An example for liveAt():
29//
Chris Lattner2fcc5e42004-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 Lattner78f62e32004-07-23 17:49:16 +000034//
Chris Lattnerc96d2992004-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 Lattner78f62e32004-07-23 17:49:16 +000038 if (r == ranges.begin())
39 return false;
40
41 --r;
Chris Lattner2fcc5e42004-07-23 18:40:00 +000042 return r->contains(I);
Chris Lattner78f62e32004-07-23 17:49:16 +000043}
44
45// An example for overlaps():
46//
47// 0: A = ...
48// 4: B = ...
49// 8: C = A + B ;; last use of A
50//
51// The live intervals should look like:
52//
53// A = [3, 11)
54// B = [7, x)
55// C = [11, y)
56//
57// A->overlaps(C) should return false since we want to be able to join
58// A and C.
59bool LiveInterval::overlaps(const LiveInterval& other) const {
60 Ranges::const_iterator i = ranges.begin();
61 Ranges::const_iterator ie = ranges.end();
62 Ranges::const_iterator j = other.ranges.begin();
63 Ranges::const_iterator je = other.ranges.end();
Chris Lattnerccc75d4f2004-07-25 07:11:19 +000064
Chris Lattner78f62e32004-07-23 17:49:16 +000065 if (i->start < j->start) {
Chris Lattner2fcc5e42004-07-23 18:40:00 +000066 i = std::upper_bound(i, ie, j->start);
Chris Lattner78f62e32004-07-23 17:49:16 +000067 if (i != ranges.begin()) --i;
Chris Lattner2fcc5e42004-07-23 18:40:00 +000068 } else if (j->start < i->start) {
69 j = std::upper_bound(j, je, i->start);
Chris Lattner78f62e32004-07-23 17:49:16 +000070 if (j != other.ranges.begin()) --j;
Chris Lattner2fcc5e42004-07-23 18:40:00 +000071 } else {
72 return true;
Chris Lattner78f62e32004-07-23 17:49:16 +000073 }
74
75 while (i != ie && j != je) {
76 if (i->start == j->start)
77 return true;
78
79 if (i->start > j->start) {
Alkis Evlogimenoscf72e7f2004-07-24 11:44:15 +000080 std::swap(i, j);
81 std::swap(ie, je);
Chris Lattner78f62e32004-07-23 17:49:16 +000082 }
83 assert(i->start < j->start);
84
85 if (i->end > j->start)
86 return true;
87 ++i;
88 }
89
90 return false;
91}
92
Chris Lattner038747f2004-07-24 02:52:23 +000093/// joinable - Two intervals are joinable if the either don't overlap at all
94/// or if the destination of the copy is a single assignment value, and it
95/// only overlaps with one value in the source interval.
96bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
Chris Lattnerccc75d4f2004-07-25 07:11:19 +000097 const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
98 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
99 assert(SourceLR && DestLR && "Not joining due to a copy?");
100 unsigned OtherValIdx = SourceLR->ValId;
101 unsigned ThisValIdx = DestLR->ValId;
102
103 Ranges::const_iterator i = ranges.begin();
104 Ranges::const_iterator ie = ranges.end();
105 Ranges::const_iterator j = other.ranges.begin();
106 Ranges::const_iterator je = other.ranges.end();
107
108 if (i->start < j->start) {
109 i = std::upper_bound(i, ie, j->start);
110 if (i != ranges.begin()) --i;
111 } else if (j->start < i->start) {
112 j = std::upper_bound(j, je, i->start);
113 if (j != other.ranges.begin()) --j;
114 }
115
116 while (i != ie && j != je) {
117 if (i->start == j->start) {
118 // If this is not the allowed value merge, we cannot join.
119 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerbbe845b2004-07-25 07:47:25 +0000120 return false;
Chris Lattnerccc75d4f2004-07-25 07:11:19 +0000121 } else if (i->start < j->start) {
122 if (i->end > j->start) {
123 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerbbe845b2004-07-25 07:47:25 +0000124 return false;
Chris Lattnerccc75d4f2004-07-25 07:11:19 +0000125 }
126 } else {
127 if (j->end > i->start) {
128 if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
Chris Lattnerbbe845b2004-07-25 07:47:25 +0000129 return false;
Chris Lattnerccc75d4f2004-07-25 07:11:19 +0000130 }
131 }
132 if (i->end < j->end)
133 ++i;
134 else
135 ++j;
136 }
137
Chris Lattnerbbe845b2004-07-25 07:47:25 +0000138 return true;
Chris Lattner038747f2004-07-24 02:52:23 +0000139}
140
141
Chris Lattnerb4acba42004-07-23 19:38:44 +0000142/// extendIntervalEndTo - This method is used when we want to extend the range
143/// specified by I to end at the specified endpoint. To do this, we should
144/// merge and eliminate all ranges that this will overlap with. The iterator is
145/// not invalidated.
146void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
147 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattner038747f2004-07-24 02:52:23 +0000148 unsigned ValId = I->ValId;
Chris Lattner78f62e32004-07-23 17:49:16 +0000149
Chris Lattnerb4acba42004-07-23 19:38:44 +0000150 // Search for the first interval that we can't merge with.
151 Ranges::iterator MergeTo = next(I);
Chris Lattner038747f2004-07-24 02:52:23 +0000152 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
153 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
154 }
Chris Lattnerb4acba42004-07-23 19:38:44 +0000155
156 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
157 I->end = std::max(NewEnd, prior(MergeTo)->end);
158
159 // Erase any dead ranges
160 ranges.erase(next(I), MergeTo);
161}
162
163
164/// extendIntervalStartTo - This method is used when we want to extend the range
165/// specified by I to start at the specified endpoint. To do this, we should
166/// merge and eliminate all ranges that this will overlap with.
167LiveInterval::Ranges::iterator
168LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
169 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattner038747f2004-07-24 02:52:23 +0000170 unsigned ValId = I->ValId;
Chris Lattnerb4acba42004-07-23 19:38:44 +0000171
172 // Search for the first interval that we can't merge with.
173 Ranges::iterator MergeTo = I;
174 do {
175 if (MergeTo == ranges.begin()) {
176 I->start = NewStart;
Chris Lattner038747f2004-07-24 02:52:23 +0000177 ranges.erase(MergeTo, I);
Chris Lattnerb4acba42004-07-23 19:38:44 +0000178 return I;
179 }
Chris Lattner038747f2004-07-24 02:52:23 +0000180 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb4acba42004-07-23 19:38:44 +0000181 --MergeTo;
182 } while (NewStart <= MergeTo->start);
183
184 // If we start in the middle of another interval, just delete a range and
185 // extend that interval.
Chris Lattner038747f2004-07-24 02:52:23 +0000186 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb4acba42004-07-23 19:38:44 +0000187 MergeTo->end = I->end;
188 } else {
189 // Otherwise, extend the interval right after.
190 ++MergeTo;
191 MergeTo->start = NewStart;
192 MergeTo->end = I->end;
193 }
194
195 ranges.erase(next(MergeTo), next(I));
196 return MergeTo;
197}
198
199LiveInterval::Ranges::iterator
200LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
201 unsigned Start = LR.start, End = LR.end;
202 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
203
204 // If the inserted interval starts in the middle or right at the end of
205 // another interval, just extend that interval to contain the range of LR.
206 if (it != ranges.begin()) {
207 Ranges::iterator B = prior(it);
Chris Lattner038747f2004-07-24 02:52:23 +0000208 if (LR.ValId == B->ValId) {
209 if (B->start <= Start && B->end >= Start) {
210 extendIntervalEndTo(B, End);
211 return B;
212 }
213 } else {
214 // Check to make sure that we are not overlapping two live ranges with
215 // different ValId's.
216 assert(B->end <= Start &&
Brian Gaekea057cd22004-11-16 06:52:35 +0000217 "Cannot overlap two LiveRanges with differing ValID's"
218 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb4acba42004-07-23 19:38:44 +0000219 }
220 }
221
222 // Otherwise, if this range ends in the middle of, or right next to, another
223 // interval, merge it into that interval.
Chris Lattner038747f2004-07-24 02:52:23 +0000224 if (it != ranges.end())
225 if (LR.ValId == it->ValId) {
226 if (it->start <= End) {
227 it = extendIntervalStartTo(it, Start);
228
229 // If LR is a complete superset of an interval, we may need to grow its
230 // endpoint as well.
231 if (End > it->end)
232 extendIntervalEndTo(it, End);
233 return it;
234 }
235 } else {
236 // Check to make sure that we are not overlapping two live ranges with
237 // different ValId's.
238 assert(it->start >= End &&
239 "Cannot overlap two LiveRanges with differing ValID's");
240 }
Chris Lattnerb4acba42004-07-23 19:38:44 +0000241
242 // Otherwise, this is just a new range that doesn't interact with anything.
243 // Insert it.
244 return ranges.insert(it, LR);
Chris Lattner78f62e32004-07-23 17:49:16 +0000245}
246
Chris Lattner038747f2004-07-24 02:52:23 +0000247
248/// removeRange - Remove the specified range from this interval. Note that
249/// the range must already be in this interval in its entirety.
250void LiveInterval::removeRange(unsigned Start, unsigned End) {
251 // Find the LiveRange containing this span.
252 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
253 assert(I != ranges.begin() && "Range is not in interval!");
254 --I;
255 assert(I->contains(Start) && I->contains(End-1) &&
256 "Range is not entirely in interval!");
257
258 // If the span we are removing is at the start of the LiveRange, adjust it.
259 if (I->start == Start) {
260 if (I->end == End)
261 ranges.erase(I); // Removed the whole LiveRange.
262 else
263 I->start = End;
264 return;
265 }
266
267 // Otherwise if the span we are removing is at the end of the LiveRange,
268 // adjust the other way.
269 if (I->end == End) {
Chris Lattneraf7e8982004-07-25 05:43:53 +0000270 I->end = Start;
Chris Lattner038747f2004-07-24 02:52:23 +0000271 return;
272 }
273
274 // Otherwise, we are splitting the LiveRange into two pieces.
275 unsigned OldEnd = I->end;
276 I->end = Start; // Trim the old interval.
277
278 // Insert the new one.
279 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
280}
281
282/// getLiveRangeContaining - Return the live range that contains the
283/// specified index, or null if there is none.
Chris Lattnerc8002d42004-07-25 06:23:01 +0000284const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
285 Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
Chris Lattner038747f2004-07-24 02:52:23 +0000286 if (It != ranges.begin()) {
Chris Lattnerc8002d42004-07-25 06:23:01 +0000287 const LiveRange &LR = *prior(It);
Chris Lattner038747f2004-07-24 02:52:23 +0000288 if (LR.contains(Idx))
289 return &LR;
290 }
291
292 return 0;
293}
294
295
296
297/// join - Join two live intervals (this, and other) together. This operation
298/// is the result of a copy instruction in the source program, that occurs at
299/// index 'CopyIdx' that copies from 'Other' to 'this'.
300void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
Chris Lattnerc8002d42004-07-25 06:23:01 +0000301 const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
302 const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
Chris Lattner038747f2004-07-24 02:52:23 +0000303 assert(SourceLR && DestLR && "Not joining due to a copy?");
304 unsigned MergedSrcValIdx = SourceLR->ValId;
305 unsigned MergedDstValIdx = DestLR->ValId;
Chris Lattner78f62e32004-07-23 17:49:16 +0000306
Chris Lattnerd9bbbb82004-07-24 03:41:50 +0000307 // Try to do the least amount of work possible. In particular, if there are
308 // more liverange chunks in the other set than there are in the 'this' set,
309 // swap sets to merge the fewest chunks in possible.
310 if (Other.ranges.size() > ranges.size()) {
311 std::swap(MergedSrcValIdx, MergedDstValIdx);
312 std::swap(ranges, Other.ranges);
313 std::swap(NumValues, Other.NumValues);
314 }
315
Chris Lattnerb4acba42004-07-23 19:38:44 +0000316 // Join the ranges of other into the ranges of this interval.
Chris Lattner038747f2004-07-24 02:52:23 +0000317 Ranges::iterator InsertPos = ranges.begin();
318 std::map<unsigned, unsigned> Dst2SrcIdxMap;
319 for (Ranges::iterator I = Other.ranges.begin(),
320 E = Other.ranges.end(); I != E; ++I) {
321 // Map the ValId in the other live range to the current live range.
322 if (I->ValId == MergedSrcValIdx)
323 I->ValId = MergedDstValIdx;
324 else {
325 unsigned &NV = Dst2SrcIdxMap[I->ValId];
326 if (NV == 0) NV = getNextValue();
327 I->ValId = NV;
328 }
Chris Lattnerb4acba42004-07-23 19:38:44 +0000329
Chris Lattner038747f2004-07-24 02:52:23 +0000330 InsertPos = addRangeFrom(*I, InsertPos);
331 }
332
333 weight += Other.weight;
Chris Lattner78f62e32004-07-23 17:49:16 +0000334}
335
Chris Lattner78f62e32004-07-23 17:49:16 +0000336std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattner038747f2004-07-24 02:52:23 +0000337 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattner78f62e32004-07-23 17:49:16 +0000338}
339
Chris Lattner038747f2004-07-24 02:52:23 +0000340void LiveRange::dump() const {
341 std::cerr << *this << "\n";
342}
343
344
Chris Lattner78f62e32004-07-23 17:49:16 +0000345std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
346 os << "%reg" << li.reg << ',' << li.weight;
347 if (li.empty())
348 return os << "EMPTY";
349
350 os << " = ";
351 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
352 e = li.ranges.end(); i != e; ++i)
353 os << *i;
354 return os;
355}
Chris Lattner038747f2004-07-24 02:52:23 +0000356
357void LiveInterval::dump() const {
358 std::cerr << *this << "\n";
359}