blob: ad5728458062fd1e07a39f233d5ba0f13da3b64e [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 {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +0000122 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +0000123 const_iterator i = begin();
124 const_iterator ie = end();
125 const_iterator j = StartPos;
126 const_iterator je = other.end();
127
128 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000129 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000130
Chris Lattnerfb449b92004-07-23 17:49:16 +0000131 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000132 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000133 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000134 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000135 ++StartPos;
136 if (StartPos != other.end() && StartPos->start <= i->start) {
137 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000138 j = std::upper_bound(j, je, i->start);
139 if (j != other.ranges.begin()) --j;
140 }
Chris Lattneraa141472004-07-23 18:40:00 +0000141 } else {
142 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000143 }
144
Chris Lattner9fddc122004-11-18 05:28:21 +0000145 if (j == je) return false;
146
147 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000148 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000149 std::swap(i, j);
150 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000151 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000152
153 if (i->end > j->start)
154 return true;
155 ++i;
156 }
157
158 return false;
159}
160
Evan Chengcccdb2b2009-04-18 08:52:15 +0000161/// overlaps - Return true if the live interval overlaps a range specified
162/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000163bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000164 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000165 const_iterator I = std::lower_bound(begin(), end(), End);
166 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000167}
168
Chris Lattnerb26c2152004-07-23 19:38:44 +0000169/// extendIntervalEndTo - This method is used when we want to extend the range
170/// specified by I to end at the specified endpoint. To do this, we should
171/// merge and eliminate all ranges that this will overlap with. The iterator is
172/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000173void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000174 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000175 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000176
Chris Lattnerb26c2152004-07-23 19:38:44 +0000177 // Search for the first interval that we can't merge with.
178 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000179 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000180 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000181 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000182
183 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
184 I->end = std::max(NewEnd, prior(MergeTo)->end);
185
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000186 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000187 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000188
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000189 // If the newly formed range now touches the range after it and if they have
190 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000191 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000192 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000193 I->end = Next->end;
194 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000195 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000196}
197
198
199/// extendIntervalStartTo - This method is used when we want to extend the range
200/// specified by I to start at the specified endpoint. To do this, we should
201/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000202LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000203LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000204 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000205 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000206
207 // Search for the first interval that we can't merge with.
208 Ranges::iterator MergeTo = I;
209 do {
210 if (MergeTo == ranges.begin()) {
211 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000212 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000213 return I;
214 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000215 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000216 --MergeTo;
217 } while (NewStart <= MergeTo->start);
218
219 // If we start in the middle of another interval, just delete a range and
220 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000222 MergeTo->end = I->end;
223 } else {
224 // Otherwise, extend the interval right after.
225 ++MergeTo;
226 MergeTo->start = NewStart;
227 MergeTo->end = I->end;
228 }
229
230 ranges.erase(next(MergeTo), next(I));
231 return MergeTo;
232}
233
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000234LiveInterval::iterator
235LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000236 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000237 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000238
239 // If the inserted interval starts in the middle or right at the end of
240 // another interval, just extend that interval to contain the range of LR.
241 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000242 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000243 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000244 if (B->start <= Start && B->end >= Start) {
245 extendIntervalEndTo(B, End);
246 return B;
247 }
248 } else {
249 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000250 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000251 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000252 "Cannot overlap two LiveRanges with differing ValID's"
253 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000254 }
255 }
256
257 // Otherwise, if this range ends in the middle of, or right next to, another
258 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000259 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000260 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000261 if (it->start <= End) {
262 it = extendIntervalStartTo(it, Start);
263
264 // If LR is a complete superset of an interval, we may need to grow its
265 // endpoint as well.
266 if (End > it->end)
267 extendIntervalEndTo(it, End);
268 return it;
269 }
270 } else {
271 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000272 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000273 assert(it->start >= End &&
274 "Cannot overlap two LiveRanges with differing ValID's");
275 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000276 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000277
278 // Otherwise, this is just a new range that doesn't interact with anything.
279 // Insert it.
280 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000281}
282
Lang Hames86511252009-09-04 20:41:11 +0000283/// isInOneLiveRange - Return true if the range specified is entirely in
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000284/// a single LiveRange of the live interval.
Lang Hames233a60e2009-11-03 23:52:08 +0000285bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000286 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
287 if (I == ranges.begin())
288 return false;
289 --I;
Lang Hames86511252009-09-04 20:41:11 +0000290 return I->containsRange(Start, End);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000291}
292
Chris Lattnerabf295f2004-07-24 02:52:23 +0000293
294/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000295/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000296void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000297 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000298 // Find the LiveRange containing this span.
299 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
300 assert(I != ranges.begin() && "Range is not in interval!");
301 --I;
Lang Hames86511252009-09-04 20:41:11 +0000302 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000303
304 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000305 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000306 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000307 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000308 if (RemoveDeadValNo) {
309 // Check if val# is dead.
310 bool isDead = true;
311 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
312 if (II != I && II->valno == ValNo) {
313 isDead = false;
314 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000315 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000316 if (isDead) {
317 // Now that ValNo is dead, remove it. If it is the largest value
318 // number, just nuke it (and any other deleted values neighboring it),
319 // otherwise mark it as ~1U so it can be nuked later.
320 if (ValNo->id == getNumValNums()-1) {
321 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000322 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000323 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000324 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000325 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000326 }
327 }
328 }
329
Chris Lattnerabf295f2004-07-24 02:52:23 +0000330 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000331 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000332 I->start = End;
333 return;
334 }
335
336 // Otherwise if the span we are removing is at the end of the LiveRange,
337 // adjust the other way.
338 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000339 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340 return;
341 }
342
343 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000344 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000345 I->end = Start; // Trim the old interval.
346
347 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000348 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000349}
350
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000351/// removeValNo - Remove all the ranges defined by the specified value#.
352/// Also remove the value# from value# list.
353void LiveInterval::removeValNo(VNInfo *ValNo) {
354 if (empty()) return;
355 Ranges::iterator I = ranges.end();
356 Ranges::iterator E = ranges.begin();
357 do {
358 --I;
359 if (I->valno == ValNo)
360 ranges.erase(I);
361 } while (I != E);
362 // Now that ValNo is dead, remove it. If it is the largest value
363 // number, just nuke it (and any other deleted values neighboring it),
364 // otherwise mark it as ~1U so it can be nuked later.
365 if (ValNo->id == getNumValNums()-1) {
366 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000367 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000368 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000369 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000370 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000371 }
372}
Lang Hames86511252009-09-04 20:41:11 +0000373
Chris Lattnerabf295f2004-07-24 02:52:23 +0000374/// getLiveRangeContaining - Return the live range that contains the
375/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000376LiveInterval::const_iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000377LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000378 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000379 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000380 --It;
381 if (It->contains(Idx))
382 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000383 }
384
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000385 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000386}
387
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000388LiveInterval::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000389LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000390 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000391 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000392 --It;
393 if (It->contains(Idx))
394 return It;
395 }
396
397 return end();
398}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000399
Lang Hames86511252009-09-04 20:41:11 +0000400/// findDefinedVNInfo - Find the VNInfo defined by the specified
401/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000402VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000403 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000404 i != e; ++i) {
405 if ((*i)->def == Idx)
406 return *i;
407 }
408
409 return 0;
410}
411
412/// findDefinedVNInfo - Find the VNInfo defined by the specified
413/// register (stack inteval).
414VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
415 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
416 i != e; ++i) {
417 if ((*i)->getReg() == reg)
418 return *i;
419 }
420 return 0;
Evan Cheng3f32d652008-06-04 09:18:41 +0000421}
422
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000423/// join - Join two live intervals (this, and other) together. This applies
424/// mappings to the value numbers in the LHS/RHS intervals as specified. If
425/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000426void LiveInterval::join(LiveInterval &Other,
427 const int *LHSValNoAssignments,
David Greeneaf992f72007-09-06 19:46:46 +0000428 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000429 SmallVector<VNInfo*, 16> &NewVNInfo,
430 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000431 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000432 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000433 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000434 unsigned NumVals = getNumValNums();
435 unsigned NumNewVals = NewVNInfo.size();
436 for (unsigned i = 0; i != NumVals; ++i) {
437 unsigned LHSValID = LHSValNoAssignments[i];
438 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000439 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000440 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000441 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000442
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000443 // If we have to apply a mapping to our base interval assignment, rewrite it
444 // now.
445 if (MustMapCurValNos) {
446 // Map the first live range.
447 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000448 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000449 ++OutIt;
450 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000451 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000452
453 // If this live range has the same value # as its immediate predecessor,
454 // and if they are neighbors, remove one LiveRange. This happens when we
455 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000456 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000457 (OutIt-1)->end = OutIt->end;
458 } else {
459 if (I != OutIt) {
460 OutIt->start = I->start;
461 OutIt->end = I->end;
462 }
463
464 // Didn't merge, on to the next one.
465 ++OutIt;
466 }
467 }
468
469 // If we merge some live ranges, chop off the end.
470 ranges.erase(OutIt, end());
471 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000472
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000473 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000474 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000475 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
476 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
477
478 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000479 // LiveInterval now. Also remove dead val#'s.
480 unsigned NumValNos = 0;
481 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000482 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000483 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000484 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000485 valnos.push_back(VNI);
486 else
487 valnos[NumValNos] = VNI;
488 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000489 }
490 }
Evan Cheng34301352007-09-01 02:03:17 +0000491 if (NumNewVals < NumVals)
492 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000493
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000494 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000495 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000496 unsigned RangeNo = 0;
497 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
498 // Map the valno in the other live range to the current live range.
499 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000500 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000501 InsertPos = addRangeFrom(*I, InsertPos);
502 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000503
David Greene29ff37f2009-07-22 20:08:25 +0000504 ComputeJoinedWeight(Other);
Evan Cheng90f95f82009-06-14 20:22:55 +0000505
506 // Update regalloc hint if currently there isn't one.
507 if (TargetRegisterInfo::isVirtualRegister(reg) &&
508 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
Evan Cheng358dec52009-06-15 08:28:29 +0000509 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
510 if (Hint.first == 0 && Hint.second == 0) {
511 std::pair<unsigned, unsigned> OtherHint =
Evan Cheng90f95f82009-06-14 20:22:55 +0000512 MRI->getRegAllocationHint(Other.reg);
Evan Cheng358dec52009-06-15 08:28:29 +0000513 if (OtherHint.first || OtherHint.second)
Evan Cheng90f95f82009-06-14 20:22:55 +0000514 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
515 }
516 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000517}
518
Chris Lattnerf21f0202006-09-02 05:26:59 +0000519/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
520/// interval as the specified value number. The LiveRanges in RHS are
521/// allowed to overlap with LiveRanges in the current interval, but only if
522/// the overlapping LiveRanges have the specified value number.
523void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000524 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000525 // TODO: Make this more efficient.
526 iterator InsertPos = begin();
527 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000528 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000529 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000530 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000531 InsertPos = addRangeFrom(Tmp, InsertPos);
532 }
533}
534
535
Evan Cheng32dfbea2007-10-12 08:50:34 +0000536/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
537/// in RHS into this live interval as the specified value number.
538/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000539/// current interval, it will replace the value numbers of the overlaped
540/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000541void LiveInterval::MergeValueInAsValue(
542 const LiveInterval &RHS,
543 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000544 SmallVector<VNInfo*, 4> ReplacedValNos;
545 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000546 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000547 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000548 if (I->valno != RHSValNo)
549 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000550 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000551 IP = std::upper_bound(IP, end(), Start);
552 // If the start of this range overlaps with an existing liverange, trim it.
553 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000554 if (IP[-1].valno != LHSValNo) {
555 ReplacedValNos.push_back(IP[-1].valno);
556 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000557 }
558 Start = IP[-1].end;
559 // Trimmed away the whole range?
560 if (Start >= End) continue;
561 }
562 // If the end of this range overlaps with an existing liverange, trim it.
563 if (IP != end() && End > IP->start) {
564 if (IP->valno != LHSValNo) {
565 ReplacedValNos.push_back(IP->valno);
566 IP->valno = LHSValNo; // Update val#.
567 }
568 End = IP->start;
569 // If this trimmed away the whole range, ignore it.
570 if (Start == End) continue;
571 }
572
Evan Cheng32dfbea2007-10-12 08:50:34 +0000573 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000574 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
575 }
576
577
578 SmallSet<VNInfo*, 4> Seen;
579 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
580 VNInfo *V1 = ReplacedValNos[i];
581 if (Seen.insert(V1)) {
582 bool isDead = true;
583 for (const_iterator I = begin(), E = end(); I != E; ++I)
584 if (I->valno == V1) {
585 isDead = false;
586 break;
587 }
588 if (isDead) {
589 // Now that V1 is dead, remove it. If it is the largest value number,
590 // just nuke it (and any other deleted values neighboring it), otherwise
591 // mark it as ~1U so it can be nuked later.
592 if (V1->id == getNumValNums()-1) {
593 do {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000594 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000595 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000596 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000597 V1->setIsUnused(true);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000598 }
599 }
600 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000601 }
602}
603
604
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000605/// MergeInClobberRanges - For any live ranges that are not defined in the
606/// current interval, but are defined in the Clobbers interval, mark them
607/// used with an unknown definition value.
Lang Hames233a60e2009-11-03 23:52:08 +0000608void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
609 const LiveInterval &Clobbers,
Benjamin Kramer991de142010-03-30 20:16:45 +0000610 VNInfo::Allocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000611 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000612
Evan Cheng0adb5272009-04-25 09:25:19 +0000613 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000614 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000615 iterator IP = begin();
616 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000617 // For every val# in the Clobbers interval, create a new "unknown" val#.
618 VNInfo *ClobberValNo = 0;
619 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
620 if (VI != ValNoMaps.end())
621 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000622 else if (UnusedValNo)
623 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000624 else {
Lang Hames86511252009-09-04 20:41:11 +0000625 UnusedValNo = ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000626 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000627 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
628 }
629
Dan Gohman97121ba2009-04-08 00:15:30 +0000630 bool Done = false;
Lang Hames233a60e2009-11-03 23:52:08 +0000631 SlotIndex Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000632 // If a clobber range starts before an existing range and ends after
633 // it, the clobber range will need to be split into multiple ranges.
634 // Loop until the entire clobber range is handled.
635 while (!Done) {
636 Done = true;
637 IP = std::upper_bound(IP, end(), Start);
Lang Hames233a60e2009-11-03 23:52:08 +0000638 SlotIndex SubRangeStart = Start;
639 SlotIndex SubRangeEnd = End;
Dan Gohman97121ba2009-04-08 00:15:30 +0000640
641 // If the start of this range overlaps with an existing liverange, trim it.
642 if (IP != begin() && IP[-1].end > SubRangeStart) {
643 SubRangeStart = IP[-1].end;
644 // Trimmed away the whole range?
645 if (SubRangeStart >= SubRangeEnd) continue;
646 }
647 // If the end of this range overlaps with an existing liverange, trim it.
648 if (IP != end() && SubRangeEnd > IP->start) {
649 // If the clobber live range extends beyond the existing live range,
650 // it'll need at least another live range, so set the flag to keep
651 // iterating.
652 if (SubRangeEnd > IP->end) {
653 Start = IP->end;
654 Done = false;
655 }
656 SubRangeEnd = IP->start;
657 // If this trimmed away the whole range, ignore it.
658 if (SubRangeStart == SubRangeEnd) continue;
659 }
660
661 // Insert the clobber interval.
662 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
663 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000664 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000665 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000666 }
Evan Cheng27e46662009-04-27 17:35:19 +0000667
668 if (UnusedValNo) {
669 // Delete the last unused val#.
670 valnos.pop_back();
Evan Cheng27e46662009-04-27 17:35:19 +0000671 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000672}
673
Lang Hames233a60e2009-11-03 23:52:08 +0000674void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
675 SlotIndex Start,
676 SlotIndex End,
Benjamin Kramer991de142010-03-30 20:16:45 +0000677 VNInfo::Allocator &VNInfoAllocator) {
Evan Chenga2e64352009-03-11 00:03:21 +0000678 // Find a value # to use for the clobber ranges. If there is already a value#
679 // for unknown values, use it.
Lang Hames86511252009-09-04 20:41:11 +0000680 VNInfo *ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000681 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000682
683 iterator IP = begin();
684 IP = std::upper_bound(IP, end(), Start);
685
686 // If the start of this range overlaps with an existing liverange, trim it.
687 if (IP != begin() && IP[-1].end > Start) {
688 Start = IP[-1].end;
689 // Trimmed away the whole range?
690 if (Start >= End) return;
691 }
692 // If the end of this range overlaps with an existing liverange, trim it.
693 if (IP != end() && End > IP->start) {
694 End = IP->start;
695 // If this trimmed away the whole range, ignore it.
696 if (Start == End) return;
697 }
698
699 // Insert the clobber interval.
700 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
701}
702
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000703/// MergeValueNumberInto - This method is called when two value nubmers
704/// are found to be equivalent. This eliminates V1, replacing all
705/// LiveRanges with the V1 value number with the V2 value number. This can
706/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000707VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000708 assert(V1 != V2 && "Identical value#'s are always equivalent!");
709
710 // This code actually merges the (numerically) larger value number into the
711 // smaller value number, which is likely to allow us to compactify the value
712 // space. The only thing we have to be careful of is to preserve the
713 // instruction that defines the result value.
714
715 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000716 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000717 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000718 std::swap(V1, V2);
719 }
720
721 // Merge V1 live ranges into V2.
722 for (iterator I = begin(); I != end(); ) {
723 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000724 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000725
726 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
727 // range, extend it.
728 if (LR != begin()) {
729 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000730 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000731 Prev->end = LR->end;
732
733 // Erase this live-range.
734 ranges.erase(LR);
735 I = Prev+1;
736 LR = Prev;
737 }
738 }
739
740 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
741 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000742 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000743
744 // If we can merge it into later V2 live ranges, do so now. We ignore any
745 // following V1 live ranges, as they will be merged in subsequent iterations
746 // of the loop.
747 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000748 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000749 LR->end = I->end;
750 ranges.erase(I);
751 I = LR+1;
752 }
753 }
754 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000755
756 // Now that V1 is dead, remove it. If it is the largest value number, just
757 // nuke it (and any other deleted values neighboring it), otherwise mark it as
758 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000759 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000760 do {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000761 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000762 } while (valnos.back()->isUnused());
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000763 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000764 V1->setIsUnused(true);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000765 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000766
767 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000768}
769
Evan Cheng32dfbea2007-10-12 08:50:34 +0000770void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000771 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000772 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000773 ranges.clear();
774 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000775 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000776 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
777
Evan Cheng32dfbea2007-10-12 08:50:34 +0000778 weight = RHS.weight;
779 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
780 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000781 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000782 }
783 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
784 const LiveRange &LR = RHS.ranges[i];
785 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
786 }
787}
788
Evan Chenge52eef82007-04-17 20:25:11 +0000789unsigned LiveInterval::getSize() const {
790 unsigned Sum = 0;
791 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000792 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000793 return Sum;
794}
795
David Greene29ff37f2009-07-22 20:08:25 +0000796/// ComputeJoinedWeight - Set the weight of a live interval Joined
797/// after Other has been merged into it.
798void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
799 // If either of these intervals was spilled, the weight is the
800 // weight of the non-spilled interval. This can only happen with
801 // iterative coalescers.
802
David Greene92b78bb2009-07-22 22:32:19 +0000803 if (Other.weight != HUGE_VALF) {
804 weight += Other.weight;
805 }
806 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000807 !TargetRegisterInfo::isPhysicalRegister(reg)) {
808 // Remove this assert if you have an iterative coalescer
809 assert(0 && "Joining to spilled interval");
810 weight = Other.weight;
811 }
David Greene29ff37f2009-07-22 20:08:25 +0000812 else {
813 // Otherwise the weight stays the same
814 // Remove this assert if you have an iterative coalescer
815 assert(0 && "Joining from spilled interval");
816 }
817}
818
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000819raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
820 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
821}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000822
Chris Lattnerabf295f2004-07-24 02:52:23 +0000823void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000824 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000825}
826
Chris Lattnerc02497f2009-08-23 03:47:42 +0000827void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000828 if (isStackSlot())
829 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000830 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000831 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000832 else
833 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000834
Chris Lattner38135af2005-05-14 05:34:15 +0000835 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000836
Chris Lattner38135af2005-05-14 05:34:15 +0000837 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000838 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000839 else {
840 OS << " = ";
841 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000842 E = ranges.end(); I != E; ++I) {
843 OS << *I;
844 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
845 }
Chris Lattner38135af2005-05-14 05:34:15 +0000846 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000847
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000848 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000849 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000850 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000851 unsigned vnum = 0;
852 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
853 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000854 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000855 if (vnum) OS << " ";
856 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000857 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000858 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000859 } else {
Lang Hames61945692009-12-09 05:39:12 +0000860 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000861 OS << "?";
862 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000863 OS << vni->def;
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000864 if (vni->hasPHIKill())
865 OS << "-phikill";
866 if (vni->hasRedefByEC())
867 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000868 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000869 }
870 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000871}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000872
873void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000874 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000875}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000876
877
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000878void LiveRange::print(raw_ostream &os) const {
879 os << *this;
880}