Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 1 | //===-- LiveInterval.cpp - Live Interval Representation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 10 | // This file implements the LiveRange and LiveInterval classes. Given some |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 11 | // numbering of each the machine instructions an interval [i, j) is said to be a |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 12 | // live range for register v if there is no instruction with number j' >= j |
Bob Wilson | 5b56290 | 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 |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 14 | // that v is live at i'. In this implementation ranges can have holes, |
| 15 | // i.e. a range might look like [1,20), [50,65), [1000,1001). Each |
| 16 | // individual segment is represented as an instance of LiveRange::Segment, |
| 17 | // and the whole range is represented as an instance of LiveRange. |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Bill Wendling | 3f6f0fd | 2006-11-28 02:08:17 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveInterval.h" |
Matthias Braun | 858d1df | 2016-05-20 19:46:13 +0000 | [diff] [blame] | 22 | |
Matthias Braun | f9acaca | 2016-05-31 22:38:06 +0000 | [diff] [blame] | 23 | #include "LiveRangeUtils.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "RegisterCoalescer.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/ADT/SmallSet.h" |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Evan Cheng | 085caf1 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Greene | c215532 | 2010-01-04 22:41:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | f26e740 | 2009-07-24 10:47:20 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | fc59e0e | 2004-09-28 02:38:58 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Benjamin Kramer | 51f6096c | 2015-03-23 12:30:58 +0000 | [diff] [blame] | 35 | namespace { |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | // Implementation of various methods necessary for calculation of live ranges. |
| 38 | // The implementation of the methods abstracts from the concrete type of the |
| 39 | // segment collection. |
| 40 | // |
| 41 | // Implementation of the class follows the Template design pattern. The base |
| 42 | // class contains generic algorithms that call collection-specific methods, |
| 43 | // which are provided in concrete subclasses. In order to avoid virtual calls |
| 44 | // these methods are provided by means of C++ template instantiation. |
| 45 | // The base class calls the methods of the subclass through method impl(), |
| 46 | // which casts 'this' pointer to the type of the subclass. |
| 47 | // |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | template <typename ImplT, typename IteratorT, typename CollectionT> |
| 51 | class CalcLiveRangeUtilBase { |
| 52 | protected: |
| 53 | LiveRange *LR; |
| 54 | |
| 55 | protected: |
| 56 | CalcLiveRangeUtilBase(LiveRange *LR) : LR(LR) {} |
| 57 | |
| 58 | public: |
| 59 | typedef LiveRange::Segment Segment; |
| 60 | typedef IteratorT iterator; |
| 61 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 62 | /// A counterpart of LiveRange::createDeadDef: Make sure the range has a |
| 63 | /// value defined at @p Def. |
| 64 | /// If @p ForVNI is null, and there is no value defined at @p Def, a new |
| 65 | /// value will be allocated using @p VNInfoAllocator. |
| 66 | /// If @p ForVNI is null, the return value is the value defined at @p Def, |
| 67 | /// either a pre-existing one, or the one newly created. |
| 68 | /// If @p ForVNI is not null, then @p Def should be the location where |
| 69 | /// @p ForVNI is defined. If the range does not have a value defined at |
| 70 | /// @p Def, the value @p ForVNI will be used instead of allocating a new |
| 71 | /// one. If the range already has a value defined at @p Def, it must be |
| 72 | /// same as @p ForVNI. In either case, @p ForVNI will be the return value. |
| 73 | VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator *VNInfoAllocator, |
| 74 | VNInfo *ForVNI) { |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 75 | assert(!Def.isDead() && "Cannot define a value at the dead slot"); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 76 | assert((!ForVNI || ForVNI->def == Def) && |
| 77 | "If ForVNI is specified, it must match Def"); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 78 | iterator I = impl().find(Def); |
| 79 | if (I == segments().end()) { |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 80 | VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 81 | impl().insertAtEnd(Segment(Def, Def.getDeadSlot(), VNI)); |
| 82 | return VNI; |
| 83 | } |
| 84 | |
| 85 | Segment *S = segmentAt(I); |
| 86 | if (SlotIndex::isSameInstr(Def, S->start)) { |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 87 | assert((!ForVNI || ForVNI == S->valno) && "Value number mismatch"); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 88 | assert(S->valno->def == S->start && "Inconsistent existing value def"); |
| 89 | |
| 90 | // It is possible to have both normal and early-clobber defs of the same |
| 91 | // register on an instruction. It doesn't make a lot of sense, but it is |
| 92 | // possible to specify in inline assembly. |
| 93 | // |
| 94 | // Just convert everything to early-clobber. |
| 95 | Def = std::min(Def, S->start); |
| 96 | if (Def != S->start) |
| 97 | S->start = S->valno->def = Def; |
| 98 | return S->valno; |
| 99 | } |
| 100 | assert(SlotIndex::isEarlierInstr(Def, S->start) && "Already live at def"); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 101 | VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 102 | segments().insert(I, Segment(Def, Def.getDeadSlot(), VNI)); |
| 103 | return VNI; |
| 104 | } |
| 105 | |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 106 | VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Use) { |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 107 | if (segments().empty()) |
| 108 | return nullptr; |
| 109 | iterator I = |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 110 | impl().findInsertPos(Segment(Use.getPrevSlot(), Use, nullptr)); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 111 | if (I == segments().begin()) |
| 112 | return nullptr; |
| 113 | --I; |
| 114 | if (I->end <= StartIdx) |
| 115 | return nullptr; |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 116 | if (I->end < Use) |
| 117 | extendSegmentEndTo(I, Use); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 118 | return I->valno; |
| 119 | } |
| 120 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 121 | std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs, |
| 122 | SlotIndex StartIdx, SlotIndex Use) { |
| 123 | if (segments().empty()) |
| 124 | return std::make_pair(nullptr, false); |
| 125 | SlotIndex BeforeUse = Use.getPrevSlot(); |
| 126 | iterator I = impl().findInsertPos(Segment(BeforeUse, Use, nullptr)); |
| 127 | if (I == segments().begin()) |
| 128 | return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse)); |
| 129 | --I; |
| 130 | if (I->end <= StartIdx) |
| 131 | return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse)); |
| 132 | if (I->end < Use) { |
| 133 | if (LR->isUndefIn(Undefs, I->end, BeforeUse)) |
| 134 | return std::make_pair(nullptr, true); |
| 135 | extendSegmentEndTo(I, Use); |
| 136 | } |
| 137 | return std::make_pair(I->valno, false); |
| 138 | } |
| 139 | |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 140 | /// This method is used when we want to extend the segment specified |
| 141 | /// by I to end at the specified endpoint. To do this, we should |
| 142 | /// merge and eliminate all segments that this will overlap |
| 143 | /// with. The iterator is not invalidated. |
| 144 | void extendSegmentEndTo(iterator I, SlotIndex NewEnd) { |
| 145 | assert(I != segments().end() && "Not a valid segment!"); |
| 146 | Segment *S = segmentAt(I); |
| 147 | VNInfo *ValNo = I->valno; |
| 148 | |
| 149 | // Search for the first segment that we can't merge with. |
| 150 | iterator MergeTo = std::next(I); |
| 151 | for (; MergeTo != segments().end() && NewEnd >= MergeTo->end; ++MergeTo) |
| 152 | assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); |
| 153 | |
| 154 | // If NewEnd was in the middle of a segment, make sure to get its endpoint. |
| 155 | S->end = std::max(NewEnd, std::prev(MergeTo)->end); |
| 156 | |
| 157 | // If the newly formed segment now touches the segment after it and if they |
| 158 | // have the same value number, merge the two segments into one segment. |
| 159 | if (MergeTo != segments().end() && MergeTo->start <= I->end && |
| 160 | MergeTo->valno == ValNo) { |
| 161 | S->end = MergeTo->end; |
| 162 | ++MergeTo; |
| 163 | } |
| 164 | |
| 165 | // Erase any dead segments. |
| 166 | segments().erase(std::next(I), MergeTo); |
| 167 | } |
| 168 | |
| 169 | /// This method is used when we want to extend the segment specified |
| 170 | /// by I to start at the specified endpoint. To do this, we should |
| 171 | /// merge and eliminate all segments that this will overlap with. |
| 172 | iterator extendSegmentStartTo(iterator I, SlotIndex NewStart) { |
| 173 | assert(I != segments().end() && "Not a valid segment!"); |
| 174 | Segment *S = segmentAt(I); |
| 175 | VNInfo *ValNo = I->valno; |
| 176 | |
| 177 | // Search for the first segment that we can't merge with. |
| 178 | iterator MergeTo = I; |
| 179 | do { |
| 180 | if (MergeTo == segments().begin()) { |
| 181 | S->start = NewStart; |
| 182 | segments().erase(MergeTo, I); |
| 183 | return I; |
| 184 | } |
| 185 | assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); |
| 186 | --MergeTo; |
| 187 | } while (NewStart <= MergeTo->start); |
| 188 | |
| 189 | // If we start in the middle of another segment, just delete a range and |
| 190 | // extend that segment. |
| 191 | if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) { |
| 192 | segmentAt(MergeTo)->end = S->end; |
| 193 | } else { |
| 194 | // Otherwise, extend the segment right after. |
| 195 | ++MergeTo; |
| 196 | Segment *MergeToSeg = segmentAt(MergeTo); |
| 197 | MergeToSeg->start = NewStart; |
| 198 | MergeToSeg->end = S->end; |
| 199 | } |
| 200 | |
| 201 | segments().erase(std::next(MergeTo), std::next(I)); |
| 202 | return MergeTo; |
| 203 | } |
| 204 | |
| 205 | iterator addSegment(Segment S) { |
| 206 | SlotIndex Start = S.start, End = S.end; |
| 207 | iterator I = impl().findInsertPos(S); |
| 208 | |
| 209 | // If the inserted segment starts in the middle or right at the end of |
| 210 | // another segment, just extend that segment to contain the segment of S. |
| 211 | if (I != segments().begin()) { |
| 212 | iterator B = std::prev(I); |
| 213 | if (S.valno == B->valno) { |
| 214 | if (B->start <= Start && B->end >= Start) { |
| 215 | extendSegmentEndTo(B, End); |
| 216 | return B; |
| 217 | } |
| 218 | } else { |
| 219 | // Check to make sure that we are not overlapping two live segments with |
| 220 | // different valno's. |
| 221 | assert(B->end <= Start && |
| 222 | "Cannot overlap two segments with differing ValID's" |
| 223 | " (did you def the same reg twice in a MachineInstr?)"); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Otherwise, if this segment ends in the middle of, or right next |
| 228 | // to, another segment, merge it into that segment. |
| 229 | if (I != segments().end()) { |
| 230 | if (S.valno == I->valno) { |
| 231 | if (I->start <= End) { |
| 232 | I = extendSegmentStartTo(I, Start); |
| 233 | |
| 234 | // If S is a complete superset of a segment, we may need to grow its |
| 235 | // endpoint as well. |
| 236 | if (End > I->end) |
| 237 | extendSegmentEndTo(I, End); |
| 238 | return I; |
| 239 | } |
| 240 | } else { |
| 241 | // Check to make sure that we are not overlapping two live segments with |
| 242 | // different valno's. |
| 243 | assert(I->start >= End && |
| 244 | "Cannot overlap two segments with differing ValID's"); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Otherwise, this is just a new segment that doesn't interact with |
| 249 | // anything. |
| 250 | // Insert it. |
| 251 | return segments().insert(I, S); |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | ImplT &impl() { return *static_cast<ImplT *>(this); } |
| 256 | |
| 257 | CollectionT &segments() { return impl().segmentsColl(); } |
| 258 | |
| 259 | Segment *segmentAt(iterator I) { return const_cast<Segment *>(&(*I)); } |
| 260 | }; |
| 261 | |
| 262 | //===----------------------------------------------------------------------===// |
| 263 | // Instantiation of the methods for calculation of live ranges |
| 264 | // based on a segment vector. |
| 265 | //===----------------------------------------------------------------------===// |
| 266 | |
| 267 | class CalcLiveRangeUtilVector; |
| 268 | typedef CalcLiveRangeUtilBase<CalcLiveRangeUtilVector, LiveRange::iterator, |
| 269 | LiveRange::Segments> CalcLiveRangeUtilVectorBase; |
| 270 | |
| 271 | class CalcLiveRangeUtilVector : public CalcLiveRangeUtilVectorBase { |
| 272 | public: |
| 273 | CalcLiveRangeUtilVector(LiveRange *LR) : CalcLiveRangeUtilVectorBase(LR) {} |
| 274 | |
| 275 | private: |
| 276 | friend CalcLiveRangeUtilVectorBase; |
| 277 | |
| 278 | LiveRange::Segments &segmentsColl() { return LR->segments; } |
| 279 | |
| 280 | void insertAtEnd(const Segment &S) { LR->segments.push_back(S); } |
| 281 | |
| 282 | iterator find(SlotIndex Pos) { return LR->find(Pos); } |
| 283 | |
| 284 | iterator findInsertPos(Segment S) { |
| 285 | return std::upper_bound(LR->begin(), LR->end(), S.start); |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | //===----------------------------------------------------------------------===// |
| 290 | // Instantiation of the methods for calculation of live ranges |
| 291 | // based on a segment set. |
| 292 | //===----------------------------------------------------------------------===// |
| 293 | |
| 294 | class CalcLiveRangeUtilSet; |
| 295 | typedef CalcLiveRangeUtilBase<CalcLiveRangeUtilSet, |
| 296 | LiveRange::SegmentSet::iterator, |
| 297 | LiveRange::SegmentSet> CalcLiveRangeUtilSetBase; |
| 298 | |
| 299 | class CalcLiveRangeUtilSet : public CalcLiveRangeUtilSetBase { |
| 300 | public: |
| 301 | CalcLiveRangeUtilSet(LiveRange *LR) : CalcLiveRangeUtilSetBase(LR) {} |
| 302 | |
| 303 | private: |
| 304 | friend CalcLiveRangeUtilSetBase; |
| 305 | |
| 306 | LiveRange::SegmentSet &segmentsColl() { return *LR->segmentSet; } |
| 307 | |
| 308 | void insertAtEnd(const Segment &S) { |
| 309 | LR->segmentSet->insert(LR->segmentSet->end(), S); |
| 310 | } |
| 311 | |
| 312 | iterator find(SlotIndex Pos) { |
| 313 | iterator I = |
| 314 | LR->segmentSet->upper_bound(Segment(Pos, Pos.getNextSlot(), nullptr)); |
| 315 | if (I == LR->segmentSet->begin()) |
| 316 | return I; |
| 317 | iterator PrevI = std::prev(I); |
| 318 | if (Pos < (*PrevI).end) |
| 319 | return PrevI; |
| 320 | return I; |
| 321 | } |
| 322 | |
| 323 | iterator findInsertPos(Segment S) { |
| 324 | iterator I = LR->segmentSet->upper_bound(S); |
| 325 | if (I != LR->segmentSet->end() && !(S.start < *I)) |
| 326 | ++I; |
| 327 | return I; |
| 328 | } |
| 329 | }; |
Benjamin Kramer | 51f6096c | 2015-03-23 12:30:58 +0000 | [diff] [blame] | 330 | } // namespace |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 331 | |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | // LiveRange methods |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 336 | LiveRange::iterator LiveRange::find(SlotIndex Pos) { |
Jakob Stoklund Olesen | dae1dc1 | 2011-03-12 01:50:35 +0000 | [diff] [blame] | 337 | // This algorithm is basically std::upper_bound. |
| 338 | // Unfortunately, std::upper_bound cannot be used with mixed types until we |
| 339 | // adopt C++0x. Many libraries can do it, but not all. |
| 340 | if (empty() || Pos >= endIndex()) |
| 341 | return end(); |
| 342 | iterator I = begin(); |
Matthias Braun | 305ef7f | 2013-09-06 16:44:32 +0000 | [diff] [blame] | 343 | size_t Len = size(); |
Jakob Stoklund Olesen | dae1dc1 | 2011-03-12 01:50:35 +0000 | [diff] [blame] | 344 | do { |
| 345 | size_t Mid = Len >> 1; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 346 | if (Pos < I[Mid].end) { |
Jakob Stoklund Olesen | dae1dc1 | 2011-03-12 01:50:35 +0000 | [diff] [blame] | 347 | Len = Mid; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 348 | } else { |
| 349 | I += Mid + 1; |
| 350 | Len -= Mid + 1; |
| 351 | } |
Jakob Stoklund Olesen | dae1dc1 | 2011-03-12 01:50:35 +0000 | [diff] [blame] | 352 | } while (Len); |
| 353 | return I; |
Jakob Stoklund Olesen | 55d738e2 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 356 | VNInfo *LiveRange::createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc) { |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 357 | // Use the segment set, if it is available. |
| 358 | if (segmentSet != nullptr) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 359 | return CalcLiveRangeUtilSet(this).createDeadDef(Def, &VNIAlloc, nullptr); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 360 | // Otherwise use the segment vector. |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 361 | return CalcLiveRangeUtilVector(this).createDeadDef(Def, &VNIAlloc, nullptr); |
| 362 | } |
| 363 | |
| 364 | VNInfo *LiveRange::createDeadDef(VNInfo *VNI) { |
| 365 | // Use the segment set, if it is available. |
| 366 | if (segmentSet != nullptr) |
| 367 | return CalcLiveRangeUtilSet(this).createDeadDef(VNI->def, nullptr, VNI); |
| 368 | // Otherwise use the segment vector. |
| 369 | return CalcLiveRangeUtilVector(this).createDeadDef(VNI->def, nullptr, VNI); |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 372 | // overlaps - Return true if the intersection of the two live ranges is |
Chris Lattner | cb0c965 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 373 | // not empty. |
| 374 | // |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 375 | // An example for overlaps(): |
| 376 | // |
| 377 | // 0: A = ... |
| 378 | // 4: B = ... |
| 379 | // 8: C = A + B ;; last use of A |
| 380 | // |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 381 | // The live ranges should look like: |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 382 | // |
| 383 | // A = [3, 11) |
| 384 | // B = [7, x) |
| 385 | // C = [11, y) |
| 386 | // |
| 387 | // A->overlaps(C) should return false since we want to be able to join |
| 388 | // A and C. |
Chris Lattner | cb0c965 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 389 | // |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 390 | bool LiveRange::overlapsFrom(const LiveRange& other, |
| 391 | const_iterator StartPos) const { |
| 392 | assert(!empty() && "empty range"); |
Chris Lattner | cb0c965 | 2004-11-18 03:47:34 +0000 | [diff] [blame] | 393 | const_iterator i = begin(); |
| 394 | const_iterator ie = end(); |
| 395 | const_iterator j = StartPos; |
| 396 | const_iterator je = other.end(); |
| 397 | |
| 398 | assert((StartPos->start <= i->start || StartPos == other.begin()) && |
Chris Lattner | 7598c31 | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 399 | StartPos != other.end() && "Bogus start position hint!"); |
Chris Lattner | ccc75d4f | 2004-07-25 07:11:19 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 401 | if (i->start < j->start) { |
Chris Lattner | 2fcc5e4 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 402 | i = std::upper_bound(i, ie, j->start); |
Matthias Braun | 305ef7f | 2013-09-06 16:44:32 +0000 | [diff] [blame] | 403 | if (i != begin()) --i; |
Chris Lattner | 2fcc5e4 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 404 | } else if (j->start < i->start) { |
Chris Lattner | 5684f48 | 2004-12-04 01:22:09 +0000 | [diff] [blame] | 405 | ++StartPos; |
| 406 | if (StartPos != other.end() && StartPos->start <= i->start) { |
| 407 | assert(StartPos < other.end() && i < end()); |
Chris Lattner | 7598c31 | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 408 | j = std::upper_bound(j, je, i->start); |
Matthias Braun | 305ef7f | 2013-09-06 16:44:32 +0000 | [diff] [blame] | 409 | if (j != other.begin()) --j; |
Chris Lattner | 7598c31 | 2004-11-18 04:02:11 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | 2fcc5e4 | 2004-07-23 18:40:00 +0000 | [diff] [blame] | 411 | } else { |
| 412 | return true; |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | e3b9cb9 | 2004-11-18 05:28:21 +0000 | [diff] [blame] | 415 | if (j == je) return false; |
| 416 | |
| 417 | while (i != ie) { |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 418 | if (i->start > j->start) { |
Alkis Evlogimenos | cf72e7f | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 419 | std::swap(i, j); |
| 420 | std::swap(ie, je); |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 421 | } |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 422 | |
| 423 | if (i->end > j->start) |
| 424 | return true; |
| 425 | ++i; |
| 426 | } |
| 427 | |
| 428 | return false; |
| 429 | } |
| 430 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 431 | bool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP, |
| 432 | const SlotIndexes &Indexes) const { |
| 433 | assert(!empty() && "empty range"); |
Jakob Stoklund Olesen | 866908c | 2012-09-06 18:15:23 +0000 | [diff] [blame] | 434 | if (Other.empty()) |
| 435 | return false; |
| 436 | |
| 437 | // Use binary searches to find initial positions. |
| 438 | const_iterator I = find(Other.beginIndex()); |
| 439 | const_iterator IE = end(); |
| 440 | if (I == IE) |
| 441 | return false; |
| 442 | const_iterator J = Other.find(I->start); |
| 443 | const_iterator JE = Other.end(); |
| 444 | if (J == JE) |
| 445 | return false; |
| 446 | |
| 447 | for (;;) { |
| 448 | // J has just been advanced to satisfy: |
| 449 | assert(J->end >= I->start); |
| 450 | // Check for an overlap. |
| 451 | if (J->start < I->end) { |
| 452 | // I and J are overlapping. Find the later start. |
| 453 | SlotIndex Def = std::max(I->start, J->start); |
| 454 | // Allow the overlap if Def is a coalescable copy. |
| 455 | if (Def.isBlock() || |
| 456 | !CP.isCoalescable(Indexes.getInstructionFromIndex(Def))) |
| 457 | return true; |
| 458 | } |
| 459 | // Advance the iterator that ends first to check for more overlaps. |
| 460 | if (J->end > I->end) { |
| 461 | std::swap(I, J); |
| 462 | std::swap(IE, JE); |
| 463 | } |
| 464 | // Advance J until J->end >= I->start. |
| 465 | do |
| 466 | if (++J == JE) |
| 467 | return false; |
| 468 | while (J->end < I->start); |
| 469 | } |
| 470 | } |
| 471 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 472 | /// overlaps - Return true if the live range overlaps an interval specified |
Evan Cheng | b685be0 | 2009-04-18 08:52:15 +0000 | [diff] [blame] | 473 | /// by [Start, End). |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 474 | bool LiveRange::overlaps(SlotIndex Start, SlotIndex End) const { |
Evan Cheng | b685be0 | 2009-04-18 08:52:15 +0000 | [diff] [blame] | 475 | assert(Start < End && "Invalid range"); |
Jakob Stoklund Olesen | b43455f | 2010-07-13 19:42:20 +0000 | [diff] [blame] | 476 | const_iterator I = std::lower_bound(begin(), end(), End); |
| 477 | return I != begin() && (--I)->end > Start; |
Evan Cheng | b685be0 | 2009-04-18 08:52:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Matthias Braun | e62c207 | 2014-12-10 01:12:06 +0000 | [diff] [blame] | 480 | bool LiveRange::covers(const LiveRange &Other) const { |
| 481 | if (empty()) |
| 482 | return Other.empty(); |
| 483 | |
| 484 | const_iterator I = begin(); |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 485 | for (const Segment &O : Other.segments) { |
| 486 | I = advanceTo(I, O.start); |
| 487 | if (I == end() || I->start > O.start) |
Matthias Braun | e62c207 | 2014-12-10 01:12:06 +0000 | [diff] [blame] | 488 | return false; |
| 489 | |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 490 | // Check adjacent live segments and see if we can get behind O.end. |
| 491 | while (I->end < O.end) { |
Matthias Braun | e62c207 | 2014-12-10 01:12:06 +0000 | [diff] [blame] | 492 | const_iterator Last = I; |
| 493 | // Get next segment and abort if it was not adjacent. |
| 494 | ++I; |
| 495 | if (I == end() || Last->end != I->start) |
| 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | return true; |
| 500 | } |
Lang Hames | 2e3f20b | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 501 | |
| 502 | /// ValNo is dead, remove it. If it is the largest value number, just nuke it |
| 503 | /// (and any other deleted values neighboring it), otherwise mark it as ~1U so |
| 504 | /// it can be nuked later. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 505 | void LiveRange::markValNoForDeletion(VNInfo *ValNo) { |
Lang Hames | 2e3f20b | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 506 | if (ValNo->id == getNumValNums()-1) { |
| 507 | do { |
| 508 | valnos.pop_back(); |
| 509 | } while (!valnos.empty() && valnos.back()->isUnused()); |
| 510 | } else { |
Jakob Stoklund Olesen | daae19f | 2012-08-03 20:59:32 +0000 | [diff] [blame] | 511 | ValNo->markUnused(); |
Lang Hames | 2e3f20b | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Jakob Stoklund Olesen | 8c0f693 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 515 | /// RenumberValues - Renumber all values in order of appearance and delete the |
| 516 | /// remaining unused values. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 517 | void LiveRange::RenumberValues() { |
Jakob Stoklund Olesen | 8c0f693 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 518 | SmallPtrSet<VNInfo*, 8> Seen; |
| 519 | valnos.clear(); |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 520 | for (const Segment &S : segments) { |
| 521 | VNInfo *VNI = S.valno; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 522 | if (!Seen.insert(VNI).second) |
Jakob Stoklund Olesen | 8c0f693 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 523 | continue; |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 524 | assert(!VNI->isUnused() && "Unused valno used by live segment"); |
Jakob Stoklund Olesen | 8c0f693 | 2010-08-06 18:46:59 +0000 | [diff] [blame] | 525 | VNI->id = (unsigned)valnos.size(); |
| 526 | valnos.push_back(VNI); |
| 527 | } |
| 528 | } |
| 529 | |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 530 | void LiveRange::addSegmentToSet(Segment S) { |
| 531 | CalcLiveRangeUtilSet(this).addSegment(S); |
Chris Lattner | b4acba4 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 534 | LiveRange::iterator LiveRange::addSegment(Segment S) { |
| 535 | // Use the segment set, if it is available. |
| 536 | if (segmentSet != nullptr) { |
| 537 | addSegmentToSet(S); |
| 538 | return end(); |
Chris Lattner | b4acba4 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 539 | } |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 540 | // Otherwise use the segment vector. |
| 541 | return CalcLiveRangeUtilVector(this).addSegment(S); |
Chris Lattner | b4acba4 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Matthias Braun | dbcca0d | 2014-12-24 02:11:51 +0000 | [diff] [blame] | 544 | void LiveRange::append(const Segment S) { |
| 545 | // Check that the segment belongs to the back of the list. |
| 546 | assert(segments.empty() || segments.back().end <= S.start); |
| 547 | segments.push_back(S); |
| 548 | } |
| 549 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 550 | std::pair<VNInfo*,bool> LiveRange::extendInBlock(ArrayRef<SlotIndex> Undefs, |
| 551 | SlotIndex StartIdx, SlotIndex Kill) { |
| 552 | // Use the segment set, if it is available. |
| 553 | if (segmentSet != nullptr) |
| 554 | return CalcLiveRangeUtilSet(this).extendInBlock(Undefs, StartIdx, Kill); |
| 555 | // Otherwise use the segment vector. |
| 556 | return CalcLiveRangeUtilVector(this).extendInBlock(Undefs, StartIdx, Kill); |
| 557 | } |
| 558 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 559 | VNInfo *LiveRange::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) { |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 560 | // Use the segment set, if it is available. |
| 561 | if (segmentSet != nullptr) |
| 562 | return CalcLiveRangeUtilSet(this).extendInBlock(StartIdx, Kill); |
| 563 | // Otherwise use the segment vector. |
| 564 | return CalcLiveRangeUtilVector(this).extendInBlock(StartIdx, Kill); |
Jakob Stoklund Olesen | 9e326a8 | 2011-03-02 00:06:15 +0000 | [diff] [blame] | 565 | } |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 566 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 567 | /// Remove the specified segment from this range. Note that the segment must |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 568 | /// be in a single Segment in its entirety. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 569 | void LiveRange::removeSegment(SlotIndex Start, SlotIndex End, |
| 570 | bool RemoveDeadValNo) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 571 | // Find the Segment containing this span. |
Matthias Braun | 305ef7f | 2013-09-06 16:44:32 +0000 | [diff] [blame] | 572 | iterator I = find(Start); |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 573 | assert(I != end() && "Segment is not in range!"); |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 574 | assert(I->containsInterval(Start, End) |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 575 | && "Segment is not entirely in range!"); |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 576 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 577 | // If the span we are removing is at the start of the Segment, adjust it. |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 578 | VNInfo *ValNo = I->valno; |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 579 | if (I->start == Start) { |
Evan Cheng | 05cc486 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 580 | if (I->end == End) { |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 581 | if (RemoveDeadValNo) { |
| 582 | // Check if val# is dead. |
| 583 | bool isDead = true; |
| 584 | for (const_iterator II = begin(), EE = end(); II != EE; ++II) |
| 585 | if (II != I && II->valno == ValNo) { |
| 586 | isDead = false; |
| 587 | break; |
Jakob Stoklund Olesen | 55d738e2 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 588 | } |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 589 | if (isDead) { |
Lang Hames | 2e3f20b | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 590 | // Now that ValNo is dead, remove it. |
| 591 | markValNoForDeletion(ValNo); |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 595 | segments.erase(I); // Removed the whole Segment. |
Evan Cheng | 05cc486 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 596 | } else |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 597 | I->start = End; |
| 598 | return; |
| 599 | } |
| 600 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 601 | // Otherwise if the span we are removing is at the end of the Segment, |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 602 | // adjust the other way. |
| 603 | if (I->end == End) { |
Chris Lattner | af7e898 | 2004-07-25 05:43:53 +0000 | [diff] [blame] | 604 | I->end = Start; |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 608 | // Otherwise, we are splitting the Segment into two pieces. |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 609 | SlotIndex OldEnd = I->end; |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 610 | I->end = Start; // Trim the old segment. |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 611 | |
| 612 | // Insert the new one. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 613 | segments.insert(std::next(I), Segment(End, OldEnd, ValNo)); |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 616 | /// removeValNo - Remove all the segments defined by the specified value#. |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 617 | /// Also remove the value# from value# list. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 618 | void LiveRange::removeValNo(VNInfo *ValNo) { |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 619 | if (empty()) return; |
David Majnemer | c700490 | 2016-08-12 04:32:37 +0000 | [diff] [blame] | 620 | segments.erase(remove_if(*this, [ValNo](const Segment &S) { |
Benjamin Kramer | 4c5dcb0 | 2015-02-28 20:14:27 +0000 | [diff] [blame] | 621 | return S.valno == ValNo; |
| 622 | }), end()); |
Lang Hames | 2e3f20b | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 623 | // Now that ValNo is dead, remove it. |
| 624 | markValNoForDeletion(ValNo); |
Evan Cheng | 47f462a | 2008-02-13 02:48:26 +0000 | [diff] [blame] | 625 | } |
Lang Hames | 3fffe62 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 626 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 627 | void LiveRange::join(LiveRange &Other, |
| 628 | const int *LHSValNoAssignments, |
| 629 | const int *RHSValNoAssignments, |
| 630 | SmallVectorImpl<VNInfo *> &NewVNInfo) { |
Chandler Carruth | ac766b9 | 2012-07-10 05:06:03 +0000 | [diff] [blame] | 631 | verify(); |
| 632 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 633 | // Determine if any of our values are mapped. This is uncommon, so we want |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 634 | // to avoid the range scan if not. |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 635 | bool MustMapCurValNos = false; |
Evan Cheng | 2089a21 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 636 | unsigned NumVals = getNumValNums(); |
| 637 | unsigned NumNewVals = NewVNInfo.size(); |
| 638 | for (unsigned i = 0; i != NumVals; ++i) { |
| 639 | unsigned LHSValID = LHSValNoAssignments[i]; |
| 640 | if (i != LHSValID || |
Lang Hames | 4d04f75 | 2012-02-02 06:55:45 +0000 | [diff] [blame] | 641 | (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) { |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 642 | MustMapCurValNos = true; |
Lang Hames | 4d04f75 | 2012-02-02 06:55:45 +0000 | [diff] [blame] | 643 | break; |
| 644 | } |
Chris Lattner | d9bbbb8 | 2004-07-24 03:41:50 +0000 | [diff] [blame] | 645 | } |
Evan Cheng | 1ad4a61 | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 646 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 647 | // If we have to apply a mapping to our base range assignment, rewrite it now. |
Jakob Stoklund Olesen | 4976d0d | 2012-09-27 21:05:59 +0000 | [diff] [blame] | 648 | if (MustMapCurValNos && !empty()) { |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 649 | // Map the first live range. |
Lang Hames | 3a20bc3 | 2012-02-02 05:37:34 +0000 | [diff] [blame] | 650 | |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 651 | iterator OutIt = begin(); |
Evan Cheng | 1ad4a61 | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 652 | OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 653 | for (iterator I = std::next(OutIt), E = end(); I != E; ++I) { |
Lang Hames | 3a20bc3 | 2012-02-02 05:37:34 +0000 | [diff] [blame] | 654 | VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]]; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 655 | assert(nextValNo && "Huh?"); |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 657 | // If this live range has the same value # as its immediate predecessor, |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 658 | // and if they are neighbors, remove one Segment. This happens when we |
Lang Hames | 3a20bc3 | 2012-02-02 05:37:34 +0000 | [diff] [blame] | 659 | // have [0,4:0)[4,7:1) and map 0/1 onto the same value #. |
| 660 | if (OutIt->valno == nextValNo && OutIt->end == I->start) { |
| 661 | OutIt->end = I->end; |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 662 | } else { |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 663 | // Didn't merge. Move OutIt to the next segment, |
Lang Hames | 3a20bc3 | 2012-02-02 05:37:34 +0000 | [diff] [blame] | 664 | ++OutIt; |
| 665 | OutIt->valno = nextValNo; |
| 666 | if (OutIt != I) { |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 667 | OutIt->start = I->start; |
| 668 | OutIt->end = I->end; |
| 669 | } |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 672 | // If we merge some segments, chop off the end. |
Lang Hames | 3a20bc3 | 2012-02-02 05:37:34 +0000 | [diff] [blame] | 673 | ++OutIt; |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 674 | segments.erase(OutIt, end()); |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 675 | } |
Evan Cheng | 05cc486 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 676 | |
Jakob Stoklund Olesen | 1744fd8 | 2013-02-20 23:51:10 +0000 | [diff] [blame] | 677 | // Rewrite Other values before changing the VNInfo ids. |
| 678 | // This can leave Other in an invalid state because we're not coalescing |
| 679 | // touching segments that now have identical values. That's OK since Other is |
| 680 | // not supposed to be valid after calling join(); |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 681 | for (Segment &S : Other.segments) |
| 682 | S.valno = NewVNInfo[RHSValNoAssignments[S.valno->id]]; |
Evan Cheng | 1ad4a61 | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 683 | |
| 684 | // Update val# info. Renumber them and make sure they all belong to this |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 685 | // LiveRange now. Also remove dead val#'s. |
Evan Cheng | db53aef | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 686 | unsigned NumValNos = 0; |
| 687 | for (unsigned i = 0; i < NumNewVals; ++i) { |
Evan Cheng | 1ad4a61 | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 688 | VNInfo *VNI = NewVNInfo[i]; |
Evan Cheng | db53aef | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 689 | if (VNI) { |
Evan Cheng | 7e09994 | 2009-04-28 06:24:09 +0000 | [diff] [blame] | 690 | if (NumValNos >= NumVals) |
Evan Cheng | db53aef | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 691 | valnos.push_back(VNI); |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 692 | else |
Evan Cheng | db53aef | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 693 | valnos[NumValNos] = VNI; |
| 694 | VNI->id = NumValNos++; // Renumber val#. |
Evan Cheng | 2089a21 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
Evan Cheng | 2089a21 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 697 | if (NumNewVals < NumVals) |
| 698 | valnos.resize(NumNewVals); // shrinkify |
Evan Cheng | 05cc486 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 699 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 700 | // Okay, now insert the RHS live segments into the LHS. |
Jakob Stoklund Olesen | 623d832 | 2013-02-20 18:18:15 +0000 | [diff] [blame] | 701 | LiveRangeUpdater Updater(this); |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 702 | for (Segment &S : Other.segments) |
| 703 | Updater.add(S); |
Chandler Carruth | e18614d | 2012-07-10 05:16:17 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 706 | /// Merge all of the segments in RHS into this live range as the specified |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 707 | /// value number. The segments in RHS are allowed to overlap with segments in |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 708 | /// the current range, but only if the overlapping segments have the |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 709 | /// specified value number. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 710 | void LiveRange::MergeSegmentsInAsValue(const LiveRange &RHS, |
| 711 | VNInfo *LHSValNo) { |
Jakob Stoklund Olesen | 623d832 | 2013-02-20 18:18:15 +0000 | [diff] [blame] | 712 | LiveRangeUpdater Updater(this); |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 713 | for (const Segment &S : RHS.segments) |
| 714 | Updater.add(S.start, S.end, LHSValNo); |
Chris Lattner | 5a56d30 | 2006-09-02 05:26:59 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 717 | /// MergeValueInAsValue - Merge all of the live segments of a specific val# |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 718 | /// in RHS into this live range as the specified value number. |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 719 | /// The segments in RHS are allowed to overlap with segments in the |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 720 | /// current range, it will replace the value numbers of the overlaped |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 721 | /// segments with the specified value number. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 722 | void LiveRange::MergeValueInAsValue(const LiveRange &RHS, |
| 723 | const VNInfo *RHSValNo, |
| 724 | VNInfo *LHSValNo) { |
Jakob Stoklund Olesen | 623d832 | 2013-02-20 18:18:15 +0000 | [diff] [blame] | 725 | LiveRangeUpdater Updater(this); |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 726 | for (const Segment &S : RHS.segments) |
| 727 | if (S.valno == RHSValNo) |
| 728 | Updater.add(S.start, S.end, LHSValNo); |
Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 731 | /// MergeValueNumberInto - This method is called when two value nubmers |
| 732 | /// are found to be equivalent. This eliminates V1, replacing all |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 733 | /// segments with the V1 value number with the V2 value number. This can |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 734 | /// cause merging of V1/V2 values numbers and compaction of the value space. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 735 | VNInfo *LiveRange::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 736 | assert(V1 != V2 && "Identical value#'s are always equivalent!"); |
| 737 | |
| 738 | // This code actually merges the (numerically) larger value number into the |
| 739 | // smaller value number, which is likely to allow us to compactify the value |
| 740 | // space. The only thing we have to be careful of is to preserve the |
| 741 | // instruction that defines the result value. |
| 742 | |
| 743 | // Make sure V2 is smaller than V1. |
Evan Cheng | 1ad4a61 | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 744 | if (V1->id < V2->id) { |
Lang Hames | 3b90d97 | 2009-08-10 23:43:28 +0000 | [diff] [blame] | 745 | V1->copyFrom(*V2); |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 746 | std::swap(V1, V2); |
| 747 | } |
| 748 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 749 | // Merge V1 segments into V2. |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 750 | for (iterator I = begin(); I != end(); ) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 751 | iterator S = I++; |
| 752 | if (S->valno != V1) continue; // Not a V1 Segment. |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 753 | |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 754 | // Okay, we found a V1 live range. If it had a previous, touching, V2 live |
| 755 | // range, extend it. |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 756 | if (S != begin()) { |
| 757 | iterator Prev = S-1; |
| 758 | if (Prev->valno == V2 && Prev->end == S->start) { |
| 759 | Prev->end = S->end; |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 760 | |
| 761 | // Erase this live-range. |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 762 | segments.erase(S); |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 763 | I = Prev+1; |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 764 | S = Prev; |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 767 | |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 768 | // Okay, now we have a V1 or V2 live range that is maximally merged forward. |
| 769 | // Ensure that it is a V2 live-range. |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 770 | S->valno = V2; |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 771 | |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 772 | // If we can merge it into later V2 segments, do so now. We ignore any |
| 773 | // following V1 segments, as they will be merged in subsequent iterations |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 774 | // of the loop. |
| 775 | if (I != end()) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 776 | if (I->start == S->end && I->valno == V2) { |
| 777 | S->end = I->end; |
| 778 | segments.erase(I); |
| 779 | I = S+1; |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 783 | |
Lang Hames | 2e3f20b | 2010-07-26 01:49:41 +0000 | [diff] [blame] | 784 | // Now that V1 is dead, remove it. |
| 785 | markValNoForDeletion(V1); |
Jakob Stoklund Olesen | 073cd80 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 786 | |
Owen Anderson | 4eda2cb | 2009-02-02 22:42:01 +0000 | [diff] [blame] | 787 | return V2; |
Chris Lattner | bdf1210 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 790 | void LiveRange::flushSegmentSet() { |
| 791 | assert(segmentSet != nullptr && "segment set must have been created"); |
| 792 | assert( |
| 793 | segments.empty() && |
| 794 | "segment set can be used only initially before switching to the array"); |
| 795 | segments.append(segmentSet->begin(), segmentSet->end()); |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 796 | segmentSet = nullptr; |
| 797 | verify(); |
| 798 | } |
| 799 | |
Andrew Kaylor | 1224488 | 2016-02-08 22:52:51 +0000 | [diff] [blame] | 800 | bool LiveRange::isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const { |
| 801 | ArrayRef<SlotIndex>::iterator SlotI = Slots.begin(); |
| 802 | ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); |
| 803 | |
| 804 | // If there are no regmask slots, we have nothing to search. |
| 805 | if (SlotI == SlotE) |
| 806 | return false; |
| 807 | |
| 808 | // Start our search at the first segment that ends after the first slot. |
| 809 | const_iterator SegmentI = find(*SlotI); |
| 810 | const_iterator SegmentE = end(); |
| 811 | |
| 812 | // If there are no segments that end after the first slot, we're done. |
| 813 | if (SegmentI == SegmentE) |
| 814 | return false; |
| 815 | |
| 816 | // Look for each slot in the live range. |
| 817 | for ( ; SlotI != SlotE; ++SlotI) { |
| 818 | // Go to the next segment that ends after the current slot. |
| 819 | // The slot may be within a hole in the range. |
| 820 | SegmentI = advanceTo(SegmentI, *SlotI); |
| 821 | if (SegmentI == SegmentE) |
| 822 | return false; |
| 823 | |
| 824 | // If this segment contains the slot, we're done. |
| 825 | if (SegmentI->contains(*SlotI)) |
| 826 | return true; |
| 827 | // Otherwise, look for the next slot. |
| 828 | } |
| 829 | |
| 830 | // We didn't find a segment containing any of the slots. |
| 831 | return false; |
| 832 | } |
| 833 | |
Matthias Braun | 7dc96de | 2015-02-06 17:28:47 +0000 | [diff] [blame] | 834 | void LiveInterval::freeSubRange(SubRange *S) { |
| 835 | S->~SubRange(); |
| 836 | // Memory was allocated with BumpPtr allocator and is not freed here. |
| 837 | } |
| 838 | |
Matthias Braun | 2079aa9 | 2014-12-10 01:12:40 +0000 | [diff] [blame] | 839 | void LiveInterval::removeEmptySubRanges() { |
| 840 | SubRange **NextPtr = &SubRanges; |
| 841 | SubRange *I = *NextPtr; |
| 842 | while (I != nullptr) { |
| 843 | if (!I->empty()) { |
| 844 | NextPtr = &I->Next; |
| 845 | I = *NextPtr; |
| 846 | continue; |
| 847 | } |
| 848 | // Skip empty subranges until we find the first nonempty one. |
| 849 | do { |
Matthias Braun | 7dc96de | 2015-02-06 17:28:47 +0000 | [diff] [blame] | 850 | SubRange *Next = I->Next; |
| 851 | freeSubRange(I); |
| 852 | I = Next; |
Matthias Braun | 2079aa9 | 2014-12-10 01:12:40 +0000 | [diff] [blame] | 853 | } while (I != nullptr && I->empty()); |
| 854 | *NextPtr = I; |
| 855 | } |
| 856 | } |
| 857 | |
Matthias Braun | 7dc96de | 2015-02-06 17:28:47 +0000 | [diff] [blame] | 858 | void LiveInterval::clearSubRanges() { |
| 859 | for (SubRange *I = SubRanges, *Next; I != nullptr; I = Next) { |
| 860 | Next = I->Next; |
| 861 | freeSubRange(I); |
| 862 | } |
| 863 | SubRanges = nullptr; |
| 864 | } |
| 865 | |
Evan Cheng | 57b5214 | 2007-04-17 20:25:11 +0000 | [diff] [blame] | 866 | unsigned LiveInterval::getSize() const { |
| 867 | unsigned Sum = 0; |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 868 | for (const Segment &S : segments) |
| 869 | Sum += S.start.distance(S.end); |
Evan Cheng | 57b5214 | 2007-04-17 20:25:11 +0000 | [diff] [blame] | 870 | return Sum; |
| 871 | } |
| 872 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame^] | 873 | void LiveInterval::computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs, |
| 874 | LaneBitmask LaneMask, |
| 875 | const MachineRegisterInfo &MRI, |
| 876 | const SlotIndexes &Indexes) const { |
| 877 | assert(TargetRegisterInfo::isVirtualRegister(reg)); |
| 878 | LaneBitmask VRegMask = MRI.getMaxLaneMaskForVReg(reg); |
| 879 | assert((VRegMask & LaneMask) != 0); |
| 880 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
| 881 | for (const MachineOperand &MO : MRI.def_operands(reg)) { |
| 882 | if (!MO.isUndef()) |
| 883 | continue; |
| 884 | unsigned SubReg = MO.getSubReg(); |
| 885 | assert(SubReg != 0 && "Undef should only be set on subreg defs"); |
| 886 | LaneBitmask DefMask = TRI.getSubRegIndexLaneMask(SubReg); |
| 887 | LaneBitmask UndefMask = VRegMask & ~DefMask; |
| 888 | if ((UndefMask & LaneMask) != 0) { |
| 889 | const MachineInstr &MI = *MO.getParent(); |
| 890 | bool EarlyClobber = MO.isEarlyClobber(); |
| 891 | SlotIndex Pos = Indexes.getInstructionIndex(MI).getRegSlot(EarlyClobber); |
| 892 | Undefs.push_back(Pos); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 897 | raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange::Segment &S) { |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 898 | return os << '[' << S.start << ',' << S.end << ':' << S.valno->id << ')'; |
Daniel Dunbar | 796e43e | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 899 | } |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 900 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 901 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 902 | LLVM_DUMP_METHOD void LiveRange::Segment::dump() const { |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 903 | dbgs() << *this << '\n'; |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 904 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 905 | #endif |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 906 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 907 | void LiveRange::print(raw_ostream &OS) const { |
Chris Lattner | c08d786 | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 908 | if (empty()) |
Jakob Stoklund Olesen | f3f7d6f | 2012-06-05 22:51:54 +0000 | [diff] [blame] | 909 | OS << "EMPTY"; |
Chris Lattner | c08d786 | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 910 | else { |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 911 | for (const Segment &S : segments) { |
| 912 | OS << S; |
| 913 | assert(S.valno == getValNumInfo(S.valno->id) && "Bad VNInfo"); |
Jakob Stoklund Olesen | 731ea71 | 2010-06-23 15:34:36 +0000 | [diff] [blame] | 914 | } |
Chris Lattner | c08d786 | 2005-05-14 05:34:15 +0000 | [diff] [blame] | 915 | } |
Jakob Stoklund Olesen | 55d738e2 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 2e9f1bc | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 917 | // Print value number info. |
Chris Lattner | 34434e9 | 2006-08-29 23:18:15 +0000 | [diff] [blame] | 918 | if (getNumValNums()) { |
Chris Lattner | 2e9f1bc | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 919 | OS << " "; |
Evan Cheng | a5b10b3 | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 920 | unsigned vnum = 0; |
| 921 | for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; |
| 922 | ++i, ++vnum) { |
Evan Cheng | 1ad4a61 | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 923 | const VNInfo *vni = *i; |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 924 | if (vnum) OS << ' '; |
| 925 | OS << vnum << '@'; |
Lang Hames | 16cab19 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 926 | if (vni->isUnused()) { |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 927 | OS << 'x'; |
Chris Lattner | 2e9f1bc | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 928 | } else { |
Lang Hames | 5649568 | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 929 | OS << vni->def; |
Jakob Stoklund Olesen | 9a41490 | 2010-10-05 18:48:57 +0000 | [diff] [blame] | 930 | if (vni->isPHIDef()) |
Jakob Stoklund Olesen | 9f565e1 | 2012-08-03 20:19:44 +0000 | [diff] [blame] | 931 | OS << "-phi"; |
Evan Cheng | 0d0fee2 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 932 | } |
Chris Lattner | 2e9f1bc | 2006-08-22 18:19:46 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
Chris Lattner | 78f62e3 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 935 | } |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 936 | |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 937 | void LiveInterval::SubRange::print(raw_ostream &OS) const { |
| 938 | OS << " L" << PrintLaneMask(LaneMask) << ' ' |
| 939 | << static_cast<const LiveRange&>(*this); |
| 940 | } |
| 941 | |
Matthias Braun | f6fe6bf | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 942 | void LiveInterval::print(raw_ostream &OS) const { |
| 943 | OS << PrintReg(reg) << ' '; |
| 944 | super::print(OS); |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 945 | // Print subranges |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 946 | for (const SubRange &SR : subranges()) |
| 947 | OS << SR; |
Matthias Braun | f6fe6bf | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 950 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 951 | LLVM_DUMP_METHOD void LiveRange::dump() const { |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 952 | dbgs() << *this << '\n'; |
| 953 | } |
| 954 | |
| 955 | LLVM_DUMP_METHOD void LiveInterval::SubRange::dump() const { |
| 956 | dbgs() << *this << '\n'; |
Chris Lattner | 038747f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 957 | } |
Matthias Braun | f6fe6bf | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 958 | |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 959 | LLVM_DUMP_METHOD void LiveInterval::dump() const { |
Krzysztof Parzyszek | f5b9bb6 | 2016-07-12 17:37:44 +0000 | [diff] [blame] | 960 | dbgs() << *this << '\n'; |
Matthias Braun | f6fe6bf | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 961 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 962 | #endif |
Jeff Cohen | b82309f | 2006-12-15 22:57:14 +0000 | [diff] [blame] | 963 | |
Chandler Carruth | ac766b9 | 2012-07-10 05:06:03 +0000 | [diff] [blame] | 964 | #ifndef NDEBUG |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 965 | void LiveRange::verify() const { |
Chandler Carruth | ac766b9 | 2012-07-10 05:06:03 +0000 | [diff] [blame] | 966 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
| 967 | assert(I->start.isValid()); |
| 968 | assert(I->end.isValid()); |
| 969 | assert(I->start < I->end); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 970 | assert(I->valno != nullptr); |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 971 | assert(I->valno->id < valnos.size()); |
Chandler Carruth | ac766b9 | 2012-07-10 05:06:03 +0000 | [diff] [blame] | 972 | assert(I->valno == valnos[I->valno->id]); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 973 | if (std::next(I) != E) { |
| 974 | assert(I->end <= std::next(I)->start); |
| 975 | if (I->end == std::next(I)->start) |
| 976 | assert(I->valno != std::next(I)->valno); |
Chandler Carruth | ac766b9 | 2012-07-10 05:06:03 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | } |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 980 | |
| 981 | void LiveInterval::verify(const MachineRegisterInfo *MRI) const { |
| 982 | super::verify(); |
| 983 | |
| 984 | // Make sure SubRanges are fine and LaneMasks are disjunct. |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 985 | LaneBitmask Mask = 0; |
| 986 | LaneBitmask MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg) : ~0u; |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 987 | for (const SubRange &SR : subranges()) { |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 988 | // Subrange lanemask should be disjunct to any previous subrange masks. |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 989 | assert((Mask & SR.LaneMask) == 0); |
| 990 | Mask |= SR.LaneMask; |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 991 | |
| 992 | // subrange mask should not contained in maximum lane mask for the vreg. |
| 993 | assert((Mask & ~MaxMask) == 0); |
Matthias Braun | 0d4cebd | 2015-07-16 18:55:35 +0000 | [diff] [blame] | 994 | // empty subranges must be removed. |
| 995 | assert(!SR.empty()); |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 996 | |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 997 | SR.verify(); |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 998 | // Main liverange should cover subrange. |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 999 | assert(covers(SR)); |
Matthias Braun | 3f1d8fd | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
Chandler Carruth | ac766b9 | 2012-07-10 05:06:03 +0000 | [diff] [blame] | 1002 | #endif |
| 1003 | |
Jeff Cohen | b82309f | 2006-12-15 22:57:14 +0000 | [diff] [blame] | 1004 | |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1005 | //===----------------------------------------------------------------------===// |
| 1006 | // LiveRangeUpdater class |
| 1007 | //===----------------------------------------------------------------------===// |
| 1008 | // |
| 1009 | // The LiveRangeUpdater class always maintains these invariants: |
| 1010 | // |
| 1011 | // - When LastStart is invalid, Spills is empty and the iterators are invalid. |
| 1012 | // This is the initial state, and the state created by flush(). |
| 1013 | // In this state, isDirty() returns false. |
| 1014 | // |
| 1015 | // Otherwise, segments are kept in three separate areas: |
| 1016 | // |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1017 | // 1. [begin; WriteI) at the front of LR. |
| 1018 | // 2. [ReadI; end) at the back of LR. |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1019 | // 3. Spills. |
| 1020 | // |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1021 | // - LR.begin() <= WriteI <= ReadI <= LR.end(). |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1022 | // - Segments in all three areas are fully ordered and coalesced. |
| 1023 | // - Segments in area 1 precede and can't coalesce with segments in area 2. |
| 1024 | // - Segments in Spills precede and can't coalesce with segments in area 2. |
| 1025 | // - No coalescing is possible between segments in Spills and segments in area |
| 1026 | // 1, and there are no overlapping segments. |
| 1027 | // |
| 1028 | // The segments in Spills are not ordered with respect to the segments in area |
| 1029 | // 1. They need to be merged. |
| 1030 | // |
| 1031 | // When they exist, Spills.back().start <= LastStart, |
| 1032 | // and WriteI[-1].start <= LastStart. |
| 1033 | |
| 1034 | void LiveRangeUpdater::print(raw_ostream &OS) const { |
| 1035 | if (!isDirty()) { |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1036 | if (LR) |
| 1037 | OS << "Clean updater: " << *LR << '\n'; |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1038 | else |
| 1039 | OS << "Null updater.\n"; |
| 1040 | return; |
| 1041 | } |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1042 | assert(LR && "Can't have null LR in dirty updater."); |
| 1043 | OS << " updater with gap = " << (ReadI - WriteI) |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1044 | << ", last start = " << LastStart |
| 1045 | << ":\n Area 1:"; |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 1046 | for (const auto &S : make_range(LR->begin(), WriteI)) |
| 1047 | OS << ' ' << S; |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1048 | OS << "\n Spills:"; |
| 1049 | for (unsigned I = 0, E = Spills.size(); I != E; ++I) |
| 1050 | OS << ' ' << Spills[I]; |
| 1051 | OS << "\n Area 2:"; |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 1052 | for (const auto &S : make_range(ReadI, LR->end())) |
| 1053 | OS << ' ' << S; |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1054 | OS << '\n'; |
| 1055 | } |
| 1056 | |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1057 | LLVM_DUMP_METHOD void LiveRangeUpdater::dump() const { |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1058 | print(errs()); |
| 1059 | } |
| 1060 | |
| 1061 | // Determine if A and B should be coalesced. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1062 | static inline bool coalescable(const LiveRange::Segment &A, |
| 1063 | const LiveRange::Segment &B) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 1064 | assert(A.start <= B.start && "Unordered live segments."); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1065 | if (A.end == B.start) |
| 1066 | return A.valno == B.valno; |
| 1067 | if (A.end < B.start) |
| 1068 | return false; |
| 1069 | assert(A.valno == B.valno && "Cannot overlap different values"); |
| 1070 | return true; |
| 1071 | } |
| 1072 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1073 | void LiveRangeUpdater::add(LiveRange::Segment Seg) { |
| 1074 | assert(LR && "Cannot add to a null destination"); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1075 | |
Quentin Colombet | a8cb36e | 2015-02-06 18:42:41 +0000 | [diff] [blame] | 1076 | // Fall back to the regular add method if the live range |
| 1077 | // is using the segment set instead of the segment vector. |
| 1078 | if (LR->segmentSet != nullptr) { |
| 1079 | LR->addSegmentToSet(Seg); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1083 | // Flush the state if Start moves backwards. |
| 1084 | if (!LastStart.isValid() || LastStart > Seg.start) { |
| 1085 | if (isDirty()) |
| 1086 | flush(); |
| 1087 | // This brings us to an uninitialized state. Reinitialize. |
| 1088 | assert(Spills.empty() && "Leftover spilled segments"); |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1089 | WriteI = ReadI = LR->begin(); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | // Remember start for next time. |
| 1093 | LastStart = Seg.start; |
| 1094 | |
| 1095 | // Advance ReadI until it ends after Seg.start. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1096 | LiveRange::iterator E = LR->end(); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1097 | if (ReadI != E && ReadI->end <= Seg.start) { |
| 1098 | // First try to close the gap between WriteI and ReadI with spills. |
| 1099 | if (ReadI != WriteI) |
| 1100 | mergeSpills(); |
| 1101 | // Then advance ReadI. |
| 1102 | if (ReadI == WriteI) |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1103 | ReadI = WriteI = LR->find(Seg.start); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1104 | else |
| 1105 | while (ReadI != E && ReadI->end <= Seg.start) |
| 1106 | *WriteI++ = *ReadI++; |
| 1107 | } |
| 1108 | |
| 1109 | assert(ReadI == E || ReadI->end > Seg.start); |
| 1110 | |
| 1111 | // Check if the ReadI segment begins early. |
| 1112 | if (ReadI != E && ReadI->start <= Seg.start) { |
| 1113 | assert(ReadI->valno == Seg.valno && "Cannot overlap different values"); |
| 1114 | // Bail if Seg is completely contained in ReadI. |
| 1115 | if (ReadI->end >= Seg.end) |
| 1116 | return; |
| 1117 | // Coalesce into Seg. |
| 1118 | Seg.start = ReadI->start; |
| 1119 | ++ReadI; |
| 1120 | } |
| 1121 | |
| 1122 | // Coalesce as much as possible from ReadI into Seg. |
| 1123 | while (ReadI != E && coalescable(Seg, *ReadI)) { |
| 1124 | Seg.end = std::max(Seg.end, ReadI->end); |
| 1125 | ++ReadI; |
| 1126 | } |
| 1127 | |
| 1128 | // Try coalescing Spills.back() into Seg. |
| 1129 | if (!Spills.empty() && coalescable(Spills.back(), Seg)) { |
| 1130 | Seg.start = Spills.back().start; |
| 1131 | Seg.end = std::max(Spills.back().end, Seg.end); |
| 1132 | Spills.pop_back(); |
| 1133 | } |
| 1134 | |
| 1135 | // Try coalescing Seg into WriteI[-1]. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1136 | if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) { |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1137 | WriteI[-1].end = std::max(WriteI[-1].end, Seg.end); |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | // Seg doesn't coalesce with anything, and needs to be inserted somewhere. |
| 1142 | if (WriteI != ReadI) { |
| 1143 | *WriteI++ = Seg; |
| 1144 | return; |
| 1145 | } |
| 1146 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1147 | // Finally, append to LR or Spills. |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1148 | if (WriteI == E) { |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1149 | LR->segments.push_back(Seg); |
| 1150 | WriteI = ReadI = LR->end(); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1151 | } else |
| 1152 | Spills.push_back(Seg); |
| 1153 | } |
| 1154 | |
| 1155 | // Merge as many spilled segments as possible into the gap between WriteI |
| 1156 | // and ReadI. Advance WriteI to reflect the inserted instructions. |
| 1157 | void LiveRangeUpdater::mergeSpills() { |
| 1158 | // Perform a backwards merge of Spills and [SpillI;WriteI). |
| 1159 | size_t GapSize = ReadI - WriteI; |
| 1160 | size_t NumMoved = std::min(Spills.size(), GapSize); |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1161 | LiveRange::iterator Src = WriteI; |
| 1162 | LiveRange::iterator Dst = Src + NumMoved; |
| 1163 | LiveRange::iterator SpillSrc = Spills.end(); |
| 1164 | LiveRange::iterator B = LR->begin(); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1165 | |
| 1166 | // This is the new WriteI position after merging spills. |
| 1167 | WriteI = Dst; |
| 1168 | |
| 1169 | // Now merge Src and Spills backwards. |
| 1170 | while (Src != Dst) { |
| 1171 | if (Src != B && Src[-1].start > SpillSrc[-1].start) |
| 1172 | *--Dst = *--Src; |
| 1173 | else |
| 1174 | *--Dst = *--SpillSrc; |
| 1175 | } |
| 1176 | assert(NumMoved == size_t(Spills.end() - SpillSrc)); |
| 1177 | Spills.erase(SpillSrc, Spills.end()); |
| 1178 | } |
| 1179 | |
| 1180 | void LiveRangeUpdater::flush() { |
| 1181 | if (!isDirty()) |
| 1182 | return; |
| 1183 | // Clear the dirty state. |
| 1184 | LastStart = SlotIndex(); |
| 1185 | |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1186 | assert(LR && "Cannot add to a null destination"); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1187 | |
| 1188 | // Nothing to merge? |
| 1189 | if (Spills.empty()) { |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1190 | LR->segments.erase(WriteI, ReadI); |
| 1191 | LR->verify(); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | // Resize the WriteI - ReadI gap to match Spills. |
| 1196 | size_t GapSize = ReadI - WriteI; |
| 1197 | if (GapSize < Spills.size()) { |
| 1198 | // The gap is too small. Make some room. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1199 | size_t WritePos = WriteI - LR->begin(); |
| 1200 | LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment()); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1201 | // This also invalidated ReadI, but it is recomputed below. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1202 | WriteI = LR->begin() + WritePos; |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1203 | } else { |
| 1204 | // Shrink the gap if necessary. |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1205 | LR->segments.erase(WriteI + Spills.size(), ReadI); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1206 | } |
| 1207 | ReadI = WriteI + Spills.size(); |
| 1208 | mergeSpills(); |
Matthias Braun | d7df935 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1209 | LR->verify(); |
Jakob Stoklund Olesen | 521c708 | 2013-02-20 18:18:12 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Matthias Braun | bf47f63 | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1212 | unsigned ConnectedVNInfoEqClasses::Classify(const LiveRange &LR) { |
Jakob Stoklund Olesen | 05cae83 | 2010-10-08 21:19:28 +0000 | [diff] [blame] | 1213 | // Create initial equivalence classes. |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1214 | EqClass.clear(); |
Matthias Braun | bf47f63 | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1215 | EqClass.grow(LR.getNumValNums()); |
Jakob Stoklund Olesen | 05cae83 | 2010-10-08 21:19:28 +0000 | [diff] [blame] | 1216 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1217 | const VNInfo *used = nullptr, *unused = nullptr; |
Jakob Stoklund Olesen | dff6a6e | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 1218 | |
Jakob Stoklund Olesen | 05cae83 | 2010-10-08 21:19:28 +0000 | [diff] [blame] | 1219 | // Determine connections. |
Matthias Braun | bf47f63 | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1220 | for (const VNInfo *VNI : LR.valnos) { |
Jakob Stoklund Olesen | dff6a6e | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 1221 | // Group all unused values into one class. |
| 1222 | if (VNI->isUnused()) { |
| 1223 | if (unused) |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1224 | EqClass.join(unused->id, VNI->id); |
Jakob Stoklund Olesen | dff6a6e | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 1225 | unused = VNI; |
| 1226 | continue; |
| 1227 | } |
| 1228 | used = VNI; |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1229 | if (VNI->isPHIDef()) { |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1230 | const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1231 | assert(MBB && "Phi-def has no defining MBB"); |
| 1232 | // Connect to values live out of predecessors. |
| 1233 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 1234 | PE = MBB->pred_end(); PI != PE; ++PI) |
Matthias Braun | bf47f63 | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1235 | if (const VNInfo *PVNI = LR.getVNInfoBefore(LIS.getMBBEndIdx(*PI))) |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1236 | EqClass.join(VNI->id, PVNI->id); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1237 | } else { |
| 1238 | // Normal value defined by an instruction. Check for two-addr redef. |
| 1239 | // FIXME: This could be coincidental. Should we really check for a tied |
| 1240 | // operand constraint? |
Jakob Stoklund Olesen | 4c278f8 | 2010-12-21 00:48:17 +0000 | [diff] [blame] | 1241 | // Note that VNI->def may be a use slot for an early clobber def. |
Matthias Braun | bf47f63 | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 1242 | if (const VNInfo *UVNI = LR.getVNInfoBefore(VNI->def)) |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1243 | EqClass.join(VNI->id, UVNI->id); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1244 | } |
| 1245 | } |
Jakob Stoklund Olesen | dff6a6e | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 1246 | |
| 1247 | // Lump all the unused values in with the last used value. |
| 1248 | if (used && unused) |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1249 | EqClass.join(used->id, unused->id); |
Jakob Stoklund Olesen | dff6a6e | 2010-10-29 17:37:29 +0000 | [diff] [blame] | 1250 | |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1251 | EqClass.compress(); |
| 1252 | return EqClass.getNumClasses(); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1255 | void ConnectedVNInfoEqClasses::Distribute(LiveInterval &LI, LiveInterval *LIV[], |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1256 | MachineRegisterInfo &MRI) { |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1257 | // Rewrite instructions. |
| 1258 | for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg), |
| 1259 | RE = MRI.reg_end(); RI != RE;) { |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1260 | MachineOperand &MO = *RI; |
| 1261 | MachineInstr *MI = RI->getParent(); |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1262 | ++RI; |
Jakob Stoklund Olesen | 43711c5 | 2013-05-23 17:02:23 +0000 | [diff] [blame] | 1263 | // DBG_VALUE instructions don't have slot indexes, so get the index of the |
| 1264 | // instruction before them. |
| 1265 | // Normally, DBG_VALUE instructions are removed before this function is |
| 1266 | // called, but it is not a requirement. |
| 1267 | SlotIndex Idx; |
| 1268 | if (MI->isDebugValue()) |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1269 | Idx = LIS.getSlotIndexes()->getIndexBefore(*MI); |
Jakob Stoklund Olesen | 43711c5 | 2013-05-23 17:02:23 +0000 | [diff] [blame] | 1270 | else |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1271 | Idx = LIS.getInstructionIndex(*MI); |
Matthias Braun | 88dd0ab | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 1272 | LiveQueryResult LRQ = LI.Query(Idx); |
Jakob Stoklund Olesen | cef9a61 | 2012-07-25 17:15:15 +0000 | [diff] [blame] | 1273 | const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined(); |
| 1274 | // In the case of an <undef> use that isn't tied to any def, VNI will be |
| 1275 | // NULL. If the use is tied to a def, VNI will be the defined value. |
Jakob Stoklund Olesen | 82d77e8 | 2012-05-19 05:25:50 +0000 | [diff] [blame] | 1276 | if (!VNI) |
| 1277 | continue; |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 1278 | if (unsigned EqClass = getEqClass(VNI)) |
| 1279 | MO.setReg(LIV[EqClass-1]->reg); |
Jakob Stoklund Olesen | 315b42c | 2011-03-17 00:23:45 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Matthias Braun | 5efe871 | 2015-09-22 22:37:42 +0000 | [diff] [blame] | 1282 | // Distribute subregister liveranges. |
| 1283 | if (LI.hasSubRanges()) { |
| 1284 | unsigned NumComponents = EqClass.getNumClasses(); |
| 1285 | SmallVector<unsigned, 8> VNIMapping; |
| 1286 | SmallVector<LiveInterval::SubRange*, 8> SubRanges; |
| 1287 | BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator(); |
| 1288 | for (LiveInterval::SubRange &SR : LI.subranges()) { |
| 1289 | // Create new subranges in the split intervals and construct a mapping |
| 1290 | // for the VNInfos in the subrange. |
| 1291 | unsigned NumValNos = SR.valnos.size(); |
| 1292 | VNIMapping.clear(); |
| 1293 | VNIMapping.reserve(NumValNos); |
| 1294 | SubRanges.clear(); |
| 1295 | SubRanges.resize(NumComponents-1, nullptr); |
| 1296 | for (unsigned I = 0; I < NumValNos; ++I) { |
| 1297 | const VNInfo &VNI = *SR.valnos[I]; |
Matthias Braun | ae81c29 | 2016-03-24 21:41:38 +0000 | [diff] [blame] | 1298 | unsigned ComponentNum; |
| 1299 | if (VNI.isUnused()) { |
| 1300 | ComponentNum = 0; |
| 1301 | } else { |
| 1302 | const VNInfo *MainRangeVNI = LI.getVNInfoAt(VNI.def); |
| 1303 | assert(MainRangeVNI != nullptr |
| 1304 | && "SubRange def must have corresponding main range def"); |
| 1305 | ComponentNum = getEqClass(MainRangeVNI); |
| 1306 | if (ComponentNum > 0 && SubRanges[ComponentNum-1] == nullptr) { |
| 1307 | SubRanges[ComponentNum-1] |
| 1308 | = LIV[ComponentNum-1]->createSubRange(Allocator, SR.LaneMask); |
| 1309 | } |
Matthias Braun | 5efe871 | 2015-09-22 22:37:42 +0000 | [diff] [blame] | 1310 | } |
Matthias Braun | ae81c29 | 2016-03-24 21:41:38 +0000 | [diff] [blame] | 1311 | VNIMapping.push_back(ComponentNum); |
Matthias Braun | 5efe871 | 2015-09-22 22:37:42 +0000 | [diff] [blame] | 1312 | } |
| 1313 | DistributeRange(SR, SubRanges.data(), VNIMapping); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1314 | } |
Matthias Braun | 5efe871 | 2015-09-22 22:37:42 +0000 | [diff] [blame] | 1315 | LI.removeEmptySubRanges(); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1316 | } |
Matthias Braun | 5efe871 | 2015-09-22 22:37:42 +0000 | [diff] [blame] | 1317 | |
| 1318 | // Distribute main liverange. |
| 1319 | DistributeRange(LI, LIV, EqClass); |
Jakob Stoklund Olesen | 0f1677e | 2010-10-07 23:34:34 +0000 | [diff] [blame] | 1320 | } |