blob: fd5fe787b6bf07c6ce43970b99e3ae2de0803397 [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 Olesen2de0e802010-09-21 18:24:30 +000033// CompEnd - Compare LiveRange end to Pos.
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000034namespace {
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000035struct CompEnd {
36 bool operator()(SlotIndex Pos, const LiveRange &LR) const {
37 return Pos < LR.end;
38 }
39 bool operator()(const LiveRange &LR, SlotIndex Pos) const {
40 return LR.end < Pos;
41 }
42};
Jakob Stoklund Olesen89bfef02010-09-21 18:34:17 +000043}
Chris Lattnerfb449b92004-07-23 17:49:16 +000044
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +000045LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
Jakob Stoklund Olesen2de0e802010-09-21 18:24:30 +000046 return std::upper_bound(begin(), end(), Pos, CompEnd());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000047}
48
49/// killedInRange - Return true if the interval has kills in [Start,End).
50bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
51 Ranges::const_iterator r =
52 std::lower_bound(ranges.begin(), ranges.end(), End);
53
54 // Now r points to the first interval with start >= End, or ranges.end().
55 if (r == ranges.begin())
56 return false;
57
58 --r;
59 // Now r points to the last interval with end <= End.
60 // r->end is the kill point.
61 return r->end >= Start && r->end < End;
62}
63
Chris Lattnerbae74d92004-11-18 03:47:34 +000064// overlaps - Return true if the intersection of the two live intervals is
65// not empty.
66//
Chris Lattnerfb449b92004-07-23 17:49:16 +000067// An example for overlaps():
68//
69// 0: A = ...
70// 4: B = ...
71// 8: C = A + B ;; last use of A
72//
73// The live intervals should look like:
74//
75// A = [3, 11)
76// B = [7, x)
77// C = [11, y)
78//
79// A->overlaps(C) should return false since we want to be able to join
80// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +000081//
82bool LiveInterval::overlapsFrom(const LiveInterval& other,
83 const_iterator StartPos) const {
Jakob Stoklund Olesen6382d2c2010-07-13 19:56:28 +000084 assert(!empty() && "empty interval");
Chris Lattnerbae74d92004-11-18 03:47:34 +000085 const_iterator i = begin();
86 const_iterator ie = end();
87 const_iterator j = StartPos;
88 const_iterator je = other.end();
89
90 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +000091 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +000092
Chris Lattnerfb449b92004-07-23 17:49:16 +000093 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +000094 i = std::upper_bound(i, ie, j->start);
Chris Lattnerfb449b92004-07-23 17:49:16 +000095 if (i != ranges.begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +000096 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +000097 ++StartPos;
98 if (StartPos != other.end() && StartPos->start <= i->start) {
99 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000100 j = std::upper_bound(j, je, i->start);
101 if (j != other.ranges.begin()) --j;
102 }
Chris Lattneraa141472004-07-23 18:40:00 +0000103 } else {
104 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000105 }
106
Chris Lattner9fddc122004-11-18 05:28:21 +0000107 if (j == je) return false;
108
109 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000110 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000111 std::swap(i, j);
112 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000113 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000114
115 if (i->end > j->start)
116 return true;
117 ++i;
118 }
119
120 return false;
121}
122
Evan Chengcccdb2b2009-04-18 08:52:15 +0000123/// overlaps - Return true if the live interval overlaps a range specified
124/// by [Start, End).
Lang Hames233a60e2009-11-03 23:52:08 +0000125bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000126 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000127 const_iterator I = std::lower_bound(begin(), end(), End);
128 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000129}
130
Lang Hames6f4e4df2010-07-26 01:49:41 +0000131
132/// ValNo is dead, remove it. If it is the largest value number, just nuke it
133/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
134/// it can be nuked later.
135void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
136 if (ValNo->id == getNumValNums()-1) {
137 do {
138 valnos.pop_back();
139 } while (!valnos.empty() && valnos.back()->isUnused());
140 } else {
141 ValNo->setIsUnused(true);
142 }
143}
144
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000145/// RenumberValues - Renumber all values in order of appearance and delete the
146/// remaining unused values.
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000147void LiveInterval::RenumberValues(LiveIntervals &lis) {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000148 SmallPtrSet<VNInfo*, 8> Seen;
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000149 bool seenPHIDef = false;
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000150 valnos.clear();
151 for (const_iterator I = begin(), E = end(); I != E; ++I) {
152 VNInfo *VNI = I->valno;
153 if (!Seen.insert(VNI))
154 continue;
155 assert(!VNI->isUnused() && "Unused valno used by live range");
156 VNI->id = (unsigned)valnos.size();
157 valnos.push_back(VNI);
Jakob Stoklund Olesenfff2c472010-08-12 20:38:03 +0000158 VNI->setHasPHIKill(false);
159 if (VNI->isPHIDef())
160 seenPHIDef = true;
161 }
162
163 // Recompute phi kill flags.
164 if (!seenPHIDef)
165 return;
166 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
167 VNInfo *VNI = *I;
168 if (!VNI->isPHIDef())
169 continue;
170 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
171 assert(PHIBB && "No basic block for phi-def");
172 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
173 PE = PHIBB->pred_end(); PI != PE; ++PI) {
174 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
175 if (KVNI)
176 KVNI->setHasPHIKill(true);
177 }
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000178 }
179}
180
Chris Lattnerb26c2152004-07-23 19:38:44 +0000181/// extendIntervalEndTo - This method is used when we want to extend the range
182/// specified by I to end at the specified endpoint. To do this, we should
183/// merge and eliminate all ranges that this will overlap with. The iterator is
184/// not invalidated.
Lang Hames233a60e2009-11-03 23:52:08 +0000185void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000186 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000187 VNInfo *ValNo = I->valno;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000188
Chris Lattnerb26c2152004-07-23 19:38:44 +0000189 // Search for the first interval that we can't merge with.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000190 Ranges::iterator MergeTo = llvm::next(I);
Chris Lattnerabf295f2004-07-24 02:52:23 +0000191 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000192 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000193 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000194
195 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
196 I->end = std::max(NewEnd, prior(MergeTo)->end);
197
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000198 // Erase any dead ranges.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000199 ranges.erase(llvm::next(I), MergeTo);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000200
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000201 // If the newly formed range now touches the range after it and if they have
202 // the same value number, merge the two ranges into one range.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000203 Ranges::iterator Next = llvm::next(I);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000204 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
Chris Lattnercef60102005-10-20 22:50:10 +0000205 I->end = Next->end;
206 ranges.erase(Next);
Chris Lattnerb0fa11c2005-10-20 07:39:25 +0000207 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000208}
209
210
211/// extendIntervalStartTo - This method is used when we want to extend the range
212/// specified by I to start at the specified endpoint. To do this, we should
213/// merge and eliminate all ranges that this will overlap with.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000214LiveInterval::Ranges::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000215LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000216 assert(I != ranges.end() && "Not a valid interval!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000217 VNInfo *ValNo = I->valno;
Chris Lattnerb26c2152004-07-23 19:38:44 +0000218
219 // Search for the first interval that we can't merge with.
220 Ranges::iterator MergeTo = I;
221 do {
222 if (MergeTo == ranges.begin()) {
223 I->start = NewStart;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000224 ranges.erase(MergeTo, I);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000225 return I;
226 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000227 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000228 --MergeTo;
229 } while (NewStart <= MergeTo->start);
230
231 // If we start in the middle of another interval, just delete a range and
232 // extend that interval.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000233 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
Chris Lattnerb26c2152004-07-23 19:38:44 +0000234 MergeTo->end = I->end;
235 } else {
236 // Otherwise, extend the interval right after.
237 ++MergeTo;
238 MergeTo->start = NewStart;
239 MergeTo->end = I->end;
240 }
241
Oscar Fuentesee56c422010-08-02 06:00:15 +0000242 ranges.erase(llvm::next(MergeTo), llvm::next(I));
Chris Lattnerb26c2152004-07-23 19:38:44 +0000243 return MergeTo;
244}
245
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000246LiveInterval::iterator
247LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
Lang Hames233a60e2009-11-03 23:52:08 +0000248 SlotIndex Start = LR.start, End = LR.end;
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000249 iterator it = std::upper_bound(From, ranges.end(), Start);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000250
251 // If the inserted interval starts in the middle or right at the end of
252 // another interval, just extend that interval to contain the range of LR.
253 if (it != ranges.begin()) {
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000254 iterator B = prior(it);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000255 if (LR.valno == B->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000256 if (B->start <= Start && B->end >= Start) {
257 extendIntervalEndTo(B, End);
258 return B;
259 }
260 } else {
261 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000262 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000263 assert(B->end <= Start &&
Brian Gaeke8311bef2004-11-16 06:52:35 +0000264 "Cannot overlap two LiveRanges with differing ValID's"
265 " (did you def the same reg twice in a MachineInstr?)");
Chris Lattnerb26c2152004-07-23 19:38:44 +0000266 }
267 }
268
269 // Otherwise, if this range ends in the middle of, or right next to, another
270 // interval, merge it into that interval.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000271 if (it != ranges.end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000272 if (LR.valno == it->valno) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000273 if (it->start <= End) {
274 it = extendIntervalStartTo(it, Start);
275
276 // If LR is a complete superset of an interval, we may need to grow its
277 // endpoint as well.
278 if (End > it->end)
279 extendIntervalEndTo(it, End);
280 return it;
281 }
282 } else {
283 // Check to make sure that we are not overlapping two live ranges with
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000284 // different valno's.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000285 assert(it->start >= End &&
286 "Cannot overlap two LiveRanges with differing ValID's");
287 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000288 }
Chris Lattnerb26c2152004-07-23 19:38:44 +0000289
290 // Otherwise, this is just a new range that doesn't interact with anything.
291 // Insert it.
292 return ranges.insert(it, LR);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000293}
294
Chris Lattnerabf295f2004-07-24 02:52:23 +0000295
296/// removeRange - Remove the specified range from this interval. Note that
Evan Cheng42cc6e32009-01-29 00:06:09 +0000297/// the range must be in a single LiveRange in its entirety.
Lang Hames233a60e2009-11-03 23:52:08 +0000298void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000299 bool RemoveDeadValNo) {
Chris Lattnerabf295f2004-07-24 02:52:23 +0000300 // Find the LiveRange containing this span.
Jakob Stoklund Olesenf568b272010-09-21 17:12:18 +0000301 Ranges::iterator I = find(Start);
302 assert(I != ranges.end() && "Range is not in interval!");
Lang Hames86511252009-09-04 20:41:11 +0000303 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000304
305 // If the span we are removing is at the start of the LiveRange, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000306 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000307 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000308 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000309 if (RemoveDeadValNo) {
310 // Check if val# is dead.
311 bool isDead = true;
312 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
313 if (II != I && II->valno == ValNo) {
314 isDead = false;
315 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000316 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000317 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000318 // Now that ValNo is dead, remove it.
319 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000320 }
321 }
322
Chris Lattnerabf295f2004-07-24 02:52:23 +0000323 ranges.erase(I); // Removed the whole LiveRange.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000324 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000325 I->start = End;
326 return;
327 }
328
329 // Otherwise if the span we are removing is at the end of the LiveRange,
330 // adjust the other way.
331 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000332 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000333 return;
334 }
335
336 // Otherwise, we are splitting the LiveRange into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000337 SlotIndex OldEnd = I->end;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000338 I->end = Start; // Trim the old interval.
339
340 // Insert the new one.
Oscar Fuentesee56c422010-08-02 06:00:15 +0000341 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000342}
343
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000344/// removeValNo - Remove all the ranges defined by the specified value#.
345/// Also remove the value# from value# list.
346void LiveInterval::removeValNo(VNInfo *ValNo) {
347 if (empty()) return;
348 Ranges::iterator I = ranges.end();
349 Ranges::iterator E = ranges.begin();
350 do {
351 --I;
352 if (I->valno == ValNo)
353 ranges.erase(I);
354 } while (I != E);
Lang Hames6f4e4df2010-07-26 01:49:41 +0000355 // Now that ValNo is dead, remove it.
356 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000357}
Lang Hames86511252009-09-04 20:41:11 +0000358
Lang Hames86511252009-09-04 20:41:11 +0000359/// findDefinedVNInfo - Find the VNInfo defined by the specified
360/// index (register interval).
Lang Hames233a60e2009-11-03 23:52:08 +0000361VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
Evan Cheng3f32d652008-06-04 09:18:41 +0000362 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
Lang Hames86511252009-09-04 20:41:11 +0000363 i != e; ++i) {
364 if ((*i)->def == Idx)
365 return *i;
366 }
367
368 return 0;
369}
370
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000371/// join - Join two live intervals (this, and other) together. This applies
372/// mappings to the value numbers in the LHS/RHS intervals as specified. If
373/// the intervals are not joinable, this aborts.
Lang Hames233a60e2009-11-03 23:52:08 +0000374void LiveInterval::join(LiveInterval &Other,
375 const int *LHSValNoAssignments,
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000376 const int *RHSValNoAssignments,
Evan Cheng90f95f82009-06-14 20:22:55 +0000377 SmallVector<VNInfo*, 16> &NewVNInfo,
378 MachineRegisterInfo *MRI) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000379 // Determine if any of our live range values are mapped. This is uncommon, so
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000380 // we want to avoid the interval scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000381 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000382 unsigned NumVals = getNumValNums();
383 unsigned NumNewVals = NewVNInfo.size();
384 for (unsigned i = 0; i != NumVals; ++i) {
385 unsigned LHSValID = LHSValNoAssignments[i];
386 if (i != LHSValID ||
Evan Chengf3bb2e62007-09-05 21:46:51 +0000387 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000388 MustMapCurValNos = true;
Chris Lattnerdeb99712004-07-24 03:41:50 +0000389 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000390
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000391 // If we have to apply a mapping to our base interval assignment, rewrite it
392 // now.
393 if (MustMapCurValNos) {
394 // Map the first live range.
395 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000396 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000397 ++OutIt;
398 for (iterator I = OutIt, E = end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000399 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000400
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000401 // If this live range has the same value # as its immediate predecessor,
402 // and if they are neighbors, remove one LiveRange. This happens when we
403 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000404 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000405 (OutIt-1)->end = OutIt->end;
406 } else {
407 if (I != OutIt) {
408 OutIt->start = I->start;
409 OutIt->end = I->end;
410 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000411
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000412 // Didn't merge, on to the next one.
413 ++OutIt;
414 }
415 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000416
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000417 // If we merge some live ranges, chop off the end.
418 ranges.erase(OutIt, end());
419 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000420
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000421 // Remember assignements because val# ids are changing.
Evan Cheng34301352007-09-01 02:03:17 +0000422 SmallVector<unsigned, 16> OtherAssignments;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000423 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
424 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
425
426 // Update val# info. Renumber them and make sure they all belong to this
Evan Chengf3bb2e62007-09-05 21:46:51 +0000427 // LiveInterval now. Also remove dead val#'s.
428 unsigned NumValNos = 0;
429 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000430 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000431 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000432 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000433 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000434 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000435 valnos[NumValNos] = VNI;
436 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000437 }
438 }
Evan Cheng34301352007-09-01 02:03:17 +0000439 if (NumNewVals < NumVals)
440 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000441
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000442 // Okay, now insert the RHS live ranges into the LHS.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000443 iterator InsertPos = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000444 unsigned RangeNo = 0;
445 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
446 // Map the valno in the other live range to the current live range.
447 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000448 assert(I->valno && "Adding a dead range?");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000449 InsertPos = addRangeFrom(*I, InsertPos);
450 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000451
David Greene29ff37f2009-07-22 20:08:25 +0000452 ComputeJoinedWeight(Other);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000453}
454
Chris Lattnerf21f0202006-09-02 05:26:59 +0000455/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
456/// interval as the specified value number. The LiveRanges in RHS are
457/// allowed to overlap with LiveRanges in the current interval, but only if
458/// the overlapping LiveRanges have the specified value number.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000459void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000460 VNInfo *LHSValNo) {
Chris Lattnerf21f0202006-09-02 05:26:59 +0000461 // TODO: Make this more efficient.
462 iterator InsertPos = begin();
463 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000464 // Map the valno in the other live range to the current live range.
Chris Lattnerf21f0202006-09-02 05:26:59 +0000465 LiveRange Tmp = *I;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000466 Tmp.valno = LHSValNo;
Chris Lattnerf21f0202006-09-02 05:26:59 +0000467 InsertPos = addRangeFrom(Tmp, InsertPos);
468 }
469}
470
471
Evan Cheng32dfbea2007-10-12 08:50:34 +0000472/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
473/// in RHS into this live interval as the specified value number.
474/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000475/// current interval, it will replace the value numbers of the overlaped
476/// live ranges with the specified value number.
Lang Hames233a60e2009-11-03 23:52:08 +0000477void LiveInterval::MergeValueInAsValue(
478 const LiveInterval &RHS,
479 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000480 SmallVector<VNInfo*, 4> ReplacedValNos;
481 iterator IP = begin();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000482 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000483 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
Evan Cheng32dfbea2007-10-12 08:50:34 +0000484 if (I->valno != RHSValNo)
485 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000486 SlotIndex Start = I->start, End = I->end;
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000487 IP = std::upper_bound(IP, end(), Start);
488 // If the start of this range overlaps with an existing liverange, trim it.
489 if (IP != begin() && IP[-1].end > Start) {
Evan Cheng294e6522008-01-30 22:44:55 +0000490 if (IP[-1].valno != LHSValNo) {
491 ReplacedValNos.push_back(IP[-1].valno);
492 IP[-1].valno = LHSValNo; // Update val#.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000493 }
494 Start = IP[-1].end;
495 // Trimmed away the whole range?
496 if (Start >= End) continue;
497 }
498 // If the end of this range overlaps with an existing liverange, trim it.
499 if (IP != end() && End > IP->start) {
500 if (IP->valno != LHSValNo) {
501 ReplacedValNos.push_back(IP->valno);
502 IP->valno = LHSValNo; // Update val#.
503 }
504 End = IP->start;
505 // If this trimmed away the whole range, ignore it.
506 if (Start == End) continue;
507 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000508
Evan Cheng32dfbea2007-10-12 08:50:34 +0000509 // Map the valno in the other live range to the current live range.
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000510 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
511 }
512
513
514 SmallSet<VNInfo*, 4> Seen;
515 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
516 VNInfo *V1 = ReplacedValNos[i];
517 if (Seen.insert(V1)) {
518 bool isDead = true;
519 for (const_iterator I = begin(), E = end(); I != E; ++I)
520 if (I->valno == V1) {
521 isDead = false;
522 break;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000523 }
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000524 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000525 // Now that V1 is dead, remove it.
526 markValNoForDeletion(V1);
Evan Cheng3c1f4a42007-10-17 02:13:29 +0000527 }
528 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000529 }
530}
531
532
Evan Chenga2e64352009-03-11 00:03:21 +0000533
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000534/// MergeValueNumberInto - This method is called when two value nubmers
535/// are found to be equivalent. This eliminates V1, replacing all
536/// LiveRanges with the V1 value number with the V2 value number. This can
537/// cause merging of V1/V2 values numbers and compaction of the value space.
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000538VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000539 assert(V1 != V2 && "Identical value#'s are always equivalent!");
540
541 // This code actually merges the (numerically) larger value number into the
542 // smaller value number, which is likely to allow us to compactify the value
543 // space. The only thing we have to be careful of is to preserve the
544 // instruction that defines the result value.
545
546 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000547 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000548 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000549 std::swap(V1, V2);
550 }
551
552 // Merge V1 live ranges into V2.
553 for (iterator I = begin(); I != end(); ) {
554 iterator LR = I++;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000555 if (LR->valno != V1) continue; // Not a V1 LiveRange.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000556
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000557 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
558 // range, extend it.
559 if (LR != begin()) {
560 iterator Prev = LR-1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000561 if (Prev->valno == V2 && Prev->end == LR->start) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000562 Prev->end = LR->end;
563
564 // Erase this live-range.
565 ranges.erase(LR);
566 I = Prev+1;
567 LR = Prev;
568 }
569 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000570
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000571 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
572 // Ensure that it is a V2 live-range.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000573 LR->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000574
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000575 // If we can merge it into later V2 live ranges, do so now. We ignore any
576 // following V1 live ranges, as they will be merged in subsequent iterations
577 // of the loop.
578 if (I != end()) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000579 if (I->start == LR->end && I->valno == V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000580 LR->end = I->end;
581 ranges.erase(I);
582 I = LR+1;
583 }
584 }
585 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000586
Lang Hames6f4e4df2010-07-26 01:49:41 +0000587 // Now that V1 is dead, remove it.
588 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000589
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000590 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000591}
592
Evan Cheng32dfbea2007-10-12 08:50:34 +0000593void LiveInterval::Copy(const LiveInterval &RHS,
Evan Cheng90f95f82009-06-14 20:22:55 +0000594 MachineRegisterInfo *MRI,
Benjamin Kramer991de142010-03-30 20:16:45 +0000595 VNInfo::Allocator &VNInfoAllocator) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000596 ranges.clear();
597 valnos.clear();
Evan Cheng358dec52009-06-15 08:28:29 +0000598 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000599 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
600
Evan Cheng32dfbea2007-10-12 08:50:34 +0000601 weight = RHS.weight;
602 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
603 const VNInfo *VNI = RHS.getValNumInfo(i);
Lang Hames857c4e02009-06-17 21:01:20 +0000604 createValueCopy(VNI, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000605 }
606 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
607 const LiveRange &LR = RHS.ranges[i];
608 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
609 }
610}
611
Evan Chenge52eef82007-04-17 20:25:11 +0000612unsigned LiveInterval::getSize() const {
613 unsigned Sum = 0;
614 for (const_iterator I = begin(), E = end(); I != E; ++I)
Lang Hames86511252009-09-04 20:41:11 +0000615 Sum += I->start.distance(I->end);
Evan Chenge52eef82007-04-17 20:25:11 +0000616 return Sum;
617}
618
David Greene29ff37f2009-07-22 20:08:25 +0000619/// ComputeJoinedWeight - Set the weight of a live interval Joined
620/// after Other has been merged into it.
621void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
622 // If either of these intervals was spilled, the weight is the
623 // weight of the non-spilled interval. This can only happen with
624 // iterative coalescers.
625
David Greene92b78bb2009-07-22 22:32:19 +0000626 if (Other.weight != HUGE_VALF) {
627 weight += Other.weight;
628 }
629 else if (weight == HUGE_VALF &&
David Greene29ff37f2009-07-22 20:08:25 +0000630 !TargetRegisterInfo::isPhysicalRegister(reg)) {
631 // Remove this assert if you have an iterative coalescer
632 assert(0 && "Joining to spilled interval");
633 weight = Other.weight;
634 }
David Greene29ff37f2009-07-22 20:08:25 +0000635 else {
636 // Otherwise the weight stays the same
637 // Remove this assert if you have an iterative coalescer
638 assert(0 && "Joining from spilled interval");
639 }
640}
641
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000642raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
643 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
644}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000645
Chris Lattnerabf295f2004-07-24 02:52:23 +0000646void LiveRange::dump() const {
David Greene52421542010-01-04 22:41:43 +0000647 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000648}
649
Chris Lattnerc02497f2009-08-23 03:47:42 +0000650void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Evan Cheng99ec7792008-06-23 21:03:19 +0000651 if (isStackSlot())
652 OS << "SS#" << getStackSlotIndex();
Evan Cheng3f32d652008-06-04 09:18:41 +0000653 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000654 OS << TRI->getName(reg);
Chris Lattner38135af2005-05-14 05:34:15 +0000655 else
656 OS << "%reg" << reg;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000657
Chris Lattner38135af2005-05-14 05:34:15 +0000658 OS << ',' << weight;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000659
Chris Lattner38135af2005-05-14 05:34:15 +0000660 if (empty())
Evan Cheng3f32d652008-06-04 09:18:41 +0000661 OS << " EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000662 else {
663 OS << " = ";
664 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000665 E = ranges.end(); I != E; ++I) {
666 OS << *I;
667 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
668 }
Chris Lattner38135af2005-05-14 05:34:15 +0000669 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000670
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000671 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000672 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000673 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000674 unsigned vnum = 0;
675 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
676 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000677 const VNInfo *vni = *i;
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000678 if (vnum) OS << " ";
679 OS << vnum << "@";
Lang Hames857c4e02009-06-17 21:01:20 +0000680 if (vni->isUnused()) {
Evan Cheng8df78602007-08-08 03:00:28 +0000681 OS << "x";
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000682 } else {
Lang Hames61945692009-12-09 05:39:12 +0000683 if (!vni->isDefAccurate() && !vni->isPHIDef())
Evan Cheng4f8ff162007-08-11 00:59:19 +0000684 OS << "?";
685 else
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000686 OS << vni->def;
Jakob Stoklund Olesend9f6ec92010-07-13 21:19:05 +0000687 if (vni->hasPHIKill())
688 OS << "-phikill";
689 if (vni->hasRedefByEC())
690 OS << "-ec";
Evan Chenga8d94f12007-08-07 23:49:57 +0000691 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000692 }
693 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000694}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000695
696void LiveInterval::dump() const {
David Greene52421542010-01-04 22:41:43 +0000697 dbgs() << *this << "\n";
Chris Lattnerabf295f2004-07-24 02:52:23 +0000698}
Jeff Cohenc21c5ee2006-12-15 22:57:14 +0000699
700
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000701void LiveRange::print(raw_ostream &os) const {
702 os << *this;
703}