blob: 8d632cb5ca143a1328f5e2447df03654862a0fd3 [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
13// such that v is live at j' abd there is no instruction with number i' < i such
14// 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"
Daniel Dunbara717b7b2009-07-24 10:47:20 +000027#include "llvm/Support/raw_ostream.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000029#include <algorithm>
Chris Lattnerfb449b92004-07-23 17:49:16 +000030using namespace llvm;
31
32// An example for liveAt():
33//
Chris Lattneraa141472004-07-23 18:40:00 +000034// this = [1,4), liveAt(0) will return false. The instruction defining this
35// spans slots [0,3]. The interval belongs to an spilled definition of the
36// variable it represents. This is because slot 1 is used (def slot) and spans
37// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000038//
Lang Hames233a60e2009-11-03 23:52:08 +000039bool LiveInterval::liveAt(SlotIndex I) const {
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000040 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000041
Chris Lattnerfb449b92004-07-23 17:49:16 +000042 if (r == ranges.begin())
43 return false;
44
45 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000046 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000047}
48
Evan Chengc8d044e2008-02-15 18:24:29 +000049// liveBeforeAndAt - Check if the interval is live at the index and the index
50// just before it. If index is liveAt, check if it starts a new live range.
51// If it does, then check if the previous live range ends at index-1.
Lang Hames233a60e2009-11-03 23:52:08 +000052bool LiveInterval::liveBeforeAndAt(SlotIndex I) const {
Evan Chengc8d044e2008-02-15 18:24:29 +000053 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
54
55 if (r == ranges.begin())
56 return false;
57
58 --r;
59 if (!r->contains(I))
60 return false;
61 if (I != r->start)
62 return true;
63 // I is the start of a live range. Check if the previous live range ends
64 // at I-1.
65 if (r == ranges.begin())
66 return false;
67 return r->end == I;
68}
69
Chris Lattnerbae74d92004-11-18 03:47:34 +000070// overlaps - Return true if the intersection of the two live intervals is
71// not empty.
72//
Chris Lattnerfb449b92004-07-23 17:49:16 +000073// An example for overlaps():
74//
75// 0: A = ...
76// 4: B = ...
77// 8: C = A + B ;; last use of A
78//
79// The live intervals should look like:
80//
81// A = [3, 11)
82// B = [7, x)
83// C = [11, y)
84//
85// A->overlaps(C) should return false since we want to be able to join
86// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000087//
88bool LiveInterval::overlapsFrom(const LiveInterval& other,
89 const_iterator StartPos) const {
90 const_iterator i = begin();
91 const_iterator ie = end();
92 const_iterator j = StartPos;
93 const_iterator je = other.end();
94
95 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000096 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000097
Chris Lattnerfb449b92004-07-23 17:49:16 +000098 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000099 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000100 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000101 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000102 ++StartPos;
103 if (StartPos != other.end() && StartPos->start <= i->start) {
104 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000105 j = std::upper_bound(j, je, i->start);
106 if (j != other.ranges.begin()) --j;
107 }
Chris Lattneraa141472004-07-23 18:40:00 +0000108 } else {
109 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000110 }
111
Chris Lattner9fddc122004-11-18 05:28:21 +0000112 if (j == je) return false;
113
114 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000115 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000116 std::swap(i, j);
117 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000118 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000119
120 if (i->end > j->start)
121 return true;
122 ++i;
123 }
124
125 return false;
126}
127
Evan Chengcccdb2b2009-04-18 08:52:15 +0000128/// overlaps - Return true if the live interval overlaps a range specified
129/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000130bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000131 assert(Start < End && "Invalid range");
132 const_iterator I = begin();
133 const_iterator E = end();
134 const_iterator si = std::upper_bound(I, E, Start);
135 const_iterator ei = std::upper_bound(I, E, End);
136 if (si != ei)
137 return true;
138 if (si == I)
139 return false;
140 --si;
141 return si->contains(Start);
142}
143
Chris Lattnerb26c2152004-07-23 19:38:44 +0000144/// extendIntervalEndTo - This method is used when we want to extend the range
145/// specified by I to end at the specified endpoint. To do this, we should
146/// merge and eliminate all ranges that this will overlap with. The iterator is
147/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000148void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000149 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000150 VNInfo *ValNo = I->valno;
Lang Hames233a60e2009-11-03 23:52:08 +0000151 SlotIndex OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000152
Chris Lattnerb26c2152004-07-23 19:38:44 +0000153 // Search for the first interval that we can't merge with.
154 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000155 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000156 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000157 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000158
159 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
160 I->end = std::max(NewEnd, prior(MergeTo)->end);
161
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000162 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000163 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000164
165 // Update kill info.
Lang Hames233a60e2009-11-03 23:52:08 +0000166 ValNo->removeKills(OldEnd, I->end.getPrevSlot());
Evan Cheng4f8ff162007-08-11 00:59:19 +0000167
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000168 // If the newly formed range now touches the range after it and if they have
169 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000170 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000171 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000172 I->end = Next->end;
173 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000174 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000175}
176
177
178/// extendIntervalStartTo - This method is used when we want to extend the range
179/// specified by I to start at the specified endpoint. To do this, we should
180/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000181LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000182LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000183 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000184 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000185
186 // Search for the first interval that we can't merge with.
187 Ranges::iterator MergeTo = I;
188 do {
189 if (MergeTo == ranges.begin()) {
190 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000191 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000192 return I;
193 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000194 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000195 --MergeTo;
196 } while (NewStart <= MergeTo->start);
197
198 // If we start in the middle of another interval, just delete a range and
199 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000200 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000201 MergeTo->end = I->end;
202 } else {
203 // Otherwise, extend the interval right after.
204 ++MergeTo;
205 MergeTo->start = NewStart;
206 MergeTo->end = I->end;
207 }
208
209 ranges.erase(next(MergeTo), next(I));
210 return MergeTo;
211}
212
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000213LiveInterval::iterator
214LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000215 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000216 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000217
218 // If the inserted interval starts in the middle or right at the end of
219 // another interval, just extend that interval to contain the range of LR.
220 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000221 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000222 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000223 if (B->start <= Start && B->end >= Start) {
224 extendIntervalEndTo(B, End);
225 return B;
226 }
227 } else {
228 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000229 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000230 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000231 "Cannot overlap two LiveRanges with differing ValID's"
232 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000233 }
234 }
235
236 // Otherwise, if this range ends in the middle of, or right next to, another
237 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000238 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000239 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000240 if (it->start <= End) {
241 it = extendIntervalStartTo(it, Start);
242
243 // If LR is a complete superset of an interval, we may need to grow its
244 // endpoint as well.
245 if (End > it->end)
246 extendIntervalEndTo(it, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000247 else if (End < it->end)
Evan Chengc3868e02007-11-29 01:05:47 +0000248 // Overlapping intervals, there might have been a kill here.
Lang Hames86511252009-09-04 20:41:11 +0000249 it->valno->removeKill(End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000250 return it;
251 }
252 } else {
253 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000254 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000255 assert(it->start >= End &&
256 "Cannot overlap two LiveRanges with differing ValID's");
257 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000258 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000259
260 // Otherwise, this is just a new range that doesn't interact with anything.
261 // Insert it.
262 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000263}
264
Lang Hames86511252009-09-04 20:41:11 +0000265/// isInOneLiveRange - Return true if the range specified is entirely in
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000266/// a single LiveRange of the live interval.
Lang Hames233a60e2009-11-03 23:52:08 +0000267bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000268 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
269 if (I == ranges.begin())
270 return false;
271 --I;
Lang Hames86511252009-09-04 20:41:11 +0000272 return I->containsRange(Start, End);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000273}
274
Chris Lattnerabf295f2004-07-24 02:52:23 +0000275
276/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000277/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000278void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000279 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000280 // Find the LiveRange containing this span.
281 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
282 assert(I != ranges.begin() && "Range is not in interval!");
283 --I;
Lang Hames86511252009-09-04 20:41:11 +0000284 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000285
286 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000287 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000288 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000289 if (I->end == End) {
Lang Hames86511252009-09-04 20:41:11 +0000290 ValNo->removeKills(Start, End);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000291 if (RemoveDeadValNo) {
292 // Check if val# is dead.
293 bool isDead = true;
294 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
295 if (II != I && II->valno == ValNo) {
296 isDead = false;
297 break;
298 }
299 if (isDead) {
300 // Now that ValNo is dead, remove it. If it is the largest value
301 // number, just nuke it (and any other deleted values neighboring it),
302 // otherwise mark it as ~1U so it can be nuked later.
303 if (ValNo->id == getNumValNums()-1) {
304 do {
305 VNInfo *VNI = valnos.back();
306 valnos.pop_back();
307 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000308 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000309 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000310 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000311 }
312 }
313 }
314
Chris Lattnerabf295f2004-07-24 02:52:23 +0000315 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000316 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000317 I->start = End;
318 return;
319 }
320
321 // Otherwise if the span we are removing is at the end of the LiveRange,
322 // adjust the other way.
323 if (I->end == End) {
Lang Hames86511252009-09-04 20:41:11 +0000324 ValNo->removeKills(Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000325 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000326 return;
327 }
328
329 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000330 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000331 I->end = Start; // Trim the old interval.
332
333 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000334 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000335}
336
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000337/// removeValNo - Remove all the ranges defined by the specified value#.
338/// Also remove the value# from value# list.
339void LiveInterval::removeValNo(VNInfo *ValNo) {
340 if (empty()) return;
341 Ranges::iterator I = ranges.end();
342 Ranges::iterator E = ranges.begin();
343 do {
344 --I;
345 if (I->valno == ValNo)
346 ranges.erase(I);
347 } while (I != E);
348 // Now that ValNo is dead, remove it. If it is the largest value
349 // number, just nuke it (and any other deleted values neighboring it),
350 // otherwise mark it as ~1U so it can be nuked later.
351 if (ValNo->id == getNumValNums()-1) {
352 do {
353 VNInfo *VNI = valnos.back();
354 valnos.pop_back();
355 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000356 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000357 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000358 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000359 }
360}
Lang Hames86511252009-09-04 20:41:11 +0000361
Chris Lattnerabf295f2004-07-24 02:52:23 +0000362/// getLiveRangeContaining - Return the live range that contains the
363/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000364LiveInterval::const_iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000365LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000366 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000367 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000368 --It;
369 if (It->contains(Idx))
370 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000371 }
372
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000373 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000374}
375
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000376LiveInterval::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000377LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000378 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000379 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000380 --It;
381 if (It->contains(Idx))
382 return It;
383 }
384
385 return end();
386}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000387
Lang Hames86511252009-09-04 20:41:11 +0000388/// findDefinedVNInfo - Find the VNInfo defined by the specified
389/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000390VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000391 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000392 i != e; ++i) {
393 if ((*i)->def == Idx)
394 return *i;
395 }
396
397 return 0;
398}
399
400/// findDefinedVNInfo - Find the VNInfo defined by the specified
401/// register (stack inteval).
402VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
403 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
404 i != e; ++i) {
405 if ((*i)->getReg() == reg)
406 return *i;
407 }
408 return 0;
Evan Cheng3f32d652008-06-04 09:18:41 +0000409}
410
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000411/// join - Join two live intervals (this, and other) together. This applies
412/// mappings to the value numbers in the LHS/RHS intervals as specified. If
413/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000414void LiveInterval::join(LiveInterval &Other,
415 const int *LHSValNoAssignments,
David Greeneaf992f72007-09-06 19:46:46 +0000416 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000417 SmallVector<VNInfo*, 16> &NewVNInfo,
418 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000419 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000420 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000421 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000422 unsigned NumVals = getNumValNums();
423 unsigned NumNewVals = NewVNInfo.size();
424 for (unsigned i = 0; i != NumVals; ++i) {
425 unsigned LHSValID = LHSValNoAssignments[i];
426 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000427 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000428 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000429 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000430
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000431 // If we have to apply a mapping to our base interval assignment, rewrite it
432 // now.
433 if (MustMapCurValNos) {
434 // Map the first live range.
435 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000436 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000437 ++OutIt;
438 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000439 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000440
441 // If this live range has the same value # as its immediate predecessor,
442 // and if they are neighbors, remove one LiveRange. This happens when we
443 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000444 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000445 (OutIt-1)->end = OutIt->end;
446 } else {
447 if (I != OutIt) {
448 OutIt->start = I->start;
449 OutIt->end = I->end;
450 }
451
452 // Didn't merge, on to the next one.
453 ++OutIt;
454 }
455 }
456
457 // If we merge some live ranges, chop off the end.
458 ranges.erase(OutIt, end());
459 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000460
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000461 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000462 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000463 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
464 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
465
466 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000467 // LiveInterval now. Also remove dead val#'s.
468 unsigned NumValNos = 0;
469 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000470 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000471 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000472 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000473 valnos.push_back(VNI);
474 else
475 valnos[NumValNos] = VNI;
476 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000477 }
478 }
Evan Cheng34301352007-09-01 02:03:17 +0000479 if (NumNewVals < NumVals)
480 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000481
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000482 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000483 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000484 unsigned RangeNo = 0;
485 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
486 // Map the valno in the other live range to the current live range.
487 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000488 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000489 InsertPos = addRangeFrom(*I, InsertPos);
490 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000491
David Greene29ff37f2009-07-22 20:08:25 +0000492 ComputeJoinedWeight(Other);
Evan Cheng90f95f82009-06-14 20:22:55 +0000493
494 // Update regalloc hint if currently there isn't one.
495 if (TargetRegisterInfo::isVirtualRegister(reg) &&
496 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
Evan Cheng358dec52009-06-15 08:28:29 +0000497 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
498 if (Hint.first == 0 && Hint.second == 0) {
499 std::pair<unsigned, unsigned> OtherHint =
Evan Cheng90f95f82009-06-14 20:22:55 +0000500 MRI->getRegAllocationHint(Other.reg);
Evan Cheng358dec52009-06-15 08:28:29 +0000501 if (OtherHint.first || OtherHint.second)
Evan Cheng90f95f82009-06-14 20:22:55 +0000502 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
503 }
504 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000505}
506
Chris Lattnerf21f0202006-09-02 05:26:59 +0000507/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
508/// interval as the specified value number. The LiveRanges in RHS are
509/// allowed to overlap with LiveRanges in the current interval, but only if
510/// the overlapping LiveRanges have the specified value number.
511void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000512 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000513 // TODO: Make this more efficient.
514 iterator InsertPos = begin();
515 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000516 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000517 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000518 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000519 InsertPos = addRangeFrom(Tmp, InsertPos);
520 }
521}
522
523
Evan Cheng32dfbea2007-10-12 08:50:34 +0000524/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
525/// in RHS into this live interval as the specified value number.
526/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000527/// current interval, it will replace the value numbers of the overlaped
528/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000529void LiveInterval::MergeValueInAsValue(
530 const LiveInterval &RHS,
531 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000532 SmallVector<VNInfo*, 4> ReplacedValNos;
533 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000534 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
535 if (I->valno != RHSValNo)
536 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000537 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000538 IP = std::upper_bound(IP, end(), Start);
539 // If the start of this range overlaps with an existing liverange, trim it.
540 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000541 if (IP[-1].valno != LHSValNo) {
542 ReplacedValNos.push_back(IP[-1].valno);
543 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000544 }
545 Start = IP[-1].end;
546 // Trimmed away the whole range?
547 if (Start >= End) continue;
548 }
549 // If the end of this range overlaps with an existing liverange, trim it.
550 if (IP != end() && End > IP->start) {
551 if (IP->valno != LHSValNo) {
552 ReplacedValNos.push_back(IP->valno);
553 IP->valno = LHSValNo; // Update val#.
554 }
555 End = IP->start;
556 // If this trimmed away the whole range, ignore it.
557 if (Start == End) continue;
558 }
559
Evan Cheng32dfbea2007-10-12 08:50:34 +0000560 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000561 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
562 }
563
564
565 SmallSet<VNInfo*, 4> Seen;
566 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
567 VNInfo *V1 = ReplacedValNos[i];
568 if (Seen.insert(V1)) {
569 bool isDead = true;
570 for (const_iterator I = begin(), E = end(); I != E; ++I)
571 if (I->valno == V1) {
572 isDead = false;
573 break;
574 }
575 if (isDead) {
576 // Now that V1 is dead, remove it. If it is the largest value number,
577 // just nuke it (and any other deleted values neighboring it), otherwise
578 // mark it as ~1U so it can be nuked later.
579 if (V1->id == getNumValNums()-1) {
580 do {
581 VNInfo *VNI = valnos.back();
582 valnos.pop_back();
583 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000584 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000585 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000586 V1->setIsUnused(true);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000587 }
588 }
589 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000590 }
591}
592
593
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000594/// MergeInClobberRanges - For any live ranges that are not defined in the
595/// current interval, but are defined in the Clobbers interval, mark them
596/// used with an unknown definition value.
Lang Hames233a60e2009-11-03 23:52:08 +0000597void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
598 const LiveInterval &Clobbers,
Evan Chengf3bb2e62007-09-05 21:46:51 +0000599 BumpPtrAllocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000600 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000601
Evan Cheng0adb5272009-04-25 09:25:19 +0000602 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000603 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000604 iterator IP = begin();
605 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000606 // For every val# in the Clobbers interval, create a new "unknown" val#.
607 VNInfo *ClobberValNo = 0;
608 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
609 if (VI != ValNoMaps.end())
610 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000611 else if (UnusedValNo)
612 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000613 else {
Lang Hames86511252009-09-04 20:41:11 +0000614 UnusedValNo = ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000615 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000616 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
617 }
618
Dan Gohman97121ba2009-04-08 00:15:30 +0000619 bool Done = false;
Lang Hames233a60e2009-11-03 23:52:08 +0000620 SlotIndex Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000621 // If a clobber range starts before an existing range and ends after
622 // it, the clobber range will need to be split into multiple ranges.
623 // Loop until the entire clobber range is handled.
624 while (!Done) {
625 Done = true;
626 IP = std::upper_bound(IP, end(), Start);
Lang Hames233a60e2009-11-03 23:52:08 +0000627 SlotIndex SubRangeStart = Start;
628 SlotIndex SubRangeEnd = End;
Dan Gohman97121ba2009-04-08 00:15:30 +0000629
630 // If the start of this range overlaps with an existing liverange, trim it.
631 if (IP != begin() && IP[-1].end > SubRangeStart) {
632 SubRangeStart = IP[-1].end;
633 // Trimmed away the whole range?
634 if (SubRangeStart >= SubRangeEnd) continue;
635 }
636 // If the end of this range overlaps with an existing liverange, trim it.
637 if (IP != end() && SubRangeEnd > IP->start) {
638 // If the clobber live range extends beyond the existing live range,
639 // it'll need at least another live range, so set the flag to keep
640 // iterating.
641 if (SubRangeEnd > IP->end) {
642 Start = IP->end;
643 Done = false;
644 }
645 SubRangeEnd = IP->start;
646 // If this trimmed away the whole range, ignore it.
647 if (SubRangeStart == SubRangeEnd) continue;
648 }
649
650 // Insert the clobber interval.
651 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
652 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000653 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000654 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000655 }
Evan Cheng27e46662009-04-27 17:35:19 +0000656
657 if (UnusedValNo) {
658 // Delete the last unused val#.
659 valnos.pop_back();
660 UnusedValNo->~VNInfo();
661 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000662}
663
Lang Hames233a60e2009-11-03 23:52:08 +0000664void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
665 SlotIndex Start,
666 SlotIndex End,
Evan Chenga2e64352009-03-11 00:03:21 +0000667 BumpPtrAllocator &VNInfoAllocator) {
668 // Find a value # to use for the clobber ranges. If there is already a value#
669 // for unknown values, use it.
Lang Hames86511252009-09-04 20:41:11 +0000670 VNInfo *ClobberValNo =
Lang Hames233a60e2009-11-03 23:52:08 +0000671 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000672
673 iterator IP = begin();
674 IP = std::upper_bound(IP, end(), Start);
675
676 // If the start of this range overlaps with an existing liverange, trim it.
677 if (IP != begin() && IP[-1].end > Start) {
678 Start = IP[-1].end;
679 // Trimmed away the whole range?
680 if (Start >= End) return;
681 }
682 // If the end of this range overlaps with an existing liverange, trim it.
683 if (IP != end() && End > IP->start) {
684 End = IP->start;
685 // If this trimmed away the whole range, ignore it.
686 if (Start == End) return;
687 }
688
689 // Insert the clobber interval.
690 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
691}
692
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000693/// MergeValueNumberInto - This method is called when two value nubmers
694/// are found to be equivalent. This eliminates V1, replacing all
695/// LiveRanges with the V1 value number with the V2 value number. This can
696/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000697VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000698 assert(V1 != V2 && "Identical value#'s are always equivalent!");
699
700 // This code actually merges the (numerically) larger value number into the
701 // smaller value number, which is likely to allow us to compactify the value
702 // space. The only thing we have to be careful of is to preserve the
703 // instruction that defines the result value.
704
705 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000706 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000707 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000708 std::swap(V1, V2);
709 }
710
711 // Merge V1 live ranges into V2.
712 for (iterator I = begin(); I != end(); ) {
713 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000714 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000715
716 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
717 // range, extend it.
718 if (LR != begin()) {
719 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000720 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000721 Prev->end = LR->end;
722
723 // Erase this live-range.
724 ranges.erase(LR);
725 I = Prev+1;
726 LR = Prev;
727 }
728 }
729
730 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
731 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000732 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000733
734 // If we can merge it into later V2 live ranges, do so now. We ignore any
735 // following V1 live ranges, as they will be merged in subsequent iterations
736 // of the loop.
737 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000738 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000739 LR->end = I->end;
740 ranges.erase(I);
741 I = LR+1;
742 }
743 }
744 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000745
746 // Now that V1 is dead, remove it. If it is the largest value number, just
747 // nuke it (and any other deleted values neighboring it), otherwise mark it as
748 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000749 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000750 do {
Evan Chengdd199d22007-09-06 01:07:24 +0000751 VNInfo *VNI = valnos.back();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000752 valnos.pop_back();
Evan Chengdd199d22007-09-06 01:07:24 +0000753 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000754 } while (valnos.back()->isUnused());
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000755 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000756 V1->setIsUnused(true);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000757 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000758
759 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000760}
761
Evan Cheng32dfbea2007-10-12 08:50:34 +0000762void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000763 MachineRegisterInfo *MRI,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000764 BumpPtrAllocator &VNInfoAllocator) {
765 ranges.clear();
766 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000767 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000768 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
769
Evan Cheng32dfbea2007-10-12 08:50:34 +0000770 weight = RHS.weight;
771 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
772 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000773 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000774 }
775 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
776 const LiveRange &LR = RHS.ranges[i];
777 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
778 }
779}
780
Evan Chenge52eef82007-04-17 20:25:11 +0000781unsigned LiveInterval::getSize() const {
782 unsigned Sum = 0;
783 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000784 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000785 return Sum;
786}
787
David Greene29ff37f2009-07-22 20:08:25 +0000788/// ComputeJoinedWeight - Set the weight of a live interval Joined
789/// after Other has been merged into it.
790void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
791 // If either of these intervals was spilled, the weight is the
792 // weight of the non-spilled interval. This can only happen with
793 // iterative coalescers.
794
David Greene92b78bb2009-07-22 22:32:19 +0000795 if (Other.weight != HUGE_VALF) {
796 weight += Other.weight;
797 }
798 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000799 !TargetRegisterInfo::isPhysicalRegister(reg)) {
800 // Remove this assert if you have an iterative coalescer
801 assert(0 && "Joining to spilled interval");
802 weight = Other.weight;
803 }
David Greene29ff37f2009-07-22 20:08:25 +0000804 else {
805 // Otherwise the weight stays the same
806 // Remove this assert if you have an iterative coalescer
807 assert(0 && "Joining from spilled interval");
808 }
809}
810
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000811raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
812 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
813}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000814
Chris Lattnerabf295f2004-07-24 02:52:23 +0000815void LiveRange::dump() const {
Daniel Dunbara717b7b2009-07-24 10:47:20 +0000816 errs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000817}
818
Chris Lattnerc02497f2009-08-23 03:47:42 +0000819void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000820 if (isStackSlot())
821 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000822 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000823 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000824 else
825 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000826
Chris Lattner38135af2005-05-14 05:34:15 +0000827 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000828
Chris Lattner38135af2005-05-14 05:34:15 +0000829 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000830 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000831 else {
832 OS << " = ";
833 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
834 E = ranges.end(); I != E; ++I)
835 OS << *I;
836 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000837
838 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000839 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000840 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000841 unsigned vnum = 0;
842 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
843 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000844 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000845 if (vnum) OS << " ";
846 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000847 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000848 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000849 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000850 if (!vni->isDefAccurate())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000851 OS << "?";
852 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000853 OS << vni->def;
854 unsigned ee = vni->kills.size();
Lang Hames857c4e02009-06-17 21:01:20 +0000855 if (ee || vni->hasPHIKill()) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000856 OS << "-(";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000857 for (unsigned j = 0; j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +0000858 OS << vni->kills[j];
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000859 if (j != ee-1)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000860 OS << " ";
861 }
Lang Hames857c4e02009-06-17 21:01:20 +0000862 if (vni->hasPHIKill()) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000863 if (ee)
864 OS << " ";
865 OS << "phi";
866 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000867 OS << ")";
868 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000869 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000870 }
871 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000872}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000873
874void LiveInterval::dump() const {
Daniel Dunbara717b7b2009-07-24 10:47:20 +0000875 errs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000876}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000877
878
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000879void LiveRange::print(raw_ostream &os) const {
880 os << *this;
881}