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 | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 104 | /// extendIntervalEndTo - This method is used when we want to extend the range |
| 105 | /// specified by I to end at the specified endpoint. To do this, we should |
| 106 | /// merge and eliminate all ranges that this will overlap with. The iterator is |
| 107 | /// not invalidated. |
| 108 | void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) { |
| 109 | assert(I != ranges.end() && "Not a valid interval!"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 110 | unsigned ValId = I->ValId; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 111 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 112 | // Search for the first interval that we can't merge with. |
| 113 | Ranges::iterator MergeTo = next(I); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 114 | for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) { |
| 115 | assert(MergeTo->ValId == ValId && "Cannot merge with differing values!"); |
| 116 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 117 | |
| 118 | // If NewEnd was in the middle of an interval, make sure to get its endpoint. |
| 119 | I->end = std::max(NewEnd, prior(MergeTo)->end); |
| 120 | |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 121 | // Erase any dead ranges. |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 122 | ranges.erase(next(I), MergeTo); |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 123 | |
| 124 | // If the newly formed range now touches the range after it and if they have |
| 125 | // the same value number, merge the two ranges into one range. |
Chris Lattner | cef6010 | 2005-10-20 22:50:10 +0000 | [diff] [blame] | 126 | Ranges::iterator Next = next(I); |
Chris Lattner | 9e20d35 | 2005-10-21 06:41:30 +0000 | [diff] [blame] | 127 | if (Next != ranges.end() && Next->start <= I->end && Next->ValId == ValId) { |
Chris Lattner | cef6010 | 2005-10-20 22:50:10 +0000 | [diff] [blame] | 128 | I->end = Next->end; |
| 129 | ranges.erase(Next); |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
| 134 | /// extendIntervalStartTo - This method is used when we want to extend the range |
| 135 | /// specified by I to start at the specified endpoint. To do this, we should |
| 136 | /// merge and eliminate all ranges that this will overlap with. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 137 | LiveInterval::Ranges::iterator |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 138 | LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) { |
| 139 | assert(I != ranges.end() && "Not a valid interval!"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 140 | unsigned ValId = I->ValId; |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 141 | |
| 142 | // Search for the first interval that we can't merge with. |
| 143 | Ranges::iterator MergeTo = I; |
| 144 | do { |
| 145 | if (MergeTo == ranges.begin()) { |
| 146 | I->start = NewStart; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 147 | ranges.erase(MergeTo, I); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 148 | return I; |
| 149 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 150 | assert(MergeTo->ValId == ValId && "Cannot merge with differing values!"); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 151 | --MergeTo; |
| 152 | } while (NewStart <= MergeTo->start); |
| 153 | |
| 154 | // If we start in the middle of another interval, just delete a range and |
| 155 | // extend that interval. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 156 | if (MergeTo->end >= NewStart && MergeTo->ValId == ValId) { |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 157 | MergeTo->end = I->end; |
| 158 | } else { |
| 159 | // Otherwise, extend the interval right after. |
| 160 | ++MergeTo; |
| 161 | MergeTo->start = NewStart; |
| 162 | MergeTo->end = I->end; |
| 163 | } |
| 164 | |
| 165 | ranges.erase(next(MergeTo), next(I)); |
| 166 | return MergeTo; |
| 167 | } |
| 168 | |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 169 | LiveInterval::iterator |
| 170 | LiveInterval::addRangeFrom(LiveRange LR, iterator From) { |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 171 | unsigned Start = LR.start, End = LR.end; |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 172 | iterator it = std::upper_bound(From, ranges.end(), Start); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 173 | |
| 174 | // If the inserted interval starts in the middle or right at the end of |
| 175 | // another interval, just extend that interval to contain the range of LR. |
| 176 | if (it != ranges.begin()) { |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 177 | iterator B = prior(it); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 178 | if (LR.ValId == B->ValId) { |
| 179 | if (B->start <= Start && B->end >= Start) { |
| 180 | extendIntervalEndTo(B, End); |
| 181 | return B; |
| 182 | } |
| 183 | } else { |
| 184 | // Check to make sure that we are not overlapping two live ranges with |
| 185 | // different ValId's. |
| 186 | assert(B->end <= Start && |
Brian Gaeke | 8311bef | 2004-11-16 06:52:35 +0000 | [diff] [blame] | 187 | "Cannot overlap two LiveRanges with differing ValID's" |
| 188 | " (did you def the same reg twice in a MachineInstr?)"); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | // Otherwise, if this range ends in the middle of, or right next to, another |
| 193 | // interval, merge it into that interval. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 194 | if (it != ranges.end()) |
| 195 | if (LR.ValId == it->ValId) { |
| 196 | if (it->start <= End) { |
| 197 | it = extendIntervalStartTo(it, Start); |
| 198 | |
| 199 | // If LR is a complete superset of an interval, we may need to grow its |
| 200 | // endpoint as well. |
| 201 | if (End > it->end) |
| 202 | extendIntervalEndTo(it, End); |
| 203 | return it; |
| 204 | } |
| 205 | } else { |
| 206 | // Check to make sure that we are not overlapping two live ranges with |
| 207 | // different ValId's. |
| 208 | assert(it->start >= End && |
| 209 | "Cannot overlap two LiveRanges with differing ValID's"); |
| 210 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 211 | |
| 212 | // Otherwise, this is just a new range that doesn't interact with anything. |
| 213 | // Insert it. |
| 214 | return ranges.insert(it, LR); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 217 | |
| 218 | /// removeRange - Remove the specified range from this interval. Note that |
| 219 | /// the range must already be in this interval in its entirety. |
| 220 | void LiveInterval::removeRange(unsigned Start, unsigned End) { |
| 221 | // Find the LiveRange containing this span. |
| 222 | Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start); |
| 223 | assert(I != ranges.begin() && "Range is not in interval!"); |
| 224 | --I; |
| 225 | assert(I->contains(Start) && I->contains(End-1) && |
| 226 | "Range is not entirely in interval!"); |
| 227 | |
| 228 | // If the span we are removing is at the start of the LiveRange, adjust it. |
| 229 | if (I->start == Start) { |
| 230 | if (I->end == End) |
| 231 | ranges.erase(I); // Removed the whole LiveRange. |
| 232 | else |
| 233 | I->start = End; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // Otherwise if the span we are removing is at the end of the LiveRange, |
| 238 | // adjust the other way. |
| 239 | if (I->end == End) { |
Chris Lattner | 6925a9f | 2004-07-25 05:43:53 +0000 | [diff] [blame] | 240 | I->end = Start; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
| 244 | // Otherwise, we are splitting the LiveRange into two pieces. |
| 245 | unsigned OldEnd = I->end; |
| 246 | I->end = Start; // Trim the old interval. |
| 247 | |
| 248 | // Insert the new one. |
| 249 | ranges.insert(next(I), LiveRange(End, OldEnd, I->ValId)); |
| 250 | } |
| 251 | |
| 252 | /// getLiveRangeContaining - Return the live range that contains the |
| 253 | /// specified index, or null if there is none. |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 254 | LiveInterval::const_iterator |
| 255 | LiveInterval::FindLiveRangeContaining(unsigned Idx) const { |
| 256 | const_iterator It = std::upper_bound(begin(), end(), Idx); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 257 | if (It != ranges.begin()) { |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 258 | --It; |
| 259 | if (It->contains(Idx)) |
| 260 | return It; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 263 | return end(); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 266 | LiveInterval::iterator |
| 267 | LiveInterval::FindLiveRangeContaining(unsigned Idx) { |
| 268 | iterator It = std::upper_bound(begin(), end(), Idx); |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 269 | if (It != begin()) { |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 270 | --It; |
| 271 | if (It->contains(Idx)) |
| 272 | return It; |
| 273 | } |
| 274 | |
| 275 | return end(); |
| 276 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 278 | /// join - Join two live intervals (this, and other) together. This applies |
| 279 | /// mappings to the value numbers in the LHS/RHS intervals as specified. If |
| 280 | /// the intervals are not joinable, this aborts. |
| 281 | void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments, |
| 282 | int *RHSValNoAssignments, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 283 | SmallVector<std::pair<unsigned, |
| 284 | unsigned>, 16> &NewValueNumberInfo) { |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 285 | |
Chris Lattner | deb9971 | 2004-07-24 03:41:50 +0000 | [diff] [blame] | 286 | // Try to do the least amount of work possible. In particular, if there are |
| 287 | // more liverange chunks in the other set than there are in the 'this' set, |
| 288 | // swap sets to merge the fewest chunks in possible. |
Chris Lattner | e7f729b | 2006-08-26 01:28:16 +0000 | [diff] [blame] | 289 | // |
| 290 | // Also, if one range is a physreg and one is a vreg, we always merge from the |
| 291 | // vreg into the physreg, which leaves the vreg intervals pristine. |
| 292 | unsigned OtherOffs = 1, ThisOffs = 0; |
| 293 | if ((Other.ranges.size() > ranges.size() && |
| 294 | MRegisterInfo::isVirtualRegister(reg)) || |
| 295 | MRegisterInfo::isPhysicalRegister(Other.reg)) { |
| 296 | swap(Other); |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 297 | std::swap(LHSValNoAssignments, RHSValNoAssignments); |
| 298 | } |
| 299 | |
| 300 | // Determine if any of our live range values are mapped. This is uncommon, so |
| 301 | // we want to avoid the interval scan if not. |
| 302 | bool MustMapCurValNos = false; |
| 303 | for (unsigned i = 0, e = getNumValNums(); i != e; ++i) { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 304 | if (ValueNumberInfo[i].first == ~2U) continue; // tombstone value # |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 305 | if (i != (unsigned)LHSValNoAssignments[i]) { |
| 306 | MustMapCurValNos = true; |
| 307 | break; |
| 308 | } |
Chris Lattner | deb9971 | 2004-07-24 03:41:50 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | e7f729b | 2006-08-26 01:28:16 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 311 | // If we have to apply a mapping to our base interval assignment, rewrite it |
| 312 | // now. |
| 313 | if (MustMapCurValNos) { |
| 314 | // Map the first live range. |
| 315 | iterator OutIt = begin(); |
| 316 | OutIt->ValId = LHSValNoAssignments[OutIt->ValId]; |
| 317 | ++OutIt; |
| 318 | for (iterator I = OutIt, E = end(); I != E; ++I) { |
| 319 | OutIt->ValId = LHSValNoAssignments[I->ValId]; |
| 320 | |
| 321 | // If this live range has the same value # as its immediate predecessor, |
| 322 | // and if they are neighbors, remove one LiveRange. This happens when we |
| 323 | // have [0,3:0)[4,7:1) and map 0/1 onto the same value #. |
| 324 | if (OutIt->ValId == (OutIt-1)->ValId && (OutIt-1)->end == OutIt->start) { |
| 325 | (OutIt-1)->end = OutIt->end; |
| 326 | } else { |
| 327 | if (I != OutIt) { |
| 328 | OutIt->start = I->start; |
| 329 | OutIt->end = I->end; |
| 330 | } |
| 331 | |
| 332 | // Didn't merge, on to the next one. |
| 333 | ++OutIt; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // If we merge some live ranges, chop off the end. |
| 338 | ranges.erase(OutIt, end()); |
| 339 | } |
| 340 | |
| 341 | // Okay, now insert the RHS live ranges into the LHS. |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 342 | iterator InsertPos = begin(); |
| 343 | for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) { |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 344 | // Map the ValId in the other live range to the current live range. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 345 | I->ValId = RHSValNoAssignments[I->ValId]; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 346 | InsertPos = addRangeFrom(*I, InsertPos); |
| 347 | } |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 349 | ValueNumberInfo.clear(); |
| 350 | ValueNumberInfo.append(NewValueNumberInfo.begin(), NewValueNumberInfo.end()); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 351 | weight += Other.weight; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 354 | /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live |
| 355 | /// interval as the specified value number. The LiveRanges in RHS are |
| 356 | /// allowed to overlap with LiveRanges in the current interval, but only if |
| 357 | /// the overlapping LiveRanges have the specified value number. |
| 358 | void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, |
| 359 | unsigned LHSValNo) { |
| 360 | // TODO: Make this more efficient. |
| 361 | iterator InsertPos = begin(); |
| 362 | for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { |
| 363 | // Map the ValId in the other live range to the current live range. |
| 364 | LiveRange Tmp = *I; |
| 365 | Tmp.ValId = LHSValNo; |
| 366 | InsertPos = addRangeFrom(Tmp, InsertPos); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 371 | /// MergeInClobberRanges - For any live ranges that are not defined in the |
| 372 | /// current interval, but are defined in the Clobbers interval, mark them |
| 373 | /// used with an unknown definition value. |
| 374 | void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers) { |
| 375 | if (Clobbers.begin() == Clobbers.end()) return; |
| 376 | |
| 377 | // Find a value # to use for the clobber ranges. If there is already a value# |
| 378 | // for unknown values, use it. |
| 379 | // FIXME: Use a single sentinal number for these! |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 380 | unsigned ClobberValNo = getNextValue(~0U, 0); |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 381 | |
| 382 | iterator IP = begin(); |
| 383 | for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) { |
| 384 | unsigned Start = I->start, End = I->end; |
| 385 | IP = std::upper_bound(IP, end(), Start); |
| 386 | |
| 387 | // If the start of this range overlaps with an existing liverange, trim it. |
| 388 | if (IP != begin() && IP[-1].end > Start) { |
| 389 | Start = IP[-1].end; |
| 390 | // Trimmed away the whole range? |
| 391 | if (Start >= End) continue; |
| 392 | } |
| 393 | // If the end of this range overlaps with an existing liverange, trim it. |
| 394 | if (IP != end() && End > IP->start) { |
| 395 | End = IP->start; |
| 396 | // If this trimmed away the whole range, ignore it. |
| 397 | if (Start == End) continue; |
| 398 | } |
| 399 | |
| 400 | // Insert the clobber interval. |
| 401 | IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP); |
| 402 | } |
| 403 | } |
| 404 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 405 | /// MergeValueNumberInto - This method is called when two value nubmers |
| 406 | /// are found to be equivalent. This eliminates V1, replacing all |
| 407 | /// LiveRanges with the V1 value number with the V2 value number. This can |
| 408 | /// cause merging of V1/V2 values numbers and compaction of the value space. |
| 409 | void LiveInterval::MergeValueNumberInto(unsigned V1, unsigned V2) { |
| 410 | assert(V1 != V2 && "Identical value#'s are always equivalent!"); |
| 411 | |
| 412 | // This code actually merges the (numerically) larger value number into the |
| 413 | // smaller value number, which is likely to allow us to compactify the value |
| 414 | // space. The only thing we have to be careful of is to preserve the |
| 415 | // instruction that defines the result value. |
| 416 | |
| 417 | // Make sure V2 is smaller than V1. |
| 418 | if (V1 < V2) { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 419 | setValueNumberInfo(V1, getValNumInfo(V2)); |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 420 | std::swap(V1, V2); |
| 421 | } |
| 422 | |
| 423 | // Merge V1 live ranges into V2. |
| 424 | for (iterator I = begin(); I != end(); ) { |
| 425 | iterator LR = I++; |
| 426 | if (LR->ValId != V1) continue; // Not a V1 LiveRange. |
| 427 | |
| 428 | // Okay, we found a V1 live range. If it had a previous, touching, V2 live |
| 429 | // range, extend it. |
| 430 | if (LR != begin()) { |
| 431 | iterator Prev = LR-1; |
| 432 | if (Prev->ValId == V2 && Prev->end == LR->start) { |
| 433 | Prev->end = LR->end; |
| 434 | |
| 435 | // Erase this live-range. |
| 436 | ranges.erase(LR); |
| 437 | I = Prev+1; |
| 438 | LR = Prev; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // Okay, now we have a V1 or V2 live range that is maximally merged forward. |
| 443 | // Ensure that it is a V2 live-range. |
| 444 | LR->ValId = V2; |
| 445 | |
| 446 | // If we can merge it into later V2 live ranges, do so now. We ignore any |
| 447 | // following V1 live ranges, as they will be merged in subsequent iterations |
| 448 | // of the loop. |
| 449 | if (I != end()) { |
| 450 | if (I->start == LR->end && I->ValId == V2) { |
| 451 | LR->end = I->end; |
| 452 | ranges.erase(I); |
| 453 | I = LR+1; |
| 454 | } |
| 455 | } |
| 456 | } |
Chris Lattner | c82b3aa | 2006-08-24 23:22:59 +0000 | [diff] [blame] | 457 | |
| 458 | // Now that V1 is dead, remove it. If it is the largest value number, just |
| 459 | // nuke it (and any other deleted values neighboring it), otherwise mark it as |
| 460 | // ~1U so it can be nuked later. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 461 | if (V1 == getNumValNums()-1) { |
Chris Lattner | c82b3aa | 2006-08-24 23:22:59 +0000 | [diff] [blame] | 462 | do { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 463 | ValueNumberInfo.pop_back(); |
| 464 | } while (ValueNumberInfo.back().first == ~1U); |
Chris Lattner | c82b3aa | 2006-08-24 23:22:59 +0000 | [diff] [blame] | 465 | } else { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 466 | ValueNumberInfo[V1].first = ~1U; |
Chris Lattner | c82b3aa | 2006-08-24 23:22:59 +0000 | [diff] [blame] | 467 | } |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 471 | std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) { |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 472 | return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")"; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 475 | void LiveRange::dump() const { |
| 476 | std::cerr << *this << "\n"; |
| 477 | } |
| 478 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 479 | void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const { |
| 480 | if (MRI && MRegisterInfo::isPhysicalRegister(reg)) |
| 481 | OS << MRI->getName(reg); |
| 482 | else |
| 483 | OS << "%reg" << reg; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 485 | OS << ',' << weight; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 487 | if (empty()) |
| 488 | OS << "EMPTY"; |
| 489 | else { |
| 490 | OS << " = "; |
| 491 | for (LiveInterval::Ranges::const_iterator I = ranges.begin(), |
| 492 | E = ranges.end(); I != E; ++I) |
| 493 | OS << *I; |
| 494 | } |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 495 | |
| 496 | // Print value number info. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 497 | if (getNumValNums()) { |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 498 | OS << " "; |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 499 | for (unsigned i = 0; i != getNumValNums(); ++i) { |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 500 | if (i) OS << " "; |
| 501 | OS << i << "@"; |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 502 | if (ValueNumberInfo[i].first == ~0U) { |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 503 | OS << "?"; |
| 504 | } else { |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 505 | OS << ValueNumberInfo[i].first; |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 509 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 510 | |
| 511 | void LiveInterval::dump() const { |
| 512 | std::cerr << *this << "\n"; |
| 513 | } |