blob: c3bf2d234c0a0835ed4f306331c815e8d962cbbb [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"
Jakob Stoklund Olesen45c5c572012-09-06 18:15:23 +000030#include "RegisterCoalescer.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000031#include <algorithm>
Chris Lattnerfb449b92004-07-23 17:49:16 +000032using namespace llvm;
33
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000034LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen55768d72011-03-12 01:50:35 +000035 // This algorithm is basically std::upper_bound.
36 // Unfortunately, std::upper_bound cannot be used with mixed types until we
37 // adopt C++0x. Many libraries can do it, but not all.
38 if (empty() || Pos >= endIndex())
39 return end();
40 iterator I = begin();
41 size_t Len = ranges.size();
42 do {
43 size_t Mid = Len >> 1;
44 if (Pos < I[Mid].end)
45 Len = Mid;
46 else
47 I += Mid + 1, Len -= Mid + 1;
48 } while (Len);
49 return I;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000050}
51
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000052VNInfo *LiveInterval::createDeadDef(SlotIndex Def,
53 VNInfo::Allocator &VNInfoAllocator) {
54 assert(!Def.isDead() && "Cannot define a value at the dead slot");
55 iterator I = find(Def);
56 if (I == end()) {
57 VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
58 ranges.push_back(LiveRange(Def, Def.getDeadSlot(), VNI));
59 return VNI;
60 }
61 if (SlotIndex::isSameInstr(Def, I->start)) {
62 assert(I->start == Def && "Cannot insert def, already live");
63 assert(I->valno->def == Def && "Inconsistent existing value def");
64 return I->valno;
65 }
66 assert(SlotIndex::isEarlierInstr(Def, I->start) && "Already live at def");
67 VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
68 ranges.insert(I, LiveRange(Def, Def.getDeadSlot(), VNI));
69 return VNI;
70}
71
Chris Lattnerbae74d92004-11-18 03:47:34 +000072// overlaps - Return true if the intersection of the two live intervals is
73// not empty.
74//
Chris Lattnerfb449b92004-07-23 17:49:16 +000075// An example for overlaps():
76//
77// 0: A = ...
78// 4: B = ...
79// 8: C = A + B ;; last use of A
80//
81// The live intervals should look like:
82//
83// A = [3, 11)
84// B = [7, x)
85// C = [11, y)
86//
87// A->overlaps(C) should return false since we want to be able to join
88// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000089//
90bool LiveInterval::overlapsFrom(const LiveInterval& other,
91 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000092 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000093 const_iterator i = begin();
94 const_iterator ie = end();
95 const_iterator j = StartPos;
96 const_iterator je = other.end();
97
98 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000099 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000100
Chris Lattnerfb449b92004-07-23 17:49:16 +0000101 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000102 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000103 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000104 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000105 ++StartPos;
106 if (StartPos != other.end() && StartPos->start <= i->start) {
107 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000108 j = std::upper_bound(j, je, i->start);
109 if (j != other.ranges.begin()) --j;
110 }
Chris Lattneraa141472004-07-23 18:40:00 +0000111 } else {
112 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000113 }
114
Chris Lattner9fddc122004-11-18 05:28:21 +0000115 if (j == je) return false;
116
117 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000118 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000119 std::swap(i, j);
120 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000121 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000122
123 if (i->end > j->start)
124 return true;
125 ++i;
126 }
127
128 return false;
129}
130
Jakob Stoklund Olesen45c5c572012-09-06 18:15:23 +0000131bool LiveInterval::overlaps(const LiveInterval &Other,
132 const CoalescerPair &CP,
133 const SlotIndexes &Indexes) const {
134 assert(!empty() && "empty interval");
135 if (Other.empty())
136 return false;
137
138 // Use binary searches to find initial positions.
139 const_iterator I = find(Other.beginIndex());
140 const_iterator IE = end();
141 if (I == IE)
142 return false;
143 const_iterator J = Other.find(I->start);
144 const_iterator JE = Other.end();
145 if (J == JE)
146 return false;
147
148 for (;;) {
149 // J has just been advanced to satisfy:
150 assert(J->end >= I->start);
151 // Check for an overlap.
152 if (J->start < I->end) {
153 // I and J are overlapping. Find the later start.
154 SlotIndex Def = std::max(I->start, J->start);
155 // Allow the overlap if Def is a coalescable copy.
156 if (Def.isBlock() ||
157 !CP.isCoalescable(Indexes.getInstructionFromIndex(Def)))
158 return true;
159 }
160 // Advance the iterator that ends first to check for more overlaps.
161 if (J->end > I->end) {
162 std::swap(I, J);
163 std::swap(IE, JE);
164 }
165 // Advance J until J->end >= I->start.
166 do
167 if (++J == JE)
168 return false;
169 while (J->end < I->start);
170 }
171}
172
Evan Chengcccdb2b2009-04-18 08:52:15 +0000173/// overlaps - Return true if the live interval overlaps a range specified
174/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000175bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000176 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000177 const_iterator I = std::lower_bound(begin(), end(), End);
178 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000179}
180
Lang Hames6f4e4df2010-07-26 01:49:41 +0000181
182/// ValNo is dead, remove it. If it is the largest value number, just nuke it
183/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
184/// it can be nuked later.
185void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
186 if (ValNo->id == getNumValNums()-1) {
187 do {
188 valnos.pop_back();
189 } while (!valnos.empty() && valnos.back()->isUnused());
190 } else {
Jakob Stoklund Olesenb2beac22012-08-03 20:59:32 +0000191 ValNo->markUnused();
Lang Hames6f4e4df2010-07-26 01:49:41 +0000192 }
193}
194
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000195/// RenumberValues - Renumber all values in order of appearance and delete the
196/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000197void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000198 SmallPtrSet<VNInfo*, 8> Seen;
199 valnos.clear();
200 for (const_iterator I = begin(), E = end(); I != E; ++I) {
201 VNInfo *VNI = I->valno;
202 if (!Seen.insert(VNI))
203 continue;
204 assert(!VNI->isUnused() && "Unused valno used by live range");
205 VNI->id = (unsigned)valnos.size();
206 valnos.push_back(VNI);
207 }
208}
209
Chris Lattnerb26c2152004-07-23 19:38:44 +0000210/// extendIntervalEndTo - This method is used when we want to extend the range
211/// specified by I to end at the specified endpoint. To do this, we should
212/// merge and eliminate all ranges that this will overlap with. The iterator is
213/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000214void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000215 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000216 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000217
Chris Lattnerb26c2152004-07-23 19:38:44 +0000218 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000219 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000220 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000222 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000223
224 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
225 I->end = std::max(NewEnd, prior(MergeTo)->end);
226
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000227 // If the newly formed range now touches the range after it and if they have
228 // the same value number, merge the two ranges into one range.
Chandler Carruth95c88b82012-07-05 12:40:45 +0000229 if (MergeTo != ranges.end() && MergeTo->start <= I->end &&
230 MergeTo->valno == ValNo) {
231 I->end = MergeTo->end;
232 ++MergeTo;
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000233 }
Chandler Carruth95c88b82012-07-05 12:40:45 +0000234
235 // Erase any dead ranges.
236 ranges.erase(llvm::next(I), MergeTo);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000237}
238
239
240/// extendIntervalStartTo - This method is used when we want to extend the range
241/// specified by I to start at the specified endpoint. To do this, we should
242/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000243LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000244LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000245 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000246 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000247
248 // Search for the first interval that we can't merge with.
249 Ranges::iterator MergeTo = I;
250 do {
251 if (MergeTo == ranges.begin()) {
252 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000253 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000254 return I;
255 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000256 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000257 --MergeTo;
258 } while (NewStart <= MergeTo->start);
259
260 // If we start in the middle of another interval, just delete a range and
261 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000262 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000263 MergeTo->end = I->end;
264 } else {
265 // Otherwise, extend the interval right after.
266 ++MergeTo;
267 MergeTo->start = NewStart;
268 MergeTo->end = I->end;
269 }
270
Oscar Fuentesee56c422010-08-02 06:00:15 +0000271 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000272 return MergeTo;
273}
274
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000275LiveInterval::iterator
276LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000277 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000278 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000279
280 // If the inserted interval starts in the middle or right at the end of
281 // another interval, just extend that interval to contain the range of LR.
282 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000283 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000284 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000285 if (B->start <= Start && B->end >= Start) {
286 extendIntervalEndTo(B, End);
287 return B;
288 }
289 } else {
290 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000291 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000292 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000293 "Cannot overlap two LiveRanges with differing ValID's"
294 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000295 }
296 }
297
298 // Otherwise, if this range ends in the middle of, or right next to, another
299 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000300 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000301 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000302 if (it->start <= End) {
303 it = extendIntervalStartTo(it, Start);
304
305 // If LR is a complete superset of an interval, we may need to grow its
306 // endpoint as well.
307 if (End > it->end)
308 extendIntervalEndTo(it, End);
309 return it;
310 }
311 } else {
312 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000313 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000314 assert(it->start >= End &&
315 "Cannot overlap two LiveRanges with differing ValID's");
316 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000317 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000318
319 // Otherwise, this is just a new range that doesn't interact with anything.
320 // Insert it.
321 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000322}
323
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000324/// extendInBlock - If this interval is live before Kill in the basic
325/// block that starts at StartIdx, extend it to be live up to Kill and return
326/// the value. If there is no live range before Kill, return NULL.
327VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000328 if (empty())
329 return 0;
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000330 iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot());
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000331 if (I == begin())
332 return 0;
333 --I;
334 if (I->end <= StartIdx)
335 return 0;
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000336 if (I->end < Kill)
337 extendIntervalEndTo(I, Kill);
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000338 return I->valno;
339}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340
341/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000342/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000343void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000344 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000345 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000346 Ranges::iterator I = find(Start);
347 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000348 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000349
350 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000351 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000352 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000353 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000354 if (RemoveDeadValNo) {
355 // Check if val# is dead.
356 bool isDead = true;
357 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
358 if (II != I && II->valno == ValNo) {
359 isDead = false;
360 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000361 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000362 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000363 // Now that ValNo is dead, remove it.
364 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000365 }
366 }
367
Chris Lattnerabf295f2004-07-24 02:52:23 +0000368 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000369 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000370 I->start = End;
371 return;
372 }
373
374 // Otherwise if the span we are removing is at the end of the LiveRange,
375 // adjust the other way.
376 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000377 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000378 return;
379 }
380
381 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000382 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000383 I->end = Start; // Trim the old interval.
384
385 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000386 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000387}
388
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000389/// removeValNo - Remove all the ranges defined by the specified value#.
390/// Also remove the value# from value# list.
391void LiveInterval::removeValNo(VNInfo *ValNo) {
392 if (empty()) return;
393 Ranges::iterator I = ranges.end();
394 Ranges::iterator E = ranges.begin();
395 do {
396 --I;
397 if (I->valno == ValNo)
398 ranges.erase(I);
399 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000400 // Now that ValNo is dead, remove it.
401 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000402}
Lang Hames86511252009-09-04 20:41:11 +0000403
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000404/// join - Join two live intervals (this, and other) together. This applies
405/// mappings to the value numbers in the LHS/RHS intervals as specified. If
406/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000407void LiveInterval::join(LiveInterval &Other,
408 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000409 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000410 SmallVector<VNInfo*, 16> &NewVNInfo,
411 MachineRegisterInfo *MRI) {
Chandler Carruth261b6332012-07-10 05:06:03 +0000412 verify();
413
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000414 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000415 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000416 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000417 unsigned NumVals = getNumValNums();
418 unsigned NumNewVals = NewVNInfo.size();
419 for (unsigned i = 0; i != NumVals; ++i) {
420 unsigned LHSValID = LHSValNoAssignments[i];
421 if (i != LHSValID ||
Lang Hamesd88710a2012-02-02 06:55:45 +0000422 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000423 MustMapCurValNos = true;
Lang Hamesd88710a2012-02-02 06:55:45 +0000424 break;
425 }
Chris Lattnerdeb99712004-07-24 03:41:50 +0000426 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000427
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000428 // If we have to apply a mapping to our base interval assignment, rewrite it
429 // now.
Jakob Stoklund Olesen657720b2012-09-27 21:05:59 +0000430 if (MustMapCurValNos && !empty()) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000431 // Map the first live range.
Lang Hames02e08d52012-02-02 05:37:34 +0000432
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000433 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000434 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Lang Hames02e08d52012-02-02 05:37:34 +0000435 for (iterator I = next(OutIt), E = end(); I != E; ++I) {
436 VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
437 assert(nextValNo != 0 && "Huh?");
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000438
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000439 // If this live range has the same value # as its immediate predecessor,
440 // and if they are neighbors, remove one LiveRange. This happens when we
Lang Hames02e08d52012-02-02 05:37:34 +0000441 // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
442 if (OutIt->valno == nextValNo && OutIt->end == I->start) {
443 OutIt->end = I->end;
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000444 } else {
Lang Hames02e08d52012-02-02 05:37:34 +0000445 // Didn't merge. Move OutIt to the next interval,
446 ++OutIt;
447 OutIt->valno = nextValNo;
448 if (OutIt != I) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000449 OutIt->start = I->start;
450 OutIt->end = I->end;
451 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000452 }
453 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000454 // If we merge some live ranges, chop off the end.
Lang Hames02e08d52012-02-02 05:37:34 +0000455 ++OutIt;
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000456 ranges.erase(OutIt, end());
457 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000458
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000459 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000460 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000461 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
462 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
463
464 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000465 // LiveInterval now. Also remove dead val#'s.
466 unsigned NumValNos = 0;
467 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000468 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000469 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000470 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000471 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000472 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000473 valnos[NumValNos] = VNI;
474 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000475 }
476 }
Evan Cheng34301352007-09-01 02:03:17 +0000477 if (NumNewVals < NumVals)
478 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000479
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000480 // Okay, now insert the RHS live ranges into the LHS.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 unsigned RangeNo = 0;
482 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
483 // Map the valno in the other live range to the current live range.
484 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000485 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000486 }
Chandler Carruth4e996de2012-07-10 22:25:21 +0000487 mergeIntervalRanges(Other);
Chandler Carruth261b6332012-07-10 05:06:03 +0000488
489 verify();
Chris Lattnerfb449b92004-07-23 17:49:16 +0000490}
491
Chandler Carruthe585e752012-07-10 05:16:17 +0000492/// \brief Helper function for merging in another LiveInterval's ranges.
493///
494/// This is a helper routine implementing an efficient merge of another
495/// LiveIntervals ranges into the current interval.
496///
Chandler Carruth4e996de2012-07-10 22:25:21 +0000497/// \param LHSValNo If non-NULL, set as the new value number for every range
498/// from RHS which is merged into the LHS.
Chandler Carruthe585e752012-07-10 05:16:17 +0000499/// \param RHSValNo If non-NULL, then only ranges in RHS whose original value
500/// number maches this value number will be merged into LHS.
501void LiveInterval::mergeIntervalRanges(const LiveInterval &RHS,
502 VNInfo *LHSValNo,
503 const VNInfo *RHSValNo) {
504 if (RHS.empty())
505 return;
506
Chandler Carruth4e996de2012-07-10 22:25:21 +0000507 // Ensure we're starting with a valid range. Note that we don't verify RHS
508 // because it may have had its value numbers adjusted in preparation for
509 // merging.
Chandler Carruthe585e752012-07-10 05:16:17 +0000510 verify();
Chandler Carruthe585e752012-07-10 05:16:17 +0000511
512 // The strategy for merging these efficiently is as follows:
513 //
514 // 1) Find the beginning of the impacted ranges in the LHS.
515 // 2) Create a new, merged sub-squence of ranges merging from the position in
516 // #1 until either LHS or RHS is exhausted. Any part of LHS between RHS
517 // entries being merged will be copied into this new range.
518 // 3) Replace the relevant section in LHS with these newly merged ranges.
519 // 4) Append any remaning ranges from RHS if LHS is exhausted in #2.
520 //
521 // We don't follow the typical in-place merge strategy for sorted ranges of
522 // appending the new ranges to the back and then using std::inplace_merge
523 // because one step of the merge can both mutate the original elements and
524 // remove elements from the original. Essentially, because the merge includes
525 // collapsing overlapping ranges, a more complex approach is required.
526
527 // We do an initial binary search to optimize for a common pattern: a large
528 // LHS, and a very small RHS.
529 const_iterator RI = RHS.begin(), RE = RHS.end();
530 iterator LE = end(), LI = std::upper_bound(begin(), LE, *RI);
531
532 // Merge into NewRanges until one of the ranges is exhausted.
533 SmallVector<LiveRange, 4> NewRanges;
534
535 // Keep track of where to begin the replacement.
536 iterator ReplaceI = LI;
537
538 // If there are preceding ranges in the LHS, put the last one into NewRanges
539 // so we can optionally extend it. Adjust the replacement point accordingly.
540 if (LI != begin()) {
541 ReplaceI = llvm::prior(LI);
542 NewRanges.push_back(*ReplaceI);
543 }
544
545 // Now loop over the mergable portions of both LHS and RHS, merging into
546 // NewRanges.
547 while (LI != LE && RI != RE) {
548 // Skip incoming ranges with the wrong value.
549 if (RHSValNo && RI->valno != RHSValNo) {
550 ++RI;
551 continue;
552 }
553
554 // Select the first range. We pick the earliest start point, and then the
555 // largest range.
556 LiveRange R = *LI;
557 if (*RI < R) {
558 R = *RI;
559 ++RI;
Chandler Carruth4e996de2012-07-10 22:25:21 +0000560 if (LHSValNo)
561 R.valno = LHSValNo;
Chandler Carruthe585e752012-07-10 05:16:17 +0000562 } else {
563 ++LI;
564 }
565
566 if (NewRanges.empty()) {
567 NewRanges.push_back(R);
568 continue;
569 }
570
571 LiveRange &LastR = NewRanges.back();
572 if (R.valno == LastR.valno) {
573 // Try to merge this range into the last one.
574 if (R.start <= LastR.end) {
575 LastR.end = std::max(LastR.end, R.end);
576 continue;
577 }
578 } else {
579 // We can't merge ranges across a value number.
580 assert(R.start >= LastR.end &&
581 "Cannot overlap two LiveRanges with differing ValID's");
582 }
583
584 // If all else fails, just append the range.
585 NewRanges.push_back(R);
586 }
587 assert(RI == RE || LI == LE);
588
589 // Check for being able to merge into the trailing sequence of ranges on the LHS.
590 if (!NewRanges.empty())
591 for (; LI != LE && (LI->valno == NewRanges.back().valno &&
592 LI->start <= NewRanges.back().end);
593 ++LI)
594 NewRanges.back().end = std::max(NewRanges.back().end, LI->end);
595
596 // Replace the ranges in the LHS with the newly merged ones. It would be
597 // really nice if there were a move-supporting 'replace' directly in
598 // SmallVector, but as there is not, we pay the price of copies to avoid
599 // wasted memory allocations.
600 SmallVectorImpl<LiveRange>::iterator NRI = NewRanges.begin(),
601 NRE = NewRanges.end();
602 for (; ReplaceI != LI && NRI != NRE; ++ReplaceI, ++NRI)
603 *ReplaceI = *NRI;
604 if (NRI == NRE)
605 ranges.erase(ReplaceI, LI);
606 else
607 ranges.insert(LI, NRI, NRE);
608
609 // And finally insert any trailing end of RHS (if we have one).
Chandler Carruth4e996de2012-07-10 22:25:21 +0000610 for (; RI != RE; ++RI) {
611 LiveRange R = *RI;
612 if (LHSValNo)
613 R.valno = LHSValNo;
Chandler Carruth1b8da1d2012-07-10 15:41:33 +0000614 if (!ranges.empty() &&
Chandler Carruth4e996de2012-07-10 22:25:21 +0000615 ranges.back().valno == R.valno && R.start <= ranges.back().end)
616 ranges.back().end = std::max(ranges.back().end, R.end);
617 else
618 ranges.push_back(R);
619 }
Chandler Carruthe585e752012-07-10 05:16:17 +0000620
621 // Ensure we finished with a valid new sequence of ranges.
622 verify();
623}
624
Chris Lattnerf21f0202006-09-02 05:26:59 +0000625/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
626/// interval as the specified value number. The LiveRanges in RHS are
627/// allowed to overlap with LiveRanges in the current interval, but only if
628/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000629void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000630 VNInfo *LHSValNo) {
Chandler Carruthe585e752012-07-10 05:16:17 +0000631 mergeIntervalRanges(RHS, LHSValNo);
Chris Lattnerf21f0202006-09-02 05:26:59 +0000632}
633
Evan Cheng32dfbea2007-10-12 08:50:34 +0000634/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
635/// in RHS into this live interval as the specified value number.
636/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000637/// current interval, it will replace the value numbers of the overlaped
638/// live ranges with the specified value number.
Chandler Carruthe585e752012-07-10 05:16:17 +0000639void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
640 const VNInfo *RHSValNo,
641 VNInfo *LHSValNo) {
642 mergeIntervalRanges(RHS, LHSValNo, RHSValNo);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000643}
644
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000645/// MergeValueNumberInto - This method is called when two value nubmers
646/// are found to be equivalent. This eliminates V1, replacing all
647/// LiveRanges with the V1 value number with the V2 value number. This can
648/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000649VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000650 assert(V1 != V2 && "Identical value#'s are always equivalent!");
651
652 // This code actually merges the (numerically) larger value number into the
653 // smaller value number, which is likely to allow us to compactify the value
654 // space. The only thing we have to be careful of is to preserve the
655 // instruction that defines the result value.
656
657 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000658 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000659 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000660 std::swap(V1, V2);
661 }
662
663 // Merge V1 live ranges into V2.
664 for (iterator I = begin(); I != end(); ) {
665 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000666 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000667
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000668 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
669 // range, extend it.
670 if (LR != begin()) {
671 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000672 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000673 Prev->end = LR->end;
674
675 // Erase this live-range.
676 ranges.erase(LR);
677 I = Prev+1;
678 LR = Prev;
679 }
680 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000681
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000682 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
683 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000684 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000685
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000686 // If we can merge it into later V2 live ranges, do so now. We ignore any
687 // following V1 live ranges, as they will be merged in subsequent iterations
688 // of the loop.
689 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000690 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000691 LR->end = I->end;
692 ranges.erase(I);
693 I = LR+1;
694 }
695 }
696 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000697
Lang Hames6f4e4df2010-07-26 01:49:41 +0000698 // Now that V1 is dead, remove it.
699 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000700
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000701 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000702}
703
Evan Chenge52eef82007-04-17 20:25:11 +0000704unsigned LiveInterval::getSize() const {
705 unsigned Sum = 0;
706 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000707 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000708 return Sum;
709}
710
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000711raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
712 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
713}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000714
Manman Renb720be62012-09-11 22:23:19 +0000715#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnerabf295f2004-07-24 02:52:23 +0000716void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000717 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000718}
Manman Ren77e300e2012-09-06 19:06:06 +0000719#endif
Chris Lattnerabf295f2004-07-24 02:52:23 +0000720
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000721void LiveInterval::print(raw_ostream &OS) const {
Chris Lattner38135af2005-05-14 05:34:15 +0000722 if (empty())
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000723 OS << "EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000724 else {
Chris Lattner38135af2005-05-14 05:34:15 +0000725 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000726 E = ranges.end(); I != E; ++I) {
727 OS << *I;
728 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
729 }
Chris Lattner38135af2005-05-14 05:34:15 +0000730 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000731
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000732 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000733 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000734 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000735 unsigned vnum = 0;
736 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
737 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000738 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000739 if (vnum) OS << " ";
740 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000741 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000742 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000743 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000744 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000745 if (vni->isPHIDef())
Jakob Stoklund Olesenbf60aa92012-08-03 20:19:44 +0000746 OS << "-phi";
Evan Chenga8d94f12007-08-07 23:49:57 +0000747 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000748 }
749 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000750}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000751
Manman Renb720be62012-09-11 22:23:19 +0000752#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnerabf295f2004-07-24 02:52:23 +0000753void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000754 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000755}
Manman Ren77e300e2012-09-06 19:06:06 +0000756#endif
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000757
Chandler Carruth261b6332012-07-10 05:06:03 +0000758#ifndef NDEBUG
759void LiveInterval::verify() const {
760 for (const_iterator I = begin(), E = end(); I != E; ++I) {
761 assert(I->start.isValid());
762 assert(I->end.isValid());
763 assert(I->start < I->end);
764 assert(I->valno != 0);
765 assert(I->valno == valnos[I->valno->id]);
766 if (llvm::next(I) != E) {
767 assert(I->end <= llvm::next(I)->start);
768 if (I->end == llvm::next(I)->start)
769 assert(I->valno != llvm::next(I)->valno);
770 }
771 }
772}
773#endif
774
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000775
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000776void LiveRange::print(raw_ostream &os) const {
777 os << *this;
778}
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000779
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000780unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000781 // Create initial equivalence classes.
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000782 EqClass.clear();
783 EqClass.grow(LI->getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000784
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000785 const VNInfo *used = 0, *unused = 0;
786
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000787 // Determine connections.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000788 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
789 I != E; ++I) {
790 const VNInfo *VNI = *I;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000791 // Group all unused values into one class.
792 if (VNI->isUnused()) {
793 if (unused)
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000794 EqClass.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000795 unused = VNI;
796 continue;
797 }
798 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000799 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000800 const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000801 assert(MBB && "Phi-def has no defining MBB");
802 // Connect to values live out of predecessors.
803 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
804 PE = MBB->pred_end(); PI != PE; ++PI)
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000805 if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000806 EqClass.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000807 } else {
808 // Normal value defined by an instruction. Check for two-addr redef.
809 // FIXME: This could be coincidental. Should we really check for a tied
810 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000811 // Note that VNI->def may be a use slot for an early clobber def.
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000812 if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def))
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000813 EqClass.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000814 }
815 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000816
817 // Lump all the unused values in with the last used value.
818 if (used && unused)
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000819 EqClass.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000820
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000821 EqClass.compress();
822 return EqClass.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000823}
824
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000825void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
826 MachineRegisterInfo &MRI) {
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000827 assert(LIV[0] && "LIV[0] must be set");
828 LiveInterval &LI = *LIV[0];
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000829
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000830 // Rewrite instructions.
831 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
832 RE = MRI.reg_end(); RI != RE;) {
833 MachineOperand &MO = RI.getOperand();
834 MachineInstr *MI = MO.getParent();
835 ++RI;
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000836 // DBG_VALUE instructions should have been eliminated earlier.
Jakob Stoklund Olesen84315f02012-07-25 17:15:15 +0000837 LiveRangeQuery LRQ(LI, LIS.getInstructionIndex(MI));
838 const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined();
839 // In the case of an <undef> use that isn't tied to any def, VNI will be
840 // NULL. If the use is tied to a def, VNI will be the defined value.
Jakob Stoklund Olesenbd6f44a2012-05-19 05:25:50 +0000841 if (!VNI)
842 continue;
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000843 MO.setReg(LIV[getEqClass(VNI)]->reg);
844 }
845
846 // Move runs to new intervals.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000847 LiveInterval::iterator J = LI.begin(), E = LI.end();
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000848 while (J != E && EqClass[J->valno->id] == 0)
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000849 ++J;
850 for (LiveInterval::iterator I = J; I != E; ++I) {
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000851 if (unsigned eq = EqClass[I->valno->id]) {
Benjamin Kramerccefe322010-10-09 16:36:44 +0000852 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000853 "New intervals should be empty");
854 LIV[eq]->ranges.push_back(*I);
855 } else
856 *J++ = *I;
857 }
858 LI.ranges.erase(J, E);
859
860 // Transfer VNInfos to their new owners and renumber them.
861 unsigned j = 0, e = LI.getNumValNums();
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000862 while (j != e && EqClass[j] == 0)
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000863 ++j;
864 for (unsigned i = j; i != e; ++i) {
865 VNInfo *VNI = LI.getValNumInfo(i);
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000866 if (unsigned eq = EqClass[i]) {
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000867 VNI->id = LIV[eq]->getNumValNums();
868 LIV[eq]->valnos.push_back(VNI);
869 } else {
870 VNI->id = j;
871 LI.valnos[j++] = VNI;
872 }
873 }
874 LI.valnos.resize(j);
875}