blob: 21a9b7d4db6f6f9dee36a36146d487222e90f1fc [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;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000183
Chris Lattnerb26c2152004-07-23 19:38:44 +0000184 // Search for the first interval that we can't merge with.
185 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000186 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000187 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000188 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000189
190 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
191 I->end = std::max(NewEnd, prior(MergeTo)->end);
192
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000193 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000194 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000195
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000196 // If the newly formed range now touches the range after it and if they have
197 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000198 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000199 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000200 I->end = Next->end;
201 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000202 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000203}
204
205
206/// extendIntervalStartTo - This method is used when we want to extend the range
207/// specified by I to start at the specified endpoint. To do this, we should
208/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000209LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000210LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000211 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000212 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000213
214 // Search for the first interval that we can't merge with.
215 Ranges::iterator MergeTo = I;
216 do {
217 if (MergeTo == ranges.begin()) {
218 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000219 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000220 return I;
221 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000222 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000223 --MergeTo;
224 } while (NewStart <= MergeTo->start);
225
226 // If we start in the middle of another interval, just delete a range and
227 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000228 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000229 MergeTo->end = I->end;
230 } else {
231 // Otherwise, extend the interval right after.
232 ++MergeTo;
233 MergeTo->start = NewStart;
234 MergeTo->end = I->end;
235 }
236
237 ranges.erase(next(MergeTo), next(I));
238 return MergeTo;
239}
240
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000241LiveInterval::iterator
242LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000243 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000244 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000245
246 // If the inserted interval starts in the middle or right at the end of
247 // another interval, just extend that interval to contain the range of LR.
248 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000249 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000250 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000251 if (B->start <= Start && B->end >= Start) {
252 extendIntervalEndTo(B, End);
253 return B;
254 }
255 } else {
256 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000257 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000258 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000259 "Cannot overlap two LiveRanges with differing ValID's"
260 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000261 }
262 }
263
264 // Otherwise, if this range ends in the middle of, or right next to, another
265 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000266 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000267 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000268 if (it->start <= End) {
269 it = extendIntervalStartTo(it, Start);
270
271 // If LR is a complete superset of an interval, we may need to grow its
272 // endpoint as well.
273 if (End > it->end)
274 extendIntervalEndTo(it, End);
275 return it;
276 }
277 } else {
278 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000279 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000280 assert(it->start >= End &&
281 "Cannot overlap two LiveRanges with differing ValID's");
282 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000283 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000284
285 // Otherwise, this is just a new range that doesn't interact with anything.
286 // Insert it.
287 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000288}
289
Lang Hames86511252009-09-04 20:41:11 +0000290/// isInOneLiveRange - Return true if the range specified is entirely in
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000291/// a single LiveRange of the live interval.
Lang Hames233a60e2009-11-03 23:52:08 +0000292bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000293 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
294 if (I == ranges.begin())
295 return false;
296 --I;
Lang Hames86511252009-09-04 20:41:11 +0000297 return I->containsRange(Start, End);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000298}
299
Chris Lattnerabf295f2004-07-24 02:52:23 +0000300
301/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000302/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000303void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000304 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000305 // Find the LiveRange containing this span.
306 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
307 assert(I != ranges.begin() && "Range is not in interval!");
308 --I;
Lang Hames86511252009-09-04 20:41:11 +0000309 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000310
311 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000312 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000313 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000314 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000315 if (RemoveDeadValNo) {
316 // Check if val# is dead.
317 bool isDead = true;
318 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
319 if (II != I && II->valno == ValNo) {
320 isDead = false;
321 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000322 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000323 if (isDead) {
324 // Now that ValNo is dead, remove it. If it is the largest value
325 // number, just nuke it (and any other deleted values neighboring it),
326 // otherwise mark it as ~1U so it can be nuked later.
327 if (ValNo->id == getNumValNums()-1) {
328 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000329 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000330 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000331 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000332 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000333 }
334 }
335 }
336
Chris Lattnerabf295f2004-07-24 02:52:23 +0000337 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000338 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000339 I->start = End;
340 return;
341 }
342
343 // Otherwise if the span we are removing is at the end of the LiveRange,
344 // adjust the other way.
345 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000346 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000347 return;
348 }
349
350 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000351 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000352 I->end = Start; // Trim the old interval.
353
354 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000355 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000356}
357
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000358/// removeValNo - Remove all the ranges defined by the specified value#.
359/// Also remove the value# from value# list.
360void LiveInterval::removeValNo(VNInfo *ValNo) {
361 if (empty()) return;
362 Ranges::iterator I = ranges.end();
363 Ranges::iterator E = ranges.begin();
364 do {
365 --I;
366 if (I->valno == ValNo)
367 ranges.erase(I);
368 } while (I != E);
369 // Now that ValNo is dead, remove it. If it is the largest value
370 // number, just nuke it (and any other deleted values neighboring it),
371 // otherwise mark it as ~1U so it can be nuked later.
372 if (ValNo->id == getNumValNums()-1) {
373 do {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000374 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000375 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000376 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000377 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000378 }
379}
Lang Hames86511252009-09-04 20:41:11 +0000380
Chris Lattnerabf295f2004-07-24 02:52:23 +0000381/// getLiveRangeContaining - Return the live range that contains the
382/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000383LiveInterval::const_iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000384LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000385 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000386 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000387 --It;
388 if (It->contains(Idx))
389 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000390 }
391
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000392 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000393}
394
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000395LiveInterval::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000396LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000397 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000398 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000399 --It;
400 if (It->contains(Idx))
401 return It;
402 }
403
404 return end();
405}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000406
Lang Hames86511252009-09-04 20:41:11 +0000407/// findDefinedVNInfo - Find the VNInfo defined by the specified
408/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000409VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000410 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000411 i != e; ++i) {
412 if ((*i)->def == Idx)
413 return *i;
414 }
415
416 return 0;
417}
418
419/// findDefinedVNInfo - Find the VNInfo defined by the specified
420/// register (stack inteval).
421VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
422 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
423 i != e; ++i) {
424 if ((*i)->getReg() == reg)
425 return *i;
426 }
427 return 0;
Evan Cheng3f32d652008-06-04 09:18:41 +0000428}
429
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000430/// join - Join two live intervals (this, and other) together. This applies
431/// mappings to the value numbers in the LHS/RHS intervals as specified. If
432/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000433void LiveInterval::join(LiveInterval &Other,
434 const int *LHSValNoAssignments,
David Greeneaf992f72007-09-06 19:46:46 +0000435 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000436 SmallVector<VNInfo*, 16> &NewVNInfo,
437 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000438 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000439 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000440 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000441 unsigned NumVals = getNumValNums();
442 unsigned NumNewVals = NewVNInfo.size();
443 for (unsigned i = 0; i != NumVals; ++i) {
444 unsigned LHSValID = LHSValNoAssignments[i];
445 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000446 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000447 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000448 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000449
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000450 // If we have to apply a mapping to our base interval assignment, rewrite it
451 // now.
452 if (MustMapCurValNos) {
453 // Map the first live range.
454 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000455 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000456 ++OutIt;
457 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000458 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000459
460 // If this live range has the same value # as its immediate predecessor,
461 // and if they are neighbors, remove one LiveRange. This happens when we
462 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000463 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000464 (OutIt-1)->end = OutIt->end;
465 } else {
466 if (I != OutIt) {
467 OutIt->start = I->start;
468 OutIt->end = I->end;
469 }
470
471 // Didn't merge, on to the next one.
472 ++OutIt;
473 }
474 }
475
476 // If we merge some live ranges, chop off the end.
477 ranges.erase(OutIt, end());
478 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000479
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000480 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000481 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000482 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
483 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
484
485 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000486 // LiveInterval now. Also remove dead val#'s.
487 unsigned NumValNos = 0;
488 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000489 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000490 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000491 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000492 valnos.push_back(VNI);
493 else
494 valnos[NumValNos] = VNI;
495 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000496 }
497 }
Evan Cheng34301352007-09-01 02:03:17 +0000498 if (NumNewVals < NumVals)
499 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000500
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000501 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000502 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000503 unsigned RangeNo = 0;
504 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
505 // Map the valno in the other live range to the current live range.
506 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000507 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000508 InsertPos = addRangeFrom(*I, InsertPos);
509 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000510
David Greene29ff37f2009-07-22 20:08:25 +0000511 ComputeJoinedWeight(Other);
Evan Cheng90f95f82009-06-14 20:22:55 +0000512
513 // Update regalloc hint if currently there isn't one.
514 if (TargetRegisterInfo::isVirtualRegister(reg) &&
515 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
Evan Cheng358dec52009-06-15 08:28:29 +0000516 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
517 if (Hint.first == 0 && Hint.second == 0) {
518 std::pair<unsigned, unsigned> OtherHint =
Evan Cheng90f95f82009-06-14 20:22:55 +0000519 MRI->getRegAllocationHint(Other.reg);
Evan Cheng358dec52009-06-15 08:28:29 +0000520 if (OtherHint.first || OtherHint.second)
Evan Cheng90f95f82009-06-14 20:22:55 +0000521 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
522 }
523 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000524}
525
Chris Lattnerf21f0202006-09-02 05:26:59 +0000526/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
527/// interval as the specified value number. The LiveRanges in RHS are
528/// allowed to overlap with LiveRanges in the current interval, but only if
529/// the overlapping LiveRanges have the specified value number.
530void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000531 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000532 // TODO: Make this more efficient.
533 iterator InsertPos = begin();
534 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000535 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000536 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000537 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000538 InsertPos = addRangeFrom(Tmp, InsertPos);
539 }
540}
541
542
Evan Cheng32dfbea2007-10-12 08:50:34 +0000543/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
544/// in RHS into this live interval as the specified value number.
545/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000546/// current interval, it will replace the value numbers of the overlaped
547/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000548void LiveInterval::MergeValueInAsValue(
549 const LiveInterval &RHS,
550 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000551 SmallVector<VNInfo*, 4> ReplacedValNos;
552 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000553 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000554 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000555 if (I->valno != RHSValNo)
556 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000557 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000558 IP = std::upper_bound(IP, end(), Start);
559 // If the start of this range overlaps with an existing liverange, trim it.
560 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000561 if (IP[-1].valno != LHSValNo) {
562 ReplacedValNos.push_back(IP[-1].valno);
563 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000564 }
565 Start = IP[-1].end;
566 // Trimmed away the whole range?
567 if (Start >= End) continue;
568 }
569 // If the end of this range overlaps with an existing liverange, trim it.
570 if (IP != end() && End > IP->start) {
571 if (IP->valno != LHSValNo) {
572 ReplacedValNos.push_back(IP->valno);
573 IP->valno = LHSValNo; // Update val#.
574 }
575 End = IP->start;
576 // If this trimmed away the whole range, ignore it.
577 if (Start == End) continue;
578 }
579
Evan Cheng32dfbea2007-10-12 08:50:34 +0000580 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000581 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
582 }
583
584
585 SmallSet<VNInfo*, 4> Seen;
586 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
587 VNInfo *V1 = ReplacedValNos[i];
588 if (Seen.insert(V1)) {
589 bool isDead = true;
590 for (const_iterator I = begin(), E = end(); I != E; ++I)
591 if (I->valno == V1) {
592 isDead = false;
593 break;
594 }
595 if (isDead) {
596 // Now that V1 is dead, remove it. If it is the largest value number,
597 // just nuke it (and any other deleted values neighboring it), otherwise
598 // mark it as ~1U so it can be nuked later.
599 if (V1->id == getNumValNums()-1) {
600 do {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000601 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000602 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000603 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000604 V1->setIsUnused(true);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000605 }
606 }
607 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000608 }
609}
610
611
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000612/// MergeInClobberRanges - For any live ranges that are not defined in the
613/// current interval, but are defined in the Clobbers interval, mark them
614/// used with an unknown definition value.
Lang Hames233a60e2009-11-03 23:52:08 +0000615void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
616 const LiveInterval &Clobbers,
Benjamin Kramer991de142010-03-30 20:16:45 +0000617 VNInfo::Allocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000618 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000619
Evan Cheng0adb5272009-04-25 09:25:19 +0000620 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000621 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000622 iterator IP = begin();
623 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000624 // For every val# in the Clobbers interval, create a new "unknown" val#.
625 VNInfo *ClobberValNo = 0;
626 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
627 if (VI != ValNoMaps.end())
628 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000629 else if (UnusedValNo)
630 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000631 else {
Lang Hames86511252009-09-04 20:41:11 +0000632 UnusedValNo = ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000633 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000634 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
635 }
636
Dan Gohman97121ba2009-04-08 00:15:30 +0000637 bool Done = false;
Lang Hames233a60e2009-11-03 23:52:08 +0000638 SlotIndex Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000639 // If a clobber range starts before an existing range and ends after
640 // it, the clobber range will need to be split into multiple ranges.
641 // Loop until the entire clobber range is handled.
642 while (!Done) {
643 Done = true;
644 IP = std::upper_bound(IP, end(), Start);
Lang Hames233a60e2009-11-03 23:52:08 +0000645 SlotIndex SubRangeStart = Start;
646 SlotIndex SubRangeEnd = End;
Dan Gohman97121ba2009-04-08 00:15:30 +0000647
648 // If the start of this range overlaps with an existing liverange, trim it.
649 if (IP != begin() && IP[-1].end > SubRangeStart) {
650 SubRangeStart = IP[-1].end;
651 // Trimmed away the whole range?
652 if (SubRangeStart >= SubRangeEnd) continue;
653 }
654 // If the end of this range overlaps with an existing liverange, trim it.
655 if (IP != end() && SubRangeEnd > IP->start) {
656 // If the clobber live range extends beyond the existing live range,
657 // it'll need at least another live range, so set the flag to keep
658 // iterating.
659 if (SubRangeEnd > IP->end) {
660 Start = IP->end;
661 Done = false;
662 }
663 SubRangeEnd = IP->start;
664 // If this trimmed away the whole range, ignore it.
665 if (SubRangeStart == SubRangeEnd) continue;
666 }
667
668 // Insert the clobber interval.
669 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
670 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000671 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000672 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000673 }
Evan Cheng27e46662009-04-27 17:35:19 +0000674
675 if (UnusedValNo) {
676 // Delete the last unused val#.
677 valnos.pop_back();
Evan Cheng27e46662009-04-27 17:35:19 +0000678 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000679}
680
Lang Hames233a60e2009-11-03 23:52:08 +0000681void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
682 SlotIndex Start,
683 SlotIndex End,
Benjamin Kramer991de142010-03-30 20:16:45 +0000684 VNInfo::Allocator &VNInfoAllocator) {
Evan Chenga2e64352009-03-11 00:03:21 +0000685 // Find a value # to use for the clobber ranges. If there is already a value#
686 // for unknown values, use it.
Lang Hames86511252009-09-04 20:41:11 +0000687 VNInfo *ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000688 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000689
690 iterator IP = begin();
691 IP = std::upper_bound(IP, end(), Start);
692
693 // If the start of this range overlaps with an existing liverange, trim it.
694 if (IP != begin() && IP[-1].end > Start) {
695 Start = IP[-1].end;
696 // Trimmed away the whole range?
697 if (Start >= End) return;
698 }
699 // If the end of this range overlaps with an existing liverange, trim it.
700 if (IP != end() && End > IP->start) {
701 End = IP->start;
702 // If this trimmed away the whole range, ignore it.
703 if (Start == End) return;
704 }
705
706 // Insert the clobber interval.
707 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
708}
709
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000710/// MergeValueNumberInto - This method is called when two value nubmers
711/// are found to be equivalent. This eliminates V1, replacing all
712/// LiveRanges with the V1 value number with the V2 value number. This can
713/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000714VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000715 assert(V1 != V2 && "Identical value#'s are always equivalent!");
716
717 // This code actually merges the (numerically) larger value number into the
718 // smaller value number, which is likely to allow us to compactify the value
719 // space. The only thing we have to be careful of is to preserve the
720 // instruction that defines the result value.
721
722 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000723 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000724 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000725 std::swap(V1, V2);
726 }
727
728 // Merge V1 live ranges into V2.
729 for (iterator I = begin(); I != end(); ) {
730 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000731 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000732
733 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
734 // range, extend it.
735 if (LR != begin()) {
736 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000737 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000738 Prev->end = LR->end;
739
740 // Erase this live-range.
741 ranges.erase(LR);
742 I = Prev+1;
743 LR = Prev;
744 }
745 }
746
747 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
748 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000749 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000750
751 // If we can merge it into later V2 live ranges, do so now. We ignore any
752 // following V1 live ranges, as they will be merged in subsequent iterations
753 // of the loop.
754 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000755 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000756 LR->end = I->end;
757 ranges.erase(I);
758 I = LR+1;
759 }
760 }
761 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000762
763 // Now that V1 is dead, remove it. If it is the largest value number, just
764 // nuke it (and any other deleted values neighboring it), otherwise mark it as
765 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000766 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000767 do {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000768 valnos.pop_back();
Lang Hames857c4e02009-06-17 21:01:20 +0000769 } while (valnos.back()->isUnused());
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000770 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000771 V1->setIsUnused(true);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000772 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000773
774 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000775}
776
Evan Cheng32dfbea2007-10-12 08:50:34 +0000777void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000778 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000779 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000780 ranges.clear();
781 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000782 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000783 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
784
Evan Cheng32dfbea2007-10-12 08:50:34 +0000785 weight = RHS.weight;
786 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
787 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000788 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000789 }
790 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
791 const LiveRange &LR = RHS.ranges[i];
792 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
793 }
794}
795
Evan Chenge52eef82007-04-17 20:25:11 +0000796unsigned LiveInterval::getSize() const {
797 unsigned Sum = 0;
798 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000799 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000800 return Sum;
801}
802
David Greene29ff37f2009-07-22 20:08:25 +0000803/// ComputeJoinedWeight - Set the weight of a live interval Joined
804/// after Other has been merged into it.
805void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
806 // If either of these intervals was spilled, the weight is the
807 // weight of the non-spilled interval. This can only happen with
808 // iterative coalescers.
809
David Greene92b78bb2009-07-22 22:32:19 +0000810 if (Other.weight != HUGE_VALF) {
811 weight += Other.weight;
812 }
813 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000814 !TargetRegisterInfo::isPhysicalRegister(reg)) {
815 // Remove this assert if you have an iterative coalescer
816 assert(0 && "Joining to spilled interval");
817 weight = Other.weight;
818 }
David Greene29ff37f2009-07-22 20:08:25 +0000819 else {
820 // Otherwise the weight stays the same
821 // Remove this assert if you have an iterative coalescer
822 assert(0 && "Joining from spilled interval");
823 }
824}
825
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000826raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
827 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
828}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000829
Chris Lattnerabf295f2004-07-24 02:52:23 +0000830void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000831 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000832}
833
Chris Lattnerc02497f2009-08-23 03:47:42 +0000834void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000835 if (isStackSlot())
836 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000837 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000838 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000839 else
840 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000841
Chris Lattner38135af2005-05-14 05:34:15 +0000842 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000843
Chris Lattner38135af2005-05-14 05:34:15 +0000844 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000845 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000846 else {
847 OS << " = ";
848 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000849 E = ranges.end(); I != E; ++I) {
850 OS << *I;
851 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
852 }
Chris Lattner38135af2005-05-14 05:34:15 +0000853 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000854
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000855 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000856 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000857 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000858 unsigned vnum = 0;
859 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
860 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000861 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000862 if (vnum) OS << " ";
863 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000864 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000865 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000866 } else {
Lang Hames61945692009-12-09 05:39:12 +0000867 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000868 OS << "?";
869 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000870 OS << vni->def;
Evan Chenga8d94f12007-08-07 23:49:57 +0000871 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000872 }
873 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000874}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000875
876void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000877 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000878}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000879
880
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000881void LiveRange::print(raw_ostream &os) const {
882 os << *this;
883}