1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LiveRange and LiveInterval classes. Given some
11 // numbering of each the machine instructions an interval [i, j) is said to be a
12 // live interval for register v if there is no instruction with number j' > j
13 // such that v is live at j' abd there is no instruction with number i' < i such
14 // that v is live at i'. In this implementation intervals can have holes,
15 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
16 // individual range is represented as an instance of LiveRange, and the whole
17 // interval is represented as an instance of LiveInterval.
19 //===----------------------------------------------------------------------===//
21 #include "LiveInterval.h"
22 #include "Support/STLExtras.h"
27 // An example for liveAt():
29 // this = [1,4), liveAt(0) will return false. The instruction defining this
30 // spans slots [0,3]. The interval belongs to an spilled definition of the
31 // variable it represents. This is because slot 1 is used (def slot) and spans
32 // up to slot 3 (store slot).
34 bool LiveInterval::liveAt(unsigned I) const {
35 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
37 if (r == ranges.begin())
41 return r->contains(I);
44 // An example for overlaps():
48 // 8: C = A + B ;; last use of A
50 // The live intervals should look like:
56 // A->overlaps(C) should return false since we want to be able to join
58 bool LiveInterval::overlaps(const LiveInterval& other) const {
59 Ranges::const_iterator i = ranges.begin();
60 Ranges::const_iterator ie = ranges.end();
61 Ranges::const_iterator j = other.ranges.begin();
62 Ranges::const_iterator je = other.ranges.end();
63 if (i->start < j->start) {
64 i = std::upper_bound(i, ie, j->start);
65 if (i != ranges.begin()) --i;
66 } else if (j->start < i->start) {
67 j = std::upper_bound(j, je, i->start);
68 if (j != other.ranges.begin()) --j;
73 while (i != ie && j != je) {
74 if (i->start == j->start)
77 if (i->start > j->start) {
81 assert(i->start < j->start);
83 if (i->end > j->start)
91 /// joinable - Two intervals are joinable if the either don't overlap at all
92 /// or if the destination of the copy is a single assignment value, and it
93 /// only overlaps with one value in the source interval.
94 bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
95 return overlaps(other);
99 /// extendIntervalEndTo - This method is used when we want to extend the range
100 /// specified by I to end at the specified endpoint. To do this, we should
101 /// merge and eliminate all ranges that this will overlap with. The iterator is
103 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
104 assert(I != ranges.end() && "Not a valid interval!");
105 unsigned ValId = I->ValId;
107 // Search for the first interval that we can't merge with.
108 Ranges::iterator MergeTo = next(I);
109 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
110 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
113 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
114 I->end = std::max(NewEnd, prior(MergeTo)->end);
116 // Erase any dead ranges
117 ranges.erase(next(I), MergeTo);
121 /// extendIntervalStartTo - This method is used when we want to extend the range
122 /// specified by I to start at the specified endpoint. To do this, we should
123 /// merge and eliminate all ranges that this will overlap with.
124 LiveInterval::Ranges::iterator
125 LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
126 assert(I != ranges.end() && "Not a valid interval!");
127 unsigned ValId = I->ValId;
129 // Search for the first interval that we can't merge with.
130 Ranges::iterator MergeTo = I;
132 if (MergeTo == ranges.begin()) {
134 ranges.erase(MergeTo, I);
137 assert(MergeTo->ValId == ValId && "Cannot merge with differing values!");
139 } while (NewStart <= MergeTo->start);
141 // If we start in the middle of another interval, just delete a range and
142 // extend that interval.
143 if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) {
144 MergeTo->end = I->end;
146 // Otherwise, extend the interval right after.
148 MergeTo->start = NewStart;
149 MergeTo->end = I->end;
152 ranges.erase(next(MergeTo), next(I));
156 LiveInterval::Ranges::iterator
157 LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
158 unsigned Start = LR.start, End = LR.end;
159 Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
161 // If the inserted interval starts in the middle or right at the end of
162 // another interval, just extend that interval to contain the range of LR.
163 if (it != ranges.begin()) {
164 Ranges::iterator B = prior(it);
165 if (LR.ValId == B->ValId) {
166 if (B->start <= Start && B->end >= Start) {
167 extendIntervalEndTo(B, End);
171 // Check to make sure that we are not overlapping two live ranges with
172 // different ValId's.
173 assert(B->end <= Start &&
174 "Cannot overlap two LiveRanges with differing ValID's");
178 // Otherwise, if this range ends in the middle of, or right next to, another
179 // interval, merge it into that interval.
180 if (it != ranges.end())
181 if (LR.ValId == it->ValId) {
182 if (it->start <= End) {
183 it = extendIntervalStartTo(it, Start);
185 // If LR is a complete superset of an interval, we may need to grow its
188 extendIntervalEndTo(it, End);
192 // Check to make sure that we are not overlapping two live ranges with
193 // different ValId's.
194 assert(it->start >= End &&
195 "Cannot overlap two LiveRanges with differing ValID's");
198 // Otherwise, this is just a new range that doesn't interact with anything.
200 return ranges.insert(it, LR);
204 /// removeRange - Remove the specified range from this interval. Note that
205 /// the range must already be in this interval in its entirety.
206 void LiveInterval::removeRange(unsigned Start, unsigned End) {
207 // Find the LiveRange containing this span.
208 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
209 assert(I != ranges.begin() && "Range is not in interval!");
211 assert(I->contains(Start) && I->contains(End-1) &&
212 "Range is not entirely in interval!");
214 // If the span we are removing is at the start of the LiveRange, adjust it.
215 if (I->start == Start) {
217 ranges.erase(I); // Removed the whole LiveRange.
223 // Otherwise if the span we are removing is at the end of the LiveRange,
224 // adjust the other way.
230 // Otherwise, we are splitting the LiveRange into two pieces.
231 unsigned OldEnd = I->end;
232 I->end = Start; // Trim the old interval.
234 // Insert the new one.
235 ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId));
238 /// getLiveRangeContaining - Return the live range that contains the
239 /// specified index, or null if there is none.
240 LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) {
241 Ranges::iterator It = std::upper_bound(ranges.begin(), ranges.end(), Idx);
242 if (It != ranges.begin()) {
243 LiveRange &LR = *prior(It);
244 if (LR.contains(Idx))
253 /// join - Join two live intervals (this, and other) together. This operation
254 /// is the result of a copy instruction in the source program, that occurs at
255 /// index 'CopyIdx' that copies from 'Other' to 'this'.
256 void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
257 LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
258 LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
259 assert(SourceLR && DestLR && "Not joining due to a copy?");
260 unsigned MergedSrcValIdx = SourceLR->ValId;
261 unsigned MergedDstValIdx = DestLR->ValId;
263 // Try to do the least amount of work possible. In particular, if there are
264 // more liverange chunks in the other set than there are in the 'this' set,
265 // swap sets to merge the fewest chunks in possible.
266 if (Other.ranges.size() > ranges.size()) {
267 std::swap(MergedSrcValIdx, MergedDstValIdx);
268 std::swap(ranges, Other.ranges);
269 std::swap(NumValues, Other.NumValues);
272 // Join the ranges of other into the ranges of this interval.
273 Ranges::iterator InsertPos = ranges.begin();
274 std::map<unsigned, unsigned> Dst2SrcIdxMap;
275 for (Ranges::iterator I = Other.ranges.begin(),
276 E = Other.ranges.end(); I != E; ++I) {
277 // Map the ValId in the other live range to the current live range.
278 if (I->ValId == MergedSrcValIdx)
279 I->ValId = MergedDstValIdx;
281 unsigned &NV = Dst2SrcIdxMap[I->ValId];
282 if (NV == 0) NV = getNextValue();
286 InsertPos = addRangeFrom(*I, InsertPos);
289 weight += Other.weight;
292 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
293 return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
296 void LiveRange::dump() const {
297 std::cerr << *this << "\n";
301 std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
302 os << "%reg" << li.reg << ',' << li.weight;
304 return os << "EMPTY";
307 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
308 e = li.ranges.end(); i != e; ++i)
313 void LiveInterval::dump() const {
314 std::cerr << *this << "\n";