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 |
Bob Wilson | 86af655 | 2010-01-12 22:18:56 +0000 | [diff] [blame] | 13 | // such that v is live at j' and there is no instruction with number i' < i such |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 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" |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Evan Cheng | 0adb527 | 2009-04-25 09:25:19 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | 3c1f4a4 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Bill Wendling | 38b0e7b | 2006-11-28 03:31:29 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
David Greene | 5242154 | 2010-01-04 22:41:43 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | a717b7b | 2009-07-24 10:47:20 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | c4d3b91 | 2004-09-28 02:38:58 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Jakob Stoklund Olesen | f568b27 | 2010-09-21 17:12:18 +0000 | [diff] [blame] | 33 | LiveInterval::iterator LiveInterval::find(SlotIndex Pos) { |
Jakob Stoklund Olesen | 55768d7 | 2011-03-12 01:50:35 +0000 | [diff] [blame] | 34 | // This algorithm is basically std::upper_bound. |
| 35 | // Unfortunately, std::upper_bound cannot be used with mixed types until we |
| 36 | // adopt C++0x. Many libraries can do it, but not all. |
| 37 | if (empty() || Pos >= endIndex()) |
| 38 | return end(); |
| 39 | iterator I = begin(); |
| 40 | size_t Len = ranges.size(); |
| 41 | do { |
| 42 | size_t Mid = Len >> 1; |
| 43 | if (Pos < I[Mid].end) |
| 44 | Len = Mid; |
| 45 | else |
| 46 | I += Mid + 1, Len -= Mid + 1; |
| 47 | } while (Len); |
| 48 | return I; |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// killedInRange - Return true if the interval has kills in [Start,End). |
| 52 | bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const { |
| 53 | Ranges::const_iterator r = |
| 54 | std::lower_bound(ranges.begin(), ranges.end(), End); |
| 55 | |
| 56 | // Now r points to the first interval with start >= End, or ranges.end(). |
| 57 | if (r == ranges.begin()) |
| 58 | return false; |
| 59 | |
| 60 | --r; |
| 61 | // Now r points to the last interval with end <= End. |
| 62 | // r->end is the kill point. |
| 63 | return r->end >= Start && r->end < End; |
| 64 | } |
| 65 | |
Chris Lattner | bae74d9 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 66 | // overlaps - Return true if the intersection of the two live intervals is |
| 67 | // not empty. |
| 68 | // |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 69 | // An example for overlaps(): |
| 70 | // |
| 71 | // 0: A = ... |
| 72 | // 4: B = ... |
| 73 | // 8: C = A + B ;; last use of A |
| 74 | // |
| 75 | // The live intervals should look like: |
| 76 | // |
| 77 | // A = [3, 11) |
| 78 | // B = [7, x) |
| 79 | // C = [11, y) |
| 80 | // |
| 81 | // A->overlaps(C) should return false since we want to be able to join |
| 82 | // A and C. |
Chris Lattner | bae74d9 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 83 | // |
| 84 | bool LiveInterval::overlapsFrom(const LiveInterval& other, |
| 85 | const_iterator StartPos) const { |
Jakob Stoklund Olesen | 6382d2c | 2010-07-13 19:56:28 +0000 | [diff] [blame] | 86 | assert(!empty() && "empty interval"); |
Chris Lattner | bae74d9 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 87 | const_iterator i = begin(); |
| 88 | const_iterator ie = end(); |
| 89 | const_iterator j = StartPos; |
| 90 | const_iterator je = other.end(); |
| 91 | |
| 92 | assert((StartPos->start <= i->start || StartPos == other.begin()) && |
Chris Lattner | 8c68b6a | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 93 | StartPos != other.end() && "Bogus start position hint!"); |
Chris Lattner | f542649 | 2004-07-25 07:11:19 +0000 | [diff] [blame] | 94 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 95 | if (i->start < j->start) { |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 96 | i = std::upper_bound(i, ie, j->start); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 97 | if (i != ranges.begin()) --i; |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 98 | } else if (j->start < i->start) { |
Chris Lattner | ead1b3f | 2004-12-04 01:22:09 +0000 | [diff] [blame] | 99 | ++StartPos; |
| 100 | if (StartPos != other.end() && StartPos->start <= i->start) { |
| 101 | assert(StartPos < other.end() && i < end()); |
Chris Lattner | 8c68b6a | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 102 | j = std::upper_bound(j, je, i->start); |
| 103 | if (j != other.ranges.begin()) --j; |
| 104 | } |
Chris Lattner | aa14147 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 105 | } else { |
| 106 | return true; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chris Lattner | 9fddc12 | 2004-11-18 05:28:21 +0000 | [diff] [blame] | 109 | if (j == je) return false; |
| 110 | |
| 111 | while (i != ie) { |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 112 | if (i->start > j->start) { |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 113 | std::swap(i, j); |
| 114 | std::swap(ie, je); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 116 | |
| 117 | if (i->end > j->start) |
| 118 | return true; |
| 119 | ++i; |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Evan Cheng | cccdb2b | 2009-04-18 08:52:15 +0000 | [diff] [blame] | 125 | /// overlaps - Return true if the live interval overlaps a range specified |
| 126 | /// by [Start, End). |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 127 | bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const { |
Evan Cheng | cccdb2b | 2009-04-18 08:52:15 +0000 | [diff] [blame] | 128 | assert(Start < End && "Invalid range"); |
Jakob Stoklund Olesen | 186eb73 | 2010-07-13 19:42:20 +0000 | [diff] [blame] | 129 | const_iterator I = std::lower_bound(begin(), end(), End); |
| 130 | return I != begin() && (--I)->end > Start; |
Evan Cheng | cccdb2b | 2009-04-18 08:52:15 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Lang Hames | 6f4e4df | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 133 | |
| 134 | /// ValNo is dead, remove it. If it is the largest value number, just nuke it |
| 135 | /// (and any other deleted values neighboring it), otherwise mark it as ~1U so |
| 136 | /// it can be nuked later. |
| 137 | void LiveInterval::markValNoForDeletion(VNInfo *ValNo) { |
| 138 | if (ValNo->id == getNumValNums()-1) { |
| 139 | do { |
| 140 | valnos.pop_back(); |
| 141 | } while (!valnos.empty() && valnos.back()->isUnused()); |
| 142 | } else { |
| 143 | ValNo->setIsUnused(true); |
| 144 | } |
| 145 | } |
| 146 | |
Jakob Stoklund Olesen | 2343659 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 147 | /// RenumberValues - Renumber all values in order of appearance and delete the |
| 148 | /// remaining unused values. |
Jakob Stoklund Olesen | fff2c47 | 2010-08-12 20:38:03 +0000 | [diff] [blame] | 149 | void LiveInterval::RenumberValues(LiveIntervals &lis) { |
Jakob Stoklund Olesen | 2343659 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 150 | SmallPtrSet<VNInfo*, 8> Seen; |
| 151 | valnos.clear(); |
| 152 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
| 153 | VNInfo *VNI = I->valno; |
| 154 | if (!Seen.insert(VNI)) |
| 155 | continue; |
| 156 | assert(!VNI->isUnused() && "Unused valno used by live range"); |
| 157 | VNI->id = (unsigned)valnos.size(); |
| 158 | valnos.push_back(VNI); |
| 159 | } |
| 160 | } |
| 161 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 162 | /// extendIntervalEndTo - This method is used when we want to extend the range |
| 163 | /// specified by I to end at the specified endpoint. To do this, we should |
| 164 | /// merge and eliminate all ranges that this will overlap with. The iterator is |
| 165 | /// not invalidated. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 166 | void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) { |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 167 | assert(I != ranges.end() && "Not a valid interval!"); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 168 | VNInfo *ValNo = I->valno; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 169 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 170 | // Search for the first interval that we can't merge with. |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 171 | Ranges::iterator MergeTo = llvm::next(I); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 172 | for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 173 | assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 175 | |
| 176 | // If NewEnd was in the middle of an interval, make sure to get its endpoint. |
| 177 | I->end = std::max(NewEnd, prior(MergeTo)->end); |
| 178 | |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 179 | // Erase any dead ranges. |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 180 | ranges.erase(llvm::next(I), MergeTo); |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 181 | |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 182 | // If the newly formed range now touches the range after it and if they have |
| 183 | // the same value number, merge the two ranges into one range. |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 184 | Ranges::iterator Next = llvm::next(I); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 185 | if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) { |
Chris Lattner | cef6010 | 2005-10-20 22:50:10 +0000 | [diff] [blame] | 186 | I->end = Next->end; |
| 187 | ranges.erase(Next); |
Chris Lattner | b0fa11c | 2005-10-20 07:39:25 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | /// extendIntervalStartTo - This method is used when we want to extend the range |
| 193 | /// specified by I to start at the specified endpoint. To do this, we should |
| 194 | /// merge and eliminate all ranges that this will overlap with. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 195 | LiveInterval::Ranges::iterator |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 196 | LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) { |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 197 | assert(I != ranges.end() && "Not a valid interval!"); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 198 | VNInfo *ValNo = I->valno; |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 199 | |
| 200 | // Search for the first interval that we can't merge with. |
| 201 | Ranges::iterator MergeTo = I; |
| 202 | do { |
| 203 | if (MergeTo == ranges.begin()) { |
| 204 | I->start = NewStart; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 205 | ranges.erase(MergeTo, I); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 206 | return I; |
| 207 | } |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 208 | assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 209 | --MergeTo; |
| 210 | } while (NewStart <= MergeTo->start); |
| 211 | |
| 212 | // If we start in the middle of another interval, just delete a range and |
| 213 | // extend that interval. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 214 | if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) { |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 215 | MergeTo->end = I->end; |
| 216 | } else { |
| 217 | // Otherwise, extend the interval right after. |
| 218 | ++MergeTo; |
| 219 | MergeTo->start = NewStart; |
| 220 | MergeTo->end = I->end; |
| 221 | } |
| 222 | |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 223 | ranges.erase(llvm::next(MergeTo), llvm::next(I)); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 224 | return MergeTo; |
| 225 | } |
| 226 | |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 227 | LiveInterval::iterator |
| 228 | LiveInterval::addRangeFrom(LiveRange LR, iterator From) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 229 | SlotIndex Start = LR.start, End = LR.end; |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 230 | iterator it = std::upper_bound(From, ranges.end(), Start); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 231 | |
| 232 | // If the inserted interval starts in the middle or right at the end of |
| 233 | // another interval, just extend that interval to contain the range of LR. |
| 234 | if (it != ranges.begin()) { |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 235 | iterator B = prior(it); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 236 | if (LR.valno == B->valno) { |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 237 | if (B->start <= Start && B->end >= Start) { |
| 238 | extendIntervalEndTo(B, End); |
| 239 | return B; |
| 240 | } |
| 241 | } else { |
| 242 | // 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] | 243 | // different valno's. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 244 | assert(B->end <= Start && |
Brian Gaeke | 8311bef | 2004-11-16 06:52:35 +0000 | [diff] [blame] | 245 | "Cannot overlap two LiveRanges with differing ValID's" |
| 246 | " (did you def the same reg twice in a MachineInstr?)"); |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | // Otherwise, if this range ends in the middle of, or right next to, another |
| 251 | // interval, merge it into that interval. |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 252 | if (it != ranges.end()) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 253 | if (LR.valno == it->valno) { |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 254 | if (it->start <= End) { |
| 255 | it = extendIntervalStartTo(it, Start); |
| 256 | |
| 257 | // If LR is a complete superset of an interval, we may need to grow its |
| 258 | // endpoint as well. |
| 259 | if (End > it->end) |
| 260 | extendIntervalEndTo(it, End); |
| 261 | return it; |
| 262 | } |
| 263 | } else { |
| 264 | // 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] | 265 | // different valno's. |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 266 | assert(it->start >= End && |
| 267 | "Cannot overlap two LiveRanges with differing ValID's"); |
| 268 | } |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 269 | } |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 270 | |
| 271 | // Otherwise, this is just a new range that doesn't interact with anything. |
| 272 | // Insert it. |
| 273 | return ranges.insert(it, LR); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Jakob Stoklund Olesen | ee5655d | 2011-09-13 16:47:56 +0000 | [diff] [blame] | 276 | /// extendInBlock - If this interval is live before Kill in the basic |
| 277 | /// block that starts at StartIdx, extend it to be live up to Kill and return |
| 278 | /// the value. If there is no live range before Kill, return NULL. |
| 279 | VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) { |
Jakob Stoklund Olesen | 9763e2b | 2011-03-02 00:06:15 +0000 | [diff] [blame] | 280 | if (empty()) |
| 281 | return 0; |
Jakob Stoklund Olesen | ee5655d | 2011-09-13 16:47:56 +0000 | [diff] [blame] | 282 | iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot()); |
Jakob Stoklund Olesen | 9763e2b | 2011-03-02 00:06:15 +0000 | [diff] [blame] | 283 | if (I == begin()) |
| 284 | return 0; |
| 285 | --I; |
| 286 | if (I->end <= StartIdx) |
| 287 | return 0; |
Jakob Stoklund Olesen | ee5655d | 2011-09-13 16:47:56 +0000 | [diff] [blame] | 288 | if (I->end < Kill) |
| 289 | extendIntervalEndTo(I, Kill); |
Jakob Stoklund Olesen | 9763e2b | 2011-03-02 00:06:15 +0000 | [diff] [blame] | 290 | return I->valno; |
| 291 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 292 | |
| 293 | /// removeRange - Remove the specified range from this interval. Note that |
Evan Cheng | 42cc6e3 | 2009-01-29 00:06:09 +0000 | [diff] [blame] | 294 | /// the range must be in a single LiveRange in its entirety. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 295 | void LiveInterval::removeRange(SlotIndex Start, SlotIndex End, |
Evan Cheng | d2b8d7b | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 296 | bool RemoveDeadValNo) { |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 297 | // Find the LiveRange containing this span. |
Jakob Stoklund Olesen | f568b27 | 2010-09-21 17:12:18 +0000 | [diff] [blame] | 298 | Ranges::iterator I = find(Start); |
| 299 | assert(I != ranges.end() && "Range is not in interval!"); |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 300 | assert(I->containsRange(Start, End) && "Range is not entirely in interval!"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 301 | |
| 302 | // 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] | 303 | VNInfo *ValNo = I->valno; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 304 | if (I->start == Start) { |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 305 | if (I->end == End) { |
Evan Cheng | d2b8d7b | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 306 | if (RemoveDeadValNo) { |
| 307 | // Check if val# is dead. |
| 308 | bool isDead = true; |
| 309 | for (const_iterator II = begin(), EE = end(); II != EE; ++II) |
| 310 | if (II != I && II->valno == ValNo) { |
| 311 | isDead = false; |
| 312 | break; |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 313 | } |
Evan Cheng | d2b8d7b | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 314 | if (isDead) { |
Lang Hames | 6f4e4df | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 315 | // Now that ValNo is dead, remove it. |
| 316 | markValNoForDeletion(ValNo); |
Evan Cheng | d2b8d7b | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 320 | ranges.erase(I); // Removed the whole LiveRange. |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 321 | } else |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 322 | I->start = End; |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // Otherwise if the span we are removing is at the end of the LiveRange, |
| 327 | // adjust the other way. |
| 328 | if (I->end == End) { |
Chris Lattner | 6925a9f | 2004-07-25 05:43:53 +0000 | [diff] [blame] | 329 | I->end = Start; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
| 333 | // Otherwise, we are splitting the LiveRange into two pieces. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 334 | SlotIndex OldEnd = I->end; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 335 | I->end = Start; // Trim the old interval. |
| 336 | |
| 337 | // Insert the new one. |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 338 | ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo)); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Evan Cheng | d2b8d7b | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 341 | /// removeValNo - Remove all the ranges defined by the specified value#. |
| 342 | /// Also remove the value# from value# list. |
| 343 | void LiveInterval::removeValNo(VNInfo *ValNo) { |
| 344 | if (empty()) return; |
| 345 | Ranges::iterator I = ranges.end(); |
| 346 | Ranges::iterator E = ranges.begin(); |
| 347 | do { |
| 348 | --I; |
| 349 | if (I->valno == ValNo) |
| 350 | ranges.erase(I); |
| 351 | } while (I != E); |
Lang Hames | 6f4e4df | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 352 | // Now that ValNo is dead, remove it. |
| 353 | markValNoForDeletion(ValNo); |
Evan Cheng | d2b8d7b | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 354 | } |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 355 | |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 356 | /// findDefinedVNInfo - Find the VNInfo defined by the specified |
| 357 | /// index (register interval). |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 358 | VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const { |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 359 | for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end(); |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 360 | i != e; ++i) { |
| 361 | if ((*i)->def == Idx) |
| 362 | return *i; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 368 | /// join - Join two live intervals (this, and other) together. This applies |
| 369 | /// mappings to the value numbers in the LHS/RHS intervals as specified. If |
| 370 | /// the intervals are not joinable, this aborts. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 371 | void LiveInterval::join(LiveInterval &Other, |
| 372 | const int *LHSValNoAssignments, |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 373 | const int *RHSValNoAssignments, |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 374 | SmallVector<VNInfo*, 16> &NewVNInfo, |
| 375 | MachineRegisterInfo *MRI) { |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 376 | // Determine if any of our live range values are mapped. This is uncommon, so |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 377 | // we want to avoid the interval scan if not. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 378 | bool MustMapCurValNos = false; |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 379 | unsigned NumVals = getNumValNums(); |
| 380 | unsigned NumNewVals = NewVNInfo.size(); |
| 381 | for (unsigned i = 0; i != NumVals; ++i) { |
| 382 | unsigned LHSValID = LHSValNoAssignments[i]; |
| 383 | if (i != LHSValID || |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 384 | (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 385 | MustMapCurValNos = true; |
Chris Lattner | deb9971 | 2004-07-24 03:41:50 +0000 | [diff] [blame] | 386 | } |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 388 | // If we have to apply a mapping to our base interval assignment, rewrite it |
| 389 | // now. |
| 390 | if (MustMapCurValNos) { |
| 391 | // Map the first live range. |
| 392 | iterator OutIt = begin(); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 393 | OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 394 | ++OutIt; |
| 395 | for (iterator I = OutIt, E = end(); I != E; ++I) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 396 | OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]]; |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 398 | // If this live range has the same value # as its immediate predecessor, |
| 399 | // and if they are neighbors, remove one LiveRange. This happens when we |
| 400 | // 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] | 401 | if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) { |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 402 | (OutIt-1)->end = OutIt->end; |
| 403 | } else { |
| 404 | if (I != OutIt) { |
| 405 | OutIt->start = I->start; |
| 406 | OutIt->end = I->end; |
| 407 | } |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 409 | // Didn't merge, on to the next one. |
| 410 | ++OutIt; |
| 411 | } |
| 412 | } |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 414 | // If we merge some live ranges, chop off the end. |
| 415 | ranges.erase(OutIt, end()); |
| 416 | } |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 417 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 418 | // Remember assignements because val# ids are changing. |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 419 | SmallVector<unsigned, 16> OtherAssignments; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 420 | for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) |
| 421 | OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]); |
| 422 | |
| 423 | // 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] | 424 | // LiveInterval now. Also remove dead val#'s. |
| 425 | unsigned NumValNos = 0; |
| 426 | for (unsigned i = 0; i < NumNewVals; ++i) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 427 | VNInfo *VNI = NewVNInfo[i]; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 428 | if (VNI) { |
Evan Cheng | 30590f5 | 2009-04-28 06:24:09 +0000 | [diff] [blame] | 429 | if (NumValNos >= NumVals) |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 430 | valnos.push_back(VNI); |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 431 | else |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 432 | valnos[NumValNos] = VNI; |
| 433 | VNI->id = NumValNos++; // Renumber val#. |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 436 | if (NumNewVals < NumVals) |
| 437 | valnos.resize(NumNewVals); // shrinkify |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 439 | // Okay, now insert the RHS live ranges into the LHS. |
Chris Lattner | c114b2c | 2006-08-25 23:41:24 +0000 | [diff] [blame] | 440 | iterator InsertPos = begin(); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 441 | unsigned RangeNo = 0; |
| 442 | for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) { |
| 443 | // Map the valno in the other live range to the current live range. |
| 444 | I->valno = NewVNInfo[OtherAssignments[RangeNo]]; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 445 | assert(I->valno && "Adding a dead range?"); |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 446 | InsertPos = addRangeFrom(*I, InsertPos); |
| 447 | } |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 448 | |
David Greene | 29ff37f | 2009-07-22 20:08:25 +0000 | [diff] [blame] | 449 | ComputeJoinedWeight(Other); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 452 | /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live |
| 453 | /// interval as the specified value number. The LiveRanges in RHS are |
| 454 | /// allowed to overlap with LiveRanges in the current interval, but only if |
| 455 | /// the overlapping LiveRanges have the specified value number. |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 456 | void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 457 | VNInfo *LHSValNo) { |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 458 | // TODO: Make this more efficient. |
| 459 | iterator InsertPos = begin(); |
| 460 | for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 461 | // 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] | 462 | LiveRange Tmp = *I; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 463 | Tmp.valno = LHSValNo; |
Chris Lattner | f21f020 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 464 | InsertPos = addRangeFrom(Tmp, InsertPos); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 469 | /// MergeValueInAsValue - Merge all of the live ranges of a specific val# |
| 470 | /// in RHS into this live interval as the specified value number. |
| 471 | /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the |
Evan Cheng | 3c1f4a4 | 2007-10-17 02:13:29 +0000 | [diff] [blame] | 472 | /// current interval, it will replace the value numbers of the overlaped |
| 473 | /// live ranges with the specified value number. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 474 | void LiveInterval::MergeValueInAsValue( |
| 475 | const LiveInterval &RHS, |
| 476 | const VNInfo *RHSValNo, VNInfo *LHSValNo) { |
Jakob Stoklund Olesen | d2eff13 | 2011-03-19 23:02:49 +0000 | [diff] [blame] | 477 | // TODO: Make this more efficient. |
| 478 | iterator InsertPos = begin(); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 479 | for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { |
| 480 | if (I->valno != RHSValNo) |
| 481 | continue; |
| 482 | // Map the valno in the other live range to the current live range. |
Jakob Stoklund Olesen | d2eff13 | 2011-03-19 23:02:49 +0000 | [diff] [blame] | 483 | LiveRange Tmp = *I; |
| 484 | Tmp.valno = LHSValNo; |
| 485 | InsertPos = addRangeFrom(Tmp, InsertPos); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 490 | /// MergeValueNumberInto - This method is called when two value nubmers |
| 491 | /// are found to be equivalent. This eliminates V1, replacing all |
| 492 | /// LiveRanges with the V1 value number with the V2 value number. This can |
| 493 | /// cause merging of V1/V2 values numbers and compaction of the value space. |
Owen Anderson | 5b93f6f | 2009-02-02 22:42:01 +0000 | [diff] [blame] | 494 | VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 495 | assert(V1 != V2 && "Identical value#'s are always equivalent!"); |
| 496 | |
| 497 | // This code actually merges the (numerically) larger value number into the |
| 498 | // smaller value number, which is likely to allow us to compactify the value |
| 499 | // space. The only thing we have to be careful of is to preserve the |
| 500 | // instruction that defines the result value. |
| 501 | |
| 502 | // Make sure V2 is smaller than V1. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 503 | if (V1->id < V2->id) { |
Lang Hames | 52c1afc | 2009-08-10 23:43:28 +0000 | [diff] [blame] | 504 | V1->copyFrom(*V2); |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 505 | std::swap(V1, V2); |
| 506 | } |
| 507 | |
| 508 | // Merge V1 live ranges into V2. |
| 509 | for (iterator I = begin(); I != end(); ) { |
| 510 | iterator LR = I++; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 511 | if (LR->valno != V1) continue; // Not a V1 LiveRange. |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 512 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 513 | // Okay, we found a V1 live range. If it had a previous, touching, V2 live |
| 514 | // range, extend it. |
| 515 | if (LR != begin()) { |
| 516 | iterator Prev = LR-1; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 517 | if (Prev->valno == V2 && Prev->end == LR->start) { |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 518 | Prev->end = LR->end; |
| 519 | |
| 520 | // Erase this live-range. |
| 521 | ranges.erase(LR); |
| 522 | I = Prev+1; |
| 523 | LR = Prev; |
| 524 | } |
| 525 | } |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 526 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 527 | // Okay, now we have a V1 or V2 live range that is maximally merged forward. |
| 528 | // Ensure that it is a V2 live-range. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 529 | LR->valno = V2; |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 530 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 531 | // If we can merge it into later V2 live ranges, do so now. We ignore any |
| 532 | // following V1 live ranges, as they will be merged in subsequent iterations |
| 533 | // of the loop. |
| 534 | if (I != end()) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 535 | if (I->start == LR->end && I->valno == V2) { |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 536 | LR->end = I->end; |
| 537 | ranges.erase(I); |
| 538 | I = LR+1; |
| 539 | } |
| 540 | } |
| 541 | } |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 542 | |
Jakob Stoklund Olesen | e0a73ec | 2010-10-01 23:52:25 +0000 | [diff] [blame] | 543 | // Merge the relevant flags. |
| 544 | V2->mergeFlags(V1); |
| 545 | |
Lang Hames | 6f4e4df | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 546 | // Now that V1 is dead, remove it. |
| 547 | markValNoForDeletion(V1); |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 548 | |
Owen Anderson | 5b93f6f | 2009-02-02 22:42:01 +0000 | [diff] [blame] | 549 | return V2; |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 552 | void LiveInterval::Copy(const LiveInterval &RHS, |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 553 | MachineRegisterInfo *MRI, |
Benjamin Kramer | 991de14 | 2010-03-30 20:16:45 +0000 | [diff] [blame] | 554 | VNInfo::Allocator &VNInfoAllocator) { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 555 | ranges.clear(); |
| 556 | valnos.clear(); |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 557 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg); |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 558 | MRI->setRegAllocationHint(reg, Hint.first, Hint.second); |
| 559 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 560 | weight = RHS.weight; |
| 561 | for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) { |
| 562 | const VNInfo *VNI = RHS.getValNumInfo(i); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 563 | createValueCopy(VNI, VNInfoAllocator); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 564 | } |
| 565 | for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) { |
| 566 | const LiveRange &LR = RHS.ranges[i]; |
| 567 | addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id))); |
| 568 | } |
| 569 | } |
| 570 | |
Evan Cheng | e52eef8 | 2007-04-17 20:25:11 +0000 | [diff] [blame] | 571 | unsigned LiveInterval::getSize() const { |
| 572 | unsigned Sum = 0; |
| 573 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 574 | Sum += I->start.distance(I->end); |
Evan Cheng | e52eef8 | 2007-04-17 20:25:11 +0000 | [diff] [blame] | 575 | return Sum; |
| 576 | } |
| 577 | |
David Greene | 29ff37f | 2009-07-22 20:08:25 +0000 | [diff] [blame] | 578 | /// ComputeJoinedWeight - Set the weight of a live interval Joined |
| 579 | /// after Other has been merged into it. |
| 580 | void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) { |
| 581 | // If either of these intervals was spilled, the weight is the |
| 582 | // weight of the non-spilled interval. This can only happen with |
| 583 | // iterative coalescers. |
| 584 | |
David Greene | 92b78bb | 2009-07-22 22:32:19 +0000 | [diff] [blame] | 585 | if (Other.weight != HUGE_VALF) { |
| 586 | weight += Other.weight; |
| 587 | } |
| 588 | else if (weight == HUGE_VALF && |
David Greene | 29ff37f | 2009-07-22 20:08:25 +0000 | [diff] [blame] | 589 | !TargetRegisterInfo::isPhysicalRegister(reg)) { |
| 590 | // Remove this assert if you have an iterative coalescer |
| 591 | assert(0 && "Joining to spilled interval"); |
| 592 | weight = Other.weight; |
| 593 | } |
David Greene | 29ff37f | 2009-07-22 20:08:25 +0000 | [diff] [blame] | 594 | else { |
| 595 | // Otherwise the weight stays the same |
| 596 | // Remove this assert if you have an iterative coalescer |
| 597 | assert(0 && "Joining from spilled interval"); |
| 598 | } |
| 599 | } |
| 600 | |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 601 | raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) { |
| 602 | return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")"; |
| 603 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 604 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 605 | void LiveRange::dump() const { |
David Greene | 5242154 | 2010-01-04 22:41:43 +0000 | [diff] [blame] | 606 | dbgs() << *this << "\n"; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Chris Lattner | c02497f | 2009-08-23 03:47:42 +0000 | [diff] [blame] | 609 | void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const { |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 610 | OS << PrintReg(reg, TRI); |
| 611 | if (weight != 0) |
| 612 | OS << ',' << weight; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 614 | if (empty()) |
Evan Cheng | 3f32d65 | 2008-06-04 09:18:41 +0000 | [diff] [blame] | 615 | OS << " EMPTY"; |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 616 | else { |
| 617 | OS << " = "; |
| 618 | for (LiveInterval::Ranges::const_iterator I = ranges.begin(), |
Jakob Stoklund Olesen | 014b863 | 2010-06-23 15:34:36 +0000 | [diff] [blame] | 619 | E = ranges.end(); I != E; ++I) { |
| 620 | OS << *I; |
| 621 | assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo"); |
| 622 | } |
Chris Lattner | 38135af | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 623 | } |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 624 | |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 625 | // Print value number info. |
Chris Lattner | 6d8fbef | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 626 | if (getNumValNums()) { |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 627 | OS << " "; |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 628 | unsigned vnum = 0; |
| 629 | for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; |
| 630 | ++i, ++vnum) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 631 | const VNInfo *vni = *i; |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 632 | if (vnum) OS << " "; |
| 633 | OS << vnum << "@"; |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 634 | if (vni->isUnused()) { |
Evan Cheng | 8df7860 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 635 | OS << "x"; |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 636 | } else { |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 637 | OS << vni->def; |
Jakob Stoklund Olesen | a818c07 | 2010-10-05 18:48:57 +0000 | [diff] [blame] | 638 | if (vni->isPHIDef()) |
| 639 | OS << "-phidef"; |
Jakob Stoklund Olesen | d9f6ec9 | 2010-07-13 21:19:05 +0000 | [diff] [blame] | 640 | if (vni->hasPHIKill()) |
| 641 | OS << "-phikill"; |
| 642 | if (vni->hasRedefByEC()) |
| 643 | OS << "-ec"; |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | be4f88a | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 647 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 648 | |
| 649 | void LiveInterval::dump() const { |
David Greene | 5242154 | 2010-01-04 22:41:43 +0000 | [diff] [blame] | 650 | dbgs() << *this << "\n"; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 651 | } |
Jeff Cohen | c21c5ee | 2006-12-15 22:57:14 +0000 | [diff] [blame] | 652 | |
| 653 | |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 654 | void LiveRange::print(raw_ostream &os) const { |
| 655 | os << *this; |
| 656 | } |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 657 | |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 658 | unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) { |
Jakob Stoklund Olesen | 54f32e6 | 2010-10-08 21:19:28 +0000 | [diff] [blame] | 659 | // Create initial equivalence classes. |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 660 | EqClass.clear(); |
| 661 | EqClass.grow(LI->getNumValNums()); |
Jakob Stoklund Olesen | 54f32e6 | 2010-10-08 21:19:28 +0000 | [diff] [blame] | 662 | |
Jakob Stoklund Olesen | 6d30905 | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 663 | const VNInfo *used = 0, *unused = 0; |
| 664 | |
Jakob Stoklund Olesen | 54f32e6 | 2010-10-08 21:19:28 +0000 | [diff] [blame] | 665 | // Determine connections. |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 666 | for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end(); |
| 667 | I != E; ++I) { |
| 668 | const VNInfo *VNI = *I; |
Jakob Stoklund Olesen | 6d30905 | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 669 | // Group all unused values into one class. |
| 670 | if (VNI->isUnused()) { |
| 671 | if (unused) |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 672 | EqClass.join(unused->id, VNI->id); |
Jakob Stoklund Olesen | 6d30905 | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 673 | unused = VNI; |
| 674 | continue; |
| 675 | } |
| 676 | used = VNI; |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 677 | if (VNI->isPHIDef()) { |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 678 | const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def); |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 679 | assert(MBB && "Phi-def has no defining MBB"); |
| 680 | // Connect to values live out of predecessors. |
| 681 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 682 | PE = MBB->pred_end(); PI != PE; ++PI) |
Jakob Stoklund Olesen | 194eb71 | 2011-11-14 01:39:36 +0000 | [diff] [blame] | 683 | if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI))) |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 684 | EqClass.join(VNI->id, PVNI->id); |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 685 | } else { |
| 686 | // Normal value defined by an instruction. Check for two-addr redef. |
| 687 | // FIXME: This could be coincidental. Should we really check for a tied |
| 688 | // operand constraint? |
Jakob Stoklund Olesen | b907e8a | 2010-12-21 00:48:17 +0000 | [diff] [blame] | 689 | // Note that VNI->def may be a use slot for an early clobber def. |
Jakob Stoklund Olesen | 194eb71 | 2011-11-14 01:39:36 +0000 | [diff] [blame] | 690 | if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def)) |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 691 | EqClass.join(VNI->id, UVNI->id); |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
Jakob Stoklund Olesen | 6d30905 | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 694 | |
| 695 | // Lump all the unused values in with the last used value. |
| 696 | if (used && unused) |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 697 | EqClass.join(used->id, unused->id); |
Jakob Stoklund Olesen | 6d30905 | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 698 | |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 699 | EqClass.compress(); |
| 700 | return EqClass.getNumClasses(); |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 703 | void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[], |
| 704 | MachineRegisterInfo &MRI) { |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 705 | assert(LIV[0] && "LIV[0] must be set"); |
| 706 | LiveInterval &LI = *LIV[0]; |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 707 | |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 708 | // Rewrite instructions. |
| 709 | for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg), |
| 710 | RE = MRI.reg_end(); RI != RE;) { |
| 711 | MachineOperand &MO = RI.getOperand(); |
| 712 | MachineInstr *MI = MO.getParent(); |
| 713 | ++RI; |
| 714 | if (MO.isUse() && MO.isUndef()) |
| 715 | continue; |
| 716 | // DBG_VALUE instructions should have been eliminated earlier. |
| 717 | SlotIndex Idx = LIS.getInstructionIndex(MI); |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 718 | Idx = Idx.getRegSlot(MO.isUse()); |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 719 | const VNInfo *VNI = LI.getVNInfoAt(Idx); |
| 720 | assert(VNI && "Interval not live at use."); |
| 721 | MO.setReg(LIV[getEqClass(VNI)]->reg); |
| 722 | } |
| 723 | |
| 724 | // Move runs to new intervals. |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 725 | LiveInterval::iterator J = LI.begin(), E = LI.end(); |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 726 | while (J != E && EqClass[J->valno->id] == 0) |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 727 | ++J; |
| 728 | for (LiveInterval::iterator I = J; I != E; ++I) { |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 729 | if (unsigned eq = EqClass[I->valno->id]) { |
Benjamin Kramer | ccefe32 | 2010-10-09 16:36:44 +0000 | [diff] [blame] | 730 | assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) && |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 731 | "New intervals should be empty"); |
| 732 | LIV[eq]->ranges.push_back(*I); |
| 733 | } else |
| 734 | *J++ = *I; |
| 735 | } |
| 736 | LI.ranges.erase(J, E); |
| 737 | |
| 738 | // Transfer VNInfos to their new owners and renumber them. |
| 739 | unsigned j = 0, e = LI.getNumValNums(); |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 740 | while (j != e && EqClass[j] == 0) |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 741 | ++j; |
| 742 | for (unsigned i = j; i != e; ++i) { |
| 743 | VNInfo *VNI = LI.getValNumInfo(i); |
Jakob Stoklund Olesen | 2254227 | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 744 | if (unsigned eq = EqClass[i]) { |
Jakob Stoklund Olesen | 0253df9 | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 745 | VNI->id = LIV[eq]->getNumValNums(); |
| 746 | LIV[eq]->valnos.push_back(VNI); |
| 747 | } else { |
| 748 | VNI->id = j; |
| 749 | LI.valnos[j++] = VNI; |
| 750 | } |
| 751 | } |
| 752 | LI.valnos.resize(j); |
| 753 | } |