blob: 68d9ad4b3e658fd2a43311a0550c7a9bcc372442 [file] [log] [blame]
Chris Lattnerfb449b92004-07-23 17:49:16 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfb449b92004-07-23 17:49:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveRange and LiveInterval classes. Given some
11// numbering of each the machine instructions an interval [i, j) is said to be a
12// live interval for register v if there is no instruction with number j' > j
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"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000022#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000023#include "llvm/ADT/STLExtras.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000024#include "llvm/Support/Streams.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000026#include <algorithm>
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
Evan Chengc8d044e2008-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
Chris Lattnerbae74d92004-11-18 03:47:34 +000068// overlaps - Return true if the intersection of the two live intervals is
69// not empty.
70//
Chris Lattnerfb449b92004-07-23 17:49:16 +000071// 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.
Chris Lattnerbae74d92004-11-18 03:47:34 +000085//
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()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000094 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000095
Chris Lattnerfb449b92004-07-23 17:49:16 +000096 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000097 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000098 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000099 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000100 ++StartPos;
101 if (StartPos != other.end() && StartPos->start <= i->start) {
102 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000103 j = std::upper_bound(j, je, i->start);
104 if (j != other.ranges.begin()) --j;
105 }
Chris Lattneraa141472004-07-23 18:40:00 +0000106 } else {
107 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000108 }
109
Chris Lattner9fddc122004-11-18 05:28:21 +0000110 if (j == je) return false;
111
112 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000113 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000114 std::swap(i, j);
115 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000116 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000117
118 if (i->end > j->start)
119 return true;
120 ++i;
121 }
122
123 return false;
124}
125
Evan Chengcccdb2b2009-04-18 08:52:15 +0000126/// overlaps - Return true if the live interval overlaps a range specified
127/// by [Start, End).
128bool LiveInterval::overlaps(unsigned Start, unsigned End) const {
129 assert(Start < End && "Invalid range");
130 const_iterator I = begin();
131 const_iterator E = end();
132 const_iterator si = std::upper_bound(I, E, Start);
133 const_iterator ei = std::upper_bound(I, E, End);
134 if (si != ei)
135 return true;
136 if (si == I)
137 return false;
138 --si;
139 return si->contains(Start);
140}
141
Chris Lattnerb26c2152004-07-23 19:38:44 +0000142/// extendIntervalEndTo - This method is used when we want to extend the range
143/// specified by I to end at the specified endpoint. To do this, we should
144/// merge and eliminate all ranges that this will overlap with. The iterator is
145/// not invalidated.
146void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
147 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000148 VNInfo *ValNo = I->valno;
Evan Cheng430a7b02007-08-14 01:56:58 +0000149 unsigned OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000150
Chris Lattnerb26c2152004-07-23 19:38:44 +0000151 // Search for the first interval that we can't merge with.
152 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000153 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000154 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000155 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000156
157 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
158 I->end = std::max(NewEnd, prior(MergeTo)->end);
159
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000160 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000161 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000162
163 // Update kill info.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000164 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000165
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000166 // If the newly formed range now touches the range after it and if they have
167 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000168 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000169 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000170 I->end = Next->end;
171 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000172 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000173}
174
175
176/// extendIntervalStartTo - This method is used when we want to extend the range
177/// specified by I to start at the specified endpoint. To do this, we should
178/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000179LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000180LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
181 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000182 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000183
184 // Search for the first interval that we can't merge with.
185 Ranges::iterator MergeTo = I;
186 do {
187 if (MergeTo == ranges.begin()) {
188 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000189 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000190 return I;
191 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000192 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000193 --MergeTo;
194 } while (NewStart <= MergeTo->start);
195
196 // If we start in the middle of another interval, just delete a range and
197 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000198 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000199 MergeTo->end = I->end;
200 } else {
201 // Otherwise, extend the interval right after.
202 ++MergeTo;
203 MergeTo->start = NewStart;
204 MergeTo->end = I->end;
205 }
206
207 ranges.erase(next(MergeTo), next(I));
208 return MergeTo;
209}
210
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000211LiveInterval::iterator
212LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000213 unsigned Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000214 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000215
216 // If the inserted interval starts in the middle or right at the end of
217 // another interval, just extend that interval to contain the range of LR.
218 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000219 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000220 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000221 if (B->start <= Start && B->end >= Start) {
222 extendIntervalEndTo(B, End);
223 return B;
224 }
225 } else {
226 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000227 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000228 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000229 "Cannot overlap two LiveRanges with differing ValID's"
230 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000231 }
232 }
233
234 // Otherwise, if this range ends in the middle of, or right next to, another
235 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000236 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000237 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000238 if (it->start <= End) {
239 it = extendIntervalStartTo(it, Start);
240
241 // If LR is a complete superset of an interval, we may need to grow its
242 // endpoint as well.
243 if (End > it->end)
244 extendIntervalEndTo(it, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000245 else if (End < it->end)
Evan Chengc3868e02007-11-29 01:05:47 +0000246 // Overlapping intervals, there might have been a kill here.
247 removeKill(it->valno, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000248 return it;
249 }
250 } else {
251 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000252 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000253 assert(it->start >= End &&
254 "Cannot overlap two LiveRanges with differing ValID's");
255 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000256 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000257
258 // Otherwise, this is just a new range that doesn't interact with anything.
259 // Insert it.
260 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000261}
262
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000263/// isInOneLiveRange - Return true if the range specified is entirely in the
264/// a single LiveRange of the live interval.
265bool LiveInterval::isInOneLiveRange(unsigned Start, unsigned End) {
266 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
267 if (I == ranges.begin())
268 return false;
269 --I;
270 return I->contains(Start) && I->contains(End-1);
271}
272
Chris Lattnerabf295f2004-07-24 02:52:23 +0000273
274/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000275/// the range must be in a single LiveRange in its entirety.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000276void LiveInterval::removeRange(unsigned Start, unsigned End,
277 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000278 // Find the LiveRange containing this span.
279 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
280 assert(I != ranges.begin() && "Range is not in interval!");
281 --I;
282 assert(I->contains(Start) && I->contains(End-1) &&
283 "Range is not entirely in interval!");
284
285 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000286 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000287 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000288 if (I->end == End) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000289 removeKills(I->valno, Start, End);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000290 if (RemoveDeadValNo) {
291 // Check if val# is dead.
292 bool isDead = true;
293 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
294 if (II != I && II->valno == ValNo) {
295 isDead = false;
296 break;
297 }
298 if (isDead) {
299 // Now that ValNo is dead, remove it. If it is the largest value
300 // number, just nuke it (and any other deleted values neighboring it),
301 // otherwise mark it as ~1U so it can be nuked later.
302 if (ValNo->id == getNumValNums()-1) {
303 do {
304 VNInfo *VNI = valnos.back();
305 valnos.pop_back();
306 VNI->~VNInfo();
307 } while (!valnos.empty() && valnos.back()->def == ~1U);
308 } else {
309 ValNo->def = ~1U;
310 }
311 }
312 }
313
Chris Lattnerabf295f2004-07-24 02:52:23 +0000314 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000315 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000316 I->start = End;
317 return;
318 }
319
320 // Otherwise if the span we are removing is at the end of the LiveRange,
321 // adjust the other way.
322 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000323 removeKills(ValNo, Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000324 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000325 return;
326 }
327
328 // Otherwise, we are splitting the LiveRange into two pieces.
329 unsigned OldEnd = I->end;
330 I->end = Start; // Trim the old interval.
331
332 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000333 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000334}
335
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000336/// removeValNo - Remove all the ranges defined by the specified value#.
337/// Also remove the value# from value# list.
338void LiveInterval::removeValNo(VNInfo *ValNo) {
339 if (empty()) return;
340 Ranges::iterator I = ranges.end();
341 Ranges::iterator E = ranges.begin();
342 do {
343 --I;
344 if (I->valno == ValNo)
345 ranges.erase(I);
346 } while (I != E);
347 // Now that ValNo is dead, remove it. If it is the largest value
348 // number, just nuke it (and any other deleted values neighboring it),
349 // otherwise mark it as ~1U so it can be nuked later.
350 if (ValNo->id == getNumValNums()-1) {
351 do {
352 VNInfo *VNI = valnos.back();
353 valnos.pop_back();
354 VNI->~VNInfo();
355 } while (!valnos.empty() && valnos.back()->def == ~1U);
356 } else {
357 ValNo->def = ~1U;
358 }
359}
360
Chris Lattnerabf295f2004-07-24 02:52:23 +0000361/// getLiveRangeContaining - Return the live range that contains the
362/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000363LiveInterval::const_iterator
364LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
365 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000366 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000367 --It;
368 if (It->contains(Idx))
369 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000370 }
371
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000372 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000373}
374
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000375LiveInterval::iterator
376LiveInterval::FindLiveRangeContaining(unsigned Idx) {
377 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000378 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000379 --It;
380 if (It->contains(Idx))
381 return It;
382 }
383
384 return end();
385}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000386
Evan Cheng3f32d652008-06-04 09:18:41 +0000387/// findDefinedVNInfo - Find the VNInfo that's defined at the specified index
388/// (register interval) or defined by the specified register (stack inteval).
389VNInfo *LiveInterval::findDefinedVNInfo(unsigned DefIdxOrReg) const {
390 VNInfo *VNI = NULL;
391 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
392 i != e; ++i)
393 if ((*i)->def == DefIdxOrReg) {
394 VNI = *i;
395 break;
396 }
397 return VNI;
398}
399
400
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000401/// join - Join two live intervals (this, and other) together. This applies
402/// mappings to the value numbers in the LHS/RHS intervals as specified. If
403/// the intervals are not joinable, this aborts.
David Greeneaf992f72007-09-06 19:46:46 +0000404void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
405 const int *RHSValNoAssignments,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000406 SmallVector<VNInfo*, 16> &NewVNInfo) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000407 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000408 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000409 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000410 unsigned NumVals = getNumValNums();
411 unsigned NumNewVals = NewVNInfo.size();
412 for (unsigned i = 0; i != NumVals; ++i) {
413 unsigned LHSValID = LHSValNoAssignments[i];
414 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000415 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000416 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000417 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000418
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000419 // If we have to apply a mapping to our base interval assignment, rewrite it
420 // now.
421 if (MustMapCurValNos) {
422 // Map the first live range.
423 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000424 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000425 ++OutIt;
426 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000427 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000428
429 // If this live range has the same value # as its immediate predecessor,
430 // and if they are neighbors, remove one LiveRange. This happens when we
431 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000432 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000433 (OutIt-1)->end = OutIt->end;
434 } else {
435 if (I != OutIt) {
436 OutIt->start = I->start;
437 OutIt->end = I->end;
438 }
439
440 // Didn't merge, on to the next one.
441 ++OutIt;
442 }
443 }
444
445 // If we merge some live ranges, chop off the end.
446 ranges.erase(OutIt, end());
447 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000448
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000449 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000450 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000451 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
452 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
453
454 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000455 // LiveInterval now. Also remove dead val#'s.
456 unsigned NumValNos = 0;
457 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000458 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000459 if (VNI) {
460 if (i >= NumVals)
461 valnos.push_back(VNI);
462 else
463 valnos[NumValNos] = VNI;
464 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000465 }
466 }
Evan Cheng34301352007-09-01 02:03:17 +0000467 if (NumNewVals < NumVals)
468 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000469
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000470 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000471 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000472 unsigned RangeNo = 0;
473 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
474 // Map the valno in the other live range to the current live range.
475 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000476 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000477 InsertPos = addRangeFrom(*I, InsertPos);
478 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000479
Chris Lattnerabf295f2004-07-24 02:52:23 +0000480 weight += Other.weight;
Evan Chenge52eef82007-04-17 20:25:11 +0000481 if (Other.preference && !preference)
482 preference = Other.preference;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000483}
484
Chris Lattnerf21f0202006-09-02 05:26:59 +0000485/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
486/// interval as the specified value number. The LiveRanges in RHS are
487/// allowed to overlap with LiveRanges in the current interval, but only if
488/// the overlapping LiveRanges have the specified value number.
489void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000490 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000491 // TODO: Make this more efficient.
492 iterator InsertPos = begin();
493 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000494 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000495 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000496 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000497 InsertPos = addRangeFrom(Tmp, InsertPos);
498 }
499}
500
501
Evan Cheng32dfbea2007-10-12 08:50:34 +0000502/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
503/// in RHS into this live interval as the specified value number.
504/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000505/// current interval, it will replace the value numbers of the overlaped
506/// live ranges with the specified value number.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000507void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng34729252007-10-14 10:08:34 +0000508 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000509 SmallVector<VNInfo*, 4> ReplacedValNos;
510 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000511 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
512 if (I->valno != RHSValNo)
513 continue;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000514 unsigned Start = I->start, End = I->end;
515 IP = std::upper_bound(IP, end(), Start);
516 // If the start of this range overlaps with an existing liverange, trim it.
517 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000518 if (IP[-1].valno != LHSValNo) {
519 ReplacedValNos.push_back(IP[-1].valno);
520 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000521 }
522 Start = IP[-1].end;
523 // Trimmed away the whole range?
524 if (Start >= End) continue;
525 }
526 // If the end of this range overlaps with an existing liverange, trim it.
527 if (IP != end() && End > IP->start) {
528 if (IP->valno != LHSValNo) {
529 ReplacedValNos.push_back(IP->valno);
530 IP->valno = LHSValNo; // Update val#.
531 }
532 End = IP->start;
533 // If this trimmed away the whole range, ignore it.
534 if (Start == End) continue;
535 }
536
Evan Cheng32dfbea2007-10-12 08:50:34 +0000537 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000538 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
539 }
540
541
542 SmallSet<VNInfo*, 4> Seen;
543 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
544 VNInfo *V1 = ReplacedValNos[i];
545 if (Seen.insert(V1)) {
546 bool isDead = true;
547 for (const_iterator I = begin(), E = end(); I != E; ++I)
548 if (I->valno == V1) {
549 isDead = false;
550 break;
551 }
552 if (isDead) {
553 // Now that V1 is dead, remove it. If it is the largest value number,
554 // just nuke it (and any other deleted values neighboring it), otherwise
555 // mark it as ~1U so it can be nuked later.
556 if (V1->id == getNumValNums()-1) {
557 do {
558 VNInfo *VNI = valnos.back();
559 valnos.pop_back();
560 VNI->~VNInfo();
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000561 } while (!valnos.empty() && valnos.back()->def == ~1U);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000562 } else {
563 V1->def = ~1U;
564 }
565 }
566 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000567 }
568}
569
Evan Chenga2e64352009-03-11 00:03:21 +0000570VNInfo *LiveInterval::getUnknownValNo(BumpPtrAllocator &VNInfoAllocator) {
571 unsigned i = getNumValNums();
572 if (i) {
573 do {
574 --i;
575 VNInfo *VNI = getValNumInfo(i);
576 if (VNI->def == ~0U && !VNI->copy &&
577 !VNI->hasPHIKill && !VNI->redefByEC && VNI->kills.empty())
578 return VNI;
579 } while (i != 0);
580 }
581 return getNextValue(~0U, 0, VNInfoAllocator);
582}
583
Evan Cheng32dfbea2007-10-12 08:50:34 +0000584
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000585/// MergeInClobberRanges - For any live ranges that are not defined in the
586/// current interval, but are defined in the Clobbers interval, mark them
587/// used with an unknown definition value.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000588void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
589 BumpPtrAllocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000590 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000591
592 // Find a value # to use for the clobber ranges. If there is already a value#
593 // for unknown values, use it.
Evan Chenga2e64352009-03-11 00:03:21 +0000594 VNInfo *ClobberValNo = getUnknownValNo(VNInfoAllocator);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000595
596 iterator IP = begin();
597 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Dan Gohman97121ba2009-04-08 00:15:30 +0000598 bool Done = false;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000599 unsigned Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000600 // If a clobber range starts before an existing range and ends after
601 // it, the clobber range will need to be split into multiple ranges.
602 // Loop until the entire clobber range is handled.
603 while (!Done) {
604 Done = true;
605 IP = std::upper_bound(IP, end(), Start);
606 unsigned SubRangeStart = Start;
607 unsigned SubRangeEnd = End;
608
609 // If the start of this range overlaps with an existing liverange, trim it.
610 if (IP != begin() && IP[-1].end > SubRangeStart) {
611 SubRangeStart = IP[-1].end;
612 // Trimmed away the whole range?
613 if (SubRangeStart >= SubRangeEnd) continue;
614 }
615 // If the end of this range overlaps with an existing liverange, trim it.
616 if (IP != end() && SubRangeEnd > IP->start) {
617 // If the clobber live range extends beyond the existing live range,
618 // it'll need at least another live range, so set the flag to keep
619 // iterating.
620 if (SubRangeEnd > IP->end) {
621 Start = IP->end;
622 Done = false;
623 }
624 SubRangeEnd = IP->start;
625 // If this trimmed away the whole range, ignore it.
626 if (SubRangeStart == SubRangeEnd) continue;
627 }
628
629 // Insert the clobber interval.
630 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
631 IP);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000632 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000633 }
634}
635
Evan Chenga2e64352009-03-11 00:03:21 +0000636void LiveInterval::MergeInClobberRange(unsigned Start, unsigned End,
637 BumpPtrAllocator &VNInfoAllocator) {
638 // Find a value # to use for the clobber ranges. If there is already a value#
639 // for unknown values, use it.
640 VNInfo *ClobberValNo = getUnknownValNo(VNInfoAllocator);
641
642 iterator IP = begin();
643 IP = std::upper_bound(IP, end(), Start);
644
645 // If the start of this range overlaps with an existing liverange, trim it.
646 if (IP != begin() && IP[-1].end > Start) {
647 Start = IP[-1].end;
648 // Trimmed away the whole range?
649 if (Start >= End) return;
650 }
651 // If the end of this range overlaps with an existing liverange, trim it.
652 if (IP != end() && End > IP->start) {
653 End = IP->start;
654 // If this trimmed away the whole range, ignore it.
655 if (Start == End) return;
656 }
657
658 // Insert the clobber interval.
659 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
660}
661
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000662/// MergeValueNumberInto - This method is called when two value nubmers
663/// are found to be equivalent. This eliminates V1, replacing all
664/// LiveRanges with the V1 value number with the V2 value number. This can
665/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000666VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000667 assert(V1 != V2 && "Identical value#'s are always equivalent!");
668
669 // This code actually merges the (numerically) larger value number into the
670 // smaller value number, which is likely to allow us to compactify the value
671 // space. The only thing we have to be careful of is to preserve the
672 // instruction that defines the result value.
673
674 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000675 if (V1->id < V2->id) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000676 copyValNumInfo(V1, V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000677 std::swap(V1, V2);
678 }
679
680 // Merge V1 live ranges into V2.
681 for (iterator I = begin(); I != end(); ) {
682 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000683 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000684
685 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
686 // range, extend it.
687 if (LR != begin()) {
688 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000689 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000690 Prev->end = LR->end;
691
692 // Erase this live-range.
693 ranges.erase(LR);
694 I = Prev+1;
695 LR = Prev;
696 }
697 }
698
699 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
700 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000701 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000702
703 // If we can merge it into later V2 live ranges, do so now. We ignore any
704 // following V1 live ranges, as they will be merged in subsequent iterations
705 // of the loop.
706 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000707 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000708 LR->end = I->end;
709 ranges.erase(I);
710 I = LR+1;
711 }
712 }
713 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000714
715 // Now that V1 is dead, remove it. If it is the largest value number, just
716 // nuke it (and any other deleted values neighboring it), otherwise mark it as
717 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000718 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000719 do {
Evan Chengdd199d22007-09-06 01:07:24 +0000720 VNInfo *VNI = valnos.back();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000721 valnos.pop_back();
Evan Chengdd199d22007-09-06 01:07:24 +0000722 VNI->~VNInfo();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000723 } while (valnos.back()->def == ~1U);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000724 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000725 V1->def = ~1U;
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000726 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000727
728 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000729}
730
Evan Cheng32dfbea2007-10-12 08:50:34 +0000731void LiveInterval::Copy(const LiveInterval &RHS,
732 BumpPtrAllocator &VNInfoAllocator) {
733 ranges.clear();
734 valnos.clear();
735 preference = RHS.preference;
736 weight = RHS.weight;
737 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
738 const VNInfo *VNI = RHS.getValNumInfo(i);
739 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
740 copyValNumInfo(NewVNI, VNI);
741 }
742 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
743 const LiveRange &LR = RHS.ranges[i];
744 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
745 }
746}
747
Evan Chenge52eef82007-04-17 20:25:11 +0000748unsigned LiveInterval::getSize() const {
749 unsigned Sum = 0;
750 for (const_iterator I = begin(), E = end(); I != E; ++I)
751 Sum += I->end - I->start;
752 return Sum;
753}
754
Chris Lattnerfb449b92004-07-23 17:49:16 +0000755std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000756 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000757}
758
Chris Lattnerabf295f2004-07-24 02:52:23 +0000759void LiveRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000760 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000761}
762
Dan Gohman6f0d0242008-02-10 18:45:23 +0000763void LiveInterval::print(std::ostream &OS,
764 const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000765 if (isStackSlot())
766 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000767 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000768 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000769 else
770 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000771
Chris Lattner38135af2005-05-14 05:34:15 +0000772 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000773
Chris Lattner38135af2005-05-14 05:34:15 +0000774 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000775 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000776 else {
777 OS << " = ";
778 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
779 E = ranges.end(); I != E; ++I)
780 OS << *I;
781 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000782
783 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000784 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000785 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000786 unsigned vnum = 0;
787 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
788 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000789 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000790 if (vnum) OS << " ";
791 OS << vnum << "@";
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000792 if (vni->def == ~1U) {
Evan Cheng8df78602007-08-08 03:00:28 +0000793 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000794 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000795 if (vni->def == ~0U)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000796 OS << "?";
797 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000798 OS << vni->def;
799 unsigned ee = vni->kills.size();
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000800 if (ee || vni->hasPHIKill) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000801 OS << "-(";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000802 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000803 OS << vni->kills[j];
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000804 if (j != ee-1)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000805 OS << " ";
806 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000807 if (vni->hasPHIKill) {
808 if (ee)
809 OS << " ";
810 OS << "phi";
811 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000812 OS << ")";
813 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000814 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000815 }
816 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000817}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000818
819void LiveInterval::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000820 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000821}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000822
823
Jeff Cohen4b607742006-12-16 02:15:42 +0000824void LiveRange::print(std::ostream &os) const {
825 os << *this;
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000826}