blob: 1b446e69d3d12467d26ee846aea2a2fda62f814d [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
Bob Wilson86af6552010-01-12 22:18:56 +000013// such that v is live at j' and there is no instruction with number i' < i such
Chris Lattnerfb449b92004-07-23 17:49:16 +000014// 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"
Lang Hames233a60e2009-11-03 23:52:08 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Cheng90f95f82009-06-14 20:22:55 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng0adb5272009-04-25 09:25:19 +000024#include "llvm/ADT/DenseMap.h"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000025#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000026#include "llvm/ADT/STLExtras.h"
David Greene52421542010-01-04 22:41:43 +000027#include "llvm/Support/Debug.h"
Daniel Dunbara717b7b2009-07-24 10:47:20 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000029#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000030#include <algorithm>
Chris Lattnerfb449b92004-07-23 17:49:16 +000031using namespace llvm;
32
John Wiegley6fd24722011-03-11 08:54:34 +000033// SlotIndexIterator - adapt an iterator over LiveRanges to look
34// like an iterator over SlotIndexes by accessing the .end member.
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000035namespace {
John Wiegley6fd24722011-03-11 08:54:34 +000036struct SlotIndexIterator
37 : std::iterator<std::random_access_iterator_tag, SlotIndex> {
38
39 SlotIndexIterator() {
Jakob Stoklund Olesenb64f6692011-03-03 04:23:52 +000040 }
John Wiegley6fd24722011-03-11 08:54:34 +000041
42 explicit SlotIndexIterator(LiveInterval::iterator it)
43 : it(it) {
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000044 }
John Wiegley6fd24722011-03-11 08:54:34 +000045
46 SlotIndexIterator(const SlotIndexIterator & that)
47 : it(that.it) {
Jakob Stoklund Olesen5bf76cd2011-03-08 19:37:54 +000048 }
John Wiegley6fd24722011-03-11 08:54:34 +000049
50 SlotIndexIterator & operator=(const SlotIndexIterator & that) {
51 it = that.it;
52 return *this;
53 }
54
55 SlotIndexIterator & operator++() {
56 ++it;
57 return *this;
58 }
59
60 SlotIndexIterator operator++(int) {
61 SlotIndexIterator that(*this);
62 ++*this;
63 return that;
64 }
65
66 SlotIndexIterator & operator--() {
67 --it;
68 return *this;
69 }
70
71 SlotIndexIterator operator--(int) {
72 SlotIndexIterator that(*this);
73 --*this;
74 return that;
75 }
76
77 SlotIndexIterator & operator+=(std::ptrdiff_t n) {
78 it += n;
79 return *this;
80 }
81
82 SlotIndexIterator & operator-=(std::ptrdiff_t n) {
83 it -= n;
84 return *this;
85 }
86
87 friend bool operator==(SlotIndexIterator lhs, SlotIndexIterator rhs) {
88 return lhs.it == rhs.it;
89 }
90
91 friend bool operator!=(SlotIndexIterator lhs, SlotIndexIterator rhs) {
92 return lhs.it != rhs.it;
93 }
94
95 friend bool operator<(SlotIndexIterator lhs, SlotIndexIterator rhs) {
96 return lhs.it < rhs.it;
97 }
98
99 friend bool operator<=(SlotIndexIterator lhs, SlotIndexIterator rhs) {
100 return lhs.it <= rhs.it;
101 }
102
103 friend bool operator>(SlotIndexIterator lhs, SlotIndexIterator rhs) {
104 return lhs.it > rhs.it;
105 }
106
107 friend bool operator>=(SlotIndexIterator lhs, SlotIndexIterator rhs) {
108 return lhs.it >= rhs.it;
109 }
110
111 friend SlotIndexIterator operator+(SlotIndexIterator that, std::ptrdiff_t n) {
112 return SlotIndexIterator(that.it + n);
113 }
114
115 friend SlotIndexIterator operator+(std::ptrdiff_t n, SlotIndexIterator that) {
116 return SlotIndexIterator(n + that.it);
117 }
118
119 friend SlotIndexIterator operator-(SlotIndexIterator that, std::ptrdiff_t n) {
120 return SlotIndexIterator(that.it - n);
121 }
122
123 friend std::ptrdiff_t operator-(SlotIndexIterator lhs, SlotIndexIterator rhs) {
124 return lhs.it - rhs.it;
125 }
126
127 reference operator*() const {
128 return it->end;
129 }
130
131 reference operator[](std::ptrdiff_t n) const {
132 return it[n].end;
133 }
134
135 pointer operator->() const {
136 return &it->end;
137 }
138
139 LiveInterval::iterator base() const {
140 return it;
141 }
142
143private:
144 LiveInterval::iterator it;
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +0000145};
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +0000146}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000147
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000148LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen201ecfc2010-10-05 18:48:55 +0000149 assert(Pos.isValid() && "Cannot search for an invalid index");
John Wiegley6fd24722011-03-11 08:54:34 +0000150 return std::upper_bound(
151 SlotIndexIterator(begin()),
152 SlotIndexIterator(end()), Pos).base();
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000153}
154
155/// killedInRange - Return true if the interval has kills in [Start,End).
156bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
157 Ranges::const_iterator r =
158 std::lower_bound(ranges.begin(), ranges.end(), End);
159
160 // Now r points to the first interval with start >= End, or ranges.end().
161 if (r == ranges.begin())
162 return false;
163
164 --r;
165 // Now r points to the last interval with end <= End.
166 // r->end is the kill point.
167 return r->end >= Start && r->end < End;
168}
169
Chris Lattnerbae74d92004-11-18 03:47:34 +0000170// overlaps - Return true if the intersection of the two live intervals is
171// not empty.
172//
Chris Lattnerfb449b92004-07-23 17:49:16 +0000173// An example for overlaps():
174//
175// 0: A = ...
176// 4: B = ...
177// 8: C = A + B ;; last use of A
178//
179// The live intervals should look like:
180//
181// A = [3, 11)
182// B = [7, x)
183// C = [11, y)
184//
185// A->overlaps(C) should return false since we want to be able to join
186// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +0000187//
188bool LiveInterval::overlapsFrom(const LiveInterval& other,
189 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +0000190 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +0000191 const_iterator i = begin();
192 const_iterator ie = end();
193 const_iterator j = StartPos;
194 const_iterator je = other.end();
195
196 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000197 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000198
Chris Lattnerfb449b92004-07-23 17:49:16 +0000199 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000200 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000201 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000202 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000203 ++StartPos;
204 if (StartPos != other.end() && StartPos->start <= i->start) {
205 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000206 j = std::upper_bound(j, je, i->start);
207 if (j != other.ranges.begin()) --j;
208 }
Chris Lattneraa141472004-07-23 18:40:00 +0000209 } else {
210 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000211 }
212
Chris Lattner9fddc122004-11-18 05:28:21 +0000213 if (j == je) return false;
214
215 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000216 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000217 std::swap(i, j);
218 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000219 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000220
221 if (i->end > j->start)
222 return true;
223 ++i;
224 }
225
226 return false;
227}
228
Evan Chengcccdb2b2009-04-18 08:52:15 +0000229/// overlaps - Return true if the live interval overlaps a range specified
230/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000231bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000232 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000233 const_iterator I = std::lower_bound(begin(), end(), End);
234 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000235}
236
Lang Hames6f4e4df2010-07-26 01:49:41 +0000237
238/// ValNo is dead, remove it. If it is the largest value number, just nuke it
239/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
240/// it can be nuked later.
241void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
242 if (ValNo->id == getNumValNums()-1) {
243 do {
244 valnos.pop_back();
245 } while (!valnos.empty() && valnos.back()->isUnused());
246 } else {
247 ValNo->setIsUnused(true);
248 }
249}
250
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000251/// RenumberValues - Renumber all values in order of appearance and delete the
252/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000253void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000254 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000255 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000256 valnos.clear();
257 for (const_iterator I = begin(), E = end(); I != E; ++I) {
258 VNInfo *VNI = I->valno;
259 if (!Seen.insert(VNI))
260 continue;
261 assert(!VNI->isUnused() && "Unused valno used by live range");
262 VNI->id = (unsigned)valnos.size();
263 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000264 VNI->setHasPHIKill(false);
265 if (VNI->isPHIDef())
266 seenPHIDef = true;
267 }
268
269 // Recompute phi kill flags.
270 if (!seenPHIDef)
271 return;
272 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
273 VNInfo *VNI = *I;
274 if (!VNI->isPHIDef())
275 continue;
276 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
277 assert(PHIBB && "No basic block for phi-def");
278 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
279 PE = PHIBB->pred_end(); PI != PE; ++PI) {
280 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
281 if (KVNI)
282 KVNI->setHasPHIKill(true);
283 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000284 }
285}
286
Chris Lattnerb26c2152004-07-23 19:38:44 +0000287/// extendIntervalEndTo - This method is used when we want to extend the range
288/// specified by I to end at the specified endpoint. To do this, we should
289/// merge and eliminate all ranges that this will overlap with. The iterator is
290/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000291void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000292 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000293 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000294
Chris Lattnerb26c2152004-07-23 19:38:44 +0000295 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000296 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000297 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000298 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000299 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000300
301 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
302 I->end = std::max(NewEnd, prior(MergeTo)->end);
303
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000304 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000305 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000306
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000307 // If the newly formed range now touches the range after it and if they have
308 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000309 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000310 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000311 I->end = Next->end;
312 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000313 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000314}
315
316
317/// extendIntervalStartTo - This method is used when we want to extend the range
318/// specified by I to start at the specified endpoint. To do this, we should
319/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000320LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000321LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000322 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000323 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000324
325 // Search for the first interval that we can't merge with.
326 Ranges::iterator MergeTo = I;
327 do {
328 if (MergeTo == ranges.begin()) {
329 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000330 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000331 return I;
332 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000333 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000334 --MergeTo;
335 } while (NewStart <= MergeTo->start);
336
337 // If we start in the middle of another interval, just delete a range and
338 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000339 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000340 MergeTo->end = I->end;
341 } else {
342 // Otherwise, extend the interval right after.
343 ++MergeTo;
344 MergeTo->start = NewStart;
345 MergeTo->end = I->end;
346 }
347
Oscar Fuentesee56c422010-08-02 06:00:15 +0000348 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000349 return MergeTo;
350}
351
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000352LiveInterval::iterator
353LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000354 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000355 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000356
357 // If the inserted interval starts in the middle or right at the end of
358 // another interval, just extend that interval to contain the range of LR.
359 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000360 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000361 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000362 if (B->start <= Start && B->end >= Start) {
363 extendIntervalEndTo(B, End);
364 return B;
365 }
366 } else {
367 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000368 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000369 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000370 "Cannot overlap two LiveRanges with differing ValID's"
371 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000372 }
373 }
374
375 // Otherwise, if this range ends in the middle of, or right next to, another
376 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000377 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000378 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000379 if (it->start <= End) {
380 it = extendIntervalStartTo(it, Start);
381
382 // If LR is a complete superset of an interval, we may need to grow its
383 // endpoint as well.
384 if (End > it->end)
385 extendIntervalEndTo(it, End);
386 return it;
387 }
388 } else {
389 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000390 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000391 assert(it->start >= End &&
392 "Cannot overlap two LiveRanges with differing ValID's");
393 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000394 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000395
396 // Otherwise, this is just a new range that doesn't interact with anything.
397 // Insert it.
398 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000399}
400
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000401/// extendInBlock - If this interval is live before UseIdx in the basic
402/// block that starts at StartIdx, extend it to be live at UseIdx and return
403/// the value. If there is no live range before UseIdx, return NULL.
404VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex UseIdx) {
405 if (empty())
406 return 0;
407 iterator I = std::upper_bound(begin(), end(), UseIdx);
408 if (I == begin())
409 return 0;
410 --I;
411 if (I->end <= StartIdx)
412 return 0;
413 if (I->end <= UseIdx)
414 extendIntervalEndTo(I, UseIdx.getNextSlot());
415 return I->valno;
416}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000417
418/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000419/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000420void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000421 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000422 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000423 Ranges::iterator I = find(Start);
424 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000425 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000426
427 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000428 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000429 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000430 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000431 if (RemoveDeadValNo) {
432 // Check if val# is dead.
433 bool isDead = true;
434 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
435 if (II != I && II->valno == ValNo) {
436 isDead = false;
437 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000438 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000439 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000440 // Now that ValNo is dead, remove it.
441 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000442 }
443 }
444
Chris Lattnerabf295f2004-07-24 02:52:23 +0000445 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000446 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000447 I->start = End;
448 return;
449 }
450
451 // Otherwise if the span we are removing is at the end of the LiveRange,
452 // adjust the other way.
453 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000454 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000455 return;
456 }
457
458 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000459 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000460 I->end = Start; // Trim the old interval.
461
462 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000463 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000464}
465
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000466/// removeValNo - Remove all the ranges defined by the specified value#.
467/// Also remove the value# from value# list.
468void LiveInterval::removeValNo(VNInfo *ValNo) {
469 if (empty()) return;
470 Ranges::iterator I = ranges.end();
471 Ranges::iterator E = ranges.begin();
472 do {
473 --I;
474 if (I->valno == ValNo)
475 ranges.erase(I);
476 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000477 // Now that ValNo is dead, remove it.
478 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000479}
Lang Hames86511252009-09-04 20:41:11 +0000480
Lang Hames86511252009-09-04 20:41:11 +0000481/// findDefinedVNInfo - Find the VNInfo defined by the specified
482/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000483VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000484 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000485 i != e; ++i) {
486 if ((*i)->def == Idx)
487 return *i;
488 }
489
490 return 0;
491}
492
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000493/// join - Join two live intervals (this, and other) together. This applies
494/// mappings to the value numbers in the LHS/RHS intervals as specified. If
495/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000496void LiveInterval::join(LiveInterval &Other,
497 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000498 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000499 SmallVector<VNInfo*, 16> &NewVNInfo,
500 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000501 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000502 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000503 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000504 unsigned NumVals = getNumValNums();
505 unsigned NumNewVals = NewVNInfo.size();
506 for (unsigned i = 0; i != NumVals; ++i) {
507 unsigned LHSValID = LHSValNoAssignments[i];
508 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000509 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000510 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000511 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000512
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000513 // If we have to apply a mapping to our base interval assignment, rewrite it
514 // now.
515 if (MustMapCurValNos) {
516 // Map the first live range.
517 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000518 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000519 ++OutIt;
520 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000521 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000522
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000523 // If this live range has the same value # as its immediate predecessor,
524 // and if they are neighbors, remove one LiveRange. This happens when we
525 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000526 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000527 (OutIt-1)->end = OutIt->end;
528 } else {
529 if (I != OutIt) {
530 OutIt->start = I->start;
531 OutIt->end = I->end;
532 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000533
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000534 // Didn't merge, on to the next one.
535 ++OutIt;
536 }
537 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000538
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000539 // If we merge some live ranges, chop off the end.
540 ranges.erase(OutIt, end());
541 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000542
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000543 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000544 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000545 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
546 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
547
548 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000549 // LiveInterval now. Also remove dead val#'s.
550 unsigned NumValNos = 0;
551 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000552 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000553 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000554 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000555 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000556 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000557 valnos[NumValNos] = VNI;
558 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000559 }
560 }
Evan Cheng34301352007-09-01 02:03:17 +0000561 if (NumNewVals < NumVals)
562 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000563
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000564 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000565 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000566 unsigned RangeNo = 0;
567 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
568 // Map the valno in the other live range to the current live range.
569 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000570 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000571 InsertPos = addRangeFrom(*I, InsertPos);
572 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000573
David Greene29ff37f2009-07-22 20:08:25 +0000574 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000575}
576
Chris Lattnerf21f0202006-09-02 05:26:59 +0000577/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
578/// interval as the specified value number. The LiveRanges in RHS are
579/// allowed to overlap with LiveRanges in the current interval, but only if
580/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000581void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000582 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000583 // TODO: Make this more efficient.
584 iterator InsertPos = begin();
585 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000586 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000587 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000588 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000589 InsertPos = addRangeFrom(Tmp, InsertPos);
590 }
591}
592
593
Evan Cheng32dfbea2007-10-12 08:50:34 +0000594/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
595/// in RHS into this live interval as the specified value number.
596/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000597/// current interval, it will replace the value numbers of the overlaped
598/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000599void LiveInterval::MergeValueInAsValue(
600 const LiveInterval &RHS,
601 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000602 SmallVector<VNInfo*, 4> ReplacedValNos;
603 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000604 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000605 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000606 if (I->valno != RHSValNo)
607 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000608 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000609 IP = std::upper_bound(IP, end(), Start);
610 // If the start of this range overlaps with an existing liverange, trim it.
611 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000612 if (IP[-1].valno != LHSValNo) {
613 ReplacedValNos.push_back(IP[-1].valno);
614 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000615 }
616 Start = IP[-1].end;
617 // Trimmed away the whole range?
618 if (Start >= End) continue;
619 }
620 // If the end of this range overlaps with an existing liverange, trim it.
621 if (IP != end() && End > IP->start) {
622 if (IP->valno != LHSValNo) {
623 ReplacedValNos.push_back(IP->valno);
624 IP->valno = LHSValNo; // Update val#.
625 }
626 End = IP->start;
627 // If this trimmed away the whole range, ignore it.
628 if (Start == End) continue;
629 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000630
Evan Cheng32dfbea2007-10-12 08:50:34 +0000631 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000632 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
633 }
634
635
636 SmallSet<VNInfo*, 4> Seen;
637 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
638 VNInfo *V1 = ReplacedValNos[i];
639 if (Seen.insert(V1)) {
640 bool isDead = true;
641 for (const_iterator I = begin(), E = end(); I != E; ++I)
642 if (I->valno == V1) {
643 isDead = false;
644 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000645 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000646 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000647 // Now that V1 is dead, remove it.
648 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000649 }
650 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000651 }
652}
653
654
Evan Chenga2e64352009-03-11 00:03:21 +0000655
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000656/// MergeValueNumberInto - This method is called when two value nubmers
657/// are found to be equivalent. This eliminates V1, replacing all
658/// LiveRanges with the V1 value number with the V2 value number. This can
659/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000660VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000661 assert(V1 != V2 && "Identical value#'s are always equivalent!");
662
663 // This code actually merges the (numerically) larger value number into the
664 // smaller value number, which is likely to allow us to compactify the value
665 // space. The only thing we have to be careful of is to preserve the
666 // instruction that defines the result value.
667
668 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000669 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000670 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000671 std::swap(V1, V2);
672 }
673
674 // Merge V1 live ranges into V2.
675 for (iterator I = begin(); I != end(); ) {
676 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000677 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000678
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000679 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
680 // range, extend it.
681 if (LR != begin()) {
682 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000683 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000684 Prev->end = LR->end;
685
686 // Erase this live-range.
687 ranges.erase(LR);
688 I = Prev+1;
689 LR = Prev;
690 }
691 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000692
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000693 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
694 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000695 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000696
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000697 // If we can merge it into later V2 live ranges, do so now. We ignore any
698 // following V1 live ranges, as they will be merged in subsequent iterations
699 // of the loop.
700 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000701 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000702 LR->end = I->end;
703 ranges.erase(I);
704 I = LR+1;
705 }
706 }
707 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000708
Jakob Stoklund Olesene0a73ec2010-10-01 23:52:25 +0000709 // Merge the relevant flags.
710 V2->mergeFlags(V1);
711
Lang Hames6f4e4df2010-07-26 01:49:41 +0000712 // Now that V1 is dead, remove it.
713 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000714
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000715 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000716}
717
Evan Cheng32dfbea2007-10-12 08:50:34 +0000718void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000719 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000720 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000721 ranges.clear();
722 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000723 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000724 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
725
Evan Cheng32dfbea2007-10-12 08:50:34 +0000726 weight = RHS.weight;
727 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
728 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000729 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000730 }
731 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
732 const LiveRange &LR = RHS.ranges[i];
733 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
734 }
735}
736
Evan Chenge52eef82007-04-17 20:25:11 +0000737unsigned LiveInterval::getSize() const {
738 unsigned Sum = 0;
739 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000740 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000741 return Sum;
742}
743
David Greene29ff37f2009-07-22 20:08:25 +0000744/// ComputeJoinedWeight - Set the weight of a live interval Joined
745/// after Other has been merged into it.
746void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
747 // If either of these intervals was spilled, the weight is the
748 // weight of the non-spilled interval. This can only happen with
749 // iterative coalescers.
750
David Greene92b78bb2009-07-22 22:32:19 +0000751 if (Other.weight != HUGE_VALF) {
752 weight += Other.weight;
753 }
754 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000755 !TargetRegisterInfo::isPhysicalRegister(reg)) {
756 // Remove this assert if you have an iterative coalescer
757 assert(0 && "Joining to spilled interval");
758 weight = Other.weight;
759 }
David Greene29ff37f2009-07-22 20:08:25 +0000760 else {
761 // Otherwise the weight stays the same
762 // Remove this assert if you have an iterative coalescer
763 assert(0 && "Joining from spilled interval");
764 }
765}
766
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000767raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
768 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
769}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000770
Chris Lattnerabf295f2004-07-24 02:52:23 +0000771void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000772 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000773}
774
Chris Lattnerc02497f2009-08-23 03:47:42 +0000775void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000776 OS << PrintReg(reg, TRI);
777 if (weight != 0)
778 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000779
Chris Lattner38135af2005-05-14 05:34:15 +0000780 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000781 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000782 else {
783 OS << " = ";
784 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000785 E = ranges.end(); I != E; ++I) {
786 OS << *I;
787 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
788 }
Chris Lattner38135af2005-05-14 05:34:15 +0000789 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000790
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000791 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000792 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000793 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000794 unsigned vnum = 0;
795 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
796 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000797 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000798 if (vnum) OS << " ";
799 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000800 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000801 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000802 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000803 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000804 if (vni->isPHIDef())
805 OS << "-phidef";
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000806 if (vni->hasPHIKill())
807 OS << "-phikill";
808 if (vni->hasRedefByEC())
809 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000810 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000811 }
812 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000813}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000814
815void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000816 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000817}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000818
819
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000820void LiveRange::print(raw_ostream &os) const {
821 os << *this;
822}
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000823
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000824unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000825 // Create initial equivalence classes.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000826 eqClass_.clear();
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000827 eqClass_.grow(LI->getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000828
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000829 const VNInfo *used = 0, *unused = 0;
830
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000831 // Determine connections.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000832 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
833 I != E; ++I) {
834 const VNInfo *VNI = *I;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000835 // Group all unused values into one class.
836 if (VNI->isUnused()) {
837 if (unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000838 eqClass_.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000839 unused = VNI;
840 continue;
841 }
842 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000843 if (VNI->isPHIDef()) {
844 const MachineBasicBlock *MBB = lis_.getMBBFromIndex(VNI->def);
845 assert(MBB && "Phi-def has no defining MBB");
846 // Connect to values live out of predecessors.
847 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
848 PE = MBB->pred_end(); PI != PE; ++PI)
849 if (const VNInfo *PVNI =
850 LI->getVNInfoAt(lis_.getMBBEndIdx(*PI).getPrevSlot()))
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000851 eqClass_.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000852 } else {
853 // Normal value defined by an instruction. Check for two-addr redef.
854 // FIXME: This could be coincidental. Should we really check for a tied
855 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000856 // Note that VNI->def may be a use slot for an early clobber def.
857 if (const VNInfo *UVNI = LI->getVNInfoAt(VNI->def.getPrevSlot()))
858 eqClass_.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000859 }
860 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000861
862 // Lump all the unused values in with the last used value.
863 if (used && unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000864 eqClass_.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000865
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000866 eqClass_.compress();
867 return eqClass_.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000868}
869
870void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[]) {
871 assert(LIV[0] && "LIV[0] must be set");
872 LiveInterval &LI = *LIV[0];
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000873
874 // First move runs to new intervals.
875 LiveInterval::iterator J = LI.begin(), E = LI.end();
876 while (J != E && eqClass_[J->valno->id] == 0)
877 ++J;
878 for (LiveInterval::iterator I = J; I != E; ++I) {
879 if (unsigned eq = eqClass_[I->valno->id]) {
Benjamin Kramerccefe322010-10-09 16:36:44 +0000880 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000881 "New intervals should be empty");
882 LIV[eq]->ranges.push_back(*I);
883 } else
884 *J++ = *I;
885 }
886 LI.ranges.erase(J, E);
887
888 // Transfer VNInfos to their new owners and renumber them.
889 unsigned j = 0, e = LI.getNumValNums();
890 while (j != e && eqClass_[j] == 0)
891 ++j;
892 for (unsigned i = j; i != e; ++i) {
893 VNInfo *VNI = LI.getValNumInfo(i);
894 if (unsigned eq = eqClass_[i]) {
895 VNI->id = LIV[eq]->getNumValNums();
896 LIV[eq]->valnos.push_back(VNI);
897 } else {
898 VNI->id = j;
899 LI.valnos[j++] = VNI;
900 }
901 }
902 LI.valnos.resize(j);
903}