blob: 78bde3998c5fa19368a4c57e373157252a1d5fb1 [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"
Evan Cheng90f95f82009-06-14 20:22:55 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng0adb5272009-04-25 09:25:19 +000023#include "llvm/ADT/DenseMap.h"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000024#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000025#include "llvm/ADT/STLExtras.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000026#include "llvm/Support/Streams.h"
Daniel Dunbar1cd1d982009-07-24 10:36:58 +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>
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000030#include <ostream>
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//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000040bool LiveInterval::liveAt(unsigned I) const {
41 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.
53bool LiveInterval::liveBeforeAndAt(unsigned I) const {
54 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
Chris Lattnerbae74d92004-11-18 03:47:34 +000071// overlaps - Return true if the intersection of the two live intervals is
72// not empty.
73//
Chris Lattnerfb449b92004-07-23 17:49:16 +000074// An example for overlaps():
75//
76// 0: A = ...
77// 4: B = ...
78// 8: C = A + B ;; last use of A
79//
80// The live intervals should look like:
81//
82// A = [3, 11)
83// B = [7, x)
84// C = [11, y)
85//
86// A->overlaps(C) should return false since we want to be able to join
87// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000088//
89bool LiveInterval::overlapsFrom(const LiveInterval& other,
90 const_iterator StartPos) const {
91 const_iterator i = begin();
92 const_iterator ie = end();
93 const_iterator j = StartPos;
94 const_iterator je = other.end();
95
96 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000097 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000098
Chris Lattnerfb449b92004-07-23 17:49:16 +000099 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000100 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000101 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000102 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000103 ++StartPos;
104 if (StartPos != other.end() && StartPos->start <= i->start) {
105 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000106 j = std::upper_bound(j, je, i->start);
107 if (j != other.ranges.begin()) --j;
108 }
Chris Lattneraa141472004-07-23 18:40:00 +0000109 } else {
110 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000111 }
112
Chris Lattner9fddc122004-11-18 05:28:21 +0000113 if (j == je) return false;
114
115 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000116 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000117 std::swap(i, j);
118 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000119 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000120
121 if (i->end > j->start)
122 return true;
123 ++i;
124 }
125
126 return false;
127}
128
Evan Chengcccdb2b2009-04-18 08:52:15 +0000129/// overlaps - Return true if the live interval overlaps a range specified
130/// by [Start, End).
131bool LiveInterval::overlaps(unsigned Start, unsigned End) const {
132 assert(Start < End && "Invalid range");
133 const_iterator I = begin();
134 const_iterator E = end();
135 const_iterator si = std::upper_bound(I, E, Start);
136 const_iterator ei = std::upper_bound(I, E, End);
137 if (si != ei)
138 return true;
139 if (si == I)
140 return false;
141 --si;
142 return si->contains(Start);
143}
144
Chris Lattnerb26c2152004-07-23 19:38:44 +0000145/// extendIntervalEndTo - This method is used when we want to extend the range
146/// specified by I to end at the specified endpoint. To do this, we should
147/// merge and eliminate all ranges that this will overlap with. The iterator is
148/// not invalidated.
149void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
150 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000151 VNInfo *ValNo = I->valno;
Evan Cheng430a7b02007-08-14 01:56:58 +0000152 unsigned OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000153
Chris Lattnerb26c2152004-07-23 19:38:44 +0000154 // Search for the first interval that we can't merge with.
155 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000156 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000157 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000158 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000159
160 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
161 I->end = std::max(NewEnd, prior(MergeTo)->end);
162
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000163 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000164 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000165
166 // Update kill info.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000167 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000168
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000169 // If the newly formed range now touches the range after it and if they have
170 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000171 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000172 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000173 I->end = Next->end;
174 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000175 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000176}
177
178
179/// extendIntervalStartTo - This method is used when we want to extend the range
180/// specified by I to start at the specified endpoint. To do this, we should
181/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000182LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000183LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
184 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000185 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000186
187 // Search for the first interval that we can't merge with.
188 Ranges::iterator MergeTo = I;
189 do {
190 if (MergeTo == ranges.begin()) {
191 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000192 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000193 return I;
194 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000195 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000196 --MergeTo;
197 } while (NewStart <= MergeTo->start);
198
199 // If we start in the middle of another interval, just delete a range and
200 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000201 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000202 MergeTo->end = I->end;
203 } else {
204 // Otherwise, extend the interval right after.
205 ++MergeTo;
206 MergeTo->start = NewStart;
207 MergeTo->end = I->end;
208 }
209
210 ranges.erase(next(MergeTo), next(I));
211 return MergeTo;
212}
213
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000214LiveInterval::iterator
215LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000216 unsigned Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000217 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000218
219 // If the inserted interval starts in the middle or right at the end of
220 // another interval, just extend that interval to contain the range of LR.
221 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000222 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000223 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000224 if (B->start <= Start && B->end >= Start) {
225 extendIntervalEndTo(B, End);
226 return B;
227 }
228 } else {
229 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000230 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000231 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000232 "Cannot overlap two LiveRanges with differing ValID's"
233 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000234 }
235 }
236
237 // Otherwise, if this range ends in the middle of, or right next to, another
238 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000239 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000240 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000241 if (it->start <= End) {
242 it = extendIntervalStartTo(it, Start);
243
244 // If LR is a complete superset of an interval, we may need to grow its
245 // endpoint as well.
246 if (End > it->end)
247 extendIntervalEndTo(it, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000248 else if (End < it->end)
Evan Chengc3868e02007-11-29 01:05:47 +0000249 // Overlapping intervals, there might have been a kill here.
250 removeKill(it->valno, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000251 return it;
252 }
253 } else {
254 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000255 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000256 assert(it->start >= End &&
257 "Cannot overlap two LiveRanges with differing ValID's");
258 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000259 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000260
261 // Otherwise, this is just a new range that doesn't interact with anything.
262 // Insert it.
263 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000264}
265
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000266/// isInOneLiveRange - Return true if the range specified is entirely in the
267/// a single LiveRange of the live interval.
268bool LiveInterval::isInOneLiveRange(unsigned Start, unsigned End) {
269 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
270 if (I == ranges.begin())
271 return false;
272 --I;
273 return I->contains(Start) && I->contains(End-1);
274}
275
Chris Lattnerabf295f2004-07-24 02:52:23 +0000276
277/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000278/// the range must be in a single LiveRange in its entirety.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000279void LiveInterval::removeRange(unsigned Start, unsigned End,
280 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000281 // Find the LiveRange containing this span.
282 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
283 assert(I != ranges.begin() && "Range is not in interval!");
284 --I;
285 assert(I->contains(Start) && I->contains(End-1) &&
286 "Range is not entirely in interval!");
287
288 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000289 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000290 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000291 if (I->end == End) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000292 removeKills(I->valno, Start, End);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000293 if (RemoveDeadValNo) {
294 // Check if val# is dead.
295 bool isDead = true;
296 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
297 if (II != I && II->valno == ValNo) {
298 isDead = false;
299 break;
300 }
301 if (isDead) {
302 // Now that ValNo is dead, remove it. If it is the largest value
303 // number, just nuke it (and any other deleted values neighboring it),
304 // otherwise mark it as ~1U so it can be nuked later.
305 if (ValNo->id == getNumValNums()-1) {
306 do {
307 VNInfo *VNI = valnos.back();
308 valnos.pop_back();
309 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000310 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000311 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000312 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000313 }
314 }
315 }
316
Chris Lattnerabf295f2004-07-24 02:52:23 +0000317 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000318 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000319 I->start = End;
320 return;
321 }
322
323 // Otherwise if the span we are removing is at the end of the LiveRange,
324 // adjust the other way.
325 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000326 removeKills(ValNo, Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000327 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000328 return;
329 }
330
331 // Otherwise, we are splitting the LiveRange into two pieces.
332 unsigned OldEnd = I->end;
333 I->end = Start; // Trim the old interval.
334
335 // Insert the new one.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000336 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000337}
338
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000339/// removeValNo - Remove all the ranges defined by the specified value#.
340/// Also remove the value# from value# list.
341void LiveInterval::removeValNo(VNInfo *ValNo) {
342 if (empty()) return;
343 Ranges::iterator I = ranges.end();
344 Ranges::iterator E = ranges.begin();
345 do {
346 --I;
347 if (I->valno == ValNo)
348 ranges.erase(I);
349 } while (I != E);
350 // Now that ValNo is dead, remove it. If it is the largest value
351 // number, just nuke it (and any other deleted values neighboring it),
352 // otherwise mark it as ~1U so it can be nuked later.
353 if (ValNo->id == getNumValNums()-1) {
354 do {
355 VNInfo *VNI = valnos.back();
356 valnos.pop_back();
357 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000358 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000359 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000360 ValNo->setIsUnused(true);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000361 }
362}
363
Lang Hamesf41538d2009-06-02 16:53:25 +0000364/// scaleNumbering - Renumber VNI and ranges to provide gaps for new
365/// instructions.
366void LiveInterval::scaleNumbering(unsigned factor) {
367 // Scale ranges.
368 for (iterator RI = begin(), RE = end(); RI != RE; ++RI) {
369 RI->start = InstrSlots::scale(RI->start, factor);
370 RI->end = InstrSlots::scale(RI->end, factor);
371 }
372
373 // Scale VNI info.
374 for (vni_iterator VNI = vni_begin(), VNIE = vni_end(); VNI != VNIE; ++VNI) {
375 VNInfo *vni = *VNI;
Lang Hames857c4e02009-06-17 21:01:20 +0000376
Lang Hames98d59822009-06-24 02:17:32 +0000377 if (vni->isDefAccurate())
378 vni->def = InstrSlots::scale(vni->def, factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000379
380 for (unsigned i = 0; i < vni->kills.size(); ++i) {
Lang Hamesffd13262009-07-09 03:57:02 +0000381 if (!vni->kills[i].isPHIKill)
382 vni->kills[i].killIdx =
383 InstrSlots::scale(vni->kills[i].killIdx, factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000384 }
385 }
386}
387
Chris Lattnerabf295f2004-07-24 02:52:23 +0000388/// getLiveRangeContaining - Return the live range that contains the
389/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000390LiveInterval::const_iterator
391LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
392 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000393 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000394 --It;
395 if (It->contains(Idx))
396 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000397 }
398
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000399 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000400}
401
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000402LiveInterval::iterator
403LiveInterval::FindLiveRangeContaining(unsigned Idx) {
404 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000405 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000406 --It;
407 if (It->contains(Idx))
408 return It;
409 }
410
411 return end();
412}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000413
Evan Cheng3f32d652008-06-04 09:18:41 +0000414/// findDefinedVNInfo - Find the VNInfo that's defined at the specified index
415/// (register interval) or defined by the specified register (stack inteval).
416VNInfo *LiveInterval::findDefinedVNInfo(unsigned DefIdxOrReg) const {
417 VNInfo *VNI = NULL;
418 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
419 i != e; ++i)
420 if ((*i)->def == DefIdxOrReg) {
421 VNI = *i;
422 break;
423 }
424 return VNI;
425}
426
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000427/// join - Join two live intervals (this, and other) together. This applies
428/// mappings to the value numbers in the LHS/RHS intervals as specified. If
429/// the intervals are not joinable, this aborts.
David Greeneaf992f72007-09-06 19:46:46 +0000430void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
431 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000432 SmallVector<VNInfo*, 16> &NewVNInfo,
433 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000434 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000435 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000436 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000437 unsigned NumVals = getNumValNums();
438 unsigned NumNewVals = NewVNInfo.size();
439 for (unsigned i = 0; i != NumVals; ++i) {
440 unsigned LHSValID = LHSValNoAssignments[i];
441 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000442 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000443 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000444 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000445
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000446 // If we have to apply a mapping to our base interval assignment, rewrite it
447 // now.
448 if (MustMapCurValNos) {
449 // Map the first live range.
450 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000451 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000452 ++OutIt;
453 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000454 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000455
456 // If this live range has the same value # as its immediate predecessor,
457 // and if they are neighbors, remove one LiveRange. This happens when we
458 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000459 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000460 (OutIt-1)->end = OutIt->end;
461 } else {
462 if (I != OutIt) {
463 OutIt->start = I->start;
464 OutIt->end = I->end;
465 }
466
467 // Didn't merge, on to the next one.
468 ++OutIt;
469 }
470 }
471
472 // If we merge some live ranges, chop off the end.
473 ranges.erase(OutIt, end());
474 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000475
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000476 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000477 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000478 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
479 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
480
481 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000482 // LiveInterval now. Also remove dead val#'s.
483 unsigned NumValNos = 0;
484 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000485 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000486 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000487 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000488 valnos.push_back(VNI);
489 else
490 valnos[NumValNos] = VNI;
491 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000492 }
493 }
Evan Cheng34301352007-09-01 02:03:17 +0000494 if (NumNewVals < NumVals)
495 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000496
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000497 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000498 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000499 unsigned RangeNo = 0;
500 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
501 // Map the valno in the other live range to the current live range.
502 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000503 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000504 InsertPos = addRangeFrom(*I, InsertPos);
505 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000506
David Greene29ff37f2009-07-22 20:08:25 +0000507 ComputeJoinedWeight(Other);
Evan Cheng90f95f82009-06-14 20:22:55 +0000508
509 // Update regalloc hint if currently there isn't one.
510 if (TargetRegisterInfo::isVirtualRegister(reg) &&
511 TargetRegisterInfo::isVirtualRegister(Other.reg)) {
Evan Cheng358dec52009-06-15 08:28:29 +0000512 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg);
513 if (Hint.first == 0 && Hint.second == 0) {
514 std::pair<unsigned, unsigned> OtherHint =
Evan Cheng90f95f82009-06-14 20:22:55 +0000515 MRI->getRegAllocationHint(Other.reg);
Evan Cheng358dec52009-06-15 08:28:29 +0000516 if (OtherHint.first || OtherHint.second)
Evan Cheng90f95f82009-06-14 20:22:55 +0000517 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second);
518 }
519 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000520}
521
Chris Lattnerf21f0202006-09-02 05:26:59 +0000522/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
523/// interval as the specified value number. The LiveRanges in RHS are
524/// allowed to overlap with LiveRanges in the current interval, but only if
525/// the overlapping LiveRanges have the specified value number.
526void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000527 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000528 // TODO: Make this more efficient.
529 iterator InsertPos = begin();
530 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000531 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000532 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000533 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000534 InsertPos = addRangeFrom(Tmp, InsertPos);
535 }
536}
537
538
Evan Cheng32dfbea2007-10-12 08:50:34 +0000539/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
540/// in RHS into this live interval as the specified value number.
541/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000542/// current interval, it will replace the value numbers of the overlaped
543/// live ranges with the specified value number.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000544void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng34729252007-10-14 10:08:34 +0000545 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000546 SmallVector<VNInfo*, 4> ReplacedValNos;
547 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000548 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
549 if (I->valno != RHSValNo)
550 continue;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000551 unsigned Start = I->start, End = I->end;
552 IP = std::upper_bound(IP, end(), Start);
553 // If the start of this range overlaps with an existing liverange, trim it.
554 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000555 if (IP[-1].valno != LHSValNo) {
556 ReplacedValNos.push_back(IP[-1].valno);
557 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000558 }
559 Start = IP[-1].end;
560 // Trimmed away the whole range?
561 if (Start >= End) continue;
562 }
563 // If the end of this range overlaps with an existing liverange, trim it.
564 if (IP != end() && End > IP->start) {
565 if (IP->valno != LHSValNo) {
566 ReplacedValNos.push_back(IP->valno);
567 IP->valno = LHSValNo; // Update val#.
568 }
569 End = IP->start;
570 // If this trimmed away the whole range, ignore it.
571 if (Start == End) continue;
572 }
573
Evan Cheng32dfbea2007-10-12 08:50:34 +0000574 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000575 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
576 }
577
578
579 SmallSet<VNInfo*, 4> Seen;
580 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
581 VNInfo *V1 = ReplacedValNos[i];
582 if (Seen.insert(V1)) {
583 bool isDead = true;
584 for (const_iterator I = begin(), E = end(); I != E; ++I)
585 if (I->valno == V1) {
586 isDead = false;
587 break;
588 }
589 if (isDead) {
590 // Now that V1 is dead, remove it. If it is the largest value number,
591 // just nuke it (and any other deleted values neighboring it), otherwise
592 // mark it as ~1U so it can be nuked later.
593 if (V1->id == getNumValNums()-1) {
594 do {
595 VNInfo *VNI = valnos.back();
596 valnos.pop_back();
597 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000598 } while (!valnos.empty() && valnos.back()->isUnused());
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000599 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000600 V1->setIsUnused(true);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000601 }
602 }
603 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000604 }
605}
606
607
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000608/// MergeInClobberRanges - For any live ranges that are not defined in the
609/// current interval, but are defined in the Clobbers interval, mark them
610/// used with an unknown definition value.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000611void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
612 BumpPtrAllocator &VNInfoAllocator) {
Dan Gohmana8c763b2008-08-14 18:13:49 +0000613 if (Clobbers.empty()) return;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000614
Evan Cheng0adb5272009-04-25 09:25:19 +0000615 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000616 VNInfo *UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000617 iterator IP = begin();
618 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
Evan Cheng0adb5272009-04-25 09:25:19 +0000619 // For every val# in the Clobbers interval, create a new "unknown" val#.
620 VNInfo *ClobberValNo = 0;
621 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
622 if (VI != ValNoMaps.end())
623 ClobberValNo = VI->second;
Evan Chenga4b2bab2009-04-25 20:20:15 +0000624 else if (UnusedValNo)
625 ClobberValNo = UnusedValNo;
Evan Cheng0adb5272009-04-25 09:25:19 +0000626 else {
Lang Hames857c4e02009-06-17 21:01:20 +0000627 UnusedValNo = ClobberValNo = getNextValue(0, 0, false, VNInfoAllocator);
Evan Cheng0adb5272009-04-25 09:25:19 +0000628 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
629 }
630
Dan Gohman97121ba2009-04-08 00:15:30 +0000631 bool Done = false;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000632 unsigned Start = I->start, End = I->end;
Dan Gohman97121ba2009-04-08 00:15:30 +0000633 // If a clobber range starts before an existing range and ends after
634 // it, the clobber range will need to be split into multiple ranges.
635 // Loop until the entire clobber range is handled.
636 while (!Done) {
637 Done = true;
638 IP = std::upper_bound(IP, end(), Start);
639 unsigned SubRangeStart = Start;
640 unsigned SubRangeEnd = End;
641
642 // If the start of this range overlaps with an existing liverange, trim it.
643 if (IP != begin() && IP[-1].end > SubRangeStart) {
644 SubRangeStart = IP[-1].end;
645 // Trimmed away the whole range?
646 if (SubRangeStart >= SubRangeEnd) continue;
647 }
648 // If the end of this range overlaps with an existing liverange, trim it.
649 if (IP != end() && SubRangeEnd > IP->start) {
650 // If the clobber live range extends beyond the existing live range,
651 // it'll need at least another live range, so set the flag to keep
652 // iterating.
653 if (SubRangeEnd > IP->end) {
654 Start = IP->end;
655 Done = false;
656 }
657 SubRangeEnd = IP->start;
658 // If this trimmed away the whole range, ignore it.
659 if (SubRangeStart == SubRangeEnd) continue;
660 }
661
662 // Insert the clobber interval.
663 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
664 IP);
Evan Chenga4b2bab2009-04-25 20:20:15 +0000665 UnusedValNo = 0;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000666 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000667 }
Evan Cheng27e46662009-04-27 17:35:19 +0000668
669 if (UnusedValNo) {
670 // Delete the last unused val#.
671 valnos.pop_back();
672 UnusedValNo->~VNInfo();
673 }
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000674}
675
Evan Chenga2e64352009-03-11 00:03:21 +0000676void LiveInterval::MergeInClobberRange(unsigned Start, unsigned End,
677 BumpPtrAllocator &VNInfoAllocator) {
678 // Find a value # to use for the clobber ranges. If there is already a value#
679 // for unknown values, use it.
Lang Hames857c4e02009-06-17 21:01:20 +0000680 VNInfo *ClobberValNo = getNextValue(0, 0, false, VNInfoAllocator);
Evan Chenga2e64352009-03-11 00:03:21 +0000681
682 iterator IP = begin();
683 IP = std::upper_bound(IP, end(), Start);
684
685 // If the start of this range overlaps with an existing liverange, trim it.
686 if (IP != begin() && IP[-1].end > Start) {
687 Start = IP[-1].end;
688 // Trimmed away the whole range?
689 if (Start >= End) return;
690 }
691 // If the end of this range overlaps with an existing liverange, trim it.
692 if (IP != end() && End > IP->start) {
693 End = IP->start;
694 // If this trimmed away the whole range, ignore it.
695 if (Start == End) return;
696 }
697
698 // Insert the clobber interval.
699 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
700}
701
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000702/// MergeValueNumberInto - This method is called when two value nubmers
703/// are found to be equivalent. This eliminates V1, replacing all
704/// LiveRanges with the V1 value number with the V2 value number. This can
705/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000706VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000707 assert(V1 != V2 && "Identical value#'s are always equivalent!");
708
709 // This code actually merges the (numerically) larger value number into the
710 // smaller value number, which is likely to allow us to compactify the value
711 // space. The only thing we have to be careful of is to preserve the
712 // instruction that defines the result value.
713
714 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000715 if (V1->id < V2->id) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000716 copyValNumInfo(V1, V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000717 std::swap(V1, V2);
718 }
719
720 // Merge V1 live ranges into V2.
721 for (iterator I = begin(); I != end(); ) {
722 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000723 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000724
725 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
726 // range, extend it.
727 if (LR != begin()) {
728 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000729 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000730 Prev->end = LR->end;
731
732 // Erase this live-range.
733 ranges.erase(LR);
734 I = Prev+1;
735 LR = Prev;
736 }
737 }
738
739 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
740 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000741 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000742
743 // If we can merge it into later V2 live ranges, do so now. We ignore any
744 // following V1 live ranges, as they will be merged in subsequent iterations
745 // of the loop.
746 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000747 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000748 LR->end = I->end;
749 ranges.erase(I);
750 I = LR+1;
751 }
752 }
753 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000754
755 // Now that V1 is dead, remove it. If it is the largest value number, just
756 // nuke it (and any other deleted values neighboring it), otherwise mark it as
757 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000758 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000759 do {
Evan Chengdd199d22007-09-06 01:07:24 +0000760 VNInfo *VNI = valnos.back();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000761 valnos.pop_back();
Evan Chengdd199d22007-09-06 01:07:24 +0000762 VNI->~VNInfo();
Lang Hames857c4e02009-06-17 21:01:20 +0000763 } while (valnos.back()->isUnused());
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000764 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000765 V1->setIsUnused(true);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000766 }
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000767
768 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000769}
770
Evan Cheng32dfbea2007-10-12 08:50:34 +0000771void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000772 MachineRegisterInfo *MRI,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000773 BumpPtrAllocator &VNInfoAllocator) {
774 ranges.clear();
775 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000776 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000777 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
778
Evan Cheng32dfbea2007-10-12 08:50:34 +0000779 weight = RHS.weight;
780 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
781 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000782 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000783 }
784 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
785 const LiveRange &LR = RHS.ranges[i];
786 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
787 }
788}
789
Evan Chenge52eef82007-04-17 20:25:11 +0000790unsigned LiveInterval::getSize() const {
791 unsigned Sum = 0;
792 for (const_iterator I = begin(), E = end(); I != E; ++I)
793 Sum += I->end - I->start;
794 return Sum;
795}
796
David Greene29ff37f2009-07-22 20:08:25 +0000797/// ComputeJoinedWeight - Set the weight of a live interval Joined
798/// after Other has been merged into it.
799void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
800 // If either of these intervals was spilled, the weight is the
801 // weight of the non-spilled interval. This can only happen with
802 // iterative coalescers.
803
David Greene92b78bb2009-07-22 22:32:19 +0000804 if (Other.weight != HUGE_VALF) {
805 weight += Other.weight;
806 }
807 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000808 !TargetRegisterInfo::isPhysicalRegister(reg)) {
809 // Remove this assert if you have an iterative coalescer
810 assert(0 && "Joining to spilled interval");
811 weight = Other.weight;
812 }
David Greene29ff37f2009-07-22 20:08:25 +0000813 else {
814 // Otherwise the weight stays the same
815 // Remove this assert if you have an iterative coalescer
816 assert(0 && "Joining from spilled interval");
817 }
818}
819
Chris Lattnerfb449b92004-07-23 17:49:16 +0000820std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000821 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000822}
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000823raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
824 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
825}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000826
Chris Lattnerabf295f2004-07-24 02:52:23 +0000827void LiveRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000828 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000829}
830
Dan Gohman6f0d0242008-02-10 18:45:23 +0000831void LiveInterval::print(std::ostream &OS,
832 const TargetRegisterInfo *TRI) const {
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000833 raw_os_ostream RawOS(OS);
834 print(RawOS, TRI);
835}
836
837void LiveInterval::print(raw_ostream &OS,
838 const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000839 if (isStackSlot())
840 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000841 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000842 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000843 else
844 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000845
Chris Lattner38135af2005-05-14 05:34:15 +0000846 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000847
Chris Lattner38135af2005-05-14 05:34:15 +0000848 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000849 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000850 else {
851 OS << " = ";
852 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
853 E = ranges.end(); I != E; ++I)
854 OS << *I;
855 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000856
857 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000858 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000859 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000860 unsigned vnum = 0;
861 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
862 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000863 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000864 if (vnum) OS << " ";
865 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000866 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000867 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000868 } else {
Lang Hames857c4e02009-06-17 21:01:20 +0000869 if (!vni->isDefAccurate())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000870 OS << "?";
871 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000872 OS << vni->def;
873 unsigned ee = vni->kills.size();
Lang Hames857c4e02009-06-17 21:01:20 +0000874 if (ee || vni->hasPHIKill()) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000875 OS << "-(";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000876 for (unsigned j = 0; j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +0000877 OS << vni->kills[j].killIdx;
878 if (vni->kills[j].isPHIKill)
879 OS << "*";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000880 if (j != ee-1)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000881 OS << " ";
882 }
Lang Hames857c4e02009-06-17 21:01:20 +0000883 if (vni->hasPHIKill()) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000884 if (ee)
885 OS << " ";
886 OS << "phi";
887 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000888 OS << ")";
889 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000890 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000891 }
892 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000893}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000894
895void LiveInterval::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000896 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000897}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000898
899
Jeff Cohen4b607742006-12-16 02:15:42 +0000900void LiveRange::print(std::ostream &os) const {
901 os << *this;
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000902}
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000903void LiveRange::print(raw_ostream &os) const {
904 os << *this;
905}