blob: 44101ffee7a1aa0986f538034b1966173eb79dc2 [file] [log] [blame]
Chris Lattnerfb449b92004-07-23 17:49:16 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfb449b92004-07-23 17:49:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveRange and LiveInterval classes. Given some
11// numbering of each the machine instructions an interval [i, j) is said to be a
12// live interval for register v if there is no instruction with number j' > j
Bob Wilson86af6552010-01-12 22:18:56 +000013// such that v is live at j' and there is no instruction with number i' < i such
Chris Lattnerfb449b92004-07-23 17:49:16 +000014// that v is live at i'. In this implementation intervals can have holes,
15// i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
16// individual range is represented as an instance of LiveRange, and the whole
17// interval is represented as an instance of LiveInterval.
18//
19//===----------------------------------------------------------------------===//
20
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000021#include "llvm/CodeGen/LiveInterval.h"
Lang Hames233a60e2009-11-03 23:52:08 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Cheng90f95f82009-06-14 20:22:55 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng0adb5272009-04-25 09:25:19 +000024#include "llvm/ADT/DenseMap.h"
Evan Cheng3c1f4a42007-10-17 02:13:29 +000025#include "llvm/ADT/SmallSet.h"
Bill Wendling38b0e7b2006-11-28 03:31:29 +000026#include "llvm/ADT/STLExtras.h"
David Greene52421542010-01-04 22:41:43 +000027#include "llvm/Support/Debug.h"
Daniel Dunbara717b7b2009-07-24 10:47:20 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000029#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000030#include <algorithm>
Chris Lattnerfb449b92004-07-23 17:49:16 +000031using namespace llvm;
32
Jakob Stoklund Olesen7c727072010-09-21 20:16:12 +000033// CompEnd - Compare LiveRange ends.
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000034namespace {
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000035struct CompEnd {
Jakob Stoklund Olesen7c727072010-09-21 20:16:12 +000036 bool operator()(const LiveRange &A, const LiveRange &B) const {
37 return A.end < B.end;
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000038 }
39};
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000040}
Chris Lattnerfb449b92004-07-23 17:49:16 +000041
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000042LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen7c727072010-09-21 20:16:12 +000043 return std::upper_bound(begin(), end(), LiveRange(SlotIndex(), Pos, 0),
44 CompEnd());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000045}
46
47/// killedInRange - Return true if the interval has kills in [Start,End).
48bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
49 Ranges::const_iterator r =
50 std::lower_bound(ranges.begin(), ranges.end(), End);
51
52 // Now r points to the first interval with start >= End, or ranges.end().
53 if (r == ranges.begin())
54 return false;
55
56 --r;
57 // Now r points to the last interval with end <= End.
58 // r->end is the kill point.
59 return r->end >= Start && r->end < End;
60}
61
Chris Lattnerbae74d92004-11-18 03:47:34 +000062// overlaps - Return true if the intersection of the two live intervals is
63// not empty.
64//
Chris Lattnerfb449b92004-07-23 17:49:16 +000065// An example for overlaps():
66//
67// 0: A = ...
68// 4: B = ...
69// 8: C = A + B ;; last use of A
70//
71// The live intervals should look like:
72//
73// A = [3, 11)
74// B = [7, x)
75// C = [11, y)
76//
77// A->overlaps(C) should return false since we want to be able to join
78// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000079//
80bool LiveInterval::overlapsFrom(const LiveInterval& other,
81 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000082 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000083 const_iterator i = begin();
84 const_iterator ie = end();
85 const_iterator j = StartPos;
86 const_iterator je = other.end();
87
88 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000089 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000090
Chris Lattnerfb449b92004-07-23 17:49:16 +000091 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000092 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000093 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000094 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000095 ++StartPos;
96 if (StartPos != other.end() && StartPos->start <= i->start) {
97 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000098 j = std::upper_bound(j, je, i->start);
99 if (j != other.ranges.begin()) --j;
100 }
Chris Lattneraa141472004-07-23 18:40:00 +0000101 } else {
102 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000103 }
104
Chris Lattner9fddc122004-11-18 05:28:21 +0000105 if (j == je) return false;
106
107 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000108 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000109 std::swap(i, j);
110 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000111 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000112
113 if (i->end > j->start)
114 return true;
115 ++i;
116 }
117
118 return false;
119}
120
Evan Chengcccdb2b2009-04-18 08:52:15 +0000121/// overlaps - Return true if the live interval overlaps a range specified
122/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000123bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000124 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000125 const_iterator I = std::lower_bound(begin(), end(), End);
126 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000127}
128
Lang Hames6f4e4df2010-07-26 01:49:41 +0000129
130/// ValNo is dead, remove it. If it is the largest value number, just nuke it
131/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
132/// it can be nuked later.
133void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
134 if (ValNo->id == getNumValNums()-1) {
135 do {
136 valnos.pop_back();
137 } while (!valnos.empty() && valnos.back()->isUnused());
138 } else {
139 ValNo->setIsUnused(true);
140 }
141}
142
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000143/// RenumberValues - Renumber all values in order of appearance and delete the
144/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000145void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000146 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000147 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000148 valnos.clear();
149 for (const_iterator I = begin(), E = end(); I != E; ++I) {
150 VNInfo *VNI = I->valno;
151 if (!Seen.insert(VNI))
152 continue;
153 assert(!VNI->isUnused() && "Unused valno used by live range");
154 VNI->id = (unsigned)valnos.size();
155 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000156 VNI->setHasPHIKill(false);
157 if (VNI->isPHIDef())
158 seenPHIDef = true;
159 }
160
161 // Recompute phi kill flags.
162 if (!seenPHIDef)
163 return;
164 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
165 VNInfo *VNI = *I;
166 if (!VNI->isPHIDef())
167 continue;
168 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
169 assert(PHIBB && "No basic block for phi-def");
170 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
171 PE = PHIBB->pred_end(); PI != PE; ++PI) {
172 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
173 if (KVNI)
174 KVNI->setHasPHIKill(true);
175 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000176 }
177}
178
Chris Lattnerb26c2152004-07-23 19:38:44 +0000179/// extendIntervalEndTo - This method is used when we want to extend the range
180/// specified by I to end at the specified endpoint. To do this, we should
181/// merge and eliminate all ranges that this will overlap with. The iterator is
182/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000183void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000184 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000185 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000186
Chris Lattnerb26c2152004-07-23 19:38:44 +0000187 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000188 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000189 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000190 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000191 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000192
193 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
194 I->end = std::max(NewEnd, prior(MergeTo)->end);
195
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000196 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000197 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000198
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000199 // If the newly formed range now touches the range after it and if they have
200 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000201 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000202 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000203 I->end = Next->end;
204 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000205 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000206}
207
208
209/// extendIntervalStartTo - This method is used when we want to extend the range
210/// specified by I to start at the specified endpoint. To do this, we should
211/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000212LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000213LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000214 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000215 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000216
217 // Search for the first interval that we can't merge with.
218 Ranges::iterator MergeTo = I;
219 do {
220 if (MergeTo == ranges.begin()) {
221 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000222 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000223 return I;
224 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000225 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000226 --MergeTo;
227 } while (NewStart <= MergeTo->start);
228
229 // If we start in the middle of another interval, just delete a range and
230 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000231 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000232 MergeTo->end = I->end;
233 } else {
234 // Otherwise, extend the interval right after.
235 ++MergeTo;
236 MergeTo->start = NewStart;
237 MergeTo->end = I->end;
238 }
239
Oscar Fuentesee56c422010-08-02 06:00:15 +0000240 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000241 return MergeTo;
242}
243
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000244LiveInterval::iterator
245LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000246 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000247 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000248
249 // If the inserted interval starts in the middle or right at the end of
250 // another interval, just extend that interval to contain the range of LR.
251 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000252 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000253 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000254 if (B->start <= Start && B->end >= Start) {
255 extendIntervalEndTo(B, End);
256 return B;
257 }
258 } else {
259 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000260 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000261 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000262 "Cannot overlap two LiveRanges with differing ValID's"
263 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000264 }
265 }
266
267 // Otherwise, if this range ends in the middle of, or right next to, another
268 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000269 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000270 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000271 if (it->start <= End) {
272 it = extendIntervalStartTo(it, Start);
273
274 // If LR is a complete superset of an interval, we may need to grow its
275 // endpoint as well.
276 if (End > it->end)
277 extendIntervalEndTo(it, End);
278 return it;
279 }
280 } else {
281 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000282 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000283 assert(it->start >= End &&
284 "Cannot overlap two LiveRanges with differing ValID's");
285 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000286 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000287
288 // Otherwise, this is just a new range that doesn't interact with anything.
289 // Insert it.
290 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000291}
292
Chris Lattnerabf295f2004-07-24 02:52:23 +0000293
294/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000295/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000296void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000297 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000298 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000299 Ranges::iterator I = find(Start);
300 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000301 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000302
303 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000304 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000305 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000306 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000307 if (RemoveDeadValNo) {
308 // Check if val# is dead.
309 bool isDead = true;
310 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
311 if (II != I && II->valno == ValNo) {
312 isDead = false;
313 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000314 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000315 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000316 // Now that ValNo is dead, remove it.
317 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000318 }
319 }
320
Chris Lattnerabf295f2004-07-24 02:52:23 +0000321 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000322 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000323 I->start = End;
324 return;
325 }
326
327 // Otherwise if the span we are removing is at the end of the LiveRange,
328 // adjust the other way.
329 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000330 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000331 return;
332 }
333
334 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000335 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000336 I->end = Start; // Trim the old interval.
337
338 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000339 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000340}
341
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000342/// removeValNo - Remove all the ranges defined by the specified value#.
343/// Also remove the value# from value# list.
344void LiveInterval::removeValNo(VNInfo *ValNo) {
345 if (empty()) return;
346 Ranges::iterator I = ranges.end();
347 Ranges::iterator E = ranges.begin();
348 do {
349 --I;
350 if (I->valno == ValNo)
351 ranges.erase(I);
352 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000353 // Now that ValNo is dead, remove it.
354 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000355}
Lang Hames86511252009-09-04 20:41:11 +0000356
Lang Hames86511252009-09-04 20:41:11 +0000357/// findDefinedVNInfo - Find the VNInfo defined by the specified
358/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000359VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000360 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000361 i != e; ++i) {
362 if ((*i)->def == Idx)
363 return *i;
364 }
365
366 return 0;
367}
368
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000369/// join - Join two live intervals (this, and other) together. This applies
370/// mappings to the value numbers in the LHS/RHS intervals as specified. If
371/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000372void LiveInterval::join(LiveInterval &Other,
373 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000374 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000375 SmallVector<VNInfo*, 16> &NewVNInfo,
376 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000377 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000378 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000379 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000380 unsigned NumVals = getNumValNums();
381 unsigned NumNewVals = NewVNInfo.size();
382 for (unsigned i = 0; i != NumVals; ++i) {
383 unsigned LHSValID = LHSValNoAssignments[i];
384 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000385 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000386 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000387 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000388
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000389 // If we have to apply a mapping to our base interval assignment, rewrite it
390 // now.
391 if (MustMapCurValNos) {
392 // Map the first live range.
393 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000394 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000395 ++OutIt;
396 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000397 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000398
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000399 // If this live range has the same value # as its immediate predecessor,
400 // and if they are neighbors, remove one LiveRange. This happens when we
401 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000402 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000403 (OutIt-1)->end = OutIt->end;
404 } else {
405 if (I != OutIt) {
406 OutIt->start = I->start;
407 OutIt->end = I->end;
408 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000409
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000410 // Didn't merge, on to the next one.
411 ++OutIt;
412 }
413 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000414
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000415 // If we merge some live ranges, chop off the end.
416 ranges.erase(OutIt, end());
417 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000418
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000419 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000420 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000421 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
422 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
423
424 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000425 // LiveInterval now. Also remove dead val#'s.
426 unsigned NumValNos = 0;
427 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000428 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000429 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000430 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000431 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000432 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000433 valnos[NumValNos] = VNI;
434 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000435 }
436 }
Evan Cheng34301352007-09-01 02:03:17 +0000437 if (NumNewVals < NumVals)
438 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000439
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000440 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000441 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000442 unsigned RangeNo = 0;
443 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
444 // Map the valno in the other live range to the current live range.
445 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000446 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000447 InsertPos = addRangeFrom(*I, InsertPos);
448 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000449
David Greene29ff37f2009-07-22 20:08:25 +0000450 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000451}
452
Chris Lattnerf21f0202006-09-02 05:26:59 +0000453/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
454/// interval as the specified value number. The LiveRanges in RHS are
455/// allowed to overlap with LiveRanges in the current interval, but only if
456/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000457void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000458 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000459 // TODO: Make this more efficient.
460 iterator InsertPos = begin();
461 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000462 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000463 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000464 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000465 InsertPos = addRangeFrom(Tmp, InsertPos);
466 }
467}
468
469
Evan Cheng32dfbea2007-10-12 08:50:34 +0000470/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
471/// in RHS into this live interval as the specified value number.
472/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000473/// current interval, it will replace the value numbers of the overlaped
474/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000475void LiveInterval::MergeValueInAsValue(
476 const LiveInterval &RHS,
477 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000478 SmallVector<VNInfo*, 4> ReplacedValNos;
479 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000480 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000481 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000482 if (I->valno != RHSValNo)
483 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000484 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000485 IP = std::upper_bound(IP, end(), Start);
486 // If the start of this range overlaps with an existing liverange, trim it.
487 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000488 if (IP[-1].valno != LHSValNo) {
489 ReplacedValNos.push_back(IP[-1].valno);
490 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000491 }
492 Start = IP[-1].end;
493 // Trimmed away the whole range?
494 if (Start >= End) continue;
495 }
496 // If the end of this range overlaps with an existing liverange, trim it.
497 if (IP != end() && End > IP->start) {
498 if (IP->valno != LHSValNo) {
499 ReplacedValNos.push_back(IP->valno);
500 IP->valno = LHSValNo; // Update val#.
501 }
502 End = IP->start;
503 // If this trimmed away the whole range, ignore it.
504 if (Start == End) continue;
505 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000506
Evan Cheng32dfbea2007-10-12 08:50:34 +0000507 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000508 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
509 }
510
511
512 SmallSet<VNInfo*, 4> Seen;
513 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
514 VNInfo *V1 = ReplacedValNos[i];
515 if (Seen.insert(V1)) {
516 bool isDead = true;
517 for (const_iterator I = begin(), E = end(); I != E; ++I)
518 if (I->valno == V1) {
519 isDead = false;
520 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000521 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000522 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000523 // Now that V1 is dead, remove it.
524 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000525 }
526 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000527 }
528}
529
530
Evan Chenga2e64352009-03-11 00:03:21 +0000531
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000532/// MergeValueNumberInto - This method is called when two value nubmers
533/// are found to be equivalent. This eliminates V1, replacing all
534/// LiveRanges with the V1 value number with the V2 value number. This can
535/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000536VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000537 assert(V1 != V2 && "Identical value#'s are always equivalent!");
538
539 // This code actually merges the (numerically) larger value number into the
540 // smaller value number, which is likely to allow us to compactify the value
541 // space. The only thing we have to be careful of is to preserve the
542 // instruction that defines the result value.
543
544 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000545 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000546 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000547 std::swap(V1, V2);
548 }
549
550 // Merge V1 live ranges into V2.
551 for (iterator I = begin(); I != end(); ) {
552 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000553 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000554
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000555 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
556 // range, extend it.
557 if (LR != begin()) {
558 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000559 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000560 Prev->end = LR->end;
561
562 // Erase this live-range.
563 ranges.erase(LR);
564 I = Prev+1;
565 LR = Prev;
566 }
567 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000568
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000569 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
570 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000571 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000572
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000573 // If we can merge it into later V2 live ranges, do so now. We ignore any
574 // following V1 live ranges, as they will be merged in subsequent iterations
575 // of the loop.
576 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000577 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000578 LR->end = I->end;
579 ranges.erase(I);
580 I = LR+1;
581 }
582 }
583 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000584
Lang Hames6f4e4df2010-07-26 01:49:41 +0000585 // Now that V1 is dead, remove it.
586 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000587
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000588 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000589}
590
Evan Cheng32dfbea2007-10-12 08:50:34 +0000591void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000592 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000593 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000594 ranges.clear();
595 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000596 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000597 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
598
Evan Cheng32dfbea2007-10-12 08:50:34 +0000599 weight = RHS.weight;
600 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
601 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000602 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000603 }
604 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
605 const LiveRange &LR = RHS.ranges[i];
606 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
607 }
608}
609
Evan Chenge52eef82007-04-17 20:25:11 +0000610unsigned LiveInterval::getSize() const {
611 unsigned Sum = 0;
612 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000613 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000614 return Sum;
615}
616
David Greene29ff37f2009-07-22 20:08:25 +0000617/// ComputeJoinedWeight - Set the weight of a live interval Joined
618/// after Other has been merged into it.
619void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
620 // If either of these intervals was spilled, the weight is the
621 // weight of the non-spilled interval. This can only happen with
622 // iterative coalescers.
623
David Greene92b78bb2009-07-22 22:32:19 +0000624 if (Other.weight != HUGE_VALF) {
625 weight += Other.weight;
626 }
627 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000628 !TargetRegisterInfo::isPhysicalRegister(reg)) {
629 // Remove this assert if you have an iterative coalescer
630 assert(0 && "Joining to spilled interval");
631 weight = Other.weight;
632 }
David Greene29ff37f2009-07-22 20:08:25 +0000633 else {
634 // Otherwise the weight stays the same
635 // Remove this assert if you have an iterative coalescer
636 assert(0 && "Joining from spilled interval");
637 }
638}
639
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000640raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
641 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
642}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000643
Chris Lattnerabf295f2004-07-24 02:52:23 +0000644void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000645 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000646}
647
Chris Lattnerc02497f2009-08-23 03:47:42 +0000648void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000649 if (isStackSlot())
650 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000651 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000652 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000653 else
654 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000655
Chris Lattner38135af2005-05-14 05:34:15 +0000656 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000657
Chris Lattner38135af2005-05-14 05:34:15 +0000658 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000659 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000660 else {
661 OS << " = ";
662 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000663 E = ranges.end(); I != E; ++I) {
664 OS << *I;
665 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
666 }
Chris Lattner38135af2005-05-14 05:34:15 +0000667 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000668
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000669 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000670 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000671 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000672 unsigned vnum = 0;
673 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
674 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000675 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000676 if (vnum) OS << " ";
677 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000678 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000679 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000680 } else {
Lang Hames61945692009-12-09 05:39:12 +0000681 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000682 OS << "?";
683 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000684 OS << vni->def;
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000685 if (vni->hasPHIKill())
686 OS << "-phikill";
687 if (vni->hasRedefByEC())
688 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000689 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000690 }
691 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000692}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000693
694void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000695 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000696}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000697
698
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000699void LiveRange::print(raw_ostream &os) const {
700 os << *this;
701}