blob: 59f380ad26414325cc75f9df098b2ecd2aa17435 [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
33// An example for liveAt():
34//
Chris Lattneraa141472004-07-23 18:40:00 +000035// this = [1,4), liveAt(0) will return false. The instruction defining this
36// spans slots [0,3]. The interval belongs to an spilled definition of the
37// variable it represents. This is because slot 1 is used (def slot) and spans
38// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000039//
Lang Hames233a60e2009-11-03 23:52:08 +000040bool LiveInterval::liveAt(SlotIndex I) const {
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000041 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000042
Chris Lattnerfb449b92004-07-23 17:49:16 +000043 if (r == ranges.begin())
44 return false;
45
46 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000047 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000048}
49
Evan Chengc8d044e2008-02-15 18:24:29 +000050// liveBeforeAndAt - Check if the interval is live at the index and the index
51// just before it. If index is liveAt, check if it starts a new live range.
52// If it does, then check if the previous live range ends at index-1.
Lang Hames233a60e2009-11-03 23:52:08 +000053bool LiveInterval::liveBeforeAndAt(SlotIndex I) const {
Evan Chengc8d044e2008-02-15 18:24:29 +000054 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
55
56 if (r == ranges.begin())
57 return false;
58
59 --r;
60 if (!r->contains(I))
61 return false;
62 if (I != r->start)
63 return true;
64 // I is the start of a live range. Check if the previous live range ends
65 // at I-1.
66 if (r == ranges.begin())
67 return false;
68 return r->end == I;
69}
70
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000071/// killedAt - Return true if a live range ends at index. Note that the kill
72/// point is not contained in the half-open live range. It is usually the
73/// getDefIndex() slot following its last use.
74bool LiveInterval::killedAt(SlotIndex I) const {
75 Ranges::const_iterator r = std::lower_bound(ranges.begin(), ranges.end(), I);
76
77 // Now r points to the first interval with start >= I, or ranges.end().
78 if (r == ranges.begin())
79 return false;
80
81 --r;
82 // Now r points to the last interval with end <= I.
83 // r->end is the kill point.
84 return r->end == I;
85}
86
87/// killedInRange - Return true if the interval has kills in [Start,End).
88bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
89 Ranges::const_iterator r =
90 std::lower_bound(ranges.begin(), ranges.end(), End);
91
92 // Now r points to the first interval with start >= End, or ranges.end().
93 if (r == ranges.begin())
94 return false;
95
96 --r;
97 // Now r points to the last interval with end <= End.
98 // r->end is the kill point.
99 return r->end >= Start && r->end < End;
100}
101
Chris Lattnerbae74d92004-11-18 03:47:34 +0000102// overlaps - Return true if the intersection of the two live intervals is
103// not empty.
104//
Chris Lattnerfb449b92004-07-23 17:49:16 +0000105// An example for overlaps():
106//
107// 0: A = ...
108// 4: B = ...
109// 8: C = A + B ;; last use of A
110//
111// The live intervals should look like:
112//
113// A = [3, 11)
114// B = [7, x)
115// C = [11, y)
116//
117// A->overlaps(C) should return false since we want to be able to join
118// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +0000119//
120bool LiveInterval::overlapsFrom(const LiveInterval& other,
121 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +0000122 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +0000123 const_iterator i = begin();
124 const_iterator ie = end();
125 const_iterator j = StartPos;
126 const_iterator je = other.end();
127
128 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000129 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000130
Chris Lattnerfb449b92004-07-23 17:49:16 +0000131 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000132 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000133 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000134 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000135 ++StartPos;
136 if (StartPos != other.end() && StartPos->start <= i->start) {
137 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000138 j = std::upper_bound(j, je, i->start);
139 if (j != other.ranges.begin()) --j;
140 }
Chris Lattneraa141472004-07-23 18:40:00 +0000141 } else {
142 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000143 }
144
Chris Lattner9fddc122004-11-18 05:28:21 +0000145 if (j == je) return false;
146
147 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000148 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000149 std::swap(i, j);
150 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000151 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000152
153 if (i->end > j->start)
154 return true;
155 ++i;
156 }
157
158 return false;
159}
160
Evan Chengcccdb2b2009-04-18 08:52:15 +0000161/// overlaps - Return true if the live interval overlaps a range specified
162/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000163bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000164 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000165 const_iterator I = std::lower_bound(begin(), end(), End);
166 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000167}
168
Lang Hames6f4e4df2010-07-26 01:49:41 +0000169
170/// ValNo is dead, remove it. If it is the largest value number, just nuke it
171/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
172/// it can be nuked later.
173void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
174 if (ValNo->id == getNumValNums()-1) {
175 do {
176 valnos.pop_back();
177 } while (!valnos.empty() && valnos.back()->isUnused());
178 } else {
179 ValNo->setIsUnused(true);
180 }
181}
182
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000183/// RenumberValues - Renumber all values in order of appearance and delete the
184/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000185void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000186 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000187 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000188 valnos.clear();
189 for (const_iterator I = begin(), E = end(); I != E; ++I) {
190 VNInfo *VNI = I->valno;
191 if (!Seen.insert(VNI))
192 continue;
193 assert(!VNI->isUnused() && "Unused valno used by live range");
194 VNI->id = (unsigned)valnos.size();
195 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000196 VNI->setHasPHIKill(false);
197 if (VNI->isPHIDef())
198 seenPHIDef = true;
199 }
200
201 // Recompute phi kill flags.
202 if (!seenPHIDef)
203 return;
204 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
205 VNInfo *VNI = *I;
206 if (!VNI->isPHIDef())
207 continue;
208 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
209 assert(PHIBB && "No basic block for phi-def");
210 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
211 PE = PHIBB->pred_end(); PI != PE; ++PI) {
212 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
213 if (KVNI)
214 KVNI->setHasPHIKill(true);
215 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000216 }
217}
218
Chris Lattnerb26c2152004-07-23 19:38:44 +0000219/// extendIntervalEndTo - This method is used when we want to extend the range
220/// specified by I to end at the specified endpoint. To do this, we should
221/// merge and eliminate all ranges that this will overlap with. The iterator is
222/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000223void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000224 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000225 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000226
Chris Lattnerb26c2152004-07-23 19:38:44 +0000227 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000228 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000229 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000230 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000231 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000232
233 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
234 I->end = std::max(NewEnd, prior(MergeTo)->end);
235
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000236 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000237 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000238
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000239 // If the newly formed range now touches the range after it and if they have
240 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000241 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000242 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000243 I->end = Next->end;
244 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000245 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000246}
247
248
249/// extendIntervalStartTo - This method is used when we want to extend the range
250/// specified by I to start at the specified endpoint. To do this, we should
251/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000253LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000254 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000255 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000256
257 // Search for the first interval that we can't merge with.
258 Ranges::iterator MergeTo = I;
259 do {
260 if (MergeTo == ranges.begin()) {
261 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000262 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000263 return I;
264 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000265 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000266 --MergeTo;
267 } while (NewStart <= MergeTo->start);
268
269 // If we start in the middle of another interval, just delete a range and
270 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000271 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000272 MergeTo->end = I->end;
273 } else {
274 // Otherwise, extend the interval right after.
275 ++MergeTo;
276 MergeTo->start = NewStart;
277 MergeTo->end = I->end;
278 }
279
Oscar Fuentesee56c422010-08-02 06:00:15 +0000280 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000281 return MergeTo;
282}
283
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000284LiveInterval::iterator
285LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000286 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000287 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000288
289 // If the inserted interval starts in the middle or right at the end of
290 // another interval, just extend that interval to contain the range of LR.
291 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000292 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000293 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000294 if (B->start <= Start && B->end >= Start) {
295 extendIntervalEndTo(B, End);
296 return B;
297 }
298 } else {
299 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000300 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000301 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000302 "Cannot overlap two LiveRanges with differing ValID's"
303 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000304 }
305 }
306
307 // Otherwise, if this range ends in the middle of, or right next to, another
308 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000309 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000310 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000311 if (it->start <= End) {
312 it = extendIntervalStartTo(it, Start);
313
314 // If LR is a complete superset of an interval, we may need to grow its
315 // endpoint as well.
316 if (End > it->end)
317 extendIntervalEndTo(it, End);
318 return it;
319 }
320 } else {
321 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000322 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000323 assert(it->start >= End &&
324 "Cannot overlap two LiveRanges with differing ValID's");
325 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000326 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000327
328 // Otherwise, this is just a new range that doesn't interact with anything.
329 // Insert it.
330 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000331}
332
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000333/// isInOneLiveRange - Return true if the range specified is entirely in
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000334/// a single LiveRange of the live interval.
Lang Hames233a60e2009-11-03 23:52:08 +0000335bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000336 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
337 if (I == ranges.begin())
338 return false;
339 --I;
Lang Hames86511252009-09-04 20:41:11 +0000340 return I->containsRange(Start, End);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000341}
342
Chris Lattnerabf295f2004-07-24 02:52:23 +0000343
344/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000345/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000346void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000347 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000348 // Find the LiveRange containing this span.
349 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
350 assert(I != ranges.begin() && "Range is not in interval!");
351 --I;
Lang Hames86511252009-09-04 20:41:11 +0000352 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000353
354 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000355 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000356 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000357 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000358 if (RemoveDeadValNo) {
359 // Check if val# is dead.
360 bool isDead = true;
361 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
362 if (II != I && II->valno == ValNo) {
363 isDead = false;
364 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000365 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000366 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000367 // Now that ValNo is dead, remove it.
368 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000369 }
370 }
371
Chris Lattnerabf295f2004-07-24 02:52:23 +0000372 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000373 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000374 I->start = End;
375 return;
376 }
377
378 // Otherwise if the span we are removing is at the end of the LiveRange,
379 // adjust the other way.
380 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000381 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000382 return;
383 }
384
385 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000386 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000387 I->end = Start; // Trim the old interval.
388
389 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000390 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000391}
392
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000393/// removeValNo - Remove all the ranges defined by the specified value#.
394/// Also remove the value# from value# list.
395void LiveInterval::removeValNo(VNInfo *ValNo) {
396 if (empty()) return;
397 Ranges::iterator I = ranges.end();
398 Ranges::iterator E = ranges.begin();
399 do {
400 --I;
401 if (I->valno == ValNo)
402 ranges.erase(I);
403 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000404 // Now that ValNo is dead, remove it.
405 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000406}
Lang Hames86511252009-09-04 20:41:11 +0000407
Chris Lattnerabf295f2004-07-24 02:52:23 +0000408/// getLiveRangeContaining - Return the live range that contains the
409/// specified index, or null if there is none.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000410LiveInterval::const_iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000411LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000412 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000413 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000414 --It;
415 if (It->contains(Idx))
416 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000417 }
418
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000419 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000420}
421
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000422LiveInterval::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000423LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000424 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000425 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000426 --It;
427 if (It->contains(Idx))
428 return It;
429 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000430
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000431 return end();
432}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000433
Lang Hames86511252009-09-04 20:41:11 +0000434/// findDefinedVNInfo - Find the VNInfo defined by the specified
435/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000436VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000437 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000438 i != e; ++i) {
439 if ((*i)->def == Idx)
440 return *i;
441 }
442
443 return 0;
444}
445
446/// findDefinedVNInfo - Find the VNInfo defined by the specified
447/// register (stack inteval).
448VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
449 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
450 i != e; ++i) {
451 if ((*i)->getReg() == reg)
452 return *i;
453 }
454 return 0;
Evan Cheng3f32d652008-06-04 09:18:41 +0000455}
456
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000457/// join - Join two live intervals (this, and other) together. This applies
458/// mappings to the value numbers in the LHS/RHS intervals as specified. If
459/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000460void LiveInterval::join(LiveInterval &Other,
461 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000462 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000463 SmallVector<VNInfo*, 16> &NewVNInfo,
464 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000465 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000466 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000467 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000468 unsigned NumVals = getNumValNums();
469 unsigned NumNewVals = NewVNInfo.size();
470 for (unsigned i = 0; i != NumVals; ++i) {
471 unsigned LHSValID = LHSValNoAssignments[i];
472 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000473 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000474 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000475 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000476
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000477 // If we have to apply a mapping to our base interval assignment, rewrite it
478 // now.
479 if (MustMapCurValNos) {
480 // Map the first live range.
481 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000482 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000483 ++OutIt;
484 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000485 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000486
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000487 // If this live range has the same value # as its immediate predecessor,
488 // and if they are neighbors, remove one LiveRange. This happens when we
489 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000490 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000491 (OutIt-1)->end = OutIt->end;
492 } else {
493 if (I != OutIt) {
494 OutIt->start = I->start;
495 OutIt->end = I->end;
496 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000497
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000498 // Didn't merge, on to the next one.
499 ++OutIt;
500 }
501 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000502
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000503 // If we merge some live ranges, chop off the end.
504 ranges.erase(OutIt, end());
505 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000506
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000507 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000508 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000509 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
510 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
511
512 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000513 // LiveInterval now. Also remove dead val#'s.
514 unsigned NumValNos = 0;
515 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000516 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000517 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000518 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000519 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000520 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000521 valnos[NumValNos] = VNI;
522 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000523 }
524 }
Evan Cheng34301352007-09-01 02:03:17 +0000525 if (NumNewVals < NumVals)
526 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000527
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000528 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000529 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000530 unsigned RangeNo = 0;
531 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
532 // Map the valno in the other live range to the current live range.
533 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000534 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000535 InsertPos = addRangeFrom(*I, InsertPos);
536 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000537
David Greene29ff37f2009-07-22 20:08:25 +0000538 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000539}
540
Chris Lattnerf21f0202006-09-02 05:26:59 +0000541/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
542/// interval as the specified value number. The LiveRanges in RHS are
543/// allowed to overlap with LiveRanges in the current interval, but only if
544/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000545void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000546 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000547 // TODO: Make this more efficient.
548 iterator InsertPos = begin();
549 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000550 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000551 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000552 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000553 InsertPos = addRangeFrom(Tmp, InsertPos);
554 }
555}
556
557
Evan Cheng32dfbea2007-10-12 08:50:34 +0000558/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
559/// in RHS into this live interval as the specified value number.
560/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000561/// current interval, it will replace the value numbers of the overlaped
562/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000563void LiveInterval::MergeValueInAsValue(
564 const LiveInterval &RHS,
565 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000566 SmallVector<VNInfo*, 4> ReplacedValNos;
567 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000568 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000569 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000570 if (I->valno != RHSValNo)
571 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000572 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000573 IP = std::upper_bound(IP, end(), Start);
574 // If the start of this range overlaps with an existing liverange, trim it.
575 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000576 if (IP[-1].valno != LHSValNo) {
577 ReplacedValNos.push_back(IP[-1].valno);
578 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000579 }
580 Start = IP[-1].end;
581 // Trimmed away the whole range?
582 if (Start >= End) continue;
583 }
584 // If the end of this range overlaps with an existing liverange, trim it.
585 if (IP != end() && End > IP->start) {
586 if (IP->valno != LHSValNo) {
587 ReplacedValNos.push_back(IP->valno);
588 IP->valno = LHSValNo; // Update val#.
589 }
590 End = IP->start;
591 // If this trimmed away the whole range, ignore it.
592 if (Start == End) continue;
593 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000594
Evan Cheng32dfbea2007-10-12 08:50:34 +0000595 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000596 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
597 }
598
599
600 SmallSet<VNInfo*, 4> Seen;
601 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
602 VNInfo *V1 = ReplacedValNos[i];
603 if (Seen.insert(V1)) {
604 bool isDead = true;
605 for (const_iterator I = begin(), E = end(); I != E; ++I)
606 if (I->valno == V1) {
607 isDead = false;
608 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000609 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000610 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000611 // Now that V1 is dead, remove it.
612 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000613 }
614 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000615 }
616}
617
618
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000619/// MergeInClobberRanges - For any live ranges that are not defined in the
620/// current interval, but are defined in the Clobbers interval, mark them
621/// used with an unknown definition value.
Lang Hames233a60e2009-11-03 23:52:08 +0000622void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
623 const LiveInterval &Clobbers,
Benjamin Kramer991de142010-03-30 20:16:45 +0000624 VNInfo::Allocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000625 if (Clobbers.empty()) return;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000626
Evan Cheng0adb5272009-04-25 09:25:19 +0000627 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000628 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000629 iterator IP = begin();
630 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000631 // For every val# in the Clobbers interval, create a new "unknown" val#.
632 VNInfo *ClobberValNo = 0;
633 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
634 if (VI != ValNoMaps.end())
635 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000636 else if (UnusedValNo)
637 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000638 else {
Lang Hames86511252009-09-04 20:41:11 +0000639 UnusedValNo = ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000640 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000641 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
642 }
643
Dan Gohman97121ba2009-04-08 00:15:30 +0000644 bool Done = false;
Lang Hames233a60e2009-11-03 23:52:08 +0000645 SlotIndex Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000646 // If a clobber range starts before an existing range and ends after
647 // it, the clobber range will need to be split into multiple ranges.
648 // Loop until the entire clobber range is handled.
649 while (!Done) {
650 Done = true;
651 IP = std::upper_bound(IP, end(), Start);
Lang Hames233a60e2009-11-03 23:52:08 +0000652 SlotIndex SubRangeStart = Start;
653 SlotIndex SubRangeEnd = End;
Dan Gohman97121ba2009-04-08 00:15:30 +0000654
655 // If the start of this range overlaps with an existing liverange, trim it.
656 if (IP != begin() && IP[-1].end > SubRangeStart) {
657 SubRangeStart = IP[-1].end;
658 // Trimmed away the whole range?
659 if (SubRangeStart >= SubRangeEnd) continue;
660 }
661 // If the end of this range overlaps with an existing liverange, trim it.
662 if (IP != end() && SubRangeEnd > IP->start) {
663 // If the clobber live range extends beyond the existing live range,
664 // it'll need at least another live range, so set the flag to keep
665 // iterating.
666 if (SubRangeEnd > IP->end) {
667 Start = IP->end;
668 Done = false;
669 }
670 SubRangeEnd = IP->start;
671 // If this trimmed away the whole range, ignore it.
672 if (SubRangeStart == SubRangeEnd) continue;
673 }
674
675 // Insert the clobber interval.
676 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
677 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000678 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000679 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000680 }
Evan Cheng27e46662009-04-27 17:35:19 +0000681
682 if (UnusedValNo) {
683 // Delete the last unused val#.
684 valnos.pop_back();
Evan Cheng27e46662009-04-27 17:35:19 +0000685 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000686}
687
Lang Hames233a60e2009-11-03 23:52:08 +0000688void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
689 SlotIndex Start,
690 SlotIndex End,
Benjamin Kramer991de142010-03-30 20:16:45 +0000691 VNInfo::Allocator &VNInfoAllocator) {
Evan Chenga2e64352009-03-11 00:03:21 +0000692 // Find a value # to use for the clobber ranges. If there is already a value#
693 // for unknown values, use it.
Lang Hames86511252009-09-04 20:41:11 +0000694 VNInfo *ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000695 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000696
Evan Chenga2e64352009-03-11 00:03:21 +0000697 iterator IP = begin();
698 IP = std::upper_bound(IP, end(), Start);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000699
Evan Chenga2e64352009-03-11 00:03:21 +0000700 // If the start of this range overlaps with an existing liverange, trim it.
701 if (IP != begin() && IP[-1].end > Start) {
702 Start = IP[-1].end;
703 // Trimmed away the whole range?
704 if (Start >= End) return;
705 }
706 // If the end of this range overlaps with an existing liverange, trim it.
707 if (IP != end() && End > IP->start) {
708 End = IP->start;
709 // If this trimmed away the whole range, ignore it.
710 if (Start == End) return;
711 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000712
Evan Chenga2e64352009-03-11 00:03:21 +0000713 // Insert the clobber interval.
714 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
715}
716
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000717/// MergeValueNumberInto - This method is called when two value nubmers
718/// are found to be equivalent. This eliminates V1, replacing all
719/// LiveRanges with the V1 value number with the V2 value number. This can
720/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000721VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000722 assert(V1 != V2 && "Identical value#'s are always equivalent!");
723
724 // This code actually merges the (numerically) larger value number into the
725 // smaller value number, which is likely to allow us to compactify the value
726 // space. The only thing we have to be careful of is to preserve the
727 // instruction that defines the result value.
728
729 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000730 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000731 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000732 std::swap(V1, V2);
733 }
734
735 // Merge V1 live ranges into V2.
736 for (iterator I = begin(); I != end(); ) {
737 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000738 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000739
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000740 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
741 // range, extend it.
742 if (LR != begin()) {
743 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000744 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000745 Prev->end = LR->end;
746
747 // Erase this live-range.
748 ranges.erase(LR);
749 I = Prev+1;
750 LR = Prev;
751 }
752 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000753
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000754 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
755 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000756 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000757
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000758 // If we can merge it into later V2 live ranges, do so now. We ignore any
759 // following V1 live ranges, as they will be merged in subsequent iterations
760 // of the loop.
761 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000762 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000763 LR->end = I->end;
764 ranges.erase(I);
765 I = LR+1;
766 }
767 }
768 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000769
Lang Hames6f4e4df2010-07-26 01:49:41 +0000770 // Now that V1 is dead, remove it.
771 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000772
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000773 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000774}
775
Evan Cheng32dfbea2007-10-12 08:50:34 +0000776void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000777 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000778 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000779 ranges.clear();
780 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000781 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000782 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
783
Evan Cheng32dfbea2007-10-12 08:50:34 +0000784 weight = RHS.weight;
785 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
786 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000787 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000788 }
789 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
790 const LiveRange &LR = RHS.ranges[i];
791 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
792 }
793}
794
Evan Chenge52eef82007-04-17 20:25:11 +0000795unsigned LiveInterval::getSize() const {
796 unsigned Sum = 0;
797 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000798 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000799 return Sum;
800}
801
David Greene29ff37f2009-07-22 20:08:25 +0000802/// ComputeJoinedWeight - Set the weight of a live interval Joined
803/// after Other has been merged into it.
804void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
805 // If either of these intervals was spilled, the weight is the
806 // weight of the non-spilled interval. This can only happen with
807 // iterative coalescers.
808
David Greene92b78bb2009-07-22 22:32:19 +0000809 if (Other.weight != HUGE_VALF) {
810 weight += Other.weight;
811 }
812 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000813 !TargetRegisterInfo::isPhysicalRegister(reg)) {
814 // Remove this assert if you have an iterative coalescer
815 assert(0 && "Joining to spilled interval");
816 weight = Other.weight;
817 }
David Greene29ff37f2009-07-22 20:08:25 +0000818 else {
819 // Otherwise the weight stays the same
820 // Remove this assert if you have an iterative coalescer
821 assert(0 && "Joining from spilled interval");
822 }
823}
824
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000825raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
826 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
827}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000828
Chris Lattnerabf295f2004-07-24 02:52:23 +0000829void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000830 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000831}
832
Chris Lattnerc02497f2009-08-23 03:47:42 +0000833void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000834 if (isStackSlot())
835 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000836 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000837 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000838 else
839 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000840
Chris Lattner38135af2005-05-14 05:34:15 +0000841 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000842
Chris Lattner38135af2005-05-14 05:34:15 +0000843 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000844 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000845 else {
846 OS << " = ";
847 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000848 E = ranges.end(); I != E; ++I) {
849 OS << *I;
850 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
851 }
Chris Lattner38135af2005-05-14 05:34:15 +0000852 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000853
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000854 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000855 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000856 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000857 unsigned vnum = 0;
858 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
859 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000860 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000861 if (vnum) OS << " ";
862 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000863 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000864 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000865 } else {
Lang Hames61945692009-12-09 05:39:12 +0000866 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000867 OS << "?";
868 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000869 OS << vni->def;
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000870 if (vni->hasPHIKill())
871 OS << "-phikill";
872 if (vni->hasRedefByEC())
873 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000874 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000875 }
876 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000877}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000878
879void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000880 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000881}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000882
883
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000884void LiveRange::print(raw_ostream &os) const {
885 os << *this;
886}