blob: 5b855aa19383d499be09d71d0599ed5c2e9947b1 [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"
Evan Cheng303fed82007-10-17 02:13:29 +000022#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/Streams.h"
25#include "llvm/Target/MRegisterInfo.h"
26#include <algorithm>
27#include <map>
28#include <ostream>
29using namespace llvm;
30
31// An example for liveAt():
32//
33// this = [1,4), liveAt(0) will return false. The instruction defining this
34// spans slots [0,3]. The interval belongs to an spilled definition of the
35// variable it represents. This is because slot 1 is used (def slot) and spans
36// up to slot 3 (store slot).
37//
38bool LiveInterval::liveAt(unsigned I) const {
39 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
40
41 if (r == ranges.begin())
42 return false;
43
44 --r;
45 return r->contains(I);
46}
47
48// overlaps - Return true if the intersection of the two live intervals is
49// not empty.
50//
51// An example for overlaps():
52//
53// 0: A = ...
54// 4: B = ...
55// 8: C = A + B ;; last use of A
56//
57// The live intervals should look like:
58//
59// A = [3, 11)
60// B = [7, x)
61// C = [11, y)
62//
63// A->overlaps(C) should return false since we want to be able to join
64// A and C.
65//
66bool LiveInterval::overlapsFrom(const LiveInterval& other,
67 const_iterator StartPos) const {
68 const_iterator i = begin();
69 const_iterator ie = end();
70 const_iterator j = StartPos;
71 const_iterator je = other.end();
72
73 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
74 StartPos != other.end() && "Bogus start position hint!");
75
76 if (i->start < j->start) {
77 i = std::upper_bound(i, ie, j->start);
78 if (i != ranges.begin()) --i;
79 } else if (j->start < i->start) {
80 ++StartPos;
81 if (StartPos != other.end() && StartPos->start <= i->start) {
82 assert(StartPos < other.end() && i < end());
83 j = std::upper_bound(j, je, i->start);
84 if (j != other.ranges.begin()) --j;
85 }
86 } else {
87 return true;
88 }
89
90 if (j == je) return false;
91
92 while (i != ie) {
93 if (i->start > j->start) {
94 std::swap(i, j);
95 std::swap(ie, je);
96 }
97
98 if (i->end > j->start)
99 return true;
100 ++i;
101 }
102
103 return false;
104}
105
106/// extendIntervalEndTo - This method is used when we want to extend the range
107/// specified by I to end at the specified endpoint. To do this, we should
108/// merge and eliminate all ranges that this will overlap with. The iterator is
109/// not invalidated.
110void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
111 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000112 VNInfo *ValNo = I->valno;
Evan Cheng2d88a7b2007-08-14 01:56:58 +0000113 unsigned OldEnd = I->end;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 // Search for the first interval that we can't merge with.
116 Ranges::iterator MergeTo = next(I);
117 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000118 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 }
120
121 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
122 I->end = std::max(NewEnd, prior(MergeTo)->end);
123
124 // Erase any dead ranges.
125 ranges.erase(next(I), MergeTo);
Evan Cheng816a7f32007-08-11 00:59:19 +0000126
127 // Update kill info.
Evan Cheng319802c2007-09-05 21:46:51 +0000128 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng816a7f32007-08-11 00:59:19 +0000129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 // If the newly formed range now touches the range after it and if they have
131 // the same value number, merge the two ranges into one range.
132 Ranges::iterator Next = next(I);
Evan Cheng983b81d2007-08-29 20:45:00 +0000133 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 I->end = Next->end;
135 ranges.erase(Next);
136 }
137}
138
139
140/// extendIntervalStartTo - This method is used when we want to extend the range
141/// specified by I to start at the specified endpoint. To do this, we should
142/// merge and eliminate all ranges that this will overlap with.
143LiveInterval::Ranges::iterator
144LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
145 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000146 VNInfo *ValNo = I->valno;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
148 // Search for the first interval that we can't merge with.
149 Ranges::iterator MergeTo = I;
150 do {
151 if (MergeTo == ranges.begin()) {
152 I->start = NewStart;
153 ranges.erase(MergeTo, I);
154 return I;
155 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000156 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 --MergeTo;
158 } while (NewStart <= MergeTo->start);
159
160 // If we start in the middle of another interval, just delete a range and
161 // extend that interval.
Evan Cheng983b81d2007-08-29 20:45:00 +0000162 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 MergeTo->end = I->end;
164 } else {
165 // Otherwise, extend the interval right after.
166 ++MergeTo;
167 MergeTo->start = NewStart;
168 MergeTo->end = I->end;
169 }
170
171 ranges.erase(next(MergeTo), next(I));
172 return MergeTo;
173}
174
175LiveInterval::iterator
176LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
177 unsigned Start = LR.start, End = LR.end;
178 iterator it = std::upper_bound(From, ranges.end(), Start);
179
180 // If the inserted interval starts in the middle or right at the end of
181 // another interval, just extend that interval to contain the range of LR.
182 if (it != ranges.begin()) {
183 iterator B = prior(it);
Evan Cheng983b81d2007-08-29 20:45:00 +0000184 if (LR.valno == B->valno) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 if (B->start <= Start && B->end >= Start) {
186 extendIntervalEndTo(B, End);
187 return B;
188 }
189 } else {
190 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng983b81d2007-08-29 20:45:00 +0000191 // different valno's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 assert(B->end <= Start &&
193 "Cannot overlap two LiveRanges with differing ValID's"
194 " (did you def the same reg twice in a MachineInstr?)");
195 }
196 }
197
198 // Otherwise, if this range ends in the middle of, or right next to, another
199 // interval, merge it into that interval.
200 if (it != ranges.end())
Evan Cheng983b81d2007-08-29 20:45:00 +0000201 if (LR.valno == it->valno) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 if (it->start <= End) {
203 it = extendIntervalStartTo(it, Start);
204
205 // If LR is a complete superset of an interval, we may need to grow its
206 // endpoint as well.
207 if (End > it->end)
208 extendIntervalEndTo(it, End);
209 return it;
210 }
211 } else {
212 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng983b81d2007-08-29 20:45:00 +0000213 // different valno's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 assert(it->start >= End &&
215 "Cannot overlap two LiveRanges with differing ValID's");
216 }
217
218 // Otherwise, this is just a new range that doesn't interact with anything.
219 // Insert it.
220 return ranges.insert(it, LR);
221}
222
223
224/// removeRange - Remove the specified range from this interval. Note that
225/// the range must already be in this interval in its entirety.
226void LiveInterval::removeRange(unsigned Start, unsigned End) {
227 // Find the LiveRange containing this span.
228 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
229 assert(I != ranges.begin() && "Range is not in interval!");
230 --I;
231 assert(I->contains(Start) && I->contains(End-1) &&
232 "Range is not entirely in interval!");
233
234 // If the span we are removing is at the start of the LiveRange, adjust it.
235 if (I->start == Start) {
Evan Cheng816a7f32007-08-11 00:59:19 +0000236 if (I->end == End) {
Evan Cheng319802c2007-09-05 21:46:51 +0000237 removeKills(I->valno, Start, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng816a7f32007-08-11 00:59:19 +0000239 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 I->start = End;
241 return;
242 }
243
244 // Otherwise if the span we are removing is at the end of the LiveRange,
245 // adjust the other way.
246 if (I->end == End) {
Evan Cheng319802c2007-09-05 21:46:51 +0000247 removeKills(I->valno, Start, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 I->end = Start;
249 return;
250 }
251
252 // Otherwise, we are splitting the LiveRange into two pieces.
253 unsigned OldEnd = I->end;
254 I->end = Start; // Trim the old interval.
255
256 // Insert the new one.
Evan Cheng983b81d2007-08-29 20:45:00 +0000257 ranges.insert(next(I), LiveRange(End, OldEnd, I->valno));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258}
259
260/// getLiveRangeContaining - Return the live range that contains the
261/// specified index, or null if there is none.
262LiveInterval::const_iterator
263LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
264 const_iterator It = std::upper_bound(begin(), end(), Idx);
265 if (It != ranges.begin()) {
266 --It;
267 if (It->contains(Idx))
268 return It;
269 }
270
271 return end();
272}
273
274LiveInterval::iterator
275LiveInterval::FindLiveRangeContaining(unsigned Idx) {
276 iterator It = std::upper_bound(begin(), end(), Idx);
277 if (It != begin()) {
278 --It;
279 if (It->contains(Idx))
280 return It;
281 }
282
283 return end();
284}
285
286/// join - Join two live intervals (this, and other) together. This applies
287/// mappings to the value numbers in the LHS/RHS intervals as specified. If
288/// the intervals are not joinable, this aborts.
David Greenee97f0772007-09-06 19:46:46 +0000289void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
290 const int *RHSValNoAssignments,
Evan Cheng983b81d2007-08-29 20:45:00 +0000291 SmallVector<VNInfo*, 16> &NewVNInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng8b7533e2007-09-01 02:03:17 +0000293 // we want to avoid the interval scan if not.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 bool MustMapCurValNos = false;
Evan Cheng8b7533e2007-09-01 02:03:17 +0000295 unsigned NumVals = getNumValNums();
296 unsigned NumNewVals = NewVNInfo.size();
297 for (unsigned i = 0; i != NumVals; ++i) {
298 unsigned LHSValID = LHSValNoAssignments[i];
299 if (i != LHSValID ||
Evan Cheng319802c2007-09-05 21:46:51 +0000300 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 MustMapCurValNos = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 // If we have to apply a mapping to our base interval assignment, rewrite it
305 // now.
306 if (MustMapCurValNos) {
307 // Map the first live range.
308 iterator OutIt = begin();
Evan Cheng983b81d2007-08-29 20:45:00 +0000309 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 ++OutIt;
311 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000312 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313
314 // If this live range has the same value # as its immediate predecessor,
315 // and if they are neighbors, remove one LiveRange. This happens when we
316 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng983b81d2007-08-29 20:45:00 +0000317 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 (OutIt-1)->end = OutIt->end;
319 } else {
320 if (I != OutIt) {
321 OutIt->start = I->start;
322 OutIt->end = I->end;
323 }
324
325 // Didn't merge, on to the next one.
326 ++OutIt;
327 }
328 }
329
330 // If we merge some live ranges, chop off the end.
331 ranges.erase(OutIt, end());
332 }
Evan Cheng816a7f32007-08-11 00:59:19 +0000333
Evan Cheng983b81d2007-08-29 20:45:00 +0000334 // Remember assignements because val# ids are changing.
Evan Cheng8b7533e2007-09-01 02:03:17 +0000335 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng983b81d2007-08-29 20:45:00 +0000336 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
337 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
338
339 // Update val# info. Renumber them and make sure they all belong to this
Evan Cheng319802c2007-09-05 21:46:51 +0000340 // LiveInterval now. Also remove dead val#'s.
341 unsigned NumValNos = 0;
342 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000343 VNInfo *VNI = NewVNInfo[i];
Evan Cheng319802c2007-09-05 21:46:51 +0000344 if (VNI) {
345 if (i >= NumVals)
346 valnos.push_back(VNI);
347 else
348 valnos[NumValNos] = VNI;
349 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng8b7533e2007-09-01 02:03:17 +0000350 }
351 }
Evan Cheng8b7533e2007-09-01 02:03:17 +0000352 if (NumNewVals < NumVals)
353 valnos.resize(NumNewVals); // shrinkify
Evan Cheng816a7f32007-08-11 00:59:19 +0000354
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 // Okay, now insert the RHS live ranges into the LHS.
356 iterator InsertPos = begin();
Evan Cheng983b81d2007-08-29 20:45:00 +0000357 unsigned RangeNo = 0;
358 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
359 // Map the valno in the other live range to the current live range.
360 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Cheng319802c2007-09-05 21:46:51 +0000361 assert(I->valno && "Adding a dead range?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 InsertPos = addRangeFrom(*I, InsertPos);
363 }
364
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 weight += Other.weight;
366 if (Other.preference && !preference)
367 preference = Other.preference;
368}
369
370/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
371/// interval as the specified value number. The LiveRanges in RHS are
372/// allowed to overlap with LiveRanges in the current interval, but only if
373/// the overlapping LiveRanges have the specified value number.
374void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng983b81d2007-08-29 20:45:00 +0000375 VNInfo *LHSValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 // TODO: Make this more efficient.
377 iterator InsertPos = begin();
378 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000379 // Map the valno in the other live range to the current live range.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 LiveRange Tmp = *I;
Evan Cheng983b81d2007-08-29 20:45:00 +0000381 Tmp.valno = LHSValNo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 InsertPos = addRangeFrom(Tmp, InsertPos);
383 }
384}
385
386
Evan Cheng687d1082007-10-12 08:50:34 +0000387/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
388/// in RHS into this live interval as the specified value number.
389/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng303fed82007-10-17 02:13:29 +0000390/// current interval, it will replace the value numbers of the overlaped
391/// live ranges with the specified value number.
Evan Cheng687d1082007-10-12 08:50:34 +0000392void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng06582a02007-10-14 10:08:34 +0000393 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng303fed82007-10-17 02:13:29 +0000394 SmallVector<VNInfo*, 4> ReplacedValNos;
395 iterator IP = begin();
Evan Cheng687d1082007-10-12 08:50:34 +0000396 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
397 if (I->valno != RHSValNo)
398 continue;
Evan Cheng303fed82007-10-17 02:13:29 +0000399 unsigned Start = I->start, End = I->end;
400 IP = std::upper_bound(IP, end(), Start);
401 // If the start of this range overlaps with an existing liverange, trim it.
402 if (IP != begin() && IP[-1].end > Start) {
403 if (IP->valno != LHSValNo) {
404 ReplacedValNos.push_back(IP->valno);
405 IP->valno = LHSValNo; // Update val#.
406 }
407 Start = IP[-1].end;
408 // Trimmed away the whole range?
409 if (Start >= End) continue;
410 }
411 // If the end of this range overlaps with an existing liverange, trim it.
412 if (IP != end() && End > IP->start) {
413 if (IP->valno != LHSValNo) {
414 ReplacedValNos.push_back(IP->valno);
415 IP->valno = LHSValNo; // Update val#.
416 }
417 End = IP->start;
418 // If this trimmed away the whole range, ignore it.
419 if (Start == End) continue;
420 }
421
Evan Cheng687d1082007-10-12 08:50:34 +0000422 // Map the valno in the other live range to the current live range.
Evan Cheng303fed82007-10-17 02:13:29 +0000423 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
424 }
425
426
427 SmallSet<VNInfo*, 4> Seen;
428 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
429 VNInfo *V1 = ReplacedValNos[i];
430 if (Seen.insert(V1)) {
431 bool isDead = true;
432 for (const_iterator I = begin(), E = end(); I != E; ++I)
433 if (I->valno == V1) {
434 isDead = false;
435 break;
436 }
437 if (isDead) {
438 // Now that V1 is dead, remove it. If it is the largest value number,
439 // just nuke it (and any other deleted values neighboring it), otherwise
440 // mark it as ~1U so it can be nuked later.
441 if (V1->id == getNumValNums()-1) {
442 do {
443 VNInfo *VNI = valnos.back();
444 valnos.pop_back();
445 VNI->~VNInfo();
446 } while (valnos.back()->def == ~1U);
447 } else {
448 V1->def = ~1U;
449 }
450 }
451 }
Evan Cheng687d1082007-10-12 08:50:34 +0000452 }
453}
454
455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456/// MergeInClobberRanges - For any live ranges that are not defined in the
457/// current interval, but are defined in the Clobbers interval, mark them
458/// used with an unknown definition value.
Evan Cheng319802c2007-09-05 21:46:51 +0000459void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
460 BumpPtrAllocator &VNInfoAllocator) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 if (Clobbers.begin() == Clobbers.end()) return;
462
463 // Find a value # to use for the clobber ranges. If there is already a value#
464 // for unknown values, use it.
465 // FIXME: Use a single sentinal number for these!
Evan Cheng319802c2007-09-05 21:46:51 +0000466 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467
468 iterator IP = begin();
469 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
470 unsigned Start = I->start, End = I->end;
471 IP = std::upper_bound(IP, end(), Start);
472
473 // If the start of this range overlaps with an existing liverange, trim it.
474 if (IP != begin() && IP[-1].end > Start) {
475 Start = IP[-1].end;
476 // Trimmed away the whole range?
477 if (Start >= End) continue;
478 }
479 // If the end of this range overlaps with an existing liverange, trim it.
480 if (IP != end() && End > IP->start) {
481 End = IP->start;
482 // If this trimmed away the whole range, ignore it.
483 if (Start == End) continue;
484 }
485
486 // Insert the clobber interval.
487 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
488 }
489}
490
491/// MergeValueNumberInto - This method is called when two value nubmers
492/// are found to be equivalent. This eliminates V1, replacing all
493/// LiveRanges with the V1 value number with the V2 value number. This can
494/// cause merging of V1/V2 values numbers and compaction of the value space.
Evan Cheng983b81d2007-08-29 20:45:00 +0000495void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 assert(V1 != V2 && "Identical value#'s are always equivalent!");
497
498 // This code actually merges the (numerically) larger value number into the
499 // smaller value number, which is likely to allow us to compactify the value
500 // space. The only thing we have to be careful of is to preserve the
501 // instruction that defines the result value.
502
503 // Make sure V2 is smaller than V1.
Evan Cheng983b81d2007-08-29 20:45:00 +0000504 if (V1->id < V2->id) {
Evan Cheng319802c2007-09-05 21:46:51 +0000505 copyValNumInfo(V1, V2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 std::swap(V1, V2);
507 }
508
509 // Merge V1 live ranges into V2.
510 for (iterator I = begin(); I != end(); ) {
511 iterator LR = I++;
Evan Cheng983b81d2007-08-29 20:45:00 +0000512 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513
514 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
515 // range, extend it.
516 if (LR != begin()) {
517 iterator Prev = LR-1;
Evan Cheng983b81d2007-08-29 20:45:00 +0000518 if (Prev->valno == V2 && Prev->end == LR->start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 Prev->end = LR->end;
520
521 // Erase this live-range.
522 ranges.erase(LR);
523 I = Prev+1;
524 LR = Prev;
525 }
526 }
527
528 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
529 // Ensure that it is a V2 live-range.
Evan Cheng983b81d2007-08-29 20:45:00 +0000530 LR->valno = V2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531
532 // If we can merge it into later V2 live ranges, do so now. We ignore any
533 // following V1 live ranges, as they will be merged in subsequent iterations
534 // of the loop.
535 if (I != end()) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000536 if (I->start == LR->end && I->valno == V2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 LR->end = I->end;
538 ranges.erase(I);
539 I = LR+1;
540 }
541 }
542 }
543
544 // Now that V1 is dead, remove it. If it is the largest value number, just
545 // nuke it (and any other deleted values neighboring it), otherwise mark it as
546 // ~1U so it can be nuked later.
Evan Cheng983b81d2007-08-29 20:45:00 +0000547 if (V1->id == getNumValNums()-1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 do {
Evan Cheng27344d42007-09-06 01:07:24 +0000549 VNInfo *VNI = valnos.back();
Evan Cheng983b81d2007-08-29 20:45:00 +0000550 valnos.pop_back();
Evan Cheng27344d42007-09-06 01:07:24 +0000551 VNI->~VNInfo();
Evan Cheng983b81d2007-08-29 20:45:00 +0000552 } while (valnos.back()->def == ~1U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 } else {
Evan Cheng983b81d2007-08-29 20:45:00 +0000554 V1->def = ~1U;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 }
556}
557
Evan Cheng687d1082007-10-12 08:50:34 +0000558void LiveInterval::Copy(const LiveInterval &RHS,
559 BumpPtrAllocator &VNInfoAllocator) {
560 ranges.clear();
561 valnos.clear();
562 preference = RHS.preference;
563 weight = RHS.weight;
564 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
565 const VNInfo *VNI = RHS.getValNumInfo(i);
566 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
567 copyValNumInfo(NewVNI, VNI);
568 }
569 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
570 const LiveRange &LR = RHS.ranges[i];
571 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
572 }
573}
574
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575unsigned LiveInterval::getSize() const {
576 unsigned Sum = 0;
577 for (const_iterator I = begin(), E = end(); I != E; ++I)
578 Sum += I->end - I->start;
579 return Sum;
580}
581
582std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000583 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584}
585
586void LiveRange::dump() const {
587 cerr << *this << "\n";
588}
589
590void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const {
591 if (MRI && MRegisterInfo::isPhysicalRegister(reg))
592 OS << MRI->getName(reg);
593 else
594 OS << "%reg" << reg;
595
596 OS << ',' << weight;
597
598 if (empty())
599 OS << "EMPTY";
600 else {
601 OS << " = ";
602 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
603 E = ranges.end(); I != E; ++I)
604 OS << *I;
605 }
606
607 // Print value number info.
608 if (getNumValNums()) {
609 OS << " ";
Evan Chengba990522007-08-28 08:28:51 +0000610 unsigned vnum = 0;
611 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
612 ++i, ++vnum) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000613 const VNInfo *vni = *i;
Evan Chengba990522007-08-28 08:28:51 +0000614 if (vnum) OS << " ";
615 OS << vnum << "@";
Evan Cheng983b81d2007-08-29 20:45:00 +0000616 if (vni->def == ~1U) {
Evan Cheng58c2b762007-08-08 03:00:28 +0000617 OS << "x";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 } else {
Evan Cheng983b81d2007-08-29 20:45:00 +0000619 if (vni->def == ~0U)
Evan Cheng816a7f32007-08-11 00:59:19 +0000620 OS << "?";
621 else
Evan Cheng983b81d2007-08-29 20:45:00 +0000622 OS << vni->def;
623 unsigned ee = vni->kills.size();
Evan Chengba990522007-08-28 08:28:51 +0000624 if (ee) {
Evan Cheng816a7f32007-08-11 00:59:19 +0000625 OS << "-(";
Evan Chengba990522007-08-28 08:28:51 +0000626 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000627 OS << vni->kills[j];
Evan Chengba990522007-08-28 08:28:51 +0000628 if (j != ee-1)
Evan Cheng816a7f32007-08-11 00:59:19 +0000629 OS << " ";
630 }
631 OS << ")";
632 }
Evan Cheng4151fde2007-08-07 23:49:57 +0000633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 }
635 }
636}
637
638void LiveInterval::dump() const {
639 cerr << *this << "\n";
640}
641
642
643void LiveRange::print(std::ostream &os) const {
644 os << *this;
645}