blob: f2345bc2c58711b9af1f4d1b3132d4e8f8fde055 [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 Olesenb64f6692011-03-03 04:23:52 +000036 bool operator()(SlotIndex A, const LiveRange &B) const {
37 return A < B.end;
38 }
39 bool operator()(const LiveRange &A, SlotIndex B) const {
40 return A.end < B;
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000041 }
42};
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000043}
Chris Lattnerfb449b92004-07-23 17:49:16 +000044
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000045LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen201ecfc2010-10-05 18:48:55 +000046 assert(Pos.isValid() && "Cannot search for an invalid index");
Jakob Stoklund Olesenb64f6692011-03-03 04:23:52 +000047 return std::upper_bound(begin(), end(), Pos, CompEnd());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000048}
49
50/// killedInRange - Return true if the interval has kills in [Start,End).
51bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
52 Ranges::const_iterator r =
53 std::lower_bound(ranges.begin(), ranges.end(), End);
54
55 // Now r points to the first interval with start >= End, or ranges.end().
56 if (r == ranges.begin())
57 return false;
58
59 --r;
60 // Now r points to the last interval with end <= End.
61 // r->end is the kill point.
62 return r->end >= Start && r->end < End;
63}
64
Chris Lattnerbae74d92004-11-18 03:47:34 +000065// overlaps - Return true if the intersection of the two live intervals is
66// not empty.
67//
Chris Lattnerfb449b92004-07-23 17:49:16 +000068// An example for overlaps():
69//
70// 0: A = ...
71// 4: B = ...
72// 8: C = A + B ;; last use of A
73//
74// The live intervals should look like:
75//
76// A = [3, 11)
77// B = [7, x)
78// C = [11, y)
79//
80// A->overlaps(C) should return false since we want to be able to join
81// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000082//
83bool LiveInterval::overlapsFrom(const LiveInterval& other,
84 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000085 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000086 const_iterator i = begin();
87 const_iterator ie = end();
88 const_iterator j = StartPos;
89 const_iterator je = other.end();
90
91 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000092 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000093
Chris Lattnerfb449b92004-07-23 17:49:16 +000094 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000095 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000096 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000097 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000098 ++StartPos;
99 if (StartPos != other.end() && StartPos->start <= i->start) {
100 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000101 j = std::upper_bound(j, je, i->start);
102 if (j != other.ranges.begin()) --j;
103 }
Chris Lattneraa141472004-07-23 18:40:00 +0000104 } else {
105 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000106 }
107
Chris Lattner9fddc122004-11-18 05:28:21 +0000108 if (j == je) return false;
109
110 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000111 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000112 std::swap(i, j);
113 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000114 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000115
116 if (i->end > j->start)
117 return true;
118 ++i;
119 }
120
121 return false;
122}
123
Evan Chengcccdb2b2009-04-18 08:52:15 +0000124/// overlaps - Return true if the live interval overlaps a range specified
125/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000126bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000127 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000128 const_iterator I = std::lower_bound(begin(), end(), End);
129 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000130}
131
Lang Hames6f4e4df2010-07-26 01:49:41 +0000132
133/// ValNo is dead, remove it. If it is the largest value number, just nuke it
134/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
135/// it can be nuked later.
136void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
137 if (ValNo->id == getNumValNums()-1) {
138 do {
139 valnos.pop_back();
140 } while (!valnos.empty() && valnos.back()->isUnused());
141 } else {
142 ValNo->setIsUnused(true);
143 }
144}
145
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000146/// RenumberValues - Renumber all values in order of appearance and delete the
147/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000148void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000149 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000150 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000151 valnos.clear();
152 for (const_iterator I = begin(), E = end(); I != E; ++I) {
153 VNInfo *VNI = I->valno;
154 if (!Seen.insert(VNI))
155 continue;
156 assert(!VNI->isUnused() && "Unused valno used by live range");
157 VNI->id = (unsigned)valnos.size();
158 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000159 VNI->setHasPHIKill(false);
160 if (VNI->isPHIDef())
161 seenPHIDef = true;
162 }
163
164 // Recompute phi kill flags.
165 if (!seenPHIDef)
166 return;
167 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
168 VNInfo *VNI = *I;
169 if (!VNI->isPHIDef())
170 continue;
171 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
172 assert(PHIBB && "No basic block for phi-def");
173 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
174 PE = PHIBB->pred_end(); PI != PE; ++PI) {
175 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
176 if (KVNI)
177 KVNI->setHasPHIKill(true);
178 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000179 }
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 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000200 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000201
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000202 // If the newly formed range now touches the range after it and if they have
203 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000204 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000205 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000206 I->end = Next->end;
207 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000208 }
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 Olesen9763e2b2011-03-02 00:06:15 +0000296/// extendInBlock - If this interval is live before UseIdx in the basic
297/// block that starts at StartIdx, extend it to be live at UseIdx and return
298/// the value. If there is no live range before UseIdx, return NULL.
299VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex UseIdx) {
300 if (empty())
301 return 0;
302 iterator I = std::upper_bound(begin(), end(), UseIdx);
303 if (I == begin())
304 return 0;
305 --I;
306 if (I->end <= StartIdx)
307 return 0;
308 if (I->end <= UseIdx)
309 extendIntervalEndTo(I, UseIdx.getNextSlot());
310 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
Lang Hames86511252009-09-04 20:41:11 +0000376/// findDefinedVNInfo - Find the VNInfo defined by the specified
377/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000378VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000379 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000380 i != e; ++i) {
381 if ((*i)->def == Idx)
382 return *i;
383 }
384
385 return 0;
386}
387
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000388/// join - Join two live intervals (this, and other) together. This applies
389/// mappings to the value numbers in the LHS/RHS intervals as specified. If
390/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000391void LiveInterval::join(LiveInterval &Other,
392 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000393 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000394 SmallVector<VNInfo*, 16> &NewVNInfo,
395 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000396 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000397 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000398 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000399 unsigned NumVals = getNumValNums();
400 unsigned NumNewVals = NewVNInfo.size();
401 for (unsigned i = 0; i != NumVals; ++i) {
402 unsigned LHSValID = LHSValNoAssignments[i];
403 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000404 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000405 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000406 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000407
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000408 // If we have to apply a mapping to our base interval assignment, rewrite it
409 // now.
410 if (MustMapCurValNos) {
411 // Map the first live range.
412 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000413 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000414 ++OutIt;
415 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000416 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000417
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000418 // If this live range has the same value # as its immediate predecessor,
419 // and if they are neighbors, remove one LiveRange. This happens when we
420 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000421 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000422 (OutIt-1)->end = OutIt->end;
423 } else {
424 if (I != OutIt) {
425 OutIt->start = I->start;
426 OutIt->end = I->end;
427 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000428
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000429 // Didn't merge, on to the next one.
430 ++OutIt;
431 }
432 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000433
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000434 // If we merge some live ranges, chop off the end.
435 ranges.erase(OutIt, end());
436 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000437
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000438 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000439 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000440 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
441 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
442
443 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000444 // LiveInterval now. Also remove dead val#'s.
445 unsigned NumValNos = 0;
446 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000447 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000448 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000449 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000450 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000451 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000452 valnos[NumValNos] = VNI;
453 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000454 }
455 }
Evan Cheng34301352007-09-01 02:03:17 +0000456 if (NumNewVals < NumVals)
457 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000458
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000459 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000460 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000461 unsigned RangeNo = 0;
462 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
463 // Map the valno in the other live range to the current live range.
464 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000465 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000466 InsertPos = addRangeFrom(*I, InsertPos);
467 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000468
David Greene29ff37f2009-07-22 20:08:25 +0000469 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000470}
471
Chris Lattnerf21f0202006-09-02 05:26:59 +0000472/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
473/// interval as the specified value number. The LiveRanges in RHS are
474/// allowed to overlap with LiveRanges in the current interval, but only if
475/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000476void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000477 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000478 // TODO: Make this more efficient.
479 iterator InsertPos = begin();
480 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000482 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000483 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000484 InsertPos = addRangeFrom(Tmp, InsertPos);
485 }
486}
487
488
Evan Cheng32dfbea2007-10-12 08:50:34 +0000489/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
490/// in RHS into this live interval as the specified value number.
491/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000492/// current interval, it will replace the value numbers of the overlaped
493/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000494void LiveInterval::MergeValueInAsValue(
495 const LiveInterval &RHS,
496 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000497 SmallVector<VNInfo*, 4> ReplacedValNos;
498 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000499 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000500 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000501 if (I->valno != RHSValNo)
502 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000503 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000504 IP = std::upper_bound(IP, end(), Start);
505 // If the start of this range overlaps with an existing liverange, trim it.
506 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000507 if (IP[-1].valno != LHSValNo) {
508 ReplacedValNos.push_back(IP[-1].valno);
509 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000510 }
511 Start = IP[-1].end;
512 // Trimmed away the whole range?
513 if (Start >= End) continue;
514 }
515 // If the end of this range overlaps with an existing liverange, trim it.
516 if (IP != end() && End > IP->start) {
517 if (IP->valno != LHSValNo) {
518 ReplacedValNos.push_back(IP->valno);
519 IP->valno = LHSValNo; // Update val#.
520 }
521 End = IP->start;
522 // If this trimmed away the whole range, ignore it.
523 if (Start == End) continue;
524 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000525
Evan Cheng32dfbea2007-10-12 08:50:34 +0000526 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000527 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
528 }
529
530
531 SmallSet<VNInfo*, 4> Seen;
532 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
533 VNInfo *V1 = ReplacedValNos[i];
534 if (Seen.insert(V1)) {
535 bool isDead = true;
536 for (const_iterator I = begin(), E = end(); I != E; ++I)
537 if (I->valno == V1) {
538 isDead = false;
539 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000540 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000541 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000542 // Now that V1 is dead, remove it.
543 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000544 }
545 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000546 }
547}
548
549
Evan Chenga2e64352009-03-11 00:03:21 +0000550
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000551/// MergeValueNumberInto - This method is called when two value nubmers
552/// are found to be equivalent. This eliminates V1, replacing all
553/// LiveRanges with the V1 value number with the V2 value number. This can
554/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000555VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000556 assert(V1 != V2 && "Identical value#'s are always equivalent!");
557
558 // This code actually merges the (numerically) larger value number into the
559 // smaller value number, which is likely to allow us to compactify the value
560 // space. The only thing we have to be careful of is to preserve the
561 // instruction that defines the result value.
562
563 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000564 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000565 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000566 std::swap(V1, V2);
567 }
568
569 // Merge V1 live ranges into V2.
570 for (iterator I = begin(); I != end(); ) {
571 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000572 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000573
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000574 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
575 // range, extend it.
576 if (LR != begin()) {
577 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000578 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000579 Prev->end = LR->end;
580
581 // Erase this live-range.
582 ranges.erase(LR);
583 I = Prev+1;
584 LR = Prev;
585 }
586 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000587
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000588 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
589 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000590 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000591
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000592 // If we can merge it into later V2 live ranges, do so now. We ignore any
593 // following V1 live ranges, as they will be merged in subsequent iterations
594 // of the loop.
595 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000596 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000597 LR->end = I->end;
598 ranges.erase(I);
599 I = LR+1;
600 }
601 }
602 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000603
Jakob Stoklund Olesene0a73ec2010-10-01 23:52:25 +0000604 // Merge the relevant flags.
605 V2->mergeFlags(V1);
606
Lang Hames6f4e4df2010-07-26 01:49:41 +0000607 // Now that V1 is dead, remove it.
608 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000609
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000610 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000611}
612
Evan Cheng32dfbea2007-10-12 08:50:34 +0000613void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000614 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000615 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000616 ranges.clear();
617 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000618 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000619 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
620
Evan Cheng32dfbea2007-10-12 08:50:34 +0000621 weight = RHS.weight;
622 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
623 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000624 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000625 }
626 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
627 const LiveRange &LR = RHS.ranges[i];
628 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
629 }
630}
631
Evan Chenge52eef82007-04-17 20:25:11 +0000632unsigned LiveInterval::getSize() const {
633 unsigned Sum = 0;
634 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000635 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000636 return Sum;
637}
638
David Greene29ff37f2009-07-22 20:08:25 +0000639/// ComputeJoinedWeight - Set the weight of a live interval Joined
640/// after Other has been merged into it.
641void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
642 // If either of these intervals was spilled, the weight is the
643 // weight of the non-spilled interval. This can only happen with
644 // iterative coalescers.
645
David Greene92b78bb2009-07-22 22:32:19 +0000646 if (Other.weight != HUGE_VALF) {
647 weight += Other.weight;
648 }
649 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000650 !TargetRegisterInfo::isPhysicalRegister(reg)) {
651 // Remove this assert if you have an iterative coalescer
652 assert(0 && "Joining to spilled interval");
653 weight = Other.weight;
654 }
David Greene29ff37f2009-07-22 20:08:25 +0000655 else {
656 // Otherwise the weight stays the same
657 // Remove this assert if you have an iterative coalescer
658 assert(0 && "Joining from spilled interval");
659 }
660}
661
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000662raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
663 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
664}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000665
Chris Lattnerabf295f2004-07-24 02:52:23 +0000666void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000667 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000668}
669
Chris Lattnerc02497f2009-08-23 03:47:42 +0000670void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000671 OS << PrintReg(reg, TRI);
672 if (weight != 0)
673 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000674
Chris Lattner38135af2005-05-14 05:34:15 +0000675 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000676 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000677 else {
678 OS << " = ";
679 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000680 E = ranges.end(); I != E; ++I) {
681 OS << *I;
682 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
683 }
Chris Lattner38135af2005-05-14 05:34:15 +0000684 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000685
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000686 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000687 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000688 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000689 unsigned vnum = 0;
690 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
691 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000692 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000693 if (vnum) OS << " ";
694 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000695 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000696 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000697 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000698 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000699 if (vni->isPHIDef())
700 OS << "-phidef";
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000701 if (vni->hasPHIKill())
702 OS << "-phikill";
703 if (vni->hasRedefByEC())
704 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000705 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000706 }
707 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000708}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000709
710void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000711 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000712}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000713
714
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000715void LiveRange::print(raw_ostream &os) const {
716 os << *this;
717}
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000718
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000719unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000720 // Create initial equivalence classes.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000721 eqClass_.clear();
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000722 eqClass_.grow(LI->getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000723
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000724 const VNInfo *used = 0, *unused = 0;
725
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000726 // Determine connections.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000727 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
728 I != E; ++I) {
729 const VNInfo *VNI = *I;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000730 // Group all unused values into one class.
731 if (VNI->isUnused()) {
732 if (unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000733 eqClass_.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000734 unused = VNI;
735 continue;
736 }
737 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000738 if (VNI->isPHIDef()) {
739 const MachineBasicBlock *MBB = lis_.getMBBFromIndex(VNI->def);
740 assert(MBB && "Phi-def has no defining MBB");
741 // Connect to values live out of predecessors.
742 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
743 PE = MBB->pred_end(); PI != PE; ++PI)
744 if (const VNInfo *PVNI =
745 LI->getVNInfoAt(lis_.getMBBEndIdx(*PI).getPrevSlot()))
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000746 eqClass_.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000747 } else {
748 // Normal value defined by an instruction. Check for two-addr redef.
749 // FIXME: This could be coincidental. Should we really check for a tied
750 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000751 // Note that VNI->def may be a use slot for an early clobber def.
752 if (const VNInfo *UVNI = LI->getVNInfoAt(VNI->def.getPrevSlot()))
753 eqClass_.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000754 }
755 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000756
757 // Lump all the unused values in with the last used value.
758 if (used && unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000759 eqClass_.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000760
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000761 eqClass_.compress();
762 return eqClass_.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000763}
764
765void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[]) {
766 assert(LIV[0] && "LIV[0] must be set");
767 LiveInterval &LI = *LIV[0];
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000768
769 // First move runs to new intervals.
770 LiveInterval::iterator J = LI.begin(), E = LI.end();
771 while (J != E && eqClass_[J->valno->id] == 0)
772 ++J;
773 for (LiveInterval::iterator I = J; I != E; ++I) {
774 if (unsigned eq = eqClass_[I->valno->id]) {
Benjamin Kramerccefe322010-10-09 16:36:44 +0000775 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000776 "New intervals should be empty");
777 LIV[eq]->ranges.push_back(*I);
778 } else
779 *J++ = *I;
780 }
781 LI.ranges.erase(J, E);
782
783 // Transfer VNInfos to their new owners and renumber them.
784 unsigned j = 0, e = LI.getNumValNums();
785 while (j != e && eqClass_[j] == 0)
786 ++j;
787 for (unsigned i = j; i != e; ++i) {
788 VNInfo *VNI = LI.getValNumInfo(i);
789 if (unsigned eq = eqClass_[i]) {
790 VNI->id = LIV[eq]->getNumValNums();
791 LIV[eq]->valnos.push_back(VNI);
792 } else {
793 VNI->id = j;
794 LI.valnos[j++] = VNI;
795 }
796 }
797 LI.valnos.resize(j);
798}