blob: 48c25a14a3506a687fd0bc88b31758599c1bb50b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
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"
Dan Gohman1e57df32008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#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
Evan Cheng7d2b9082008-02-15 18:24:29 +000047// liveBeforeAndAt - Check if the interval is live at the index and the index
48// just before it. If index is liveAt, check if it starts a new live range.
49// If it does, then check if the previous live range ends at index-1.
50bool LiveInterval::liveBeforeAndAt(unsigned I) const {
51 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
52
53 if (r == ranges.begin())
54 return false;
55
56 --r;
57 if (!r->contains(I))
58 return false;
59 if (I != r->start)
60 return true;
61 // I is the start of a live range. Check if the previous live range ends
62 // at I-1.
63 if (r == ranges.begin())
64 return false;
65 return r->end == I;
66}
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068// overlaps - Return true if the intersection of the two live intervals is
69// not empty.
70//
71// An example for overlaps():
72//
73// 0: A = ...
74// 4: B = ...
75// 8: C = A + B ;; last use of A
76//
77// The live intervals should look like:
78//
79// A = [3, 11)
80// B = [7, x)
81// C = [11, y)
82//
83// A->overlaps(C) should return false since we want to be able to join
84// A and C.
85//
86bool LiveInterval::overlapsFrom(const LiveInterval& other,
87 const_iterator StartPos) const {
88 const_iterator i = begin();
89 const_iterator ie = end();
90 const_iterator j = StartPos;
91 const_iterator je = other.end();
92
93 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
94 StartPos != other.end() && "Bogus start position hint!");
95
96 if (i->start < j->start) {
97 i = std::upper_bound(i, ie, j->start);
98 if (i != ranges.begin()) --i;
99 } else if (j->start < i->start) {
100 ++StartPos;
101 if (StartPos != other.end() && StartPos->start <= i->start) {
102 assert(StartPos < other.end() && i < end());
103 j = std::upper_bound(j, je, i->start);
104 if (j != other.ranges.begin()) --j;
105 }
106 } else {
107 return true;
108 }
109
110 if (j == je) return false;
111
112 while (i != ie) {
113 if (i->start > j->start) {
114 std::swap(i, j);
115 std::swap(ie, je);
116 }
117
118 if (i->end > j->start)
119 return true;
120 ++i;
121 }
122
123 return false;
124}
125
126/// extendIntervalEndTo - This method is used when we want to extend the range
127/// specified by I to end at the specified endpoint. To do this, we should
128/// merge and eliminate all ranges that this will overlap with. The iterator is
129/// not invalidated.
130void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
131 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000132 VNInfo *ValNo = I->valno;
Evan Cheng2d88a7b2007-08-14 01:56:58 +0000133 unsigned OldEnd = I->end;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134
135 // Search for the first interval that we can't merge with.
136 Ranges::iterator MergeTo = next(I);
137 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000138 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 }
140
141 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
142 I->end = std::max(NewEnd, prior(MergeTo)->end);
143
144 // Erase any dead ranges.
145 ranges.erase(next(I), MergeTo);
Evan Cheng816a7f32007-08-11 00:59:19 +0000146
147 // Update kill info.
Evan Cheng319802c2007-09-05 21:46:51 +0000148 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng816a7f32007-08-11 00:59:19 +0000149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 // If the newly formed range now touches the range after it and if they have
151 // the same value number, merge the two ranges into one range.
152 Ranges::iterator Next = next(I);
Evan Cheng983b81d2007-08-29 20:45:00 +0000153 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 I->end = Next->end;
155 ranges.erase(Next);
156 }
157}
158
159
160/// extendIntervalStartTo - This method is used when we want to extend the range
161/// specified by I to start at the specified endpoint. To do this, we should
162/// merge and eliminate all ranges that this will overlap with.
163LiveInterval::Ranges::iterator
164LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
165 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000166 VNInfo *ValNo = I->valno;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167
168 // Search for the first interval that we can't merge with.
169 Ranges::iterator MergeTo = I;
170 do {
171 if (MergeTo == ranges.begin()) {
172 I->start = NewStart;
173 ranges.erase(MergeTo, I);
174 return I;
175 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000176 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 --MergeTo;
178 } while (NewStart <= MergeTo->start);
179
180 // If we start in the middle of another interval, just delete a range and
181 // extend that interval.
Evan Cheng983b81d2007-08-29 20:45:00 +0000182 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 MergeTo->end = I->end;
184 } else {
185 // Otherwise, extend the interval right after.
186 ++MergeTo;
187 MergeTo->start = NewStart;
188 MergeTo->end = I->end;
189 }
190
191 ranges.erase(next(MergeTo), next(I));
192 return MergeTo;
193}
194
195LiveInterval::iterator
196LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
197 unsigned Start = LR.start, End = LR.end;
198 iterator it = std::upper_bound(From, ranges.end(), Start);
199
200 // If the inserted interval starts in the middle or right at the end of
201 // another interval, just extend that interval to contain the range of LR.
202 if (it != ranges.begin()) {
203 iterator B = prior(it);
Evan Cheng983b81d2007-08-29 20:45:00 +0000204 if (LR.valno == B->valno) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 if (B->start <= Start && B->end >= Start) {
206 extendIntervalEndTo(B, End);
207 return B;
208 }
209 } else {
210 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng983b81d2007-08-29 20:45:00 +0000211 // different valno's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 assert(B->end <= Start &&
213 "Cannot overlap two LiveRanges with differing ValID's"
214 " (did you def the same reg twice in a MachineInstr?)");
215 }
216 }
217
218 // Otherwise, if this range ends in the middle of, or right next to, another
219 // interval, merge it into that interval.
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000220 if (it != ranges.end()) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000221 if (LR.valno == it->valno) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 if (it->start <= End) {
223 it = extendIntervalStartTo(it, Start);
224
225 // If LR is a complete superset of an interval, we may need to grow its
226 // endpoint as well.
227 if (End > it->end)
228 extendIntervalEndTo(it, End);
Evan Cheng8b70e632007-11-29 09:49:23 +0000229 else if (End < it->end)
Evan Cheng1f458152007-11-29 01:05:47 +0000230 // Overlapping intervals, there might have been a kill here.
231 removeKill(it->valno, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return it;
233 }
234 } else {
235 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng983b81d2007-08-29 20:45:00 +0000236 // different valno's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 assert(it->start >= End &&
238 "Cannot overlap two LiveRanges with differing ValID's");
239 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000240 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241
242 // Otherwise, this is just a new range that doesn't interact with anything.
243 // Insert it.
244 return ranges.insert(it, LR);
245}
246
247
248/// removeRange - Remove the specified range from this interval. Note that
249/// the range must already be in this interval in its entirety.
Evan Cheng49208cf2008-02-13 02:48:26 +0000250void LiveInterval::removeRange(unsigned Start, unsigned End,
251 bool RemoveDeadValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 // Find the LiveRange containing this span.
253 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
254 assert(I != ranges.begin() && "Range is not in interval!");
255 --I;
256 assert(I->contains(Start) && I->contains(End-1) &&
257 "Range is not entirely in interval!");
258
259 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Cheng49208cf2008-02-13 02:48:26 +0000260 VNInfo *ValNo = I->valno;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 if (I->start == Start) {
Evan Cheng816a7f32007-08-11 00:59:19 +0000262 if (I->end == End) {
Evan Cheng319802c2007-09-05 21:46:51 +0000263 removeKills(I->valno, Start, End);
Evan Cheng49208cf2008-02-13 02:48:26 +0000264 if (RemoveDeadValNo) {
265 // Check if val# is dead.
266 bool isDead = true;
267 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
268 if (II != I && II->valno == ValNo) {
269 isDead = false;
270 break;
271 }
272 if (isDead) {
273 // Now that ValNo is dead, remove it. If it is the largest value
274 // number, just nuke it (and any other deleted values neighboring it),
275 // otherwise mark it as ~1U so it can be nuked later.
276 if (ValNo->id == getNumValNums()-1) {
277 do {
278 VNInfo *VNI = valnos.back();
279 valnos.pop_back();
280 VNI->~VNInfo();
281 } while (!valnos.empty() && valnos.back()->def == ~1U);
282 } else {
283 ValNo->def = ~1U;
284 }
285 }
286 }
287
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng816a7f32007-08-11 00:59:19 +0000289 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 I->start = End;
291 return;
292 }
293
294 // Otherwise if the span we are removing is at the end of the LiveRange,
295 // adjust the other way.
296 if (I->end == End) {
Evan Cheng49208cf2008-02-13 02:48:26 +0000297 removeKills(ValNo, Start, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 I->end = Start;
299 return;
300 }
301
302 // Otherwise, we are splitting the LiveRange into two pieces.
303 unsigned OldEnd = I->end;
304 I->end = Start; // Trim the old interval.
305
306 // Insert the new one.
Evan Cheng49208cf2008-02-13 02:48:26 +0000307 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308}
309
Evan Cheng49208cf2008-02-13 02:48:26 +0000310/// removeValNo - Remove all the ranges defined by the specified value#.
311/// Also remove the value# from value# list.
312void LiveInterval::removeValNo(VNInfo *ValNo) {
313 if (empty()) return;
314 Ranges::iterator I = ranges.end();
315 Ranges::iterator E = ranges.begin();
316 do {
317 --I;
318 if (I->valno == ValNo)
319 ranges.erase(I);
320 } while (I != E);
321 // Now that ValNo is dead, remove it. If it is the largest value
322 // number, just nuke it (and any other deleted values neighboring it),
323 // otherwise mark it as ~1U so it can be nuked later.
324 if (ValNo->id == getNumValNums()-1) {
325 do {
326 VNInfo *VNI = valnos.back();
327 valnos.pop_back();
328 VNI->~VNInfo();
329 } while (!valnos.empty() && valnos.back()->def == ~1U);
330 } else {
331 ValNo->def = ~1U;
332 }
333}
334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335/// getLiveRangeContaining - Return the live range that contains the
336/// specified index, or null if there is none.
337LiveInterval::const_iterator
338LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
339 const_iterator It = std::upper_bound(begin(), end(), Idx);
340 if (It != ranges.begin()) {
341 --It;
342 if (It->contains(Idx))
343 return It;
344 }
345
346 return end();
347}
348
349LiveInterval::iterator
350LiveInterval::FindLiveRangeContaining(unsigned Idx) {
351 iterator It = std::upper_bound(begin(), end(), Idx);
352 if (It != begin()) {
353 --It;
354 if (It->contains(Idx))
355 return It;
356 }
357
358 return end();
359}
360
361/// join - Join two live intervals (this, and other) together. This applies
362/// mappings to the value numbers in the LHS/RHS intervals as specified. If
363/// the intervals are not joinable, this aborts.
David Greenee97f0772007-09-06 19:46:46 +0000364void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
365 const int *RHSValNoAssignments,
Evan Cheng983b81d2007-08-29 20:45:00 +0000366 SmallVector<VNInfo*, 16> &NewVNInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng8b7533e2007-09-01 02:03:17 +0000368 // we want to avoid the interval scan if not.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 bool MustMapCurValNos = false;
Evan Cheng8b7533e2007-09-01 02:03:17 +0000370 unsigned NumVals = getNumValNums();
371 unsigned NumNewVals = NewVNInfo.size();
372 for (unsigned i = 0; i != NumVals; ++i) {
373 unsigned LHSValID = LHSValNoAssignments[i];
374 if (i != LHSValID ||
Evan Cheng319802c2007-09-05 21:46:51 +0000375 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 MustMapCurValNos = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 }
Evan Cheng983b81d2007-08-29 20:45:00 +0000378
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 // If we have to apply a mapping to our base interval assignment, rewrite it
380 // now.
381 if (MustMapCurValNos) {
382 // Map the first live range.
383 iterator OutIt = begin();
Evan Cheng983b81d2007-08-29 20:45:00 +0000384 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 ++OutIt;
386 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000387 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388
389 // If this live range has the same value # as its immediate predecessor,
390 // and if they are neighbors, remove one LiveRange. This happens when we
391 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng983b81d2007-08-29 20:45:00 +0000392 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 (OutIt-1)->end = OutIt->end;
394 } else {
395 if (I != OutIt) {
396 OutIt->start = I->start;
397 OutIt->end = I->end;
398 }
399
400 // Didn't merge, on to the next one.
401 ++OutIt;
402 }
403 }
404
405 // If we merge some live ranges, chop off the end.
406 ranges.erase(OutIt, end());
407 }
Evan Cheng816a7f32007-08-11 00:59:19 +0000408
Evan Cheng983b81d2007-08-29 20:45:00 +0000409 // Remember assignements because val# ids are changing.
Evan Cheng8b7533e2007-09-01 02:03:17 +0000410 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng983b81d2007-08-29 20:45:00 +0000411 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
412 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
413
414 // Update val# info. Renumber them and make sure they all belong to this
Evan Cheng319802c2007-09-05 21:46:51 +0000415 // LiveInterval now. Also remove dead val#'s.
416 unsigned NumValNos = 0;
417 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000418 VNInfo *VNI = NewVNInfo[i];
Evan Cheng319802c2007-09-05 21:46:51 +0000419 if (VNI) {
420 if (i >= NumVals)
421 valnos.push_back(VNI);
422 else
423 valnos[NumValNos] = VNI;
424 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng8b7533e2007-09-01 02:03:17 +0000425 }
426 }
Evan Cheng8b7533e2007-09-01 02:03:17 +0000427 if (NumNewVals < NumVals)
428 valnos.resize(NumNewVals); // shrinkify
Evan Cheng816a7f32007-08-11 00:59:19 +0000429
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 // Okay, now insert the RHS live ranges into the LHS.
431 iterator InsertPos = begin();
Evan Cheng983b81d2007-08-29 20:45:00 +0000432 unsigned RangeNo = 0;
433 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
434 // Map the valno in the other live range to the current live range.
435 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Cheng319802c2007-09-05 21:46:51 +0000436 assert(I->valno && "Adding a dead range?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 InsertPos = addRangeFrom(*I, InsertPos);
438 }
439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 weight += Other.weight;
441 if (Other.preference && !preference)
442 preference = Other.preference;
443}
444
445/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
446/// interval as the specified value number. The LiveRanges in RHS are
447/// allowed to overlap with LiveRanges in the current interval, but only if
448/// the overlapping LiveRanges have the specified value number.
449void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng983b81d2007-08-29 20:45:00 +0000450 VNInfo *LHSValNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 // TODO: Make this more efficient.
452 iterator InsertPos = begin();
453 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000454 // Map the valno in the other live range to the current live range.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 LiveRange Tmp = *I;
Evan Cheng983b81d2007-08-29 20:45:00 +0000456 Tmp.valno = LHSValNo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 InsertPos = addRangeFrom(Tmp, InsertPos);
458 }
459}
460
461
Evan Cheng687d1082007-10-12 08:50:34 +0000462/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
463/// in RHS into this live interval as the specified value number.
464/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng303fed82007-10-17 02:13:29 +0000465/// current interval, it will replace the value numbers of the overlaped
466/// live ranges with the specified value number.
Evan Cheng687d1082007-10-12 08:50:34 +0000467void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng06582a02007-10-14 10:08:34 +0000468 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng303fed82007-10-17 02:13:29 +0000469 SmallVector<VNInfo*, 4> ReplacedValNos;
470 iterator IP = begin();
Evan Cheng687d1082007-10-12 08:50:34 +0000471 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
472 if (I->valno != RHSValNo)
473 continue;
Evan Cheng303fed82007-10-17 02:13:29 +0000474 unsigned Start = I->start, End = I->end;
475 IP = std::upper_bound(IP, end(), Start);
476 // If the start of this range overlaps with an existing liverange, trim it.
477 if (IP != begin() && IP[-1].end > Start) {
Evan Chengc7fe7be2008-01-30 22:44:55 +0000478 if (IP[-1].valno != LHSValNo) {
479 ReplacedValNos.push_back(IP[-1].valno);
480 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng303fed82007-10-17 02:13:29 +0000481 }
482 Start = IP[-1].end;
483 // Trimmed away the whole range?
484 if (Start >= End) continue;
485 }
486 // If the end of this range overlaps with an existing liverange, trim it.
487 if (IP != end() && End > IP->start) {
488 if (IP->valno != LHSValNo) {
489 ReplacedValNos.push_back(IP->valno);
490 IP->valno = LHSValNo; // Update val#.
491 }
492 End = IP->start;
493 // If this trimmed away the whole range, ignore it.
494 if (Start == End) continue;
495 }
496
Evan Cheng687d1082007-10-12 08:50:34 +0000497 // Map the valno in the other live range to the current live range.
Evan Cheng303fed82007-10-17 02:13:29 +0000498 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
499 }
500
501
502 SmallSet<VNInfo*, 4> Seen;
503 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
504 VNInfo *V1 = ReplacedValNos[i];
505 if (Seen.insert(V1)) {
506 bool isDead = true;
507 for (const_iterator I = begin(), E = end(); I != E; ++I)
508 if (I->valno == V1) {
509 isDead = false;
510 break;
511 }
512 if (isDead) {
513 // Now that V1 is dead, remove it. If it is the largest value number,
514 // just nuke it (and any other deleted values neighboring it), otherwise
515 // mark it as ~1U so it can be nuked later.
516 if (V1->id == getNumValNums()-1) {
517 do {
518 VNInfo *VNI = valnos.back();
519 valnos.pop_back();
520 VNI->~VNInfo();
Evan Cheng49208cf2008-02-13 02:48:26 +0000521 } while (!valnos.empty() && valnos.back()->def == ~1U);
Evan Cheng303fed82007-10-17 02:13:29 +0000522 } else {
523 V1->def = ~1U;
524 }
525 }
526 }
Evan Cheng687d1082007-10-12 08:50:34 +0000527 }
528}
529
530
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531/// MergeInClobberRanges - For any live ranges that are not defined in the
532/// current interval, but are defined in the Clobbers interval, mark them
533/// used with an unknown definition value.
Evan Cheng319802c2007-09-05 21:46:51 +0000534void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
535 BumpPtrAllocator &VNInfoAllocator) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 if (Clobbers.begin() == Clobbers.end()) return;
537
538 // Find a value # to use for the clobber ranges. If there is already a value#
539 // for unknown values, use it.
540 // FIXME: Use a single sentinal number for these!
Evan Cheng319802c2007-09-05 21:46:51 +0000541 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542
543 iterator IP = begin();
544 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
545 unsigned Start = I->start, End = I->end;
546 IP = std::upper_bound(IP, end(), Start);
547
548 // If the start of this range overlaps with an existing liverange, trim it.
549 if (IP != begin() && IP[-1].end > Start) {
550 Start = IP[-1].end;
551 // Trimmed away the whole range?
552 if (Start >= End) continue;
553 }
554 // If the end of this range overlaps with an existing liverange, trim it.
555 if (IP != end() && End > IP->start) {
556 End = IP->start;
557 // If this trimmed away the whole range, ignore it.
558 if (Start == End) continue;
559 }
560
561 // Insert the clobber interval.
562 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
563 }
564}
565
566/// MergeValueNumberInto - This method is called when two value nubmers
567/// are found to be equivalent. This eliminates V1, replacing all
568/// LiveRanges with the V1 value number with the V2 value number. This can
569/// cause merging of V1/V2 values numbers and compaction of the value space.
Evan Cheng983b81d2007-08-29 20:45:00 +0000570void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 assert(V1 != V2 && "Identical value#'s are always equivalent!");
572
573 // This code actually merges the (numerically) larger value number into the
574 // smaller value number, which is likely to allow us to compactify the value
575 // space. The only thing we have to be careful of is to preserve the
576 // instruction that defines the result value.
577
578 // Make sure V2 is smaller than V1.
Evan Cheng983b81d2007-08-29 20:45:00 +0000579 if (V1->id < V2->id) {
Evan Cheng319802c2007-09-05 21:46:51 +0000580 copyValNumInfo(V1, V2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 std::swap(V1, V2);
582 }
583
584 // Merge V1 live ranges into V2.
585 for (iterator I = begin(); I != end(); ) {
586 iterator LR = I++;
Evan Cheng983b81d2007-08-29 20:45:00 +0000587 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588
589 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
590 // range, extend it.
591 if (LR != begin()) {
592 iterator Prev = LR-1;
Evan Cheng983b81d2007-08-29 20:45:00 +0000593 if (Prev->valno == V2 && Prev->end == LR->start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 Prev->end = LR->end;
595
596 // Erase this live-range.
597 ranges.erase(LR);
598 I = Prev+1;
599 LR = Prev;
600 }
601 }
602
603 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
604 // Ensure that it is a V2 live-range.
Evan Cheng983b81d2007-08-29 20:45:00 +0000605 LR->valno = V2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606
607 // If we can merge it into later V2 live ranges, do so now. We ignore any
608 // following V1 live ranges, as they will be merged in subsequent iterations
609 // of the loop.
610 if (I != end()) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000611 if (I->start == LR->end && I->valno == V2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 LR->end = I->end;
613 ranges.erase(I);
614 I = LR+1;
615 }
616 }
617 }
618
619 // Now that V1 is dead, remove it. If it is the largest value number, just
620 // nuke it (and any other deleted values neighboring it), otherwise mark it as
621 // ~1U so it can be nuked later.
Evan Cheng983b81d2007-08-29 20:45:00 +0000622 if (V1->id == getNumValNums()-1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 do {
Evan Cheng27344d42007-09-06 01:07:24 +0000624 VNInfo *VNI = valnos.back();
Evan Cheng983b81d2007-08-29 20:45:00 +0000625 valnos.pop_back();
Evan Cheng27344d42007-09-06 01:07:24 +0000626 VNI->~VNInfo();
Evan Cheng983b81d2007-08-29 20:45:00 +0000627 } while (valnos.back()->def == ~1U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 } else {
Evan Cheng983b81d2007-08-29 20:45:00 +0000629 V1->def = ~1U;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 }
631}
632
Evan Cheng687d1082007-10-12 08:50:34 +0000633void LiveInterval::Copy(const LiveInterval &RHS,
634 BumpPtrAllocator &VNInfoAllocator) {
635 ranges.clear();
636 valnos.clear();
637 preference = RHS.preference;
638 weight = RHS.weight;
639 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
640 const VNInfo *VNI = RHS.getValNumInfo(i);
641 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
642 copyValNumInfo(NewVNI, VNI);
643 }
644 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
645 const LiveRange &LR = RHS.ranges[i];
646 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
647 }
648}
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650unsigned LiveInterval::getSize() const {
651 unsigned Sum = 0;
652 for (const_iterator I = begin(), E = end(); I != E; ++I)
653 Sum += I->end - I->start;
654 return Sum;
655}
656
657std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000658 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659}
660
661void LiveRange::dump() const {
662 cerr << *this << "\n";
663}
664
Dan Gohman1e57df32008-02-10 18:45:23 +0000665void LiveInterval::print(std::ostream &OS,
666 const TargetRegisterInfo *TRI) const {
667 if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
668 OS << TRI->getName(reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 else
670 OS << "%reg" << reg;
671
672 OS << ',' << weight;
673
674 if (empty())
675 OS << "EMPTY";
676 else {
677 OS << " = ";
678 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
679 E = ranges.end(); I != E; ++I)
680 OS << *I;
681 }
682
683 // Print value number info.
684 if (getNumValNums()) {
685 OS << " ";
Evan Chengba990522007-08-28 08:28:51 +0000686 unsigned vnum = 0;
687 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
688 ++i, ++vnum) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000689 const VNInfo *vni = *i;
Evan Chengba990522007-08-28 08:28:51 +0000690 if (vnum) OS << " ";
691 OS << vnum << "@";
Evan Cheng983b81d2007-08-29 20:45:00 +0000692 if (vni->def == ~1U) {
Evan Cheng58c2b762007-08-08 03:00:28 +0000693 OS << "x";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 } else {
Evan Cheng983b81d2007-08-29 20:45:00 +0000695 if (vni->def == ~0U)
Evan Cheng816a7f32007-08-11 00:59:19 +0000696 OS << "?";
697 else
Evan Cheng983b81d2007-08-29 20:45:00 +0000698 OS << vni->def;
699 unsigned ee = vni->kills.size();
Evan Cheng49208cf2008-02-13 02:48:26 +0000700 if (ee || vni->hasPHIKill) {
Evan Cheng816a7f32007-08-11 00:59:19 +0000701 OS << "-(";
Evan Chengba990522007-08-28 08:28:51 +0000702 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000703 OS << vni->kills[j];
Evan Chengba990522007-08-28 08:28:51 +0000704 if (j != ee-1)
Evan Cheng816a7f32007-08-11 00:59:19 +0000705 OS << " ";
706 }
Evan Cheng49208cf2008-02-13 02:48:26 +0000707 if (vni->hasPHIKill) {
708 if (ee)
709 OS << " ";
710 OS << "phi";
711 }
Evan Cheng816a7f32007-08-11 00:59:19 +0000712 OS << ")";
713 }
Evan Cheng4151fde2007-08-07 23:49:57 +0000714 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
716 }
717}
718
719void LiveInterval::dump() const {
720 cerr << *this << "\n";
721}
722
723
724void LiveRange::print(std::ostream &os) const {
725 os << *this;
726}