1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the LiveRange and LiveInterval classes. Given some 11 // numbering of each the machine instructions an interval [i, j) is said to be a 12 // live range for register v if there is no instruction with number j' >= j 13 // such that v is live at j' and there is no instruction with number i' < i such 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. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "llvm/CodeGen/LiveInterval.h" 22 23 #include "LiveRangeUtils.h" 24 #include "RegisterCoalescer.h" 25 #include "llvm/ADT/STLExtras.h" 26 #include "llvm/ADT/SmallSet.h" 27 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include "llvm/Target/TargetRegisterInfo.h" 32 #include <algorithm> 33 using namespace llvm; 34 35 namespace { 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 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) { 75 assert(!Def.isDead() && "Cannot define a value at the dead slot"); 76 assert((!ForVNI || ForVNI->def == Def) && 77 "If ForVNI is specified, it must match Def"); 78 iterator I = impl().find(Def); 79 if (I == segments().end()) { 80 VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator); 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)) { 87 assert((!ForVNI || ForVNI == S->valno) && "Value number mismatch"); 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"); 101 VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator); 102 segments().insert(I, Segment(Def, Def.getDeadSlot(), VNI)); 103 return VNI; 104 } 105 106 VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Use) { 107 if (segments().empty()) 108 return nullptr; 109 iterator I = 110 impl().findInsertPos(Segment(Use.getPrevSlot(), Use, nullptr)); 111 if (I == segments().begin()) 112 return nullptr; 113 --I; 114 if (I->end <= StartIdx) 115 return nullptr; 116 if (I->end < Use) 117 extendSegmentEndTo(I, Use); 118 return I->valno; 119 } 120 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 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 }; 330 } // namespace 331 332 //===----------------------------------------------------------------------===// 333 // LiveRange methods 334 //===----------------------------------------------------------------------===// 335 336 LiveRange::iterator LiveRange::find(SlotIndex Pos) { 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(); 343 size_t Len = size(); 344 do { 345 size_t Mid = Len >> 1; 346 if (Pos < I[Mid].end) { 347 Len = Mid; 348 } else { 349 I += Mid + 1; 350 Len -= Mid + 1; 351 } 352 } while (Len); 353 return I; 354 } 355 356 VNInfo *LiveRange::createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc) { 357 // Use the segment set, if it is available. 358 if (segmentSet != nullptr) 359 return CalcLiveRangeUtilSet(this).createDeadDef(Def, &VNIAlloc, nullptr); 360 // Otherwise use the segment vector. 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); 370 } 371 372 // overlaps - Return true if the intersection of the two live ranges is 373 // not empty. 374 // 375 // An example for overlaps(): 376 // 377 // 0: A = ... 378 // 4: B = ... 379 // 8: C = A + B ;; last use of A 380 // 381 // The live ranges should look like: 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. 389 // 390 bool LiveRange::overlapsFrom(const LiveRange& other, 391 const_iterator StartPos) const { 392 assert(!empty() && "empty range"); 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()) && 399 StartPos != other.end() && "Bogus start position hint!"); 400 401 if (i->start < j->start) { 402 i = std::upper_bound(i, ie, j->start); 403 if (i != begin()) --i; 404 } else if (j->start < i->start) { 405 ++StartPos; 406 if (StartPos != other.end() && StartPos->start <= i->start) { 407 assert(StartPos < other.end() && i < end()); 408 j = std::upper_bound(j, je, i->start); 409 if (j != other.begin()) --j; 410 } 411 } else { 412 return true; 413 } 414 415 if (j == je) return false; 416 417 while (i != ie) { 418 if (i->start > j->start) { 419 std::swap(i, j); 420 std::swap(ie, je); 421 } 422 423 if (i->end > j->start) 424 return true; 425 ++i; 426 } 427 428 return false; 429 } 430 431 bool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP, 432 const SlotIndexes &Indexes) const { 433 assert(!empty() && "empty range"); 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 472 /// overlaps - Return true if the live range overlaps an interval specified 473 /// by [Start, End). 474 bool LiveRange::overlaps(SlotIndex Start, SlotIndex End) const { 475 assert(Start < End && "Invalid range"); 476 const_iterator I = std::lower_bound(begin(), end(), End); 477 return I != begin() && (--I)->end > Start; 478 } 479 480 bool LiveRange::covers(const LiveRange &Other) const { 481 if (empty()) 482 return Other.empty(); 483 484 const_iterator I = begin(); 485 for (const Segment &O : Other.segments) { 486 I = advanceTo(I, O.start); 487 if (I == end() || I->start > O.start) 488 return false; 489 490 // Check adjacent live segments and see if we can get behind O.end. 491 while (I->end < O.end) { 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 } 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. 505 void LiveRange::markValNoForDeletion(VNInfo *ValNo) { 506 if (ValNo->id == getNumValNums()-1) { 507 do { 508 valnos.pop_back(); 509 } while (!valnos.empty() && valnos.back()->isUnused()); 510 } else { 511 ValNo->markUnused(); 512 } 513 } 514 515 /// RenumberValues - Renumber all values in order of appearance and delete the 516 /// remaining unused values. 517 void LiveRange::RenumberValues() { 518 SmallPtrSet<VNInfo*, 8> Seen; 519 valnos.clear(); 520 for (const Segment &S : segments) { 521 VNInfo *VNI = S.valno; 522 if (!Seen.insert(VNI).second) 523 continue; 524 assert(!VNI->isUnused() && "Unused valno used by live segment"); 525 VNI->id = (unsigned)valnos.size(); 526 valnos.push_back(VNI); 527 } 528 } 529 530 void LiveRange::addSegmentToSet(Segment S) { 531 CalcLiveRangeUtilSet(this).addSegment(S); 532 } 533 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(); 539 } 540 // Otherwise use the segment vector. 541 return CalcLiveRangeUtilVector(this).addSegment(S); 542 } 543 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 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 559 VNInfo *LiveRange::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) { 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); 565 } 566 567 /// Remove the specified segment from this range. Note that the segment must 568 /// be in a single Segment in its entirety. 569 void LiveRange::removeSegment(SlotIndex Start, SlotIndex End, 570 bool RemoveDeadValNo) { 571 // Find the Segment containing this span. 572 iterator I = find(Start); 573 assert(I != end() && "Segment is not in range!"); 574 assert(I->containsInterval(Start, End) 575 && "Segment is not entirely in range!"); 576 577 // If the span we are removing is at the start of the Segment, adjust it. 578 VNInfo *ValNo = I->valno; 579 if (I->start == Start) { 580 if (I->end == End) { 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; 588 } 589 if (isDead) { 590 // Now that ValNo is dead, remove it. 591 markValNoForDeletion(ValNo); 592 } 593 } 594 595 segments.erase(I); // Removed the whole Segment. 596 } else 597 I->start = End; 598 return; 599 } 600 601 // Otherwise if the span we are removing is at the end of the Segment, 602 // adjust the other way. 603 if (I->end == End) { 604 I->end = Start; 605 return; 606 } 607 608 // Otherwise, we are splitting the Segment into two pieces. 609 SlotIndex OldEnd = I->end; 610 I->end = Start; // Trim the old segment. 611 612 // Insert the new one. 613 segments.insert(std::next(I), Segment(End, OldEnd, ValNo)); 614 } 615 616 /// removeValNo - Remove all the segments defined by the specified value#. 617 /// Also remove the value# from value# list. 618 void LiveRange::removeValNo(VNInfo *ValNo) { 619 if (empty()) return; 620 segments.erase(remove_if(*this, [ValNo](const Segment &S) { 621 return S.valno == ValNo; 622 }), end()); 623 // Now that ValNo is dead, remove it. 624 markValNoForDeletion(ValNo); 625 } 626 627 void LiveRange::join(LiveRange &Other, 628 const int *LHSValNoAssignments, 629 const int *RHSValNoAssignments, 630 SmallVectorImpl<VNInfo *> &NewVNInfo) { 631 verify(); 632 633 // Determine if any of our values are mapped. This is uncommon, so we want 634 // to avoid the range scan if not. 635 bool MustMapCurValNos = false; 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 || 641 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) { 642 MustMapCurValNos = true; 643 break; 644 } 645 } 646 647 // If we have to apply a mapping to our base range assignment, rewrite it now. 648 if (MustMapCurValNos && !empty()) { 649 // Map the first live range. 650 651 iterator OutIt = begin(); 652 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; 653 for (iterator I = std::next(OutIt), E = end(); I != E; ++I) { 654 VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]]; 655 assert(nextValNo && "Huh?"); 656 657 // If this live range has the same value # as its immediate predecessor, 658 // and if they are neighbors, remove one Segment. This happens when we 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; 662 } else { 663 // Didn't merge. Move OutIt to the next segment, 664 ++OutIt; 665 OutIt->valno = nextValNo; 666 if (OutIt != I) { 667 OutIt->start = I->start; 668 OutIt->end = I->end; 669 } 670 } 671 } 672 // If we merge some segments, chop off the end. 673 ++OutIt; 674 segments.erase(OutIt, end()); 675 } 676 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(); 681 for (Segment &S : Other.segments) 682 S.valno = NewVNInfo[RHSValNoAssignments[S.valno->id]]; 683 684 // Update val# info. Renumber them and make sure they all belong to this 685 // LiveRange now. Also remove dead val#'s. 686 unsigned NumValNos = 0; 687 for (unsigned i = 0; i < NumNewVals; ++i) { 688 VNInfo *VNI = NewVNInfo[i]; 689 if (VNI) { 690 if (NumValNos >= NumVals) 691 valnos.push_back(VNI); 692 else 693 valnos[NumValNos] = VNI; 694 VNI->id = NumValNos++; // Renumber val#. 695 } 696 } 697 if (NumNewVals < NumVals) 698 valnos.resize(NumNewVals); // shrinkify 699 700 // Okay, now insert the RHS live segments into the LHS. 701 LiveRangeUpdater Updater(this); 702 for (Segment &S : Other.segments) 703 Updater.add(S); 704 } 705 706 /// Merge all of the segments in RHS into this live range as the specified 707 /// value number. The segments in RHS are allowed to overlap with segments in 708 /// the current range, but only if the overlapping segments have the 709 /// specified value number. 710 void LiveRange::MergeSegmentsInAsValue(const LiveRange &RHS, 711 VNInfo *LHSValNo) { 712 LiveRangeUpdater Updater(this); 713 for (const Segment &S : RHS.segments) 714 Updater.add(S.start, S.end, LHSValNo); 715 } 716 717 /// MergeValueInAsValue - Merge all of the live segments of a specific val# 718 /// in RHS into this live range as the specified value number. 719 /// The segments in RHS are allowed to overlap with segments in the 720 /// current range, it will replace the value numbers of the overlaped 721 /// segments with the specified value number. 722 void LiveRange::MergeValueInAsValue(const LiveRange &RHS, 723 const VNInfo *RHSValNo, 724 VNInfo *LHSValNo) { 725 LiveRangeUpdater Updater(this); 726 for (const Segment &S : RHS.segments) 727 if (S.valno == RHSValNo) 728 Updater.add(S.start, S.end, LHSValNo); 729 } 730 731 /// MergeValueNumberInto - This method is called when two value nubmers 732 /// are found to be equivalent. This eliminates V1, replacing all 733 /// segments with the V1 value number with the V2 value number. This can 734 /// cause merging of V1/V2 values numbers and compaction of the value space. 735 VNInfo *LiveRange::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { 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. 744 if (V1->id < V2->id) { 745 V1->copyFrom(*V2); 746 std::swap(V1, V2); 747 } 748 749 // Merge V1 segments into V2. 750 for (iterator I = begin(); I != end(); ) { 751 iterator S = I++; 752 if (S->valno != V1) continue; // Not a V1 Segment. 753 754 // Okay, we found a V1 live range. If it had a previous, touching, V2 live 755 // range, extend it. 756 if (S != begin()) { 757 iterator Prev = S-1; 758 if (Prev->valno == V2 && Prev->end == S->start) { 759 Prev->end = S->end; 760 761 // Erase this live-range. 762 segments.erase(S); 763 I = Prev+1; 764 S = Prev; 765 } 766 } 767 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. 770 S->valno = V2; 771 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 774 // of the loop. 775 if (I != end()) { 776 if (I->start == S->end && I->valno == V2) { 777 S->end = I->end; 778 segments.erase(I); 779 I = S+1; 780 } 781 } 782 } 783 784 // Now that V1 is dead, remove it. 785 markValNoForDeletion(V1); 786 787 return V2; 788 } 789 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()); 796 segmentSet = nullptr; 797 verify(); 798 } 799 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 834 void LiveInterval::freeSubRange(SubRange *S) { 835 S->~SubRange(); 836 // Memory was allocated with BumpPtr allocator and is not freed here. 837 } 838 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 { 850 SubRange *Next = I->Next; 851 freeSubRange(I); 852 I = Next; 853 } while (I != nullptr && I->empty()); 854 *NextPtr = I; 855 } 856 } 857 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 866 void LiveInterval::refineSubRanges(BumpPtrAllocator &Allocator, 867 LaneBitmask LaneMask, std::function<void(LiveInterval::SubRange&)> Apply) { 868 869 LaneBitmask ToApply = LaneMask; 870 for (SubRange &SR : subranges()) { 871 LaneBitmask SRMask = SR.LaneMask; 872 LaneBitmask Matching = SRMask & LaneMask; 873 if (Matching.none()) 874 continue; 875 876 SubRange *MatchingRange; 877 if (SRMask == Matching) { 878 // The subrange fits (it does not cover bits outside \p LaneMask). 879 MatchingRange = &SR; 880 } else { 881 // We have to split the subrange into a matching and non-matching part. 882 // Reduce lanemask of existing lane to non-matching part. 883 SR.LaneMask = SRMask & ~Matching; 884 // Create a new subrange for the matching part 885 MatchingRange = createSubRangeFrom(Allocator, Matching, SR); 886 } 887 Apply(*MatchingRange); 888 ToApply &= ~Matching; 889 } 890 // Create a new subrange if there are uncovered bits left. 891 if (ToApply.any()) { 892 SubRange *NewRange = createSubRange(Allocator, ToApply); 893 Apply(*NewRange); 894 } 895 } 896 897 unsigned LiveInterval::getSize() const { 898 unsigned Sum = 0; 899 for (const Segment &S : segments) 900 Sum += S.start.distance(S.end); 901 return Sum; 902 } 903 904 void LiveInterval::computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs, 905 LaneBitmask LaneMask, 906 const MachineRegisterInfo &MRI, 907 const SlotIndexes &Indexes) const { 908 assert(TargetRegisterInfo::isVirtualRegister(reg)); 909 LaneBitmask VRegMask = MRI.getMaxLaneMaskForVReg(reg); 910 assert((VRegMask & LaneMask).any()); 911 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 912 for (const MachineOperand &MO : MRI.def_operands(reg)) { 913 if (!MO.isUndef()) 914 continue; 915 unsigned SubReg = MO.getSubReg(); 916 assert(SubReg != 0 && "Undef should only be set on subreg defs"); 917 LaneBitmask DefMask = TRI.getSubRegIndexLaneMask(SubReg); 918 LaneBitmask UndefMask = VRegMask & ~DefMask; 919 if ((UndefMask & LaneMask).any()) { 920 const MachineInstr &MI = *MO.getParent(); 921 bool EarlyClobber = MO.isEarlyClobber(); 922 SlotIndex Pos = Indexes.getInstructionIndex(MI).getRegSlot(EarlyClobber); 923 Undefs.push_back(Pos); 924 } 925 } 926 } 927 928 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange::Segment &S) { 929 return os << '[' << S.start << ',' << S.end << ':' << S.valno->id << ')'; 930 } 931 932 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 933 LLVM_DUMP_METHOD void LiveRange::Segment::dump() const { 934 dbgs() << *this << '\n'; 935 } 936 #endif 937 938 void LiveRange::print(raw_ostream &OS) const { 939 if (empty()) 940 OS << "EMPTY"; 941 else { 942 for (const Segment &S : segments) { 943 OS << S; 944 assert(S.valno == getValNumInfo(S.valno->id) && "Bad VNInfo"); 945 } 946 } 947 948 // Print value number info. 949 if (getNumValNums()) { 950 OS << " "; 951 unsigned vnum = 0; 952 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; 953 ++i, ++vnum) { 954 const VNInfo *vni = *i; 955 if (vnum) OS << ' '; 956 OS << vnum << '@'; 957 if (vni->isUnused()) { 958 OS << 'x'; 959 } else { 960 OS << vni->def; 961 if (vni->isPHIDef()) 962 OS << "-phi"; 963 } 964 } 965 } 966 } 967 968 void LiveInterval::SubRange::print(raw_ostream &OS) const { 969 OS << " L" << PrintLaneMask(LaneMask) << ' ' 970 << static_cast<const LiveRange&>(*this); 971 } 972 973 void LiveInterval::print(raw_ostream &OS) const { 974 OS << PrintReg(reg) << ' '; 975 super::print(OS); 976 // Print subranges 977 for (const SubRange &SR : subranges()) 978 OS << SR; 979 } 980 981 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 982 LLVM_DUMP_METHOD void LiveRange::dump() const { 983 dbgs() << *this << '\n'; 984 } 985 986 LLVM_DUMP_METHOD void LiveInterval::SubRange::dump() const { 987 dbgs() << *this << '\n'; 988 } 989 990 LLVM_DUMP_METHOD void LiveInterval::dump() const { 991 dbgs() << *this << '\n'; 992 } 993 #endif 994 995 #ifndef NDEBUG 996 void LiveRange::verify() const { 997 for (const_iterator I = begin(), E = end(); I != E; ++I) { 998 assert(I->start.isValid()); 999 assert(I->end.isValid()); 1000 assert(I->start < I->end); 1001 assert(I->valno != nullptr); 1002 assert(I->valno->id < valnos.size()); 1003 assert(I->valno == valnos[I->valno->id]); 1004 if (std::next(I) != E) { 1005 assert(I->end <= std::next(I)->start); 1006 if (I->end == std::next(I)->start) 1007 assert(I->valno != std::next(I)->valno); 1008 } 1009 } 1010 } 1011 1012 void LiveInterval::verify(const MachineRegisterInfo *MRI) const { 1013 super::verify(); 1014 1015 // Make sure SubRanges are fine and LaneMasks are disjunct. 1016 LaneBitmask Mask; 1017 LaneBitmask MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg) 1018 : LaneBitmask::getAll(); 1019 for (const SubRange &SR : subranges()) { 1020 // Subrange lanemask should be disjunct to any previous subrange masks. 1021 assert((Mask & SR.LaneMask).none()); 1022 Mask |= SR.LaneMask; 1023 1024 // subrange mask should not contained in maximum lane mask for the vreg. 1025 assert((Mask & ~MaxMask).none()); 1026 // empty subranges must be removed. 1027 assert(!SR.empty()); 1028 1029 SR.verify(); 1030 // Main liverange should cover subrange. 1031 assert(covers(SR)); 1032 } 1033 } 1034 #endif 1035 1036 1037 //===----------------------------------------------------------------------===// 1038 // LiveRangeUpdater class 1039 //===----------------------------------------------------------------------===// 1040 // 1041 // The LiveRangeUpdater class always maintains these invariants: 1042 // 1043 // - When LastStart is invalid, Spills is empty and the iterators are invalid. 1044 // This is the initial state, and the state created by flush(). 1045 // In this state, isDirty() returns false. 1046 // 1047 // Otherwise, segments are kept in three separate areas: 1048 // 1049 // 1. [begin; WriteI) at the front of LR. 1050 // 2. [ReadI; end) at the back of LR. 1051 // 3. Spills. 1052 // 1053 // - LR.begin() <= WriteI <= ReadI <= LR.end(). 1054 // - Segments in all three areas are fully ordered and coalesced. 1055 // - Segments in area 1 precede and can't coalesce with segments in area 2. 1056 // - Segments in Spills precede and can't coalesce with segments in area 2. 1057 // - No coalescing is possible between segments in Spills and segments in area 1058 // 1, and there are no overlapping segments. 1059 // 1060 // The segments in Spills are not ordered with respect to the segments in area 1061 // 1. They need to be merged. 1062 // 1063 // When they exist, Spills.back().start <= LastStart, 1064 // and WriteI[-1].start <= LastStart. 1065 1066 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1067 void LiveRangeUpdater::print(raw_ostream &OS) const { 1068 if (!isDirty()) { 1069 if (LR) 1070 OS << "Clean updater: " << *LR << '\n'; 1071 else 1072 OS << "Null updater.\n"; 1073 return; 1074 } 1075 assert(LR && "Can't have null LR in dirty updater."); 1076 OS << " updater with gap = " << (ReadI - WriteI) 1077 << ", last start = " << LastStart 1078 << ":\n Area 1:"; 1079 for (const auto &S : make_range(LR->begin(), WriteI)) 1080 OS << ' ' << S; 1081 OS << "\n Spills:"; 1082 for (unsigned I = 0, E = Spills.size(); I != E; ++I) 1083 OS << ' ' << Spills[I]; 1084 OS << "\n Area 2:"; 1085 for (const auto &S : make_range(ReadI, LR->end())) 1086 OS << ' ' << S; 1087 OS << '\n'; 1088 } 1089 1090 LLVM_DUMP_METHOD void LiveRangeUpdater::dump() const { 1091 print(errs()); 1092 } 1093 #endif 1094 1095 // Determine if A and B should be coalesced. 1096 static inline bool coalescable(const LiveRange::Segment &A, 1097 const LiveRange::Segment &B) { 1098 assert(A.start <= B.start && "Unordered live segments."); 1099 if (A.end == B.start) 1100 return A.valno == B.valno; 1101 if (A.end < B.start) 1102 return false; 1103 assert(A.valno == B.valno && "Cannot overlap different values"); 1104 return true; 1105 } 1106 1107 void LiveRangeUpdater::add(LiveRange::Segment Seg) { 1108 assert(LR && "Cannot add to a null destination"); 1109 1110 // Fall back to the regular add method if the live range 1111 // is using the segment set instead of the segment vector. 1112 if (LR->segmentSet != nullptr) { 1113 LR->addSegmentToSet(Seg); 1114 return; 1115 } 1116 1117 // Flush the state if Start moves backwards. 1118 if (!LastStart.isValid() || LastStart > Seg.start) { 1119 if (isDirty()) 1120 flush(); 1121 // This brings us to an uninitialized state. Reinitialize. 1122 assert(Spills.empty() && "Leftover spilled segments"); 1123 WriteI = ReadI = LR->begin(); 1124 } 1125 1126 // Remember start for next time. 1127 LastStart = Seg.start; 1128 1129 // Advance ReadI until it ends after Seg.start. 1130 LiveRange::iterator E = LR->end(); 1131 if (ReadI != E && ReadI->end <= Seg.start) { 1132 // First try to close the gap between WriteI and ReadI with spills. 1133 if (ReadI != WriteI) 1134 mergeSpills(); 1135 // Then advance ReadI. 1136 if (ReadI == WriteI) 1137 ReadI = WriteI = LR->find(Seg.start); 1138 else 1139 while (ReadI != E && ReadI->end <= Seg.start) 1140 *WriteI++ = *ReadI++; 1141 } 1142 1143 assert(ReadI == E || ReadI->end > Seg.start); 1144 1145 // Check if the ReadI segment begins early. 1146 if (ReadI != E && ReadI->start <= Seg.start) { 1147 assert(ReadI->valno == Seg.valno && "Cannot overlap different values"); 1148 // Bail if Seg is completely contained in ReadI. 1149 if (ReadI->end >= Seg.end) 1150 return; 1151 // Coalesce into Seg. 1152 Seg.start = ReadI->start; 1153 ++ReadI; 1154 } 1155 1156 // Coalesce as much as possible from ReadI into Seg. 1157 while (ReadI != E && coalescable(Seg, *ReadI)) { 1158 Seg.end = std::max(Seg.end, ReadI->end); 1159 ++ReadI; 1160 } 1161 1162 // Try coalescing Spills.back() into Seg. 1163 if (!Spills.empty() && coalescable(Spills.back(), Seg)) { 1164 Seg.start = Spills.back().start; 1165 Seg.end = std::max(Spills.back().end, Seg.end); 1166 Spills.pop_back(); 1167 } 1168 1169 // Try coalescing Seg into WriteI[-1]. 1170 if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) { 1171 WriteI[-1].end = std::max(WriteI[-1].end, Seg.end); 1172 return; 1173 } 1174 1175 // Seg doesn't coalesce with anything, and needs to be inserted somewhere. 1176 if (WriteI != ReadI) { 1177 *WriteI++ = Seg; 1178 return; 1179 } 1180 1181 // Finally, append to LR or Spills. 1182 if (WriteI == E) { 1183 LR->segments.push_back(Seg); 1184 WriteI = ReadI = LR->end(); 1185 } else 1186 Spills.push_back(Seg); 1187 } 1188 1189 // Merge as many spilled segments as possible into the gap between WriteI 1190 // and ReadI. Advance WriteI to reflect the inserted instructions. 1191 void LiveRangeUpdater::mergeSpills() { 1192 // Perform a backwards merge of Spills and [SpillI;WriteI). 1193 size_t GapSize = ReadI - WriteI; 1194 size_t NumMoved = std::min(Spills.size(), GapSize); 1195 LiveRange::iterator Src = WriteI; 1196 LiveRange::iterator Dst = Src + NumMoved; 1197 LiveRange::iterator SpillSrc = Spills.end(); 1198 LiveRange::iterator B = LR->begin(); 1199 1200 // This is the new WriteI position after merging spills. 1201 WriteI = Dst; 1202 1203 // Now merge Src and Spills backwards. 1204 while (Src != Dst) { 1205 if (Src != B && Src[-1].start > SpillSrc[-1].start) 1206 *--Dst = *--Src; 1207 else 1208 *--Dst = *--SpillSrc; 1209 } 1210 assert(NumMoved == size_t(Spills.end() - SpillSrc)); 1211 Spills.erase(SpillSrc, Spills.end()); 1212 } 1213 1214 void LiveRangeUpdater::flush() { 1215 if (!isDirty()) 1216 return; 1217 // Clear the dirty state. 1218 LastStart = SlotIndex(); 1219 1220 assert(LR && "Cannot add to a null destination"); 1221 1222 // Nothing to merge? 1223 if (Spills.empty()) { 1224 LR->segments.erase(WriteI, ReadI); 1225 LR->verify(); 1226 return; 1227 } 1228 1229 // Resize the WriteI - ReadI gap to match Spills. 1230 size_t GapSize = ReadI - WriteI; 1231 if (GapSize < Spills.size()) { 1232 // The gap is too small. Make some room. 1233 size_t WritePos = WriteI - LR->begin(); 1234 LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment()); 1235 // This also invalidated ReadI, but it is recomputed below. 1236 WriteI = LR->begin() + WritePos; 1237 } else { 1238 // Shrink the gap if necessary. 1239 LR->segments.erase(WriteI + Spills.size(), ReadI); 1240 } 1241 ReadI = WriteI + Spills.size(); 1242 mergeSpills(); 1243 LR->verify(); 1244 } 1245 1246 unsigned ConnectedVNInfoEqClasses::Classify(const LiveRange &LR) { 1247 // Create initial equivalence classes. 1248 EqClass.clear(); 1249 EqClass.grow(LR.getNumValNums()); 1250 1251 const VNInfo *used = nullptr, *unused = nullptr; 1252 1253 // Determine connections. 1254 for (const VNInfo *VNI : LR.valnos) { 1255 // Group all unused values into one class. 1256 if (VNI->isUnused()) { 1257 if (unused) 1258 EqClass.join(unused->id, VNI->id); 1259 unused = VNI; 1260 continue; 1261 } 1262 used = VNI; 1263 if (VNI->isPHIDef()) { 1264 const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def); 1265 assert(MBB && "Phi-def has no defining MBB"); 1266 // Connect to values live out of predecessors. 1267 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), 1268 PE = MBB->pred_end(); PI != PE; ++PI) 1269 if (const VNInfo *PVNI = LR.getVNInfoBefore(LIS.getMBBEndIdx(*PI))) 1270 EqClass.join(VNI->id, PVNI->id); 1271 } else { 1272 // Normal value defined by an instruction. Check for two-addr redef. 1273 // FIXME: This could be coincidental. Should we really check for a tied 1274 // operand constraint? 1275 // Note that VNI->def may be a use slot for an early clobber def. 1276 if (const VNInfo *UVNI = LR.getVNInfoBefore(VNI->def)) 1277 EqClass.join(VNI->id, UVNI->id); 1278 } 1279 } 1280 1281 // Lump all the unused values in with the last used value. 1282 if (used && unused) 1283 EqClass.join(used->id, unused->id); 1284 1285 EqClass.compress(); 1286 return EqClass.getNumClasses(); 1287 } 1288 1289 void ConnectedVNInfoEqClasses::Distribute(LiveInterval &LI, LiveInterval *LIV[], 1290 MachineRegisterInfo &MRI) { 1291 // Rewrite instructions. 1292 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg), 1293 RE = MRI.reg_end(); RI != RE;) { 1294 MachineOperand &MO = *RI; 1295 MachineInstr *MI = RI->getParent(); 1296 ++RI; 1297 // DBG_VALUE instructions don't have slot indexes, so get the index of the 1298 // instruction before them. 1299 // Normally, DBG_VALUE instructions are removed before this function is 1300 // called, but it is not a requirement. 1301 SlotIndex Idx; 1302 if (MI->isDebugValue()) 1303 Idx = LIS.getSlotIndexes()->getIndexBefore(*MI); 1304 else 1305 Idx = LIS.getInstructionIndex(*MI); 1306 LiveQueryResult LRQ = LI.Query(Idx); 1307 const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined(); 1308 // In the case of an <undef> use that isn't tied to any def, VNI will be 1309 // NULL. If the use is tied to a def, VNI will be the defined value. 1310 if (!VNI) 1311 continue; 1312 if (unsigned EqClass = getEqClass(VNI)) 1313 MO.setReg(LIV[EqClass-1]->reg); 1314 } 1315 1316 // Distribute subregister liveranges. 1317 if (LI.hasSubRanges()) { 1318 unsigned NumComponents = EqClass.getNumClasses(); 1319 SmallVector<unsigned, 8> VNIMapping; 1320 SmallVector<LiveInterval::SubRange*, 8> SubRanges; 1321 BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator(); 1322 for (LiveInterval::SubRange &SR : LI.subranges()) { 1323 // Create new subranges in the split intervals and construct a mapping 1324 // for the VNInfos in the subrange. 1325 unsigned NumValNos = SR.valnos.size(); 1326 VNIMapping.clear(); 1327 VNIMapping.reserve(NumValNos); 1328 SubRanges.clear(); 1329 SubRanges.resize(NumComponents-1, nullptr); 1330 for (unsigned I = 0; I < NumValNos; ++I) { 1331 const VNInfo &VNI = *SR.valnos[I]; 1332 unsigned ComponentNum; 1333 if (VNI.isUnused()) { 1334 ComponentNum = 0; 1335 } else { 1336 const VNInfo *MainRangeVNI = LI.getVNInfoAt(VNI.def); 1337 assert(MainRangeVNI != nullptr 1338 && "SubRange def must have corresponding main range def"); 1339 ComponentNum = getEqClass(MainRangeVNI); 1340 if (ComponentNum > 0 && SubRanges[ComponentNum-1] == nullptr) { 1341 SubRanges[ComponentNum-1] 1342 = LIV[ComponentNum-1]->createSubRange(Allocator, SR.LaneMask); 1343 } 1344 } 1345 VNIMapping.push_back(ComponentNum); 1346 } 1347 DistributeRange(SR, SubRanges.data(), VNIMapping); 1348 } 1349 LI.removeEmptySubRanges(); 1350 } 1351 1352 // Distribute main liverange. 1353 DistributeRange(LI, LIV, EqClass); 1354 } 1355