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