1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===// 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 contains the SplitAnalysis class as well as mutator functions for 11 // live range splitting. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "SplitKit.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 18 #include "llvm/CodeGen/LiveRangeEdit.h" 19 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 20 #include "llvm/CodeGen/MachineDominators.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineLoopInfo.h" 23 #include "llvm/CodeGen/MachineRegisterInfo.h" 24 #include "llvm/CodeGen/VirtRegMap.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/MathExtras.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/Target/TargetInstrInfo.h" 29 #include "llvm/Target/TargetMachine.h" 30 31 using namespace llvm; 32 33 #define DEBUG_TYPE "regalloc" 34 35 STATISTIC(NumFinished, "Number of splits finished"); 36 STATISTIC(NumSimple, "Number of splits that were simple"); 37 STATISTIC(NumCopies, "Number of copies inserted for splitting"); 38 STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); 39 STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); 40 41 //===----------------------------------------------------------------------===// 42 // Last Insert Point Analysis 43 //===----------------------------------------------------------------------===// 44 45 InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis, 46 unsigned BBNum) 47 : LIS(lis), LastInsertPoint(BBNum) {} 48 49 SlotIndex 50 InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI, 51 const MachineBasicBlock &MBB) { 52 unsigned Num = MBB.getNumber(); 53 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num]; 54 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB); 55 56 SmallVector<const MachineBasicBlock *, 1> EHPadSucessors; 57 for (const MachineBasicBlock *SMBB : MBB.successors()) 58 if (SMBB->isEHPad()) 59 EHPadSucessors.push_back(SMBB); 60 61 // Compute insert points on the first call. The pair is independent of the 62 // current live interval. 63 if (!LIP.first.isValid()) { 64 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator(); 65 if (FirstTerm == MBB.end()) 66 LIP.first = MBBEnd; 67 else 68 LIP.first = LIS.getInstructionIndex(*FirstTerm); 69 70 // If there is a landing pad successor, also find the call instruction. 71 if (EHPadSucessors.empty()) 72 return LIP.first; 73 // There may not be a call instruction (?) in which case we ignore LPad. 74 LIP.second = LIP.first; 75 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin(); 76 I != E;) { 77 --I; 78 if (I->isCall()) { 79 LIP.second = LIS.getInstructionIndex(*I); 80 break; 81 } 82 } 83 } 84 85 // If CurLI is live into a landing pad successor, move the last insert point 86 // back to the call that may throw. 87 if (!LIP.second) 88 return LIP.first; 89 90 if (none_of(EHPadSucessors, [&](const MachineBasicBlock *EHPad) { 91 return LIS.isLiveInToMBB(CurLI, EHPad); 92 })) 93 return LIP.first; 94 95 // Find the value leaving MBB. 96 const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd); 97 if (!VNI) 98 return LIP.first; 99 100 // If the value leaving MBB was defined after the call in MBB, it can't 101 // really be live-in to the landing pad. This can happen if the landing pad 102 // has a PHI, and this register is undef on the exceptional edge. 103 // <rdar://problem/10664933> 104 if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd) 105 return LIP.first; 106 107 // Value is properly live-in to the landing pad. 108 // Only allow inserts before the call. 109 return LIP.second; 110 } 111 112 MachineBasicBlock::iterator 113 InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI, 114 MachineBasicBlock &MBB) { 115 SlotIndex LIP = getLastInsertPoint(CurLI, MBB); 116 if (LIP == LIS.getMBBEndIdx(&MBB)) 117 return MBB.end(); 118 return LIS.getInstructionFromIndex(LIP); 119 } 120 121 //===----------------------------------------------------------------------===// 122 // Split Analysis 123 //===----------------------------------------------------------------------===// 124 125 SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, 126 const MachineLoopInfo &mli) 127 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli), 128 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr), 129 IPA(lis, MF.getNumBlockIDs()) {} 130 131 void SplitAnalysis::clear() { 132 UseSlots.clear(); 133 UseBlocks.clear(); 134 ThroughBlocks.clear(); 135 CurLI = nullptr; 136 DidRepairRange = false; 137 } 138 139 /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. 140 void SplitAnalysis::analyzeUses() { 141 assert(UseSlots.empty() && "Call clear first"); 142 143 // First get all the defs from the interval values. This provides the correct 144 // slots for early clobbers. 145 for (const VNInfo *VNI : CurLI->valnos) 146 if (!VNI->isPHIDef() && !VNI->isUnused()) 147 UseSlots.push_back(VNI->def); 148 149 // Get use slots form the use-def chain. 150 const MachineRegisterInfo &MRI = MF.getRegInfo(); 151 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg)) 152 if (!MO.isUndef()) 153 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot()); 154 155 array_pod_sort(UseSlots.begin(), UseSlots.end()); 156 157 // Remove duplicates, keeping the smaller slot for each instruction. 158 // That is what we want for early clobbers. 159 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), 160 SlotIndex::isSameInstr), 161 UseSlots.end()); 162 163 // Compute per-live block info. 164 if (!calcLiveBlockInfo()) { 165 // FIXME: calcLiveBlockInfo found inconsistencies in the live range. 166 // I am looking at you, RegisterCoalescer! 167 DidRepairRange = true; 168 ++NumRepairs; 169 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); 170 const_cast<LiveIntervals&>(LIS) 171 .shrinkToUses(const_cast<LiveInterval*>(CurLI)); 172 UseBlocks.clear(); 173 ThroughBlocks.clear(); 174 bool fixed = calcLiveBlockInfo(); 175 (void)fixed; 176 assert(fixed && "Couldn't fix broken live interval"); 177 } 178 179 DEBUG(dbgs() << "Analyze counted " 180 << UseSlots.size() << " instrs in " 181 << UseBlocks.size() << " blocks, through " 182 << NumThroughBlocks << " blocks.\n"); 183 } 184 185 /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks 186 /// where CurLI is live. 187 bool SplitAnalysis::calcLiveBlockInfo() { 188 ThroughBlocks.resize(MF.getNumBlockIDs()); 189 NumThroughBlocks = NumGapBlocks = 0; 190 if (CurLI->empty()) 191 return true; 192 193 LiveInterval::const_iterator LVI = CurLI->begin(); 194 LiveInterval::const_iterator LVE = CurLI->end(); 195 196 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; 197 UseI = UseSlots.begin(); 198 UseE = UseSlots.end(); 199 200 // Loop over basic blocks where CurLI is live. 201 MachineFunction::iterator MFI = 202 LIS.getMBBFromIndex(LVI->start)->getIterator(); 203 for (;;) { 204 BlockInfo BI; 205 BI.MBB = &*MFI; 206 SlotIndex Start, Stop; 207 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 208 209 // If the block contains no uses, the range must be live through. At one 210 // point, RegisterCoalescer could create dangling ranges that ended 211 // mid-block. 212 if (UseI == UseE || *UseI >= Stop) { 213 ++NumThroughBlocks; 214 ThroughBlocks.set(BI.MBB->getNumber()); 215 // The range shouldn't end mid-block if there are no uses. This shouldn't 216 // happen. 217 if (LVI->end < Stop) 218 return false; 219 } else { 220 // This block has uses. Find the first and last uses in the block. 221 BI.FirstInstr = *UseI; 222 assert(BI.FirstInstr >= Start); 223 do ++UseI; 224 while (UseI != UseE && *UseI < Stop); 225 BI.LastInstr = UseI[-1]; 226 assert(BI.LastInstr < Stop); 227 228 // LVI is the first live segment overlapping MBB. 229 BI.LiveIn = LVI->start <= Start; 230 231 // When not live in, the first use should be a def. 232 if (!BI.LiveIn) { 233 assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 234 assert(LVI->start == BI.FirstInstr && "First instr should be a def"); 235 BI.FirstDef = BI.FirstInstr; 236 } 237 238 // Look for gaps in the live range. 239 BI.LiveOut = true; 240 while (LVI->end < Stop) { 241 SlotIndex LastStop = LVI->end; 242 if (++LVI == LVE || LVI->start >= Stop) { 243 BI.LiveOut = false; 244 BI.LastInstr = LastStop; 245 break; 246 } 247 248 if (LastStop < LVI->start) { 249 // There is a gap in the live range. Create duplicate entries for the 250 // live-in snippet and the live-out snippet. 251 ++NumGapBlocks; 252 253 // Push the Live-in part. 254 BI.LiveOut = false; 255 UseBlocks.push_back(BI); 256 UseBlocks.back().LastInstr = LastStop; 257 258 // Set up BI for the live-out part. 259 BI.LiveIn = false; 260 BI.LiveOut = true; 261 BI.FirstInstr = BI.FirstDef = LVI->start; 262 } 263 264 // A Segment that starts in the middle of the block must be a def. 265 assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 266 if (!BI.FirstDef) 267 BI.FirstDef = LVI->start; 268 } 269 270 UseBlocks.push_back(BI); 271 272 // LVI is now at LVE or LVI->end >= Stop. 273 if (LVI == LVE) 274 break; 275 } 276 277 // Live segment ends exactly at Stop. Move to the next segment. 278 if (LVI->end == Stop && ++LVI == LVE) 279 break; 280 281 // Pick the next basic block. 282 if (LVI->start < Stop) 283 ++MFI; 284 else 285 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator(); 286 } 287 288 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); 289 return true; 290 } 291 292 unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { 293 if (cli->empty()) 294 return 0; 295 LiveInterval *li = const_cast<LiveInterval*>(cli); 296 LiveInterval::iterator LVI = li->begin(); 297 LiveInterval::iterator LVE = li->end(); 298 unsigned Count = 0; 299 300 // Loop over basic blocks where li is live. 301 MachineFunction::const_iterator MFI = 302 LIS.getMBBFromIndex(LVI->start)->getIterator(); 303 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI); 304 for (;;) { 305 ++Count; 306 LVI = li->advanceTo(LVI, Stop); 307 if (LVI == LVE) 308 return Count; 309 do { 310 ++MFI; 311 Stop = LIS.getMBBEndIdx(&*MFI); 312 } while (Stop <= LVI->start); 313 } 314 } 315 316 bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { 317 unsigned OrigReg = VRM.getOriginal(CurLI->reg); 318 const LiveInterval &Orig = LIS.getInterval(OrigReg); 319 assert(!Orig.empty() && "Splitting empty interval?"); 320 LiveInterval::const_iterator I = Orig.find(Idx); 321 322 // Range containing Idx should begin at Idx. 323 if (I != Orig.end() && I->start <= Idx) 324 return I->start == Idx; 325 326 // Range does not contain Idx, previous must end at Idx. 327 return I != Orig.begin() && (--I)->end == Idx; 328 } 329 330 void SplitAnalysis::analyze(const LiveInterval *li) { 331 clear(); 332 CurLI = li; 333 analyzeUses(); 334 } 335 336 337 //===----------------------------------------------------------------------===// 338 // Split Editor 339 //===----------------------------------------------------------------------===// 340 341 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 342 SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa, 343 LiveIntervals &lis, VirtRegMap &vrm, 344 MachineDominatorTree &mdt, 345 MachineBlockFrequencyInfo &mbfi) 346 : SA(sa), AA(aa), LIS(lis), VRM(vrm), 347 MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt), 348 TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()), 349 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()), 350 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition), 351 RegAssign(Allocator) {} 352 353 void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) { 354 Edit = &LRE; 355 SpillMode = SM; 356 OpenIdx = 0; 357 RegAssign.clear(); 358 Values.clear(); 359 360 // Reset the LiveRangeCalc instances needed for this spill mode. 361 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 362 &LIS.getVNInfoAllocator()); 363 if (SpillMode) 364 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 365 &LIS.getVNInfoAllocator()); 366 367 // We don't need an AliasAnalysis since we will only be performing 368 // cheap-as-a-copy remats anyway. 369 Edit->anyRematerializable(nullptr); 370 } 371 372 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 373 LLVM_DUMP_METHOD void SplitEditor::dump() const { 374 if (RegAssign.empty()) { 375 dbgs() << " empty\n"; 376 return; 377 } 378 379 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) 380 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); 381 dbgs() << '\n'; 382 } 383 #endif 384 385 LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM, 386 LiveInterval &LI) { 387 for (LiveInterval::SubRange &S : LI.subranges()) 388 if (S.LaneMask == LM) 389 return S; 390 llvm_unreachable("SubRange for this mask not found"); 391 } 392 393 void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) { 394 if (!LI.hasSubRanges()) { 395 LI.createDeadDef(VNI); 396 return; 397 } 398 399 SlotIndex Def = VNI->def; 400 if (Original) { 401 // If we are transferring a def from the original interval, make sure 402 // to only update the subranges for which the original subranges had 403 // a def at this location. 404 for (LiveInterval::SubRange &S : LI.subranges()) { 405 auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent()); 406 VNInfo *PV = PS.getVNInfoAt(Def); 407 if (PV != nullptr && PV->def == Def) 408 S.createDeadDef(Def, LIS.getVNInfoAllocator()); 409 } 410 } else { 411 // This is a new def: either from rematerialization, or from an inserted 412 // copy. Since rematerialization can regenerate a definition of a sub- 413 // register, we need to check which subranges need to be updated. 414 const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def); 415 assert(DefMI != nullptr); 416 LaneBitmask LM; 417 for (const MachineOperand &DefOp : DefMI->defs()) { 418 unsigned R = DefOp.getReg(); 419 if (R != LI.reg) 420 continue; 421 if (unsigned SR = DefOp.getSubReg()) 422 LM |= TRI.getSubRegIndexLaneMask(SR); 423 else { 424 LM = MRI.getMaxLaneMaskForVReg(R); 425 break; 426 } 427 } 428 for (LiveInterval::SubRange &S : LI.subranges()) 429 if ((S.LaneMask & LM).any()) 430 S.createDeadDef(Def, LIS.getVNInfoAllocator()); 431 } 432 } 433 434 VNInfo *SplitEditor::defValue(unsigned RegIdx, 435 const VNInfo *ParentVNI, 436 SlotIndex Idx, 437 bool Original) { 438 assert(ParentVNI && "Mapping NULL value"); 439 assert(Idx.isValid() && "Invalid SlotIndex"); 440 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); 441 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 442 443 // Create a new value. 444 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator()); 445 446 bool Force = LI->hasSubRanges(); 447 ValueForcePair FP(Force ? nullptr : VNI, Force); 448 // Use insert for lookup, so we can add missing values with a second lookup. 449 std::pair<ValueMap::iterator, bool> InsP = 450 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP)); 451 452 // This was the first time (RegIdx, ParentVNI) was mapped, and it is not 453 // forced. Keep it as a simple def without any liveness. 454 if (!Force && InsP.second) 455 return VNI; 456 457 // If the previous value was a simple mapping, add liveness for it now. 458 if (VNInfo *OldVNI = InsP.first->second.getPointer()) { 459 addDeadDef(*LI, OldVNI, Original); 460 461 // No longer a simple mapping. Switch to a complex mapping. If the 462 // interval has subranges, make it a forced mapping. 463 InsP.first->second = ValueForcePair(nullptr, Force); 464 } 465 466 // This is a complex mapping, add liveness for VNI 467 addDeadDef(*LI, VNI, Original); 468 return VNI; 469 } 470 471 void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) { 472 assert(ParentVNI && "Mapping NULL value"); 473 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)]; 474 VNInfo *VNI = VFP.getPointer(); 475 476 // ParentVNI was either unmapped or already complex mapped. Either way, just 477 // set the force bit. 478 if (!VNI) { 479 VFP.setInt(true); 480 return; 481 } 482 483 // This was previously a single mapping. Make sure the old def is represented 484 // by a trivial live range. 485 addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false); 486 487 // Mark as complex mapped, forced. 488 VFP = ValueForcePair(nullptr, true); 489 } 490 491 SlotIndex SplitEditor::buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg, 492 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore, 493 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) { 494 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY); 495 bool FirstCopy = !Def.isValid(); 496 MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc) 497 .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy) 498 | getInternalReadRegState(!FirstCopy), SubIdx) 499 .addReg(FromReg, 0, SubIdx); 500 501 BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator(); 502 if (FirstCopy) { 503 SlotIndexes &Indexes = *LIS.getSlotIndexes(); 504 Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot(); 505 DestLI.createDeadDef(Def, Allocator); 506 } else { 507 CopyMI->bundleWithPred(); 508 } 509 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx); 510 DestLI.refineSubRanges(Allocator, LaneMask, 511 [Def, &Allocator](LiveInterval::SubRange& SR) { 512 SR.createDeadDef(Def, Allocator); 513 }); 514 return Def; 515 } 516 517 SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg, 518 LaneBitmask LaneMask, MachineBasicBlock &MBB, 519 MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) { 520 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY); 521 if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) { 522 // The full vreg is copied. 523 MachineInstr *CopyMI = 524 BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg); 525 SlotIndexes &Indexes = *LIS.getSlotIndexes(); 526 return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot(); 527 } 528 529 // Only a subset of lanes needs to be copied. The following is a simple 530 // heuristic to construct a sequence of COPYs. We could add a target 531 // specific callback if this turns out to be suboptimal. 532 LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx)); 533 534 // First pass: Try to find a perfectly matching subregister index. If none 535 // exists find the one covering the most lanemask bits. 536 SmallVector<unsigned, 8> PossibleIndexes; 537 unsigned BestIdx = 0; 538 unsigned BestCover = 0; 539 const TargetRegisterClass *RC = MRI.getRegClass(FromReg); 540 assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class"); 541 for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) { 542 // Is this index even compatible with the given class? 543 if (TRI.getSubClassWithSubReg(RC, Idx) != RC) 544 continue; 545 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx); 546 // Early exit if we found a perfect match. 547 if (SubRegMask == LaneMask) { 548 BestIdx = Idx; 549 break; 550 } 551 552 // The index must not cover any lanes outside \p LaneMask. 553 if ((SubRegMask & ~LaneMask).any()) 554 continue; 555 556 unsigned PopCount = countPopulation(SubRegMask.getAsInteger()); 557 PossibleIndexes.push_back(Idx); 558 if (PopCount > BestCover) { 559 BestCover = PopCount; 560 BestIdx = Idx; 561 } 562 } 563 564 // Abort if we cannot possibly implement the COPY with the given indexes. 565 if (BestIdx == 0) 566 report_fatal_error("Impossible to implement partial COPY"); 567 568 SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, 569 BestIdx, DestLI, Late, SlotIndex()); 570 571 // Greedy heuristic: Keep iterating keeping the best covering subreg index 572 // each time. 573 LaneBitmask LanesLeft = 574 LaneMask & ~(TRI.getSubRegIndexLaneMask(BestCover)); 575 while (LanesLeft.any()) { 576 unsigned BestIdx = 0; 577 int BestCover = INT_MIN; 578 for (unsigned Idx : PossibleIndexes) { 579 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx); 580 // Early exit if we found a perfect match. 581 if (SubRegMask == LanesLeft) { 582 BestIdx = Idx; 583 break; 584 } 585 586 // Try to cover as much of the remaining lanes as possible but 587 // as few of the already covered lanes as possible. 588 int Cover = countPopulation((SubRegMask & LanesLeft).getAsInteger()) 589 - countPopulation((SubRegMask & ~LanesLeft).getAsInteger()); 590 if (Cover > BestCover) { 591 BestCover = Cover; 592 BestIdx = Idx; 593 } 594 } 595 596 if (BestIdx == 0) 597 report_fatal_error("Impossible to implement partial COPY"); 598 599 buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx, 600 DestLI, Late, Def); 601 LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx); 602 } 603 604 return Def; 605 } 606 607 VNInfo *SplitEditor::defFromParent(unsigned RegIdx, 608 VNInfo *ParentVNI, 609 SlotIndex UseIdx, 610 MachineBasicBlock &MBB, 611 MachineBasicBlock::iterator I) { 612 SlotIndex Def; 613 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 614 615 // We may be trying to avoid interference that ends at a deleted instruction, 616 // so always begin RegIdx 0 early and all others late. 617 bool Late = RegIdx != 0; 618 619 // Attempt cheap-as-a-copy rematerialization. 620 unsigned Original = VRM.getOriginal(Edit->get(RegIdx)); 621 LiveInterval &OrigLI = LIS.getInterval(Original); 622 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx); 623 624 unsigned Reg = LI->reg; 625 bool DidRemat = false; 626 if (OrigVNI) { 627 LiveRangeEdit::Remat RM(ParentVNI); 628 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def); 629 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) { 630 Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late); 631 ++NumRemats; 632 DidRemat = true; 633 } 634 } 635 if (!DidRemat) { 636 LaneBitmask LaneMask; 637 if (LI->hasSubRanges()) { 638 LaneMask = LaneBitmask::getNone(); 639 for (LiveInterval::SubRange &S : LI->subranges()) 640 LaneMask |= S.LaneMask; 641 } else { 642 LaneMask = LaneBitmask::getAll(); 643 } 644 645 ++NumCopies; 646 Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx); 647 } 648 649 // Define the value in Reg. 650 return defValue(RegIdx, ParentVNI, Def, false); 651 } 652 653 /// Create a new virtual register and live interval. 654 unsigned SplitEditor::openIntv() { 655 // Create the complement as index 0. 656 if (Edit->empty()) 657 Edit->createEmptyInterval(); 658 659 // Create the open interval. 660 OpenIdx = Edit->size(); 661 Edit->createEmptyInterval(); 662 return OpenIdx; 663 } 664 665 void SplitEditor::selectIntv(unsigned Idx) { 666 assert(Idx != 0 && "Cannot select the complement interval"); 667 assert(Idx < Edit->size() && "Can only select previously opened interval"); 668 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n'); 669 OpenIdx = Idx; 670 } 671 672 SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { 673 assert(OpenIdx && "openIntv not called before enterIntvBefore"); 674 DEBUG(dbgs() << " enterIntvBefore " << Idx); 675 Idx = Idx.getBaseIndex(); 676 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 677 if (!ParentVNI) { 678 DEBUG(dbgs() << ": not live\n"); 679 return Idx; 680 } 681 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 682 MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 683 assert(MI && "enterIntvBefore called with invalid index"); 684 685 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); 686 return VNI->def; 687 } 688 689 SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) { 690 assert(OpenIdx && "openIntv not called before enterIntvAfter"); 691 DEBUG(dbgs() << " enterIntvAfter " << Idx); 692 Idx = Idx.getBoundaryIndex(); 693 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 694 if (!ParentVNI) { 695 DEBUG(dbgs() << ": not live\n"); 696 return Idx; 697 } 698 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 699 MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 700 assert(MI && "enterIntvAfter called with invalid index"); 701 702 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), 703 std::next(MachineBasicBlock::iterator(MI))); 704 return VNI->def; 705 } 706 707 SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { 708 assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); 709 SlotIndex End = LIS.getMBBEndIdx(&MBB); 710 SlotIndex Last = End.getPrevSlot(); 711 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); 712 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); 713 if (!ParentVNI) { 714 DEBUG(dbgs() << ": not live\n"); 715 return End; 716 } 717 DEBUG(dbgs() << ": valno " << ParentVNI->id); 718 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, 719 SA.getLastSplitPointIter(&MBB)); 720 RegAssign.insert(VNI->def, End, OpenIdx); 721 DEBUG(dump()); 722 return VNI->def; 723 } 724 725 /// useIntv - indicate that all instructions in MBB should use OpenLI. 726 void SplitEditor::useIntv(const MachineBasicBlock &MBB) { 727 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); 728 } 729 730 void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { 731 assert(OpenIdx && "openIntv not called before useIntv"); 732 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); 733 RegAssign.insert(Start, End, OpenIdx); 734 DEBUG(dump()); 735 } 736 737 SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { 738 assert(OpenIdx && "openIntv not called before leaveIntvAfter"); 739 DEBUG(dbgs() << " leaveIntvAfter " << Idx); 740 741 // The interval must be live beyond the instruction at Idx. 742 SlotIndex Boundary = Idx.getBoundaryIndex(); 743 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary); 744 if (!ParentVNI) { 745 DEBUG(dbgs() << ": not live\n"); 746 return Boundary.getNextSlot(); 747 } 748 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 749 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary); 750 assert(MI && "No instruction at index"); 751 752 // In spill mode, make live ranges as short as possible by inserting the copy 753 // before MI. This is only possible if that instruction doesn't redefine the 754 // value. The inserted COPY is not a kill, and we don't need to recompute 755 // the source live range. The spiller also won't try to hoist this copy. 756 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) && 757 MI->readsVirtualRegister(Edit->getReg())) { 758 forceRecompute(0, ParentVNI); 759 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 760 return Idx; 761 } 762 763 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(), 764 std::next(MachineBasicBlock::iterator(MI))); 765 return VNI->def; 766 } 767 768 SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { 769 assert(OpenIdx && "openIntv not called before leaveIntvBefore"); 770 DEBUG(dbgs() << " leaveIntvBefore " << Idx); 771 772 // The interval must be live into the instruction at Idx. 773 Idx = Idx.getBaseIndex(); 774 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 775 if (!ParentVNI) { 776 DEBUG(dbgs() << ": not live\n"); 777 return Idx.getNextSlot(); 778 } 779 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 780 781 MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 782 assert(MI && "No instruction at index"); 783 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 784 return VNI->def; 785 } 786 787 SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { 788 assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); 789 SlotIndex Start = LIS.getMBBStartIdx(&MBB); 790 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); 791 792 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 793 if (!ParentVNI) { 794 DEBUG(dbgs() << ": not live\n"); 795 return Start; 796 } 797 798 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, 799 MBB.SkipPHIsLabelsAndDebug(MBB.begin())); 800 RegAssign.insert(Start, VNI->def, OpenIdx); 801 DEBUG(dump()); 802 return VNI->def; 803 } 804 805 void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { 806 assert(OpenIdx && "openIntv not called before overlapIntv"); 807 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 808 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) && 809 "Parent changes value in extended range"); 810 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && 811 "Range cannot span basic blocks"); 812 813 // The complement interval will be extended as needed by LRCalc.extend(). 814 if (ParentVNI) 815 forceRecompute(0, ParentVNI); 816 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); 817 RegAssign.insert(Start, End, OpenIdx); 818 DEBUG(dump()); 819 } 820 821 //===----------------------------------------------------------------------===// 822 // Spill modes 823 //===----------------------------------------------------------------------===// 824 825 void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) { 826 LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 827 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n"); 828 RegAssignMap::iterator AssignI; 829 AssignI.setMap(RegAssign); 830 831 for (unsigned i = 0, e = Copies.size(); i != e; ++i) { 832 SlotIndex Def = Copies[i]->def; 833 MachineInstr *MI = LIS.getInstructionFromIndex(Def); 834 assert(MI && "No instruction for back-copy"); 835 836 MachineBasicBlock *MBB = MI->getParent(); 837 MachineBasicBlock::iterator MBBI(MI); 838 bool AtBegin; 839 do AtBegin = MBBI == MBB->begin(); 840 while (!AtBegin && (--MBBI)->isDebugValue()); 841 842 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI); 843 LIS.removeVRegDefAt(*LI, Def); 844 LIS.RemoveMachineInstrFromMaps(*MI); 845 MI->eraseFromParent(); 846 847 // Adjust RegAssign if a register assignment is killed at Def. We want to 848 // avoid calculating the live range of the source register if possible. 849 AssignI.find(Def.getPrevSlot()); 850 if (!AssignI.valid() || AssignI.start() >= Def) 851 continue; 852 // If MI doesn't kill the assigned register, just leave it. 853 if (AssignI.stop() != Def) 854 continue; 855 unsigned RegIdx = AssignI.value(); 856 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) { 857 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n'); 858 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def)); 859 } else { 860 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot(); 861 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI); 862 AssignI.setStop(Kill); 863 } 864 } 865 } 866 867 MachineBasicBlock* 868 SplitEditor::findShallowDominator(MachineBasicBlock *MBB, 869 MachineBasicBlock *DefMBB) { 870 if (MBB == DefMBB) 871 return MBB; 872 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def."); 873 874 const MachineLoopInfo &Loops = SA.Loops; 875 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB); 876 MachineDomTreeNode *DefDomNode = MDT[DefMBB]; 877 878 // Best candidate so far. 879 MachineBasicBlock *BestMBB = MBB; 880 unsigned BestDepth = UINT_MAX; 881 882 for (;;) { 883 const MachineLoop *Loop = Loops.getLoopFor(MBB); 884 885 // MBB isn't in a loop, it doesn't get any better. All dominators have a 886 // higher frequency by definition. 887 if (!Loop) { 888 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 889 << MBB->getNumber() << " at depth 0\n"); 890 return MBB; 891 } 892 893 // We'll never be able to exit the DefLoop. 894 if (Loop == DefLoop) { 895 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 896 << MBB->getNumber() << " in the same loop\n"); 897 return MBB; 898 } 899 900 // Least busy dominator seen so far. 901 unsigned Depth = Loop->getLoopDepth(); 902 if (Depth < BestDepth) { 903 BestMBB = MBB; 904 BestDepth = Depth; 905 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 906 << MBB->getNumber() << " at depth " << Depth << '\n'); 907 } 908 909 // Leave loop by going to the immediate dominator of the loop header. 910 // This is a bigger stride than simply walking up the dominator tree. 911 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom(); 912 913 // Too far up the dominator tree? 914 if (!IDom || !MDT.dominates(DefDomNode, IDom)) 915 return BestMBB; 916 917 MBB = IDom->getBlock(); 918 } 919 } 920 921 void SplitEditor::computeRedundantBackCopies( 922 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) { 923 LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 924 LiveInterval *Parent = &Edit->getParent(); 925 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums()); 926 SmallPtrSet<VNInfo *, 8> DominatedVNIs; 927 928 // Aggregate VNIs having the same value as ParentVNI. 929 for (VNInfo *VNI : LI->valnos) { 930 if (VNI->isUnused()) 931 continue; 932 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 933 EqualVNs[ParentVNI->id].insert(VNI); 934 } 935 936 // For VNI aggregation of each ParentVNI, collect dominated, i.e., 937 // redundant VNIs to BackCopies. 938 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 939 VNInfo *ParentVNI = Parent->getValNumInfo(i); 940 if (!NotToHoistSet.count(ParentVNI->id)) 941 continue; 942 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin(); 943 SmallPtrSetIterator<VNInfo *> It2 = It1; 944 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) { 945 It2 = It1; 946 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) { 947 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2)) 948 continue; 949 950 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def); 951 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def); 952 if (MBB1 == MBB2) { 953 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1)); 954 } else if (MDT.dominates(MBB1, MBB2)) { 955 DominatedVNIs.insert(*It2); 956 } else if (MDT.dominates(MBB2, MBB1)) { 957 DominatedVNIs.insert(*It1); 958 } 959 } 960 } 961 if (!DominatedVNIs.empty()) { 962 forceRecompute(0, ParentVNI); 963 for (auto VNI : DominatedVNIs) { 964 BackCopies.push_back(VNI); 965 } 966 DominatedVNIs.clear(); 967 } 968 } 969 } 970 971 /// For SM_Size mode, find a common dominator for all the back-copies for 972 /// the same ParentVNI and hoist the backcopies to the dominator BB. 973 /// For SM_Speed mode, if the common dominator is hot and it is not beneficial 974 /// to do the hoisting, simply remove the dominated backcopies for the same 975 /// ParentVNI. 976 void SplitEditor::hoistCopies() { 977 // Get the complement interval, always RegIdx 0. 978 LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 979 LiveInterval *Parent = &Edit->getParent(); 980 981 // Track the nearest common dominator for all back-copies for each ParentVNI, 982 // indexed by ParentVNI->id. 983 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair; 984 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums()); 985 // The total cost of all the back-copies for each ParentVNI. 986 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums()); 987 // The ParentVNI->id set for which hoisting back-copies are not beneficial 988 // for Speed. 989 DenseSet<unsigned> NotToHoistSet; 990 991 // Find the nearest common dominator for parent values with multiple 992 // back-copies. If a single back-copy dominates, put it in DomPair.second. 993 for (VNInfo *VNI : LI->valnos) { 994 if (VNI->isUnused()) 995 continue; 996 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 997 assert(ParentVNI && "Parent not live at complement def"); 998 999 // Don't hoist remats. The complement is probably going to disappear 1000 // completely anyway. 1001 if (Edit->didRematerialize(ParentVNI)) 1002 continue; 1003 1004 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def); 1005 1006 DomPair &Dom = NearestDom[ParentVNI->id]; 1007 1008 // Keep directly defined parent values. This is either a PHI or an 1009 // instruction in the complement range. All other copies of ParentVNI 1010 // should be eliminated. 1011 if (VNI->def == ParentVNI->def) { 1012 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n'); 1013 Dom = DomPair(ValMBB, VNI->def); 1014 continue; 1015 } 1016 // Skip the singly mapped values. There is nothing to gain from hoisting a 1017 // single back-copy. 1018 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) { 1019 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n'); 1020 continue; 1021 } 1022 1023 if (!Dom.first) { 1024 // First time we see ParentVNI. VNI dominates itself. 1025 Dom = DomPair(ValMBB, VNI->def); 1026 } else if (Dom.first == ValMBB) { 1027 // Two defs in the same block. Pick the earlier def. 1028 if (!Dom.second.isValid() || VNI->def < Dom.second) 1029 Dom.second = VNI->def; 1030 } else { 1031 // Different basic blocks. Check if one dominates. 1032 MachineBasicBlock *Near = 1033 MDT.findNearestCommonDominator(Dom.first, ValMBB); 1034 if (Near == ValMBB) 1035 // Def ValMBB dominates. 1036 Dom = DomPair(ValMBB, VNI->def); 1037 else if (Near != Dom.first) 1038 // None dominate. Hoist to common dominator, need new def. 1039 Dom = DomPair(Near, SlotIndex()); 1040 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB); 1041 } 1042 1043 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def 1044 << " for parent " << ParentVNI->id << '@' << ParentVNI->def 1045 << " hoist to BB#" << Dom.first->getNumber() << ' ' 1046 << Dom.second << '\n'); 1047 } 1048 1049 // Insert the hoisted copies. 1050 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 1051 DomPair &Dom = NearestDom[i]; 1052 if (!Dom.first || Dom.second.isValid()) 1053 continue; 1054 // This value needs a hoisted copy inserted at the end of Dom.first. 1055 VNInfo *ParentVNI = Parent->getValNumInfo(i); 1056 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def); 1057 // Get a less loopy dominator than Dom.first. 1058 Dom.first = findShallowDominator(Dom.first, DefMBB); 1059 if (SpillMode == SM_Speed && 1060 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) { 1061 NotToHoistSet.insert(ParentVNI->id); 1062 continue; 1063 } 1064 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot(); 1065 Dom.second = 1066 defFromParent(0, ParentVNI, Last, *Dom.first, 1067 SA.getLastSplitPointIter(Dom.first))->def; 1068 } 1069 1070 // Remove redundant back-copies that are now known to be dominated by another 1071 // def with the same value. 1072 SmallVector<VNInfo*, 8> BackCopies; 1073 for (VNInfo *VNI : LI->valnos) { 1074 if (VNI->isUnused()) 1075 continue; 1076 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 1077 const DomPair &Dom = NearestDom[ParentVNI->id]; 1078 if (!Dom.first || Dom.second == VNI->def || 1079 NotToHoistSet.count(ParentVNI->id)) 1080 continue; 1081 BackCopies.push_back(VNI); 1082 forceRecompute(0, ParentVNI); 1083 } 1084 1085 // If it is not beneficial to hoist all the BackCopies, simply remove 1086 // redundant BackCopies in speed mode. 1087 if (SpillMode == SM_Speed && !NotToHoistSet.empty()) 1088 computeRedundantBackCopies(NotToHoistSet, BackCopies); 1089 1090 removeBackCopies(BackCopies); 1091 } 1092 1093 1094 /// transferValues - Transfer all possible values to the new live ranges. 1095 /// Values that were rematerialized are left alone, they need LRCalc.extend(). 1096 bool SplitEditor::transferValues() { 1097 bool Skipped = false; 1098 RegAssignMap::const_iterator AssignI = RegAssign.begin(); 1099 for (const LiveRange::Segment &S : Edit->getParent()) { 1100 DEBUG(dbgs() << " blit " << S << ':'); 1101 VNInfo *ParentVNI = S.valno; 1102 // RegAssign has holes where RegIdx 0 should be used. 1103 SlotIndex Start = S.start; 1104 AssignI.advanceTo(Start); 1105 do { 1106 unsigned RegIdx; 1107 SlotIndex End = S.end; 1108 if (!AssignI.valid()) { 1109 RegIdx = 0; 1110 } else if (AssignI.start() <= Start) { 1111 RegIdx = AssignI.value(); 1112 if (AssignI.stop() < End) { 1113 End = AssignI.stop(); 1114 ++AssignI; 1115 } 1116 } else { 1117 RegIdx = 0; 1118 End = std::min(End, AssignI.start()); 1119 } 1120 1121 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. 1122 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx 1123 << '(' << PrintReg(Edit->get(RegIdx)) << ')'); 1124 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1125 1126 // Check for a simply defined value that can be blitted directly. 1127 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id)); 1128 if (VNInfo *VNI = VFP.getPointer()) { 1129 DEBUG(dbgs() << ':' << VNI->id); 1130 LI.addSegment(LiveInterval::Segment(Start, End, VNI)); 1131 Start = End; 1132 continue; 1133 } 1134 1135 // Skip values with forced recomputation. 1136 if (VFP.getInt()) { 1137 DEBUG(dbgs() << "(recalc)"); 1138 Skipped = true; 1139 Start = End; 1140 continue; 1141 } 1142 1143 LiveRangeCalc &LRC = getLRCalc(RegIdx); 1144 1145 // This value has multiple defs in RegIdx, but it wasn't rematerialized, 1146 // so the live range is accurate. Add live-in blocks in [Start;End) to the 1147 // LiveInBlocks. 1148 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); 1149 SlotIndex BlockStart, BlockEnd; 1150 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB); 1151 1152 // The first block may be live-in, or it may have its own def. 1153 if (Start != BlockStart) { 1154 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End)); 1155 assert(VNI && "Missing def for complex mapped value"); 1156 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber()); 1157 // MBB has its own def. Is it also live-out? 1158 if (BlockEnd <= End) 1159 LRC.setLiveOutValue(&*MBB, VNI); 1160 1161 // Skip to the next block for live-in. 1162 ++MBB; 1163 BlockStart = BlockEnd; 1164 } 1165 1166 // Handle the live-in blocks covered by [Start;End). 1167 assert(Start <= BlockStart && "Expected live-in block"); 1168 while (BlockStart < End) { 1169 DEBUG(dbgs() << ">BB#" << MBB->getNumber()); 1170 BlockEnd = LIS.getMBBEndIdx(&*MBB); 1171 if (BlockStart == ParentVNI->def) { 1172 // This block has the def of a parent PHI, so it isn't live-in. 1173 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); 1174 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End)); 1175 assert(VNI && "Missing def for complex mapped parent PHI"); 1176 if (End >= BlockEnd) 1177 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well. 1178 } else { 1179 // This block needs a live-in value. The last block covered may not 1180 // be live-out. 1181 if (End < BlockEnd) 1182 LRC.addLiveInBlock(LI, MDT[&*MBB], End); 1183 else { 1184 // Live-through, and we don't know the value. 1185 LRC.addLiveInBlock(LI, MDT[&*MBB]); 1186 LRC.setLiveOutValue(&*MBB, nullptr); 1187 } 1188 } 1189 BlockStart = BlockEnd; 1190 ++MBB; 1191 } 1192 Start = End; 1193 } while (Start != S.end); 1194 DEBUG(dbgs() << '\n'); 1195 } 1196 1197 LRCalc[0].calculateValues(); 1198 if (SpillMode) 1199 LRCalc[1].calculateValues(); 1200 1201 return Skipped; 1202 } 1203 1204 static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) { 1205 const LiveRange::Segment *Seg = LR.getSegmentContaining(Def); 1206 if (Seg == nullptr) 1207 return true; 1208 if (Seg->end != Def.getDeadSlot()) 1209 return false; 1210 // This is a dead PHI. Remove it. 1211 LR.removeSegment(*Seg, true); 1212 return true; 1213 } 1214 1215 void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC, 1216 LiveRange &LR, LaneBitmask LM, 1217 ArrayRef<SlotIndex> Undefs) { 1218 for (MachineBasicBlock *P : B.predecessors()) { 1219 SlotIndex End = LIS.getMBBEndIdx(P); 1220 SlotIndex LastUse = End.getPrevSlot(); 1221 // The predecessor may not have a live-out value. That is OK, like an 1222 // undef PHI operand. 1223 LiveInterval &PLI = Edit->getParent(); 1224 // Need the cast because the inputs to ?: would otherwise be deemed 1225 // "incompatible": SubRange vs LiveInterval. 1226 LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI) 1227 : static_cast<LiveRange&>(PLI); 1228 if (PSR.liveAt(LastUse)) 1229 LRC.extend(LR, End, /*PhysReg=*/0, Undefs); 1230 } 1231 } 1232 1233 void SplitEditor::extendPHIKillRanges() { 1234 // Extend live ranges to be live-out for successor PHI values. 1235 1236 // Visit each PHI def slot in the parent live interval. If the def is dead, 1237 // remove it. Otherwise, extend the live interval to reach the end indexes 1238 // of all predecessor blocks. 1239 1240 LiveInterval &ParentLI = Edit->getParent(); 1241 for (const VNInfo *V : ParentLI.valnos) { 1242 if (V->isUnused() || !V->isPHIDef()) 1243 continue; 1244 1245 unsigned RegIdx = RegAssign.lookup(V->def); 1246 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1247 LiveRangeCalc &LRC = getLRCalc(RegIdx); 1248 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def); 1249 if (!removeDeadSegment(V->def, LI)) 1250 extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{}); 1251 } 1252 1253 SmallVector<SlotIndex, 4> Undefs; 1254 LiveRangeCalc SubLRC; 1255 1256 for (LiveInterval::SubRange &PS : ParentLI.subranges()) { 1257 for (const VNInfo *V : PS.valnos) { 1258 if (V->isUnused() || !V->isPHIDef()) 1259 continue; 1260 unsigned RegIdx = RegAssign.lookup(V->def); 1261 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1262 LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI); 1263 if (removeDeadSegment(V->def, S)) 1264 continue; 1265 1266 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def); 1267 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 1268 &LIS.getVNInfoAllocator()); 1269 Undefs.clear(); 1270 LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes()); 1271 extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs); 1272 } 1273 } 1274 } 1275 1276 /// rewriteAssigned - Rewrite all uses of Edit->getReg(). 1277 void SplitEditor::rewriteAssigned(bool ExtendRanges) { 1278 struct ExtPoint { 1279 ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N) 1280 : MO(O), RegIdx(R), Next(N) {} 1281 MachineOperand MO; 1282 unsigned RegIdx; 1283 SlotIndex Next; 1284 }; 1285 1286 SmallVector<ExtPoint,4> ExtPoints; 1287 1288 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), 1289 RE = MRI.reg_end(); RI != RE;) { 1290 MachineOperand &MO = *RI; 1291 MachineInstr *MI = MO.getParent(); 1292 ++RI; 1293 // LiveDebugVariables should have handled all DBG_VALUE instructions. 1294 if (MI->isDebugValue()) { 1295 DEBUG(dbgs() << "Zapping " << *MI); 1296 MO.setReg(0); 1297 continue; 1298 } 1299 1300 // <undef> operands don't really read the register, so it doesn't matter 1301 // which register we choose. When the use operand is tied to a def, we must 1302 // use the same register as the def, so just do that always. 1303 SlotIndex Idx = LIS.getInstructionIndex(*MI); 1304 if (MO.isDef() || MO.isUndef()) 1305 Idx = Idx.getRegSlot(MO.isEarlyClobber()); 1306 1307 // Rewrite to the mapped register at Idx. 1308 unsigned RegIdx = RegAssign.lookup(Idx); 1309 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx)); 1310 MO.setReg(LI.reg); 1311 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' 1312 << Idx << ':' << RegIdx << '\t' << *MI); 1313 1314 // Extend liveness to Idx if the instruction reads reg. 1315 if (!ExtendRanges || MO.isUndef()) 1316 continue; 1317 1318 // Skip instructions that don't read Reg. 1319 if (MO.isDef()) { 1320 if (!MO.getSubReg() && !MO.isEarlyClobber()) 1321 continue; 1322 // We may want to extend a live range for a partial redef, or for a use 1323 // tied to an early clobber. 1324 Idx = Idx.getPrevSlot(); 1325 if (!Edit->getParent().liveAt(Idx)) 1326 continue; 1327 } else 1328 Idx = Idx.getRegSlot(true); 1329 1330 SlotIndex Next = Idx.getNextSlot(); 1331 if (LI.hasSubRanges()) { 1332 // We have to delay extending subranges until we have seen all operands 1333 // defining the register. This is because a <def,read-undef> operand 1334 // will create an "undef" point, and we cannot extend any subranges 1335 // until all of them have been accounted for. 1336 if (MO.isUse()) 1337 ExtPoints.push_back(ExtPoint(MO, RegIdx, Next)); 1338 } else { 1339 LiveRangeCalc &LRC = getLRCalc(RegIdx); 1340 LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>()); 1341 } 1342 } 1343 1344 for (ExtPoint &EP : ExtPoints) { 1345 LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx)); 1346 assert(LI.hasSubRanges()); 1347 1348 LiveRangeCalc SubLRC; 1349 unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg(); 1350 LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub) 1351 : MRI.getMaxLaneMaskForVReg(Reg); 1352 for (LiveInterval::SubRange &S : LI.subranges()) { 1353 if ((S.LaneMask & LM).none()) 1354 continue; 1355 // The problem here can be that the new register may have been created 1356 // for a partially defined original register. For example: 1357 // %vreg827:subreg_hireg<def,read-undef> = ... 1358 // ... 1359 // %vreg828<def> = COPY %vreg827 1360 if (S.empty()) 1361 continue; 1362 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 1363 &LIS.getVNInfoAllocator()); 1364 SmallVector<SlotIndex, 4> Undefs; 1365 LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes()); 1366 SubLRC.extend(S, EP.Next, 0, Undefs); 1367 } 1368 } 1369 1370 for (unsigned R : *Edit) { 1371 LiveInterval &LI = LIS.getInterval(R); 1372 if (!LI.hasSubRanges()) 1373 continue; 1374 LI.clear(); 1375 LI.removeEmptySubRanges(); 1376 LIS.constructMainRangeFromSubranges(LI); 1377 } 1378 } 1379 1380 void SplitEditor::deleteRematVictims() { 1381 SmallVector<MachineInstr*, 8> Dead; 1382 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ 1383 LiveInterval *LI = &LIS.getInterval(*I); 1384 for (const LiveRange::Segment &S : LI->segments) { 1385 // Dead defs end at the dead slot. 1386 if (S.end != S.valno->def.getDeadSlot()) 1387 continue; 1388 if (S.valno->isPHIDef()) 1389 continue; 1390 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def); 1391 assert(MI && "Missing instruction for dead def"); 1392 MI->addRegisterDead(LI->reg, &TRI); 1393 1394 if (!MI->allDefsAreDead()) 1395 continue; 1396 1397 DEBUG(dbgs() << "All defs dead: " << *MI); 1398 Dead.push_back(MI); 1399 } 1400 } 1401 1402 if (Dead.empty()) 1403 return; 1404 1405 Edit->eliminateDeadDefs(Dead, None, &AA); 1406 } 1407 1408 void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { 1409 ++NumFinished; 1410 1411 // At this point, the live intervals in Edit contain VNInfos corresponding to 1412 // the inserted copies. 1413 1414 // Add the original defs from the parent interval. 1415 for (const VNInfo *ParentVNI : Edit->getParent().valnos) { 1416 if (ParentVNI->isUnused()) 1417 continue; 1418 unsigned RegIdx = RegAssign.lookup(ParentVNI->def); 1419 defValue(RegIdx, ParentVNI, ParentVNI->def, true); 1420 1421 // Force rematted values to be recomputed everywhere. 1422 // The new live ranges may be truncated. 1423 if (Edit->didRematerialize(ParentVNI)) 1424 for (unsigned i = 0, e = Edit->size(); i != e; ++i) 1425 forceRecompute(i, ParentVNI); 1426 } 1427 1428 // Hoist back-copies to the complement interval when in spill mode. 1429 switch (SpillMode) { 1430 case SM_Partition: 1431 // Leave all back-copies as is. 1432 break; 1433 case SM_Size: 1434 case SM_Speed: 1435 // hoistCopies will behave differently between size and speed. 1436 hoistCopies(); 1437 } 1438 1439 // Transfer the simply mapped values, check if any are skipped. 1440 bool Skipped = transferValues(); 1441 1442 // Rewrite virtual registers, possibly extending ranges. 1443 rewriteAssigned(Skipped); 1444 1445 if (Skipped) 1446 extendPHIKillRanges(); 1447 else 1448 ++NumSimple; 1449 1450 // Delete defs that were rematted everywhere. 1451 if (Skipped) 1452 deleteRematVictims(); 1453 1454 // Get rid of unused values and set phi-kill flags. 1455 for (unsigned Reg : *Edit) { 1456 LiveInterval &LI = LIS.getInterval(Reg); 1457 LI.removeEmptySubRanges(); 1458 LI.RenumberValues(); 1459 } 1460 1461 // Provide a reverse mapping from original indices to Edit ranges. 1462 if (LRMap) { 1463 LRMap->clear(); 1464 for (unsigned i = 0, e = Edit->size(); i != e; ++i) 1465 LRMap->push_back(i); 1466 } 1467 1468 // Now check if any registers were separated into multiple components. 1469 ConnectedVNInfoEqClasses ConEQ(LIS); 1470 for (unsigned i = 0, e = Edit->size(); i != e; ++i) { 1471 // Don't use iterators, they are invalidated by create() below. 1472 unsigned VReg = Edit->get(i); 1473 LiveInterval &LI = LIS.getInterval(VReg); 1474 SmallVector<LiveInterval*, 8> SplitLIs; 1475 LIS.splitSeparateComponents(LI, SplitLIs); 1476 unsigned Original = VRM.getOriginal(VReg); 1477 for (LiveInterval *SplitLI : SplitLIs) 1478 VRM.setIsSplitFromReg(SplitLI->reg, Original); 1479 1480 // The new intervals all map back to i. 1481 if (LRMap) 1482 LRMap->resize(Edit->size(), i); 1483 } 1484 1485 // Calculate spill weight and allocation hints for new intervals. 1486 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI); 1487 1488 assert(!LRMap || LRMap->size() == Edit->size()); 1489 } 1490 1491 1492 //===----------------------------------------------------------------------===// 1493 // Single Block Splitting 1494 //===----------------------------------------------------------------------===// 1495 1496 bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI, 1497 bool SingleInstrs) const { 1498 // Always split for multiple instructions. 1499 if (!BI.isOneInstr()) 1500 return true; 1501 // Don't split for single instructions unless explicitly requested. 1502 if (!SingleInstrs) 1503 return false; 1504 // Splitting a live-through range always makes progress. 1505 if (BI.LiveIn && BI.LiveOut) 1506 return true; 1507 // No point in isolating a copy. It has no register class constraints. 1508 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike()) 1509 return false; 1510 // Finally, don't isolate an end point that was created by earlier splits. 1511 return isOriginalEndpoint(BI.FirstInstr); 1512 } 1513 1514 void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { 1515 openIntv(); 1516 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); 1517 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr, 1518 LastSplitPoint)); 1519 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) { 1520 useIntv(SegStart, leaveIntvAfter(BI.LastInstr)); 1521 } else { 1522 // The last use is after the last valid split point. 1523 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); 1524 useIntv(SegStart, SegStop); 1525 overlapIntv(SegStop, BI.LastInstr); 1526 } 1527 } 1528 1529 1530 //===----------------------------------------------------------------------===// 1531 // Global Live Range Splitting Support 1532 //===----------------------------------------------------------------------===// 1533 1534 // These methods support a method of global live range splitting that uses a 1535 // global algorithm to decide intervals for CFG edges. They will insert split 1536 // points and color intervals in basic blocks while avoiding interference. 1537 // 1538 // Note that splitSingleBlock is also useful for blocks where both CFG edges 1539 // are on the stack. 1540 1541 void SplitEditor::splitLiveThroughBlock(unsigned MBBNum, 1542 unsigned IntvIn, SlotIndex LeaveBefore, 1543 unsigned IntvOut, SlotIndex EnterAfter){ 1544 SlotIndex Start, Stop; 1545 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum); 1546 1547 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop 1548 << ") intf " << LeaveBefore << '-' << EnterAfter 1549 << ", live-through " << IntvIn << " -> " << IntvOut); 1550 1551 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks"); 1552 1553 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block"); 1554 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf"); 1555 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block"); 1556 1557 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum); 1558 1559 if (!IntvOut) { 1560 DEBUG(dbgs() << ", spill on entry.\n"); 1561 // 1562 // <<<<<<<<< Possible LeaveBefore interference. 1563 // |-----------| Live through. 1564 // -____________ Spill on entry. 1565 // 1566 selectIntv(IntvIn); 1567 SlotIndex Idx = leaveIntvAtTop(*MBB); 1568 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 1569 (void)Idx; 1570 return; 1571 } 1572 1573 if (!IntvIn) { 1574 DEBUG(dbgs() << ", reload on exit.\n"); 1575 // 1576 // >>>>>>> Possible EnterAfter interference. 1577 // |-----------| Live through. 1578 // ___________-- Reload on exit. 1579 // 1580 selectIntv(IntvOut); 1581 SlotIndex Idx = enterIntvAtEnd(*MBB); 1582 assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 1583 (void)Idx; 1584 return; 1585 } 1586 1587 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) { 1588 DEBUG(dbgs() << ", straight through.\n"); 1589 // 1590 // |-----------| Live through. 1591 // ------------- Straight through, same intv, no interference. 1592 // 1593 selectIntv(IntvOut); 1594 useIntv(Start, Stop); 1595 return; 1596 } 1597 1598 // We cannot legally insert splits after LSP. 1599 SlotIndex LSP = SA.getLastSplitPoint(MBBNum); 1600 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf"); 1601 1602 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter || 1603 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) { 1604 DEBUG(dbgs() << ", switch avoiding interference.\n"); 1605 // 1606 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference. 1607 // |-----------| Live through. 1608 // ------======= Switch intervals between interference. 1609 // 1610 selectIntv(IntvOut); 1611 SlotIndex Idx; 1612 if (LeaveBefore && LeaveBefore < LSP) { 1613 Idx = enterIntvBefore(LeaveBefore); 1614 useIntv(Idx, Stop); 1615 } else { 1616 Idx = enterIntvAtEnd(*MBB); 1617 } 1618 selectIntv(IntvIn); 1619 useIntv(Start, Idx); 1620 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 1621 assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 1622 return; 1623 } 1624 1625 DEBUG(dbgs() << ", create local intv for interference.\n"); 1626 // 1627 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference. 1628 // |-----------| Live through. 1629 // ==---------== Switch intervals before/after interference. 1630 // 1631 assert(LeaveBefore <= EnterAfter && "Missed case"); 1632 1633 selectIntv(IntvOut); 1634 SlotIndex Idx = enterIntvAfter(EnterAfter); 1635 useIntv(Idx, Stop); 1636 assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 1637 1638 selectIntv(IntvIn); 1639 Idx = leaveIntvBefore(LeaveBefore); 1640 useIntv(Start, Idx); 1641 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 1642 } 1643 1644 1645 void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI, 1646 unsigned IntvIn, SlotIndex LeaveBefore) { 1647 SlotIndex Start, Stop; 1648 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 1649 1650 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop 1651 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 1652 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore 1653 << (BI.LiveOut ? ", stack-out" : ", killed in block")); 1654 1655 assert(IntvIn && "Must have register in"); 1656 assert(BI.LiveIn && "Must be live-in"); 1657 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference"); 1658 1659 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) { 1660 DEBUG(dbgs() << " before interference.\n"); 1661 // 1662 // <<< Interference after kill. 1663 // |---o---x | Killed in block. 1664 // ========= Use IntvIn everywhere. 1665 // 1666 selectIntv(IntvIn); 1667 useIntv(Start, BI.LastInstr); 1668 return; 1669 } 1670 1671 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 1672 1673 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) { 1674 // 1675 // <<< Possible interference after last use. 1676 // |---o---o---| Live-out on stack. 1677 // =========____ Leave IntvIn after last use. 1678 // 1679 // < Interference after last use. 1680 // |---o---o--o| Live-out on stack, late last use. 1681 // ============ Copy to stack after LSP, overlap IntvIn. 1682 // \_____ Stack interval is live-out. 1683 // 1684 if (BI.LastInstr < LSP) { 1685 DEBUG(dbgs() << ", spill after last use before interference.\n"); 1686 selectIntv(IntvIn); 1687 SlotIndex Idx = leaveIntvAfter(BI.LastInstr); 1688 useIntv(Start, Idx); 1689 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 1690 } else { 1691 DEBUG(dbgs() << ", spill before last split point.\n"); 1692 selectIntv(IntvIn); 1693 SlotIndex Idx = leaveIntvBefore(LSP); 1694 overlapIntv(Idx, BI.LastInstr); 1695 useIntv(Start, Idx); 1696 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 1697 } 1698 return; 1699 } 1700 1701 // The interference is overlapping somewhere we wanted to use IntvIn. That 1702 // means we need to create a local interval that can be allocated a 1703 // different register. 1704 unsigned LocalIntv = openIntv(); 1705 (void)LocalIntv; 1706 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n"); 1707 1708 if (!BI.LiveOut || BI.LastInstr < LSP) { 1709 // 1710 // <<<<<<< Interference overlapping uses. 1711 // |---o---o---| Live-out on stack. 1712 // =====----____ Leave IntvIn before interference, then spill. 1713 // 1714 SlotIndex To = leaveIntvAfter(BI.LastInstr); 1715 SlotIndex From = enterIntvBefore(LeaveBefore); 1716 useIntv(From, To); 1717 selectIntv(IntvIn); 1718 useIntv(Start, From); 1719 assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 1720 return; 1721 } 1722 1723 // <<<<<<< Interference overlapping uses. 1724 // |---o---o--o| Live-out on stack, late last use. 1725 // =====------- Copy to stack before LSP, overlap LocalIntv. 1726 // \_____ Stack interval is live-out. 1727 // 1728 SlotIndex To = leaveIntvBefore(LSP); 1729 overlapIntv(To, BI.LastInstr); 1730 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore)); 1731 useIntv(From, To); 1732 selectIntv(IntvIn); 1733 useIntv(Start, From); 1734 assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 1735 } 1736 1737 void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, 1738 unsigned IntvOut, SlotIndex EnterAfter) { 1739 SlotIndex Start, Stop; 1740 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 1741 1742 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop 1743 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 1744 << ", reg-out " << IntvOut << ", enter after " << EnterAfter 1745 << (BI.LiveIn ? ", stack-in" : ", defined in block")); 1746 1747 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 1748 1749 assert(IntvOut && "Must have register out"); 1750 assert(BI.LiveOut && "Must be live-out"); 1751 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference"); 1752 1753 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) { 1754 DEBUG(dbgs() << " after interference.\n"); 1755 // 1756 // >>>> Interference before def. 1757 // | o---o---| Defined in block. 1758 // ========= Use IntvOut everywhere. 1759 // 1760 selectIntv(IntvOut); 1761 useIntv(BI.FirstInstr, Stop); 1762 return; 1763 } 1764 1765 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) { 1766 DEBUG(dbgs() << ", reload after interference.\n"); 1767 // 1768 // >>>> Interference before def. 1769 // |---o---o---| Live-through, stack-in. 1770 // ____========= Enter IntvOut before first use. 1771 // 1772 selectIntv(IntvOut); 1773 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr)); 1774 useIntv(Idx, Stop); 1775 assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 1776 return; 1777 } 1778 1779 // The interference is overlapping somewhere we wanted to use IntvOut. That 1780 // means we need to create a local interval that can be allocated a 1781 // different register. 1782 DEBUG(dbgs() << ", interference overlaps uses.\n"); 1783 // 1784 // >>>>>>> Interference overlapping uses. 1785 // |---o---o---| Live-through, stack-in. 1786 // ____---====== Create local interval for interference range. 1787 // 1788 selectIntv(IntvOut); 1789 SlotIndex Idx = enterIntvAfter(EnterAfter); 1790 useIntv(Idx, Stop); 1791 assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 1792 1793 openIntv(); 1794 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr)); 1795 useIntv(From, Idx); 1796 } 1797