blob: a37296f77b06bce6a11bc12bf05e1a56e53a0eaa [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 Olesen7c727072010-09-21 20:16:12 +000033// CompEnd - Compare LiveRange ends.
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000034namespace {
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000035struct CompEnd {
Jakob Stoklund Olesen7c727072010-09-21 20:16:12 +000036 bool operator()(const LiveRange &A, const LiveRange &B) const {
37 return A.end < B.end;
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000038 }
39};
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000040}
Chris Lattnerfb449b92004-07-23 17:49:16 +000041
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000042LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen201ecfc2010-10-05 18:48:55 +000043 assert(Pos.isValid() && "Cannot search for an invalid index");
Jakob Stoklund Olesen7c727072010-09-21 20:16:12 +000044 return std::upper_bound(begin(), end(), LiveRange(SlotIndex(), Pos, 0),
45 CompEnd());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000046}
47
48/// killedInRange - Return true if the interval has kills in [Start,End).
49bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
50 Ranges::const_iterator r =
51 std::lower_bound(ranges.begin(), ranges.end(), End);
52
53 // Now r points to the first interval with start >= End, or ranges.end().
54 if (r == ranges.begin())
55 return false;
56
57 --r;
58 // Now r points to the last interval with end <= End.
59 // r->end is the kill point.
60 return r->end >= Start && r->end < End;
61}
62
Chris Lattnerbae74d92004-11-18 03:47:34 +000063// overlaps - Return true if the intersection of the two live intervals is
64// not empty.
65//
Chris Lattnerfb449b92004-07-23 17:49:16 +000066// An example for overlaps():
67//
68// 0: A = ...
69// 4: B = ...
70// 8: C = A + B ;; last use of A
71//
72// The live intervals should look like:
73//
74// A = [3, 11)
75// B = [7, x)
76// C = [11, y)
77//
78// A->overlaps(C) should return false since we want to be able to join
79// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000080//
81bool LiveInterval::overlapsFrom(const LiveInterval& other,
82 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000083 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000084 const_iterator i = begin();
85 const_iterator ie = end();
86 const_iterator j = StartPos;
87 const_iterator je = other.end();
88
89 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000090 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000091
Chris Lattnerfb449b92004-07-23 17:49:16 +000092 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000093 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000094 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000095 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000096 ++StartPos;
97 if (StartPos != other.end() && StartPos->start <= i->start) {
98 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000099 j = std::upper_bound(j, je, i->start);
100 if (j != other.ranges.begin()) --j;
101 }
Chris Lattneraa141472004-07-23 18:40:00 +0000102 } else {
103 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000104 }
105
Chris Lattner9fddc122004-11-18 05:28:21 +0000106 if (j == je) return false;
107
108 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000109 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000110 std::swap(i, j);
111 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000112 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000113
114 if (i->end > j->start)
115 return true;
116 ++i;
117 }
118
119 return false;
120}
121
Evan Chengcccdb2b2009-04-18 08:52:15 +0000122/// overlaps - Return true if the live interval overlaps a range specified
123/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000124bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000125 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000126 const_iterator I = std::lower_bound(begin(), end(), End);
127 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000128}
129
Lang Hames6f4e4df2010-07-26 01:49:41 +0000130
131/// ValNo is dead, remove it. If it is the largest value number, just nuke it
132/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
133/// it can be nuked later.
134void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
135 if (ValNo->id == getNumValNums()-1) {
136 do {
137 valnos.pop_back();
138 } while (!valnos.empty() && valnos.back()->isUnused());
139 } else {
140 ValNo->setIsUnused(true);
141 }
142}
143
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000144/// RenumberValues - Renumber all values in order of appearance and delete the
145/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000146void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000147 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000148 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000149 valnos.clear();
150 for (const_iterator I = begin(), E = end(); I != E; ++I) {
151 VNInfo *VNI = I->valno;
152 if (!Seen.insert(VNI))
153 continue;
154 assert(!VNI->isUnused() && "Unused valno used by live range");
155 VNI->id = (unsigned)valnos.size();
156 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000157 VNI->setHasPHIKill(false);
158 if (VNI->isPHIDef())
159 seenPHIDef = true;
160 }
161
162 // Recompute phi kill flags.
163 if (!seenPHIDef)
164 return;
165 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
166 VNInfo *VNI = *I;
167 if (!VNI->isPHIDef())
168 continue;
169 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
170 assert(PHIBB && "No basic block for phi-def");
171 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
172 PE = PHIBB->pred_end(); PI != PE; ++PI) {
173 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
174 if (KVNI)
175 KVNI->setHasPHIKill(true);
176 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000177 }
178}
179
Chris Lattnerb26c2152004-07-23 19:38:44 +0000180/// extendIntervalEndTo - This method is used when we want to extend the range
181/// specified by I to end at the specified endpoint. To do this, we should
182/// merge and eliminate all ranges that this will overlap with. The iterator is
183/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000184void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000185 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000186 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000187
Chris Lattnerb26c2152004-07-23 19:38:44 +0000188 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000189 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000190 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000191 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000192 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000193
194 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
195 I->end = std::max(NewEnd, prior(MergeTo)->end);
196
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000197 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000198 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000199
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000200 // If the newly formed range now touches the range after it and if they have
201 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000202 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000203 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000204 I->end = Next->end;
205 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000206 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000207}
208
209
210/// extendIntervalStartTo - This method is used when we want to extend the range
211/// specified by I to start at the specified endpoint. To do this, we should
212/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000213LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000214LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
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 Lattnerb26c2152004-07-23 19:38:44 +0000217
218 // Search for the first interval that we can't merge with.
219 Ranges::iterator MergeTo = I;
220 do {
221 if (MergeTo == ranges.begin()) {
222 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000223 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000224 return I;
225 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000226 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000227 --MergeTo;
228 } while (NewStart <= MergeTo->start);
229
230 // If we start in the middle of another interval, just delete a range and
231 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000232 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000233 MergeTo->end = I->end;
234 } else {
235 // Otherwise, extend the interval right after.
236 ++MergeTo;
237 MergeTo->start = NewStart;
238 MergeTo->end = I->end;
239 }
240
Oscar Fuentesee56c422010-08-02 06:00:15 +0000241 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000242 return MergeTo;
243}
244
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000245LiveInterval::iterator
246LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000247 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000248 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000249
250 // If the inserted interval starts in the middle or right at the end of
251 // another interval, just extend that interval to contain the range of LR.
252 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000253 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000254 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000255 if (B->start <= Start && B->end >= Start) {
256 extendIntervalEndTo(B, End);
257 return B;
258 }
259 } else {
260 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000261 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000262 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000263 "Cannot overlap two LiveRanges with differing ValID's"
264 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000265 }
266 }
267
268 // Otherwise, if this range ends in the middle of, or right next to, another
269 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000270 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000271 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000272 if (it->start <= End) {
273 it = extendIntervalStartTo(it, Start);
274
275 // If LR is a complete superset of an interval, we may need to grow its
276 // endpoint as well.
277 if (End > it->end)
278 extendIntervalEndTo(it, End);
279 return it;
280 }
281 } else {
282 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000283 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000284 assert(it->start >= End &&
285 "Cannot overlap two LiveRanges with differing ValID's");
286 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000287 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000288
289 // Otherwise, this is just a new range that doesn't interact with anything.
290 // Insert it.
291 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000292}
293
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000294/// extendInBlock - If this interval is live before UseIdx in the basic
295/// block that starts at StartIdx, extend it to be live at UseIdx and return
296/// the value. If there is no live range before UseIdx, return NULL.
297VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex UseIdx) {
298 if (empty())
299 return 0;
300 iterator I = std::upper_bound(begin(), end(), UseIdx);
301 if (I == begin())
302 return 0;
303 --I;
304 if (I->end <= StartIdx)
305 return 0;
306 if (I->end <= UseIdx)
307 extendIntervalEndTo(I, UseIdx.getNextSlot());
308 return I->valno;
309}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000310
311/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000312/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000313void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000314 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000315 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000316 Ranges::iterator I = find(Start);
317 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000318 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000319
320 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000321 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000322 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000323 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000324 if (RemoveDeadValNo) {
325 // Check if val# is dead.
326 bool isDead = true;
327 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
328 if (II != I && II->valno == ValNo) {
329 isDead = false;
330 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000331 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000332 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000333 // Now that ValNo is dead, remove it.
334 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000335 }
336 }
337
Chris Lattnerabf295f2004-07-24 02:52:23 +0000338 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000339 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340 I->start = End;
341 return;
342 }
343
344 // Otherwise if the span we are removing is at the end of the LiveRange,
345 // adjust the other way.
346 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000347 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000348 return;
349 }
350
351 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000352 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000353 I->end = Start; // Trim the old interval.
354
355 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000356 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000357}
358
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000359/// removeValNo - Remove all the ranges defined by the specified value#.
360/// Also remove the value# from value# list.
361void LiveInterval::removeValNo(VNInfo *ValNo) {
362 if (empty()) return;
363 Ranges::iterator I = ranges.end();
364 Ranges::iterator E = ranges.begin();
365 do {
366 --I;
367 if (I->valno == ValNo)
368 ranges.erase(I);
369 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000370 // Now that ValNo is dead, remove it.
371 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000372}
Lang Hames86511252009-09-04 20:41:11 +0000373
Lang Hames86511252009-09-04 20:41:11 +0000374/// findDefinedVNInfo - Find the VNInfo defined by the specified
375/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000376VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000377 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000378 i != e; ++i) {
379 if ((*i)->def == Idx)
380 return *i;
381 }
382
383 return 0;
384}
385
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000386/// join - Join two live intervals (this, and other) together. This applies
387/// mappings to the value numbers in the LHS/RHS intervals as specified. If
388/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000389void LiveInterval::join(LiveInterval &Other,
390 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000391 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000392 SmallVector<VNInfo*, 16> &NewVNInfo,
393 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000394 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000395 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000396 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000397 unsigned NumVals = getNumValNums();
398 unsigned NumNewVals = NewVNInfo.size();
399 for (unsigned i = 0; i != NumVals; ++i) {
400 unsigned LHSValID = LHSValNoAssignments[i];
401 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000402 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000403 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000404 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000405
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000406 // If we have to apply a mapping to our base interval assignment, rewrite it
407 // now.
408 if (MustMapCurValNos) {
409 // Map the first live range.
410 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000411 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000412 ++OutIt;
413 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000414 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000415
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000416 // If this live range has the same value # as its immediate predecessor,
417 // and if they are neighbors, remove one LiveRange. This happens when we
418 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000419 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000420 (OutIt-1)->end = OutIt->end;
421 } else {
422 if (I != OutIt) {
423 OutIt->start = I->start;
424 OutIt->end = I->end;
425 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000426
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000427 // Didn't merge, on to the next one.
428 ++OutIt;
429 }
430 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000431
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000432 // If we merge some live ranges, chop off the end.
433 ranges.erase(OutIt, end());
434 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000435
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000436 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000437 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000438 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
439 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
440
441 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000442 // LiveInterval now. Also remove dead val#'s.
443 unsigned NumValNos = 0;
444 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000445 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000446 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000447 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000448 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000449 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000450 valnos[NumValNos] = VNI;
451 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000452 }
453 }
Evan Cheng34301352007-09-01 02:03:17 +0000454 if (NumNewVals < NumVals)
455 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000456
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000457 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000458 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000459 unsigned RangeNo = 0;
460 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
461 // Map the valno in the other live range to the current live range.
462 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000463 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000464 InsertPos = addRangeFrom(*I, InsertPos);
465 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000466
David Greene29ff37f2009-07-22 20:08:25 +0000467 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000468}
469
Chris Lattnerf21f0202006-09-02 05:26:59 +0000470/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
471/// interval as the specified value number. The LiveRanges in RHS are
472/// allowed to overlap with LiveRanges in the current interval, but only if
473/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000474void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000475 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000476 // TODO: Make this more efficient.
477 iterator InsertPos = begin();
478 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000479 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000480 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000482 InsertPos = addRangeFrom(Tmp, InsertPos);
483 }
484}
485
486
Evan Cheng32dfbea2007-10-12 08:50:34 +0000487/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
488/// in RHS into this live interval as the specified value number.
489/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000490/// current interval, it will replace the value numbers of the overlaped
491/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000492void LiveInterval::MergeValueInAsValue(
493 const LiveInterval &RHS,
494 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000495 SmallVector<VNInfo*, 4> ReplacedValNos;
496 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000497 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000498 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000499 if (I->valno != RHSValNo)
500 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000501 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000502 IP = std::upper_bound(IP, end(), Start);
503 // If the start of this range overlaps with an existing liverange, trim it.
504 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000505 if (IP[-1].valno != LHSValNo) {
506 ReplacedValNos.push_back(IP[-1].valno);
507 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000508 }
509 Start = IP[-1].end;
510 // Trimmed away the whole range?
511 if (Start >= End) continue;
512 }
513 // If the end of this range overlaps with an existing liverange, trim it.
514 if (IP != end() && End > IP->start) {
515 if (IP->valno != LHSValNo) {
516 ReplacedValNos.push_back(IP->valno);
517 IP->valno = LHSValNo; // Update val#.
518 }
519 End = IP->start;
520 // If this trimmed away the whole range, ignore it.
521 if (Start == End) continue;
522 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000523
Evan Cheng32dfbea2007-10-12 08:50:34 +0000524 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000525 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
526 }
527
528
529 SmallSet<VNInfo*, 4> Seen;
530 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
531 VNInfo *V1 = ReplacedValNos[i];
532 if (Seen.insert(V1)) {
533 bool isDead = true;
534 for (const_iterator I = begin(), E = end(); I != E; ++I)
535 if (I->valno == V1) {
536 isDead = false;
537 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000538 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000539 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000540 // Now that V1 is dead, remove it.
541 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000542 }
543 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000544 }
545}
546
547
Evan Chenga2e64352009-03-11 00:03:21 +0000548
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000549/// MergeValueNumberInto - This method is called when two value nubmers
550/// are found to be equivalent. This eliminates V1, replacing all
551/// LiveRanges with the V1 value number with the V2 value number. This can
552/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000553VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000554 assert(V1 != V2 && "Identical value#'s are always equivalent!");
555
556 // This code actually merges the (numerically) larger value number into the
557 // smaller value number, which is likely to allow us to compactify the value
558 // space. The only thing we have to be careful of is to preserve the
559 // instruction that defines the result value.
560
561 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000562 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000563 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000564 std::swap(V1, V2);
565 }
566
567 // Merge V1 live ranges into V2.
568 for (iterator I = begin(); I != end(); ) {
569 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000570 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000571
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000572 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
573 // range, extend it.
574 if (LR != begin()) {
575 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000576 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000577 Prev->end = LR->end;
578
579 // Erase this live-range.
580 ranges.erase(LR);
581 I = Prev+1;
582 LR = Prev;
583 }
584 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000585
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000586 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
587 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000588 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000589
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000590 // If we can merge it into later V2 live ranges, do so now. We ignore any
591 // following V1 live ranges, as they will be merged in subsequent iterations
592 // of the loop.
593 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000594 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000595 LR->end = I->end;
596 ranges.erase(I);
597 I = LR+1;
598 }
599 }
600 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000601
Jakob Stoklund Olesene0a73ec2010-10-01 23:52:25 +0000602 // Merge the relevant flags.
603 V2->mergeFlags(V1);
604
Lang Hames6f4e4df2010-07-26 01:49:41 +0000605 // Now that V1 is dead, remove it.
606 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000607
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000608 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000609}
610
Evan Cheng32dfbea2007-10-12 08:50:34 +0000611void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000612 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000613 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000614 ranges.clear();
615 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000616 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000617 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
618
Evan Cheng32dfbea2007-10-12 08:50:34 +0000619 weight = RHS.weight;
620 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
621 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000622 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000623 }
624 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
625 const LiveRange &LR = RHS.ranges[i];
626 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
627 }
628}
629
Evan Chenge52eef82007-04-17 20:25:11 +0000630unsigned LiveInterval::getSize() const {
631 unsigned Sum = 0;
632 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000633 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000634 return Sum;
635}
636
David Greene29ff37f2009-07-22 20:08:25 +0000637/// ComputeJoinedWeight - Set the weight of a live interval Joined
638/// after Other has been merged into it.
639void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
640 // If either of these intervals was spilled, the weight is the
641 // weight of the non-spilled interval. This can only happen with
642 // iterative coalescers.
643
David Greene92b78bb2009-07-22 22:32:19 +0000644 if (Other.weight != HUGE_VALF) {
645 weight += Other.weight;
646 }
647 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000648 !TargetRegisterInfo::isPhysicalRegister(reg)) {
649 // Remove this assert if you have an iterative coalescer
650 assert(0 && "Joining to spilled interval");
651 weight = Other.weight;
652 }
David Greene29ff37f2009-07-22 20:08:25 +0000653 else {
654 // Otherwise the weight stays the same
655 // Remove this assert if you have an iterative coalescer
656 assert(0 && "Joining from spilled interval");
657 }
658}
659
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000660raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
661 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
662}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000663
Chris Lattnerabf295f2004-07-24 02:52:23 +0000664void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000665 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000666}
667
Chris Lattnerc02497f2009-08-23 03:47:42 +0000668void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000669 OS << PrintReg(reg, TRI);
670 if (weight != 0)
671 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000672
Chris Lattner38135af2005-05-14 05:34:15 +0000673 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000674 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000675 else {
676 OS << " = ";
677 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000678 E = ranges.end(); I != E; ++I) {
679 OS << *I;
680 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
681 }
Chris Lattner38135af2005-05-14 05:34:15 +0000682 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000683
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000684 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000685 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000686 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000687 unsigned vnum = 0;
688 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
689 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000690 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000691 if (vnum) OS << " ";
692 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000693 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000694 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000695 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000696 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000697 if (vni->isPHIDef())
698 OS << "-phidef";
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000699 if (vni->hasPHIKill())
700 OS << "-phikill";
701 if (vni->hasRedefByEC())
702 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000703 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000704 }
705 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000706}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000707
708void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000709 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000710}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000711
712
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000713void LiveRange::print(raw_ostream &os) const {
714 os << *this;
715}
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000716
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000717unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000718 // Create initial equivalence classes.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000719 eqClass_.clear();
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000720 eqClass_.grow(LI->getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000721
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000722 const VNInfo *used = 0, *unused = 0;
723
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000724 // Determine connections.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000725 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
726 I != E; ++I) {
727 const VNInfo *VNI = *I;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000728 // Group all unused values into one class.
729 if (VNI->isUnused()) {
730 if (unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000731 eqClass_.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000732 unused = VNI;
733 continue;
734 }
735 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000736 if (VNI->isPHIDef()) {
737 const MachineBasicBlock *MBB = lis_.getMBBFromIndex(VNI->def);
738 assert(MBB && "Phi-def has no defining MBB");
739 // Connect to values live out of predecessors.
740 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
741 PE = MBB->pred_end(); PI != PE; ++PI)
742 if (const VNInfo *PVNI =
743 LI->getVNInfoAt(lis_.getMBBEndIdx(*PI).getPrevSlot()))
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000744 eqClass_.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000745 } else {
746 // Normal value defined by an instruction. Check for two-addr redef.
747 // FIXME: This could be coincidental. Should we really check for a tied
748 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000749 // Note that VNI->def may be a use slot for an early clobber def.
750 if (const VNInfo *UVNI = LI->getVNInfoAt(VNI->def.getPrevSlot()))
751 eqClass_.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000752 }
753 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000754
755 // Lump all the unused values in with the last used value.
756 if (used && unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000757 eqClass_.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000758
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000759 eqClass_.compress();
760 return eqClass_.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000761}
762
763void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[]) {
764 assert(LIV[0] && "LIV[0] must be set");
765 LiveInterval &LI = *LIV[0];
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000766
767 // First move runs to new intervals.
768 LiveInterval::iterator J = LI.begin(), E = LI.end();
769 while (J != E && eqClass_[J->valno->id] == 0)
770 ++J;
771 for (LiveInterval::iterator I = J; I != E; ++I) {
772 if (unsigned eq = eqClass_[I->valno->id]) {
Benjamin Kramerccefe322010-10-09 16:36:44 +0000773 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000774 "New intervals should be empty");
775 LIV[eq]->ranges.push_back(*I);
776 } else
777 *J++ = *I;
778 }
779 LI.ranges.erase(J, E);
780
781 // Transfer VNInfos to their new owners and renumber them.
782 unsigned j = 0, e = LI.getNumValNums();
783 while (j != e && eqClass_[j] == 0)
784 ++j;
785 for (unsigned i = j; i != e; ++i) {
786 VNInfo *VNI = LI.getValNumInfo(i);
787 if (unsigned eq = eqClass_[i]) {
788 VNI->id = LIV[eq]->getNumValNums();
789 LIV[eq]->valnos.push_back(VNI);
790 } else {
791 VNI->id = j;
792 LI.valnos[j++] = VNI;
793 }
794 }
795 LI.valnos.resize(j);
796}