blob: 585e3a2dc24ff4e12d036ce0b97df0f78a1b33a3 [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 }
Oscar Fuentes3aefa772011-03-08 19:26:21 +000039 bool operator()(const LiveRange &A, SlotIndex B) const {
Jakob Stoklund Olesenb64f6692011-03-03 04:23:52 +000040 return A.end < B;
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000041 }
Jakob Stoklund Olesen5bf76cd2011-03-08 19:37:54 +000042 bool operator()(const LiveRange &A, const LiveRange &B) const {
43 return A.end < B.end;
44 }
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000045};
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000046}
Chris Lattnerfb449b92004-07-23 17:49:16 +000047
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000048LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen201ecfc2010-10-05 18:48:55 +000049 assert(Pos.isValid() && "Cannot search for an invalid index");
Jakob Stoklund Olesenb64f6692011-03-03 04:23:52 +000050 return std::upper_bound(begin(), end(), Pos, CompEnd());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000051}
52
53/// killedInRange - Return true if the interval has kills in [Start,End).
54bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
55 Ranges::const_iterator r =
56 std::lower_bound(ranges.begin(), ranges.end(), End);
57
58 // Now r points to the first interval with start >= End, or ranges.end().
59 if (r == ranges.begin())
60 return false;
61
62 --r;
63 // Now r points to the last interval with end <= End.
64 // r->end is the kill point.
65 return r->end >= Start && r->end < End;
66}
67
Chris Lattnerbae74d92004-11-18 03:47:34 +000068// overlaps - Return true if the intersection of the two live intervals is
69// not empty.
70//
Chris Lattnerfb449b92004-07-23 17:49:16 +000071// An example for overlaps():
72//
73// 0: A = ...
74// 4: B = ...
75// 8: C = A + B ;; last use of A
76//
77// The live intervals should look like:
78//
79// A = [3, 11)
80// B = [7, x)
81// C = [11, y)
82//
83// A->overlaps(C) should return false since we want to be able to join
84// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000085//
86bool LiveInterval::overlapsFrom(const LiveInterval& other,
87 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000088 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000089 const_iterator i = begin();
90 const_iterator ie = end();
91 const_iterator j = StartPos;
92 const_iterator je = other.end();
93
94 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000095 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000096
Chris Lattnerfb449b92004-07-23 17:49:16 +000097 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000098 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000099 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000100 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000101 ++StartPos;
102 if (StartPos != other.end() && StartPos->start <= i->start) {
103 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000104 j = std::upper_bound(j, je, i->start);
105 if (j != other.ranges.begin()) --j;
106 }
Chris Lattneraa141472004-07-23 18:40:00 +0000107 } else {
108 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000109 }
110
Chris Lattner9fddc122004-11-18 05:28:21 +0000111 if (j == je) return false;
112
113 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000114 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000115 std::swap(i, j);
116 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000117 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000118
119 if (i->end > j->start)
120 return true;
121 ++i;
122 }
123
124 return false;
125}
126
Evan Chengcccdb2b2009-04-18 08:52:15 +0000127/// overlaps - Return true if the live interval overlaps a range specified
128/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000129bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000130 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000131 const_iterator I = std::lower_bound(begin(), end(), End);
132 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000133}
134
Lang Hames6f4e4df2010-07-26 01:49:41 +0000135
136/// ValNo is dead, remove it. If it is the largest value number, just nuke it
137/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
138/// it can be nuked later.
139void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
140 if (ValNo->id == getNumValNums()-1) {
141 do {
142 valnos.pop_back();
143 } while (!valnos.empty() && valnos.back()->isUnused());
144 } else {
145 ValNo->setIsUnused(true);
146 }
147}
148
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000149/// RenumberValues - Renumber all values in order of appearance and delete the
150/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000151void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000152 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000153 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000154 valnos.clear();
155 for (const_iterator I = begin(), E = end(); I != E; ++I) {
156 VNInfo *VNI = I->valno;
157 if (!Seen.insert(VNI))
158 continue;
159 assert(!VNI->isUnused() && "Unused valno used by live range");
160 VNI->id = (unsigned)valnos.size();
161 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000162 VNI->setHasPHIKill(false);
163 if (VNI->isPHIDef())
164 seenPHIDef = true;
165 }
166
167 // Recompute phi kill flags.
168 if (!seenPHIDef)
169 return;
170 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
171 VNInfo *VNI = *I;
172 if (!VNI->isPHIDef())
173 continue;
174 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
175 assert(PHIBB && "No basic block for phi-def");
176 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
177 PE = PHIBB->pred_end(); PI != PE; ++PI) {
178 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
179 if (KVNI)
180 KVNI->setHasPHIKill(true);
181 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000182 }
183}
184
Chris Lattnerb26c2152004-07-23 19:38:44 +0000185/// extendIntervalEndTo - This method is used when we want to extend the range
186/// specified by I to end at the specified endpoint. To do this, we should
187/// merge and eliminate all ranges that this will overlap with. The iterator is
188/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000189void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000190 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000191 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000192
Chris Lattnerb26c2152004-07-23 19:38:44 +0000193 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000194 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000195 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000196 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000197 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000198
199 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
200 I->end = std::max(NewEnd, prior(MergeTo)->end);
201
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000202 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000203 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000204
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000205 // If the newly formed range now touches the range after it and if they have
206 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000207 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000208 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000209 I->end = Next->end;
210 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000211 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000212}
213
214
215/// extendIntervalStartTo - This method is used when we want to extend the range
216/// specified by I to start at the specified endpoint. To do this, we should
217/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000218LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000219LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000220 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000222
223 // Search for the first interval that we can't merge with.
224 Ranges::iterator MergeTo = I;
225 do {
226 if (MergeTo == ranges.begin()) {
227 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000228 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000229 return I;
230 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000231 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000232 --MergeTo;
233 } while (NewStart <= MergeTo->start);
234
235 // If we start in the middle of another interval, just delete a range and
236 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000237 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000238 MergeTo->end = I->end;
239 } else {
240 // Otherwise, extend the interval right after.
241 ++MergeTo;
242 MergeTo->start = NewStart;
243 MergeTo->end = I->end;
244 }
245
Oscar Fuentesee56c422010-08-02 06:00:15 +0000246 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000247 return MergeTo;
248}
249
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000250LiveInterval::iterator
251LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000252 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000253 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000254
255 // If the inserted interval starts in the middle or right at the end of
256 // another interval, just extend that interval to contain the range of LR.
257 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000258 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000259 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000260 if (B->start <= Start && B->end >= Start) {
261 extendIntervalEndTo(B, End);
262 return B;
263 }
264 } else {
265 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000266 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000267 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000268 "Cannot overlap two LiveRanges with differing ValID's"
269 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000270 }
271 }
272
273 // Otherwise, if this range ends in the middle of, or right next to, another
274 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000275 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000276 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000277 if (it->start <= End) {
278 it = extendIntervalStartTo(it, Start);
279
280 // If LR is a complete superset of an interval, we may need to grow its
281 // endpoint as well.
282 if (End > it->end)
283 extendIntervalEndTo(it, End);
284 return it;
285 }
286 } else {
287 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000288 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000289 assert(it->start >= End &&
290 "Cannot overlap two LiveRanges with differing ValID's");
291 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000292 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000293
294 // Otherwise, this is just a new range that doesn't interact with anything.
295 // Insert it.
296 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000297}
298
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000299/// extendInBlock - If this interval is live before UseIdx in the basic
300/// block that starts at StartIdx, extend it to be live at UseIdx and return
301/// the value. If there is no live range before UseIdx, return NULL.
302VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex UseIdx) {
303 if (empty())
304 return 0;
305 iterator I = std::upper_bound(begin(), end(), UseIdx);
306 if (I == begin())
307 return 0;
308 --I;
309 if (I->end <= StartIdx)
310 return 0;
311 if (I->end <= UseIdx)
312 extendIntervalEndTo(I, UseIdx.getNextSlot());
313 return I->valno;
314}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000315
316/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000317/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000318void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000319 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000320 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000321 Ranges::iterator I = find(Start);
322 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000323 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000324
325 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000326 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000327 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000328 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000329 if (RemoveDeadValNo) {
330 // Check if val# is dead.
331 bool isDead = true;
332 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
333 if (II != I && II->valno == ValNo) {
334 isDead = false;
335 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000336 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000337 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000338 // Now that ValNo is dead, remove it.
339 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000340 }
341 }
342
Chris Lattnerabf295f2004-07-24 02:52:23 +0000343 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000344 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000345 I->start = End;
346 return;
347 }
348
349 // Otherwise if the span we are removing is at the end of the LiveRange,
350 // adjust the other way.
351 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000352 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000353 return;
354 }
355
356 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000357 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000358 I->end = Start; // Trim the old interval.
359
360 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000361 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000362}
363
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000364/// removeValNo - Remove all the ranges defined by the specified value#.
365/// Also remove the value# from value# list.
366void LiveInterval::removeValNo(VNInfo *ValNo) {
367 if (empty()) return;
368 Ranges::iterator I = ranges.end();
369 Ranges::iterator E = ranges.begin();
370 do {
371 --I;
372 if (I->valno == ValNo)
373 ranges.erase(I);
374 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000375 // Now that ValNo is dead, remove it.
376 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000377}
Lang Hames86511252009-09-04 20:41:11 +0000378
Lang Hames86511252009-09-04 20:41:11 +0000379/// findDefinedVNInfo - Find the VNInfo defined by the specified
380/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000381VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000382 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000383 i != e; ++i) {
384 if ((*i)->def == Idx)
385 return *i;
386 }
387
388 return 0;
389}
390
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000391/// join - Join two live intervals (this, and other) together. This applies
392/// mappings to the value numbers in the LHS/RHS intervals as specified. If
393/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000394void LiveInterval::join(LiveInterval &Other,
395 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000396 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000397 SmallVector<VNInfo*, 16> &NewVNInfo,
398 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000399 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000400 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000401 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000402 unsigned NumVals = getNumValNums();
403 unsigned NumNewVals = NewVNInfo.size();
404 for (unsigned i = 0; i != NumVals; ++i) {
405 unsigned LHSValID = LHSValNoAssignments[i];
406 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000407 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000408 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000409 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000410
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000411 // If we have to apply a mapping to our base interval assignment, rewrite it
412 // now.
413 if (MustMapCurValNos) {
414 // Map the first live range.
415 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000416 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000417 ++OutIt;
418 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000419 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000420
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000421 // If this live range has the same value # as its immediate predecessor,
422 // and if they are neighbors, remove one LiveRange. This happens when we
423 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000424 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000425 (OutIt-1)->end = OutIt->end;
426 } else {
427 if (I != OutIt) {
428 OutIt->start = I->start;
429 OutIt->end = I->end;
430 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000431
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000432 // Didn't merge, on to the next one.
433 ++OutIt;
434 }
435 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000436
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000437 // If we merge some live ranges, chop off the end.
438 ranges.erase(OutIt, end());
439 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000440
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000441 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000442 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000443 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
444 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
445
446 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000447 // LiveInterval now. Also remove dead val#'s.
448 unsigned NumValNos = 0;
449 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000450 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000451 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000452 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000453 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000454 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000455 valnos[NumValNos] = VNI;
456 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000457 }
458 }
Evan Cheng34301352007-09-01 02:03:17 +0000459 if (NumNewVals < NumVals)
460 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000461
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000462 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000463 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000464 unsigned RangeNo = 0;
465 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
466 // Map the valno in the other live range to the current live range.
467 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000468 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000469 InsertPos = addRangeFrom(*I, InsertPos);
470 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000471
David Greene29ff37f2009-07-22 20:08:25 +0000472 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000473}
474
Chris Lattnerf21f0202006-09-02 05:26:59 +0000475/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
476/// interval as the specified value number. The LiveRanges in RHS are
477/// allowed to overlap with LiveRanges in the current interval, but only if
478/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000479void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000480 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000481 // TODO: Make this more efficient.
482 iterator InsertPos = begin();
483 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000484 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000485 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000486 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000487 InsertPos = addRangeFrom(Tmp, InsertPos);
488 }
489}
490
491
Evan Cheng32dfbea2007-10-12 08:50:34 +0000492/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
493/// in RHS into this live interval as the specified value number.
494/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000495/// current interval, it will replace the value numbers of the overlaped
496/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000497void LiveInterval::MergeValueInAsValue(
498 const LiveInterval &RHS,
499 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000500 SmallVector<VNInfo*, 4> ReplacedValNos;
501 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000502 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000503 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000504 if (I->valno != RHSValNo)
505 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000506 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000507 IP = std::upper_bound(IP, end(), Start);
508 // If the start of this range overlaps with an existing liverange, trim it.
509 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000510 if (IP[-1].valno != LHSValNo) {
511 ReplacedValNos.push_back(IP[-1].valno);
512 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000513 }
514 Start = IP[-1].end;
515 // Trimmed away the whole range?
516 if (Start >= End) continue;
517 }
518 // If the end of this range overlaps with an existing liverange, trim it.
519 if (IP != end() && End > IP->start) {
520 if (IP->valno != LHSValNo) {
521 ReplacedValNos.push_back(IP->valno);
522 IP->valno = LHSValNo; // Update val#.
523 }
524 End = IP->start;
525 // If this trimmed away the whole range, ignore it.
526 if (Start == End) continue;
527 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000528
Evan Cheng32dfbea2007-10-12 08:50:34 +0000529 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000530 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
531 }
532
533
534 SmallSet<VNInfo*, 4> Seen;
535 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
536 VNInfo *V1 = ReplacedValNos[i];
537 if (Seen.insert(V1)) {
538 bool isDead = true;
539 for (const_iterator I = begin(), E = end(); I != E; ++I)
540 if (I->valno == V1) {
541 isDead = false;
542 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000543 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000544 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000545 // Now that V1 is dead, remove it.
546 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000547 }
548 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000549 }
550}
551
552
Evan Chenga2e64352009-03-11 00:03:21 +0000553
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000554/// MergeValueNumberInto - This method is called when two value nubmers
555/// are found to be equivalent. This eliminates V1, replacing all
556/// LiveRanges with the V1 value number with the V2 value number. This can
557/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000558VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000559 assert(V1 != V2 && "Identical value#'s are always equivalent!");
560
561 // This code actually merges the (numerically) larger value number into the
562 // smaller value number, which is likely to allow us to compactify the value
563 // space. The only thing we have to be careful of is to preserve the
564 // instruction that defines the result value.
565
566 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000567 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000568 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000569 std::swap(V1, V2);
570 }
571
572 // Merge V1 live ranges into V2.
573 for (iterator I = begin(); I != end(); ) {
574 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000575 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000576
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000577 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
578 // range, extend it.
579 if (LR != begin()) {
580 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000581 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000582 Prev->end = LR->end;
583
584 // Erase this live-range.
585 ranges.erase(LR);
586 I = Prev+1;
587 LR = Prev;
588 }
589 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000590
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000591 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
592 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000593 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000594
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000595 // If we can merge it into later V2 live ranges, do so now. We ignore any
596 // following V1 live ranges, as they will be merged in subsequent iterations
597 // of the loop.
598 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000599 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000600 LR->end = I->end;
601 ranges.erase(I);
602 I = LR+1;
603 }
604 }
605 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000606
Jakob Stoklund Olesene0a73ec2010-10-01 23:52:25 +0000607 // Merge the relevant flags.
608 V2->mergeFlags(V1);
609
Lang Hames6f4e4df2010-07-26 01:49:41 +0000610 // Now that V1 is dead, remove it.
611 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000612
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000613 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000614}
615
Evan Cheng32dfbea2007-10-12 08:50:34 +0000616void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000617 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000618 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000619 ranges.clear();
620 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000621 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000622 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
623
Evan Cheng32dfbea2007-10-12 08:50:34 +0000624 weight = RHS.weight;
625 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
626 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000627 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000628 }
629 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
630 const LiveRange &LR = RHS.ranges[i];
631 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
632 }
633}
634
Evan Chenge52eef82007-04-17 20:25:11 +0000635unsigned LiveInterval::getSize() const {
636 unsigned Sum = 0;
637 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000638 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000639 return Sum;
640}
641
David Greene29ff37f2009-07-22 20:08:25 +0000642/// ComputeJoinedWeight - Set the weight of a live interval Joined
643/// after Other has been merged into it.
644void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
645 // If either of these intervals was spilled, the weight is the
646 // weight of the non-spilled interval. This can only happen with
647 // iterative coalescers.
648
David Greene92b78bb2009-07-22 22:32:19 +0000649 if (Other.weight != HUGE_VALF) {
650 weight += Other.weight;
651 }
652 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000653 !TargetRegisterInfo::isPhysicalRegister(reg)) {
654 // Remove this assert if you have an iterative coalescer
655 assert(0 && "Joining to spilled interval");
656 weight = Other.weight;
657 }
David Greene29ff37f2009-07-22 20:08:25 +0000658 else {
659 // Otherwise the weight stays the same
660 // Remove this assert if you have an iterative coalescer
661 assert(0 && "Joining from spilled interval");
662 }
663}
664
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000665raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
666 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
667}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000668
Chris Lattnerabf295f2004-07-24 02:52:23 +0000669void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000670 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000671}
672
Chris Lattnerc02497f2009-08-23 03:47:42 +0000673void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000674 OS << PrintReg(reg, TRI);
675 if (weight != 0)
676 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000677
Chris Lattner38135af2005-05-14 05:34:15 +0000678 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000679 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000680 else {
681 OS << " = ";
682 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000683 E = ranges.end(); I != E; ++I) {
684 OS << *I;
685 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
686 }
Chris Lattner38135af2005-05-14 05:34:15 +0000687 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000688
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000689 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000690 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000691 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000692 unsigned vnum = 0;
693 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
694 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000695 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000696 if (vnum) OS << " ";
697 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000698 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000699 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000700 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000701 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000702 if (vni->isPHIDef())
703 OS << "-phidef";
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000704 if (vni->hasPHIKill())
705 OS << "-phikill";
706 if (vni->hasRedefByEC())
707 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000708 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000709 }
710 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000711}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000712
713void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000714 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000715}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000716
717
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000718void LiveRange::print(raw_ostream &os) const {
719 os << *this;
720}
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000721
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000722unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000723 // Create initial equivalence classes.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000724 eqClass_.clear();
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000725 eqClass_.grow(LI->getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000726
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000727 const VNInfo *used = 0, *unused = 0;
728
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +0000729 // Determine connections.
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000730 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
731 I != E; ++I) {
732 const VNInfo *VNI = *I;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000733 // Group all unused values into one class.
734 if (VNI->isUnused()) {
735 if (unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000736 eqClass_.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000737 unused = VNI;
738 continue;
739 }
740 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000741 if (VNI->isPHIDef()) {
742 const MachineBasicBlock *MBB = lis_.getMBBFromIndex(VNI->def);
743 assert(MBB && "Phi-def has no defining MBB");
744 // Connect to values live out of predecessors.
745 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
746 PE = MBB->pred_end(); PI != PE; ++PI)
747 if (const VNInfo *PVNI =
748 LI->getVNInfoAt(lis_.getMBBEndIdx(*PI).getPrevSlot()))
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000749 eqClass_.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000750 } else {
751 // Normal value defined by an instruction. Check for two-addr redef.
752 // FIXME: This could be coincidental. Should we really check for a tied
753 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000754 // Note that VNI->def may be a use slot for an early clobber def.
755 if (const VNInfo *UVNI = LI->getVNInfoAt(VNI->def.getPrevSlot()))
756 eqClass_.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000757 }
758 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000759
760 // Lump all the unused values in with the last used value.
761 if (used && unused)
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000762 eqClass_.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +0000763
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +0000764 eqClass_.compress();
765 return eqClass_.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000766}
767
768void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[]) {
769 assert(LIV[0] && "LIV[0] must be set");
770 LiveInterval &LI = *LIV[0];
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000771
772 // First move runs to new intervals.
773 LiveInterval::iterator J = LI.begin(), E = LI.end();
774 while (J != E && eqClass_[J->valno->id] == 0)
775 ++J;
776 for (LiveInterval::iterator I = J; I != E; ++I) {
777 if (unsigned eq = eqClass_[I->valno->id]) {
Benjamin Kramerccefe322010-10-09 16:36:44 +0000778 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000779 "New intervals should be empty");
780 LIV[eq]->ranges.push_back(*I);
781 } else
782 *J++ = *I;
783 }
784 LI.ranges.erase(J, E);
785
786 // Transfer VNInfos to their new owners and renumber them.
787 unsigned j = 0, e = LI.getNumValNums();
788 while (j != e && eqClass_[j] == 0)
789 ++j;
790 for (unsigned i = j; i != e; ++i) {
791 VNInfo *VNI = LI.getValNumInfo(i);
792 if (unsigned eq = eqClass_[i]) {
793 VNI->id = LIV[eq]->getNumValNums();
794 LIV[eq]->valnos.push_back(VNI);
795 } else {
796 VNI->id = j;
797 LI.valnos[j++] = VNI;
798 }
799 }
800 LI.valnos.resize(j);
801}