blob: 6a6ecf78f68e26e0cbe9d6823211b3248da8e6e6 [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 Olesenf568b272010-09-21 17:12:18 +000033// compEnd - Compare LiveRange end to Pos.
34// This argument ordering works for upper_bound.
35static inline bool compEnd(SlotIndex Pos, const LiveRange &LR) {
36 return Pos < LR.end;
Chris Lattnerfb449b92004-07-23 17:49:16 +000037}
38
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000039LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
40 return std::upper_bound(begin(), end(), Pos, compEnd);
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000041}
42
43/// killedInRange - Return true if the interval has kills in [Start,End).
44bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
45 Ranges::const_iterator r =
46 std::lower_bound(ranges.begin(), ranges.end(), End);
47
48 // Now r points to the first interval with start >= End, or ranges.end().
49 if (r == ranges.begin())
50 return false;
51
52 --r;
53 // Now r points to the last interval with end <= End.
54 // r->end is the kill point.
55 return r->end >= Start && r->end < End;
56}
57
Chris Lattnerbae74d92004-11-18 03:47:34 +000058// overlaps - Return true if the intersection of the two live intervals is
59// not empty.
60//
Chris Lattnerfb449b92004-07-23 17:49:16 +000061// An example for overlaps():
62//
63// 0: A = ...
64// 4: B = ...
65// 8: C = A + B ;; last use of A
66//
67// The live intervals should look like:
68//
69// A = [3, 11)
70// B = [7, x)
71// C = [11, y)
72//
73// A->overlaps(C) should return false since we want to be able to join
74// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000075//
76bool LiveInterval::overlapsFrom(const LiveInterval& other,
77 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000078 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000079 const_iterator i = begin();
80 const_iterator ie = end();
81 const_iterator j = StartPos;
82 const_iterator je = other.end();
83
84 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000085 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000086
Chris Lattnerfb449b92004-07-23 17:49:16 +000087 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000088 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000089 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000090 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000091 ++StartPos;
92 if (StartPos != other.end() && StartPos->start <= i->start) {
93 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +000094 j = std::upper_bound(j, je, i->start);
95 if (j != other.ranges.begin()) --j;
96 }
Chris Lattneraa141472004-07-23 18:40:00 +000097 } else {
98 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +000099 }
100
Chris Lattner9fddc122004-11-18 05:28:21 +0000101 if (j == je) return false;
102
103 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000104 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000105 std::swap(i, j);
106 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000107 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000108
109 if (i->end > j->start)
110 return true;
111 ++i;
112 }
113
114 return false;
115}
116
Evan Chengcccdb2b2009-04-18 08:52:15 +0000117/// overlaps - Return true if the live interval overlaps a range specified
118/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000119bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000120 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000121 const_iterator I = std::lower_bound(begin(), end(), End);
122 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000123}
124
Lang Hames6f4e4df2010-07-26 01:49:41 +0000125
126/// ValNo is dead, remove it. If it is the largest value number, just nuke it
127/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
128/// it can be nuked later.
129void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
130 if (ValNo->id == getNumValNums()-1) {
131 do {
132 valnos.pop_back();
133 } while (!valnos.empty() && valnos.back()->isUnused());
134 } else {
135 ValNo->setIsUnused(true);
136 }
137}
138
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000139/// RenumberValues - Renumber all values in order of appearance and delete the
140/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000141void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000142 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000143 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000144 valnos.clear();
145 for (const_iterator I = begin(), E = end(); I != E; ++I) {
146 VNInfo *VNI = I->valno;
147 if (!Seen.insert(VNI))
148 continue;
149 assert(!VNI->isUnused() && "Unused valno used by live range");
150 VNI->id = (unsigned)valnos.size();
151 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000152 VNI->setHasPHIKill(false);
153 if (VNI->isPHIDef())
154 seenPHIDef = true;
155 }
156
157 // Recompute phi kill flags.
158 if (!seenPHIDef)
159 return;
160 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
161 VNInfo *VNI = *I;
162 if (!VNI->isPHIDef())
163 continue;
164 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
165 assert(PHIBB && "No basic block for phi-def");
166 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
167 PE = PHIBB->pred_end(); PI != PE; ++PI) {
168 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
169 if (KVNI)
170 KVNI->setHasPHIKill(true);
171 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000172 }
173}
174
Chris Lattnerb26c2152004-07-23 19:38:44 +0000175/// extendIntervalEndTo - This method is used when we want to extend the range
176/// specified by I to end at the specified endpoint. To do this, we should
177/// merge and eliminate all ranges that this will overlap with. The iterator is
178/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000179void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000180 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000181 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000182
Chris Lattnerb26c2152004-07-23 19:38:44 +0000183 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000184 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000185 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000186 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000187 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000188
189 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
190 I->end = std::max(NewEnd, prior(MergeTo)->end);
191
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000192 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000193 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000194
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000195 // If the newly formed range now touches the range after it and if they have
196 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000197 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000198 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000199 I->end = Next->end;
200 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000201 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000202}
203
204
205/// extendIntervalStartTo - This method is used when we want to extend the range
206/// specified by I to start at the specified endpoint. To do this, we should
207/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000208LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000209LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000210 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000211 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000212
213 // Search for the first interval that we can't merge with.
214 Ranges::iterator MergeTo = I;
215 do {
216 if (MergeTo == ranges.begin()) {
217 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000218 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000219 return I;
220 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000222 --MergeTo;
223 } while (NewStart <= MergeTo->start);
224
225 // If we start in the middle of another interval, just delete a range and
226 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000227 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000228 MergeTo->end = I->end;
229 } else {
230 // Otherwise, extend the interval right after.
231 ++MergeTo;
232 MergeTo->start = NewStart;
233 MergeTo->end = I->end;
234 }
235
Oscar Fuentesee56c422010-08-02 06:00:15 +0000236 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000237 return MergeTo;
238}
239
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000240LiveInterval::iterator
241LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000242 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000243 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000244
245 // If the inserted interval starts in the middle or right at the end of
246 // another interval, just extend that interval to contain the range of LR.
247 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000248 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000249 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000250 if (B->start <= Start && B->end >= Start) {
251 extendIntervalEndTo(B, End);
252 return B;
253 }
254 } else {
255 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000256 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000257 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000258 "Cannot overlap two LiveRanges with differing ValID's"
259 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000260 }
261 }
262
263 // Otherwise, if this range ends in the middle of, or right next to, another
264 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000265 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000266 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000267 if (it->start <= End) {
268 it = extendIntervalStartTo(it, Start);
269
270 // If LR is a complete superset of an interval, we may need to grow its
271 // endpoint as well.
272 if (End > it->end)
273 extendIntervalEndTo(it, End);
274 return it;
275 }
276 } else {
277 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000278 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000279 assert(it->start >= End &&
280 "Cannot overlap two LiveRanges with differing ValID's");
281 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000282 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000283
284 // Otherwise, this is just a new range that doesn't interact with anything.
285 // Insert it.
286 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000287}
288
Chris Lattnerabf295f2004-07-24 02:52:23 +0000289
290/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000291/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000292void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000293 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000294 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000295 Ranges::iterator I = find(Start);
296 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000297 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000298
299 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000300 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000301 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000302 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000303 if (RemoveDeadValNo) {
304 // Check if val# is dead.
305 bool isDead = true;
306 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
307 if (II != I && II->valno == ValNo) {
308 isDead = false;
309 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000310 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000311 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000312 // Now that ValNo is dead, remove it.
313 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000314 }
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) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000326 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000327 return;
328 }
329
330 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000331 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000332 I->end = Start; // Trim the old interval.
333
334 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000335 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000336}
337
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000338/// removeValNo - Remove all the ranges defined by the specified value#.
339/// Also remove the value# from value# list.
340void LiveInterval::removeValNo(VNInfo *ValNo) {
341 if (empty()) return;
342 Ranges::iterator I = ranges.end();
343 Ranges::iterator E = ranges.begin();
344 do {
345 --I;
346 if (I->valno == ValNo)
347 ranges.erase(I);
348 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000349 // Now that ValNo is dead, remove it.
350 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000351}
Lang Hames86511252009-09-04 20:41:11 +0000352
Lang Hames86511252009-09-04 20:41:11 +0000353/// findDefinedVNInfo - Find the VNInfo defined by the specified
354/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000355VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000356 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000357 i != e; ++i) {
358 if ((*i)->def == Idx)
359 return *i;
360 }
361
362 return 0;
363}
364
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000365/// join - Join two live intervals (this, and other) together. This applies
366/// mappings to the value numbers in the LHS/RHS intervals as specified. If
367/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000368void LiveInterval::join(LiveInterval &Other,
369 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000370 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000371 SmallVector<VNInfo*, 16> &NewVNInfo,
372 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000373 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000374 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000375 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000376 unsigned NumVals = getNumValNums();
377 unsigned NumNewVals = NewVNInfo.size();
378 for (unsigned i = 0; i != NumVals; ++i) {
379 unsigned LHSValID = LHSValNoAssignments[i];
380 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000381 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000382 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000383 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000384
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000385 // If we have to apply a mapping to our base interval assignment, rewrite it
386 // now.
387 if (MustMapCurValNos) {
388 // Map the first live range.
389 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000390 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000391 ++OutIt;
392 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000393 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000394
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000395 // If this live range has the same value # as its immediate predecessor,
396 // and if they are neighbors, remove one LiveRange. This happens when we
397 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000398 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000399 (OutIt-1)->end = OutIt->end;
400 } else {
401 if (I != OutIt) {
402 OutIt->start = I->start;
403 OutIt->end = I->end;
404 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000405
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000406 // Didn't merge, on to the next one.
407 ++OutIt;
408 }
409 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000410
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000411 // If we merge some live ranges, chop off the end.
412 ranges.erase(OutIt, end());
413 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000414
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000415 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000416 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000417 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
418 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
419
420 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000421 // LiveInterval now. Also remove dead val#'s.
422 unsigned NumValNos = 0;
423 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000424 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000425 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000426 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000427 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000428 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000429 valnos[NumValNos] = VNI;
430 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000431 }
432 }
Evan Cheng34301352007-09-01 02:03:17 +0000433 if (NumNewVals < NumVals)
434 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000435
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000436 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000437 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000438 unsigned RangeNo = 0;
439 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
440 // Map the valno in the other live range to the current live range.
441 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000442 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000443 InsertPos = addRangeFrom(*I, InsertPos);
444 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000445
David Greene29ff37f2009-07-22 20:08:25 +0000446 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000447}
448
Chris Lattnerf21f0202006-09-02 05:26:59 +0000449/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
450/// interval as the specified value number. The LiveRanges in RHS are
451/// allowed to overlap with LiveRanges in the current interval, but only if
452/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000453void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000454 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000455 // TODO: Make this more efficient.
456 iterator InsertPos = begin();
457 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000458 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000459 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000460 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000461 InsertPos = addRangeFrom(Tmp, InsertPos);
462 }
463}
464
465
Evan Cheng32dfbea2007-10-12 08:50:34 +0000466/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
467/// in RHS into this live interval as the specified value number.
468/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000469/// current interval, it will replace the value numbers of the overlaped
470/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000471void LiveInterval::MergeValueInAsValue(
472 const LiveInterval &RHS,
473 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000474 SmallVector<VNInfo*, 4> ReplacedValNos;
475 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000476 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000477 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000478 if (I->valno != RHSValNo)
479 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000480 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000481 IP = std::upper_bound(IP, end(), Start);
482 // If the start of this range overlaps with an existing liverange, trim it.
483 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000484 if (IP[-1].valno != LHSValNo) {
485 ReplacedValNos.push_back(IP[-1].valno);
486 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000487 }
488 Start = IP[-1].end;
489 // Trimmed away the whole range?
490 if (Start >= End) continue;
491 }
492 // If the end of this range overlaps with an existing liverange, trim it.
493 if (IP != end() && End > IP->start) {
494 if (IP->valno != LHSValNo) {
495 ReplacedValNos.push_back(IP->valno);
496 IP->valno = LHSValNo; // Update val#.
497 }
498 End = IP->start;
499 // If this trimmed away the whole range, ignore it.
500 if (Start == End) continue;
501 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000502
Evan Cheng32dfbea2007-10-12 08:50:34 +0000503 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000504 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
505 }
506
507
508 SmallSet<VNInfo*, 4> Seen;
509 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
510 VNInfo *V1 = ReplacedValNos[i];
511 if (Seen.insert(V1)) {
512 bool isDead = true;
513 for (const_iterator I = begin(), E = end(); I != E; ++I)
514 if (I->valno == V1) {
515 isDead = false;
516 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000517 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000518 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000519 // Now that V1 is dead, remove it.
520 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000521 }
522 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000523 }
524}
525
526
Evan Chenga2e64352009-03-11 00:03:21 +0000527
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000528/// MergeValueNumberInto - This method is called when two value nubmers
529/// are found to be equivalent. This eliminates V1, replacing all
530/// LiveRanges with the V1 value number with the V2 value number. This can
531/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000532VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000533 assert(V1 != V2 && "Identical value#'s are always equivalent!");
534
535 // This code actually merges the (numerically) larger value number into the
536 // smaller value number, which is likely to allow us to compactify the value
537 // space. The only thing we have to be careful of is to preserve the
538 // instruction that defines the result value.
539
540 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000541 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000542 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000543 std::swap(V1, V2);
544 }
545
546 // Merge V1 live ranges into V2.
547 for (iterator I = begin(); I != end(); ) {
548 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000549 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000550
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000551 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
552 // range, extend it.
553 if (LR != begin()) {
554 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000555 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000556 Prev->end = LR->end;
557
558 // Erase this live-range.
559 ranges.erase(LR);
560 I = Prev+1;
561 LR = Prev;
562 }
563 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000564
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000565 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
566 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000567 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000568
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000569 // If we can merge it into later V2 live ranges, do so now. We ignore any
570 // following V1 live ranges, as they will be merged in subsequent iterations
571 // of the loop.
572 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000573 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000574 LR->end = I->end;
575 ranges.erase(I);
576 I = LR+1;
577 }
578 }
579 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000580
Lang Hames6f4e4df2010-07-26 01:49:41 +0000581 // Now that V1 is dead, remove it.
582 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000583
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000584 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000585}
586
Evan Cheng32dfbea2007-10-12 08:50:34 +0000587void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000588 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000589 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000590 ranges.clear();
591 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000592 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000593 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
594
Evan Cheng32dfbea2007-10-12 08:50:34 +0000595 weight = RHS.weight;
596 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
597 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000598 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000599 }
600 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
601 const LiveRange &LR = RHS.ranges[i];
602 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
603 }
604}
605
Evan Chenge52eef82007-04-17 20:25:11 +0000606unsigned LiveInterval::getSize() const {
607 unsigned Sum = 0;
608 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000609 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000610 return Sum;
611}
612
David Greene29ff37f2009-07-22 20:08:25 +0000613/// ComputeJoinedWeight - Set the weight of a live interval Joined
614/// after Other has been merged into it.
615void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
616 // If either of these intervals was spilled, the weight is the
617 // weight of the non-spilled interval. This can only happen with
618 // iterative coalescers.
619
David Greene92b78bb2009-07-22 22:32:19 +0000620 if (Other.weight != HUGE_VALF) {
621 weight += Other.weight;
622 }
623 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000624 !TargetRegisterInfo::isPhysicalRegister(reg)) {
625 // Remove this assert if you have an iterative coalescer
626 assert(0 && "Joining to spilled interval");
627 weight = Other.weight;
628 }
David Greene29ff37f2009-07-22 20:08:25 +0000629 else {
630 // Otherwise the weight stays the same
631 // Remove this assert if you have an iterative coalescer
632 assert(0 && "Joining from spilled interval");
633 }
634}
635
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000636raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
637 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
638}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000639
Chris Lattnerabf295f2004-07-24 02:52:23 +0000640void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000641 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000642}
643
Chris Lattnerc02497f2009-08-23 03:47:42 +0000644void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000645 if (isStackSlot())
646 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000647 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000648 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000649 else
650 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000651
Chris Lattner38135af2005-05-14 05:34:15 +0000652 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000653
Chris Lattner38135af2005-05-14 05:34:15 +0000654 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000655 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000656 else {
657 OS << " = ";
658 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000659 E = ranges.end(); I != E; ++I) {
660 OS << *I;
661 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
662 }
Chris Lattner38135af2005-05-14 05:34:15 +0000663 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000664
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000665 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000666 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000667 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000668 unsigned vnum = 0;
669 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
670 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000671 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000672 if (vnum) OS << " ";
673 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000674 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000675 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000676 } else {
Lang Hames61945692009-12-09 05:39:12 +0000677 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000678 OS << "?";
679 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000680 OS << vni->def;
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000681 if (vni->hasPHIKill())
682 OS << "-phikill";
683 if (vni->hasRedefByEC())
684 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000685 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000686 }
687 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000688}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000689
690void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000691 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000692}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000693
694
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000695void LiveRange::print(raw_ostream &os) const {
696 os << *this;
697}