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