blob: 53ffbe3fca457fa6b84ea7dc2744b56160446500 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
21#include "llvm/CodeGen/LiveInterval.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Support/Streams.h"
24#include "llvm/Target/MRegisterInfo.h"
25#include <algorithm>
26#include <map>
27#include <ostream>
28using namespace llvm;
29
30// An example for liveAt():
31//
32// 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).
36//
37bool LiveInterval::liveAt(unsigned I) const {
38 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
39
40 if (r == ranges.begin())
41 return false;
42
43 --r;
44 return r->contains(I);
45}
46
47// overlaps - Return true if the intersection of the two live intervals is
48// not empty.
49//
50// 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.
64//
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()) &&
73 StartPos != other.end() && "Bogus start position hint!");
74
75 if (i->start < j->start) {
76 i = std::upper_bound(i, ie, j->start);
77 if (i != ranges.begin()) --i;
78 } else if (j->start < i->start) {
79 ++StartPos;
80 if (StartPos != other.end() && StartPos->start <= i->start) {
81 assert(StartPos < other.end() && i < end());
82 j = std::upper_bound(j, je, i->start);
83 if (j != other.ranges.begin()) --j;
84 }
85 } else {
86 return true;
87 }
88
89 if (j == je) return false;
90
91 while (i != ie) {
92 if (i->start > j->start) {
93 std::swap(i, j);
94 std::swap(ie, je);
95 }
96
97 if (i->end > j->start)
98 return true;
99 ++i;
100 }
101
102 return false;
103}
104
105/// 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!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000111 VNInfo *ValNo = I->valno;
Evan Cheng2d88a7b2007-08-14 01:56:58 +0000112 unsigned OldEnd = I->end;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
114 // Search for the first interval that we can't merge with.
115 Ranges::iterator MergeTo = next(I);
116 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000117 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 }
119
120 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
121 I->end = std::max(NewEnd, prior(MergeTo)->end);
122
123 // Erase any dead ranges.
124 ranges.erase(next(I), MergeTo);
Evan Cheng816a7f32007-08-11 00:59:19 +0000125
126 // Update kill info.
Evan Cheng983b81d2007-08-29 20:45:00 +0000127 removeKills(*ValNo, OldEnd, I->end-1);
Evan Cheng816a7f32007-08-11 00:59:19 +0000128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 // If the newly formed range now touches the range after it and if they have
130 // the same value number, merge the two ranges into one range.
131 Ranges::iterator Next = next(I);
Evan Cheng983b81d2007-08-29 20:45:00 +0000132 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 I->end = Next->end;
134 ranges.erase(Next);
135 }
136}
137
138
139/// extendIntervalStartTo - This method is used when we want to extend the range
140/// specified by I to start at the specified endpoint. To do this, we should
141/// merge and eliminate all ranges that this will overlap with.
142LiveInterval::Ranges::iterator
143LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
144 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000145 VNInfo *ValNo = I->valno;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 // Search for the first interval that we can't merge with.
148 Ranges::iterator MergeTo = I;
149 do {
150 if (MergeTo == ranges.begin()) {
151 I->start = NewStart;
152 ranges.erase(MergeTo, I);
153 return I;
154 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000155 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 --MergeTo;
157 } while (NewStart <= MergeTo->start);
158
159 // If we start in the middle of another interval, just delete a range and
160 // extend that interval.
Evan Cheng983b81d2007-08-29 20:45:00 +0000161 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 MergeTo->end = I->end;
163 } else {
164 // Otherwise, extend the interval right after.
165 ++MergeTo;
166 MergeTo->start = NewStart;
167 MergeTo->end = I->end;
168 }
169
170 ranges.erase(next(MergeTo), next(I));
171 return MergeTo;
172}
173
174LiveInterval::iterator
175LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
176 unsigned Start = LR.start, End = LR.end;
177 iterator it = std::upper_bound(From, ranges.end(), Start);
178
179 // If the inserted interval starts in the middle or right at the end of
180 // another interval, just extend that interval to contain the range of LR.
181 if (it != ranges.begin()) {
182 iterator B = prior(it);
Evan Cheng983b81d2007-08-29 20:45:00 +0000183 if (LR.valno == B->valno) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 if (B->start <= Start && B->end >= Start) {
185 extendIntervalEndTo(B, End);
186 return B;
187 }
188 } else {
189 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng983b81d2007-08-29 20:45:00 +0000190 // different valno's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 assert(B->end <= Start &&
192 "Cannot overlap two LiveRanges with differing ValID's"
193 " (did you def the same reg twice in a MachineInstr?)");
194 }
195 }
196
197 // Otherwise, if this range ends in the middle of, or right next to, another
198 // interval, merge it into that interval.
199 if (it != ranges.end())
Evan Cheng983b81d2007-08-29 20:45:00 +0000200 if (LR.valno == it->valno) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 if (it->start <= End) {
202 it = extendIntervalStartTo(it, Start);
203
204 // If LR is a complete superset of an interval, we may need to grow its
205 // endpoint as well.
206 if (End > it->end)
207 extendIntervalEndTo(it, End);
208 return it;
209 }
210 } else {
211 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng983b81d2007-08-29 20:45:00 +0000212 // different valno's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 assert(it->start >= End &&
214 "Cannot overlap two LiveRanges with differing ValID's");
215 }
216
217 // Otherwise, this is just a new range that doesn't interact with anything.
218 // Insert it.
219 return ranges.insert(it, LR);
220}
221
222
223/// removeRange - Remove the specified range from this interval. Note that
224/// the range must already be in this interval in its entirety.
225void LiveInterval::removeRange(unsigned Start, unsigned End) {
226 // Find the LiveRange containing this span.
227 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
228 assert(I != ranges.begin() && "Range is not in interval!");
229 --I;
230 assert(I->contains(Start) && I->contains(End-1) &&
231 "Range is not entirely in interval!");
232
233 // If the span we are removing is at the start of the LiveRange, adjust it.
234 if (I->start == Start) {
Evan Cheng816a7f32007-08-11 00:59:19 +0000235 if (I->end == End) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000236 removeKills(*I->valno, Start, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng816a7f32007-08-11 00:59:19 +0000238 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 I->start = End;
240 return;
241 }
242
243 // Otherwise if the span we are removing is at the end of the LiveRange,
244 // adjust the other way.
245 if (I->end == End) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000246 removeKills(*I->valno, Start, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 I->end = Start;
248 return;
249 }
250
251 // Otherwise, we are splitting the LiveRange into two pieces.
252 unsigned OldEnd = I->end;
253 I->end = Start; // Trim the old interval.
254
255 // Insert the new one.
Evan Cheng983b81d2007-08-29 20:45:00 +0000256 ranges.insert(next(I), LiveRange(End, OldEnd, I->valno));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257}
258
259/// getLiveRangeContaining - Return the live range that contains the
260/// specified index, or null if there is none.
261LiveInterval::const_iterator
262LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
263 const_iterator It = std::upper_bound(begin(), end(), Idx);
264 if (It != ranges.begin()) {
265 --It;
266 if (It->contains(Idx))
267 return It;
268 }
269
270 return end();
271}
272
273LiveInterval::iterator
274LiveInterval::FindLiveRangeContaining(unsigned Idx) {
275 iterator It = std::upper_bound(begin(), end(), Idx);
276 if (It != begin()) {
277 --It;
278 if (It->contains(Idx))
279 return It;
280 }
281
282 return end();
283}
284
285/// join - Join two live intervals (this, and other) together. This applies
286/// mappings to the value numbers in the LHS/RHS intervals as specified. If
287/// the intervals are not joinable, this aborts.
288void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
289 int *RHSValNoAssignments,
Evan Cheng983b81d2007-08-29 20:45:00 +0000290 SmallVector<VNInfo*, 16> &NewVNInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng8b7533e2007-09-01 02:03:17 +0000292 // we want to avoid the interval scan if not.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 bool MustMapCurValNos = false;
Evan Cheng8b7533e2007-09-01 02:03:17 +0000294 unsigned NumVals = getNumValNums();
295 unsigned NumNewVals = NewVNInfo.size();
296 for (unsigned i = 0; i != NumVals; ++i) {
297 unsigned LHSValID = LHSValNoAssignments[i];
298 if (i != LHSValID ||
299 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID]->parent != this))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 MustMapCurValNos = true;
Evan Cheng8b7533e2007-09-01 02:03:17 +0000301
302 // There might be some dead val#, create VNInfo for them.
303 if (i < NumNewVals) {
304 VNInfo *VNI = NewVNInfo[i];
305 if (!VNI) {
306 VNI = new VNInfo(this, i, ~1U, 0);
307 NewVNInfo[i] = VNI;
308 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 }
310 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 // If we have to apply a mapping to our base interval assignment, rewrite it
313 // now.
314 if (MustMapCurValNos) {
315 // Map the first live range.
316 iterator OutIt = begin();
Evan Cheng983b81d2007-08-29 20:45:00 +0000317 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 ++OutIt;
319 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000320 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
322 // If this live range has the same value # as its immediate predecessor,
323 // and if they are neighbors, remove one LiveRange. This happens when we
324 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng983b81d2007-08-29 20:45:00 +0000325 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 (OutIt-1)->end = OutIt->end;
327 } else {
328 if (I != OutIt) {
329 OutIt->start = I->start;
330 OutIt->end = I->end;
331 }
332
333 // Didn't merge, on to the next one.
334 ++OutIt;
335 }
336 }
337
338 // If we merge some live ranges, chop off the end.
339 ranges.erase(OutIt, end());
340 }
Evan Cheng816a7f32007-08-11 00:59:19 +0000341
Evan Cheng983b81d2007-08-29 20:45:00 +0000342 // Remember assignements because val# ids are changing.
Evan Cheng8b7533e2007-09-01 02:03:17 +0000343 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng983b81d2007-08-29 20:45:00 +0000344 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
345 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
346
347 // Update val# info. Renumber them and make sure they all belong to this
348 // LiveInterval now.
Evan Cheng8b7533e2007-09-01 02:03:17 +0000349 for (unsigned i = 0; i != NumVals; ++i) {
350 if (i == NumNewVals)
351 break;
Evan Cheng983b81d2007-08-29 20:45:00 +0000352 VNInfo *VNI = NewVNInfo[i];
Evan Cheng8b7533e2007-09-01 02:03:17 +0000353 if (VNI->parent != this || VNI->id != i) {
354 VNI->parent = this;
355 VNI->id = i; // Renumber val#.
356 valnos[i] = VNI;
357 }
358 }
359 for (unsigned i = NumVals; i < NumNewVals; ++i) {
360 VNInfo *VNI = NewVNInfo[i];
361 if (!VNI)
362 VNI = new VNInfo(this, i, ~1U, 0);
363 else {
364 VNI->parent = this;
365 VNI->id = i; // Renumber val#.
366 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000367 valnos.push_back(VNI);
Evan Cheng983b81d2007-08-29 20:45:00 +0000368 }
Evan Cheng8b7533e2007-09-01 02:03:17 +0000369 if (NumNewVals < NumVals)
370 valnos.resize(NumNewVals); // shrinkify
Evan Cheng816a7f32007-08-11 00:59:19 +0000371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 // Okay, now insert the RHS live ranges into the LHS.
373 iterator InsertPos = begin();
Evan Cheng983b81d2007-08-29 20:45:00 +0000374 unsigned RangeNo = 0;
375 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
376 // Map the valno in the other live range to the current live range.
377 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 InsertPos = addRangeFrom(*I, InsertPos);
379 }
380
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 weight += Other.weight;
382 if (Other.preference && !preference)
383 preference = Other.preference;
384}
385
386/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
387/// interval as the specified value number. The LiveRanges in RHS are
388/// allowed to overlap with LiveRanges in the current interval, but only if
389/// the overlapping LiveRanges have the specified value number.
390void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng983b81d2007-08-29 20:45:00 +0000391 VNInfo *LHSValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 // TODO: Make this more efficient.
393 iterator InsertPos = begin();
394 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000395 // Map the valno in the other live range to the current live range.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 LiveRange Tmp = *I;
Evan Cheng983b81d2007-08-29 20:45:00 +0000397 Tmp.valno = LHSValNo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 InsertPos = addRangeFrom(Tmp, InsertPos);
399 }
400}
401
402
403/// MergeInClobberRanges - For any live ranges that are not defined in the
404/// current interval, but are defined in the Clobbers interval, mark them
405/// used with an unknown definition value.
406void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers) {
407 if (Clobbers.begin() == Clobbers.end()) return;
408
409 // Find a value # to use for the clobber ranges. If there is already a value#
410 // for unknown values, use it.
411 // FIXME: Use a single sentinal number for these!
Evan Cheng983b81d2007-08-29 20:45:00 +0000412 VNInfo *ClobberValNo = getNextValue(~0U, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
414 iterator IP = begin();
415 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
416 unsigned Start = I->start, End = I->end;
417 IP = std::upper_bound(IP, end(), Start);
418
419 // If the start of this range overlaps with an existing liverange, trim it.
420 if (IP != begin() && IP[-1].end > Start) {
421 Start = IP[-1].end;
422 // Trimmed away the whole range?
423 if (Start >= End) continue;
424 }
425 // If the end of this range overlaps with an existing liverange, trim it.
426 if (IP != end() && End > IP->start) {
427 End = IP->start;
428 // If this trimmed away the whole range, ignore it.
429 if (Start == End) continue;
430 }
431
432 // Insert the clobber interval.
433 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
434 }
435}
436
437/// MergeValueNumberInto - This method is called when two value nubmers
438/// are found to be equivalent. This eliminates V1, replacing all
439/// LiveRanges with the V1 value number with the V2 value number. This can
440/// cause merging of V1/V2 values numbers and compaction of the value space.
Evan Cheng983b81d2007-08-29 20:45:00 +0000441void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 assert(V1 != V2 && "Identical value#'s are always equivalent!");
443
444 // This code actually merges the (numerically) larger value number into the
445 // smaller value number, which is likely to allow us to compactify the value
446 // space. The only thing we have to be careful of is to preserve the
447 // instruction that defines the result value.
448
449 // Make sure V2 is smaller than V1.
Evan Cheng983b81d2007-08-29 20:45:00 +0000450 if (V1->id < V2->id) {
451 copyValNumInfo(*V1, *V2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 std::swap(V1, V2);
453 }
454
455 // Merge V1 live ranges into V2.
456 for (iterator I = begin(); I != end(); ) {
457 iterator LR = I++;
Evan Cheng983b81d2007-08-29 20:45:00 +0000458 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459
460 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
461 // range, extend it.
462 if (LR != begin()) {
463 iterator Prev = LR-1;
Evan Cheng983b81d2007-08-29 20:45:00 +0000464 if (Prev->valno == V2 && Prev->end == LR->start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 Prev->end = LR->end;
466
467 // Erase this live-range.
468 ranges.erase(LR);
469 I = Prev+1;
470 LR = Prev;
471 }
472 }
473
474 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
475 // Ensure that it is a V2 live-range.
Evan Cheng983b81d2007-08-29 20:45:00 +0000476 LR->valno = V2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
478 // If we can merge it into later V2 live ranges, do so now. We ignore any
479 // following V1 live ranges, as they will be merged in subsequent iterations
480 // of the loop.
481 if (I != end()) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000482 if (I->start == LR->end && I->valno == V2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 LR->end = I->end;
484 ranges.erase(I);
485 I = LR+1;
486 }
487 }
488 }
489
490 // Now that V1 is dead, remove it. If it is the largest value number, just
491 // nuke it (and any other deleted values neighboring it), otherwise mark it as
492 // ~1U so it can be nuked later.
Evan Cheng983b81d2007-08-29 20:45:00 +0000493 if (V1->id == getNumValNums()-1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 do {
Evan Cheng983b81d2007-08-29 20:45:00 +0000495 VNInfo *VNI = valnos.back();
496 valnos.pop_back();
497 delete VNI;
Evan Cheng983b81d2007-08-29 20:45:00 +0000498 } while (valnos.back()->def == ~1U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 } else {
Evan Cheng983b81d2007-08-29 20:45:00 +0000500 V1->def = ~1U;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 }
502}
503
504unsigned LiveInterval::getSize() const {
505 unsigned Sum = 0;
506 for (const_iterator I = begin(), E = end(); I != E; ++I)
507 Sum += I->end - I->start;
508 return Sum;
509}
510
511std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000512 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513}
514
515void LiveRange::dump() const {
516 cerr << *this << "\n";
517}
518
519void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const {
520 if (MRI && MRegisterInfo::isPhysicalRegister(reg))
521 OS << MRI->getName(reg);
522 else
523 OS << "%reg" << reg;
524
525 OS << ',' << weight;
526
527 if (empty())
528 OS << "EMPTY";
529 else {
530 OS << " = ";
531 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
532 E = ranges.end(); I != E; ++I)
533 OS << *I;
534 }
535
536 // Print value number info.
537 if (getNumValNums()) {
538 OS << " ";
Evan Chengba990522007-08-28 08:28:51 +0000539 unsigned vnum = 0;
540 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
541 ++i, ++vnum) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000542 const VNInfo *vni = *i;
Evan Chengba990522007-08-28 08:28:51 +0000543 if (vnum) OS << " ";
544 OS << vnum << "@";
Evan Cheng983b81d2007-08-29 20:45:00 +0000545 if (vni->def == ~1U) {
Evan Cheng58c2b762007-08-08 03:00:28 +0000546 OS << "x";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 } else {
Evan Cheng983b81d2007-08-29 20:45:00 +0000548 if (vni->def == ~0U)
Evan Cheng816a7f32007-08-11 00:59:19 +0000549 OS << "?";
550 else
Evan Cheng983b81d2007-08-29 20:45:00 +0000551 OS << vni->def;
552 unsigned ee = vni->kills.size();
Evan Chengba990522007-08-28 08:28:51 +0000553 if (ee) {
Evan Cheng816a7f32007-08-11 00:59:19 +0000554 OS << "-(";
Evan Chengba990522007-08-28 08:28:51 +0000555 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000556 OS << vni->kills[j];
Evan Chengba990522007-08-28 08:28:51 +0000557 if (j != ee-1)
Evan Cheng816a7f32007-08-11 00:59:19 +0000558 OS << " ";
559 }
560 OS << ")";
561 }
Evan Cheng4151fde2007-08-07 23:49:57 +0000562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 }
564 }
565}
566
567void LiveInterval::dump() const {
568 cerr << *this << "\n";
569}
570
571
572void LiveRange::print(std::ostream &os) const {
573 os << *this;
574}