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 interval 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 intervals can have holes, 15 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each 16 // individual range is represented as an instance of LiveRange, and the whole 17 // interval is represented as an instance of LiveInterval. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "llvm/CodeGen/LiveInterval.h" 22 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 23 #include "llvm/CodeGen/MachineRegisterInfo.h" 24 #include "llvm/ADT/DenseMap.h" 25 #include "llvm/ADT/SmallSet.h" 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/Support/Debug.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include "llvm/Target/TargetRegisterInfo.h" 30 #include <algorithm> 31 using namespace llvm; 32 33 // An example for liveAt(): 34 // 35 // this = [1,4), liveAt(0) will return false. The instruction defining this 36 // spans slots [0,3]. The interval belongs to an spilled definition of the 37 // variable it represents. This is because slot 1 is used (def slot) and spans 38 // up to slot 3 (store slot). 39 // 40 bool LiveInterval::liveAt(SlotIndex I) const { 41 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I); 42 43 if (r == ranges.begin()) 44 return false; 45 46 --r; 47 return r->contains(I); 48 } 49 50 // liveBeforeAndAt - Check if the interval is live at the index and the index 51 // just before it. If index is liveAt, check if it starts a new live range. 52 // If it does, then check if the previous live range ends at index-1. 53 bool LiveInterval::liveBeforeAndAt(SlotIndex I) const { 54 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I); 55 56 if (r == ranges.begin()) 57 return false; 58 59 --r; 60 if (!r->contains(I)) 61 return false; 62 if (I != r->start) 63 return true; 64 // I is the start of a live range. Check if the previous live range ends 65 // at I-1. 66 if (r == ranges.begin()) 67 return false; 68 return r->end == I; 69 } 70 71 /// killedAt - Return true if a live range ends at index. Note that the kill 72 /// point is not contained in the half-open live range. It is usually the 73 /// getDefIndex() slot following its last use. 74 bool LiveInterval::killedAt(SlotIndex I) const { 75 Ranges::const_iterator r = std::lower_bound(ranges.begin(), ranges.end(), I); 76 77 // Now r points to the first interval with start >= I, or ranges.end(). 78 if (r == ranges.begin()) 79 return false; 80 81 --r; 82 // Now r points to the last interval with end <= I. 83 // r->end is the kill point. 84 return r->end == I; 85 } 86 87 /// killedInRange - Return true if the interval has kills in [Start,End). 88 bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const { 89 Ranges::const_iterator r = 90 std::lower_bound(ranges.begin(), ranges.end(), End); 91 92 // Now r points to the first interval with start >= End, or ranges.end(). 93 if (r == ranges.begin()) 94 return false; 95 96 --r; 97 // Now r points to the last interval with end <= End. 98 // r->end is the kill point. 99 return r->end >= Start && r->end < End; 100 } 101 102 // overlaps - Return true if the intersection of the two live intervals is 103 // not empty. 104 // 105 // An example for overlaps(): 106 // 107 // 0: A = ... 108 // 4: B = ... 109 // 8: C = A + B ;; last use of A 110 // 111 // The live intervals should look like: 112 // 113 // A = [3, 11) 114 // B = [7, x) 115 // C = [11, y) 116 // 117 // A->overlaps(C) should return false since we want to be able to join 118 // A and C. 119 // 120 bool LiveInterval::overlapsFrom(const LiveInterval& other, 121 const_iterator StartPos) const { 122 const_iterator i = begin(); 123 const_iterator ie = end(); 124 const_iterator j = StartPos; 125 const_iterator je = other.end(); 126 127 assert((StartPos->start <= i->start || StartPos == other.begin()) && 128 StartPos != other.end() && "Bogus start position hint!"); 129 130 if (i->start < j->start) { 131 i = std::upper_bound(i, ie, j->start); 132 if (i != ranges.begin()) --i; 133 } else if (j->start < i->start) { 134 ++StartPos; 135 if (StartPos != other.end() && StartPos->start <= i->start) { 136 assert(StartPos < other.end() && i < end()); 137 j = std::upper_bound(j, je, i->start); 138 if (j != other.ranges.begin()) --j; 139 } 140 } else { 141 return true; 142 } 143 144 if (j == je) return false; 145 146 while (i != ie) { 147 if (i->start > j->start) { 148 std::swap(i, j); 149 std::swap(ie, je); 150 } 151 152 if (i->end > j->start) 153 return true; 154 ++i; 155 } 156 157 return false; 158 } 159 160 /// overlaps - Return true if the live interval overlaps a range specified 161 /// by [Start, End). 162 bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const { 163 assert(Start < End && "Invalid range"); 164 const_iterator I = begin(); 165 const_iterator E = end(); 166 const_iterator si = std::upper_bound(I, E, Start); 167 const_iterator ei = std::upper_bound(I, E, End); 168 if (si != ei) 169 return true; 170 if (si == I) 171 return false; 172 --si; 173 return si->contains(Start); 174 } 175 176 /// extendIntervalEndTo - This method is used when we want to extend the range 177 /// specified by I to end at the specified endpoint. To do this, we should 178 /// merge and eliminate all ranges that this will overlap with. The iterator is 179 /// not invalidated. 180 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) { 181 assert(I != ranges.end() && "Not a valid interval!"); 182 VNInfo *ValNo = I->valno; 183 184 // Search for the first interval that we can't merge with. 185 Ranges::iterator MergeTo = next(I); 186 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) { 187 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); 188 } 189 190 // If NewEnd was in the middle of an interval, make sure to get its endpoint. 191 I->end = std::max(NewEnd, prior(MergeTo)->end); 192 193 // Erase any dead ranges. 194 ranges.erase(next(I), MergeTo); 195 196 // If the newly formed range now touches the range after it and if they have 197 // the same value number, merge the two ranges into one range. 198 Ranges::iterator Next = next(I); 199 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) { 200 I->end = Next->end; 201 ranges.erase(Next); 202 } 203 } 204 205 206 /// extendIntervalStartTo - This method is used when we want to extend the range 207 /// specified by I to start at the specified endpoint. To do this, we should 208 /// merge and eliminate all ranges that this will overlap with. 209 LiveInterval::Ranges::iterator 210 LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) { 211 assert(I != ranges.end() && "Not a valid interval!"); 212 VNInfo *ValNo = I->valno; 213 214 // Search for the first interval that we can't merge with. 215 Ranges::iterator MergeTo = I; 216 do { 217 if (MergeTo == ranges.begin()) { 218 I->start = NewStart; 219 ranges.erase(MergeTo, I); 220 return I; 221 } 222 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); 223 --MergeTo; 224 } while (NewStart <= MergeTo->start); 225 226 // If we start in the middle of another interval, just delete a range and 227 // extend that interval. 228 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) { 229 MergeTo->end = I->end; 230 } else { 231 // Otherwise, extend the interval right after. 232 ++MergeTo; 233 MergeTo->start = NewStart; 234 MergeTo->end = I->end; 235 } 236 237 ranges.erase(next(MergeTo), next(I)); 238 return MergeTo; 239 } 240 241 LiveInterval::iterator 242 LiveInterval::addRangeFrom(LiveRange LR, iterator From) { 243 SlotIndex Start = LR.start, End = LR.end; 244 iterator it = std::upper_bound(From, ranges.end(), Start); 245 246 // If the inserted interval starts in the middle or right at the end of 247 // another interval, just extend that interval to contain the range of LR. 248 if (it != ranges.begin()) { 249 iterator B = prior(it); 250 if (LR.valno == B->valno) { 251 if (B->start <= Start && B->end >= Start) { 252 extendIntervalEndTo(B, End); 253 return B; 254 } 255 } else { 256 // Check to make sure that we are not overlapping two live ranges with 257 // different valno's. 258 assert(B->end <= Start && 259 "Cannot overlap two LiveRanges with differing ValID's" 260 " (did you def the same reg twice in a MachineInstr?)"); 261 } 262 } 263 264 // Otherwise, if this range ends in the middle of, or right next to, another 265 // interval, merge it into that interval. 266 if (it != ranges.end()) { 267 if (LR.valno == it->valno) { 268 if (it->start <= End) { 269 it = extendIntervalStartTo(it, Start); 270 271 // If LR is a complete superset of an interval, we may need to grow its 272 // endpoint as well. 273 if (End > it->end) 274 extendIntervalEndTo(it, End); 275 return it; 276 } 277 } else { 278 // Check to make sure that we are not overlapping two live ranges with 279 // different valno's. 280 assert(it->start >= End && 281 "Cannot overlap two LiveRanges with differing ValID's"); 282 } 283 } 284 285 // Otherwise, this is just a new range that doesn't interact with anything. 286 // Insert it. 287 return ranges.insert(it, LR); 288 } 289 290 /// isInOneLiveRange - Return true if the range specified is entirely in 291 /// a single LiveRange of the live interval. 292 bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) { 293 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start); 294 if (I == ranges.begin()) 295 return false; 296 --I; 297 return I->containsRange(Start, End); 298 } 299 300 301 /// removeRange - Remove the specified range from this interval. Note that 302 /// the range must be in a single LiveRange in its entirety. 303 void LiveInterval::removeRange(SlotIndex Start, SlotIndex End, 304 bool RemoveDeadValNo) { 305 // Find the LiveRange containing this span. 306 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start); 307 assert(I != ranges.begin() && "Range is not in interval!"); 308 --I; 309 assert(I->containsRange(Start, End) && "Range is not entirely in interval!"); 310 311 // If the span we are removing is at the start of the LiveRange, adjust it. 312 VNInfo *ValNo = I->valno; 313 if (I->start == Start) { 314 if (I->end == End) { 315 if (RemoveDeadValNo) { 316 // Check if val# is dead. 317 bool isDead = true; 318 for (const_iterator II = begin(), EE = end(); II != EE; ++II) 319 if (II != I && II->valno == ValNo) { 320 isDead = false; 321 break; 322 } 323 if (isDead) { 324 // Now that ValNo is dead, remove it. If it is the largest value 325 // number, just nuke it (and any other deleted values neighboring it), 326 // otherwise mark it as ~1U so it can be nuked later. 327 if (ValNo->id == getNumValNums()-1) { 328 do { 329 valnos.pop_back(); 330 } while (!valnos.empty() && valnos.back()->isUnused()); 331 } else { 332 ValNo->setIsUnused(true); 333 } 334 } 335 } 336 337 ranges.erase(I); // Removed the whole LiveRange. 338 } else 339 I->start = End; 340 return; 341 } 342 343 // Otherwise if the span we are removing is at the end of the LiveRange, 344 // adjust the other way. 345 if (I->end == End) { 346 I->end = Start; 347 return; 348 } 349 350 // Otherwise, we are splitting the LiveRange into two pieces. 351 SlotIndex OldEnd = I->end; 352 I->end = Start; // Trim the old interval. 353 354 // Insert the new one. 355 ranges.insert(next(I), LiveRange(End, OldEnd, ValNo)); 356 } 357 358 /// removeValNo - Remove all the ranges defined by the specified value#. 359 /// Also remove the value# from value# list. 360 void LiveInterval::removeValNo(VNInfo *ValNo) { 361 if (empty()) return; 362 Ranges::iterator I = ranges.end(); 363 Ranges::iterator E = ranges.begin(); 364 do { 365 --I; 366 if (I->valno == ValNo) 367 ranges.erase(I); 368 } while (I != E); 369 // Now that ValNo is dead, remove it. If it is the largest value 370 // number, just nuke it (and any other deleted values neighboring it), 371 // otherwise mark it as ~1U so it can be nuked later. 372 if (ValNo->id == getNumValNums()-1) { 373 do { 374 valnos.pop_back(); 375 } while (!valnos.empty() && valnos.back()->isUnused()); 376 } else { 377 ValNo->setIsUnused(true); 378 } 379 } 380 381 /// getLiveRangeContaining - Return the live range that contains the 382 /// specified index, or null if there is none. 383 LiveInterval::const_iterator 384 LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const { 385 const_iterator It = std::upper_bound(begin(), end(), Idx); 386 if (It != ranges.begin()) { 387 --It; 388 if (It->contains(Idx)) 389 return It; 390 } 391 392 return end(); 393 } 394 395 LiveInterval::iterator 396 LiveInterval::FindLiveRangeContaining(SlotIndex Idx) { 397 iterator It = std::upper_bound(begin(), end(), Idx); 398 if (It != begin()) { 399 --It; 400 if (It->contains(Idx)) 401 return It; 402 } 403 404 return end(); 405 } 406 407 /// findDefinedVNInfo - Find the VNInfo defined by the specified 408 /// index (register interval). 409 VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const { 410 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end(); 411 i != e; ++i) { 412 if ((*i)->def == Idx) 413 return *i; 414 } 415 416 return 0; 417 } 418 419 /// findDefinedVNInfo - Find the VNInfo defined by the specified 420 /// register (stack inteval). 421 VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const { 422 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end(); 423 i != e; ++i) { 424 if ((*i)->getReg() == reg) 425 return *i; 426 } 427 return 0; 428 } 429 430 /// join - Join two live intervals (this, and other) together. This applies 431 /// mappings to the value numbers in the LHS/RHS intervals as specified. If 432 /// the intervals are not joinable, this aborts. 433 void LiveInterval::join(LiveInterval &Other, 434 const int *LHSValNoAssignments, 435 const int *RHSValNoAssignments, 436 SmallVector<VNInfo*, 16> &NewVNInfo, 437 MachineRegisterInfo *MRI) { 438 // Determine if any of our live range values are mapped. This is uncommon, so 439 // we want to avoid the interval scan if not. 440 bool MustMapCurValNos = false; 441 unsigned NumVals = getNumValNums(); 442 unsigned NumNewVals = NewVNInfo.size(); 443 for (unsigned i = 0; i != NumVals; ++i) { 444 unsigned LHSValID = LHSValNoAssignments[i]; 445 if (i != LHSValID || 446 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) 447 MustMapCurValNos = true; 448 } 449 450 // If we have to apply a mapping to our base interval assignment, rewrite it 451 // now. 452 if (MustMapCurValNos) { 453 // Map the first live range. 454 iterator OutIt = begin(); 455 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; 456 ++OutIt; 457 for (iterator I = OutIt, E = end(); I != E; ++I) { 458 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]]; 459 460 // If this live range has the same value # as its immediate predecessor, 461 // and if they are neighbors, remove one LiveRange. This happens when we 462 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #. 463 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) { 464 (OutIt-1)->end = OutIt->end; 465 } else { 466 if (I != OutIt) { 467 OutIt->start = I->start; 468 OutIt->end = I->end; 469 } 470 471 // Didn't merge, on to the next one. 472 ++OutIt; 473 } 474 } 475 476 // If we merge some live ranges, chop off the end. 477 ranges.erase(OutIt, end()); 478 } 479 480 // Remember assignements because val# ids are changing. 481 SmallVector<unsigned, 16> OtherAssignments; 482 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) 483 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]); 484 485 // Update val# info. Renumber them and make sure they all belong to this 486 // LiveInterval now. Also remove dead val#'s. 487 unsigned NumValNos = 0; 488 for (unsigned i = 0; i < NumNewVals; ++i) { 489 VNInfo *VNI = NewVNInfo[i]; 490 if (VNI) { 491 if (NumValNos >= NumVals) 492 valnos.push_back(VNI); 493 else 494 valnos[NumValNos] = VNI; 495 VNI->id = NumValNos++; // Renumber val#. 496 } 497 } 498 if (NumNewVals < NumVals) 499 valnos.resize(NumNewVals); // shrinkify 500 501 // Okay, now insert the RHS live ranges into the LHS. 502 iterator InsertPos = begin(); 503 unsigned RangeNo = 0; 504 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) { 505 // Map the valno in the other live range to the current live range. 506 I->valno = NewVNInfo[OtherAssignments[RangeNo]]; 507 assert(I->valno && "Adding a dead range?"); 508 InsertPos = addRangeFrom(*I, InsertPos); 509 } 510 511 ComputeJoinedWeight(Other); 512 513 // Update regalloc hint if currently there isn't one. 514 if (TargetRegisterInfo::isVirtualRegister(reg) && 515 TargetRegisterInfo::isVirtualRegister(Other.reg)) { 516 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(reg); 517 if (Hint.first == 0 && Hint.second == 0) { 518 std::pair<unsigned, unsigned> OtherHint = 519 MRI->getRegAllocationHint(Other.reg); 520 if (OtherHint.first || OtherHint.second) 521 MRI->setRegAllocationHint(reg, OtherHint.first, OtherHint.second); 522 } 523 } 524 } 525 526 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live 527 /// interval as the specified value number. The LiveRanges in RHS are 528 /// allowed to overlap with LiveRanges in the current interval, but only if 529 /// the overlapping LiveRanges have the specified value number. 530 void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, 531 VNInfo *LHSValNo) { 532 // TODO: Make this more efficient. 533 iterator InsertPos = begin(); 534 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { 535 // Map the valno in the other live range to the current live range. 536 LiveRange Tmp = *I; 537 Tmp.valno = LHSValNo; 538 InsertPos = addRangeFrom(Tmp, InsertPos); 539 } 540 } 541 542 543 /// MergeValueInAsValue - Merge all of the live ranges of a specific val# 544 /// in RHS into this live interval as the specified value number. 545 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the 546 /// current interval, it will replace the value numbers of the overlaped 547 /// live ranges with the specified value number. 548 void LiveInterval::MergeValueInAsValue( 549 const LiveInterval &RHS, 550 const VNInfo *RHSValNo, VNInfo *LHSValNo) { 551 SmallVector<VNInfo*, 4> ReplacedValNos; 552 iterator IP = begin(); 553 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { 554 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo"); 555 if (I->valno != RHSValNo) 556 continue; 557 SlotIndex Start = I->start, End = I->end; 558 IP = std::upper_bound(IP, end(), Start); 559 // If the start of this range overlaps with an existing liverange, trim it. 560 if (IP != begin() && IP[-1].end > Start) { 561 if (IP[-1].valno != LHSValNo) { 562 ReplacedValNos.push_back(IP[-1].valno); 563 IP[-1].valno = LHSValNo; // Update val#. 564 } 565 Start = IP[-1].end; 566 // Trimmed away the whole range? 567 if (Start >= End) continue; 568 } 569 // If the end of this range overlaps with an existing liverange, trim it. 570 if (IP != end() && End > IP->start) { 571 if (IP->valno != LHSValNo) { 572 ReplacedValNos.push_back(IP->valno); 573 IP->valno = LHSValNo; // Update val#. 574 } 575 End = IP->start; 576 // If this trimmed away the whole range, ignore it. 577 if (Start == End) continue; 578 } 579 580 // Map the valno in the other live range to the current live range. 581 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP); 582 } 583 584 585 SmallSet<VNInfo*, 4> Seen; 586 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) { 587 VNInfo *V1 = ReplacedValNos[i]; 588 if (Seen.insert(V1)) { 589 bool isDead = true; 590 for (const_iterator I = begin(), E = end(); I != E; ++I) 591 if (I->valno == V1) { 592 isDead = false; 593 break; 594 } 595 if (isDead) { 596 // Now that V1 is dead, remove it. If it is the largest value number, 597 // just nuke it (and any other deleted values neighboring it), otherwise 598 // mark it as ~1U so it can be nuked later. 599 if (V1->id == getNumValNums()-1) { 600 do { 601 valnos.pop_back(); 602 } while (!valnos.empty() && valnos.back()->isUnused()); 603 } else { 604 V1->setIsUnused(true); 605 } 606 } 607 } 608 } 609 } 610 611 612 /// MergeInClobberRanges - For any live ranges that are not defined in the 613 /// current interval, but are defined in the Clobbers interval, mark them 614 /// used with an unknown definition value. 615 void LiveInterval::MergeInClobberRanges(LiveIntervals &li_, 616 const LiveInterval &Clobbers, 617 VNInfo::Allocator &VNInfoAllocator) { 618 if (Clobbers.empty()) return; 619 620 DenseMap<VNInfo*, VNInfo*> ValNoMaps; 621 VNInfo *UnusedValNo = 0; 622 iterator IP = begin(); 623 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) { 624 // For every val# in the Clobbers interval, create a new "unknown" val#. 625 VNInfo *ClobberValNo = 0; 626 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno); 627 if (VI != ValNoMaps.end()) 628 ClobberValNo = VI->second; 629 else if (UnusedValNo) 630 ClobberValNo = UnusedValNo; 631 else { 632 UnusedValNo = ClobberValNo = 633 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator); 634 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo)); 635 } 636 637 bool Done = false; 638 SlotIndex Start = I->start, End = I->end; 639 // If a clobber range starts before an existing range and ends after 640 // it, the clobber range will need to be split into multiple ranges. 641 // Loop until the entire clobber range is handled. 642 while (!Done) { 643 Done = true; 644 IP = std::upper_bound(IP, end(), Start); 645 SlotIndex SubRangeStart = Start; 646 SlotIndex SubRangeEnd = End; 647 648 // If the start of this range overlaps with an existing liverange, trim it. 649 if (IP != begin() && IP[-1].end > SubRangeStart) { 650 SubRangeStart = IP[-1].end; 651 // Trimmed away the whole range? 652 if (SubRangeStart >= SubRangeEnd) continue; 653 } 654 // If the end of this range overlaps with an existing liverange, trim it. 655 if (IP != end() && SubRangeEnd > IP->start) { 656 // If the clobber live range extends beyond the existing live range, 657 // it'll need at least another live range, so set the flag to keep 658 // iterating. 659 if (SubRangeEnd > IP->end) { 660 Start = IP->end; 661 Done = false; 662 } 663 SubRangeEnd = IP->start; 664 // If this trimmed away the whole range, ignore it. 665 if (SubRangeStart == SubRangeEnd) continue; 666 } 667 668 // Insert the clobber interval. 669 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo), 670 IP); 671 UnusedValNo = 0; 672 } 673 } 674 675 if (UnusedValNo) { 676 // Delete the last unused val#. 677 valnos.pop_back(); 678 } 679 } 680 681 void LiveInterval::MergeInClobberRange(LiveIntervals &li_, 682 SlotIndex Start, 683 SlotIndex End, 684 VNInfo::Allocator &VNInfoAllocator) { 685 // Find a value # to use for the clobber ranges. If there is already a value# 686 // for unknown values, use it. 687 VNInfo *ClobberValNo = 688 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator); 689 690 iterator IP = begin(); 691 IP = std::upper_bound(IP, end(), Start); 692 693 // If the start of this range overlaps with an existing liverange, trim it. 694 if (IP != begin() && IP[-1].end > Start) { 695 Start = IP[-1].end; 696 // Trimmed away the whole range? 697 if (Start >= End) return; 698 } 699 // If the end of this range overlaps with an existing liverange, trim it. 700 if (IP != end() && End > IP->start) { 701 End = IP->start; 702 // If this trimmed away the whole range, ignore it. 703 if (Start == End) return; 704 } 705 706 // Insert the clobber interval. 707 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP); 708 } 709 710 /// MergeValueNumberInto - This method is called when two value nubmers 711 /// are found to be equivalent. This eliminates V1, replacing all 712 /// LiveRanges with the V1 value number with the V2 value number. This can 713 /// cause merging of V1/V2 values numbers and compaction of the value space. 714 VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { 715 assert(V1 != V2 && "Identical value#'s are always equivalent!"); 716 717 // This code actually merges the (numerically) larger value number into the 718 // smaller value number, which is likely to allow us to compactify the value 719 // space. The only thing we have to be careful of is to preserve the 720 // instruction that defines the result value. 721 722 // Make sure V2 is smaller than V1. 723 if (V1->id < V2->id) { 724 V1->copyFrom(*V2); 725 std::swap(V1, V2); 726 } 727 728 // Merge V1 live ranges into V2. 729 for (iterator I = begin(); I != end(); ) { 730 iterator LR = I++; 731 if (LR->valno != V1) continue; // Not a V1 LiveRange. 732 733 // Okay, we found a V1 live range. If it had a previous, touching, V2 live 734 // range, extend it. 735 if (LR != begin()) { 736 iterator Prev = LR-1; 737 if (Prev->valno == V2 && Prev->end == LR->start) { 738 Prev->end = LR->end; 739 740 // Erase this live-range. 741 ranges.erase(LR); 742 I = Prev+1; 743 LR = Prev; 744 } 745 } 746 747 // Okay, now we have a V1 or V2 live range that is maximally merged forward. 748 // Ensure that it is a V2 live-range. 749 LR->valno = V2; 750 751 // If we can merge it into later V2 live ranges, do so now. We ignore any 752 // following V1 live ranges, as they will be merged in subsequent iterations 753 // of the loop. 754 if (I != end()) { 755 if (I->start == LR->end && I->valno == V2) { 756 LR->end = I->end; 757 ranges.erase(I); 758 I = LR+1; 759 } 760 } 761 } 762 763 // Now that V1 is dead, remove it. If it is the largest value number, just 764 // nuke it (and any other deleted values neighboring it), otherwise mark it as 765 // ~1U so it can be nuked later. 766 if (V1->id == getNumValNums()-1) { 767 do { 768 valnos.pop_back(); 769 } while (valnos.back()->isUnused()); 770 } else { 771 V1->setIsUnused(true); 772 } 773 774 return V2; 775 } 776 777 void LiveInterval::Copy(const LiveInterval &RHS, 778 MachineRegisterInfo *MRI, 779 VNInfo::Allocator &VNInfoAllocator) { 780 ranges.clear(); 781 valnos.clear(); 782 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg); 783 MRI->setRegAllocationHint(reg, Hint.first, Hint.second); 784 785 weight = RHS.weight; 786 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) { 787 const VNInfo *VNI = RHS.getValNumInfo(i); 788 createValueCopy(VNI, VNInfoAllocator); 789 } 790 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) { 791 const LiveRange &LR = RHS.ranges[i]; 792 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id))); 793 } 794 } 795 796 unsigned LiveInterval::getSize() const { 797 unsigned Sum = 0; 798 for (const_iterator I = begin(), E = end(); I != E; ++I) 799 Sum += I->start.distance(I->end); 800 return Sum; 801 } 802 803 /// ComputeJoinedWeight - Set the weight of a live interval Joined 804 /// after Other has been merged into it. 805 void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) { 806 // If either of these intervals was spilled, the weight is the 807 // weight of the non-spilled interval. This can only happen with 808 // iterative coalescers. 809 810 if (Other.weight != HUGE_VALF) { 811 weight += Other.weight; 812 } 813 else if (weight == HUGE_VALF && 814 !TargetRegisterInfo::isPhysicalRegister(reg)) { 815 // Remove this assert if you have an iterative coalescer 816 assert(0 && "Joining to spilled interval"); 817 weight = Other.weight; 818 } 819 else { 820 // Otherwise the weight stays the same 821 // Remove this assert if you have an iterative coalescer 822 assert(0 && "Joining from spilled interval"); 823 } 824 } 825 826 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) { 827 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")"; 828 } 829 830 void LiveRange::dump() const { 831 dbgs() << *this << "\n"; 832 } 833 834 void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const { 835 if (isStackSlot()) 836 OS << "SS#" << getStackSlotIndex(); 837 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg)) 838 OS << TRI->getName(reg); 839 else 840 OS << "%reg" << reg; 841 842 OS << ',' << weight; 843 844 if (empty()) 845 OS << " EMPTY"; 846 else { 847 OS << " = "; 848 for (LiveInterval::Ranges::const_iterator I = ranges.begin(), 849 E = ranges.end(); I != E; ++I) { 850 OS << *I; 851 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo"); 852 } 853 } 854 855 // Print value number info. 856 if (getNumValNums()) { 857 OS << " "; 858 unsigned vnum = 0; 859 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; 860 ++i, ++vnum) { 861 const VNInfo *vni = *i; 862 if (vnum) OS << " "; 863 OS << vnum << "@"; 864 if (vni->isUnused()) { 865 OS << "x"; 866 } else { 867 if (!vni->isDefAccurate() && !vni->isPHIDef()) 868 OS << "?"; 869 else 870 OS << vni->def; 871 } 872 } 873 } 874 } 875 876 void LiveInterval::dump() const { 877 dbgs() << *this << "\n"; 878 } 879 880 881 void LiveRange::print(raw_ostream &os) const { 882 os << *this; 883 } 884