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