blob: 22ebf3b41d4142e73239b47854e11efadcd35cbe [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 Cheng3c1f4a42007-10-17 02:13:29 +000022#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000023#include "llvm/ADT/STLExtras.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000024#include "llvm/Support/Streams.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000026#include <algorithm>
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000027#include <ostream>
Chris Lattnerfb449b92004-07-23 17:49:16 +000028using namespace llvm;
29
30// An example for liveAt():
31//
Chris Lattneraa141472004-07-23 18:40:00 +000032// this = [1,4), liveAt(0) will return false. The instruction defining this
33// spans slots [0,3]. The interval belongs to an spilled definition of the
34// variable it represents. This is because slot 1 is used (def slot) and spans
35// up to slot 3 (store slot).
Chris Lattnerfb449b92004-07-23 17:49:16 +000036//
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000037bool LiveInterval::liveAt(unsigned I) const {
38 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
Misha Brukmanedf128a2005-04-21 22:36:52 +000039
Chris Lattnerfb449b92004-07-23 17:49:16 +000040 if (r == ranges.begin())
41 return false;
42
43 --r;
Chris Lattneraa141472004-07-23 18:40:00 +000044 return r->contains(I);
Chris Lattnerfb449b92004-07-23 17:49:16 +000045}
46
Chris Lattnerbae74d92004-11-18 03:47:34 +000047// overlaps - Return true if the intersection of the two live intervals is
48// not empty.
49//
Chris Lattnerfb449b92004-07-23 17:49:16 +000050// An example for overlaps():
51//
52// 0: A = ...
53// 4: B = ...
54// 8: C = A + B ;; last use of A
55//
56// The live intervals should look like:
57//
58// A = [3, 11)
59// B = [7, x)
60// C = [11, y)
61//
62// A->overlaps(C) should return false since we want to be able to join
63// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000064//
65bool LiveInterval::overlapsFrom(const LiveInterval& other,
66 const_iterator StartPos) const {
67 const_iterator i = begin();
68 const_iterator ie = end();
69 const_iterator j = StartPos;
70 const_iterator je = other.end();
71
72 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000073 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000074
Chris Lattnerfb449b92004-07-23 17:49:16 +000075 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000076 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000077 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000078 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000079 ++StartPos;
80 if (StartPos != other.end() && StartPos->start <= i->start) {
81 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000082 j = std::upper_bound(j, je, i->start);
83 if (j != other.ranges.begin()) --j;
84 }
Chris Lattneraa141472004-07-23 18:40:00 +000085 } else {
86 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000087 }
88
Chris Lattner9fddc122004-11-18 05:28:21 +000089 if (j == je) return false;
90
91 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000092 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000093 std::swap(i, j);
94 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +000095 }
Chris Lattnerfb449b92004-07-23 17:49:16 +000096
97 if (i->end > j->start)
98 return true;
99 ++i;
100 }
101
102 return false;
103}
104
Chris Lattnerb26c2152004-07-23 19:38:44 +0000105/// extendIntervalEndTo - This method is used when we want to extend the range
106/// specified by I to end at the specified endpoint. To do this, we should
107/// merge and eliminate all ranges that this will overlap with. The iterator is
108/// not invalidated.
109void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
110 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000111 VNInfo *ValNo = I->valno;
Evan Cheng430a7b02007-08-14 01:56:58 +0000112 unsigned OldEnd = I->end;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000113
Chris Lattnerb26c2152004-07-23 19:38:44 +0000114 // Search for the first interval that we can't merge with.
115 Ranges::iterator MergeTo = next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000116 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000117 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000118 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000119
120 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
121 I->end = std::max(NewEnd, prior(MergeTo)->end);
122
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000123 // Erase any dead ranges.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000124 ranges.erase(next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000125
126 // Update kill info.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000127 removeKills(ValNo, OldEnd, I->end-1);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000128
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000129 // If the newly formed range now touches the range after it and if they have
130 // the same value number, merge the two ranges into one range.
Chris Lattnercef60102005-10-20 22:50:10 +0000131 Ranges::iterator Next = next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000132 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000133 I->end = Next->end;
134 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000135 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000136}
137
138
139/// extendIntervalStartTo - This method is used when we want to extend the range
140/// specified by I to start at the specified endpoint. To do this, we should
141/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000142LiveInterval::Ranges::iterator
Chris Lattnerb26c2152004-07-23 19:38:44 +0000143LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
144 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000145 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000146
147 // Search for the first interval that we can't merge with.
148 Ranges::iterator MergeTo = I;
149 do {
150 if (MergeTo == ranges.begin()) {
151 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000152 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000153 return I;
154 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000155 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000156 --MergeTo;
157 } while (NewStart <= MergeTo->start);
158
159 // If we start in the middle of another interval, just delete a range and
160 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000161 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000162 MergeTo->end = I->end;
163 } else {
164 // Otherwise, extend the interval right after.
165 ++MergeTo;
166 MergeTo->start = NewStart;
167 MergeTo->end = I->end;
168 }
169
170 ranges.erase(next(MergeTo), next(I));
171 return MergeTo;
172}
173
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000174LiveInterval::iterator
175LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000176 unsigned Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000177 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000178
179 // If the inserted interval starts in the middle or right at the end of
180 // another interval, just extend that interval to contain the range of LR.
181 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000182 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000183 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000184 if (B->start <= Start && B->end >= Start) {
185 extendIntervalEndTo(B, End);
186 return B;
187 }
188 } else {
189 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000190 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000191 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000192 "Cannot overlap two LiveRanges with differing ValID's"
193 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000194 }
195 }
196
197 // Otherwise, if this range ends in the middle of, or right next to, another
198 // interval, merge it into that interval.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000199 if (it != ranges.end())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000200 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000201 if (it->start <= End) {
202 it = extendIntervalStartTo(it, Start);
203
204 // If LR is a complete superset of an interval, we may need to grow its
205 // endpoint as well.
206 if (End > it->end)
207 extendIntervalEndTo(it, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000208 else if (End < it->end)
Evan Chengc3868e02007-11-29 01:05:47 +0000209 // Overlapping intervals, there might have been a kill here.
210 removeKill(it->valno, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000211 return it;
212 }
213 } else {
214 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000215 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000216 assert(it->start >= End &&
217 "Cannot overlap two LiveRanges with differing ValID's");
218 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000219
220 // Otherwise, this is just a new range that doesn't interact with anything.
221 // Insert it.
222 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000223}
224
Chris Lattnerabf295f2004-07-24 02:52:23 +0000225
226/// removeRange - Remove the specified range from this interval. Note that
227/// the range must already be in this interval in its entirety.
228void LiveInterval::removeRange(unsigned Start, unsigned End) {
229 // Find the LiveRange containing this span.
230 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
231 assert(I != ranges.begin() && "Range is not in interval!");
232 --I;
233 assert(I->contains(Start) && I->contains(End-1) &&
234 "Range is not entirely in interval!");
235
236 // If the span we are removing is at the start of the LiveRange, adjust it.
237 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000238 if (I->end == End) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000239 removeKills(I->valno, Start, End);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000240 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000241 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000242 I->start = End;
243 return;
244 }
245
246 // Otherwise if the span we are removing is at the end of the LiveRange,
247 // adjust the other way.
248 if (I->end == End) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000249 removeKills(I->valno, Start, End);
Chris Lattner6925a9f2004-07-25 05:43:53 +0000250 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000251 return;
252 }
253
254 // Otherwise, we are splitting the LiveRange into two pieces.
255 unsigned OldEnd = I->end;
256 I->end = Start; // Trim the old interval.
257
258 // Insert the new one.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000259 ranges.insert(next(I), LiveRange(End, OldEnd, I->valno));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000260}
261
262/// getLiveRangeContaining - Return the live range that contains the
263/// specified index, or null if there is none.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000264LiveInterval::const_iterator
265LiveInterval::FindLiveRangeContaining(unsigned Idx) const {
266 const_iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000267 if (It != ranges.begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000268 --It;
269 if (It->contains(Idx))
270 return It;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000271 }
272
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000273 return end();
Chris Lattnerabf295f2004-07-24 02:52:23 +0000274}
275
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000276LiveInterval::iterator
277LiveInterval::FindLiveRangeContaining(unsigned Idx) {
278 iterator It = std::upper_bound(begin(), end(), Idx);
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000279 if (It != begin()) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000280 --It;
281 if (It->contains(Idx))
282 return It;
283 }
284
285 return end();
286}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000287
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000288/// join - Join two live intervals (this, and other) together. This applies
289/// mappings to the value numbers in the LHS/RHS intervals as specified. If
290/// the intervals are not joinable, this aborts.
David Greeneaf992f72007-09-06 19:46:46 +0000291void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
292 const int *RHSValNoAssignments,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000293 SmallVector<VNInfo*, 16> &NewVNInfo) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000294 // Determine if any of our live range values are mapped. This is uncommon, so
Evan Cheng34301352007-09-01 02:03:17 +0000295 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000296 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000297 unsigned NumVals = getNumValNums();
298 unsigned NumNewVals = NewVNInfo.size();
299 for (unsigned i = 0; i != NumVals; ++i) {
300 unsigned LHSValID = LHSValNoAssignments[i];
301 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000302 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000303 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000304 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000305
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000306 // If we have to apply a mapping to our base interval assignment, rewrite it
307 // now.
308 if (MustMapCurValNos) {
309 // Map the first live range.
310 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000311 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000312 ++OutIt;
313 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000314 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000315
316 // If this live range has the same value # as its immediate predecessor,
317 // and if they are neighbors, remove one LiveRange. This happens when we
318 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000319 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000320 (OutIt-1)->end = OutIt->end;
321 } else {
322 if (I != OutIt) {
323 OutIt->start = I->start;
324 OutIt->end = I->end;
325 }
326
327 // Didn't merge, on to the next one.
328 ++OutIt;
329 }
330 }
331
332 // If we merge some live ranges, chop off the end.
333 ranges.erase(OutIt, end());
334 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000335
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000336 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000337 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000338 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
339 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
340
341 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000342 // LiveInterval now. Also remove dead val#'s.
343 unsigned NumValNos = 0;
344 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000345 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000346 if (VNI) {
347 if (i >= NumVals)
348 valnos.push_back(VNI);
349 else
350 valnos[NumValNos] = VNI;
351 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000352 }
353 }
Evan Cheng34301352007-09-01 02:03:17 +0000354 if (NumNewVals < NumVals)
355 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000356
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000357 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000358 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000359 unsigned RangeNo = 0;
360 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
361 // Map the valno in the other live range to the current live range.
362 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000363 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000364 InsertPos = addRangeFrom(*I, InsertPos);
365 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000366
Chris Lattnerabf295f2004-07-24 02:52:23 +0000367 weight += Other.weight;
Evan Chenge52eef82007-04-17 20:25:11 +0000368 if (Other.preference && !preference)
369 preference = Other.preference;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000370}
371
Chris Lattnerf21f0202006-09-02 05:26:59 +0000372/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
373/// interval as the specified value number. The LiveRanges in RHS are
374/// allowed to overlap with LiveRanges in the current interval, but only if
375/// the overlapping LiveRanges have the specified value number.
376void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000377 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000378 // TODO: Make this more efficient.
379 iterator InsertPos = begin();
380 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000381 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000382 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000383 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000384 InsertPos = addRangeFrom(Tmp, InsertPos);
385 }
386}
387
388
Evan Cheng32dfbea2007-10-12 08:50:34 +0000389/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
390/// in RHS into this live interval as the specified value number.
391/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000392/// current interval, it will replace the value numbers of the overlaped
393/// live ranges with the specified value number.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000394void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
Evan Cheng34729252007-10-14 10:08:34 +0000395 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000396 SmallVector<VNInfo*, 4> ReplacedValNos;
397 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000398 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
399 if (I->valno != RHSValNo)
400 continue;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000401 unsigned Start = I->start, End = I->end;
402 IP = std::upper_bound(IP, end(), Start);
403 // If the start of this range overlaps with an existing liverange, trim it.
404 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000405 if (IP[-1].valno != LHSValNo) {
406 ReplacedValNos.push_back(IP[-1].valno);
407 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000408 }
409 Start = IP[-1].end;
410 // Trimmed away the whole range?
411 if (Start >= End) continue;
412 }
413 // If the end of this range overlaps with an existing liverange, trim it.
414 if (IP != end() && End > IP->start) {
415 if (IP->valno != LHSValNo) {
416 ReplacedValNos.push_back(IP->valno);
417 IP->valno = LHSValNo; // Update val#.
418 }
419 End = IP->start;
420 // If this trimmed away the whole range, ignore it.
421 if (Start == End) continue;
422 }
423
Evan Cheng32dfbea2007-10-12 08:50:34 +0000424 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000425 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
426 }
427
428
429 SmallSet<VNInfo*, 4> Seen;
430 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
431 VNInfo *V1 = ReplacedValNos[i];
432 if (Seen.insert(V1)) {
433 bool isDead = true;
434 for (const_iterator I = begin(), E = end(); I != E; ++I)
435 if (I->valno == V1) {
436 isDead = false;
437 break;
438 }
439 if (isDead) {
440 // Now that V1 is dead, remove it. If it is the largest value number,
441 // just nuke it (and any other deleted values neighboring it), otherwise
442 // mark it as ~1U so it can be nuked later.
443 if (V1->id == getNumValNums()-1) {
444 do {
445 VNInfo *VNI = valnos.back();
446 valnos.pop_back();
447 VNI->~VNInfo();
448 } while (valnos.back()->def == ~1U);
449 } else {
450 V1->def = ~1U;
451 }
452 }
453 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000454 }
455}
456
457
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000458/// MergeInClobberRanges - For any live ranges that are not defined in the
459/// current interval, but are defined in the Clobbers interval, mark them
460/// used with an unknown definition value.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000461void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
462 BumpPtrAllocator &VNInfoAllocator) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000463 if (Clobbers.begin() == Clobbers.end()) return;
464
465 // Find a value # to use for the clobber ranges. If there is already a value#
466 // for unknown values, use it.
467 // FIXME: Use a single sentinal number for these!
Evan Chengf3bb2e62007-09-05 21:46:51 +0000468 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000469
470 iterator IP = begin();
471 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
472 unsigned Start = I->start, End = I->end;
473 IP = std::upper_bound(IP, end(), Start);
474
475 // If the start of this range overlaps with an existing liverange, trim it.
476 if (IP != begin() && IP[-1].end > Start) {
477 Start = IP[-1].end;
478 // Trimmed away the whole range?
479 if (Start >= End) continue;
480 }
481 // If the end of this range overlaps with an existing liverange, trim it.
482 if (IP != end() && End > IP->start) {
483 End = IP->start;
484 // If this trimmed away the whole range, ignore it.
485 if (Start == End) continue;
486 }
487
488 // Insert the clobber interval.
489 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
490 }
491}
492
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000493/// MergeValueNumberInto - This method is called when two value nubmers
494/// are found to be equivalent. This eliminates V1, replacing all
495/// LiveRanges with the V1 value number with the V2 value number. This can
496/// cause merging of V1/V2 values numbers and compaction of the value space.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000497void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000498 assert(V1 != V2 && "Identical value#'s are always equivalent!");
499
500 // This code actually merges the (numerically) larger value number into the
501 // smaller value number, which is likely to allow us to compactify the value
502 // space. The only thing we have to be careful of is to preserve the
503 // instruction that defines the result value.
504
505 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000506 if (V1->id < V2->id) {
Evan Chengf3bb2e62007-09-05 21:46:51 +0000507 copyValNumInfo(V1, V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000508 std::swap(V1, V2);
509 }
510
511 // Merge V1 live ranges into V2.
512 for (iterator I = begin(); I != end(); ) {
513 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000514 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000515
516 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
517 // range, extend it.
518 if (LR != begin()) {
519 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000520 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000521 Prev->end = LR->end;
522
523 // Erase this live-range.
524 ranges.erase(LR);
525 I = Prev+1;
526 LR = Prev;
527 }
528 }
529
530 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
531 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000532 LR->valno = V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000533
534 // If we can merge it into later V2 live ranges, do so now. We ignore any
535 // following V1 live ranges, as they will be merged in subsequent iterations
536 // of the loop.
537 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000538 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000539 LR->end = I->end;
540 ranges.erase(I);
541 I = LR+1;
542 }
543 }
544 }
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000545
546 // Now that V1 is dead, remove it. If it is the largest value number, just
547 // nuke it (and any other deleted values neighboring it), otherwise mark it as
548 // ~1U so it can be nuked later.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000549 if (V1->id == getNumValNums()-1) {
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000550 do {
Evan Chengdd199d22007-09-06 01:07:24 +0000551 VNInfo *VNI = valnos.back();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000552 valnos.pop_back();
Evan Chengdd199d22007-09-06 01:07:24 +0000553 VNI->~VNInfo();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000554 } while (valnos.back()->def == ~1U);
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000555 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000556 V1->def = ~1U;
Chris Lattnerc82b3aa2006-08-24 23:22:59 +0000557 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000558}
559
Evan Cheng32dfbea2007-10-12 08:50:34 +0000560void LiveInterval::Copy(const LiveInterval &RHS,
561 BumpPtrAllocator &VNInfoAllocator) {
562 ranges.clear();
563 valnos.clear();
564 preference = RHS.preference;
565 weight = RHS.weight;
566 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
567 const VNInfo *VNI = RHS.getValNumInfo(i);
568 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
569 copyValNumInfo(NewVNI, VNI);
570 }
571 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
572 const LiveRange &LR = RHS.ranges[i];
573 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
574 }
575}
576
Evan Chenge52eef82007-04-17 20:25:11 +0000577unsigned LiveInterval::getSize() const {
578 unsigned Sum = 0;
579 for (const_iterator I = begin(), E = end(); I != E; ++I)
580 Sum += I->end - I->start;
581 return Sum;
582}
583
Chris Lattnerfb449b92004-07-23 17:49:16 +0000584std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000585 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
Chris Lattnerfb449b92004-07-23 17:49:16 +0000586}
587
Chris Lattnerabf295f2004-07-24 02:52:23 +0000588void LiveRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000589 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000590}
591
Dan Gohman6f0d0242008-02-10 18:45:23 +0000592void LiveInterval::print(std::ostream &OS,
593 const TargetRegisterInfo *TRI) const {
594 if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
595 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000596 else
597 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000598
Chris Lattner38135af2005-05-14 05:34:15 +0000599 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000600
Chris Lattner38135af2005-05-14 05:34:15 +0000601 if (empty())
602 OS << "EMPTY";
603 else {
604 OS << " = ";
605 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
606 E = ranges.end(); I != E; ++I)
607 OS << *I;
608 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000609
610 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000611 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000612 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000613 unsigned vnum = 0;
614 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
615 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000616 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000617 if (vnum) OS << " ";
618 OS << vnum << "@";
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000619 if (vni->def == ~1U) {
Evan Cheng8df78602007-08-08 03:00:28 +0000620 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000621 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000622 if (vni->def == ~0U)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000623 OS << "?";
624 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000625 OS << vni->def;
626 unsigned ee = vni->kills.size();
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000627 if (ee) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000628 OS << "-(";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000629 for (unsigned j = 0; j != ee; ++j) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000630 OS << vni->kills[j];
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000631 if (j != ee-1)
Evan Cheng4f8ff162007-08-11 00:59:19 +0000632 OS << " ";
633 }
Evan Chengc3fc7d92007-11-29 09:49:23 +0000634 if (vni->hasPHIKill)
635 OS << " phi";
Evan Cheng4f8ff162007-08-11 00:59:19 +0000636 OS << ")";
637 }
Evan Chenga8d94f12007-08-07 23:49:57 +0000638 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000639 }
640 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000641}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000642
643void LiveInterval::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000644 cerr << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000645}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000646
647
Jeff Cohen4b607742006-12-16 02:15:42 +0000648void LiveRange::print(std::ostream &os) const {
649 os << *this;
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000650}