blob: 34b58515b68ccd6c082e9582a6cf754c1f1c5a97 [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
Bob Wilson86af6552010-01-12 22:18:56 +000013// such that v is live at j' and there is no instruction with number i' < i such
Chris Lattnerfb449b92004-07-23 17:49:16 +000014// 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"
Lang Hames233a60e2009-11-03 23:52:08 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Cheng90f95f82009-06-14 20:22:55 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng0adb5272009-04-25 09:25:19 +000024#include "llvm/ADT/DenseMap.h"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000025#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000026#include "llvm/ADT/STLExtras.h"
David Greene52421542010-01-04 22:41:43 +000027#include "llvm/Support/Debug.h"
Daniel Dunbara717b7b2009-07-24 10:47:20 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000029#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000030#include <algorithm>
Chris Lattnerfb449b92004-07-23 17:49:16 +000031using namespace llvm;
32
33// An example for liveAt():
34//
Chris Lattneraa141472004-07-23 18:40:00 +000035// this = [1,4), liveAt(0) will return false. The instruction defining this
36// spans slots [0,3]. The interval belongs to an spilled definition of the
37// variable it represents. This is because slot 1 is used (def slot) and spans
38// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000039//
Lang Hames233a60e2009-11-03 23:52:08 +000040bool LiveInterval::liveAt(SlotIndex I) const {
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000041 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000042
Chris Lattnerfb449b92004-07-23 17:49:16 +000043 if (r == ranges.begin())
44 return false;
45
46 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000047 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000048}
49
Evan Chengc8d044e2008-02-15 18:24:29 +000050// liveBeforeAndAt - Check if the interval is live at the index and the index
51// just before it. If index is liveAt, check if it starts a new live range.
52// If it does, then check if the previous live range ends at index-1.
Lang Hames233a60e2009-11-03 23:52:08 +000053bool LiveInterval::liveBeforeAndAt(SlotIndex I) const {
Evan Chengc8d044e2008-02-15 18:24:29 +000054 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
55
56 if (r == ranges.begin())
57 return false;
58
59 --r;
60 if (!r->contains(I))
61 return false;
62 if (I != r->start)
63 return true;
64 // I is the start of a live range. Check if the previous live range ends
65 // at I-1.
66 if (r == ranges.begin())
67 return false;
68 return r->end == I;
69}
70
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000071/// killedAt - Return true if a live range ends at index. Note that the kill
72/// point is not contained in the half-open live range. It is usually the
73/// getDefIndex() slot following its last use.
74bool LiveInterval::killedAt(SlotIndex I) const {
75 Ranges::const_iterator r = std::lower_bound(ranges.begin(), ranges.end(), I);
76
77 // Now r points to the first interval with start >= I, or ranges.end().
78 if (r == ranges.begin())
79 return false;
80
81 --r;
82 // Now r points to the last interval with end <= I.
83 // r->end is the kill point.
84 return r->end == I;
85}
86
87/// killedInRange - Return true if the interval has kills in [Start,End).
88bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
89 Ranges::const_iterator r =
90 std::lower_bound(ranges.begin(), ranges.end(), End);
91
92 // Now r points to the first interval with start >= End, or ranges.end().
93 if (r == ranges.begin())
94 return false;
95
96 --r;
97 // Now r points to the last interval with end <= End.
98 // r->end is the kill point.
99 return r->end >= Start && r->end < End;
100}
101
Chris Lattnerbae74d92004-11-18 03:47:34 +0000102// overlaps - Return true if the intersection of the two live intervals is
103// not empty.
104//
Chris Lattnerfb449b92004-07-23 17:49:16 +0000105// An example for overlaps():
106//
107// 0: A = ...
108// 4: B = ...
109// 8: C = A + B ;; last use of A
110//
111// The live intervals should look like:
112//
113// A = [3, 11)
114// B = [7, x)
115// C = [11, y)
116//
117// A->overlaps(C) should return false since we want to be able to join
118// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +0000119//
120bool LiveInterval::overlapsFrom(const LiveInterval& other,
121 const_iterator StartPos) const {
122 const_iterator i = begin();
123 const_iterator ie = end();
124 const_iterator j = StartPos;
125 const_iterator je = other.end();
126
127 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000128 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000129
Chris Lattnerfb449b92004-07-23 17:49:16 +0000130 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000131 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000132 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000133 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000134 ++StartPos;
135 if (StartPos != other.end() && StartPos->start <= i->start) {
136 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000137 j = std::upper_bound(j, je, i->start);
138 if (j != other.ranges.begin()) --j;
139 }
Chris Lattneraa141472004-07-23 18:40:00 +0000140 } else {
141 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000142 }
143
Chris Lattner9fddc122004-11-18 05:28:21 +0000144 if (j == je) return false;
145
146 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000147 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000148 std::swap(i, j);
149 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000150 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000151
152 if (i->end > j->start)
153 return true;
154 ++i;
155 }
156
157 return false;
158}
159
Evan Chengcccdb2b2009-04-18 08:52:15 +0000160/// overlaps - Return true if the live interval overlaps a range specified
161/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000162bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000163 assert(Start < End && "Invalid range");
164 const_iterator I = begin();
165 const_iterator E = end();
166 const_iterator si = std::upper_bound(I, E, Start);
167 const_iterator ei = std::upper_bound(I, E, End);
168 if (si != ei)
169 return true;
170 if (si == I)
171 return false;
172 --si;
173 return si->contains(Start);
174}
175
Chris Lattnerb26c2152004-07-23 19:38:44 +0000176/// extendIntervalEndTo - This method is used when we want to extend the range
177/// specified by I to end at the specified endpoint. To do this, we should
178/// merge and eliminate all ranges that this will overlap with. The iterator is
179/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000180void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000181 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000182 VNInfo *ValNo = I->valno;
Lang Hames233a60e2009-11-03 23:52:08 +0000183 SlotIndex OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000184
Chris Lattnerb26c2152004-07-23 19:38:44 +0000185 // Search for the first interval that we can't merge with.
186 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000187 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000188 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000189 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000190
191 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
192 I->end = std::max(NewEnd, prior(MergeTo)->end);
193
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000194 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000195 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000196
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000197 // If the newly formed range now touches the range after it and if they have
198 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000199 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000200 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000201 I->end = Next->end;
202 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000203 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000204}
205
206
207/// extendIntervalStartTo - This method is used when we want to extend the range
208/// specified by I to start at the specified endpoint. To do this, we should
209/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000210LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000211LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000212 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000213 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000214
215 // Search for the first interval that we can't merge with.
216 Ranges::iterator MergeTo = I;
217 do {
218 if (MergeTo == ranges.begin()) {
219 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000220 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000221 return I;
222 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000223 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000224 --MergeTo;
225 } while (NewStart <= MergeTo->start);
226
227 // If we start in the middle of another interval, just delete a range and
228 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000229 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000230 MergeTo->end = I->end;
231 } else {
232 // Otherwise, extend the interval right after.
233 ++MergeTo;
234 MergeTo->start = NewStart;
235 MergeTo->end = I->end;
236 }
237
238 ranges.erase(next(MergeTo), next(I));
239 return MergeTo;
240}
241
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000242LiveInterval::iterator
243LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000244 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000245 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000246
247 // If the inserted interval starts in the middle or right at the end of
248 // another interval, just extend that interval to contain the range of LR.
249 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000250 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000251 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000252 if (B->start <= Start && B->end >= Start) {
253 extendIntervalEndTo(B, End);
254 return B;
255 }
256 } else {
257 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000258 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000259 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000260 "Cannot overlap two LiveRanges with differing ValID's"
261 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000262 }
263 }
264
265 // Otherwise, if this range ends in the middle of, or right next to, another
266 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000267 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000268 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000269 if (it->start <= End) {
270 it = extendIntervalStartTo(it, Start);
271
272 // If LR is a complete superset of an interval, we may need to grow its
273 // endpoint as well.
274 if (End > it->end)
275 extendIntervalEndTo(it, End);
276 return it;
277 }
278 } else {
279 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000280 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000281 assert(it->start >= End &&
282 "Cannot overlap two LiveRanges with differing ValID's");
283 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000284 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000285
286 // Otherwise, this is just a new range that doesn't interact with anything.
287 // Insert it.
288 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000289}
290
Lang Hames86511252009-09-04 20:41:11 +0000291/// isInOneLiveRange - Return true if the range specified is entirely in
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000292/// a single LiveRange of the live interval.
Lang Hames233a60e2009-11-03 23:52:08 +0000293bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000294 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
295 if (I == ranges.begin())
296 return false;
297 --I;
Lang Hames86511252009-09-04 20:41:11 +0000298 return I->containsRange(Start, End);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000299}
300
Chris Lattnerabf295f2004-07-24 02:52:23 +0000301
302/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000303/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000304void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000305 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000306 // Find the LiveRange containing this span.
307 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
308 assert(I != ranges.begin() && "Range is not in interval!");
309 --I;
Lang Hames86511252009-09-04 20:41:11 +0000310 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000311
312 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000313 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000314 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000315 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000316 if (RemoveDeadValNo) {
317 // Check if val# is dead.
318 bool isDead = true;
319 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
320 if (II != I && II->valno == ValNo) {
321 isDead = false;
322 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000323 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000324 if (isDead) {
325 // Now that ValNo is dead, remove it. If it is the largest value
326 // number, just nuke it (and any other deleted values neighboring it),
327 // otherwise mark it as ~1U so it can be nuked later.
328 if (ValNo->id == getNumValNums()-1) {
329 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000330 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000331 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000332 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000333 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000334 }
335 }
336 }
337
Chris Lattnerabf295f2004-07-24 02:52:23 +0000338 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000339 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340 I->start = End;
341 return;
342 }
343
344 // Otherwise if the span we are removing is at the end of the LiveRange,
345 // adjust the other way.
346 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000347 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000348 return;
349 }
350
351 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000352 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000353 I->end = Start; // Trim the old interval.
354
355 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000356 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000357}
358
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000359/// removeValNo - Remove all the ranges defined by the specified value#.
360/// Also remove the value# from value# list.
361void LiveInterval::removeValNo(VNInfo *ValNo) {
362 if (empty()) return;
363 Ranges::iterator I = ranges.end();
364 Ranges::iterator E = ranges.begin();
365 do {
366 --I;
367 if (I->valno == ValNo)
368 ranges.erase(I);
369 } while (I != E);
370 // Now that ValNo is dead, remove it. If it is the largest value
371 // number, just nuke it (and any other deleted values neighboring it),
372 // otherwise mark it as ~1U so it can be nuked later.
373 if (ValNo->id == getNumValNums()-1) {
374 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000375 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000376 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000377 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000378 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000379 }
380}
Lang Hames86511252009-09-04 20:41:11 +0000381
Chris Lattnerabf295f2004-07-24 02:52:23 +0000382/// getLiveRangeContaining - Return the live range that contains the
383/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000384LiveInterval::const_iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000385LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000386 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000387 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000388 --It;
389 if (It->contains(Idx))
390 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000391 }
392
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000393 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000394}
395
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000396LiveInterval::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000397LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000398 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000399 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000400 --It;
401 if (It->contains(Idx))
402 return It;
403 }
404
405 return end();
406}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000407
Lang Hames86511252009-09-04 20:41:11 +0000408/// findDefinedVNInfo - Find the VNInfo defined by the specified
409/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000410VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000411 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000412 i != e; ++i) {
413 if ((*i)->def == Idx)
414 return *i;
415 }
416
417 return 0;
418}
419
420/// findDefinedVNInfo - Find the VNInfo defined by the specified
421/// register (stack inteval).
422VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
423 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
424 i != e; ++i) {
425 if ((*i)->getReg() == reg)
426 return *i;
427 }
428 return 0;
Evan Cheng3f32d652008-06-04 09:18:41 +0000429}
430
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000431/// join - Join two live intervals (this, and other) together. This applies
432/// mappings to the value numbers in the LHS/RHS intervals as specified. If
433/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000434void LiveInterval::join(LiveInterval &Other,
435 const int *LHSValNoAssignments,
David Greeneaf992f72007-09-06 19:46:46 +0000436 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000437 SmallVector<VNInfo*, 16> &NewVNInfo,
438 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000439 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000440 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000441 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000442 unsigned NumVals = getNumValNums();
443 unsigned NumNewVals = NewVNInfo.size();
444 for (unsigned i = 0; i != NumVals; ++i) {
445 unsigned LHSValID = LHSValNoAssignments[i];
446 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000447 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000448 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000449 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000450
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000451 // If we have to apply a mapping to our base interval assignment, rewrite it
452 // now.
453 if (MustMapCurValNos) {
454 // Map the first live range.
455 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000456 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000457 ++OutIt;
458 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000459 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000460
461 // If this live range has the same value # as its immediate predecessor,
462 // and if they are neighbors, remove one LiveRange. This happens when we
463 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000464 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000465 (OutIt-1)->end = OutIt->end;
466 } else {
467 if (I != OutIt) {
468 OutIt->start = I->start;
469 OutIt->end = I->end;
470 }
471
472 // Didn't merge, on to the next one.
473 ++OutIt;
474 }
475 }
476
477 // If we merge some live ranges, chop off the end.
478 ranges.erase(OutIt, end());
479 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000480
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000482 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000483 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
484 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
485
486 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000487 // LiveInterval now. Also remove dead val#'s.
488 unsigned NumValNos = 0;
489 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000490 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000491 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000492 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000493 valnos.push_back(VNI);
494 else
495 valnos[NumValNos] = VNI;
496 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000497 }
498 }
Evan Cheng34301352007-09-01 02:03:17 +0000499 if (NumNewVals < NumVals)
500 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000501
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000502 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000503 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000504 unsigned RangeNo = 0;
505 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
506 // Map the valno in the other live range to the current live range.
507 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000508 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000509 InsertPos = addRangeFrom(*I, InsertPos);
510 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000511
David Greene29ff37f2009-07-22 20:08:25 +0000512 ComputeJoinedWeight(Other);
Evan Cheng90f95f82009-06-14 20:22:55 +0000513
514 // Update regalloc hint if currently there isn't one.
515 if (TargetRegisterInfo::isVirtualRegister(reg) &&
516 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
Evan Cheng358dec52009-06-15 08:28:29 +0000517 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
518 if (Hint.first == 0 && Hint.second == 0) {
519 std::pair<unsigned, unsigned> OtherHint =
Evan Cheng90f95f82009-06-14 20:22:55 +0000520 MRI->getRegAllocationHint(Other.reg);
Evan Cheng358dec52009-06-15 08:28:29 +0000521 if (OtherHint.first || OtherHint.second)
Evan Cheng90f95f82009-06-14 20:22:55 +0000522 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
523 }
524 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000525}
526
Chris Lattnerf21f0202006-09-02 05:26:59 +0000527/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
528/// interval as the specified value number. The LiveRanges in RHS are
529/// allowed to overlap with LiveRanges in the current interval, but only if
530/// the overlapping LiveRanges have the specified value number.
531void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000532 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000533 // TODO: Make this more efficient.
534 iterator InsertPos = begin();
535 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000536 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000537 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000538 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000539 InsertPos = addRangeFrom(Tmp, InsertPos);
540 }
541}
542
543
Evan Cheng32dfbea2007-10-12 08:50:34 +0000544/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
545/// in RHS into this live interval as the specified value number.
546/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000547/// current interval, it will replace the value numbers of the overlaped
548/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000549void LiveInterval::MergeValueInAsValue(
550 const LiveInterval &RHS,
551 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000552 SmallVector<VNInfo*, 4> ReplacedValNos;
553 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000554 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000555 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000556 if (I->valno != RHSValNo)
557 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000558 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000559 IP = std::upper_bound(IP, end(), Start);
560 // If the start of this range overlaps with an existing liverange, trim it.
561 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000562 if (IP[-1].valno != LHSValNo) {
563 ReplacedValNos.push_back(IP[-1].valno);
564 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000565 }
566 Start = IP[-1].end;
567 // Trimmed away the whole range?
568 if (Start >= End) continue;
569 }
570 // If the end of this range overlaps with an existing liverange, trim it.
571 if (IP != end() && End > IP->start) {
572 if (IP->valno != LHSValNo) {
573 ReplacedValNos.push_back(IP->valno);
574 IP->valno = LHSValNo; // Update val#.
575 }
576 End = IP->start;
577 // If this trimmed away the whole range, ignore it.
578 if (Start == End) continue;
579 }
580
Evan Cheng32dfbea2007-10-12 08:50:34 +0000581 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000582 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
583 }
584
585
586 SmallSet<VNInfo*, 4> Seen;
587 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
588 VNInfo *V1 = ReplacedValNos[i];
589 if (Seen.insert(V1)) {
590 bool isDead = true;
591 for (const_iterator I = begin(), E = end(); I != E; ++I)
592 if (I->valno == V1) {
593 isDead = false;
594 break;
595 }
596 if (isDead) {
597 // Now that V1 is dead, remove it. If it is the largest value number,
598 // just nuke it (and any other deleted values neighboring it), otherwise
599 // mark it as ~1U so it can be nuked later.
600 if (V1->id == getNumValNums()-1) {
601 do {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000602 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000603 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000604 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000605 V1->setIsUnused(true);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000606 }
607 }
608 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000609 }
610}
611
612
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000613/// MergeInClobberRanges - For any live ranges that are not defined in the
614/// current interval, but are defined in the Clobbers interval, mark them
615/// used with an unknown definition value.
Lang Hames233a60e2009-11-03 23:52:08 +0000616void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
617 const LiveInterval &Clobbers,
Benjamin Kramer991de142010-03-30 20:16:45 +0000618 VNInfo::Allocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000619 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000620
Evan Cheng0adb5272009-04-25 09:25:19 +0000621 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000622 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000623 iterator IP = begin();
624 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000625 // For every val# in the Clobbers interval, create a new "unknown" val#.
626 VNInfo *ClobberValNo = 0;
627 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
628 if (VI != ValNoMaps.end())
629 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000630 else if (UnusedValNo)
631 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000632 else {
Lang Hames86511252009-09-04 20:41:11 +0000633 UnusedValNo = ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000634 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000635 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
636 }
637
Dan Gohman97121ba2009-04-08 00:15:30 +0000638 bool Done = false;
Lang Hames233a60e2009-11-03 23:52:08 +0000639 SlotIndex Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000640 // If a clobber range starts before an existing range and ends after
641 // it, the clobber range will need to be split into multiple ranges.
642 // Loop until the entire clobber range is handled.
643 while (!Done) {
644 Done = true;
645 IP = std::upper_bound(IP, end(), Start);
Lang Hames233a60e2009-11-03 23:52:08 +0000646 SlotIndex SubRangeStart = Start;
647 SlotIndex SubRangeEnd = End;
Dan Gohman97121ba2009-04-08 00:15:30 +0000648
649 // If the start of this range overlaps with an existing liverange, trim it.
650 if (IP != begin() && IP[-1].end > SubRangeStart) {
651 SubRangeStart = IP[-1].end;
652 // Trimmed away the whole range?
653 if (SubRangeStart >= SubRangeEnd) continue;
654 }
655 // If the end of this range overlaps with an existing liverange, trim it.
656 if (IP != end() && SubRangeEnd > IP->start) {
657 // If the clobber live range extends beyond the existing live range,
658 // it'll need at least another live range, so set the flag to keep
659 // iterating.
660 if (SubRangeEnd > IP->end) {
661 Start = IP->end;
662 Done = false;
663 }
664 SubRangeEnd = IP->start;
665 // If this trimmed away the whole range, ignore it.
666 if (SubRangeStart == SubRangeEnd) continue;
667 }
668
669 // Insert the clobber interval.
670 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
671 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000672 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000673 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000674 }
Evan Cheng27e46662009-04-27 17:35:19 +0000675
676 if (UnusedValNo) {
677 // Delete the last unused val#.
678 valnos.pop_back();
Evan Cheng27e46662009-04-27 17:35:19 +0000679 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000680}
681
Lang Hames233a60e2009-11-03 23:52:08 +0000682void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
683 SlotIndex Start,
684 SlotIndex End,
Benjamin Kramer991de142010-03-30 20:16:45 +0000685 VNInfo::Allocator &VNInfoAllocator) {
Evan Chenga2e64352009-03-11 00:03:21 +0000686 // Find a value # to use for the clobber ranges. If there is already a value#
687 // for unknown values, use it.
Lang Hames86511252009-09-04 20:41:11 +0000688 VNInfo *ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000689 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000690
691 iterator IP = begin();
692 IP = std::upper_bound(IP, end(), Start);
693
694 // If the start of this range overlaps with an existing liverange, trim it.
695 if (IP != begin() && IP[-1].end > Start) {
696 Start = IP[-1].end;
697 // Trimmed away the whole range?
698 if (Start >= End) return;
699 }
700 // If the end of this range overlaps with an existing liverange, trim it.
701 if (IP != end() && End > IP->start) {
702 End = IP->start;
703 // If this trimmed away the whole range, ignore it.
704 if (Start == End) return;
705 }
706
707 // Insert the clobber interval.
708 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
709}
710
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000711/// MergeValueNumberInto - This method is called when two value nubmers
712/// are found to be equivalent. This eliminates V1, replacing all
713/// LiveRanges with the V1 value number with the V2 value number. This can
714/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000715VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000716 assert(V1 != V2 && "Identical value#'s are always equivalent!");
717
718 // This code actually merges the (numerically) larger value number into the
719 // smaller value number, which is likely to allow us to compactify the value
720 // space. The only thing we have to be careful of is to preserve the
721 // instruction that defines the result value.
722
723 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000724 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000725 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000726 std::swap(V1, V2);
727 }
728
729 // Merge V1 live ranges into V2.
730 for (iterator I = begin(); I != end(); ) {
731 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000732 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000733
734 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
735 // range, extend it.
736 if (LR != begin()) {
737 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000738 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000739 Prev->end = LR->end;
740
741 // Erase this live-range.
742 ranges.erase(LR);
743 I = Prev+1;
744 LR = Prev;
745 }
746 }
747
748 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
749 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000750 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000751
752 // If we can merge it into later V2 live ranges, do so now. We ignore any
753 // following V1 live ranges, as they will be merged in subsequent iterations
754 // of the loop.
755 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000756 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000757 LR->end = I->end;
758 ranges.erase(I);
759 I = LR+1;
760 }
761 }
762 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000763
764 // Now that V1 is dead, remove it. If it is the largest value number, just
765 // nuke it (and any other deleted values neighboring it), otherwise mark it as
766 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000767 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000768 do {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000769 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000770 } while (valnos.back()->isUnused());
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000771 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000772 V1->setIsUnused(true);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000773 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000774
775 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000776}
777
Evan Cheng32dfbea2007-10-12 08:50:34 +0000778void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000779 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000780 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000781 ranges.clear();
782 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000783 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000784 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
785
Evan Cheng32dfbea2007-10-12 08:50:34 +0000786 weight = RHS.weight;
787 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
788 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000789 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000790 }
791 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
792 const LiveRange &LR = RHS.ranges[i];
793 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
794 }
795}
796
Evan Chenge52eef82007-04-17 20:25:11 +0000797unsigned LiveInterval::getSize() const {
798 unsigned Sum = 0;
799 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000800 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000801 return Sum;
802}
803
David Greene29ff37f2009-07-22 20:08:25 +0000804/// ComputeJoinedWeight - Set the weight of a live interval Joined
805/// after Other has been merged into it.
806void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
807 // If either of these intervals was spilled, the weight is the
808 // weight of the non-spilled interval. This can only happen with
809 // iterative coalescers.
810
David Greene92b78bb2009-07-22 22:32:19 +0000811 if (Other.weight != HUGE_VALF) {
812 weight += Other.weight;
813 }
814 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000815 !TargetRegisterInfo::isPhysicalRegister(reg)) {
816 // Remove this assert if you have an iterative coalescer
817 assert(0 && "Joining to spilled interval");
818 weight = Other.weight;
819 }
David Greene29ff37f2009-07-22 20:08:25 +0000820 else {
821 // Otherwise the weight stays the same
822 // Remove this assert if you have an iterative coalescer
823 assert(0 && "Joining from spilled interval");
824 }
825}
826
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000827raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
828 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
829}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000830
Chris Lattnerabf295f2004-07-24 02:52:23 +0000831void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000832 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000833}
834
Chris Lattnerc02497f2009-08-23 03:47:42 +0000835void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000836 if (isStackSlot())
837 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000838 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000839 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000840 else
841 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000842
Chris Lattner38135af2005-05-14 05:34:15 +0000843 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000844
Chris Lattner38135af2005-05-14 05:34:15 +0000845 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000846 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000847 else {
848 OS << " = ";
849 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000850 E = ranges.end(); I != E; ++I) {
851 OS << *I;
852 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
853 }
Chris Lattner38135af2005-05-14 05:34:15 +0000854 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000855
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000856 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000857 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000858 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000859 unsigned vnum = 0;
860 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
861 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000862 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000863 if (vnum) OS << " ";
864 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000865 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000866 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000867 } else {
Lang Hames61945692009-12-09 05:39:12 +0000868 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000869 OS << "?";
870 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000871 OS << vni->def;
Evan Chenga8d94f12007-08-07 23:49:57 +0000872 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000873 }
874 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000875}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000876
877void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000878 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000879}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000880
881
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000882void LiveRange::print(raw_ostream &os) const {
883 os << *this;
884}