blob: 9b057b0fec2502f3e369254e7014a5f3136c94e1 [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");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000164 const_iterator I = std::lower_bound(begin(), end(), End);
165 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000166}
167
Chris Lattnerb26c2152004-07-23 19:38:44 +0000168/// extendIntervalEndTo - This method is used when we want to extend the range
169/// specified by I to end at the specified endpoint. To do this, we should
170/// merge and eliminate all ranges that this will overlap with. The iterator is
171/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000172void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000173 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000174 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000175
Chris Lattnerb26c2152004-07-23 19:38:44 +0000176 // Search for the first interval that we can't merge with.
177 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000178 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000179 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000180 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000181
182 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
183 I->end = std::max(NewEnd, prior(MergeTo)->end);
184
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000185 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000186 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000187
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000188 // If the newly formed range now touches the range after it and if they have
189 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000190 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000191 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000192 I->end = Next->end;
193 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000194 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000195}
196
197
198/// extendIntervalStartTo - This method is used when we want to extend the range
199/// specified by I to start at the specified endpoint. To do this, we should
200/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000201LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000202LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000203 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000204 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000205
206 // Search for the first interval that we can't merge with.
207 Ranges::iterator MergeTo = I;
208 do {
209 if (MergeTo == ranges.begin()) {
210 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000211 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000212 return I;
213 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000214 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000215 --MergeTo;
216 } while (NewStart <= MergeTo->start);
217
218 // If we start in the middle of another interval, just delete a range and
219 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000220 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000221 MergeTo->end = I->end;
222 } else {
223 // Otherwise, extend the interval right after.
224 ++MergeTo;
225 MergeTo->start = NewStart;
226 MergeTo->end = I->end;
227 }
228
229 ranges.erase(next(MergeTo), next(I));
230 return MergeTo;
231}
232
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000233LiveInterval::iterator
234LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000235 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000236 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000237
238 // If the inserted interval starts in the middle or right at the end of
239 // another interval, just extend that interval to contain the range of LR.
240 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000241 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000242 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000243 if (B->start <= Start && B->end >= Start) {
244 extendIntervalEndTo(B, End);
245 return B;
246 }
247 } else {
248 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000249 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000250 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000251 "Cannot overlap two LiveRanges with differing ValID's"
252 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000253 }
254 }
255
256 // Otherwise, if this range ends in the middle of, or right next to, another
257 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000258 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000259 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000260 if (it->start <= End) {
261 it = extendIntervalStartTo(it, Start);
262
263 // If LR is a complete superset of an interval, we may need to grow its
264 // endpoint as well.
265 if (End > it->end)
266 extendIntervalEndTo(it, End);
267 return it;
268 }
269 } else {
270 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000271 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000272 assert(it->start >= End &&
273 "Cannot overlap two LiveRanges with differing ValID's");
274 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000275 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000276
277 // Otherwise, this is just a new range that doesn't interact with anything.
278 // Insert it.
279 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000280}
281
Lang Hames86511252009-09-04 20:41:11 +0000282/// isInOneLiveRange - Return true if the range specified is entirely in
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000283/// a single LiveRange of the live interval.
Lang Hames233a60e2009-11-03 23:52:08 +0000284bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000285 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
286 if (I == ranges.begin())
287 return false;
288 --I;
Lang Hames86511252009-09-04 20:41:11 +0000289 return I->containsRange(Start, End);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000290}
291
Chris Lattnerabf295f2004-07-24 02:52:23 +0000292
293/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000294/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000295void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000296 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000297 // Find the LiveRange containing this span.
298 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
299 assert(I != ranges.begin() && "Range is not in interval!");
300 --I;
Lang Hames86511252009-09-04 20:41:11 +0000301 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000302
303 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000304 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000305 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000306 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000307 if (RemoveDeadValNo) {
308 // Check if val# is dead.
309 bool isDead = true;
310 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
311 if (II != I && II->valno == ValNo) {
312 isDead = false;
313 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000314 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000315 if (isDead) {
316 // Now that ValNo is dead, remove it. If it is the largest value
317 // number, just nuke it (and any other deleted values neighboring it),
318 // otherwise mark it as ~1U so it can be nuked later.
319 if (ValNo->id == getNumValNums()-1) {
320 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000321 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000322 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000323 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000324 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000325 }
326 }
327 }
328
Chris Lattnerabf295f2004-07-24 02:52:23 +0000329 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000330 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000331 I->start = End;
332 return;
333 }
334
335 // Otherwise if the span we are removing is at the end of the LiveRange,
336 // adjust the other way.
337 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000338 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000339 return;
340 }
341
342 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000343 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000344 I->end = Start; // Trim the old interval.
345
346 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000347 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000348}
349
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000350/// removeValNo - Remove all the ranges defined by the specified value#.
351/// Also remove the value# from value# list.
352void LiveInterval::removeValNo(VNInfo *ValNo) {
353 if (empty()) return;
354 Ranges::iterator I = ranges.end();
355 Ranges::iterator E = ranges.begin();
356 do {
357 --I;
358 if (I->valno == ValNo)
359 ranges.erase(I);
360 } while (I != E);
361 // Now that ValNo is dead, remove it. If it is the largest value
362 // number, just nuke it (and any other deleted values neighboring it),
363 // otherwise mark it as ~1U so it can be nuked later.
364 if (ValNo->id == getNumValNums()-1) {
365 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000366 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000367 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000368 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000369 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000370 }
371}
Lang Hames86511252009-09-04 20:41:11 +0000372
Chris Lattnerabf295f2004-07-24 02:52:23 +0000373/// getLiveRangeContaining - Return the live range that contains the
374/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000375LiveInterval::const_iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000376LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000377 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000378 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000379 --It;
380 if (It->contains(Idx))
381 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000382 }
383
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000384 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000385}
386
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000387LiveInterval::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000388LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000389 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000390 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000391 --It;
392 if (It->contains(Idx))
393 return It;
394 }
395
396 return end();
397}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000398
Lang Hames86511252009-09-04 20:41:11 +0000399/// findDefinedVNInfo - Find the VNInfo defined by the specified
400/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000401VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000402 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000403 i != e; ++i) {
404 if ((*i)->def == Idx)
405 return *i;
406 }
407
408 return 0;
409}
410
411/// findDefinedVNInfo - Find the VNInfo defined by the specified
412/// register (stack inteval).
413VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
414 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
415 i != e; ++i) {
416 if ((*i)->getReg() == reg)
417 return *i;
418 }
419 return 0;
Evan Cheng3f32d652008-06-04 09:18:41 +0000420}
421
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000422/// join - Join two live intervals (this, and other) together. This applies
423/// mappings to the value numbers in the LHS/RHS intervals as specified. If
424/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000425void LiveInterval::join(LiveInterval &Other,
426 const int *LHSValNoAssignments,
David Greeneaf992f72007-09-06 19:46:46 +0000427 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000428 SmallVector<VNInfo*, 16> &NewVNInfo,
429 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000430 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000431 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000432 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000433 unsigned NumVals = getNumValNums();
434 unsigned NumNewVals = NewVNInfo.size();
435 for (unsigned i = 0; i != NumVals; ++i) {
436 unsigned LHSValID = LHSValNoAssignments[i];
437 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000438 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000439 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000440 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000441
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000442 // If we have to apply a mapping to our base interval assignment, rewrite it
443 // now.
444 if (MustMapCurValNos) {
445 // Map the first live range.
446 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000447 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000448 ++OutIt;
449 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000450 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000451
452 // If this live range has the same value # as its immediate predecessor,
453 // and if they are neighbors, remove one LiveRange. This happens when we
454 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000455 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000456 (OutIt-1)->end = OutIt->end;
457 } else {
458 if (I != OutIt) {
459 OutIt->start = I->start;
460 OutIt->end = I->end;
461 }
462
463 // Didn't merge, on to the next one.
464 ++OutIt;
465 }
466 }
467
468 // If we merge some live ranges, chop off the end.
469 ranges.erase(OutIt, end());
470 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000471
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000472 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000473 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000474 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
475 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
476
477 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000478 // LiveInterval now. Also remove dead val#'s.
479 unsigned NumValNos = 0;
480 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000482 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000483 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000484 valnos.push_back(VNI);
485 else
486 valnos[NumValNos] = VNI;
487 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000488 }
489 }
Evan Cheng34301352007-09-01 02:03:17 +0000490 if (NumNewVals < NumVals)
491 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000492
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000493 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000494 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000495 unsigned RangeNo = 0;
496 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
497 // Map the valno in the other live range to the current live range.
498 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000499 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000500 InsertPos = addRangeFrom(*I, InsertPos);
501 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000502
David Greene29ff37f2009-07-22 20:08:25 +0000503 ComputeJoinedWeight(Other);
Evan Cheng90f95f82009-06-14 20:22:55 +0000504
505 // Update regalloc hint if currently there isn't one.
506 if (TargetRegisterInfo::isVirtualRegister(reg) &&
507 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
Evan Cheng358dec52009-06-15 08:28:29 +0000508 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
509 if (Hint.first == 0 && Hint.second == 0) {
510 std::pair<unsigned, unsigned> OtherHint =
Evan Cheng90f95f82009-06-14 20:22:55 +0000511 MRI->getRegAllocationHint(Other.reg);
Evan Cheng358dec52009-06-15 08:28:29 +0000512 if (OtherHint.first || OtherHint.second)
Evan Cheng90f95f82009-06-14 20:22:55 +0000513 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
514 }
515 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000516}
517
Chris Lattnerf21f0202006-09-02 05:26:59 +0000518/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
519/// interval as the specified value number. The LiveRanges in RHS are
520/// allowed to overlap with LiveRanges in the current interval, but only if
521/// the overlapping LiveRanges have the specified value number.
522void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000523 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000524 // TODO: Make this more efficient.
525 iterator InsertPos = begin();
526 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000527 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000528 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000529 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000530 InsertPos = addRangeFrom(Tmp, InsertPos);
531 }
532}
533
534
Evan Cheng32dfbea2007-10-12 08:50:34 +0000535/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
536/// in RHS into this live interval as the specified value number.
537/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000538/// current interval, it will replace the value numbers of the overlaped
539/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000540void LiveInterval::MergeValueInAsValue(
541 const LiveInterval &RHS,
542 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000543 SmallVector<VNInfo*, 4> ReplacedValNos;
544 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000545 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000546 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000547 if (I->valno != RHSValNo)
548 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000549 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000550 IP = std::upper_bound(IP, end(), Start);
551 // If the start of this range overlaps with an existing liverange, trim it.
552 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000553 if (IP[-1].valno != LHSValNo) {
554 ReplacedValNos.push_back(IP[-1].valno);
555 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000556 }
557 Start = IP[-1].end;
558 // Trimmed away the whole range?
559 if (Start >= End) continue;
560 }
561 // If the end of this range overlaps with an existing liverange, trim it.
562 if (IP != end() && End > IP->start) {
563 if (IP->valno != LHSValNo) {
564 ReplacedValNos.push_back(IP->valno);
565 IP->valno = LHSValNo; // Update val#.
566 }
567 End = IP->start;
568 // If this trimmed away the whole range, ignore it.
569 if (Start == End) continue;
570 }
571
Evan Cheng32dfbea2007-10-12 08:50:34 +0000572 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000573 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
574 }
575
576
577 SmallSet<VNInfo*, 4> Seen;
578 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
579 VNInfo *V1 = ReplacedValNos[i];
580 if (Seen.insert(V1)) {
581 bool isDead = true;
582 for (const_iterator I = begin(), E = end(); I != E; ++I)
583 if (I->valno == V1) {
584 isDead = false;
585 break;
586 }
587 if (isDead) {
588 // Now that V1 is dead, remove it. If it is the largest value number,
589 // just nuke it (and any other deleted values neighboring it), otherwise
590 // mark it as ~1U so it can be nuked later.
591 if (V1->id == getNumValNums()-1) {
592 do {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000593 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000594 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000595 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000596 V1->setIsUnused(true);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000597 }
598 }
599 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000600 }
601}
602
603
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000604/// MergeInClobberRanges - For any live ranges that are not defined in the
605/// current interval, but are defined in the Clobbers interval, mark them
606/// used with an unknown definition value.
Lang Hames233a60e2009-11-03 23:52:08 +0000607void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
608 const LiveInterval &Clobbers,
Benjamin Kramer991de142010-03-30 20:16:45 +0000609 VNInfo::Allocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000610 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000611
Evan Cheng0adb5272009-04-25 09:25:19 +0000612 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000613 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000614 iterator IP = begin();
615 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000616 // For every val# in the Clobbers interval, create a new "unknown" val#.
617 VNInfo *ClobberValNo = 0;
618 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
619 if (VI != ValNoMaps.end())
620 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000621 else if (UnusedValNo)
622 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000623 else {
Lang Hames86511252009-09-04 20:41:11 +0000624 UnusedValNo = ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000625 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000626 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
627 }
628
Dan Gohman97121ba2009-04-08 00:15:30 +0000629 bool Done = false;
Lang Hames233a60e2009-11-03 23:52:08 +0000630 SlotIndex Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000631 // If a clobber range starts before an existing range and ends after
632 // it, the clobber range will need to be split into multiple ranges.
633 // Loop until the entire clobber range is handled.
634 while (!Done) {
635 Done = true;
636 IP = std::upper_bound(IP, end(), Start);
Lang Hames233a60e2009-11-03 23:52:08 +0000637 SlotIndex SubRangeStart = Start;
638 SlotIndex SubRangeEnd = End;
Dan Gohman97121ba2009-04-08 00:15:30 +0000639
640 // If the start of this range overlaps with an existing liverange, trim it.
641 if (IP != begin() && IP[-1].end > SubRangeStart) {
642 SubRangeStart = IP[-1].end;
643 // Trimmed away the whole range?
644 if (SubRangeStart >= SubRangeEnd) continue;
645 }
646 // If the end of this range overlaps with an existing liverange, trim it.
647 if (IP != end() && SubRangeEnd > IP->start) {
648 // If the clobber live range extends beyond the existing live range,
649 // it'll need at least another live range, so set the flag to keep
650 // iterating.
651 if (SubRangeEnd > IP->end) {
652 Start = IP->end;
653 Done = false;
654 }
655 SubRangeEnd = IP->start;
656 // If this trimmed away the whole range, ignore it.
657 if (SubRangeStart == SubRangeEnd) continue;
658 }
659
660 // Insert the clobber interval.
661 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
662 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000663 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000664 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000665 }
Evan Cheng27e46662009-04-27 17:35:19 +0000666
667 if (UnusedValNo) {
668 // Delete the last unused val#.
669 valnos.pop_back();
Evan Cheng27e46662009-04-27 17:35:19 +0000670 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000671}
672
Lang Hames233a60e2009-11-03 23:52:08 +0000673void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
674 SlotIndex Start,
675 SlotIndex End,
Benjamin Kramer991de142010-03-30 20:16:45 +0000676 VNInfo::Allocator &VNInfoAllocator) {
Evan Chenga2e64352009-03-11 00:03:21 +0000677 // Find a value # to use for the clobber ranges. If there is already a value#
678 // for unknown values, use it.
Lang Hames86511252009-09-04 20:41:11 +0000679 VNInfo *ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000680 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000681
682 iterator IP = begin();
683 IP = std::upper_bound(IP, end(), Start);
684
685 // If the start of this range overlaps with an existing liverange, trim it.
686 if (IP != begin() && IP[-1].end > Start) {
687 Start = IP[-1].end;
688 // Trimmed away the whole range?
689 if (Start >= End) return;
690 }
691 // If the end of this range overlaps with an existing liverange, trim it.
692 if (IP != end() && End > IP->start) {
693 End = IP->start;
694 // If this trimmed away the whole range, ignore it.
695 if (Start == End) return;
696 }
697
698 // Insert the clobber interval.
699 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
700}
701
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000702/// MergeValueNumberInto - This method is called when two value nubmers
703/// are found to be equivalent. This eliminates V1, replacing all
704/// LiveRanges with the V1 value number with the V2 value number. This can
705/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000706VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000707 assert(V1 != V2 && "Identical value#'s are always equivalent!");
708
709 // This code actually merges the (numerically) larger value number into the
710 // smaller value number, which is likely to allow us to compactify the value
711 // space. The only thing we have to be careful of is to preserve the
712 // instruction that defines the result value.
713
714 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000715 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000716 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000717 std::swap(V1, V2);
718 }
719
720 // Merge V1 live ranges into V2.
721 for (iterator I = begin(); I != end(); ) {
722 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000723 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000724
725 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
726 // range, extend it.
727 if (LR != begin()) {
728 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000729 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000730 Prev->end = LR->end;
731
732 // Erase this live-range.
733 ranges.erase(LR);
734 I = Prev+1;
735 LR = Prev;
736 }
737 }
738
739 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
740 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000741 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000742
743 // If we can merge it into later V2 live ranges, do so now. We ignore any
744 // following V1 live ranges, as they will be merged in subsequent iterations
745 // of the loop.
746 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000747 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000748 LR->end = I->end;
749 ranges.erase(I);
750 I = LR+1;
751 }
752 }
753 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000754
755 // Now that V1 is dead, remove it. If it is the largest value number, just
756 // nuke it (and any other deleted values neighboring it), otherwise mark it as
757 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000758 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000759 do {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000760 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000761 } while (valnos.back()->isUnused());
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000762 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000763 V1->setIsUnused(true);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000764 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000765
766 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000767}
768
Evan Cheng32dfbea2007-10-12 08:50:34 +0000769void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000770 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000771 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000772 ranges.clear();
773 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000774 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000775 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
776
Evan Cheng32dfbea2007-10-12 08:50:34 +0000777 weight = RHS.weight;
778 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
779 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000780 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000781 }
782 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
783 const LiveRange &LR = RHS.ranges[i];
784 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
785 }
786}
787
Evan Chenge52eef82007-04-17 20:25:11 +0000788unsigned LiveInterval::getSize() const {
789 unsigned Sum = 0;
790 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000791 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000792 return Sum;
793}
794
David Greene29ff37f2009-07-22 20:08:25 +0000795/// ComputeJoinedWeight - Set the weight of a live interval Joined
796/// after Other has been merged into it.
797void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
798 // If either of these intervals was spilled, the weight is the
799 // weight of the non-spilled interval. This can only happen with
800 // iterative coalescers.
801
David Greene92b78bb2009-07-22 22:32:19 +0000802 if (Other.weight != HUGE_VALF) {
803 weight += Other.weight;
804 }
805 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000806 !TargetRegisterInfo::isPhysicalRegister(reg)) {
807 // Remove this assert if you have an iterative coalescer
808 assert(0 && "Joining to spilled interval");
809 weight = Other.weight;
810 }
David Greene29ff37f2009-07-22 20:08:25 +0000811 else {
812 // Otherwise the weight stays the same
813 // Remove this assert if you have an iterative coalescer
814 assert(0 && "Joining from spilled interval");
815 }
816}
817
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000818raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
819 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
820}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000821
Chris Lattnerabf295f2004-07-24 02:52:23 +0000822void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000823 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000824}
825
Chris Lattnerc02497f2009-08-23 03:47:42 +0000826void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000827 if (isStackSlot())
828 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000829 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000830 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000831 else
832 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000833
Chris Lattner38135af2005-05-14 05:34:15 +0000834 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000835
Chris Lattner38135af2005-05-14 05:34:15 +0000836 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000837 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000838 else {
839 OS << " = ";
840 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000841 E = ranges.end(); I != E; ++I) {
842 OS << *I;
843 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
844 }
Chris Lattner38135af2005-05-14 05:34:15 +0000845 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000846
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000847 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000848 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000849 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000850 unsigned vnum = 0;
851 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
852 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000853 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000854 if (vnum) OS << " ";
855 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000856 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000857 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000858 } else {
Lang Hames61945692009-12-09 05:39:12 +0000859 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000860 OS << "?";
861 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000862 OS << vni->def;
Evan Chenga8d94f12007-08-07 23:49:57 +0000863 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000864 }
865 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000866}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000867
868void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000869 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000870}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000871
872
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000873void LiveRange::print(raw_ostream &os) const {
874 os << *this;
875}