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