blob: 9dee892c75edf2fef17db6f9e2844f91598d73f0 [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 Cheng0adb5272009-04-25 09:25:19 +000022#include "llvm/ADT/DenseMap.h"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000023#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000024#include "llvm/ADT/STLExtras.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000025#include "llvm/Support/Streams.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000026#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000027#include <algorithm>
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000028#include <ostream>
Chris Lattnerfb449b92004-07-23 17:49:16 +000029using namespace llvm;
30
31// An example for liveAt():
32//
Chris Lattneraa141472004-07-23 18:40:00 +000033// this = [1,4), liveAt(0) will return false. The instruction defining this
34// spans slots [0,3]. The interval belongs to an spilled definition of the
35// variable it represents. This is because slot 1 is used (def slot) and spans
36// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000037//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000038bool LiveInterval::liveAt(unsigned I) const {
39 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000040
Chris Lattnerfb449b92004-07-23 17:49:16 +000041 if (r == ranges.begin())
42 return false;
43
44 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000045 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000046}
47
Evan Chengc8d044e2008-02-15 18:24:29 +000048// liveBeforeAndAt - Check if the interval is live at the index and the index
49// just before it. If index is liveAt, check if it starts a new live range.
50// If it does, then check if the previous live range ends at index-1.
51bool LiveInterval::liveBeforeAndAt(unsigned I) const {
52 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
53
54 if (r == ranges.begin())
55 return false;
56
57 --r;
58 if (!r->contains(I))
59 return false;
60 if (I != r->start)
61 return true;
62 // I is the start of a live range. Check if the previous live range ends
63 // at I-1.
64 if (r == ranges.begin())
65 return false;
66 return r->end == I;
67}
68
Chris Lattnerbae74d92004-11-18 03:47:34 +000069// overlaps - Return true if the intersection of the two live intervals is
70// not empty.
71//
Chris Lattnerfb449b92004-07-23 17:49:16 +000072// An example for overlaps():
73//
74// 0: A = ...
75// 4: B = ...
76// 8: C = A + B ;; last use of A
77//
78// The live intervals should look like:
79//
80// A = [3, 11)
81// B = [7, x)
82// C = [11, y)
83//
84// A->overlaps(C) should return false since we want to be able to join
85// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000086//
87bool LiveInterval::overlapsFrom(const LiveInterval& other,
88 const_iterator StartPos) const {
89 const_iterator i = begin();
90 const_iterator ie = end();
91 const_iterator j = StartPos;
92 const_iterator je = other.end();
93
94 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000095 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000096
Chris Lattnerfb449b92004-07-23 17:49:16 +000097 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000098 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000099 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000100 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000101 ++StartPos;
102 if (StartPos != other.end() && StartPos->start <= i->start) {
103 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000104 j = std::upper_bound(j, je, i->start);
105 if (j != other.ranges.begin()) --j;
106 }
Chris Lattneraa141472004-07-23 18:40:00 +0000107 } else {
108 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000109 }
110
Chris Lattner9fddc122004-11-18 05:28:21 +0000111 if (j == je) return false;
112
113 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000114 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000115 std::swap(i, j);
116 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000117 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000118
119 if (i->end > j->start)
120 return true;
121 ++i;
122 }
123
124 return false;
125}
126
Evan Chengcccdb2b2009-04-18 08:52:15 +0000127/// overlaps - Return true if the live interval overlaps a range specified
128/// by [Start, End).
129bool LiveInterval::overlaps(unsigned Start, unsigned End) const {
130 assert(Start < End && "Invalid range");
131 const_iterator I = begin();
132 const_iterator E = end();
133 const_iterator si = std::upper_bound(I, E, Start);
134 const_iterator ei = std::upper_bound(I, E, End);
135 if (si != ei)
136 return true;
137 if (si == I)
138 return false;
139 --si;
140 return si->contains(Start);
141}
142
Chris Lattnerb26c2152004-07-23 19:38:44 +0000143/// extendIntervalEndTo - This method is used when we want to extend the range
144/// specified by I to end at the specified endpoint. To do this, we should
145/// merge and eliminate all ranges that this will overlap with. The iterator is
146/// not invalidated.
147void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
148 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000149 VNInfo *ValNo = I->valno;
Evan Cheng430a7b02007-08-14 01:56:58 +0000150 unsigned OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000151
Chris Lattnerb26c2152004-07-23 19:38:44 +0000152 // Search for the first interval that we can't merge with.
153 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000154 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000155 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000156 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000157
158 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
159 I->end = std::max(NewEnd, prior(MergeTo)->end);
160
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000161 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000162 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000163
164 // Update kill info.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000165 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000166
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000167 // If the newly formed range now touches the range after it and if they have
168 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000169 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000170 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000171 I->end = Next->end;
172 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000173 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000174}
175
176
177/// extendIntervalStartTo - This method is used when we want to extend the range
178/// specified by I to start at the specified endpoint. To do this, we should
179/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000180LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000181LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
182 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000183 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000184
185 // Search for the first interval that we can't merge with.
186 Ranges::iterator MergeTo = I;
187 do {
188 if (MergeTo == ranges.begin()) {
189 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000190 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000191 return I;
192 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000193 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000194 --MergeTo;
195 } while (NewStart <= MergeTo->start);
196
197 // If we start in the middle of another interval, just delete a range and
198 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000199 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000200 MergeTo->end = I->end;
201 } else {
202 // Otherwise, extend the interval right after.
203 ++MergeTo;
204 MergeTo->start = NewStart;
205 MergeTo->end = I->end;
206 }
207
208 ranges.erase(next(MergeTo), next(I));
209 return MergeTo;
210}
211
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000212LiveInterval::iterator
213LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000214 unsigned Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000215 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000216
217 // If the inserted interval starts in the middle or right at the end of
218 // another interval, just extend that interval to contain the range of LR.
219 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000220 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000222 if (B->start <= Start && B->end >= Start) {
223 extendIntervalEndTo(B, End);
224 return B;
225 }
226 } else {
227 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000228 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000229 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000230 "Cannot overlap two LiveRanges with differing ValID's"
231 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000232 }
233 }
234
235 // Otherwise, if this range ends in the middle of, or right next to, another
236 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000237 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000238 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000239 if (it->start <= End) {
240 it = extendIntervalStartTo(it, Start);
241
242 // If LR is a complete superset of an interval, we may need to grow its
243 // endpoint as well.
244 if (End > it->end)
245 extendIntervalEndTo(it, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000246 else if (End < it->end)
Evan Chengc3868e02007-11-29 01:05:47 +0000247 // Overlapping intervals, there might have been a kill here.
248 removeKill(it->valno, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000249 return it;
250 }
251 } else {
252 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000253 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000254 assert(it->start >= End &&
255 "Cannot overlap two LiveRanges with differing ValID's");
256 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000257 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000258
259 // Otherwise, this is just a new range that doesn't interact with anything.
260 // Insert it.
261 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000262}
263
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000264/// isInOneLiveRange - Return true if the range specified is entirely in the
265/// a single LiveRange of the live interval.
266bool LiveInterval::isInOneLiveRange(unsigned Start, unsigned End) {
267 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
268 if (I == ranges.begin())
269 return false;
270 --I;
271 return I->contains(Start) && I->contains(End-1);
272}
273
Chris Lattnerabf295f2004-07-24 02:52:23 +0000274
275/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000276/// the range must be in a single LiveRange in its entirety.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000277void LiveInterval::removeRange(unsigned Start, unsigned End,
278 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000279 // Find the LiveRange containing this span.
280 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
281 assert(I != ranges.begin() && "Range is not in interval!");
282 --I;
283 assert(I->contains(Start) && I->contains(End-1) &&
284 "Range is not entirely in interval!");
285
286 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000287 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000288 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000289 if (I->end == End) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000290 removeKills(I->valno, Start, End);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000291 if (RemoveDeadValNo) {
292 // Check if val# is dead.
293 bool isDead = true;
294 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
295 if (II != I && II->valno == ValNo) {
296 isDead = false;
297 break;
298 }
299 if (isDead) {
300 // Now that ValNo is dead, remove it. If it is the largest value
301 // number, just nuke it (and any other deleted values neighboring it),
302 // otherwise mark it as ~1U so it can be nuked later.
303 if (ValNo->id == getNumValNums()-1) {
304 do {
305 VNInfo *VNI = valnos.back();
306 valnos.pop_back();
307 VNI->~VNInfo();
308 } while (!valnos.empty() && valnos.back()->def == ~1U);
309 } else {
310 ValNo->def = ~1U;
311 }
312 }
313 }
314
Chris Lattnerabf295f2004-07-24 02:52:23 +0000315 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000316 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000317 I->start = End;
318 return;
319 }
320
321 // Otherwise if the span we are removing is at the end of the LiveRange,
322 // adjust the other way.
323 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000324 removeKills(ValNo, Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000325 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000326 return;
327 }
328
329 // Otherwise, we are splitting the LiveRange into two pieces.
330 unsigned OldEnd = I->end;
331 I->end = Start; // Trim the old interval.
332
333 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000334 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000335}
336
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000337/// removeValNo - Remove all the ranges defined by the specified value#.
338/// Also remove the value# from value# list.
339void LiveInterval::removeValNo(VNInfo *ValNo) {
340 if (empty()) return;
341 Ranges::iterator I = ranges.end();
342 Ranges::iterator E = ranges.begin();
343 do {
344 --I;
345 if (I->valno == ValNo)
346 ranges.erase(I);
347 } while (I != E);
348 // Now that ValNo is dead, remove it. If it is the largest value
349 // number, just nuke it (and any other deleted values neighboring it),
350 // otherwise mark it as ~1U so it can be nuked later.
351 if (ValNo->id == getNumValNums()-1) {
352 do {
353 VNInfo *VNI = valnos.back();
354 valnos.pop_back();
355 VNI->~VNInfo();
356 } while (!valnos.empty() && valnos.back()->def == ~1U);
357 } else {
358 ValNo->def = ~1U;
359 }
360}
361
Chris Lattnerabf295f2004-07-24 02:52:23 +0000362/// getLiveRangeContaining - Return the live range that contains the
363/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000364LiveInterval::const_iterator
365LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
366 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000367 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000368 --It;
369 if (It->contains(Idx))
370 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000371 }
372
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000373 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000374}
375
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000376LiveInterval::iterator
377LiveInterval::FindLiveRangeContaining(unsigned Idx) {
378 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000379 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000380 --It;
381 if (It->contains(Idx))
382 return It;
383 }
384
385 return end();
386}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000387
Evan Cheng3f32d652008-06-04 09:18:41 +0000388/// findDefinedVNInfo - Find the VNInfo that's defined at the specified index
389/// (register interval) or defined by the specified register (stack inteval).
390VNInfo *LiveInterval::findDefinedVNInfo(unsigned DefIdxOrReg) const {
391 VNInfo *VNI = NULL;
392 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
393 i != e; ++i)
394 if ((*i)->def == DefIdxOrReg) {
395 VNI = *i;
396 break;
397 }
398 return VNI;
399}
400
401
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000402/// join - Join two live intervals (this, and other) together. This applies
403/// mappings to the value numbers in the LHS/RHS intervals as specified. If
404/// the intervals are not joinable, this aborts.
David Greeneaf992f72007-09-06 19:46:46 +0000405void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
406 const int *RHSValNoAssignments,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000407 SmallVector<VNInfo*, 16> &NewVNInfo) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000408 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000409 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000410 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000411 unsigned NumVals = getNumValNums();
412 unsigned NumNewVals = NewVNInfo.size();
413 for (unsigned i = 0; i != NumVals; ++i) {
414 unsigned LHSValID = LHSValNoAssignments[i];
415 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000416 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000417 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000418 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000419
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000420 // If we have to apply a mapping to our base interval assignment, rewrite it
421 // now.
422 if (MustMapCurValNos) {
423 // Map the first live range.
424 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000425 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000426 ++OutIt;
427 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000428 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000429
430 // If this live range has the same value # as its immediate predecessor,
431 // and if they are neighbors, remove one LiveRange. This happens when we
432 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000433 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000434 (OutIt-1)->end = OutIt->end;
435 } else {
436 if (I != OutIt) {
437 OutIt->start = I->start;
438 OutIt->end = I->end;
439 }
440
441 // Didn't merge, on to the next one.
442 ++OutIt;
443 }
444 }
445
446 // If we merge some live ranges, chop off the end.
447 ranges.erase(OutIt, end());
448 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000449
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000450 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000451 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000452 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
453 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
454
455 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000456 // LiveInterval now. Also remove dead val#'s.
457 unsigned NumValNos = 0;
458 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000459 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000460 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000461 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000462 valnos.push_back(VNI);
463 else
464 valnos[NumValNos] = VNI;
465 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000466 }
467 }
Evan Cheng34301352007-09-01 02:03:17 +0000468 if (NumNewVals < NumVals)
469 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000470
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000471 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000472 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000473 unsigned RangeNo = 0;
474 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
475 // Map the valno in the other live range to the current live range.
476 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000477 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000478 InsertPos = addRangeFrom(*I, InsertPos);
479 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000480
Chris Lattnerabf295f2004-07-24 02:52:23 +0000481 weight += Other.weight;
Evan Chenge52eef82007-04-17 20:25:11 +0000482 if (Other.preference && !preference)
483 preference = Other.preference;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000484}
485
Chris Lattnerf21f0202006-09-02 05:26:59 +0000486/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
487/// interval as the specified value number. The LiveRanges in RHS are
488/// allowed to overlap with LiveRanges in the current interval, but only if
489/// the overlapping LiveRanges have the specified value number.
490void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000491 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000492 // TODO: Make this more efficient.
493 iterator InsertPos = begin();
494 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000495 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000496 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000497 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000498 InsertPos = addRangeFrom(Tmp, InsertPos);
499 }
500}
501
502
Evan Cheng32dfbea2007-10-12 08:50:34 +0000503/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
504/// in RHS into this live interval as the specified value number.
505/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000506/// current interval, it will replace the value numbers of the overlaped
507/// live ranges with the specified value number.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000508void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng34729252007-10-14 10:08:34 +0000509 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000510 SmallVector<VNInfo*, 4> ReplacedValNos;
511 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000512 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
513 if (I->valno != RHSValNo)
514 continue;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000515 unsigned Start = I->start, End = I->end;
516 IP = std::upper_bound(IP, end(), Start);
517 // If the start of this range overlaps with an existing liverange, trim it.
518 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000519 if (IP[-1].valno != LHSValNo) {
520 ReplacedValNos.push_back(IP[-1].valno);
521 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000522 }
523 Start = IP[-1].end;
524 // Trimmed away the whole range?
525 if (Start >= End) continue;
526 }
527 // If the end of this range overlaps with an existing liverange, trim it.
528 if (IP != end() && End > IP->start) {
529 if (IP->valno != LHSValNo) {
530 ReplacedValNos.push_back(IP->valno);
531 IP->valno = LHSValNo; // Update val#.
532 }
533 End = IP->start;
534 // If this trimmed away the whole range, ignore it.
535 if (Start == End) continue;
536 }
537
Evan Cheng32dfbea2007-10-12 08:50:34 +0000538 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000539 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
540 }
541
542
543 SmallSet<VNInfo*, 4> Seen;
544 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
545 VNInfo *V1 = ReplacedValNos[i];
546 if (Seen.insert(V1)) {
547 bool isDead = true;
548 for (const_iterator I = begin(), E = end(); I != E; ++I)
549 if (I->valno == V1) {
550 isDead = false;
551 break;
552 }
553 if (isDead) {
554 // Now that V1 is dead, remove it. If it is the largest value number,
555 // just nuke it (and any other deleted values neighboring it), otherwise
556 // mark it as ~1U so it can be nuked later.
557 if (V1->id == getNumValNums()-1) {
558 do {
559 VNInfo *VNI = valnos.back();
560 valnos.pop_back();
561 VNI->~VNInfo();
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000562 } while (!valnos.empty() && valnos.back()->def == ~1U);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000563 } else {
564 V1->def = ~1U;
565 }
566 }
567 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000568 }
569}
570
571
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000572/// MergeInClobberRanges - For any live ranges that are not defined in the
573/// current interval, but are defined in the Clobbers interval, mark them
574/// used with an unknown definition value.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000575void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
576 BumpPtrAllocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000577 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000578
Evan Cheng0adb5272009-04-25 09:25:19 +0000579 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000580 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000581 iterator IP = begin();
582 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000583 // For every val# in the Clobbers interval, create a new "unknown" val#.
584 VNInfo *ClobberValNo = 0;
585 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
586 if (VI != ValNoMaps.end())
587 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000588 else if (UnusedValNo)
589 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000590 else {
Evan Chenga4b2bab2009-04-25 20:20:15 +0000591 UnusedValNo = ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000592 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
593 }
594
Dan Gohman97121ba2009-04-08 00:15:30 +0000595 bool Done = false;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000596 unsigned Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000597 // If a clobber range starts before an existing range and ends after
598 // it, the clobber range will need to be split into multiple ranges.
599 // Loop until the entire clobber range is handled.
600 while (!Done) {
601 Done = true;
602 IP = std::upper_bound(IP, end(), Start);
603 unsigned SubRangeStart = Start;
604 unsigned SubRangeEnd = End;
605
606 // If the start of this range overlaps with an existing liverange, trim it.
607 if (IP != begin() && IP[-1].end > SubRangeStart) {
608 SubRangeStart = IP[-1].end;
609 // Trimmed away the whole range?
610 if (SubRangeStart >= SubRangeEnd) continue;
611 }
612 // If the end of this range overlaps with an existing liverange, trim it.
613 if (IP != end() && SubRangeEnd > IP->start) {
614 // If the clobber live range extends beyond the existing live range,
615 // it'll need at least another live range, so set the flag to keep
616 // iterating.
617 if (SubRangeEnd > IP->end) {
618 Start = IP->end;
619 Done = false;
620 }
621 SubRangeEnd = IP->start;
622 // If this trimmed away the whole range, ignore it.
623 if (SubRangeStart == SubRangeEnd) continue;
624 }
625
626 // Insert the clobber interval.
627 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
628 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000629 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000630 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000631 }
Evan Cheng27e46662009-04-27 17:35:19 +0000632
633 if (UnusedValNo) {
634 // Delete the last unused val#.
635 valnos.pop_back();
636 UnusedValNo->~VNInfo();
637 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000638}
639
Evan Chenga2e64352009-03-11 00:03:21 +0000640void LiveInterval::MergeInClobberRange(unsigned Start, unsigned End,
641 BumpPtrAllocator &VNInfoAllocator) {
642 // Find a value # to use for the clobber ranges. If there is already a value#
643 // for unknown values, use it.
Evan Cheng0adb5272009-04-25 09:25:19 +0000644 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000645
646 iterator IP = begin();
647 IP = std::upper_bound(IP, end(), Start);
648
649 // If the start of this range overlaps with an existing liverange, trim it.
650 if (IP != begin() && IP[-1].end > Start) {
651 Start = IP[-1].end;
652 // Trimmed away the whole range?
653 if (Start >= End) return;
654 }
655 // If the end of this range overlaps with an existing liverange, trim it.
656 if (IP != end() && End > IP->start) {
657 End = IP->start;
658 // If this trimmed away the whole range, ignore it.
659 if (Start == End) return;
660 }
661
662 // Insert the clobber interval.
663 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
664}
665
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000666/// MergeValueNumberInto - This method is called when two value nubmers
667/// are found to be equivalent. This eliminates V1, replacing all
668/// LiveRanges with the V1 value number with the V2 value number. This can
669/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000670VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000671 assert(V1 != V2 && "Identical value#'s are always equivalent!");
672
673 // This code actually merges the (numerically) larger value number into the
674 // smaller value number, which is likely to allow us to compactify the value
675 // space. The only thing we have to be careful of is to preserve the
676 // instruction that defines the result value.
677
678 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000679 if (V1->id < V2->id) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000680 copyValNumInfo(V1, V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000681 std::swap(V1, V2);
682 }
683
684 // Merge V1 live ranges into V2.
685 for (iterator I = begin(); I != end(); ) {
686 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000687 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000688
689 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
690 // range, extend it.
691 if (LR != begin()) {
692 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000693 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000694 Prev->end = LR->end;
695
696 // Erase this live-range.
697 ranges.erase(LR);
698 I = Prev+1;
699 LR = Prev;
700 }
701 }
702
703 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
704 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000705 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000706
707 // If we can merge it into later V2 live ranges, do so now. We ignore any
708 // following V1 live ranges, as they will be merged in subsequent iterations
709 // of the loop.
710 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000711 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000712 LR->end = I->end;
713 ranges.erase(I);
714 I = LR+1;
715 }
716 }
717 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000718
719 // Now that V1 is dead, remove it. If it is the largest value number, just
720 // nuke it (and any other deleted values neighboring it), otherwise mark it as
721 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000722 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000723 do {
Evan Chengdd199d22007-09-06 01:07:24 +0000724 VNInfo *VNI = valnos.back();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000725 valnos.pop_back();
Evan Chengdd199d22007-09-06 01:07:24 +0000726 VNI->~VNInfo();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000727 } while (valnos.back()->def == ~1U);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000728 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000729 V1->def = ~1U;
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000730 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000731
732 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000733}
734
Evan Cheng32dfbea2007-10-12 08:50:34 +0000735void LiveInterval::Copy(const LiveInterval &RHS,
736 BumpPtrAllocator &VNInfoAllocator) {
737 ranges.clear();
738 valnos.clear();
739 preference = RHS.preference;
740 weight = RHS.weight;
741 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
742 const VNInfo *VNI = RHS.getValNumInfo(i);
743 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
744 copyValNumInfo(NewVNI, VNI);
745 }
746 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
747 const LiveRange &LR = RHS.ranges[i];
748 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
749 }
750}
751
Evan Chenge52eef82007-04-17 20:25:11 +0000752unsigned LiveInterval::getSize() const {
753 unsigned Sum = 0;
754 for (const_iterator I = begin(), E = end(); I != E; ++I)
755 Sum += I->end - I->start;
756 return Sum;
757}
758
Chris Lattnerfb449b92004-07-23 17:49:16 +0000759std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000760 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000761}
762
Chris Lattnerabf295f2004-07-24 02:52:23 +0000763void LiveRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000764 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000765}
766
Dan Gohman6f0d0242008-02-10 18:45:23 +0000767void LiveInterval::print(std::ostream &OS,
768 const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000769 if (isStackSlot())
770 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000771 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000772 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000773 else
774 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000775
Chris Lattner38135af2005-05-14 05:34:15 +0000776 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000777
Chris Lattner38135af2005-05-14 05:34:15 +0000778 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000779 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000780 else {
781 OS << " = ";
782 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
783 E = ranges.end(); I != E; ++I)
784 OS << *I;
785 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000786
787 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000788 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000789 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000790 unsigned vnum = 0;
791 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
792 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000793 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000794 if (vnum) OS << " ";
795 OS << vnum << "@";
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000796 if (vni->def == ~1U) {
Evan Cheng8df78602007-08-08 03:00:28 +0000797 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000798 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000799 if (vni->def == ~0U)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000800 OS << "?";
801 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000802 OS << vni->def;
803 unsigned ee = vni->kills.size();
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000804 if (ee || vni->hasPHIKill) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000805 OS << "-(";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000806 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000807 OS << vni->kills[j];
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000808 if (j != ee-1)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000809 OS << " ";
810 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000811 if (vni->hasPHIKill) {
812 if (ee)
813 OS << " ";
814 OS << "phi";
815 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000816 OS << ")";
817 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000818 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000819 }
820 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000821}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000822
823void LiveInterval::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000824 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000825}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000826
827
Jeff Cohen4b607742006-12-16 02:15:42 +0000828void LiveRange::print(std::ostream &os) const {
829 os << *this;
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000830}