1 //===- LiveIntervals.cpp - Live Interval Analysis -------------------------===// 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 /// \file This file implements the LiveInterval analysis pass which is used 11 /// by the Linear Scan Register allocator. This pass linearizes the 12 /// basic blocks of the function in DFS order and computes live intervals for 13 /// each virtual and physical register. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/CodeGen/LiveIntervals.h" 18 #include "LiveRangeCalc.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/DepthFirstIterator.h" 21 #include "llvm/ADT/SmallPtrSet.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/iterator_range.h" 24 #include "llvm/Analysis/AliasAnalysis.h" 25 #include "llvm/CodeGen/LiveInterval.h" 26 #include "llvm/CodeGen/LiveVariables.h" 27 #include "llvm/CodeGen/MachineBasicBlock.h" 28 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 29 #include "llvm/CodeGen/MachineDominators.h" 30 #include "llvm/CodeGen/MachineFunction.h" 31 #include "llvm/CodeGen/MachineInstr.h" 32 #include "llvm/CodeGen/MachineInstrBundle.h" 33 #include "llvm/CodeGen/MachineOperand.h" 34 #include "llvm/CodeGen/MachineRegisterInfo.h" 35 #include "llvm/CodeGen/Passes.h" 36 #include "llvm/CodeGen/SlotIndexes.h" 37 #include "llvm/CodeGen/TargetRegisterInfo.h" 38 #include "llvm/CodeGen/TargetSubtargetInfo.h" 39 #include "llvm/CodeGen/VirtRegMap.h" 40 #include "llvm/Config/llvm-config.h" 41 #include "llvm/MC/LaneBitmask.h" 42 #include "llvm/MC/MCRegisterInfo.h" 43 #include "llvm/Pass.h" 44 #include "llvm/Support/BlockFrequency.h" 45 #include "llvm/Support/CommandLine.h" 46 #include "llvm/Support/Compiler.h" 47 #include "llvm/Support/Debug.h" 48 #include "llvm/Support/MathExtras.h" 49 #include "llvm/Support/raw_ostream.h" 50 #include <algorithm> 51 #include <cassert> 52 #include <cstdint> 53 #include <iterator> 54 #include <tuple> 55 #include <utility> 56 57 using namespace llvm; 58 59 #define DEBUG_TYPE "regalloc" 60 61 char LiveIntervals::ID = 0; 62 char &llvm::LiveIntervalsID = LiveIntervals::ID; 63 INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", 64 "Live Interval Analysis", false, false) 65 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 66 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 67 INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 68 INITIALIZE_PASS_END(LiveIntervals, "liveintervals", 69 "Live Interval Analysis", false, false) 70 71 #ifndef NDEBUG 72 static cl::opt<bool> EnablePrecomputePhysRegs( 73 "precompute-phys-liveness", cl::Hidden, 74 cl::desc("Eagerly compute live intervals for all physreg units.")); 75 #else 76 static bool EnablePrecomputePhysRegs = false; 77 #endif // NDEBUG 78 79 namespace llvm { 80 81 cl::opt<bool> UseSegmentSetForPhysRegs( 82 "use-segment-set-for-physregs", cl::Hidden, cl::init(true), 83 cl::desc( 84 "Use segment set for the computation of the live ranges of physregs.")); 85 86 } // end namespace llvm 87 88 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { 89 AU.setPreservesCFG(); 90 AU.addRequired<AAResultsWrapperPass>(); 91 AU.addPreserved<AAResultsWrapperPass>(); 92 AU.addPreserved<LiveVariables>(); 93 AU.addPreservedID(MachineLoopInfoID); 94 AU.addRequiredTransitiveID(MachineDominatorsID); 95 AU.addPreservedID(MachineDominatorsID); 96 AU.addPreserved<SlotIndexes>(); 97 AU.addRequiredTransitive<SlotIndexes>(); 98 MachineFunctionPass::getAnalysisUsage(AU); 99 } 100 101 LiveIntervals::LiveIntervals() : MachineFunctionPass(ID) { 102 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 103 } 104 105 LiveIntervals::~LiveIntervals() { 106 delete LRCalc; 107 } 108 109 void LiveIntervals::releaseMemory() { 110 // Free the live intervals themselves. 111 for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i) 112 delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)]; 113 VirtRegIntervals.clear(); 114 RegMaskSlots.clear(); 115 RegMaskBits.clear(); 116 RegMaskBlocks.clear(); 117 118 for (LiveRange *LR : RegUnitRanges) 119 delete LR; 120 RegUnitRanges.clear(); 121 122 // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd. 123 VNInfoAllocator.Reset(); 124 } 125 126 bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { 127 MF = &fn; 128 MRI = &MF->getRegInfo(); 129 TRI = MF->getSubtarget().getRegisterInfo(); 130 TII = MF->getSubtarget().getInstrInfo(); 131 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 132 Indexes = &getAnalysis<SlotIndexes>(); 133 DomTree = &getAnalysis<MachineDominatorTree>(); 134 135 if (!LRCalc) 136 LRCalc = new LiveRangeCalc(); 137 138 // Allocate space for all virtual registers. 139 VirtRegIntervals.resize(MRI->getNumVirtRegs()); 140 141 computeVirtRegs(); 142 computeRegMasks(); 143 computeLiveInRegUnits(); 144 145 if (EnablePrecomputePhysRegs) { 146 // For stress testing, precompute live ranges of all physical register 147 // units, including reserved registers. 148 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) 149 getRegUnit(i); 150 } 151 DEBUG(dump()); 152 return true; 153 } 154 155 void LiveIntervals::print(raw_ostream &OS, const Module* ) const { 156 OS << "********** INTERVALS **********\n"; 157 158 // Dump the regunits. 159 for (unsigned Unit = 0, UnitE = RegUnitRanges.size(); Unit != UnitE; ++Unit) 160 if (LiveRange *LR = RegUnitRanges[Unit]) 161 OS << printRegUnit(Unit, TRI) << ' ' << *LR << '\n'; 162 163 // Dump the virtregs. 164 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 165 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 166 if (hasInterval(Reg)) 167 OS << getInterval(Reg) << '\n'; 168 } 169 170 OS << "RegMasks:"; 171 for (SlotIndex Idx : RegMaskSlots) 172 OS << ' ' << Idx; 173 OS << '\n'; 174 175 printInstrs(OS); 176 } 177 178 void LiveIntervals::printInstrs(raw_ostream &OS) const { 179 OS << "********** MACHINEINSTRS **********\n"; 180 MF->print(OS, Indexes); 181 } 182 183 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 184 LLVM_DUMP_METHOD void LiveIntervals::dumpInstrs() const { 185 printInstrs(dbgs()); 186 } 187 #endif 188 189 LiveInterval* LiveIntervals::createInterval(unsigned reg) { 190 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? huge_valf : 0.0F; 191 return new LiveInterval(reg, Weight); 192 } 193 194 /// Compute the live interval of a virtual register, based on defs and uses. 195 void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) { 196 assert(LRCalc && "LRCalc not initialized."); 197 assert(LI.empty() && "Should only compute empty intervals."); 198 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 199 LRCalc->calculate(LI, MRI->shouldTrackSubRegLiveness(LI.reg)); 200 computeDeadValues(LI, nullptr); 201 } 202 203 void LiveIntervals::computeVirtRegs() { 204 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 205 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 206 if (MRI->reg_nodbg_empty(Reg)) 207 continue; 208 createAndComputeVirtRegInterval(Reg); 209 } 210 } 211 212 void LiveIntervals::computeRegMasks() { 213 RegMaskBlocks.resize(MF->getNumBlockIDs()); 214 215 // Find all instructions with regmask operands. 216 for (const MachineBasicBlock &MBB : *MF) { 217 std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB.getNumber()]; 218 RMB.first = RegMaskSlots.size(); 219 220 // Some block starts, such as EH funclets, create masks. 221 if (const uint32_t *Mask = MBB.getBeginClobberMask(TRI)) { 222 RegMaskSlots.push_back(Indexes->getMBBStartIdx(&MBB)); 223 RegMaskBits.push_back(Mask); 224 } 225 226 for (const MachineInstr &MI : MBB) { 227 for (const MachineOperand &MO : MI.operands()) { 228 if (!MO.isRegMask()) 229 continue; 230 RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot()); 231 RegMaskBits.push_back(MO.getRegMask()); 232 } 233 } 234 235 // Some block ends, such as funclet returns, create masks. Put the mask on 236 // the last instruction of the block, because MBB slot index intervals are 237 // half-open. 238 if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) { 239 assert(!MBB.empty() && "empty return block?"); 240 RegMaskSlots.push_back( 241 Indexes->getInstructionIndex(MBB.back()).getRegSlot()); 242 RegMaskBits.push_back(Mask); 243 } 244 245 // Compute the number of register mask instructions in this block. 246 RMB.second = RegMaskSlots.size() - RMB.first; 247 } 248 } 249 250 //===----------------------------------------------------------------------===// 251 // Register Unit Liveness 252 //===----------------------------------------------------------------------===// 253 // 254 // Fixed interference typically comes from ABI boundaries: Function arguments 255 // and return values are passed in fixed registers, and so are exception 256 // pointers entering landing pads. Certain instructions require values to be 257 // present in specific registers. That is also represented through fixed 258 // interference. 259 // 260 261 /// Compute the live range of a register unit, based on the uses and defs of 262 /// aliasing registers. The range should be empty, or contain only dead 263 /// phi-defs from ABI blocks. 264 void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) { 265 assert(LRCalc && "LRCalc not initialized."); 266 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 267 268 // The physregs aliasing Unit are the roots and their super-registers. 269 // Create all values as dead defs before extending to uses. Note that roots 270 // may share super-registers. That's OK because createDeadDefs() is 271 // idempotent. It is very rare for a register unit to have multiple roots, so 272 // uniquing super-registers is probably not worthwhile. 273 bool IsReserved = false; 274 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { 275 bool IsRootReserved = true; 276 for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true); 277 Super.isValid(); ++Super) { 278 unsigned Reg = *Super; 279 if (!MRI->reg_empty(Reg)) 280 LRCalc->createDeadDefs(LR, Reg); 281 // A register unit is considered reserved if all its roots and all their 282 // super registers are reserved. 283 if (!MRI->isReserved(Reg)) 284 IsRootReserved = false; 285 } 286 IsReserved |= IsRootReserved; 287 } 288 assert(IsReserved == MRI->isReservedRegUnit(Unit) && 289 "reserved computation mismatch"); 290 291 // Now extend LR to reach all uses. 292 // Ignore uses of reserved registers. We only track defs of those. 293 if (!IsReserved) { 294 for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { 295 for (MCSuperRegIterator Super(*Root, TRI, /*IncludeSelf=*/true); 296 Super.isValid(); ++Super) { 297 unsigned Reg = *Super; 298 if (!MRI->reg_empty(Reg)) 299 LRCalc->extendToUses(LR, Reg); 300 } 301 } 302 } 303 304 // Flush the segment set to the segment vector. 305 if (UseSegmentSetForPhysRegs) 306 LR.flushSegmentSet(); 307 } 308 309 /// Precompute the live ranges of any register units that are live-in to an ABI 310 /// block somewhere. Register values can appear without a corresponding def when 311 /// entering the entry block or a landing pad. 312 void LiveIntervals::computeLiveInRegUnits() { 313 RegUnitRanges.resize(TRI->getNumRegUnits()); 314 DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n"); 315 316 // Keep track of the live range sets allocated. 317 SmallVector<unsigned, 8> NewRanges; 318 319 // Check all basic blocks for live-ins. 320 for (const MachineBasicBlock &MBB : *MF) { 321 // We only care about ABI blocks: Entry + landing pads. 322 if ((&MBB != &MF->front() && !MBB.isEHPad()) || MBB.livein_empty()) 323 continue; 324 325 // Create phi-defs at Begin for all live-in registers. 326 SlotIndex Begin = Indexes->getMBBStartIdx(&MBB); 327 DEBUG(dbgs() << Begin << "\t" << printMBBReference(MBB)); 328 for (const auto &LI : MBB.liveins()) { 329 for (MCRegUnitIterator Units(LI.PhysReg, TRI); Units.isValid(); ++Units) { 330 unsigned Unit = *Units; 331 LiveRange *LR = RegUnitRanges[Unit]; 332 if (!LR) { 333 // Use segment set to speed-up initial computation of the live range. 334 LR = RegUnitRanges[Unit] = new LiveRange(UseSegmentSetForPhysRegs); 335 NewRanges.push_back(Unit); 336 } 337 VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator()); 338 (void)VNI; 339 DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI) << '#' << VNI->id); 340 } 341 } 342 DEBUG(dbgs() << '\n'); 343 } 344 DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n"); 345 346 // Compute the 'normal' part of the ranges. 347 for (unsigned Unit : NewRanges) 348 computeRegUnitRange(*RegUnitRanges[Unit], Unit); 349 } 350 351 static void createSegmentsForValues(LiveRange &LR, 352 iterator_range<LiveInterval::vni_iterator> VNIs) { 353 for (VNInfo *VNI : VNIs) { 354 if (VNI->isUnused()) 355 continue; 356 SlotIndex Def = VNI->def; 357 LR.addSegment(LiveRange::Segment(Def, Def.getDeadSlot(), VNI)); 358 } 359 } 360 361 using ShrinkToUsesWorkList = SmallVector<std::pair<SlotIndex, VNInfo*>, 16>; 362 363 static void extendSegmentsToUses(LiveRange &LR, const SlotIndexes &Indexes, 364 ShrinkToUsesWorkList &WorkList, 365 const LiveRange &OldRange) { 366 // Keep track of the PHIs that are in use. 367 SmallPtrSet<VNInfo*, 8> UsedPHIs; 368 // Blocks that have already been added to WorkList as live-out. 369 SmallPtrSet<const MachineBasicBlock*, 16> LiveOut; 370 371 // Extend intervals to reach all uses in WorkList. 372 while (!WorkList.empty()) { 373 SlotIndex Idx = WorkList.back().first; 374 VNInfo *VNI = WorkList.back().second; 375 WorkList.pop_back(); 376 const MachineBasicBlock *MBB = Indexes.getMBBFromIndex(Idx.getPrevSlot()); 377 SlotIndex BlockStart = Indexes.getMBBStartIdx(MBB); 378 379 // Extend the live range for VNI to be live at Idx. 380 if (VNInfo *ExtVNI = LR.extendInBlock(BlockStart, Idx)) { 381 assert(ExtVNI == VNI && "Unexpected existing value number"); 382 (void)ExtVNI; 383 // Is this a PHIDef we haven't seen before? 384 if (!VNI->isPHIDef() || VNI->def != BlockStart || 385 !UsedPHIs.insert(VNI).second) 386 continue; 387 // The PHI is live, make sure the predecessors are live-out. 388 for (const MachineBasicBlock *Pred : MBB->predecessors()) { 389 if (!LiveOut.insert(Pred).second) 390 continue; 391 SlotIndex Stop = Indexes.getMBBEndIdx(Pred); 392 // A predecessor is not required to have a live-out value for a PHI. 393 if (VNInfo *PVNI = OldRange.getVNInfoBefore(Stop)) 394 WorkList.push_back(std::make_pair(Stop, PVNI)); 395 } 396 continue; 397 } 398 399 // VNI is live-in to MBB. 400 DEBUG(dbgs() << " live-in at " << BlockStart << '\n'); 401 LR.addSegment(LiveRange::Segment(BlockStart, Idx, VNI)); 402 403 // Make sure VNI is live-out from the predecessors. 404 for (const MachineBasicBlock *Pred : MBB->predecessors()) { 405 if (!LiveOut.insert(Pred).second) 406 continue; 407 SlotIndex Stop = Indexes.getMBBEndIdx(Pred); 408 assert(OldRange.getVNInfoBefore(Stop) == VNI && 409 "Wrong value out of predecessor"); 410 WorkList.push_back(std::make_pair(Stop, VNI)); 411 } 412 } 413 } 414 415 bool LiveIntervals::shrinkToUses(LiveInterval *li, 416 SmallVectorImpl<MachineInstr*> *dead) { 417 DEBUG(dbgs() << "Shrink: " << *li << '\n'); 418 assert(TargetRegisterInfo::isVirtualRegister(li->reg) 419 && "Can only shrink virtual registers"); 420 421 // Shrink subregister live ranges. 422 bool NeedsCleanup = false; 423 for (LiveInterval::SubRange &S : li->subranges()) { 424 shrinkToUses(S, li->reg); 425 if (S.empty()) 426 NeedsCleanup = true; 427 } 428 if (NeedsCleanup) 429 li->removeEmptySubRanges(); 430 431 // Find all the values used, including PHI kills. 432 ShrinkToUsesWorkList WorkList; 433 434 // Visit all instructions reading li->reg. 435 unsigned Reg = li->reg; 436 for (MachineInstr &UseMI : MRI->reg_instructions(Reg)) { 437 if (UseMI.isDebugValue() || !UseMI.readsVirtualRegister(Reg)) 438 continue; 439 SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); 440 LiveQueryResult LRQ = li->Query(Idx); 441 VNInfo *VNI = LRQ.valueIn(); 442 if (!VNI) { 443 // This shouldn't happen: readsVirtualRegister returns true, but there is 444 // no live value. It is likely caused by a target getting <undef> flags 445 // wrong. 446 DEBUG(dbgs() << Idx << '\t' << UseMI 447 << "Warning: Instr claims to read non-existent value in " 448 << *li << '\n'); 449 continue; 450 } 451 // Special case: An early-clobber tied operand reads and writes the 452 // register one slot early. 453 if (VNInfo *DefVNI = LRQ.valueDefined()) 454 Idx = DefVNI->def; 455 456 WorkList.push_back(std::make_pair(Idx, VNI)); 457 } 458 459 // Create new live ranges with only minimal live segments per def. 460 LiveRange NewLR; 461 createSegmentsForValues(NewLR, make_range(li->vni_begin(), li->vni_end())); 462 extendSegmentsToUses(NewLR, *Indexes, WorkList, *li); 463 464 // Move the trimmed segments back. 465 li->segments.swap(NewLR.segments); 466 467 // Handle dead values. 468 bool CanSeparate = computeDeadValues(*li, dead); 469 DEBUG(dbgs() << "Shrunk: " << *li << '\n'); 470 return CanSeparate; 471 } 472 473 bool LiveIntervals::computeDeadValues(LiveInterval &LI, 474 SmallVectorImpl<MachineInstr*> *dead) { 475 bool MayHaveSplitComponents = false; 476 for (VNInfo *VNI : LI.valnos) { 477 if (VNI->isUnused()) 478 continue; 479 SlotIndex Def = VNI->def; 480 LiveRange::iterator I = LI.FindSegmentContaining(Def); 481 assert(I != LI.end() && "Missing segment for VNI"); 482 483 // Is the register live before? Otherwise we may have to add a read-undef 484 // flag for subregister defs. 485 unsigned VReg = LI.reg; 486 if (MRI->shouldTrackSubRegLiveness(VReg)) { 487 if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) { 488 MachineInstr *MI = getInstructionFromIndex(Def); 489 MI->setRegisterDefReadUndef(VReg); 490 } 491 } 492 493 if (I->end != Def.getDeadSlot()) 494 continue; 495 if (VNI->isPHIDef()) { 496 // This is a dead PHI. Remove it. 497 VNI->markUnused(); 498 LI.removeSegment(I); 499 DEBUG(dbgs() << "Dead PHI at " << Def << " may separate interval\n"); 500 MayHaveSplitComponents = true; 501 } else { 502 // This is a dead def. Make sure the instruction knows. 503 MachineInstr *MI = getInstructionFromIndex(Def); 504 assert(MI && "No instruction defining live value"); 505 MI->addRegisterDead(LI.reg, TRI); 506 if (dead && MI->allDefsAreDead()) { 507 DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI); 508 dead->push_back(MI); 509 } 510 } 511 } 512 return MayHaveSplitComponents; 513 } 514 515 void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, unsigned Reg) { 516 DEBUG(dbgs() << "Shrink: " << SR << '\n'); 517 assert(TargetRegisterInfo::isVirtualRegister(Reg) 518 && "Can only shrink virtual registers"); 519 // Find all the values used, including PHI kills. 520 ShrinkToUsesWorkList WorkList; 521 522 // Visit all instructions reading Reg. 523 SlotIndex LastIdx; 524 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { 525 // Skip "undef" uses. 526 if (!MO.readsReg()) 527 continue; 528 // Maybe the operand is for a subregister we don't care about. 529 unsigned SubReg = MO.getSubReg(); 530 if (SubReg != 0) { 531 LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg); 532 if ((LaneMask & SR.LaneMask).none()) 533 continue; 534 } 535 // We only need to visit each instruction once. 536 MachineInstr *UseMI = MO.getParent(); 537 SlotIndex Idx = getInstructionIndex(*UseMI).getRegSlot(); 538 if (Idx == LastIdx) 539 continue; 540 LastIdx = Idx; 541 542 LiveQueryResult LRQ = SR.Query(Idx); 543 VNInfo *VNI = LRQ.valueIn(); 544 // For Subranges it is possible that only undef values are left in that 545 // part of the subregister, so there is no real liverange at the use 546 if (!VNI) 547 continue; 548 549 // Special case: An early-clobber tied operand reads and writes the 550 // register one slot early. 551 if (VNInfo *DefVNI = LRQ.valueDefined()) 552 Idx = DefVNI->def; 553 554 WorkList.push_back(std::make_pair(Idx, VNI)); 555 } 556 557 // Create a new live ranges with only minimal live segments per def. 558 LiveRange NewLR; 559 createSegmentsForValues(NewLR, make_range(SR.vni_begin(), SR.vni_end())); 560 extendSegmentsToUses(NewLR, *Indexes, WorkList, SR); 561 562 // Move the trimmed ranges back. 563 SR.segments.swap(NewLR.segments); 564 565 // Remove dead PHI value numbers 566 for (VNInfo *VNI : SR.valnos) { 567 if (VNI->isUnused()) 568 continue; 569 const LiveRange::Segment *Segment = SR.getSegmentContaining(VNI->def); 570 assert(Segment != nullptr && "Missing segment for VNI"); 571 if (Segment->end != VNI->def.getDeadSlot()) 572 continue; 573 if (VNI->isPHIDef()) { 574 // This is a dead PHI. Remove it. 575 DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n"); 576 VNI->markUnused(); 577 SR.removeSegment(*Segment); 578 } 579 } 580 581 DEBUG(dbgs() << "Shrunk: " << SR << '\n'); 582 } 583 584 void LiveIntervals::extendToIndices(LiveRange &LR, 585 ArrayRef<SlotIndex> Indices, 586 ArrayRef<SlotIndex> Undefs) { 587 assert(LRCalc && "LRCalc not initialized."); 588 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 589 for (SlotIndex Idx : Indices) 590 LRCalc->extend(LR, Idx, /*PhysReg=*/0, Undefs); 591 } 592 593 void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill, 594 SmallVectorImpl<SlotIndex> *EndPoints) { 595 LiveQueryResult LRQ = LR.Query(Kill); 596 VNInfo *VNI = LRQ.valueOutOrDead(); 597 if (!VNI) 598 return; 599 600 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill); 601 SlotIndex MBBEnd = Indexes->getMBBEndIdx(KillMBB); 602 603 // If VNI isn't live out from KillMBB, the value is trivially pruned. 604 if (LRQ.endPoint() < MBBEnd) { 605 LR.removeSegment(Kill, LRQ.endPoint()); 606 if (EndPoints) EndPoints->push_back(LRQ.endPoint()); 607 return; 608 } 609 610 // VNI is live out of KillMBB. 611 LR.removeSegment(Kill, MBBEnd); 612 if (EndPoints) EndPoints->push_back(MBBEnd); 613 614 // Find all blocks that are reachable from KillMBB without leaving VNI's live 615 // range. It is possible that KillMBB itself is reachable, so start a DFS 616 // from each successor. 617 using VisitedTy = df_iterator_default_set<MachineBasicBlock*,9>; 618 VisitedTy Visited; 619 for (MachineBasicBlock *Succ : KillMBB->successors()) { 620 for (df_ext_iterator<MachineBasicBlock*, VisitedTy> 621 I = df_ext_begin(Succ, Visited), E = df_ext_end(Succ, Visited); 622 I != E;) { 623 MachineBasicBlock *MBB = *I; 624 625 // Check if VNI is live in to MBB. 626 SlotIndex MBBStart, MBBEnd; 627 std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB); 628 LiveQueryResult LRQ = LR.Query(MBBStart); 629 if (LRQ.valueIn() != VNI) { 630 // This block isn't part of the VNI segment. Prune the search. 631 I.skipChildren(); 632 continue; 633 } 634 635 // Prune the search if VNI is killed in MBB. 636 if (LRQ.endPoint() < MBBEnd) { 637 LR.removeSegment(MBBStart, LRQ.endPoint()); 638 if (EndPoints) EndPoints->push_back(LRQ.endPoint()); 639 I.skipChildren(); 640 continue; 641 } 642 643 // VNI is live through MBB. 644 LR.removeSegment(MBBStart, MBBEnd); 645 if (EndPoints) EndPoints->push_back(MBBEnd); 646 ++I; 647 } 648 } 649 } 650 651 //===----------------------------------------------------------------------===// 652 // Register allocator hooks. 653 // 654 655 void LiveIntervals::addKillFlags(const VirtRegMap *VRM) { 656 // Keep track of regunit ranges. 657 SmallVector<std::pair<const LiveRange*, LiveRange::const_iterator>, 8> RU; 658 // Keep track of subregister ranges. 659 SmallVector<std::pair<const LiveInterval::SubRange*, 660 LiveRange::const_iterator>, 4> SRs; 661 662 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 663 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 664 if (MRI->reg_nodbg_empty(Reg)) 665 continue; 666 const LiveInterval &LI = getInterval(Reg); 667 if (LI.empty()) 668 continue; 669 670 // Find the regunit intervals for the assigned register. They may overlap 671 // the virtual register live range, cancelling any kills. 672 RU.clear(); 673 for (MCRegUnitIterator Unit(VRM->getPhys(Reg), TRI); Unit.isValid(); 674 ++Unit) { 675 const LiveRange &RURange = getRegUnit(*Unit); 676 if (RURange.empty()) 677 continue; 678 RU.push_back(std::make_pair(&RURange, RURange.find(LI.begin()->end))); 679 } 680 681 if (MRI->subRegLivenessEnabled()) { 682 SRs.clear(); 683 for (const LiveInterval::SubRange &SR : LI.subranges()) { 684 SRs.push_back(std::make_pair(&SR, SR.find(LI.begin()->end))); 685 } 686 } 687 688 // Every instruction that kills Reg corresponds to a segment range end 689 // point. 690 for (LiveInterval::const_iterator RI = LI.begin(), RE = LI.end(); RI != RE; 691 ++RI) { 692 // A block index indicates an MBB edge. 693 if (RI->end.isBlock()) 694 continue; 695 MachineInstr *MI = getInstructionFromIndex(RI->end); 696 if (!MI) 697 continue; 698 699 // Check if any of the regunits are live beyond the end of RI. That could 700 // happen when a physreg is defined as a copy of a virtreg: 701 // 702 // %eax = COPY %5 703 // FOO %5 <--- MI, cancel kill because %eax is live. 704 // BAR killed %eax 705 // 706 // There should be no kill flag on FOO when %5 is rewritten as %eax. 707 for (auto &RUP : RU) { 708 const LiveRange &RURange = *RUP.first; 709 LiveRange::const_iterator &I = RUP.second; 710 if (I == RURange.end()) 711 continue; 712 I = RURange.advanceTo(I, RI->end); 713 if (I == RURange.end() || I->start >= RI->end) 714 continue; 715 // I is overlapping RI. 716 goto CancelKill; 717 } 718 719 if (MRI->subRegLivenessEnabled()) { 720 // When reading a partial undefined value we must not add a kill flag. 721 // The regalloc might have used the undef lane for something else. 722 // Example: 723 // %1 = ... ; R32: %1 724 // %2:high16 = ... ; R64: %2 725 // = read killed %2 ; R64: %2 726 // = read %1 ; R32: %1 727 // The <kill> flag is correct for %2, but the register allocator may 728 // assign R0L to %1, and R0 to %2 because the low 32bits of R0 729 // are actually never written by %2. After assignment the <kill> 730 // flag at the read instruction is invalid. 731 LaneBitmask DefinedLanesMask; 732 if (!SRs.empty()) { 733 // Compute a mask of lanes that are defined. 734 DefinedLanesMask = LaneBitmask::getNone(); 735 for (auto &SRP : SRs) { 736 const LiveInterval::SubRange &SR = *SRP.first; 737 LiveRange::const_iterator &I = SRP.second; 738 if (I == SR.end()) 739 continue; 740 I = SR.advanceTo(I, RI->end); 741 if (I == SR.end() || I->start >= RI->end) 742 continue; 743 // I is overlapping RI 744 DefinedLanesMask |= SR.LaneMask; 745 } 746 } else 747 DefinedLanesMask = LaneBitmask::getAll(); 748 749 bool IsFullWrite = false; 750 for (const MachineOperand &MO : MI->operands()) { 751 if (!MO.isReg() || MO.getReg() != Reg) 752 continue; 753 if (MO.isUse()) { 754 // Reading any undefined lanes? 755 LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg()); 756 if ((UseMask & ~DefinedLanesMask).any()) 757 goto CancelKill; 758 } else if (MO.getSubReg() == 0) { 759 // Writing to the full register? 760 assert(MO.isDef()); 761 IsFullWrite = true; 762 } 763 } 764 765 // If an instruction writes to a subregister, a new segment starts in 766 // the LiveInterval. But as this is only overriding part of the register 767 // adding kill-flags is not correct here after registers have been 768 // assigned. 769 if (!IsFullWrite) { 770 // Next segment has to be adjacent in the subregister write case. 771 LiveRange::const_iterator N = std::next(RI); 772 if (N != LI.end() && N->start == RI->end) 773 goto CancelKill; 774 } 775 } 776 777 MI->addRegisterKilled(Reg, nullptr); 778 continue; 779 CancelKill: 780 MI->clearRegisterKills(Reg, nullptr); 781 } 782 } 783 } 784 785 MachineBasicBlock* 786 LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const { 787 // A local live range must be fully contained inside the block, meaning it is 788 // defined and killed at instructions, not at block boundaries. It is not 789 // live in or out of any block. 790 // 791 // It is technically possible to have a PHI-defined live range identical to a 792 // single block, but we are going to return false in that case. 793 794 SlotIndex Start = LI.beginIndex(); 795 if (Start.isBlock()) 796 return nullptr; 797 798 SlotIndex Stop = LI.endIndex(); 799 if (Stop.isBlock()) 800 return nullptr; 801 802 // getMBBFromIndex doesn't need to search the MBB table when both indexes 803 // belong to proper instructions. 804 MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start); 805 MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop); 806 return MBB1 == MBB2 ? MBB1 : nullptr; 807 } 808 809 bool 810 LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const { 811 for (const VNInfo *PHI : LI.valnos) { 812 if (PHI->isUnused() || !PHI->isPHIDef()) 813 continue; 814 const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def); 815 // Conservatively return true instead of scanning huge predecessor lists. 816 if (PHIMBB->pred_size() > 100) 817 return true; 818 for (const MachineBasicBlock *Pred : PHIMBB->predecessors()) 819 if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(Pred))) 820 return true; 821 } 822 return false; 823 } 824 825 float LiveIntervals::getSpillWeight(bool isDef, bool isUse, 826 const MachineBlockFrequencyInfo *MBFI, 827 const MachineInstr &MI) { 828 return getSpillWeight(isDef, isUse, MBFI, MI.getParent()); 829 } 830 831 float LiveIntervals::getSpillWeight(bool isDef, bool isUse, 832 const MachineBlockFrequencyInfo *MBFI, 833 const MachineBasicBlock *MBB) { 834 BlockFrequency Freq = MBFI->getBlockFreq(MBB); 835 const float Scale = 1.0f / MBFI->getEntryFreq(); 836 return (isDef + isUse) * (Freq.getFrequency() * Scale); 837 } 838 839 LiveRange::Segment 840 LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr &startInst) { 841 LiveInterval& Interval = createEmptyInterval(reg); 842 VNInfo *VN = Interval.getNextValue( 843 SlotIndex(getInstructionIndex(startInst).getRegSlot()), 844 getVNInfoAllocator()); 845 LiveRange::Segment S(SlotIndex(getInstructionIndex(startInst).getRegSlot()), 846 getMBBEndIdx(startInst.getParent()), VN); 847 Interval.addSegment(S); 848 849 return S; 850 } 851 852 //===----------------------------------------------------------------------===// 853 // Register mask functions 854 //===----------------------------------------------------------------------===// 855 856 bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI, 857 BitVector &UsableRegs) { 858 if (LI.empty()) 859 return false; 860 LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end(); 861 862 // Use a smaller arrays for local live ranges. 863 ArrayRef<SlotIndex> Slots; 864 ArrayRef<const uint32_t*> Bits; 865 if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) { 866 Slots = getRegMaskSlotsInBlock(MBB->getNumber()); 867 Bits = getRegMaskBitsInBlock(MBB->getNumber()); 868 } else { 869 Slots = getRegMaskSlots(); 870 Bits = getRegMaskBits(); 871 } 872 873 // We are going to enumerate all the register mask slots contained in LI. 874 // Start with a binary search of RegMaskSlots to find a starting point. 875 ArrayRef<SlotIndex>::iterator SlotI = 876 std::lower_bound(Slots.begin(), Slots.end(), LiveI->start); 877 ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); 878 879 // No slots in range, LI begins after the last call. 880 if (SlotI == SlotE) 881 return false; 882 883 bool Found = false; 884 while (true) { 885 assert(*SlotI >= LiveI->start); 886 // Loop over all slots overlapping this segment. 887 while (*SlotI < LiveI->end) { 888 // *SlotI overlaps LI. Collect mask bits. 889 if (!Found) { 890 // This is the first overlap. Initialize UsableRegs to all ones. 891 UsableRegs.clear(); 892 UsableRegs.resize(TRI->getNumRegs(), true); 893 Found = true; 894 } 895 // Remove usable registers clobbered by this mask. 896 UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]); 897 if (++SlotI == SlotE) 898 return Found; 899 } 900 // *SlotI is beyond the current LI segment. 901 LiveI = LI.advanceTo(LiveI, *SlotI); 902 if (LiveI == LiveE) 903 return Found; 904 // Advance SlotI until it overlaps. 905 while (*SlotI < LiveI->start) 906 if (++SlotI == SlotE) 907 return Found; 908 } 909 } 910 911 //===----------------------------------------------------------------------===// 912 // IntervalUpdate class. 913 //===----------------------------------------------------------------------===// 914 915 /// Toolkit used by handleMove to trim or extend live intervals. 916 class LiveIntervals::HMEditor { 917 private: 918 LiveIntervals& LIS; 919 const MachineRegisterInfo& MRI; 920 const TargetRegisterInfo& TRI; 921 SlotIndex OldIdx; 922 SlotIndex NewIdx; 923 SmallPtrSet<LiveRange*, 8> Updated; 924 bool UpdateFlags; 925 926 public: 927 HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI, 928 const TargetRegisterInfo& TRI, 929 SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags) 930 : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx), 931 UpdateFlags(UpdateFlags) {} 932 933 // FIXME: UpdateFlags is a workaround that creates live intervals for all 934 // physregs, even those that aren't needed for regalloc, in order to update 935 // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill 936 // flags, and postRA passes will use a live register utility instead. 937 LiveRange *getRegUnitLI(unsigned Unit) { 938 if (UpdateFlags && !MRI.isReservedRegUnit(Unit)) 939 return &LIS.getRegUnit(Unit); 940 return LIS.getCachedRegUnit(Unit); 941 } 942 943 /// Update all live ranges touched by MI, assuming a move from OldIdx to 944 /// NewIdx. 945 void updateAllRanges(MachineInstr *MI) { 946 DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI); 947 bool hasRegMask = false; 948 for (MachineOperand &MO : MI->operands()) { 949 if (MO.isRegMask()) 950 hasRegMask = true; 951 if (!MO.isReg()) 952 continue; 953 if (MO.isUse()) { 954 if (!MO.readsReg()) 955 continue; 956 // Aggressively clear all kill flags. 957 // They are reinserted by VirtRegRewriter. 958 MO.setIsKill(false); 959 } 960 961 unsigned Reg = MO.getReg(); 962 if (!Reg) 963 continue; 964 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 965 LiveInterval &LI = LIS.getInterval(Reg); 966 if (LI.hasSubRanges()) { 967 unsigned SubReg = MO.getSubReg(); 968 LaneBitmask LaneMask = SubReg ? TRI.getSubRegIndexLaneMask(SubReg) 969 : MRI.getMaxLaneMaskForVReg(Reg); 970 for (LiveInterval::SubRange &S : LI.subranges()) { 971 if ((S.LaneMask & LaneMask).none()) 972 continue; 973 updateRange(S, Reg, S.LaneMask); 974 } 975 } 976 updateRange(LI, Reg, LaneBitmask::getNone()); 977 continue; 978 } 979 980 // For physregs, only update the regunits that actually have a 981 // precomputed live range. 982 for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) 983 if (LiveRange *LR = getRegUnitLI(*Units)) 984 updateRange(*LR, *Units, LaneBitmask::getNone()); 985 } 986 if (hasRegMask) 987 updateRegMaskSlots(); 988 } 989 990 private: 991 /// Update a single live range, assuming an instruction has been moved from 992 /// OldIdx to NewIdx. 993 void updateRange(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) { 994 if (!Updated.insert(&LR).second) 995 return; 996 DEBUG({ 997 dbgs() << " "; 998 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 999 dbgs() << printReg(Reg); 1000 if (LaneMask.any()) 1001 dbgs() << " L" << PrintLaneMask(LaneMask); 1002 } else { 1003 dbgs() << printRegUnit(Reg, &TRI); 1004 } 1005 dbgs() << ":\t" << LR << '\n'; 1006 }); 1007 if (SlotIndex::isEarlierInstr(OldIdx, NewIdx)) 1008 handleMoveDown(LR); 1009 else 1010 handleMoveUp(LR, Reg, LaneMask); 1011 DEBUG(dbgs() << " -->\t" << LR << '\n'); 1012 LR.verify(); 1013 } 1014 1015 /// Update LR to reflect an instruction has been moved downwards from OldIdx 1016 /// to NewIdx (OldIdx < NewIdx). 1017 void handleMoveDown(LiveRange &LR) { 1018 LiveRange::iterator E = LR.end(); 1019 // Segment going into OldIdx. 1020 LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); 1021 1022 // No value live before or after OldIdx? Nothing to do. 1023 if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) 1024 return; 1025 1026 LiveRange::iterator OldIdxOut; 1027 // Do we have a value live-in to OldIdx? 1028 if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { 1029 // If the live-in value already extends to NewIdx, there is nothing to do. 1030 if (SlotIndex::isEarlierEqualInstr(NewIdx, OldIdxIn->end)) 1031 return; 1032 // Aggressively remove all kill flags from the old kill point. 1033 // Kill flags shouldn't be used while live intervals exist, they will be 1034 // reinserted by VirtRegRewriter. 1035 if (MachineInstr *KillMI = LIS.getInstructionFromIndex(OldIdxIn->end)) 1036 for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO) 1037 if (MO->isReg() && MO->isUse()) 1038 MO->setIsKill(false); 1039 1040 // Is there a def before NewIdx which is not OldIdx? 1041 LiveRange::iterator Next = std::next(OldIdxIn); 1042 if (Next != E && !SlotIndex::isSameInstr(OldIdx, Next->start) && 1043 SlotIndex::isEarlierInstr(Next->start, NewIdx)) { 1044 // If we are here then OldIdx was just a use but not a def. We only have 1045 // to ensure liveness extends to NewIdx. 1046 LiveRange::iterator NewIdxIn = 1047 LR.advanceTo(Next, NewIdx.getBaseIndex()); 1048 // Extend the segment before NewIdx if necessary. 1049 if (NewIdxIn == E || 1050 !SlotIndex::isEarlierInstr(NewIdxIn->start, NewIdx)) { 1051 LiveRange::iterator Prev = std::prev(NewIdxIn); 1052 Prev->end = NewIdx.getRegSlot(); 1053 } 1054 // Extend OldIdxIn. 1055 OldIdxIn->end = Next->start; 1056 return; 1057 } 1058 1059 // Adjust OldIdxIn->end to reach NewIdx. This may temporarily make LR 1060 // invalid by overlapping ranges. 1061 bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); 1062 OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber()); 1063 // If this was not a kill, then there was no def and we're done. 1064 if (!isKill) 1065 return; 1066 1067 // Did we have a Def at OldIdx? 1068 OldIdxOut = Next; 1069 if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) 1070 return; 1071 } else { 1072 OldIdxOut = OldIdxIn; 1073 } 1074 1075 // If we are here then there is a Definition at OldIdx. OldIdxOut points 1076 // to the segment starting there. 1077 assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && 1078 "No def?"); 1079 VNInfo *OldIdxVNI = OldIdxOut->valno; 1080 assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); 1081 1082 // If the defined value extends beyond NewIdx, just move the beginning 1083 // of the segment to NewIdx. 1084 SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); 1085 if (SlotIndex::isEarlierInstr(NewIdxDef, OldIdxOut->end)) { 1086 OldIdxVNI->def = NewIdxDef; 1087 OldIdxOut->start = OldIdxVNI->def; 1088 return; 1089 } 1090 1091 // If we are here then we have a Definition at OldIdx which ends before 1092 // NewIdx. 1093 1094 // Is there an existing Def at NewIdx? 1095 LiveRange::iterator AfterNewIdx 1096 = LR.advanceTo(OldIdxOut, NewIdx.getRegSlot()); 1097 bool OldIdxDefIsDead = OldIdxOut->end.isDead(); 1098 if (!OldIdxDefIsDead && 1099 SlotIndex::isEarlierInstr(OldIdxOut->end, NewIdxDef)) { 1100 // OldIdx is not a dead def, and NewIdxDef is inside a new interval. 1101 VNInfo *DefVNI; 1102 if (OldIdxOut != LR.begin() && 1103 !SlotIndex::isEarlierInstr(std::prev(OldIdxOut)->end, 1104 OldIdxOut->start)) { 1105 // There is no gap between OldIdxOut and its predecessor anymore, 1106 // merge them. 1107 LiveRange::iterator IPrev = std::prev(OldIdxOut); 1108 DefVNI = OldIdxVNI; 1109 IPrev->end = OldIdxOut->end; 1110 } else { 1111 // The value is live in to OldIdx 1112 LiveRange::iterator INext = std::next(OldIdxOut); 1113 assert(INext != E && "Must have following segment"); 1114 // We merge OldIdxOut and its successor. As we're dealing with subreg 1115 // reordering, there is always a successor to OldIdxOut in the same BB 1116 // We don't need INext->valno anymore and will reuse for the new segment 1117 // we create later. 1118 DefVNI = OldIdxVNI; 1119 INext->start = OldIdxOut->end; 1120 INext->valno->def = INext->start; 1121 } 1122 // If NewIdx is behind the last segment, extend that and append a new one. 1123 if (AfterNewIdx == E) { 1124 // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up 1125 // one position. 1126 // |- ?/OldIdxOut -| |- X0 -| ... |- Xn -| end 1127 // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS -| end 1128 std::copy(std::next(OldIdxOut), E, OldIdxOut); 1129 // The last segment is undefined now, reuse it for a dead def. 1130 LiveRange::iterator NewSegment = std::prev(E); 1131 *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), 1132 DefVNI); 1133 DefVNI->def = NewIdxDef; 1134 1135 LiveRange::iterator Prev = std::prev(NewSegment); 1136 Prev->end = NewIdxDef; 1137 } else { 1138 // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up 1139 // one position. 1140 // |- ?/OldIdxOut -| |- X0 -| ... |- Xn/AfterNewIdx -| |- Next -| 1141 // => |- X0/OldIdxOut -| ... |- Xn -| |- Xn/AfterNewIdx -| |- Next -| 1142 std::copy(std::next(OldIdxOut), std::next(AfterNewIdx), OldIdxOut); 1143 LiveRange::iterator Prev = std::prev(AfterNewIdx); 1144 // We have two cases: 1145 if (SlotIndex::isEarlierInstr(Prev->start, NewIdxDef)) { 1146 // Case 1: NewIdx is inside a liverange. Split this liverange at 1147 // NewIdxDef into the segment "Prev" followed by "NewSegment". 1148 LiveRange::iterator NewSegment = AfterNewIdx; 1149 *NewSegment = LiveRange::Segment(NewIdxDef, Prev->end, Prev->valno); 1150 Prev->valno->def = NewIdxDef; 1151 1152 *Prev = LiveRange::Segment(Prev->start, NewIdxDef, DefVNI); 1153 DefVNI->def = Prev->start; 1154 } else { 1155 // Case 2: NewIdx is in a lifetime hole. Keep AfterNewIdx as is and 1156 // turn Prev into a segment from NewIdx to AfterNewIdx->start. 1157 *Prev = LiveRange::Segment(NewIdxDef, AfterNewIdx->start, DefVNI); 1158 DefVNI->def = NewIdxDef; 1159 assert(DefVNI != AfterNewIdx->valno); 1160 } 1161 } 1162 return; 1163 } 1164 1165 if (AfterNewIdx != E && 1166 SlotIndex::isSameInstr(AfterNewIdx->start, NewIdxDef)) { 1167 // There is an existing def at NewIdx. The def at OldIdx is coalesced into 1168 // that value. 1169 assert(AfterNewIdx->valno != OldIdxVNI && "Multiple defs of value?"); 1170 LR.removeValNo(OldIdxVNI); 1171 } else { 1172 // There was no existing def at NewIdx. We need to create a dead def 1173 // at NewIdx. Shift segments over the old OldIdxOut segment, this frees 1174 // a new segment at the place where we want to construct the dead def. 1175 // |- OldIdxOut -| |- X0 -| ... |- Xn -| |- AfterNewIdx -| 1176 // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS. -| |- AfterNewIdx -| 1177 assert(AfterNewIdx != OldIdxOut && "Inconsistent iterators"); 1178 std::copy(std::next(OldIdxOut), AfterNewIdx, OldIdxOut); 1179 // We can reuse OldIdxVNI now. 1180 LiveRange::iterator NewSegment = std::prev(AfterNewIdx); 1181 VNInfo *NewSegmentVNI = OldIdxVNI; 1182 NewSegmentVNI->def = NewIdxDef; 1183 *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), 1184 NewSegmentVNI); 1185 } 1186 } 1187 1188 /// Update LR to reflect an instruction has been moved upwards from OldIdx 1189 /// to NewIdx (NewIdx < OldIdx). 1190 void handleMoveUp(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) { 1191 LiveRange::iterator E = LR.end(); 1192 // Segment going into OldIdx. 1193 LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); 1194 1195 // No value live before or after OldIdx? Nothing to do. 1196 if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) 1197 return; 1198 1199 LiveRange::iterator OldIdxOut; 1200 // Do we have a value live-in to OldIdx? 1201 if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { 1202 // If the live-in value isn't killed here, then we have no Def at 1203 // OldIdx, moreover the value must be live at NewIdx so there is nothing 1204 // to do. 1205 bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); 1206 if (!isKill) 1207 return; 1208 1209 // At this point we have to move OldIdxIn->end back to the nearest 1210 // previous use or (dead-)def but no further than NewIdx. 1211 SlotIndex DefBeforeOldIdx 1212 = std::max(OldIdxIn->start.getDeadSlot(), 1213 NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber())); 1214 OldIdxIn->end = findLastUseBefore(DefBeforeOldIdx, Reg, LaneMask); 1215 1216 // Did we have a Def at OldIdx? If not we are done now. 1217 OldIdxOut = std::next(OldIdxIn); 1218 if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) 1219 return; 1220 } else { 1221 OldIdxOut = OldIdxIn; 1222 OldIdxIn = OldIdxOut != LR.begin() ? std::prev(OldIdxOut) : E; 1223 } 1224 1225 // If we are here then there is a Definition at OldIdx. OldIdxOut points 1226 // to the segment starting there. 1227 assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && 1228 "No def?"); 1229 VNInfo *OldIdxVNI = OldIdxOut->valno; 1230 assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); 1231 bool OldIdxDefIsDead = OldIdxOut->end.isDead(); 1232 1233 // Is there an existing def at NewIdx? 1234 SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); 1235 LiveRange::iterator NewIdxOut = LR.find(NewIdx.getRegSlot()); 1236 if (SlotIndex::isSameInstr(NewIdxOut->start, NewIdx)) { 1237 assert(NewIdxOut->valno != OldIdxVNI && 1238 "Same value defined more than once?"); 1239 // If OldIdx was a dead def remove it. 1240 if (!OldIdxDefIsDead) { 1241 // Remove segment starting at NewIdx and move begin of OldIdxOut to 1242 // NewIdx so it can take its place. 1243 OldIdxVNI->def = NewIdxDef; 1244 OldIdxOut->start = NewIdxDef; 1245 LR.removeValNo(NewIdxOut->valno); 1246 } else { 1247 // Simply remove the dead def at OldIdx. 1248 LR.removeValNo(OldIdxVNI); 1249 } 1250 } else { 1251 // Previously nothing was live after NewIdx, so all we have to do now is 1252 // move the begin of OldIdxOut to NewIdx. 1253 if (!OldIdxDefIsDead) { 1254 // Do we have any intermediate Defs between OldIdx and NewIdx? 1255 if (OldIdxIn != E && 1256 SlotIndex::isEarlierInstr(NewIdxDef, OldIdxIn->start)) { 1257 // OldIdx is not a dead def and NewIdx is before predecessor start. 1258 LiveRange::iterator NewIdxIn = NewIdxOut; 1259 assert(NewIdxIn == LR.find(NewIdx.getBaseIndex())); 1260 const SlotIndex SplitPos = NewIdxDef; 1261 OldIdxVNI = OldIdxIn->valno; 1262 1263 // Merge the OldIdxIn and OldIdxOut segments into OldIdxOut. 1264 OldIdxOut->valno->def = OldIdxIn->start; 1265 *OldIdxOut = LiveRange::Segment(OldIdxIn->start, OldIdxOut->end, 1266 OldIdxOut->valno); 1267 // OldIdxIn and OldIdxVNI are now undef and can be overridden. 1268 // We Slide [NewIdxIn, OldIdxIn) down one position. 1269 // |- X0/NewIdxIn -| ... |- Xn-1 -||- Xn/OldIdxIn -||- OldIdxOut -| 1270 // => |- undef/NexIdxIn -| |- X0 -| ... |- Xn-1 -| |- Xn/OldIdxOut -| 1271 std::copy_backward(NewIdxIn, OldIdxIn, OldIdxOut); 1272 // NewIdxIn is now considered undef so we can reuse it for the moved 1273 // value. 1274 LiveRange::iterator NewSegment = NewIdxIn; 1275 LiveRange::iterator Next = std::next(NewSegment); 1276 if (SlotIndex::isEarlierInstr(Next->start, NewIdx)) { 1277 // There is no gap between NewSegment and its predecessor. 1278 *NewSegment = LiveRange::Segment(Next->start, SplitPos, 1279 Next->valno); 1280 *Next = LiveRange::Segment(SplitPos, Next->end, OldIdxVNI); 1281 Next->valno->def = SplitPos; 1282 } else { 1283 // There is a gap between NewSegment and its predecessor 1284 // Value becomes live in. 1285 *NewSegment = LiveRange::Segment(SplitPos, Next->start, OldIdxVNI); 1286 NewSegment->valno->def = SplitPos; 1287 } 1288 } else { 1289 // Leave the end point of a live def. 1290 OldIdxOut->start = NewIdxDef; 1291 OldIdxVNI->def = NewIdxDef; 1292 if (OldIdxIn != E && SlotIndex::isEarlierInstr(NewIdx, OldIdxIn->end)) 1293 OldIdxIn->end = NewIdx.getRegSlot(); 1294 } 1295 } else if (OldIdxIn != E 1296 && SlotIndex::isEarlierInstr(NewIdxOut->start, NewIdx) 1297 && SlotIndex::isEarlierInstr(NewIdx, NewIdxOut->end)) { 1298 // OldIdxVNI is a dead def that has been moved into the middle of 1299 // another value in LR. That can happen when LR is a whole register, 1300 // but the dead def is a write to a subreg that is dead at NewIdx. 1301 // The dead def may have been moved across other values 1302 // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) 1303 // down one position. 1304 // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | 1305 // => |- X0/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| 1306 std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); 1307 // Modify the segment at NewIdxOut and the following segment to meet at 1308 // the point of the dead def, with the following segment getting 1309 // OldIdxVNI as its value number. 1310 *NewIdxOut = LiveRange::Segment( 1311 NewIdxOut->start, NewIdxDef.getRegSlot(), NewIdxOut->valno); 1312 *(NewIdxOut + 1) = LiveRange::Segment( 1313 NewIdxDef.getRegSlot(), (NewIdxOut + 1)->end, OldIdxVNI); 1314 OldIdxVNI->def = NewIdxDef; 1315 // Modify subsequent segments to be defined by the moved def OldIdxVNI. 1316 for (auto Idx = NewIdxOut + 2; Idx <= OldIdxOut; ++Idx) 1317 Idx->valno = OldIdxVNI; 1318 // Aggressively remove all dead flags from the former dead definition. 1319 // Kill/dead flags shouldn't be used while live intervals exist; they 1320 // will be reinserted by VirtRegRewriter. 1321 if (MachineInstr *KillMI = LIS.getInstructionFromIndex(NewIdx)) 1322 for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO) 1323 if (MO->isReg() && !MO->isUse()) 1324 MO->setIsDead(false); 1325 } else { 1326 // OldIdxVNI is a dead def. It may have been moved across other values 1327 // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) 1328 // down one position. 1329 // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | 1330 // => |- undef/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| 1331 std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); 1332 // OldIdxVNI can be reused now to build a new dead def segment. 1333 LiveRange::iterator NewSegment = NewIdxOut; 1334 VNInfo *NewSegmentVNI = OldIdxVNI; 1335 *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), 1336 NewSegmentVNI); 1337 NewSegmentVNI->def = NewIdxDef; 1338 } 1339 } 1340 } 1341 1342 void updateRegMaskSlots() { 1343 SmallVectorImpl<SlotIndex>::iterator RI = 1344 std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(), 1345 OldIdx); 1346 assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() && 1347 "No RegMask at OldIdx."); 1348 *RI = NewIdx.getRegSlot(); 1349 assert((RI == LIS.RegMaskSlots.begin() || 1350 SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) && 1351 "Cannot move regmask instruction above another call"); 1352 assert((std::next(RI) == LIS.RegMaskSlots.end() || 1353 SlotIndex::isEarlierInstr(*RI, *std::next(RI))) && 1354 "Cannot move regmask instruction below another call"); 1355 } 1356 1357 // Return the last use of reg between NewIdx and OldIdx. 1358 SlotIndex findLastUseBefore(SlotIndex Before, unsigned Reg, 1359 LaneBitmask LaneMask) { 1360 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 1361 SlotIndex LastUse = Before; 1362 for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) { 1363 if (MO.isUndef()) 1364 continue; 1365 unsigned SubReg = MO.getSubReg(); 1366 if (SubReg != 0 && LaneMask.any() 1367 && (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask).none()) 1368 continue; 1369 1370 const MachineInstr &MI = *MO.getParent(); 1371 SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); 1372 if (InstSlot > LastUse && InstSlot < OldIdx) 1373 LastUse = InstSlot.getRegSlot(); 1374 } 1375 return LastUse; 1376 } 1377 1378 // This is a regunit interval, so scanning the use list could be very 1379 // expensive. Scan upwards from OldIdx instead. 1380 assert(Before < OldIdx && "Expected upwards move"); 1381 SlotIndexes *Indexes = LIS.getSlotIndexes(); 1382 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(Before); 1383 1384 // OldIdx may not correspond to an instruction any longer, so set MII to 1385 // point to the next instruction after OldIdx, or MBB->end(). 1386 MachineBasicBlock::iterator MII = MBB->end(); 1387 if (MachineInstr *MI = Indexes->getInstructionFromIndex( 1388 Indexes->getNextNonNullIndex(OldIdx))) 1389 if (MI->getParent() == MBB) 1390 MII = MI; 1391 1392 MachineBasicBlock::iterator Begin = MBB->begin(); 1393 while (MII != Begin) { 1394 if ((--MII)->isDebugValue()) 1395 continue; 1396 SlotIndex Idx = Indexes->getInstructionIndex(*MII); 1397 1398 // Stop searching when Before is reached. 1399 if (!SlotIndex::isEarlierInstr(Before, Idx)) 1400 return Before; 1401 1402 // Check if MII uses Reg. 1403 for (MIBundleOperands MO(*MII); MO.isValid(); ++MO) 1404 if (MO->isReg() && !MO->isUndef() && 1405 TargetRegisterInfo::isPhysicalRegister(MO->getReg()) && 1406 TRI.hasRegUnit(MO->getReg(), Reg)) 1407 return Idx.getRegSlot(); 1408 } 1409 // Didn't reach Before. It must be the first instruction in the block. 1410 return Before; 1411 } 1412 }; 1413 1414 void LiveIntervals::handleMove(MachineInstr &MI, bool UpdateFlags) { 1415 assert(!MI.isBundled() && "Can't handle bundled instructions yet."); 1416 SlotIndex OldIndex = Indexes->getInstructionIndex(MI); 1417 Indexes->removeMachineInstrFromMaps(MI); 1418 SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); 1419 assert(getMBBStartIdx(MI.getParent()) <= OldIndex && 1420 OldIndex < getMBBEndIdx(MI.getParent()) && 1421 "Cannot handle moves across basic block boundaries."); 1422 1423 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); 1424 HME.updateAllRanges(&MI); 1425 } 1426 1427 void LiveIntervals::handleMoveIntoBundle(MachineInstr &MI, 1428 MachineInstr &BundleStart, 1429 bool UpdateFlags) { 1430 SlotIndex OldIndex = Indexes->getInstructionIndex(MI); 1431 SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart); 1432 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); 1433 HME.updateAllRanges(&MI); 1434 } 1435 1436 void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin, 1437 const MachineBasicBlock::iterator End, 1438 const SlotIndex endIdx, 1439 LiveRange &LR, const unsigned Reg, 1440 LaneBitmask LaneMask) { 1441 LiveInterval::iterator LII = LR.find(endIdx); 1442 SlotIndex lastUseIdx; 1443 if (LII == LR.begin()) { 1444 // This happens when the function is called for a subregister that only 1445 // occurs _after_ the range that is to be repaired. 1446 return; 1447 } 1448 if (LII != LR.end() && LII->start < endIdx) 1449 lastUseIdx = LII->end; 1450 else 1451 --LII; 1452 1453 for (MachineBasicBlock::iterator I = End; I != Begin;) { 1454 --I; 1455 MachineInstr &MI = *I; 1456 if (MI.isDebugValue()) 1457 continue; 1458 1459 SlotIndex instrIdx = getInstructionIndex(MI); 1460 bool isStartValid = getInstructionFromIndex(LII->start); 1461 bool isEndValid = getInstructionFromIndex(LII->end); 1462 1463 // FIXME: This doesn't currently handle early-clobber or multiple removed 1464 // defs inside of the region to repair. 1465 for (MachineInstr::mop_iterator OI = MI.operands_begin(), 1466 OE = MI.operands_end(); 1467 OI != OE; ++OI) { 1468 const MachineOperand &MO = *OI; 1469 if (!MO.isReg() || MO.getReg() != Reg) 1470 continue; 1471 1472 unsigned SubReg = MO.getSubReg(); 1473 LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg); 1474 if ((Mask & LaneMask).none()) 1475 continue; 1476 1477 if (MO.isDef()) { 1478 if (!isStartValid) { 1479 if (LII->end.isDead()) { 1480 SlotIndex prevStart; 1481 if (LII != LR.begin()) 1482 prevStart = std::prev(LII)->start; 1483 1484 // FIXME: This could be more efficient if there was a 1485 // removeSegment method that returned an iterator. 1486 LR.removeSegment(*LII, true); 1487 if (prevStart.isValid()) 1488 LII = LR.find(prevStart); 1489 else 1490 LII = LR.begin(); 1491 } else { 1492 LII->start = instrIdx.getRegSlot(); 1493 LII->valno->def = instrIdx.getRegSlot(); 1494 if (MO.getSubReg() && !MO.isUndef()) 1495 lastUseIdx = instrIdx.getRegSlot(); 1496 else 1497 lastUseIdx = SlotIndex(); 1498 continue; 1499 } 1500 } 1501 1502 if (!lastUseIdx.isValid()) { 1503 VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); 1504 LiveRange::Segment S(instrIdx.getRegSlot(), 1505 instrIdx.getDeadSlot(), VNI); 1506 LII = LR.addSegment(S); 1507 } else if (LII->start != instrIdx.getRegSlot()) { 1508 VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); 1509 LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI); 1510 LII = LR.addSegment(S); 1511 } 1512 1513 if (MO.getSubReg() && !MO.isUndef()) 1514 lastUseIdx = instrIdx.getRegSlot(); 1515 else 1516 lastUseIdx = SlotIndex(); 1517 } else if (MO.isUse()) { 1518 // FIXME: This should probably be handled outside of this branch, 1519 // either as part of the def case (for defs inside of the region) or 1520 // after the loop over the region. 1521 if (!isEndValid && !LII->end.isBlock()) 1522 LII->end = instrIdx.getRegSlot(); 1523 if (!lastUseIdx.isValid()) 1524 lastUseIdx = instrIdx.getRegSlot(); 1525 } 1526 } 1527 } 1528 } 1529 1530 void 1531 LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB, 1532 MachineBasicBlock::iterator Begin, 1533 MachineBasicBlock::iterator End, 1534 ArrayRef<unsigned> OrigRegs) { 1535 // Find anchor points, which are at the beginning/end of blocks or at 1536 // instructions that already have indexes. 1537 while (Begin != MBB->begin() && !Indexes->hasIndex(*Begin)) 1538 --Begin; 1539 while (End != MBB->end() && !Indexes->hasIndex(*End)) 1540 ++End; 1541 1542 SlotIndex endIdx; 1543 if (End == MBB->end()) 1544 endIdx = getMBBEndIdx(MBB).getPrevSlot(); 1545 else 1546 endIdx = getInstructionIndex(*End); 1547 1548 Indexes->repairIndexesInRange(MBB, Begin, End); 1549 1550 for (MachineBasicBlock::iterator I = End; I != Begin;) { 1551 --I; 1552 MachineInstr &MI = *I; 1553 if (MI.isDebugValue()) 1554 continue; 1555 for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(), 1556 MOE = MI.operands_end(); 1557 MOI != MOE; ++MOI) { 1558 if (MOI->isReg() && 1559 TargetRegisterInfo::isVirtualRegister(MOI->getReg()) && 1560 !hasInterval(MOI->getReg())) { 1561 createAndComputeVirtRegInterval(MOI->getReg()); 1562 } 1563 } 1564 } 1565 1566 for (unsigned Reg : OrigRegs) { 1567 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 1568 continue; 1569 1570 LiveInterval &LI = getInterval(Reg); 1571 // FIXME: Should we support undefs that gain defs? 1572 if (!LI.hasAtLeastOneValue()) 1573 continue; 1574 1575 for (LiveInterval::SubRange &S : LI.subranges()) 1576 repairOldRegInRange(Begin, End, endIdx, S, Reg, S.LaneMask); 1577 1578 repairOldRegInRange(Begin, End, endIdx, LI, Reg); 1579 } 1580 } 1581 1582 void LiveIntervals::removePhysRegDefAt(unsigned Reg, SlotIndex Pos) { 1583 for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) { 1584 if (LiveRange *LR = getCachedRegUnit(*Unit)) 1585 if (VNInfo *VNI = LR->getVNInfoAt(Pos)) 1586 LR->removeValNo(VNI); 1587 } 1588 } 1589 1590 void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) { 1591 // LI may not have the main range computed yet, but its subranges may 1592 // be present. 1593 VNInfo *VNI = LI.getVNInfoAt(Pos); 1594 if (VNI != nullptr) { 1595 assert(VNI->def.getBaseIndex() == Pos.getBaseIndex()); 1596 LI.removeValNo(VNI); 1597 } 1598 1599 // Also remove the value defined in subranges. 1600 for (LiveInterval::SubRange &S : LI.subranges()) { 1601 if (VNInfo *SVNI = S.getVNInfoAt(Pos)) 1602 if (SVNI->def.getBaseIndex() == Pos.getBaseIndex()) 1603 S.removeValNo(SVNI); 1604 } 1605 LI.removeEmptySubRanges(); 1606 } 1607 1608 void LiveIntervals::splitSeparateComponents(LiveInterval &LI, 1609 SmallVectorImpl<LiveInterval*> &SplitLIs) { 1610 ConnectedVNInfoEqClasses ConEQ(*this); 1611 unsigned NumComp = ConEQ.Classify(LI); 1612 if (NumComp <= 1) 1613 return; 1614 DEBUG(dbgs() << " Split " << NumComp << " components: " << LI << '\n'); 1615 unsigned Reg = LI.reg; 1616 const TargetRegisterClass *RegClass = MRI->getRegClass(Reg); 1617 for (unsigned I = 1; I < NumComp; ++I) { 1618 unsigned NewVReg = MRI->createVirtualRegister(RegClass); 1619 LiveInterval &NewLI = createEmptyInterval(NewVReg); 1620 SplitLIs.push_back(&NewLI); 1621 } 1622 ConEQ.Distribute(LI, SplitLIs.data(), *MRI); 1623 } 1624 1625 void LiveIntervals::constructMainRangeFromSubranges(LiveInterval &LI) { 1626 assert(LRCalc && "LRCalc not initialized."); 1627 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 1628 LRCalc->constructMainRangeFromSubranges(LI); 1629 } 1630