blob: 741c35c43a8b61ca113eb84926af46b83e2d23a2 [file] [log] [blame]
Chris Lattnerfb449b92004-07-23 17:49:16 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfb449b92004-07-23 17:49:16 +00007//
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
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000021#include "llvm/CodeGen/LiveInterval.h"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000022#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000023#include "llvm/ADT/STLExtras.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000024#include "llvm/Support/Streams.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000026#include <algorithm>
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000027#include <ostream>
Chris Lattnerfb449b92004-07-23 17:49:16 +000028using namespace llvm;
29
30// An example for liveAt():
31//
Chris Lattneraa141472004-07-23 18:40:00 +000032// this = [1,4), liveAt(0) will return false. The instruction defining this
33// spans slots [0,3]. The interval belongs to an spilled definition of the
34// variable it represents. This is because slot 1 is used (def slot) and spans
35// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000036//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000037bool LiveInterval::liveAt(unsigned I) const {
38 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000039
Chris Lattnerfb449b92004-07-23 17:49:16 +000040 if (r == ranges.begin())
41 return false;
42
43 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000044 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000045}
46
Evan Chengc8d044e2008-02-15 18:24:29 +000047// liveBeforeAndAt - Check if the interval is live at the index and the index
48// just before it. If index is liveAt, check if it starts a new live range.
49// If it does, then check if the previous live range ends at index-1.
50bool LiveInterval::liveBeforeAndAt(unsigned I) const {
51 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
52
53 if (r == ranges.begin())
54 return false;
55
56 --r;
57 if (!r->contains(I))
58 return false;
59 if (I != r->start)
60 return true;
61 // I is the start of a live range. Check if the previous live range ends
62 // at I-1.
63 if (r == ranges.begin())
64 return false;
65 return r->end == I;
66}
67
Chris Lattnerbae74d92004-11-18 03:47:34 +000068// overlaps - Return true if the intersection of the two live intervals is
69// not empty.
70//
Chris Lattnerfb449b92004-07-23 17:49:16 +000071// An example for overlaps():
72//
73// 0: A = ...
74// 4: B = ...
75// 8: C = A + B ;; last use of A
76//
77// The live intervals should look like:
78//
79// A = [3, 11)
80// B = [7, x)
81// C = [11, y)
82//
83// A->overlaps(C) should return false since we want to be able to join
84// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000085//
86bool LiveInterval::overlapsFrom(const LiveInterval& other,
87 const_iterator StartPos) const {
88 const_iterator i = begin();
89 const_iterator ie = end();
90 const_iterator j = StartPos;
91 const_iterator je = other.end();
92
93 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000094 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000095
Chris Lattnerfb449b92004-07-23 17:49:16 +000096 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000097 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000098 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000099 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000100 ++StartPos;
101 if (StartPos != other.end() && StartPos->start <= i->start) {
102 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000103 j = std::upper_bound(j, je, i->start);
104 if (j != other.ranges.begin()) --j;
105 }
Chris Lattneraa141472004-07-23 18:40:00 +0000106 } else {
107 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000108 }
109
Chris Lattner9fddc122004-11-18 05:28:21 +0000110 if (j == je) return false;
111
112 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000113 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000114 std::swap(i, j);
115 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000116 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000117
118 if (i->end > j->start)
119 return true;
120 ++i;
121 }
122
123 return false;
124}
125
Chris Lattnerb26c2152004-07-23 19:38:44 +0000126/// extendIntervalEndTo - This method is used when we want to extend the range
127/// specified by I to end at the specified endpoint. To do this, we should
128/// merge and eliminate all ranges that this will overlap with. The iterator is
129/// not invalidated.
130void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
131 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000132 VNInfo *ValNo = I->valno;
Evan Cheng430a7b02007-08-14 01:56:58 +0000133 unsigned OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000134
Chris Lattnerb26c2152004-07-23 19:38:44 +0000135 // Search for the first interval that we can't merge with.
136 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000137 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000138 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000139 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000140
141 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
142 I->end = std::max(NewEnd, prior(MergeTo)->end);
143
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000144 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000145 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000146
147 // Update kill info.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000148 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000149
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000150 // If the newly formed range now touches the range after it and if they have
151 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000152 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000153 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000154 I->end = Next->end;
155 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000156 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000157}
158
159
160/// extendIntervalStartTo - This method is used when we want to extend the range
161/// specified by I to start at the specified endpoint. To do this, we should
162/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000163LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000164LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
165 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000166 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000167
168 // Search for the first interval that we can't merge with.
169 Ranges::iterator MergeTo = I;
170 do {
171 if (MergeTo == ranges.begin()) {
172 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000173 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000174 return I;
175 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000176 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000177 --MergeTo;
178 } while (NewStart <= MergeTo->start);
179
180 // If we start in the middle of another interval, just delete a range and
181 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000182 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000183 MergeTo->end = I->end;
184 } else {
185 // Otherwise, extend the interval right after.
186 ++MergeTo;
187 MergeTo->start = NewStart;
188 MergeTo->end = I->end;
189 }
190
191 ranges.erase(next(MergeTo), next(I));
192 return MergeTo;
193}
194
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000195LiveInterval::iterator
196LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000197 unsigned Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000198 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000199
200 // If the inserted interval starts in the middle or right at the end of
201 // another interval, just extend that interval to contain the range of LR.
202 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000203 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000204 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000205 if (B->start <= Start && B->end >= Start) {
206 extendIntervalEndTo(B, End);
207 return B;
208 }
209 } else {
210 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000211 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000212 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000213 "Cannot overlap two LiveRanges with differing ValID's"
214 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000215 }
216 }
217
218 // Otherwise, if this range ends in the middle of, or right next to, another
219 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000220 if (it != ranges.end())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000222 if (it->start <= End) {
223 it = extendIntervalStartTo(it, Start);
224
225 // If LR is a complete superset of an interval, we may need to grow its
226 // endpoint as well.
227 if (End > it->end)
228 extendIntervalEndTo(it, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000229 else if (End < it->end)
Evan Chengc3868e02007-11-29 01:05:47 +0000230 // Overlapping intervals, there might have been a kill here.
231 removeKill(it->valno, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000232 return it;
233 }
234 } else {
235 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000236 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000237 assert(it->start >= End &&
238 "Cannot overlap two LiveRanges with differing ValID's");
239 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000240
241 // Otherwise, this is just a new range that doesn't interact with anything.
242 // Insert it.
243 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000244}
245
Chris Lattnerabf295f2004-07-24 02:52:23 +0000246
247/// removeRange - Remove the specified range from this interval. Note that
248/// the range must already be in this interval in its entirety.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000249void LiveInterval::removeRange(unsigned Start, unsigned End,
250 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000251 // 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.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000259 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000260 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000261 if (I->end == End) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000262 removeKills(I->valno, Start, End);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000263 if (RemoveDeadValNo) {
264 // Check if val# is dead.
265 bool isDead = true;
266 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
267 if (II != I && II->valno == ValNo) {
268 isDead = false;
269 break;
270 }
271 if (isDead) {
272 // Now that ValNo is dead, remove it. If it is the largest value
273 // number, just nuke it (and any other deleted values neighboring it),
274 // otherwise mark it as ~1U so it can be nuked later.
275 if (ValNo->id == getNumValNums()-1) {
276 do {
277 VNInfo *VNI = valnos.back();
278 valnos.pop_back();
279 VNI->~VNInfo();
280 } while (!valnos.empty() && valnos.back()->def == ~1U);
281 } else {
282 ValNo->def = ~1U;
283 }
284 }
285 }
286
Chris Lattnerabf295f2004-07-24 02:52:23 +0000287 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000288 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000289 I->start = End;
290 return;
291 }
292
293 // Otherwise if the span we are removing is at the end of the LiveRange,
294 // adjust the other way.
295 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000296 removeKills(ValNo, Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000297 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000298 return;
299 }
300
301 // Otherwise, we are splitting the LiveRange into two pieces.
302 unsigned OldEnd = I->end;
303 I->end = Start; // Trim the old interval.
304
305 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000306 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000307}
308
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000309/// removeValNo - Remove all the ranges defined by the specified value#.
310/// Also remove the value# from value# list.
311void LiveInterval::removeValNo(VNInfo *ValNo) {
312 if (empty()) return;
313 Ranges::iterator I = ranges.end();
314 Ranges::iterator E = ranges.begin();
315 do {
316 --I;
317 if (I->valno == ValNo)
318 ranges.erase(I);
319 } while (I != E);
320 // Now that ValNo is dead, remove it. If it is the largest value
321 // number, just nuke it (and any other deleted values neighboring it),
322 // otherwise mark it as ~1U so it can be nuked later.
323 if (ValNo->id == getNumValNums()-1) {
324 do {
325 VNInfo *VNI = valnos.back();
326 valnos.pop_back();
327 VNI->~VNInfo();
328 } while (!valnos.empty() && valnos.back()->def == ~1U);
329 } else {
330 ValNo->def = ~1U;
331 }
332}
333
Chris Lattnerabf295f2004-07-24 02:52:23 +0000334/// getLiveRangeContaining - Return the live range that contains the
335/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000336LiveInterval::const_iterator
337LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
338 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000339 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000340 --It;
341 if (It->contains(Idx))
342 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000343 }
344
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000345 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000346}
347
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000348LiveInterval::iterator
349LiveInterval::FindLiveRangeContaining(unsigned Idx) {
350 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000351 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000352 --It;
353 if (It->contains(Idx))
354 return It;
355 }
356
357 return end();
358}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000359
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000360/// join - Join two live intervals (this, and other) together. This applies
361/// mappings to the value numbers in the LHS/RHS intervals as specified. If
362/// the intervals are not joinable, this aborts.
David Greeneaf992f72007-09-06 19:46:46 +0000363void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
364 const int *RHSValNoAssignments,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000365 SmallVector<VNInfo*, 16> &NewVNInfo) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000366 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000367 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000368 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000369 unsigned NumVals = getNumValNums();
370 unsigned NumNewVals = NewVNInfo.size();
371 for (unsigned i = 0; i != NumVals; ++i) {
372 unsigned LHSValID = LHSValNoAssignments[i];
373 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000374 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000375 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000376 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000377
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000378 // If we have to apply a mapping to our base interval assignment, rewrite it
379 // now.
380 if (MustMapCurValNos) {
381 // Map the first live range.
382 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000383 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000384 ++OutIt;
385 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000386 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000387
388 // If this live range has the same value # as its immediate predecessor,
389 // and if they are neighbors, remove one LiveRange. This happens when we
390 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000391 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000392 (OutIt-1)->end = OutIt->end;
393 } else {
394 if (I != OutIt) {
395 OutIt->start = I->start;
396 OutIt->end = I->end;
397 }
398
399 // Didn't merge, on to the next one.
400 ++OutIt;
401 }
402 }
403
404 // If we merge some live ranges, chop off the end.
405 ranges.erase(OutIt, end());
406 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000407
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000408 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000409 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000410 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
411 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
412
413 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000414 // LiveInterval now. Also remove dead val#'s.
415 unsigned NumValNos = 0;
416 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000417 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000418 if (VNI) {
419 if (i >= NumVals)
420 valnos.push_back(VNI);
421 else
422 valnos[NumValNos] = VNI;
423 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000424 }
425 }
Evan Cheng34301352007-09-01 02:03:17 +0000426 if (NumNewVals < NumVals)
427 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000428
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000429 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000430 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000431 unsigned RangeNo = 0;
432 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
433 // Map the valno in the other live range to the current live range.
434 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000435 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000436 InsertPos = addRangeFrom(*I, InsertPos);
437 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000438
Chris Lattnerabf295f2004-07-24 02:52:23 +0000439 weight += Other.weight;
Evan Chenge52eef82007-04-17 20:25:11 +0000440 if (Other.preference && !preference)
441 preference = Other.preference;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000442}
443
Chris Lattnerf21f0202006-09-02 05:26:59 +0000444/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
445/// interval as the specified value number. The LiveRanges in RHS are
446/// allowed to overlap with LiveRanges in the current interval, but only if
447/// the overlapping LiveRanges have the specified value number.
448void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000449 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000450 // TODO: Make this more efficient.
451 iterator InsertPos = begin();
452 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000453 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000454 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000455 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000456 InsertPos = addRangeFrom(Tmp, InsertPos);
457 }
458}
459
460
Evan Cheng32dfbea2007-10-12 08:50:34 +0000461/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
462/// in RHS into this live interval as the specified value number.
463/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000464/// current interval, it will replace the value numbers of the overlaped
465/// live ranges with the specified value number.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000466void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng34729252007-10-14 10:08:34 +0000467 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000468 SmallVector<VNInfo*, 4> ReplacedValNos;
469 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000470 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
471 if (I->valno != RHSValNo)
472 continue;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000473 unsigned Start = I->start, End = I->end;
474 IP = std::upper_bound(IP, end(), Start);
475 // If the start of this range overlaps with an existing liverange, trim it.
476 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000477 if (IP[-1].valno != LHSValNo) {
478 ReplacedValNos.push_back(IP[-1].valno);
479 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000480 }
481 Start = IP[-1].end;
482 // Trimmed away the whole range?
483 if (Start >= End) continue;
484 }
485 // If the end of this range overlaps with an existing liverange, trim it.
486 if (IP != end() && End > IP->start) {
487 if (IP->valno != LHSValNo) {
488 ReplacedValNos.push_back(IP->valno);
489 IP->valno = LHSValNo; // Update val#.
490 }
491 End = IP->start;
492 // If this trimmed away the whole range, ignore it.
493 if (Start == End) continue;
494 }
495
Evan Cheng32dfbea2007-10-12 08:50:34 +0000496 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000497 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
498 }
499
500
501 SmallSet<VNInfo*, 4> Seen;
502 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
503 VNInfo *V1 = ReplacedValNos[i];
504 if (Seen.insert(V1)) {
505 bool isDead = true;
506 for (const_iterator I = begin(), E = end(); I != E; ++I)
507 if (I->valno == V1) {
508 isDead = false;
509 break;
510 }
511 if (isDead) {
512 // Now that V1 is dead, remove it. If it is the largest value number,
513 // just nuke it (and any other deleted values neighboring it), otherwise
514 // mark it as ~1U so it can be nuked later.
515 if (V1->id == getNumValNums()-1) {
516 do {
517 VNInfo *VNI = valnos.back();
518 valnos.pop_back();
519 VNI->~VNInfo();
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000520 } while (!valnos.empty() && valnos.back()->def == ~1U);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000521 } else {
522 V1->def = ~1U;
523 }
524 }
525 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000526 }
527}
528
529
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000530/// MergeInClobberRanges - For any live ranges that are not defined in the
531/// current interval, but are defined in the Clobbers interval, mark them
532/// used with an unknown definition value.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000533void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
534 BumpPtrAllocator &VNInfoAllocator) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000535 if (Clobbers.begin() == Clobbers.end()) return;
536
537 // Find a value # to use for the clobber ranges. If there is already a value#
538 // for unknown values, use it.
539 // FIXME: Use a single sentinal number for these!
Evan Chengf3bb2e62007-09-05 21:46:51 +0000540 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000541
542 iterator IP = begin();
543 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
544 unsigned Start = I->start, End = I->end;
545 IP = std::upper_bound(IP, end(), Start);
546
547 // If the start of this range overlaps with an existing liverange, trim it.
548 if (IP != begin() && IP[-1].end > Start) {
549 Start = IP[-1].end;
550 // Trimmed away the whole range?
551 if (Start >= End) continue;
552 }
553 // If the end of this range overlaps with an existing liverange, trim it.
554 if (IP != end() && End > IP->start) {
555 End = IP->start;
556 // If this trimmed away the whole range, ignore it.
557 if (Start == End) continue;
558 }
559
560 // Insert the clobber interval.
561 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
562 }
563}
564
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000565/// MergeValueNumberInto - This method is called when two value nubmers
566/// are found to be equivalent. This eliminates V1, replacing all
567/// LiveRanges with the V1 value number with the V2 value number. This can
568/// cause merging of V1/V2 values numbers and compaction of the value space.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000569void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000570 assert(V1 != V2 && "Identical value#'s are always equivalent!");
571
572 // This code actually merges the (numerically) larger value number into the
573 // smaller value number, which is likely to allow us to compactify the value
574 // space. The only thing we have to be careful of is to preserve the
575 // instruction that defines the result value.
576
577 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000578 if (V1->id < V2->id) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000579 copyValNumInfo(V1, V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000580 std::swap(V1, V2);
581 }
582
583 // Merge V1 live ranges into V2.
584 for (iterator I = begin(); I != end(); ) {
585 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000586 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000587
588 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
589 // range, extend it.
590 if (LR != begin()) {
591 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000592 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000593 Prev->end = LR->end;
594
595 // Erase this live-range.
596 ranges.erase(LR);
597 I = Prev+1;
598 LR = Prev;
599 }
600 }
601
602 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
603 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000604 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000605
606 // If we can merge it into later V2 live ranges, do so now. We ignore any
607 // following V1 live ranges, as they will be merged in subsequent iterations
608 // of the loop.
609 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000610 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000611 LR->end = I->end;
612 ranges.erase(I);
613 I = LR+1;
614 }
615 }
616 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000617
618 // Now that V1 is dead, remove it. If it is the largest value number, just
619 // nuke it (and any other deleted values neighboring it), otherwise mark it as
620 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000621 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000622 do {
Evan Chengdd199d22007-09-06 01:07:24 +0000623 VNInfo *VNI = valnos.back();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000624 valnos.pop_back();
Evan Chengdd199d22007-09-06 01:07:24 +0000625 VNI->~VNInfo();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000626 } while (valnos.back()->def == ~1U);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000627 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000628 V1->def = ~1U;
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000629 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000630}
631
Evan Cheng32dfbea2007-10-12 08:50:34 +0000632void LiveInterval::Copy(const LiveInterval &RHS,
633 BumpPtrAllocator &VNInfoAllocator) {
634 ranges.clear();
635 valnos.clear();
636 preference = RHS.preference;
637 weight = RHS.weight;
638 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
639 const VNInfo *VNI = RHS.getValNumInfo(i);
640 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
641 copyValNumInfo(NewVNI, VNI);
642 }
643 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
644 const LiveRange &LR = RHS.ranges[i];
645 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
646 }
647}
648
Evan Chenge52eef82007-04-17 20:25:11 +0000649unsigned LiveInterval::getSize() const {
650 unsigned Sum = 0;
651 for (const_iterator I = begin(), E = end(); I != E; ++I)
652 Sum += I->end - I->start;
653 return Sum;
654}
655
Chris Lattnerfb449b92004-07-23 17:49:16 +0000656std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000657 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000658}
659
Chris Lattnerabf295f2004-07-24 02:52:23 +0000660void LiveRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000661 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000662}
663
Dan Gohman6f0d0242008-02-10 18:45:23 +0000664void LiveInterval::print(std::ostream &OS,
665 const TargetRegisterInfo *TRI) const {
666 if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
667 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000668 else
669 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000670
Chris Lattner38135af2005-05-14 05:34:15 +0000671 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000672
Chris Lattner38135af2005-05-14 05:34:15 +0000673 if (empty())
674 OS << "EMPTY";
675 else {
676 OS << " = ";
677 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
678 E = ranges.end(); I != E; ++I)
679 OS << *I;
680 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000681
682 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000683 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000684 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000685 unsigned vnum = 0;
686 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
687 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000688 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000689 if (vnum) OS << " ";
690 OS << vnum << "@";
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000691 if (vni->def == ~1U) {
Evan Cheng8df78602007-08-08 03:00:28 +0000692 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000693 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000694 if (vni->def == ~0U)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000695 OS << "?";
696 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000697 OS << vni->def;
698 unsigned ee = vni->kills.size();
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000699 if (ee || vni->hasPHIKill) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000700 OS << "-(";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000701 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000702 OS << vni->kills[j];
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000703 if (j != ee-1)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000704 OS << " ";
705 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000706 if (vni->hasPHIKill) {
707 if (ee)
708 OS << " ";
709 OS << "phi";
710 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000711 OS << ")";
712 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000713 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000714 }
715 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000716}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000717
718void LiveInterval::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000719 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000720}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000721
722
Jeff Cohen4b607742006-12-16 02:15:42 +0000723void LiveRange::print(std::ostream &os) const {
724 os << *this;
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000725}