blob: 01077db92fd62c24d4fc2d2f839c1087e5c44e7d [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
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000033LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen55768d72011-03-12 01:50:35 +000034 // This algorithm is basically std::upper_bound.
35 // Unfortunately, std::upper_bound cannot be used with mixed types until we
36 // adopt C++0x. Many libraries can do it, but not all.
37 if (empty() || Pos >= endIndex())
38 return end();
39 iterator I = begin();
40 size_t Len = ranges.size();
41 do {
42 size_t Mid = Len >> 1;
43 if (Pos < I[Mid].end)
44 Len = Mid;
45 else
46 I += Mid + 1, Len -= Mid + 1;
47 } while (Len);
48 return I;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000049}
50
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000051VNInfo *LiveInterval::createDeadDef(SlotIndex Def,
52 VNInfo::Allocator &VNInfoAllocator) {
53 assert(!Def.isDead() && "Cannot define a value at the dead slot");
54 iterator I = find(Def);
55 if (I == end()) {
56 VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
57 ranges.push_back(LiveRange(Def, Def.getDeadSlot(), VNI));
58 return VNI;
59 }
60 if (SlotIndex::isSameInstr(Def, I->start)) {
61 assert(I->start == Def && "Cannot insert def, already live");
62 assert(I->valno->def == Def && "Inconsistent existing value def");
63 return I->valno;
64 }
65 assert(SlotIndex::isEarlierInstr(Def, I->start) && "Already live at def");
66 VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
67 ranges.insert(I, LiveRange(Def, Def.getDeadSlot(), VNI));
68 return VNI;
69}
70
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000071/// killedInRange - Return true if the interval has kills in [Start,End).
72bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
73 Ranges::const_iterator r =
74 std::lower_bound(ranges.begin(), ranges.end(), End);
75
76 // Now r points to the first interval with start >= End, or ranges.end().
77 if (r == ranges.begin())
78 return false;
79
80 --r;
81 // Now r points to the last interval with end <= End.
82 // r->end is the kill point.
83 return r->end >= Start && r->end < End;
84}
85
Chris Lattnerbae74d92004-11-18 03:47:34 +000086// overlaps - Return true if the intersection of the two live intervals is
87// not empty.
88//
Chris Lattnerfb449b92004-07-23 17:49:16 +000089// An example for overlaps():
90//
91// 0: A = ...
92// 4: B = ...
93// 8: C = A + B ;; last use of A
94//
95// The live intervals should look like:
96//
97// A = [3, 11)
98// B = [7, x)
99// C = [11, y)
100//
101// A->overlaps(C) should return false since we want to be able to join
102// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +0000103//
104bool LiveInterval::overlapsFrom(const LiveInterval& other,
105 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +0000106 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +0000107 const_iterator i = begin();
108 const_iterator ie = end();
109 const_iterator j = StartPos;
110 const_iterator je = other.end();
111
112 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000113 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000114
Chris Lattnerfb449b92004-07-23 17:49:16 +0000115 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000116 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000117 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000118 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000119 ++StartPos;
120 if (StartPos != other.end() && StartPos->start <= i->start) {
121 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000122 j = std::upper_bound(j, je, i->start);
123 if (j != other.ranges.begin()) --j;
124 }
Chris Lattneraa141472004-07-23 18:40:00 +0000125 } else {
126 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000127 }
128
Chris Lattner9fddc122004-11-18 05:28:21 +0000129 if (j == je) return false;
130
131 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000132 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000133 std::swap(i, j);
134 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000135 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000136
137 if (i->end > j->start)
138 return true;
139 ++i;
140 }
141
142 return false;
143}
144
Evan Chengcccdb2b2009-04-18 08:52:15 +0000145/// overlaps - Return true if the live interval overlaps a range specified
146/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000147bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000148 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000149 const_iterator I = std::lower_bound(begin(), end(), End);
150 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000151}
152
Lang Hames6f4e4df2010-07-26 01:49:41 +0000153
154/// ValNo is dead, remove it. If it is the largest value number, just nuke it
155/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
156/// it can be nuked later.
157void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
158 if (ValNo->id == getNumValNums()-1) {
159 do {
160 valnos.pop_back();
161 } while (!valnos.empty() && valnos.back()->isUnused());
162 } else {
163 ValNo->setIsUnused(true);
164 }
165}
166
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000167/// RenumberValues - Renumber all values in order of appearance and delete the
168/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000169void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000170 SmallPtrSet<VNInfo*, 8> Seen;
171 valnos.clear();
172 for (const_iterator I = begin(), E = end(); I != E; ++I) {
173 VNInfo *VNI = I->valno;
174 if (!Seen.insert(VNI))
175 continue;
176 assert(!VNI->isUnused() && "Unused valno used by live range");
177 VNI->id = (unsigned)valnos.size();
178 valnos.push_back(VNI);
179 }
180}
181
Chris Lattnerb26c2152004-07-23 19:38:44 +0000182/// extendIntervalEndTo - This method is used when we want to extend the range
183/// specified by I to end at the specified endpoint. To do this, we should
184/// merge and eliminate all ranges that this will overlap with. The iterator is
185/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000186void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000187 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000188 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000189
Chris Lattnerb26c2152004-07-23 19:38:44 +0000190 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000191 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000192 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000193 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000194 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000195
196 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
197 I->end = std::max(NewEnd, prior(MergeTo)->end);
198
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000199 // If the newly formed range now touches the range after it and if they have
200 // the same value number, merge the two ranges into one range.
Chandler Carruth95c88b82012-07-05 12:40:45 +0000201 if (MergeTo != ranges.end() && MergeTo->start <= I->end &&
202 MergeTo->valno == ValNo) {
203 I->end = MergeTo->end;
204 ++MergeTo;
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000205 }
Chandler Carruth95c88b82012-07-05 12:40:45 +0000206
207 // Erase any dead ranges.
208 ranges.erase(llvm::next(I), MergeTo);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000209}
210
211
212/// extendIntervalStartTo - This method is used when we want to extend the range
213/// specified by I to start at the specified endpoint. To do this, we should
214/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000216LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000217 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000218 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000219
220 // Search for the first interval that we can't merge with.
221 Ranges::iterator MergeTo = I;
222 do {
223 if (MergeTo == ranges.begin()) {
224 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000225 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000226 return I;
227 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000228 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000229 --MergeTo;
230 } while (NewStart <= MergeTo->start);
231
232 // If we start in the middle of another interval, just delete a range and
233 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000234 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000235 MergeTo->end = I->end;
236 } else {
237 // Otherwise, extend the interval right after.
238 ++MergeTo;
239 MergeTo->start = NewStart;
240 MergeTo->end = I->end;
241 }
242
Oscar Fuentesee56c422010-08-02 06:00:15 +0000243 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000244 return MergeTo;
245}
246
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000247LiveInterval::iterator
248LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000249 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000250 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000251
252 // If the inserted interval starts in the middle or right at the end of
253 // another interval, just extend that interval to contain the range of LR.
254 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000255 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000256 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000257 if (B->start <= Start && B->end >= Start) {
258 extendIntervalEndTo(B, End);
259 return B;
260 }
261 } else {
262 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000263 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000264 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000265 "Cannot overlap two LiveRanges with differing ValID's"
266 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000267 }
268 }
269
270 // Otherwise, if this range ends in the middle of, or right next to, another
271 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000272 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000273 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000274 if (it->start <= End) {
275 it = extendIntervalStartTo(it, Start);
276
277 // If LR is a complete superset of an interval, we may need to grow its
278 // endpoint as well.
279 if (End > it->end)
280 extendIntervalEndTo(it, End);
281 return it;
282 }
283 } else {
284 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000285 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000286 assert(it->start >= End &&
287 "Cannot overlap two LiveRanges with differing ValID's");
288 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000289 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000290
291 // Otherwise, this is just a new range that doesn't interact with anything.
292 // Insert it.
293 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000294}
295
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000296/// extendInBlock - If this interval is live before Kill in the basic
297/// block that starts at StartIdx, extend it to be live up to Kill and return
298/// the value. If there is no live range before Kill, return NULL.
299VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000300 if (empty())
301 return 0;
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000302 iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot());
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000303 if (I == begin())
304 return 0;
305 --I;
306 if (I->end <= StartIdx)
307 return 0;
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000308 if (I->end < Kill)
309 extendIntervalEndTo(I, Kill);
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000310 return I->valno;
311}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000312
313/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000314/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000315void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000316 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000317 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000318 Ranges::iterator I = find(Start);
319 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000320 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000321
322 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000323 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000324 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000325 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000326 if (RemoveDeadValNo) {
327 // Check if val# is dead.
328 bool isDead = true;
329 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
330 if (II != I && II->valno == ValNo) {
331 isDead = false;
332 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000333 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000334 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000335 // Now that ValNo is dead, remove it.
336 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000337 }
338 }
339
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000341 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000342 I->start = End;
343 return;
344 }
345
346 // Otherwise if the span we are removing is at the end of the LiveRange,
347 // adjust the other way.
348 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000349 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000350 return;
351 }
352
353 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000354 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000355 I->end = Start; // Trim the old interval.
356
357 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000358 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000359}
360
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000361/// removeValNo - Remove all the ranges defined by the specified value#.
362/// Also remove the value# from value# list.
363void LiveInterval::removeValNo(VNInfo *ValNo) {
364 if (empty()) return;
365 Ranges::iterator I = ranges.end();
366 Ranges::iterator E = ranges.begin();
367 do {
368 --I;
369 if (I->valno == ValNo)
370 ranges.erase(I);
371 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000372 // Now that ValNo is dead, remove it.
373 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000374}
Lang Hames86511252009-09-04 20:41:11 +0000375
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000376/// join - Join two live intervals (this, and other) together. This applies
377/// mappings to the value numbers in the LHS/RHS intervals as specified. If
378/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000379void LiveInterval::join(LiveInterval &Other,
380 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000381 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000382 SmallVector<VNInfo*, 16> &NewVNInfo,
383 MachineRegisterInfo *MRI) {
Chandler Carruth261b6332012-07-10 05:06:03 +0000384 verify();
385
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000386 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000387 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000388 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000389 unsigned NumVals = getNumValNums();
390 unsigned NumNewVals = NewVNInfo.size();
391 for (unsigned i = 0; i != NumVals; ++i) {
392 unsigned LHSValID = LHSValNoAssignments[i];
393 if (i != LHSValID ||
Lang Hamesd88710a2012-02-02 06:55:45 +0000394 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000395 MustMapCurValNos = true;
Lang Hamesd88710a2012-02-02 06:55:45 +0000396 break;
397 }
Chris Lattnerdeb99712004-07-24 03:41:50 +0000398 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000399
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000400 // If we have to apply a mapping to our base interval assignment, rewrite it
401 // now.
402 if (MustMapCurValNos) {
403 // Map the first live range.
Lang Hames02e08d52012-02-02 05:37:34 +0000404
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000405 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000406 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Lang Hames02e08d52012-02-02 05:37:34 +0000407 for (iterator I = next(OutIt), E = end(); I != E; ++I) {
408 VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
409 assert(nextValNo != 0 && "Huh?");
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000410
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000411 // If this live range has the same value # as its immediate predecessor,
412 // and if they are neighbors, remove one LiveRange. This happens when we
Lang Hames02e08d52012-02-02 05:37:34 +0000413 // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
414 if (OutIt->valno == nextValNo && OutIt->end == I->start) {
415 OutIt->end = I->end;
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000416 } else {
Lang Hames02e08d52012-02-02 05:37:34 +0000417 // Didn't merge. Move OutIt to the next interval,
418 ++OutIt;
419 OutIt->valno = nextValNo;
420 if (OutIt != I) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000421 OutIt->start = I->start;
422 OutIt->end = I->end;
423 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000424 }
425 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000426 // If we merge some live ranges, chop off the end.
Lang Hames02e08d52012-02-02 05:37:34 +0000427 ++OutIt;
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000428 ranges.erase(OutIt, end());
429 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000430
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000431 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000432 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000433 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
434 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
435
436 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000437 // LiveInterval now. Also remove dead val#'s.
438 unsigned NumValNos = 0;
439 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000440 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000441 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000442 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000443 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000444 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000445 valnos[NumValNos] = VNI;
446 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000447 }
448 }
Evan Cheng34301352007-09-01 02:03:17 +0000449 if (NumNewVals < NumVals)
450 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000451
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000452 // Okay, now insert the RHS live ranges into the LHS.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000453 unsigned RangeNo = 0;
454 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
455 // Map the valno in the other live range to the current live range.
456 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000457 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000458 }
Chandler Carruth4e996de2012-07-10 22:25:21 +0000459 mergeIntervalRanges(Other);
Chandler Carruth261b6332012-07-10 05:06:03 +0000460
461 verify();
Chris Lattnerfb449b92004-07-23 17:49:16 +0000462}
463
Chandler Carruthe585e752012-07-10 05:16:17 +0000464/// \brief Helper function for merging in another LiveInterval's ranges.
465///
466/// This is a helper routine implementing an efficient merge of another
467/// LiveIntervals ranges into the current interval.
468///
Chandler Carruth4e996de2012-07-10 22:25:21 +0000469/// \param LHSValNo If non-NULL, set as the new value number for every range
470/// from RHS which is merged into the LHS.
Chandler Carruthe585e752012-07-10 05:16:17 +0000471/// \param RHSValNo If non-NULL, then only ranges in RHS whose original value
472/// number maches this value number will be merged into LHS.
473void LiveInterval::mergeIntervalRanges(const LiveInterval &RHS,
474 VNInfo *LHSValNo,
475 const VNInfo *RHSValNo) {
476 if (RHS.empty())
477 return;
478
Chandler Carruth4e996de2012-07-10 22:25:21 +0000479 // Ensure we're starting with a valid range. Note that we don't verify RHS
480 // because it may have had its value numbers adjusted in preparation for
481 // merging.
Chandler Carruthe585e752012-07-10 05:16:17 +0000482 verify();
Chandler Carruthe585e752012-07-10 05:16:17 +0000483
484 // The strategy for merging these efficiently is as follows:
485 //
486 // 1) Find the beginning of the impacted ranges in the LHS.
487 // 2) Create a new, merged sub-squence of ranges merging from the position in
488 // #1 until either LHS or RHS is exhausted. Any part of LHS between RHS
489 // entries being merged will be copied into this new range.
490 // 3) Replace the relevant section in LHS with these newly merged ranges.
491 // 4) Append any remaning ranges from RHS if LHS is exhausted in #2.
492 //
493 // We don't follow the typical in-place merge strategy for sorted ranges of
494 // appending the new ranges to the back and then using std::inplace_merge
495 // because one step of the merge can both mutate the original elements and
496 // remove elements from the original. Essentially, because the merge includes
497 // collapsing overlapping ranges, a more complex approach is required.
498
499 // We do an initial binary search to optimize for a common pattern: a large
500 // LHS, and a very small RHS.
501 const_iterator RI = RHS.begin(), RE = RHS.end();
502 iterator LE = end(), LI = std::upper_bound(begin(), LE, *RI);
503
504 // Merge into NewRanges until one of the ranges is exhausted.
505 SmallVector<LiveRange, 4> NewRanges;
506
507 // Keep track of where to begin the replacement.
508 iterator ReplaceI = LI;
509
510 // If there are preceding ranges in the LHS, put the last one into NewRanges
511 // so we can optionally extend it. Adjust the replacement point accordingly.
512 if (LI != begin()) {
513 ReplaceI = llvm::prior(LI);
514 NewRanges.push_back(*ReplaceI);
515 }
516
517 // Now loop over the mergable portions of both LHS and RHS, merging into
518 // NewRanges.
519 while (LI != LE && RI != RE) {
520 // Skip incoming ranges with the wrong value.
521 if (RHSValNo && RI->valno != RHSValNo) {
522 ++RI;
523 continue;
524 }
525
526 // Select the first range. We pick the earliest start point, and then the
527 // largest range.
528 LiveRange R = *LI;
529 if (*RI < R) {
530 R = *RI;
531 ++RI;
Chandler Carruth4e996de2012-07-10 22:25:21 +0000532 if (LHSValNo)
533 R.valno = LHSValNo;
Chandler Carruthe585e752012-07-10 05:16:17 +0000534 } else {
535 ++LI;
536 }
537
538 if (NewRanges.empty()) {
539 NewRanges.push_back(R);
540 continue;
541 }
542
543 LiveRange &LastR = NewRanges.back();
544 if (R.valno == LastR.valno) {
545 // Try to merge this range into the last one.
546 if (R.start <= LastR.end) {
547 LastR.end = std::max(LastR.end, R.end);
548 continue;
549 }
550 } else {
551 // We can't merge ranges across a value number.
552 assert(R.start >= LastR.end &&
553 "Cannot overlap two LiveRanges with differing ValID's");
554 }
555
556 // If all else fails, just append the range.
557 NewRanges.push_back(R);
558 }
559 assert(RI == RE || LI == LE);
560
561 // Check for being able to merge into the trailing sequence of ranges on the LHS.
562 if (!NewRanges.empty())
563 for (; LI != LE && (LI->valno == NewRanges.back().valno &&
564 LI->start <= NewRanges.back().end);
565 ++LI)
566 NewRanges.back().end = std::max(NewRanges.back().end, LI->end);
567
568 // Replace the ranges in the LHS with the newly merged ones. It would be
569 // really nice if there were a move-supporting 'replace' directly in
570 // SmallVector, but as there is not, we pay the price of copies to avoid
571 // wasted memory allocations.
572 SmallVectorImpl<LiveRange>::iterator NRI = NewRanges.begin(),
573 NRE = NewRanges.end();
574 for (; ReplaceI != LI && NRI != NRE; ++ReplaceI, ++NRI)
575 *ReplaceI = *NRI;
576 if (NRI == NRE)
577 ranges.erase(ReplaceI, LI);
578 else
579 ranges.insert(LI, NRI, NRE);
580
581 // And finally insert any trailing end of RHS (if we have one).
Chandler Carruth4e996de2012-07-10 22:25:21 +0000582 for (; RI != RE; ++RI) {
583 LiveRange R = *RI;
584 if (LHSValNo)
585 R.valno = LHSValNo;
Chandler Carruth1b8da1d2012-07-10 15:41:33 +0000586 if (!ranges.empty() &&
Chandler Carruth4e996de2012-07-10 22:25:21 +0000587 ranges.back().valno == R.valno && R.start <= ranges.back().end)
588 ranges.back().end = std::max(ranges.back().end, R.end);
589 else
590 ranges.push_back(R);
591 }
Chandler Carruthe585e752012-07-10 05:16:17 +0000592
593 // Ensure we finished with a valid new sequence of ranges.
594 verify();
595}
596
Chris Lattnerf21f0202006-09-02 05:26:59 +0000597/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
598/// interval as the specified value number. The LiveRanges in RHS are
599/// allowed to overlap with LiveRanges in the current interval, but only if
600/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000601void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000602 VNInfo *LHSValNo) {
Chandler Carruthe585e752012-07-10 05:16:17 +0000603 mergeIntervalRanges(RHS, LHSValNo);
Chris Lattnerf21f0202006-09-02 05:26:59 +0000604}
605
Evan Cheng32dfbea2007-10-12 08:50:34 +0000606/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
607/// in RHS into this live interval as the specified value number.
608/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000609/// current interval, it will replace the value numbers of the overlaped
610/// live ranges with the specified value number.
Chandler Carruthe585e752012-07-10 05:16:17 +0000611void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
612 const VNInfo *RHSValNo,
613 VNInfo *LHSValNo) {
614 mergeIntervalRanges(RHS, LHSValNo, RHSValNo);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000615}
616
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000617/// MergeValueNumberInto - This method is called when two value nubmers
618/// are found to be equivalent. This eliminates V1, replacing all
619/// LiveRanges with the V1 value number with the V2 value number. This can
620/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000621VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000622 assert(V1 != V2 && "Identical value#'s are always equivalent!");
623
624 // This code actually merges the (numerically) larger value number into the
625 // smaller value number, which is likely to allow us to compactify the value
626 // space. The only thing we have to be careful of is to preserve the
627 // instruction that defines the result value.
628
629 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000630 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000631 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000632 std::swap(V1, V2);
633 }
634
635 // Merge V1 live ranges into V2.
636 for (iterator I = begin(); I != end(); ) {
637 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000638 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000639
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000640 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
641 // range, extend it.
642 if (LR != begin()) {
643 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000644 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000645 Prev->end = LR->end;
646
647 // Erase this live-range.
648 ranges.erase(LR);
649 I = Prev+1;
650 LR = Prev;
651 }
652 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000653
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000654 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
655 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000656 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000657
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000658 // If we can merge it into later V2 live ranges, do so now. We ignore any
659 // following V1 live ranges, as they will be merged in subsequent iterations
660 // of the loop.
661 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000662 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000663 LR->end = I->end;
664 ranges.erase(I);
665 I = LR+1;
666 }
667 }
668 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000669
Jakob Stoklund Olesene0a73ec2010-10-01 23:52:25 +0000670 // Merge the relevant flags.
671 V2->mergeFlags(V1);
672
Lang Hames6f4e4df2010-07-26 01:49:41 +0000673 // Now that V1 is dead, remove it.
674 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000675
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000676 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000677}
678
Evan Cheng32dfbea2007-10-12 08:50:34 +0000679void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000680 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000681 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000682 ranges.clear();
683 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000684 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000685 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
686
Evan Cheng32dfbea2007-10-12 08:50:34 +0000687 weight = RHS.weight;
688 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
689 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000690 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000691 }
692 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
693 const LiveRange &LR = RHS.ranges[i];
694 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
695 }
Chandler Carruth261b6332012-07-10 05:06:03 +0000696
697 verify();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000698}
699
Evan Chenge52eef82007-04-17 20:25:11 +0000700unsigned LiveInterval::getSize() const {
701 unsigned Sum = 0;
702 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000703 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000704 return Sum;
705}
706
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000707raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
708 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
709}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000710
Chris Lattnerabf295f2004-07-24 02:52:23 +0000711void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000712 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000713}
714
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000715void LiveInterval::print(raw_ostream &OS) const {
Chris Lattner38135af2005-05-14 05:34:15 +0000716 if (empty())
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000717 OS << "EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000718 else {
Chris Lattner38135af2005-05-14 05:34:15 +0000719 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000720 E = ranges.end(); I != E; ++I) {
721 OS << *I;
722 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
723 }
Chris Lattner38135af2005-05-14 05:34:15 +0000724 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000725
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000726 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000727 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000728 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000729 unsigned vnum = 0;
730 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
731 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000732 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000733 if (vnum) OS << " ";
734 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000735 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000736 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000737 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000738 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000739 if (vni->isPHIDef())
740 OS << "-phidef";
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000741 if (vni->hasPHIKill())
742 OS << "-phikill";
Evan Chenga8d94f12007-08-07 23:49:57 +0000743 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000744 }
745 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000746}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000747
748void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000749 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000750}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000751
Chandler Carruth261b6332012-07-10 05:06:03 +0000752#ifndef NDEBUG
753void LiveInterval::verify() const {
754 for (const_iterator I = begin(), E = end(); I != E; ++I) {
755 assert(I->start.isValid());
756 assert(I->end.isValid());
757 assert(I->start < I->end);
758 assert(I->valno != 0);
759 assert(I->valno == valnos[I->valno->id]);
760 if (llvm::next(I) != E) {
761 assert(I->end <= llvm::next(I)->start);
762 if (I->end == llvm::next(I)->start)
763 assert(I->valno != llvm::next(I)->valno);
764 }
765 }
766}
767#endif
768
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000769
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000770void LiveRange::print(raw_ostream &os) const {
771 os << *this;
772}
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000773
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000774unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000775 // Create initial equivalence classes.
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000776 EqClass.clear();
777 EqClass.grow(LI->getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000778
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000779 const VNInfo *used = 0, *unused = 0;
780
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000781 // Determine connections.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000782 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
783 I != E; ++I) {
784 const VNInfo *VNI = *I;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000785 // Group all unused values into one class.
786 if (VNI->isUnused()) {
787 if (unused)
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000788 EqClass.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000789 unused = VNI;
790 continue;
791 }
792 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000793 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000794 const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000795 assert(MBB && "Phi-def has no defining MBB");
796 // Connect to values live out of predecessors.
797 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
798 PE = MBB->pred_end(); PI != PE; ++PI)
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000799 if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000800 EqClass.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000801 } else {
802 // Normal value defined by an instruction. Check for two-addr redef.
803 // FIXME: This could be coincidental. Should we really check for a tied
804 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000805 // Note that VNI->def may be a use slot for an early clobber def.
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000806 if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def))
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000807 EqClass.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000808 }
809 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000810
811 // Lump all the unused values in with the last used value.
812 if (used && unused)
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000813 EqClass.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000814
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000815 EqClass.compress();
816 return EqClass.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000817}
818
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000819void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
820 MachineRegisterInfo &MRI) {
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000821 assert(LIV[0] && "LIV[0] must be set");
822 LiveInterval &LI = *LIV[0];
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000823
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000824 // Rewrite instructions.
825 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
826 RE = MRI.reg_end(); RI != RE;) {
827 MachineOperand &MO = RI.getOperand();
828 MachineInstr *MI = MO.getParent();
829 ++RI;
830 if (MO.isUse() && MO.isUndef())
831 continue;
832 // DBG_VALUE instructions should have been eliminated earlier.
833 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000834 Idx = Idx.getRegSlot(MO.isUse());
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000835 const VNInfo *VNI = LI.getVNInfoAt(Idx);
Jakob Stoklund Olesenbd6f44a2012-05-19 05:25:50 +0000836 // FIXME: We should be able to assert(VNI) here, but the coalescer leaves
837 // dangling defs around.
838 if (!VNI)
839 continue;
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000840 MO.setReg(LIV[getEqClass(VNI)]->reg);
841 }
842
843 // Move runs to new intervals.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000844 LiveInterval::iterator J = LI.begin(), E = LI.end();
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000845 while (J != E && EqClass[J->valno->id] == 0)
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000846 ++J;
847 for (LiveInterval::iterator I = J; I != E; ++I) {
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000848 if (unsigned eq = EqClass[I->valno->id]) {
Benjamin Kramerccefe322010-10-09 16:36:44 +0000849 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000850 "New intervals should be empty");
851 LIV[eq]->ranges.push_back(*I);
852 } else
853 *J++ = *I;
854 }
855 LI.ranges.erase(J, E);
856
857 // Transfer VNInfos to their new owners and renumber them.
858 unsigned j = 0, e = LI.getNumValNums();
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000859 while (j != e && EqClass[j] == 0)
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000860 ++j;
861 for (unsigned i = j; i != e; ++i) {
862 VNInfo *VNI = LI.getValNumInfo(i);
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000863 if (unsigned eq = EqClass[i]) {
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000864 VNI->id = LIV[eq]->getNumValNums();
865 LIV[eq]->valnos.push_back(VNI);
866 } else {
867 VNI->id = j;
868 LI.valnos[j++] = VNI;
869 }
870 }
871 LI.valnos.resize(j);
872}