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