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