blob: d60c3b1b63fdf9ffbf5105eb913d55bc6c42a7d5 [file] [log] [blame]
Chris Lattnerfb449b92004-07-23 17:49:16 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveRange and LiveInterval classes. Given some
11// numbering of each the machine instructions an interval [i, j) is said to be a
12// live interval for register v if there is no instruction with number j' > j
13// such that v is live at j' abd there is no instruction with number i' < i such
14// that v is live at i'. In this implementation intervals can have holes,
15// i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
16// individual range is represented as an instance of LiveRange, and the whole
17// interval is represented as an instance of LiveInterval.
18//
19//===----------------------------------------------------------------------===//
20
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000021#include "llvm/CodeGen/LiveInterval.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000022#include "llvm/ADT/STLExtras.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000023#include "llvm/Support/Streams.h"
Chris Lattner38135af2005-05-14 05:34:15 +000024#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000025#include <algorithm>
Chris Lattnerabf295f2004-07-24 02:52:23 +000026#include <map>
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000027#include <ostream>
Chris Lattnerfb449b92004-07-23 17:49:16 +000028using namespace llvm;
29
30// An example for liveAt():
31//
Chris Lattneraa141472004-07-23 18:40:00 +000032// this = [1,4), liveAt(0) will return false. The instruction defining this
33// spans slots [0,3]. The interval belongs to an spilled definition of the
34// variable it represents. This is because slot 1 is used (def slot) and spans
35// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000036//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000037bool LiveInterval::liveAt(unsigned I) const {
38 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000039
Chris Lattnerfb449b92004-07-23 17:49:16 +000040 if (r == ranges.begin())
41 return false;
42
43 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000044 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000045}
46
Chris Lattnerbae74d92004-11-18 03:47:34 +000047// overlaps - Return true if the intersection of the two live intervals is
48// not empty.
49//
Chris Lattnerfb449b92004-07-23 17:49:16 +000050// An example for overlaps():
51//
52// 0: A = ...
53// 4: B = ...
54// 8: C = A + B ;; last use of A
55//
56// The live intervals should look like:
57//
58// A = [3, 11)
59// B = [7, x)
60// C = [11, y)
61//
62// A->overlaps(C) should return false since we want to be able to join
63// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000064//
65bool LiveInterval::overlapsFrom(const LiveInterval& other,
66 const_iterator StartPos) const {
67 const_iterator i = begin();
68 const_iterator ie = end();
69 const_iterator j = StartPos;
70 const_iterator je = other.end();
71
72 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000073 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000074
Chris Lattnerfb449b92004-07-23 17:49:16 +000075 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000076 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000077 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000078 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000079 ++StartPos;
80 if (StartPos != other.end() && StartPos->start <= i->start) {
81 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000082 j = std::upper_bound(j, je, i->start);
83 if (j != other.ranges.begin()) --j;
84 }
Chris Lattneraa141472004-07-23 18:40:00 +000085 } else {
86 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000087 }
88
Chris Lattner9fddc122004-11-18 05:28:21 +000089 if (j == je) return false;
90
91 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000092 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000093 std::swap(i, j);
94 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000095 }
Chris Lattnerfb449b92004-07-23 17:49:16 +000096
97 if (i->end > j->start)
98 return true;
99 ++i;
100 }
101
102 return false;
103}
104
Chris Lattnerb26c2152004-07-23 19:38:44 +0000105/// extendIntervalEndTo - This method is used when we want to extend the range
106/// specified by I to end at the specified endpoint. To do this, we should
107/// merge and eliminate all ranges that this will overlap with. The iterator is
108/// not invalidated.
109void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
110 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000111 unsigned ValId = I->ValId;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000112
Chris Lattnerb26c2152004-07-23 19:38:44 +0000113 // Search for the first interval that we can't merge with.
114 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000115 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
116 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
117 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000118
119 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
120 I->end = std::max(NewEnd, prior(MergeTo)->end);
121
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000122 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000123 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000124
125 // Update kill info.
126 removeKillForValNum(ValId, I->start, I->end-1);
127
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000128 // If the newly formed range now touches the range after it and if they have
129 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000130 Ranges::iterator Next = next(I);
Chris Lattner9e20d352005-10-21 06:41:30 +0000131 if (Next != ranges.end() && Next->start <= I->end && Next->ValId == ValId) {
Chris Lattnercef60102005-10-20 22:50:10 +0000132 I->end = Next->end;
133 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000134 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000135}
136
137
138/// extendIntervalStartTo - This method is used when we want to extend the range
139/// specified by I to start at the specified endpoint. To do this, we should
140/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000141LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000142LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
143 assert(I != ranges.end() && "Not a valid interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000144 unsigned ValId = I->ValId;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000145
146 // Search for the first interval that we can't merge with.
147 Ranges::iterator MergeTo = I;
148 do {
149 if (MergeTo == ranges.begin()) {
150 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000151 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000152 return I;
153 }
Chris Lattnerabf295f2004-07-24 02:52:23 +0000154 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000155 --MergeTo;
156 } while (NewStart <= MergeTo->start);
157
158 // If we start in the middle of another interval, just delete a range and
159 // extend that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000160 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000161 MergeTo->end = I->end;
162 } else {
163 // Otherwise, extend the interval right after.
164 ++MergeTo;
165 MergeTo->start = NewStart;
166 MergeTo->end = I->end;
167 }
168
169 ranges.erase(next(MergeTo), next(I));
170 return MergeTo;
171}
172
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000173LiveInterval::iterator
174LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000175 unsigned Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000176 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000177
178 // If the inserted interval starts in the middle or right at the end of
179 // another interval, just extend that interval to contain the range of LR.
180 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000181 iterator B = prior(it);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000182 if (LR.ValId == B->ValId) {
183 if (B->start <= Start && B->end >= Start) {
184 extendIntervalEndTo(B, End);
185 return B;
186 }
187 } else {
188 // Check to make sure that we are not overlapping two live ranges with
189 // different ValId's.
190 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000191 "Cannot overlap two LiveRanges with differing ValID's"
192 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000193 }
194 }
195
196 // Otherwise, if this range ends in the middle of, or right next to, another
197 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000198 if (it != ranges.end())
199 if (LR.ValId == it->ValId) {
200 if (it->start <= End) {
201 it = extendIntervalStartTo(it, Start);
202
203 // If LR is a complete superset of an interval, we may need to grow its
204 // endpoint as well.
205 if (End > it->end)
206 extendIntervalEndTo(it, End);
207 return it;
208 }
209 } else {
210 // Check to make sure that we are not overlapping two live ranges with
211 // different ValId's.
212 assert(it->start >= End &&
213 "Cannot overlap two LiveRanges with differing ValID's");
214 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000215
216 // Otherwise, this is just a new range that doesn't interact with anything.
217 // Insert it.
218 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000219}
220
Chris Lattnerabf295f2004-07-24 02:52:23 +0000221
222/// removeRange - Remove the specified range from this interval. Note that
223/// the range must already be in this interval in its entirety.
224void LiveInterval::removeRange(unsigned Start, unsigned End) {
225 // Find the LiveRange containing this span.
226 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
227 assert(I != ranges.begin() && "Range is not in interval!");
228 --I;
229 assert(I->contains(Start) && I->contains(End-1) &&
230 "Range is not entirely in interval!");
231
232 // If the span we are removing is at the start of the LiveRange, adjust it.
233 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000234 if (I->end == End) {
Evan Cheng6047dd92007-08-13 07:12:23 +0000235 removeKillForValNum(I->ValId, Start, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000236 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000237 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000238 I->start = End;
239 return;
240 }
241
242 // Otherwise if the span we are removing is at the end of the LiveRange,
243 // adjust the other way.
244 if (I->end == End) {
Evan Cheng6047dd92007-08-13 07:12:23 +0000245 removeKillForValNum(I->ValId, Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000246 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000247 return;
248 }
249
250 // Otherwise, we are splitting the LiveRange into two pieces.
251 unsigned OldEnd = I->end;
252 I->end = Start; // Trim the old interval.
253
254 // Insert the new one.
255 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
256}
257
258/// getLiveRangeContaining - Return the live range that contains the
259/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000260LiveInterval::const_iterator
261LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
262 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000263 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000264 --It;
265 if (It->contains(Idx))
266 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000267 }
268
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000269 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000270}
271
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000272LiveInterval::iterator
273LiveInterval::FindLiveRangeContaining(unsigned Idx) {
274 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000275 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000276 --It;
277 if (It->contains(Idx))
278 return It;
279 }
280
281 return end();
282}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000283
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000284/// join - Join two live intervals (this, and other) together. This applies
285/// mappings to the value numbers in the LHS/RHS intervals as specified. If
286/// the intervals are not joinable, this aborts.
287void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
288 int *RHSValNoAssignments,
Evan Chenga8d94f12007-08-07 23:49:57 +0000289 SmallVector<VNInfo, 16> &NewValueNumberInfo) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000290
Chris Lattnerdeb99712004-07-24 03:41:50 +0000291 // Try to do the least amount of work possible. In particular, if there are
292 // more liverange chunks in the other set than there are in the 'this' set,
293 // swap sets to merge the fewest chunks in possible.
Chris Lattnere7f729b2006-08-26 01:28:16 +0000294 //
295 // Also, if one range is a physreg and one is a vreg, we always merge from the
296 // vreg into the physreg, which leaves the vreg intervals pristine.
Chris Lattnere7f729b2006-08-26 01:28:16 +0000297 if ((Other.ranges.size() > ranges.size() &&
298 MRegisterInfo::isVirtualRegister(reg)) ||
299 MRegisterInfo::isPhysicalRegister(Other.reg)) {
300 swap(Other);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000301 std::swap(LHSValNoAssignments, RHSValNoAssignments);
302 }
303
304 // Determine if any of our live range values are mapped. This is uncommon, so
305 // we want to avoid the interval scan if not.
306 bool MustMapCurValNos = false;
307 for (unsigned i = 0, e = getNumValNums(); i != e; ++i) {
Evan Chenga141cfe2007-08-08 05:56:18 +0000308 if (ValueNumberInfo[i].def == ~1U) continue; // tombstone value #
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000309 if (i != (unsigned)LHSValNoAssignments[i]) {
310 MustMapCurValNos = true;
311 break;
312 }
Chris Lattnerdeb99712004-07-24 03:41:50 +0000313 }
Chris Lattnere7f729b2006-08-26 01:28:16 +0000314
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000315 // If we have to apply a mapping to our base interval assignment, rewrite it
316 // now.
317 if (MustMapCurValNos) {
318 // Map the first live range.
319 iterator OutIt = begin();
320 OutIt->ValId = LHSValNoAssignments[OutIt->ValId];
321 ++OutIt;
322 for (iterator I = OutIt, E = end(); I != E; ++I) {
323 OutIt->ValId = LHSValNoAssignments[I->ValId];
324
325 // If this live range has the same value # as its immediate predecessor,
326 // and if they are neighbors, remove one LiveRange. This happens when we
327 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
328 if (OutIt->ValId == (OutIt-1)->ValId && (OutIt-1)->end == OutIt->start) {
329 (OutIt-1)->end = OutIt->end;
330 } else {
331 if (I != OutIt) {
332 OutIt->start = I->start;
333 OutIt->end = I->end;
334 }
335
336 // Didn't merge, on to the next one.
337 ++OutIt;
338 }
339 }
340
341 // If we merge some live ranges, chop off the end.
342 ranges.erase(OutIt, end());
343 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000344
345 // Update val# info first. Increasing live ranges may invalidate some kills.
346 ValueNumberInfo.clear();
347 ValueNumberInfo.append(NewValueNumberInfo.begin(), NewValueNumberInfo.end());
348
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000349 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000350 iterator InsertPos = begin();
351 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000352 // Map the ValId in the other live range to the current live range.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000353 I->ValId = RHSValNoAssignments[I->ValId];
Chris Lattnerabf295f2004-07-24 02:52:23 +0000354 InsertPos = addRangeFrom(*I, InsertPos);
355 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000356
Chris Lattnerabf295f2004-07-24 02:52:23 +0000357 weight += Other.weight;
Evan Chenge52eef82007-04-17 20:25:11 +0000358 if (Other.preference && !preference)
359 preference = Other.preference;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000360}
361
Chris Lattnerf21f0202006-09-02 05:26:59 +0000362/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
363/// interval as the specified value number. The LiveRanges in RHS are
364/// allowed to overlap with LiveRanges in the current interval, but only if
365/// the overlapping LiveRanges have the specified value number.
366void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
367 unsigned LHSValNo) {
368 // TODO: Make this more efficient.
369 iterator InsertPos = begin();
370 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
371 // Map the ValId in the other live range to the current live range.
372 LiveRange Tmp = *I;
373 Tmp.ValId = LHSValNo;
374 InsertPos = addRangeFrom(Tmp, InsertPos);
375 }
376}
377
378
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000379/// MergeInClobberRanges - For any live ranges that are not defined in the
380/// current interval, but are defined in the Clobbers interval, mark them
381/// used with an unknown definition value.
382void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers) {
383 if (Clobbers.begin() == Clobbers.end()) return;
384
385 // Find a value # to use for the clobber ranges. If there is already a value#
386 // for unknown values, use it.
387 // FIXME: Use a single sentinal number for these!
Chris Lattner91725b72006-08-31 05:54:43 +0000388 unsigned ClobberValNo = getNextValue(~0U, 0);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000389
390 iterator IP = begin();
391 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
392 unsigned Start = I->start, End = I->end;
393 IP = std::upper_bound(IP, end(), Start);
394
395 // If the start of this range overlaps with an existing liverange, trim it.
396 if (IP != begin() && IP[-1].end > Start) {
397 Start = IP[-1].end;
398 // Trimmed away the whole range?
399 if (Start >= End) continue;
400 }
401 // If the end of this range overlaps with an existing liverange, trim it.
402 if (IP != end() && End > IP->start) {
403 End = IP->start;
404 // If this trimmed away the whole range, ignore it.
405 if (Start == End) continue;
406 }
407
408 // Insert the clobber interval.
409 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
410 }
411}
412
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000413/// MergeValueNumberInto - This method is called when two value nubmers
414/// are found to be equivalent. This eliminates V1, replacing all
415/// LiveRanges with the V1 value number with the V2 value number. This can
416/// cause merging of V1/V2 values numbers and compaction of the value space.
417void LiveInterval::MergeValueNumberInto(unsigned V1, unsigned V2) {
418 assert(V1 != V2 && "Identical value#'s are always equivalent!");
419
420 // This code actually merges the (numerically) larger value number into the
421 // smaller value number, which is likely to allow us to compactify the value
422 // space. The only thing we have to be careful of is to preserve the
423 // instruction that defines the result value.
424
425 // Make sure V2 is smaller than V1.
426 if (V1 < V2) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000427 copyValNumInfo(V1, V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000428 std::swap(V1, V2);
429 }
430
431 // Merge V1 live ranges into V2.
432 for (iterator I = begin(); I != end(); ) {
433 iterator LR = I++;
434 if (LR->ValId != V1) continue; // Not a V1 LiveRange.
435
436 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
437 // range, extend it.
438 if (LR != begin()) {
439 iterator Prev = LR-1;
440 if (Prev->ValId == V2 && Prev->end == LR->start) {
441 Prev->end = LR->end;
442
443 // Erase this live-range.
444 ranges.erase(LR);
445 I = Prev+1;
446 LR = Prev;
447 }
448 }
449
450 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
451 // Ensure that it is a V2 live-range.
452 LR->ValId = V2;
453
454 // If we can merge it into later V2 live ranges, do so now. We ignore any
455 // following V1 live ranges, as they will be merged in subsequent iterations
456 // of the loop.
457 if (I != end()) {
458 if (I->start == LR->end && I->ValId == V2) {
459 LR->end = I->end;
460 ranges.erase(I);
461 I = LR+1;
462 }
463 }
464 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000465
466 // Now that V1 is dead, remove it. If it is the largest value number, just
467 // nuke it (and any other deleted values neighboring it), otherwise mark it as
468 // ~1U so it can be nuked later.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000469 if (V1 == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000470 do {
Chris Lattner91725b72006-08-31 05:54:43 +0000471 ValueNumberInfo.pop_back();
Evan Chenga8d94f12007-08-07 23:49:57 +0000472 } while (ValueNumberInfo.back().def == ~1U);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000473 } else {
Evan Chenga8d94f12007-08-07 23:49:57 +0000474 ValueNumberInfo[V1].def = ~1U;
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000475 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000476}
477
Evan Chenge52eef82007-04-17 20:25:11 +0000478unsigned LiveInterval::getSize() const {
479 unsigned Sum = 0;
480 for (const_iterator I = begin(), E = end(); I != E; ++I)
481 Sum += I->end - I->start;
482 return Sum;
483}
484
Chris Lattnerfb449b92004-07-23 17:49:16 +0000485std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000486 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000487}
488
Chris Lattnerabf295f2004-07-24 02:52:23 +0000489void LiveRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000490 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000491}
492
Bill Wendling5c7e3262006-12-17 05:15:13 +0000493void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const {
Chris Lattner38135af2005-05-14 05:34:15 +0000494 if (MRI && MRegisterInfo::isPhysicalRegister(reg))
495 OS << MRI->getName(reg);
496 else
497 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000498
Chris Lattner38135af2005-05-14 05:34:15 +0000499 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000500
Chris Lattner38135af2005-05-14 05:34:15 +0000501 if (empty())
502 OS << "EMPTY";
503 else {
504 OS << " = ";
505 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
506 E = ranges.end(); I != E; ++I)
507 OS << *I;
508 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000509
510 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000511 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000512 OS << " ";
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000513 for (unsigned i = 0; i != getNumValNums(); ++i) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000514 if (i) OS << " ";
515 OS << i << "@";
Evan Cheng4f8ff162007-08-11 00:59:19 +0000516 if (ValueNumberInfo[i].def == ~1U) {
Evan Cheng8df78602007-08-08 03:00:28 +0000517 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000518 } else {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000519 if (ValueNumberInfo[i].def == ~0U)
520 OS << "?";
521 else
522 OS << ValueNumberInfo[i].def;
523 unsigned e = ValueNumberInfo[i].kills.size();
524 if (e) {
525 OS << "-(";
526 for (unsigned j = 0; j != e; ++j) {
527 OS << ValueNumberInfo[i].kills[j];
528 if (j != e-1)
529 OS << " ";
530 }
531 OS << ")";
532 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000533 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000534 }
535 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000536}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000537
538void LiveInterval::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000539 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000540}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000541
542
Jeff Cohen4b607742006-12-16 02:15:42 +0000543void LiveRange::print(std::ostream &os) const {
544 os << *this;
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000545}