Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 1 | //===-- LiveInterval.cpp - Live Interval Representation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LiveRange and LiveInterval classes. Given some |
| 11 | // numbering of each the machine instructions an interval [i, j) is said to be a |
| 12 | // live interval for register v if there is no instruction with number j' > j |
| 13 | // such that v is live at j' abd there is no instruction with number i' < i such |
| 14 | // that v is live at i'. In this implementation intervals can have holes, |
| 15 | // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each |
| 16 | // individual range is represented as an instance of LiveRange, and the whole |
| 17 | // interval is represented as an instance of LiveInterval. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | 3c3fe46 | 2005-09-21 04:19:09 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveInterval.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 23 | #include "llvm/Target/MRegisterInfo.h" |
Alkis Evlogimenos | c4d3b91 | 2004-09-28 02:38:58 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 25 | #include <iostream> |
| 26 | #include <map> |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | // An example for liveAt(): |
| 30 | // |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 31 | // this = [1,4), liveAt(0) will return false. The instruction defining this |
| 32 | // spans slots [0,3]. The interval belongs to an spilled definition of the |
| 33 | // variable it represents. This is because slot 1 is used (def slot) and spans |
| 34 | // up to slot 3 (store slot). |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 35 | // |
Chris Lattner | ebd7e6c | 2004-07-23 18:13:24 +0000 | [diff] [blame] | 36 | bool LiveInterval::liveAt(unsigned I) const { |
| 37 | Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 38 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 39 | if (r == ranges.begin()) |
| 40 | return false; |
| 41 | |
| 42 | --r; |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 43 | return r->contains(I); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | bae74d9 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 46 | // overlaps - Return true if the intersection of the two live intervals is |
| 47 | // not empty. |
| 48 | // |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 49 | // An example for overlaps(): |
| 50 | // |
| 51 | // 0: A = ... |
| 52 | // 4: B = ... |
| 53 | // 8: C = A + B ;; last use of A |
| 54 | // |
| 55 | // The live intervals should look like: |
| 56 | // |
| 57 | // A = [3, 11) |
| 58 | // B = [7, x) |
| 59 | // C = [11, y) |
| 60 | // |
| 61 | // A->overlaps(C) should return false since we want to be able to join |
| 62 | // A and C. |
Chris Lattner | bae74d9 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 63 | // |
| 64 | bool LiveInterval::overlapsFrom(const LiveInterval& other, |
| 65 | const_iterator StartPos) const { |
| 66 | const_iterator i = begin(); |
| 67 | const_iterator ie = end(); |
| 68 | const_iterator j = StartPos; |
| 69 | const_iterator je = other.end(); |
| 70 | |
| 71 | assert((StartPos->start <= i->start || StartPos == other.begin()) && |
Chris Lattner | 8c68b6a | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 72 | StartPos != other.end() && "Bogus start position hint!"); |
Chris Lattner | f542649 | 2004-07-25 07:11:19 +0000 | [diff] [blame] | 73 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 74 | if (i->start < j->start) { |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 75 | i = std::upper_bound(i, ie, j->start); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 76 | if (i != ranges.begin()) --i; |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 77 | } else if (j->start < i->start) { |
Chris Lattner | ead1b3f | 2004-12-04 01:22:09 +0000 | [diff] [blame] | 78 | ++StartPos; |
| 79 | if (StartPos != other.end() && StartPos->start <= i->start) { |
| 80 | assert(StartPos < other.end() && i < end()); |
Chris Lattner | 8c68b6a | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 81 | j = std::upper_bound(j, je, i->start); |
| 82 | if (j != other.ranges.begin()) --j; |
| 83 | } |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 84 | } else { |
| 85 | return true; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Chris Lattner | 9fddc12 | 2004-11-18 05:28:21 +0000 | [diff] [blame] | 88 | if (j == je) return false; |
| 89 | |
| 90 | while (i != ie) { |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 91 | if (i->start > j->start) { |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 92 | std::swap(i, j); |
| 93 | std::swap(ie, je); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 95 | |
| 96 | if (i->end > j->start) |
| 97 | return true; |
| 98 | ++i; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 104 | /// NontrivialOverlap - Check to see if the two live ranges specified by i and j |
| 105 | /// overlap. If so, check to see if they have value numbers that are not |
| 106 | /// iIdx/jIdx respectively. If both conditions are true, return true. |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 107 | static inline bool NontrivialOverlap(const LiveRange &I, const LiveRange &J, |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 108 | unsigned iIdx, unsigned jIdx) { |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 109 | if (I.start == J.start) { |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 110 | // If this is not the allowed value merge, we cannot join. |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 111 | if (I.ValId != iIdx || J.ValId != jIdx) |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 112 | return true; |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 113 | } else if (I.start < J.start) { |
Chris Lattner | 8317e12 | 2005-10-20 16:56:40 +0000 | [diff] [blame] | 114 | if (I.end > J.start && (I.ValId != iIdx || J.ValId != jIdx)) { |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | } else { |
Chris Lattner | 8317e12 | 2005-10-20 16:56:40 +0000 | [diff] [blame] | 118 | if (J.end > I.start && (I.ValId != iIdx || J.ValId != jIdx)) |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 125 | /// joinable - Two intervals are joinable if the either don't overlap at all |
| 126 | /// or if the destination of the copy is a single assignment value, and it |
| 127 | /// only overlaps with one value in the source interval. |
| 128 | bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const { |
Chris Lattner | f542649 | 2004-07-25 07:11:19 +0000 | [diff] [blame] | 129 | const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1); |
| 130 | const LiveRange *DestLR = getLiveRangeContaining(CopyIdx); |
| 131 | assert(SourceLR && DestLR && "Not joining due to a copy?"); |
| 132 | unsigned OtherValIdx = SourceLR->ValId; |
| 133 | unsigned ThisValIdx = DestLR->ValId; |
| 134 | |
| 135 | Ranges::const_iterator i = ranges.begin(); |
| 136 | Ranges::const_iterator ie = ranges.end(); |
| 137 | Ranges::const_iterator j = other.ranges.begin(); |
| 138 | Ranges::const_iterator je = other.ranges.end(); |
| 139 | |
| 140 | if (i->start < j->start) { |
| 141 | i = std::upper_bound(i, ie, j->start); |
| 142 | if (i != ranges.begin()) --i; |
| 143 | } else if (j->start < i->start) { |
| 144 | j = std::upper_bound(j, je, i->start); |
| 145 | if (j != other.ranges.begin()) --j; |
| 146 | } |
| 147 | |
| 148 | while (i != ie && j != je) { |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 149 | if (NontrivialOverlap(*i, *j, ThisValIdx, OtherValIdx)) |
Chris Lattner | f5ce267 | 2005-10-20 06:06:30 +0000 | [diff] [blame] | 150 | return false; |
| 151 | |
Chris Lattner | f542649 | 2004-07-25 07:11:19 +0000 | [diff] [blame] | 152 | if (i->end < j->end) |
| 153 | ++i; |
| 154 | else |
| 155 | ++j; |
| 156 | } |
| 157 | |
Chris Lattner | c25b55a | 2004-07-25 07:47:25 +0000 | [diff] [blame] | 158 | return true; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 161 | /// getOverlapingRanges - Given another live interval which is defined as a |
| 162 | /// copy from this one, return a list of all of the live ranges where the |
| 163 | /// two overlap and have different value numbers. |
| 164 | void LiveInterval::getOverlapingRanges(const LiveInterval &other, |
| 165 | unsigned CopyIdx, |
| 166 | std::vector<LiveRange*> &Ranges) { |
Chris Lattner | 9e20d35 | 2005-10-21 06:41:30 +0000 | [diff] [blame^] | 167 | const LiveRange *SourceLR = getLiveRangeContaining(CopyIdx-1); |
| 168 | const LiveRange *DestLR = other.getLiveRangeContaining(CopyIdx); |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 169 | assert(SourceLR && DestLR && "Not joining due to a copy?"); |
| 170 | unsigned OtherValIdx = SourceLR->ValId; |
| 171 | unsigned ThisValIdx = DestLR->ValId; |
| 172 | |
| 173 | Ranges::iterator i = ranges.begin(); |
| 174 | Ranges::iterator ie = ranges.end(); |
| 175 | Ranges::const_iterator j = other.ranges.begin(); |
| 176 | Ranges::const_iterator je = other.ranges.end(); |
| 177 | |
| 178 | if (i->start < j->start) { |
| 179 | i = std::upper_bound(i, ie, j->start); |
| 180 | if (i != ranges.begin()) --i; |
| 181 | } else if (j->start < i->start) { |
| 182 | j = std::upper_bound(j, je, i->start); |
| 183 | if (j != other.ranges.begin()) --j; |
| 184 | } |
| 185 | |
| 186 | while (i != ie && j != je) { |
| 187 | if (NontrivialOverlap(*i, *j, ThisValIdx, OtherValIdx)) |
| 188 | Ranges.push_back(&*i); |
| 189 | |
| 190 | if (i->end < j->end) |
| 191 | ++i; |
| 192 | else |
| 193 | ++j; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 198 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 199 | /// extendIntervalEndTo - This method is used when we want to extend the range |
| 200 | /// specified by I to end at the specified endpoint. To do this, we should |
| 201 | /// merge and eliminate all ranges that this will overlap with. The iterator is |
| 202 | /// not invalidated. |
| 203 | void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) { |
| 204 | assert(I != ranges.end() && "Not a valid interval!"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 205 | unsigned ValId = I->ValId; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 206 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 207 | // Search for the first interval that we can't merge with. |
| 208 | Ranges::iterator MergeTo = next(I); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 209 | for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) { |
| 210 | assert(MergeTo->ValId == ValId && "Cannot merge with differing values!"); |
| 211 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 212 | |
| 213 | // If NewEnd was in the middle of an interval, make sure to get its endpoint. |
| 214 | I->end = std::max(NewEnd, prior(MergeTo)->end); |
| 215 | |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 216 | // Erase any dead ranges. |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 217 | ranges.erase(next(I), MergeTo); |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 218 | |
| 219 | // If the newly formed range now touches the range after it and if they have |
| 220 | // the same value number, merge the two ranges into one range. |
Chris Lattner | cef6010 | 2005-10-20 22:50:10 +0000 | [diff] [blame] | 221 | Ranges::iterator Next = next(I); |
Chris Lattner | 9e20d35 | 2005-10-21 06:41:30 +0000 | [diff] [blame^] | 222 | if (Next != ranges.end() && Next->start <= I->end && Next->ValId == ValId) { |
Chris Lattner | cef6010 | 2005-10-20 22:50:10 +0000 | [diff] [blame] | 223 | I->end = Next->end; |
| 224 | ranges.erase(Next); |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | /// extendIntervalStartTo - This method is used when we want to extend the range |
| 230 | /// specified by I to start at the specified endpoint. To do this, we should |
| 231 | /// merge and eliminate all ranges that this will overlap with. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 232 | LiveInterval::Ranges::iterator |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 233 | LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) { |
| 234 | assert(I != ranges.end() && "Not a valid interval!"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 235 | unsigned ValId = I->ValId; |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 236 | |
| 237 | // Search for the first interval that we can't merge with. |
| 238 | Ranges::iterator MergeTo = I; |
| 239 | do { |
| 240 | if (MergeTo == ranges.begin()) { |
| 241 | I->start = NewStart; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 242 | ranges.erase(MergeTo, I); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 243 | return I; |
| 244 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 245 | assert(MergeTo->ValId == ValId && "Cannot merge with differing values!"); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 246 | --MergeTo; |
| 247 | } while (NewStart <= MergeTo->start); |
| 248 | |
| 249 | // If we start in the middle of another interval, just delete a range and |
| 250 | // extend that interval. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 251 | if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) { |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 252 | MergeTo->end = I->end; |
| 253 | } else { |
| 254 | // Otherwise, extend the interval right after. |
| 255 | ++MergeTo; |
| 256 | MergeTo->start = NewStart; |
| 257 | MergeTo->end = I->end; |
| 258 | } |
| 259 | |
| 260 | ranges.erase(next(MergeTo), next(I)); |
| 261 | return MergeTo; |
| 262 | } |
| 263 | |
| 264 | LiveInterval::Ranges::iterator |
| 265 | LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) { |
| 266 | unsigned Start = LR.start, End = LR.end; |
| 267 | Ranges::iterator it = std::upper_bound(From, ranges.end(), Start); |
| 268 | |
| 269 | // If the inserted interval starts in the middle or right at the end of |
| 270 | // another interval, just extend that interval to contain the range of LR. |
| 271 | if (it != ranges.begin()) { |
| 272 | Ranges::iterator B = prior(it); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 273 | if (LR.ValId == B->ValId) { |
| 274 | if (B->start <= Start && B->end >= Start) { |
| 275 | extendIntervalEndTo(B, End); |
| 276 | return B; |
| 277 | } |
| 278 | } else { |
| 279 | // Check to make sure that we are not overlapping two live ranges with |
| 280 | // different ValId's. |
| 281 | assert(B->end <= Start && |
Brian Gaeke | 8311bef | 2004-11-16 06:52:35 +0000 | [diff] [blame] | 282 | "Cannot overlap two LiveRanges with differing ValID's" |
| 283 | " (did you def the same reg twice in a MachineInstr?)"); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | // Otherwise, if this range ends in the middle of, or right next to, another |
| 288 | // interval, merge it into that interval. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 289 | if (it != ranges.end()) |
| 290 | if (LR.ValId == it->ValId) { |
| 291 | if (it->start <= End) { |
| 292 | it = extendIntervalStartTo(it, Start); |
| 293 | |
| 294 | // If LR is a complete superset of an interval, we may need to grow its |
| 295 | // endpoint as well. |
| 296 | if (End > it->end) |
| 297 | extendIntervalEndTo(it, End); |
| 298 | return it; |
| 299 | } |
| 300 | } else { |
| 301 | // Check to make sure that we are not overlapping two live ranges with |
| 302 | // different ValId's. |
| 303 | assert(it->start >= End && |
| 304 | "Cannot overlap two LiveRanges with differing ValID's"); |
| 305 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 306 | |
| 307 | // Otherwise, this is just a new range that doesn't interact with anything. |
| 308 | // Insert it. |
| 309 | return ranges.insert(it, LR); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 312 | |
| 313 | /// removeRange - Remove the specified range from this interval. Note that |
| 314 | /// the range must already be in this interval in its entirety. |
| 315 | void LiveInterval::removeRange(unsigned Start, unsigned End) { |
| 316 | // Find the LiveRange containing this span. |
| 317 | Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start); |
| 318 | assert(I != ranges.begin() && "Range is not in interval!"); |
| 319 | --I; |
| 320 | assert(I->contains(Start) && I->contains(End-1) && |
| 321 | "Range is not entirely in interval!"); |
| 322 | |
| 323 | // If the span we are removing is at the start of the LiveRange, adjust it. |
| 324 | if (I->start == Start) { |
| 325 | if (I->end == End) |
| 326 | ranges.erase(I); // Removed the whole LiveRange. |
| 327 | else |
| 328 | I->start = End; |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | // Otherwise if the span we are removing is at the end of the LiveRange, |
| 333 | // adjust the other way. |
| 334 | if (I->end == End) { |
Chris Lattner | 6925a9f | 2004-07-25 05:43:53 +0000 | [diff] [blame] | 335 | I->end = Start; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
| 339 | // Otherwise, we are splitting the LiveRange into two pieces. |
| 340 | unsigned OldEnd = I->end; |
| 341 | I->end = Start; // Trim the old interval. |
| 342 | |
| 343 | // Insert the new one. |
| 344 | ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId)); |
| 345 | } |
| 346 | |
| 347 | /// getLiveRangeContaining - Return the live range that contains the |
| 348 | /// specified index, or null if there is none. |
Chris Lattner | d3a205e | 2004-07-25 06:23:01 +0000 | [diff] [blame] | 349 | const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const { |
| 350 | Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 351 | if (It != ranges.begin()) { |
Chris Lattner | d3a205e | 2004-07-25 06:23:01 +0000 | [diff] [blame] | 352 | const LiveRange &LR = *prior(It); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 353 | if (LR.contains(Idx)) |
| 354 | return &LR; |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | |
| 362 | /// join - Join two live intervals (this, and other) together. This operation |
| 363 | /// is the result of a copy instruction in the source program, that occurs at |
| 364 | /// index 'CopyIdx' that copies from 'Other' to 'this'. |
| 365 | void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) { |
Chris Lattner | d3a205e | 2004-07-25 06:23:01 +0000 | [diff] [blame] | 366 | const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1); |
| 367 | const LiveRange *DestLR = getLiveRangeContaining(CopyIdx); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 368 | assert(SourceLR && DestLR && "Not joining due to a copy?"); |
| 369 | unsigned MergedSrcValIdx = SourceLR->ValId; |
| 370 | unsigned MergedDstValIdx = DestLR->ValId; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 371 | |
Chris Lattner | deb9971 | 2004-07-24 03:41:50 +0000 | [diff] [blame] | 372 | // Try to do the least amount of work possible. In particular, if there are |
| 373 | // more liverange chunks in the other set than there are in the 'this' set, |
| 374 | // swap sets to merge the fewest chunks in possible. |
| 375 | if (Other.ranges.size() > ranges.size()) { |
| 376 | std::swap(MergedSrcValIdx, MergedDstValIdx); |
| 377 | std::swap(ranges, Other.ranges); |
| 378 | std::swap(NumValues, Other.NumValues); |
| 379 | } |
| 380 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 381 | // Join the ranges of other into the ranges of this interval. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 382 | Ranges::iterator InsertPos = ranges.begin(); |
| 383 | std::map<unsigned, unsigned> Dst2SrcIdxMap; |
| 384 | for (Ranges::iterator I = Other.ranges.begin(), |
| 385 | E = Other.ranges.end(); I != E; ++I) { |
| 386 | // Map the ValId in the other live range to the current live range. |
| 387 | if (I->ValId == MergedSrcValIdx) |
| 388 | I->ValId = MergedDstValIdx; |
| 389 | else { |
| 390 | unsigned &NV = Dst2SrcIdxMap[I->ValId]; |
| 391 | if (NV == 0) NV = getNextValue(); |
| 392 | I->ValId = NV; |
| 393 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 394 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 395 | InsertPos = addRangeFrom(*I, InsertPos); |
| 396 | } |
| 397 | |
| 398 | weight += Other.weight; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 401 | std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) { |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 402 | return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")"; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 405 | void LiveRange::dump() const { |
| 406 | std::cerr << *this << "\n"; |
| 407 | } |
| 408 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 409 | void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const { |
| 410 | if (MRI && MRegisterInfo::isPhysicalRegister(reg)) |
| 411 | OS << MRI->getName(reg); |
| 412 | else |
| 413 | OS << "%reg" << reg; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 415 | OS << ',' << weight; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 417 | if (empty()) |
| 418 | OS << "EMPTY"; |
| 419 | else { |
| 420 | OS << " = "; |
| 421 | for (LiveInterval::Ranges::const_iterator I = ranges.begin(), |
| 422 | E = ranges.end(); I != E; ++I) |
| 423 | OS << *I; |
| 424 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 426 | |
| 427 | void LiveInterval::dump() const { |
| 428 | std::cerr << *this << "\n"; |
| 429 | } |