Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- LiveInterval.cpp - Live Interval Representation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
| 21 | #include "llvm/CodeGen/LiveInterval.h" |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
| 24 | #include "llvm/Support/Streams.h" |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | #include <ostream> |
| 28 | using namespace llvm; |
| 29 | |
| 30 | // An example for liveAt(): |
| 31 | // |
| 32 | // this = [1,4), liveAt(0) will return false. The instruction defining this |
| 33 | // spans slots [0,3]. The interval belongs to an spilled definition of the |
| 34 | // variable it represents. This is because slot 1 is used (def slot) and spans |
| 35 | // up to slot 3 (store slot). |
| 36 | // |
| 37 | bool LiveInterval::liveAt(unsigned I) const { |
| 38 | Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I); |
| 39 | |
| 40 | if (r == ranges.begin()) |
| 41 | return false; |
| 42 | |
| 43 | --r; |
| 44 | return r->contains(I); |
| 45 | } |
| 46 | |
| 47 | // overlaps - Return true if the intersection of the two live intervals is |
| 48 | // not empty. |
| 49 | // |
| 50 | // An example for overlaps(): |
| 51 | // |
| 52 | // 0: A = ... |
| 53 | // 4: B = ... |
| 54 | // 8: C = A + B ;; last use of A |
| 55 | // |
| 56 | // The live intervals should look like: |
| 57 | // |
| 58 | // A = [3, 11) |
| 59 | // B = [7, x) |
| 60 | // C = [11, y) |
| 61 | // |
| 62 | // A->overlaps(C) should return false since we want to be able to join |
| 63 | // A and C. |
| 64 | // |
| 65 | bool LiveInterval::overlapsFrom(const LiveInterval& other, |
| 66 | const_iterator StartPos) const { |
| 67 | const_iterator i = begin(); |
| 68 | const_iterator ie = end(); |
| 69 | const_iterator j = StartPos; |
| 70 | const_iterator je = other.end(); |
| 71 | |
| 72 | assert((StartPos->start <= i->start || StartPos == other.begin()) && |
| 73 | StartPos != other.end() && "Bogus start position hint!"); |
| 74 | |
| 75 | if (i->start < j->start) { |
| 76 | i = std::upper_bound(i, ie, j->start); |
| 77 | if (i != ranges.begin()) --i; |
| 78 | } else if (j->start < i->start) { |
| 79 | ++StartPos; |
| 80 | if (StartPos != other.end() && StartPos->start <= i->start) { |
| 81 | assert(StartPos < other.end() && i < end()); |
| 82 | j = std::upper_bound(j, je, i->start); |
| 83 | if (j != other.ranges.begin()) --j; |
| 84 | } |
| 85 | } else { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | if (j == je) return false; |
| 90 | |
| 91 | while (i != ie) { |
| 92 | if (i->start > j->start) { |
| 93 | std::swap(i, j); |
| 94 | std::swap(ie, je); |
| 95 | } |
| 96 | |
| 97 | if (i->end > j->start) |
| 98 | return true; |
| 99 | ++i; |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /// extendIntervalEndTo - This method is used when we want to extend the range |
| 106 | /// specified by I to end at the specified endpoint. To do this, we should |
| 107 | /// merge and eliminate all ranges that this will overlap with. The iterator is |
| 108 | /// not invalidated. |
| 109 | void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) { |
| 110 | assert(I != ranges.end() && "Not a valid interval!"); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 111 | VNInfo *ValNo = I->valno; |
Evan Cheng | 2d88a7b | 2007-08-14 01:56:58 +0000 | [diff] [blame] | 112 | unsigned OldEnd = I->end; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | |
| 114 | // Search for the first interval that we can't merge with. |
| 115 | Ranges::iterator MergeTo = next(I); |
| 116 | for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 117 | assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // If NewEnd was in the middle of an interval, make sure to get its endpoint. |
| 121 | I->end = std::max(NewEnd, prior(MergeTo)->end); |
| 122 | |
| 123 | // Erase any dead ranges. |
| 124 | ranges.erase(next(I), MergeTo); |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 125 | |
| 126 | // Update kill info. |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 127 | removeKills(ValNo, OldEnd, I->end-1); |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 128 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | // If the newly formed range now touches the range after it and if they have |
| 130 | // the same value number, merge the two ranges into one range. |
| 131 | Ranges::iterator Next = next(I); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 132 | if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 133 | I->end = Next->end; |
| 134 | ranges.erase(Next); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /// extendIntervalStartTo - This method is used when we want to extend the range |
| 140 | /// specified by I to start at the specified endpoint. To do this, we should |
| 141 | /// merge and eliminate all ranges that this will overlap with. |
| 142 | LiveInterval::Ranges::iterator |
| 143 | LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) { |
| 144 | assert(I != ranges.end() && "Not a valid interval!"); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 145 | VNInfo *ValNo = I->valno; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 146 | |
| 147 | // Search for the first interval that we can't merge with. |
| 148 | Ranges::iterator MergeTo = I; |
| 149 | do { |
| 150 | if (MergeTo == ranges.begin()) { |
| 151 | I->start = NewStart; |
| 152 | ranges.erase(MergeTo, I); |
| 153 | return I; |
| 154 | } |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 155 | assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 156 | --MergeTo; |
| 157 | } while (NewStart <= MergeTo->start); |
| 158 | |
| 159 | // If we start in the middle of another interval, just delete a range and |
| 160 | // extend that interval. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 161 | if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | MergeTo->end = I->end; |
| 163 | } else { |
| 164 | // Otherwise, extend the interval right after. |
| 165 | ++MergeTo; |
| 166 | MergeTo->start = NewStart; |
| 167 | MergeTo->end = I->end; |
| 168 | } |
| 169 | |
| 170 | ranges.erase(next(MergeTo), next(I)); |
| 171 | return MergeTo; |
| 172 | } |
| 173 | |
| 174 | LiveInterval::iterator |
| 175 | LiveInterval::addRangeFrom(LiveRange LR, iterator From) { |
| 176 | unsigned Start = LR.start, End = LR.end; |
| 177 | iterator it = std::upper_bound(From, ranges.end(), Start); |
| 178 | |
| 179 | // If the inserted interval starts in the middle or right at the end of |
| 180 | // another interval, just extend that interval to contain the range of LR. |
| 181 | if (it != ranges.begin()) { |
| 182 | iterator B = prior(it); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 183 | if (LR.valno == B->valno) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 184 | if (B->start <= Start && B->end >= Start) { |
| 185 | extendIntervalEndTo(B, End); |
| 186 | return B; |
| 187 | } |
| 188 | } else { |
| 189 | // Check to make sure that we are not overlapping two live ranges with |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 190 | // different valno's. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | assert(B->end <= Start && |
| 192 | "Cannot overlap two LiveRanges with differing ValID's" |
| 193 | " (did you def the same reg twice in a MachineInstr?)"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Otherwise, if this range ends in the middle of, or right next to, another |
| 198 | // interval, merge it into that interval. |
| 199 | if (it != ranges.end()) |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 200 | if (LR.valno == it->valno) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | if (it->start <= End) { |
| 202 | it = extendIntervalStartTo(it, Start); |
| 203 | |
| 204 | // If LR is a complete superset of an interval, we may need to grow its |
| 205 | // endpoint as well. |
| 206 | if (End > it->end) |
| 207 | extendIntervalEndTo(it, End); |
Evan Cheng | 8b70e63 | 2007-11-29 09:49:23 +0000 | [diff] [blame] | 208 | else if (End < it->end) |
Evan Cheng | 1f45815 | 2007-11-29 01:05:47 +0000 | [diff] [blame] | 209 | // Overlapping intervals, there might have been a kill here. |
| 210 | removeKill(it->valno, End); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 211 | return it; |
| 212 | } |
| 213 | } else { |
| 214 | // Check to make sure that we are not overlapping two live ranges with |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 215 | // different valno's. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 216 | assert(it->start >= End && |
| 217 | "Cannot overlap two LiveRanges with differing ValID's"); |
| 218 | } |
| 219 | |
| 220 | // Otherwise, this is just a new range that doesn't interact with anything. |
| 221 | // Insert it. |
| 222 | return ranges.insert(it, LR); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /// removeRange - Remove the specified range from this interval. Note that |
| 227 | /// the range must already be in this interval in its entirety. |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 228 | void LiveInterval::removeRange(unsigned Start, unsigned End, |
| 229 | bool RemoveDeadValNo) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 230 | // Find the LiveRange containing this span. |
| 231 | Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start); |
| 232 | assert(I != ranges.begin() && "Range is not in interval!"); |
| 233 | --I; |
| 234 | assert(I->contains(Start) && I->contains(End-1) && |
| 235 | "Range is not entirely in interval!"); |
| 236 | |
| 237 | // If the span we are removing is at the start of the LiveRange, adjust it. |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 238 | VNInfo *ValNo = I->valno; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | if (I->start == Start) { |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 240 | if (I->end == End) { |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 241 | removeKills(I->valno, Start, End); |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 242 | if (RemoveDeadValNo) { |
| 243 | // Check if val# is dead. |
| 244 | bool isDead = true; |
| 245 | for (const_iterator II = begin(), EE = end(); II != EE; ++II) |
| 246 | if (II != I && II->valno == ValNo) { |
| 247 | isDead = false; |
| 248 | break; |
| 249 | } |
| 250 | if (isDead) { |
| 251 | // Now that ValNo is dead, remove it. If it is the largest value |
| 252 | // number, just nuke it (and any other deleted values neighboring it), |
| 253 | // otherwise mark it as ~1U so it can be nuked later. |
| 254 | if (ValNo->id == getNumValNums()-1) { |
| 255 | do { |
| 256 | VNInfo *VNI = valnos.back(); |
| 257 | valnos.pop_back(); |
| 258 | VNI->~VNInfo(); |
| 259 | } while (!valnos.empty() && valnos.back()->def == ~1U); |
| 260 | } else { |
| 261 | ValNo->def = ~1U; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 266 | ranges.erase(I); // Removed the whole LiveRange. |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 267 | } else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | I->start = End; |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | // Otherwise if the span we are removing is at the end of the LiveRange, |
| 273 | // adjust the other way. |
| 274 | if (I->end == End) { |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 275 | removeKills(ValNo, Start, End); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 276 | I->end = Start; |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Otherwise, we are splitting the LiveRange into two pieces. |
| 281 | unsigned OldEnd = I->end; |
| 282 | I->end = Start; // Trim the old interval. |
| 283 | |
| 284 | // Insert the new one. |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 285 | ranges.insert(next(I), LiveRange(End, OldEnd, ValNo)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 288 | /// removeValNo - Remove all the ranges defined by the specified value#. |
| 289 | /// Also remove the value# from value# list. |
| 290 | void LiveInterval::removeValNo(VNInfo *ValNo) { |
| 291 | if (empty()) return; |
| 292 | Ranges::iterator I = ranges.end(); |
| 293 | Ranges::iterator E = ranges.begin(); |
| 294 | do { |
| 295 | --I; |
| 296 | if (I->valno == ValNo) |
| 297 | ranges.erase(I); |
| 298 | } while (I != E); |
| 299 | // Now that ValNo is dead, remove it. If it is the largest value |
| 300 | // number, just nuke it (and any other deleted values neighboring it), |
| 301 | // otherwise mark it as ~1U so it can be nuked later. |
| 302 | if (ValNo->id == getNumValNums()-1) { |
| 303 | do { |
| 304 | VNInfo *VNI = valnos.back(); |
| 305 | valnos.pop_back(); |
| 306 | VNI->~VNInfo(); |
| 307 | } while (!valnos.empty() && valnos.back()->def == ~1U); |
| 308 | } else { |
| 309 | ValNo->def = ~1U; |
| 310 | } |
| 311 | } |
| 312 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 313 | /// getLiveRangeContaining - Return the live range that contains the |
| 314 | /// specified index, or null if there is none. |
| 315 | LiveInterval::const_iterator |
| 316 | LiveInterval::FindLiveRangeContaining(unsigned Idx) const { |
| 317 | const_iterator It = std::upper_bound(begin(), end(), Idx); |
| 318 | if (It != ranges.begin()) { |
| 319 | --It; |
| 320 | if (It->contains(Idx)) |
| 321 | return It; |
| 322 | } |
| 323 | |
| 324 | return end(); |
| 325 | } |
| 326 | |
| 327 | LiveInterval::iterator |
| 328 | LiveInterval::FindLiveRangeContaining(unsigned Idx) { |
| 329 | iterator It = std::upper_bound(begin(), end(), Idx); |
| 330 | if (It != begin()) { |
| 331 | --It; |
| 332 | if (It->contains(Idx)) |
| 333 | return It; |
| 334 | } |
| 335 | |
| 336 | return end(); |
| 337 | } |
| 338 | |
| 339 | /// join - Join two live intervals (this, and other) together. This applies |
| 340 | /// mappings to the value numbers in the LHS/RHS intervals as specified. If |
| 341 | /// the intervals are not joinable, this aborts. |
David Greene | e97f077 | 2007-09-06 19:46:46 +0000 | [diff] [blame] | 342 | void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments, |
| 343 | const int *RHSValNoAssignments, |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 344 | SmallVector<VNInfo*, 16> &NewVNInfo) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 345 | // Determine if any of our live range values are mapped. This is uncommon, so |
Evan Cheng | 8b7533e | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 346 | // we want to avoid the interval scan if not. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 347 | bool MustMapCurValNos = false; |
Evan Cheng | 8b7533e | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 348 | unsigned NumVals = getNumValNums(); |
| 349 | unsigned NumNewVals = NewVNInfo.size(); |
| 350 | for (unsigned i = 0; i != NumVals; ++i) { |
| 351 | unsigned LHSValID = LHSValNoAssignments[i]; |
| 352 | if (i != LHSValID || |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 353 | (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 354 | MustMapCurValNos = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 355 | } |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 356 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 357 | // If we have to apply a mapping to our base interval assignment, rewrite it |
| 358 | // now. |
| 359 | if (MustMapCurValNos) { |
| 360 | // Map the first live range. |
| 361 | iterator OutIt = begin(); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 362 | OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 363 | ++OutIt; |
| 364 | for (iterator I = OutIt, E = end(); I != E; ++I) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 365 | OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 366 | |
| 367 | // If this live range has the same value # as its immediate predecessor, |
| 368 | // and if they are neighbors, remove one LiveRange. This happens when we |
| 369 | // have [0,3:0)[4,7:1) and map 0/1 onto the same value #. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 370 | if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 371 | (OutIt-1)->end = OutIt->end; |
| 372 | } else { |
| 373 | if (I != OutIt) { |
| 374 | OutIt->start = I->start; |
| 375 | OutIt->end = I->end; |
| 376 | } |
| 377 | |
| 378 | // Didn't merge, on to the next one. |
| 379 | ++OutIt; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // If we merge some live ranges, chop off the end. |
| 384 | ranges.erase(OutIt, end()); |
| 385 | } |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 386 | |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 387 | // Remember assignements because val# ids are changing. |
Evan Cheng | 8b7533e | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 388 | SmallVector<unsigned, 16> OtherAssignments; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 389 | for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) |
| 390 | OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]); |
| 391 | |
| 392 | // Update val# info. Renumber them and make sure they all belong to this |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 393 | // LiveInterval now. Also remove dead val#'s. |
| 394 | unsigned NumValNos = 0; |
| 395 | for (unsigned i = 0; i < NumNewVals; ++i) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 396 | VNInfo *VNI = NewVNInfo[i]; |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 397 | if (VNI) { |
| 398 | if (i >= NumVals) |
| 399 | valnos.push_back(VNI); |
| 400 | else |
| 401 | valnos[NumValNos] = VNI; |
| 402 | VNI->id = NumValNos++; // Renumber val#. |
Evan Cheng | 8b7533e | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Evan Cheng | 8b7533e | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 405 | if (NumNewVals < NumVals) |
| 406 | valnos.resize(NumNewVals); // shrinkify |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 407 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 408 | // Okay, now insert the RHS live ranges into the LHS. |
| 409 | iterator InsertPos = begin(); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 410 | unsigned RangeNo = 0; |
| 411 | for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) { |
| 412 | // Map the valno in the other live range to the current live range. |
| 413 | I->valno = NewVNInfo[OtherAssignments[RangeNo]]; |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 414 | assert(I->valno && "Adding a dead range?"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 415 | InsertPos = addRangeFrom(*I, InsertPos); |
| 416 | } |
| 417 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 418 | weight += Other.weight; |
| 419 | if (Other.preference && !preference) |
| 420 | preference = Other.preference; |
| 421 | } |
| 422 | |
| 423 | /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live |
| 424 | /// interval as the specified value number. The LiveRanges in RHS are |
| 425 | /// allowed to overlap with LiveRanges in the current interval, but only if |
| 426 | /// the overlapping LiveRanges have the specified value number. |
| 427 | void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 428 | VNInfo *LHSValNo) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 429 | // TODO: Make this more efficient. |
| 430 | iterator InsertPos = begin(); |
| 431 | for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 432 | // Map the valno in the other live range to the current live range. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 433 | LiveRange Tmp = *I; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 434 | Tmp.valno = LHSValNo; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 435 | InsertPos = addRangeFrom(Tmp, InsertPos); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | |
Evan Cheng | 687d108 | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 440 | /// MergeValueInAsValue - Merge all of the live ranges of a specific val# |
| 441 | /// in RHS into this live interval as the specified value number. |
| 442 | /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 443 | /// current interval, it will replace the value numbers of the overlaped |
| 444 | /// live ranges with the specified value number. |
Evan Cheng | 687d108 | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 445 | void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS, |
Evan Cheng | 06582a0 | 2007-10-14 10:08:34 +0000 | [diff] [blame] | 446 | const VNInfo *RHSValNo, VNInfo *LHSValNo) { |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 447 | SmallVector<VNInfo*, 4> ReplacedValNos; |
| 448 | iterator IP = begin(); |
Evan Cheng | 687d108 | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 449 | for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { |
| 450 | if (I->valno != RHSValNo) |
| 451 | continue; |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 452 | unsigned Start = I->start, End = I->end; |
| 453 | IP = std::upper_bound(IP, end(), Start); |
| 454 | // If the start of this range overlaps with an existing liverange, trim it. |
| 455 | if (IP != begin() && IP[-1].end > Start) { |
Evan Cheng | c7fe7be | 2008-01-30 22:44:55 +0000 | [diff] [blame] | 456 | if (IP[-1].valno != LHSValNo) { |
| 457 | ReplacedValNos.push_back(IP[-1].valno); |
| 458 | IP[-1].valno = LHSValNo; // Update val#. |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 459 | } |
| 460 | Start = IP[-1].end; |
| 461 | // Trimmed away the whole range? |
| 462 | if (Start >= End) continue; |
| 463 | } |
| 464 | // If the end of this range overlaps with an existing liverange, trim it. |
| 465 | if (IP != end() && End > IP->start) { |
| 466 | if (IP->valno != LHSValNo) { |
| 467 | ReplacedValNos.push_back(IP->valno); |
| 468 | IP->valno = LHSValNo; // Update val#. |
| 469 | } |
| 470 | End = IP->start; |
| 471 | // If this trimmed away the whole range, ignore it. |
| 472 | if (Start == End) continue; |
| 473 | } |
| 474 | |
Evan Cheng | 687d108 | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 475 | // Map the valno in the other live range to the current live range. |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 476 | IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | SmallSet<VNInfo*, 4> Seen; |
| 481 | for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) { |
| 482 | VNInfo *V1 = ReplacedValNos[i]; |
| 483 | if (Seen.insert(V1)) { |
| 484 | bool isDead = true; |
| 485 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 486 | if (I->valno == V1) { |
| 487 | isDead = false; |
| 488 | break; |
| 489 | } |
| 490 | if (isDead) { |
| 491 | // Now that V1 is dead, remove it. If it is the largest value number, |
| 492 | // just nuke it (and any other deleted values neighboring it), otherwise |
| 493 | // mark it as ~1U so it can be nuked later. |
| 494 | if (V1->id == getNumValNums()-1) { |
| 495 | do { |
| 496 | VNInfo *VNI = valnos.back(); |
| 497 | valnos.pop_back(); |
| 498 | VNI->~VNInfo(); |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 499 | } while (!valnos.empty() && valnos.back()->def == ~1U); |
Evan Cheng | 303fed8 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 500 | } else { |
| 501 | V1->def = ~1U; |
| 502 | } |
| 503 | } |
| 504 | } |
Evan Cheng | 687d108 | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 509 | /// MergeInClobberRanges - For any live ranges that are not defined in the |
| 510 | /// current interval, but are defined in the Clobbers interval, mark them |
| 511 | /// used with an unknown definition value. |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 512 | void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers, |
| 513 | BumpPtrAllocator &VNInfoAllocator) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 514 | if (Clobbers.begin() == Clobbers.end()) return; |
| 515 | |
| 516 | // Find a value # to use for the clobber ranges. If there is already a value# |
| 517 | // for unknown values, use it. |
| 518 | // FIXME: Use a single sentinal number for these! |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 519 | VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 520 | |
| 521 | iterator IP = begin(); |
| 522 | for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) { |
| 523 | unsigned Start = I->start, End = I->end; |
| 524 | IP = std::upper_bound(IP, end(), Start); |
| 525 | |
| 526 | // If the start of this range overlaps with an existing liverange, trim it. |
| 527 | if (IP != begin() && IP[-1].end > Start) { |
| 528 | Start = IP[-1].end; |
| 529 | // Trimmed away the whole range? |
| 530 | if (Start >= End) continue; |
| 531 | } |
| 532 | // If the end of this range overlaps with an existing liverange, trim it. |
| 533 | if (IP != end() && End > IP->start) { |
| 534 | End = IP->start; |
| 535 | // If this trimmed away the whole range, ignore it. |
| 536 | if (Start == End) continue; |
| 537 | } |
| 538 | |
| 539 | // Insert the clobber interval. |
| 540 | IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /// MergeValueNumberInto - This method is called when two value nubmers |
| 545 | /// are found to be equivalent. This eliminates V1, replacing all |
| 546 | /// LiveRanges with the V1 value number with the V2 value number. This can |
| 547 | /// cause merging of V1/V2 values numbers and compaction of the value space. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 548 | void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 549 | assert(V1 != V2 && "Identical value#'s are always equivalent!"); |
| 550 | |
| 551 | // This code actually merges the (numerically) larger value number into the |
| 552 | // smaller value number, which is likely to allow us to compactify the value |
| 553 | // space. The only thing we have to be careful of is to preserve the |
| 554 | // instruction that defines the result value. |
| 555 | |
| 556 | // Make sure V2 is smaller than V1. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 557 | if (V1->id < V2->id) { |
Evan Cheng | 319802c | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 558 | copyValNumInfo(V1, V2); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 559 | std::swap(V1, V2); |
| 560 | } |
| 561 | |
| 562 | // Merge V1 live ranges into V2. |
| 563 | for (iterator I = begin(); I != end(); ) { |
| 564 | iterator LR = I++; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 565 | if (LR->valno != V1) continue; // Not a V1 LiveRange. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 566 | |
| 567 | // Okay, we found a V1 live range. If it had a previous, touching, V2 live |
| 568 | // range, extend it. |
| 569 | if (LR != begin()) { |
| 570 | iterator Prev = LR-1; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 571 | if (Prev->valno == V2 && Prev->end == LR->start) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 572 | Prev->end = LR->end; |
| 573 | |
| 574 | // Erase this live-range. |
| 575 | ranges.erase(LR); |
| 576 | I = Prev+1; |
| 577 | LR = Prev; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // Okay, now we have a V1 or V2 live range that is maximally merged forward. |
| 582 | // Ensure that it is a V2 live-range. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 583 | LR->valno = V2; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 584 | |
| 585 | // If we can merge it into later V2 live ranges, do so now. We ignore any |
| 586 | // following V1 live ranges, as they will be merged in subsequent iterations |
| 587 | // of the loop. |
| 588 | if (I != end()) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 589 | if (I->start == LR->end && I->valno == V2) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 590 | LR->end = I->end; |
| 591 | ranges.erase(I); |
| 592 | I = LR+1; |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Now that V1 is dead, remove it. If it is the largest value number, just |
| 598 | // nuke it (and any other deleted values neighboring it), otherwise mark it as |
| 599 | // ~1U so it can be nuked later. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 600 | if (V1->id == getNumValNums()-1) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 601 | do { |
Evan Cheng | 27344d4 | 2007-09-06 01:07:24 +0000 | [diff] [blame] | 602 | VNInfo *VNI = valnos.back(); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 603 | valnos.pop_back(); |
Evan Cheng | 27344d4 | 2007-09-06 01:07:24 +0000 | [diff] [blame] | 604 | VNI->~VNInfo(); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 605 | } while (valnos.back()->def == ~1U); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 606 | } else { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 607 | V1->def = ~1U; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Evan Cheng | 687d108 | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 611 | void LiveInterval::Copy(const LiveInterval &RHS, |
| 612 | BumpPtrAllocator &VNInfoAllocator) { |
| 613 | ranges.clear(); |
| 614 | valnos.clear(); |
| 615 | preference = RHS.preference; |
| 616 | weight = RHS.weight; |
| 617 | for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) { |
| 618 | const VNInfo *VNI = RHS.getValNumInfo(i); |
| 619 | VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator); |
| 620 | copyValNumInfo(NewVNI, VNI); |
| 621 | } |
| 622 | for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) { |
| 623 | const LiveRange &LR = RHS.ranges[i]; |
| 624 | addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id))); |
| 625 | } |
| 626 | } |
| 627 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 628 | unsigned LiveInterval::getSize() const { |
| 629 | unsigned Sum = 0; |
| 630 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 631 | Sum += I->end - I->start; |
| 632 | return Sum; |
| 633 | } |
| 634 | |
| 635 | std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 636 | return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void LiveRange::dump() const { |
| 640 | cerr << *this << "\n"; |
| 641 | } |
| 642 | |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 643 | void LiveInterval::print(std::ostream &OS, |
| 644 | const TargetRegisterInfo *TRI) const { |
| 645 | if (TRI && TargetRegisterInfo::isPhysicalRegister(reg)) |
| 646 | OS << TRI->getName(reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 647 | else |
| 648 | OS << "%reg" << reg; |
| 649 | |
| 650 | OS << ',' << weight; |
| 651 | |
| 652 | if (empty()) |
| 653 | OS << "EMPTY"; |
| 654 | else { |
| 655 | OS << " = "; |
| 656 | for (LiveInterval::Ranges::const_iterator I = ranges.begin(), |
| 657 | E = ranges.end(); I != E; ++I) |
| 658 | OS << *I; |
| 659 | } |
| 660 | |
| 661 | // Print value number info. |
| 662 | if (getNumValNums()) { |
| 663 | OS << " "; |
Evan Cheng | ba99052 | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 664 | unsigned vnum = 0; |
| 665 | for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; |
| 666 | ++i, ++vnum) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 667 | const VNInfo *vni = *i; |
Evan Cheng | ba99052 | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 668 | if (vnum) OS << " "; |
| 669 | OS << vnum << "@"; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 670 | if (vni->def == ~1U) { |
Evan Cheng | 58c2b76 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 671 | OS << "x"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 672 | } else { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 673 | if (vni->def == ~0U) |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 674 | OS << "?"; |
| 675 | else |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 676 | OS << vni->def; |
| 677 | unsigned ee = vni->kills.size(); |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 678 | if (ee || vni->hasPHIKill) { |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 679 | OS << "-("; |
Evan Cheng | ba99052 | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 680 | for (unsigned j = 0; j != ee; ++j) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 681 | OS << vni->kills[j]; |
Evan Cheng | ba99052 | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 682 | if (j != ee-1) |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 683 | OS << " "; |
| 684 | } |
Evan Cheng | 49208cf | 2008-02-13 02:48:26 +0000 | [diff] [blame^] | 685 | if (vni->hasPHIKill) { |
| 686 | if (ee) |
| 687 | OS << " "; |
| 688 | OS << "phi"; |
| 689 | } |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 690 | OS << ")"; |
| 691 | } |
Evan Cheng | 4151fde | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 692 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void LiveInterval::dump() const { |
| 698 | cerr << *this << "\n"; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | void LiveRange::print(std::ostream &os) const { |
| 703 | os << *this; |
| 704 | } |