1e580952dSDimitry Andric //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===// 2e580952dSDimitry Andric // 3e580952dSDimitry Andric // The LLVM Compiler Infrastructure 4e580952dSDimitry Andric // 5e580952dSDimitry Andric // This file is distributed under the University of Illinois Open Source 6e580952dSDimitry Andric // License. See LICENSE.TXT for details. 7e580952dSDimitry Andric // 8e580952dSDimitry Andric //===----------------------------------------------------------------------===// 9e580952dSDimitry Andric // 10e580952dSDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for 11e580952dSDimitry Andric // live range splitting. 12e580952dSDimitry Andric // 13e580952dSDimitry Andric //===----------------------------------------------------------------------===// 14e580952dSDimitry Andric 15e580952dSDimitry Andric #include "SplitKit.h" 163b0f4066SDimitry Andric #include "llvm/ADT/Statistic.h" 17e580952dSDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h" 18dff0c46cSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h" 192754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 20e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 216122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 22e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 23139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 24e580952dSDimitry Andric #include "llvm/Support/Debug.h" 25e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h" 26e580952dSDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 27e580952dSDimitry Andric #include "llvm/Target/TargetMachine.h" 28e580952dSDimitry Andric 29e580952dSDimitry Andric using namespace llvm; 30e580952dSDimitry Andric 3191bc56edSDimitry Andric #define DEBUG_TYPE "regalloc" 3291bc56edSDimitry Andric 333b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished"); 343b0f4066SDimitry Andric STATISTIC(NumSimple, "Number of splits that were simple"); 35bd5abe19SDimitry Andric STATISTIC(NumCopies, "Number of copies inserted for splitting"); 36bd5abe19SDimitry Andric STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); 37bd5abe19SDimitry Andric STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); 38e580952dSDimitry Andric 39e580952dSDimitry Andric //===----------------------------------------------------------------------===// 40e580952dSDimitry Andric // Split Analysis 41e580952dSDimitry Andric //===----------------------------------------------------------------------===// 42e580952dSDimitry Andric 4339d628a0SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, 44e580952dSDimitry Andric const MachineLoopInfo &mli) 4539d628a0SDimitry Andric : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli), 4639d628a0SDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr), 473b0f4066SDimitry Andric LastSplitPoint(MF.getNumBlockIDs()) {} 48e580952dSDimitry Andric 49e580952dSDimitry Andric void SplitAnalysis::clear() { 502754fe60SDimitry Andric UseSlots.clear(); 513b0f4066SDimitry Andric UseBlocks.clear(); 523b0f4066SDimitry Andric ThroughBlocks.clear(); 5391bc56edSDimitry Andric CurLI = nullptr; 54bd5abe19SDimitry Andric DidRepairRange = false; 55e580952dSDimitry Andric } 56e580952dSDimitry Andric 573b0f4066SDimitry Andric SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) { 583b0f4066SDimitry Andric const MachineBasicBlock *MBB = MF.getBlockNumbered(Num); 593b0f4066SDimitry Andric const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor(); 603b0f4066SDimitry Andric std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num]; 61dff0c46cSDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB); 623b0f4066SDimitry Andric 633b0f4066SDimitry Andric // Compute split points on the first call. The pair is independent of the 643b0f4066SDimitry Andric // current live interval. 653b0f4066SDimitry Andric if (!LSP.first.isValid()) { 663b0f4066SDimitry Andric MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator(); 673b0f4066SDimitry Andric if (FirstTerm == MBB->end()) 68dff0c46cSDimitry Andric LSP.first = MBBEnd; 693b0f4066SDimitry Andric else 703b0f4066SDimitry Andric LSP.first = LIS.getInstructionIndex(FirstTerm); 713b0f4066SDimitry Andric 723b0f4066SDimitry Andric // If there is a landing pad successor, also find the call instruction. 733b0f4066SDimitry Andric if (!LPad) 743b0f4066SDimitry Andric return LSP.first; 753b0f4066SDimitry Andric // There may not be a call instruction (?) in which case we ignore LPad. 763b0f4066SDimitry Andric LSP.second = LSP.first; 7717a519f9SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin(); 7817a519f9SDimitry Andric I != E;) { 7917a519f9SDimitry Andric --I; 80dff0c46cSDimitry Andric if (I->isCall()) { 813b0f4066SDimitry Andric LSP.second = LIS.getInstructionIndex(I); 823b0f4066SDimitry Andric break; 833b0f4066SDimitry Andric } 843b0f4066SDimitry Andric } 8517a519f9SDimitry Andric } 863b0f4066SDimitry Andric 873b0f4066SDimitry Andric // If CurLI is live into a landing pad successor, move the last split point 883b0f4066SDimitry Andric // back to the call that may throw. 89dff0c46cSDimitry Andric if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad)) 903b0f4066SDimitry Andric return LSP.first; 91dff0c46cSDimitry Andric 92dff0c46cSDimitry Andric // Find the value leaving MBB. 93dff0c46cSDimitry Andric const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd); 94dff0c46cSDimitry Andric if (!VNI) 95dff0c46cSDimitry Andric return LSP.first; 96dff0c46cSDimitry Andric 97dff0c46cSDimitry Andric // If the value leaving MBB was defined after the call in MBB, it can't 98dff0c46cSDimitry Andric // really be live-in to the landing pad. This can happen if the landing pad 99dff0c46cSDimitry Andric // has a PHI, and this register is undef on the exceptional edge. 100dff0c46cSDimitry Andric // <rdar://problem/10664933> 101dff0c46cSDimitry Andric if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd) 102dff0c46cSDimitry Andric return LSP.first; 103dff0c46cSDimitry Andric 104dff0c46cSDimitry Andric // Value is properly live-in to the landing pad. 105dff0c46cSDimitry Andric // Only allow splits before the call. 106dff0c46cSDimitry Andric return LSP.second; 107dff0c46cSDimitry Andric } 108dff0c46cSDimitry Andric 109dff0c46cSDimitry Andric MachineBasicBlock::iterator 110dff0c46cSDimitry Andric SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) { 111dff0c46cSDimitry Andric SlotIndex LSP = getLastSplitPoint(MBB->getNumber()); 112dff0c46cSDimitry Andric if (LSP == LIS.getMBBEndIdx(MBB)) 113dff0c46cSDimitry Andric return MBB->end(); 114dff0c46cSDimitry Andric return LIS.getInstructionFromIndex(LSP); 115e580952dSDimitry Andric } 116e580952dSDimitry Andric 1172754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. 118e580952dSDimitry Andric void SplitAnalysis::analyzeUses() { 1193b0f4066SDimitry Andric assert(UseSlots.empty() && "Call clear first"); 1203b0f4066SDimitry Andric 1213b0f4066SDimitry Andric // First get all the defs from the interval values. This provides the correct 1223b0f4066SDimitry Andric // slots for early clobbers. 12339d628a0SDimitry Andric for (const VNInfo *VNI : CurLI->valnos) 12439d628a0SDimitry Andric if (!VNI->isPHIDef() && !VNI->isUnused()) 12539d628a0SDimitry Andric UseSlots.push_back(VNI->def); 1263b0f4066SDimitry Andric 1273b0f4066SDimitry Andric // Get use slots form the use-def chain. 1282754fe60SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 12991bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg)) 13091bc56edSDimitry Andric if (!MO.isUndef()) 13191bc56edSDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot()); 1323b0f4066SDimitry Andric 1332754fe60SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end()); 1343b0f4066SDimitry Andric 1353b0f4066SDimitry Andric // Remove duplicates, keeping the smaller slot for each instruction. 1363b0f4066SDimitry Andric // That is what we want for early clobbers. 1373b0f4066SDimitry Andric UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), 1383b0f4066SDimitry Andric SlotIndex::isSameInstr), 1393b0f4066SDimitry Andric UseSlots.end()); 1403b0f4066SDimitry Andric 1413b0f4066SDimitry Andric // Compute per-live block info. 1423b0f4066SDimitry Andric if (!calcLiveBlockInfo()) { 1433b0f4066SDimitry Andric // FIXME: calcLiveBlockInfo found inconsistencies in the live range. 14417a519f9SDimitry Andric // I am looking at you, RegisterCoalescer! 145bd5abe19SDimitry Andric DidRepairRange = true; 146bd5abe19SDimitry Andric ++NumRepairs; 1473b0f4066SDimitry Andric DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); 1483b0f4066SDimitry Andric const_cast<LiveIntervals&>(LIS) 1493b0f4066SDimitry Andric .shrinkToUses(const_cast<LiveInterval*>(CurLI)); 1503b0f4066SDimitry Andric UseBlocks.clear(); 1513b0f4066SDimitry Andric ThroughBlocks.clear(); 1523b0f4066SDimitry Andric bool fixed = calcLiveBlockInfo(); 1533b0f4066SDimitry Andric (void)fixed; 1543b0f4066SDimitry Andric assert(fixed && "Couldn't fix broken live interval"); 1553b0f4066SDimitry Andric } 1563b0f4066SDimitry Andric 1573b0f4066SDimitry Andric DEBUG(dbgs() << "Analyze counted " 1583b0f4066SDimitry Andric << UseSlots.size() << " instrs in " 1593b0f4066SDimitry Andric << UseBlocks.size() << " blocks, through " 1603b0f4066SDimitry Andric << NumThroughBlocks << " blocks.\n"); 161e580952dSDimitry Andric } 162e580952dSDimitry Andric 1632754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks 1642754fe60SDimitry Andric /// where CurLI is live. 1653b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() { 1663b0f4066SDimitry Andric ThroughBlocks.resize(MF.getNumBlockIDs()); 167bd5abe19SDimitry Andric NumThroughBlocks = NumGapBlocks = 0; 1682754fe60SDimitry Andric if (CurLI->empty()) 1693b0f4066SDimitry Andric return true; 170e580952dSDimitry Andric 1712754fe60SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin(); 1722754fe60SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end(); 173e580952dSDimitry Andric 1742754fe60SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; 1752754fe60SDimitry Andric UseI = UseSlots.begin(); 1762754fe60SDimitry Andric UseE = UseSlots.end(); 1772754fe60SDimitry Andric 1782754fe60SDimitry Andric // Loop over basic blocks where CurLI is live. 1792754fe60SDimitry Andric MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start); 1802754fe60SDimitry Andric for (;;) { 1812754fe60SDimitry Andric BlockInfo BI; 1822754fe60SDimitry Andric BI.MBB = MFI; 1832754fe60SDimitry Andric SlotIndex Start, Stop; 18491bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 1852754fe60SDimitry Andric 186bd5abe19SDimitry Andric // If the block contains no uses, the range must be live through. At one 18717a519f9SDimitry Andric // point, RegisterCoalescer could create dangling ranges that ended 188bd5abe19SDimitry Andric // mid-block. 189bd5abe19SDimitry Andric if (UseI == UseE || *UseI >= Stop) { 190bd5abe19SDimitry Andric ++NumThroughBlocks; 191bd5abe19SDimitry Andric ThroughBlocks.set(BI.MBB->getNumber()); 192bd5abe19SDimitry Andric // The range shouldn't end mid-block if there are no uses. This shouldn't 193bd5abe19SDimitry Andric // happen. 194bd5abe19SDimitry Andric if (LVI->end < Stop) 195bd5abe19SDimitry Andric return false; 196bd5abe19SDimitry Andric } else { 197bd5abe19SDimitry Andric // This block has uses. Find the first and last uses in the block. 1986122f3e6SDimitry Andric BI.FirstInstr = *UseI; 1996122f3e6SDimitry Andric assert(BI.FirstInstr >= Start); 2002754fe60SDimitry Andric do ++UseI; 2012754fe60SDimitry Andric while (UseI != UseE && *UseI < Stop); 2026122f3e6SDimitry Andric BI.LastInstr = UseI[-1]; 2036122f3e6SDimitry Andric assert(BI.LastInstr < Stop); 204bd5abe19SDimitry Andric 205bd5abe19SDimitry Andric // LVI is the first live segment overlapping MBB. 206bd5abe19SDimitry Andric BI.LiveIn = LVI->start <= Start; 207e580952dSDimitry Andric 2086122f3e6SDimitry Andric // When not live in, the first use should be a def. 2096122f3e6SDimitry Andric if (!BI.LiveIn) { 210f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 2116122f3e6SDimitry Andric assert(LVI->start == BI.FirstInstr && "First instr should be a def"); 2126122f3e6SDimitry Andric BI.FirstDef = BI.FirstInstr; 2136122f3e6SDimitry Andric } 2146122f3e6SDimitry Andric 2152754fe60SDimitry Andric // Look for gaps in the live range. 2162754fe60SDimitry Andric BI.LiveOut = true; 2172754fe60SDimitry Andric while (LVI->end < Stop) { 2182754fe60SDimitry Andric SlotIndex LastStop = LVI->end; 2192754fe60SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) { 2202754fe60SDimitry Andric BI.LiveOut = false; 2216122f3e6SDimitry Andric BI.LastInstr = LastStop; 222e580952dSDimitry Andric break; 223e580952dSDimitry Andric } 2246122f3e6SDimitry Andric 2252754fe60SDimitry Andric if (LastStop < LVI->start) { 226bd5abe19SDimitry Andric // There is a gap in the live range. Create duplicate entries for the 227bd5abe19SDimitry Andric // live-in snippet and the live-out snippet. 228bd5abe19SDimitry Andric ++NumGapBlocks; 229bd5abe19SDimitry Andric 230bd5abe19SDimitry Andric // Push the Live-in part. 231bd5abe19SDimitry Andric BI.LiveOut = false; 232bd5abe19SDimitry Andric UseBlocks.push_back(BI); 2336122f3e6SDimitry Andric UseBlocks.back().LastInstr = LastStop; 234bd5abe19SDimitry Andric 235bd5abe19SDimitry Andric // Set up BI for the live-out part. 236bd5abe19SDimitry Andric BI.LiveIn = false; 237bd5abe19SDimitry Andric BI.LiveOut = true; 2386122f3e6SDimitry Andric BI.FirstInstr = BI.FirstDef = LVI->start; 239e580952dSDimitry Andric } 240e580952dSDimitry Andric 241f785676fSDimitry Andric // A Segment that starts in the middle of the block must be a def. 242f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 2436122f3e6SDimitry Andric if (!BI.FirstDef) 2446122f3e6SDimitry Andric BI.FirstDef = LVI->start; 2456122f3e6SDimitry Andric } 2466122f3e6SDimitry Andric 2473b0f4066SDimitry Andric UseBlocks.push_back(BI); 248e580952dSDimitry Andric 2492754fe60SDimitry Andric // LVI is now at LVE or LVI->end >= Stop. 2502754fe60SDimitry Andric if (LVI == LVE) 2512754fe60SDimitry Andric break; 252bd5abe19SDimitry Andric } 2532754fe60SDimitry Andric 2542754fe60SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment. 2552754fe60SDimitry Andric if (LVI->end == Stop && ++LVI == LVE) 2562754fe60SDimitry Andric break; 2572754fe60SDimitry Andric 2582754fe60SDimitry Andric // Pick the next basic block. 2592754fe60SDimitry Andric if (LVI->start < Stop) 2602754fe60SDimitry Andric ++MFI; 2612754fe60SDimitry Andric else 2622754fe60SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start); 2632754fe60SDimitry Andric } 264bd5abe19SDimitry Andric 265bd5abe19SDimitry Andric assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); 2663b0f4066SDimitry Andric return true; 2673b0f4066SDimitry Andric } 2683b0f4066SDimitry Andric 2693b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { 2703b0f4066SDimitry Andric if (cli->empty()) 2713b0f4066SDimitry Andric return 0; 2723b0f4066SDimitry Andric LiveInterval *li = const_cast<LiveInterval*>(cli); 2733b0f4066SDimitry Andric LiveInterval::iterator LVI = li->begin(); 2743b0f4066SDimitry Andric LiveInterval::iterator LVE = li->end(); 2753b0f4066SDimitry Andric unsigned Count = 0; 2763b0f4066SDimitry Andric 2773b0f4066SDimitry Andric // Loop over basic blocks where li is live. 2783b0f4066SDimitry Andric MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start); 2793b0f4066SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MFI); 2803b0f4066SDimitry Andric for (;;) { 2813b0f4066SDimitry Andric ++Count; 2823b0f4066SDimitry Andric LVI = li->advanceTo(LVI, Stop); 2833b0f4066SDimitry Andric if (LVI == LVE) 2843b0f4066SDimitry Andric return Count; 2853b0f4066SDimitry Andric do { 2863b0f4066SDimitry Andric ++MFI; 2873b0f4066SDimitry Andric Stop = LIS.getMBBEndIdx(MFI); 2883b0f4066SDimitry Andric } while (Stop <= LVI->start); 2893b0f4066SDimitry Andric } 290e580952dSDimitry Andric } 291e580952dSDimitry Andric 292dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { 293dd6029ffSDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg); 294dd6029ffSDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg); 295dd6029ffSDimitry Andric assert(!Orig.empty() && "Splitting empty interval?"); 296dd6029ffSDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx); 297dd6029ffSDimitry Andric 298dd6029ffSDimitry Andric // Range containing Idx should begin at Idx. 299dd6029ffSDimitry Andric if (I != Orig.end() && I->start <= Idx) 300dd6029ffSDimitry Andric return I->start == Idx; 301dd6029ffSDimitry Andric 302dd6029ffSDimitry Andric // Range does not contain Idx, previous must end at Idx. 303dd6029ffSDimitry Andric return I != Orig.begin() && (--I)->end == Idx; 304dd6029ffSDimitry Andric } 305dd6029ffSDimitry Andric 306e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) { 307e580952dSDimitry Andric clear(); 3082754fe60SDimitry Andric CurLI = li; 309e580952dSDimitry Andric analyzeUses(); 310e580952dSDimitry Andric } 311e580952dSDimitry Andric 312e580952dSDimitry Andric 313e580952dSDimitry Andric //===----------------------------------------------------------------------===// 3143b0f4066SDimitry Andric // Split Editor 315e580952dSDimitry Andric //===----------------------------------------------------------------------===// 316e580952dSDimitry Andric 3173b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 31839d628a0SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm, 319f785676fSDimitry Andric MachineDominatorTree &mdt, 320f785676fSDimitry Andric MachineBlockFrequencyInfo &mbfi) 32139d628a0SDimitry Andric : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()), 32239d628a0SDimitry Andric MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()), 32339d628a0SDimitry Andric TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()), 32439d628a0SDimitry Andric MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition), 32539d628a0SDimitry Andric RegAssign(Allocator) {} 326e580952dSDimitry Andric 3276122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) { 3286122f3e6SDimitry Andric Edit = &LRE; 3296122f3e6SDimitry Andric SpillMode = SM; 3303b0f4066SDimitry Andric OpenIdx = 0; 3313b0f4066SDimitry Andric RegAssign.clear(); 3322754fe60SDimitry Andric Values.clear(); 3333b0f4066SDimitry Andric 3346122f3e6SDimitry Andric // Reset the LiveRangeCalc instances needed for this spill mode. 3357ae0e2c9SDimitry Andric LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 3367ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator()); 3376122f3e6SDimitry Andric if (SpillMode) 3387ae0e2c9SDimitry Andric LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 3397ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator()); 3403b0f4066SDimitry Andric 3413b0f4066SDimitry Andric // We don't need an AliasAnalysis since we will only be performing 3423b0f4066SDimitry Andric // cheap-as-a-copy remats anyway. 34391bc56edSDimitry Andric Edit->anyRematerializable(nullptr); 3442754fe60SDimitry Andric } 3452754fe60SDimitry Andric 3463861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3473b0f4066SDimitry Andric void SplitEditor::dump() const { 3483b0f4066SDimitry Andric if (RegAssign.empty()) { 3493b0f4066SDimitry Andric dbgs() << " empty\n"; 3503b0f4066SDimitry Andric return; 3512754fe60SDimitry Andric } 3522754fe60SDimitry Andric 3533b0f4066SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) 3543b0f4066SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); 3553b0f4066SDimitry Andric dbgs() << '\n'; 3563b0f4066SDimitry Andric } 3573861d79fSDimitry Andric #endif 3583b0f4066SDimitry Andric 3593b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx, 3603b0f4066SDimitry Andric const VNInfo *ParentVNI, 3613b0f4066SDimitry Andric SlotIndex Idx) { 362e580952dSDimitry Andric assert(ParentVNI && "Mapping NULL value"); 363e580952dSDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 3643b0f4066SDimitry Andric assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); 365f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 3662754fe60SDimitry Andric 3672754fe60SDimitry Andric // Create a new value. 368dff0c46cSDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator()); 3692754fe60SDimitry Andric 370e580952dSDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup. 371e580952dSDimitry Andric std::pair<ValueMap::iterator, bool> InsP = 3726122f3e6SDimitry Andric Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), 3736122f3e6SDimitry Andric ValueForcePair(VNI, false))); 3742754fe60SDimitry Andric 3753b0f4066SDimitry Andric // This was the first time (RegIdx, ParentVNI) was mapped. 3763b0f4066SDimitry Andric // Keep it as a simple def without any liveness. 3773b0f4066SDimitry Andric if (InsP.second) 3783b0f4066SDimitry Andric return VNI; 3793b0f4066SDimitry Andric 3803b0f4066SDimitry Andric // If the previous value was a simple mapping, add liveness for it now. 3816122f3e6SDimitry Andric if (VNInfo *OldVNI = InsP.first->second.getPointer()) { 3823b0f4066SDimitry Andric SlotIndex Def = OldVNI->def; 383f785676fSDimitry Andric LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI)); 3846122f3e6SDimitry Andric // No longer a simple mapping. Switch to a complex, non-forced mapping. 3856122f3e6SDimitry Andric InsP.first->second = ValueForcePair(); 3863b0f4066SDimitry Andric } 3873b0f4066SDimitry Andric 3883b0f4066SDimitry Andric // This is a complex mapping, add liveness for VNI 3893b0f4066SDimitry Andric SlotIndex Def = VNI->def; 390f785676fSDimitry Andric LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI)); 3912754fe60SDimitry Andric 3922754fe60SDimitry Andric return VNI; 3932754fe60SDimitry Andric } 3942754fe60SDimitry Andric 3956122f3e6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) { 3962754fe60SDimitry Andric assert(ParentVNI && "Mapping NULL value"); 3976122f3e6SDimitry Andric ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)]; 3986122f3e6SDimitry Andric VNInfo *VNI = VFP.getPointer(); 3993b0f4066SDimitry Andric 4006122f3e6SDimitry Andric // ParentVNI was either unmapped or already complex mapped. Either way, just 4016122f3e6SDimitry Andric // set the force bit. 4026122f3e6SDimitry Andric if (!VNI) { 4036122f3e6SDimitry Andric VFP.setInt(true); 4043b0f4066SDimitry Andric return; 4056122f3e6SDimitry Andric } 4063b0f4066SDimitry Andric 4073b0f4066SDimitry Andric // This was previously a single mapping. Make sure the old def is represented 4083b0f4066SDimitry Andric // by a trivial live range. 4093b0f4066SDimitry Andric SlotIndex Def = VNI->def; 410f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 411f785676fSDimitry Andric LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI)); 4126122f3e6SDimitry Andric // Mark as complex mapped, forced. 41391bc56edSDimitry Andric VFP = ValueForcePair(nullptr, true); 414e580952dSDimitry Andric } 415e580952dSDimitry Andric 4162754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, 4172754fe60SDimitry Andric VNInfo *ParentVNI, 4182754fe60SDimitry Andric SlotIndex UseIdx, 419e580952dSDimitry Andric MachineBasicBlock &MBB, 420e580952dSDimitry Andric MachineBasicBlock::iterator I) { 42191bc56edSDimitry Andric MachineInstr *CopyMI = nullptr; 4222754fe60SDimitry Andric SlotIndex Def; 423f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 4243b0f4066SDimitry Andric 4253b0f4066SDimitry Andric // We may be trying to avoid interference that ends at a deleted instruction, 4263b0f4066SDimitry Andric // so always begin RegIdx 0 early and all others late. 4273b0f4066SDimitry Andric bool Late = RegIdx != 0; 4282754fe60SDimitry Andric 4292754fe60SDimitry Andric // Attempt cheap-as-a-copy rematerialization. 4302754fe60SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI); 431dff0c46cSDimitry Andric if (Edit->canRematerializeAt(RM, UseIdx, true)) { 432dff0c46cSDimitry Andric Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late); 433bd5abe19SDimitry Andric ++NumRemats; 4342754fe60SDimitry Andric } else { 4352754fe60SDimitry Andric // Can't remat, just insert a copy from parent. 4362754fe60SDimitry Andric CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) 4373b0f4066SDimitry Andric .addReg(Edit->getReg()); 4383b0f4066SDimitry Andric Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late) 439dff0c46cSDimitry Andric .getRegSlot(); 440bd5abe19SDimitry Andric ++NumCopies; 4412754fe60SDimitry Andric } 4422754fe60SDimitry Andric 4432754fe60SDimitry Andric // Define the value in Reg. 444dff0c46cSDimitry Andric return defValue(RegIdx, ParentVNI, Def); 445e580952dSDimitry Andric } 446e580952dSDimitry Andric 447e580952dSDimitry Andric /// Create a new virtual register and live interval. 4483b0f4066SDimitry Andric unsigned SplitEditor::openIntv() { 4492754fe60SDimitry Andric // Create the complement as index 0. 4503b0f4066SDimitry Andric if (Edit->empty()) 451f785676fSDimitry Andric Edit->createEmptyInterval(); 452e580952dSDimitry Andric 4532754fe60SDimitry Andric // Create the open interval. 4543b0f4066SDimitry Andric OpenIdx = Edit->size(); 455f785676fSDimitry Andric Edit->createEmptyInterval(); 4563b0f4066SDimitry Andric return OpenIdx; 4573b0f4066SDimitry Andric } 4583b0f4066SDimitry Andric 4593b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) { 4603b0f4066SDimitry Andric assert(Idx != 0 && "Cannot select the complement interval"); 4613b0f4066SDimitry Andric assert(Idx < Edit->size() && "Can only select previously opened interval"); 46217a519f9SDimitry Andric DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n'); 4633b0f4066SDimitry Andric OpenIdx = Idx; 4642754fe60SDimitry Andric } 465e580952dSDimitry Andric 4662754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { 4672754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore"); 4682754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvBefore " << Idx); 4692754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 4703b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 4712754fe60SDimitry Andric if (!ParentVNI) { 4722754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 4732754fe60SDimitry Andric return Idx; 4742754fe60SDimitry Andric } 4752754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 4762754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 477e580952dSDimitry Andric assert(MI && "enterIntvBefore called with invalid index"); 478e580952dSDimitry Andric 4792754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); 4802754fe60SDimitry Andric return VNI->def; 481e580952dSDimitry Andric } 482e580952dSDimitry Andric 48317a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) { 48417a519f9SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAfter"); 48517a519f9SDimitry Andric DEBUG(dbgs() << " enterIntvAfter " << Idx); 48617a519f9SDimitry Andric Idx = Idx.getBoundaryIndex(); 48717a519f9SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 48817a519f9SDimitry Andric if (!ParentVNI) { 48917a519f9SDimitry Andric DEBUG(dbgs() << ": not live\n"); 49017a519f9SDimitry Andric return Idx; 49117a519f9SDimitry Andric } 49217a519f9SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 49317a519f9SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 49417a519f9SDimitry Andric assert(MI && "enterIntvAfter called with invalid index"); 49517a519f9SDimitry Andric 49617a519f9SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), 49791bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI))); 49817a519f9SDimitry Andric return VNI->def; 49917a519f9SDimitry Andric } 50017a519f9SDimitry Andric 5012754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { 5022754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); 5032754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB); 5042754fe60SDimitry Andric SlotIndex Last = End.getPrevSlot(); 5052754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); 5063b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); 5072754fe60SDimitry Andric if (!ParentVNI) { 5082754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5092754fe60SDimitry Andric return End; 5102754fe60SDimitry Andric } 5112754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id); 5122754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, 513dff0c46cSDimitry Andric SA.getLastSplitPointIter(&MBB)); 5142754fe60SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx); 5152754fe60SDimitry Andric DEBUG(dump()); 5162754fe60SDimitry Andric return VNI->def; 517e580952dSDimitry Andric } 518e580952dSDimitry Andric 5192754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI. 520e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) { 5212754fe60SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); 522e580952dSDimitry Andric } 523e580952dSDimitry Andric 524e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { 5252754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv"); 5262754fe60SDimitry Andric DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); 5272754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 5282754fe60SDimitry Andric DEBUG(dump()); 529e580952dSDimitry Andric } 530e580952dSDimitry Andric 5312754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { 5322754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter"); 5332754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAfter " << Idx); 5342754fe60SDimitry Andric 5352754fe60SDimitry Andric // The interval must be live beyond the instruction at Idx. 5366122f3e6SDimitry Andric SlotIndex Boundary = Idx.getBoundaryIndex(); 5376122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary); 5382754fe60SDimitry Andric if (!ParentVNI) { 5392754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5406122f3e6SDimitry Andric return Boundary.getNextSlot(); 5412754fe60SDimitry Andric } 5422754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 5436122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Boundary); 5442754fe60SDimitry Andric assert(MI && "No instruction at index"); 5456122f3e6SDimitry Andric 5466122f3e6SDimitry Andric // In spill mode, make live ranges as short as possible by inserting the copy 5476122f3e6SDimitry Andric // before MI. This is only possible if that instruction doesn't redefine the 5486122f3e6SDimitry Andric // value. The inserted COPY is not a kill, and we don't need to recompute 5496122f3e6SDimitry Andric // the source live range. The spiller also won't try to hoist this copy. 5506122f3e6SDimitry Andric if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) && 5516122f3e6SDimitry Andric MI->readsVirtualRegister(Edit->getReg())) { 5526122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 5536122f3e6SDimitry Andric defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 5546122f3e6SDimitry Andric return Idx; 5556122f3e6SDimitry Andric } 5566122f3e6SDimitry Andric 5576122f3e6SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(), 55891bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI))); 5592754fe60SDimitry Andric return VNI->def; 560e580952dSDimitry Andric } 561e580952dSDimitry Andric 5622754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { 5632754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore"); 5642754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvBefore " << Idx); 565e580952dSDimitry Andric 5662754fe60SDimitry Andric // The interval must be live into the instruction at Idx. 5676122f3e6SDimitry Andric Idx = Idx.getBaseIndex(); 5683b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 5692754fe60SDimitry Andric if (!ParentVNI) { 5702754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5712754fe60SDimitry Andric return Idx.getNextSlot(); 5722754fe60SDimitry Andric } 5732754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 5742754fe60SDimitry Andric 5752754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 5762754fe60SDimitry Andric assert(MI && "No instruction at index"); 5772754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 5782754fe60SDimitry Andric return VNI->def; 579e580952dSDimitry Andric } 580e580952dSDimitry Andric 5812754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { 5822754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); 5832754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB); 5842754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); 5852754fe60SDimitry Andric 5863b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 5872754fe60SDimitry Andric if (!ParentVNI) { 5882754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5892754fe60SDimitry Andric return Start; 590e580952dSDimitry Andric } 591e580952dSDimitry Andric 5922754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, 5932754fe60SDimitry Andric MBB.SkipPHIsAndLabels(MBB.begin())); 5942754fe60SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx); 5952754fe60SDimitry Andric DEBUG(dump()); 5962754fe60SDimitry Andric return VNI->def; 597e580952dSDimitry Andric } 598e580952dSDimitry Andric 5992754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { 6002754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv"); 6013b0f4066SDimitry Andric const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 602dff0c46cSDimitry Andric assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) && 6032754fe60SDimitry Andric "Parent changes value in extended range"); 6042754fe60SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && 6052754fe60SDimitry Andric "Range cannot span basic blocks"); 606e580952dSDimitry Andric 6076122f3e6SDimitry Andric // The complement interval will be extended as needed by LRCalc.extend(). 6083b0f4066SDimitry Andric if (ParentVNI) 6096122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 6102754fe60SDimitry Andric DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); 6112754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 6122754fe60SDimitry Andric DEBUG(dump()); 613e580952dSDimitry Andric } 614e580952dSDimitry Andric 6156122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 6166122f3e6SDimitry Andric // Spill modes 6176122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 6186122f3e6SDimitry Andric 6196122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) { 620f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 6216122f3e6SDimitry Andric DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n"); 6226122f3e6SDimitry Andric RegAssignMap::iterator AssignI; 6236122f3e6SDimitry Andric AssignI.setMap(RegAssign); 6246122f3e6SDimitry Andric 6256122f3e6SDimitry Andric for (unsigned i = 0, e = Copies.size(); i != e; ++i) { 626ff0cc061SDimitry Andric SlotIndex Def = Copies[i]->def; 6276122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Def); 6286122f3e6SDimitry Andric assert(MI && "No instruction for back-copy"); 6296122f3e6SDimitry Andric 6306122f3e6SDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 6316122f3e6SDimitry Andric MachineBasicBlock::iterator MBBI(MI); 6326122f3e6SDimitry Andric bool AtBegin; 6336122f3e6SDimitry Andric do AtBegin = MBBI == MBB->begin(); 6346122f3e6SDimitry Andric while (!AtBegin && (--MBBI)->isDebugValue()); 6356122f3e6SDimitry Andric 6366122f3e6SDimitry Andric DEBUG(dbgs() << "Removing " << Def << '\t' << *MI); 637ff0cc061SDimitry Andric LIS.removeVRegDefAt(*LI, Def); 6386122f3e6SDimitry Andric LIS.RemoveMachineInstrFromMaps(MI); 6396122f3e6SDimitry Andric MI->eraseFromParent(); 6406122f3e6SDimitry Andric 641ff0cc061SDimitry Andric // Adjust RegAssign if a register assignment is killed at Def. We want to 642ff0cc061SDimitry Andric // avoid calculating the live range of the source register if possible. 6437ae0e2c9SDimitry Andric AssignI.find(Def.getPrevSlot()); 6446122f3e6SDimitry Andric if (!AssignI.valid() || AssignI.start() >= Def) 6456122f3e6SDimitry Andric continue; 6466122f3e6SDimitry Andric // If MI doesn't kill the assigned register, just leave it. 6476122f3e6SDimitry Andric if (AssignI.stop() != Def) 6486122f3e6SDimitry Andric continue; 6496122f3e6SDimitry Andric unsigned RegIdx = AssignI.value(); 6506122f3e6SDimitry Andric if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) { 6516122f3e6SDimitry Andric DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n'); 6526122f3e6SDimitry Andric forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def)); 6536122f3e6SDimitry Andric } else { 654dff0c46cSDimitry Andric SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot(); 6556122f3e6SDimitry Andric DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI); 6566122f3e6SDimitry Andric AssignI.setStop(Kill); 6576122f3e6SDimitry Andric } 6586122f3e6SDimitry Andric } 6596122f3e6SDimitry Andric } 6606122f3e6SDimitry Andric 6616122f3e6SDimitry Andric MachineBasicBlock* 6626122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB, 6636122f3e6SDimitry Andric MachineBasicBlock *DefMBB) { 6646122f3e6SDimitry Andric if (MBB == DefMBB) 6656122f3e6SDimitry Andric return MBB; 6666122f3e6SDimitry Andric assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def."); 6676122f3e6SDimitry Andric 6686122f3e6SDimitry Andric const MachineLoopInfo &Loops = SA.Loops; 6696122f3e6SDimitry Andric const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB); 6706122f3e6SDimitry Andric MachineDomTreeNode *DefDomNode = MDT[DefMBB]; 6716122f3e6SDimitry Andric 6726122f3e6SDimitry Andric // Best candidate so far. 6736122f3e6SDimitry Andric MachineBasicBlock *BestMBB = MBB; 6746122f3e6SDimitry Andric unsigned BestDepth = UINT_MAX; 6756122f3e6SDimitry Andric 6766122f3e6SDimitry Andric for (;;) { 6776122f3e6SDimitry Andric const MachineLoop *Loop = Loops.getLoopFor(MBB); 6786122f3e6SDimitry Andric 6796122f3e6SDimitry Andric // MBB isn't in a loop, it doesn't get any better. All dominators have a 6806122f3e6SDimitry Andric // higher frequency by definition. 6816122f3e6SDimitry Andric if (!Loop) { 6826122f3e6SDimitry Andric DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 6836122f3e6SDimitry Andric << MBB->getNumber() << " at depth 0\n"); 6846122f3e6SDimitry Andric return MBB; 6856122f3e6SDimitry Andric } 6866122f3e6SDimitry Andric 6876122f3e6SDimitry Andric // We'll never be able to exit the DefLoop. 6886122f3e6SDimitry Andric if (Loop == DefLoop) { 6896122f3e6SDimitry Andric DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 6906122f3e6SDimitry Andric << MBB->getNumber() << " in the same loop\n"); 6916122f3e6SDimitry Andric return MBB; 6926122f3e6SDimitry Andric } 6936122f3e6SDimitry Andric 6946122f3e6SDimitry Andric // Least busy dominator seen so far. 6956122f3e6SDimitry Andric unsigned Depth = Loop->getLoopDepth(); 6966122f3e6SDimitry Andric if (Depth < BestDepth) { 6976122f3e6SDimitry Andric BestMBB = MBB; 6986122f3e6SDimitry Andric BestDepth = Depth; 6996122f3e6SDimitry Andric DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 7006122f3e6SDimitry Andric << MBB->getNumber() << " at depth " << Depth << '\n'); 7016122f3e6SDimitry Andric } 7026122f3e6SDimitry Andric 7036122f3e6SDimitry Andric // Leave loop by going to the immediate dominator of the loop header. 7046122f3e6SDimitry Andric // This is a bigger stride than simply walking up the dominator tree. 7056122f3e6SDimitry Andric MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom(); 7066122f3e6SDimitry Andric 7076122f3e6SDimitry Andric // Too far up the dominator tree? 7086122f3e6SDimitry Andric if (!IDom || !MDT.dominates(DefDomNode, IDom)) 7096122f3e6SDimitry Andric return BestMBB; 7106122f3e6SDimitry Andric 7116122f3e6SDimitry Andric MBB = IDom->getBlock(); 7126122f3e6SDimitry Andric } 7136122f3e6SDimitry Andric } 7146122f3e6SDimitry Andric 7156122f3e6SDimitry Andric void SplitEditor::hoistCopiesForSize() { 7166122f3e6SDimitry Andric // Get the complement interval, always RegIdx 0. 717f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 7186122f3e6SDimitry Andric LiveInterval *Parent = &Edit->getParent(); 7196122f3e6SDimitry Andric 7206122f3e6SDimitry Andric // Track the nearest common dominator for all back-copies for each ParentVNI, 7216122f3e6SDimitry Andric // indexed by ParentVNI->id. 7226122f3e6SDimitry Andric typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair; 7236122f3e6SDimitry Andric SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums()); 7246122f3e6SDimitry Andric 7256122f3e6SDimitry Andric // Find the nearest common dominator for parent values with multiple 7266122f3e6SDimitry Andric // back-copies. If a single back-copy dominates, put it in DomPair.second. 72739d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) { 7287ae0e2c9SDimitry Andric if (VNI->isUnused()) 7297ae0e2c9SDimitry Andric continue; 7306122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 7316122f3e6SDimitry Andric assert(ParentVNI && "Parent not live at complement def"); 7326122f3e6SDimitry Andric 7336122f3e6SDimitry Andric // Don't hoist remats. The complement is probably going to disappear 7346122f3e6SDimitry Andric // completely anyway. 7356122f3e6SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 7366122f3e6SDimitry Andric continue; 7376122f3e6SDimitry Andric 7386122f3e6SDimitry Andric MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def); 7396122f3e6SDimitry Andric DomPair &Dom = NearestDom[ParentVNI->id]; 7406122f3e6SDimitry Andric 7416122f3e6SDimitry Andric // Keep directly defined parent values. This is either a PHI or an 7426122f3e6SDimitry Andric // instruction in the complement range. All other copies of ParentVNI 7436122f3e6SDimitry Andric // should be eliminated. 7446122f3e6SDimitry Andric if (VNI->def == ParentVNI->def) { 7456122f3e6SDimitry Andric DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n'); 7466122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 7476122f3e6SDimitry Andric continue; 7486122f3e6SDimitry Andric } 7496122f3e6SDimitry Andric // Skip the singly mapped values. There is nothing to gain from hoisting a 7506122f3e6SDimitry Andric // single back-copy. 7516122f3e6SDimitry Andric if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) { 7526122f3e6SDimitry Andric DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n'); 7536122f3e6SDimitry Andric continue; 7546122f3e6SDimitry Andric } 7556122f3e6SDimitry Andric 7566122f3e6SDimitry Andric if (!Dom.first) { 7576122f3e6SDimitry Andric // First time we see ParentVNI. VNI dominates itself. 7586122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 7596122f3e6SDimitry Andric } else if (Dom.first == ValMBB) { 7606122f3e6SDimitry Andric // Two defs in the same block. Pick the earlier def. 7616122f3e6SDimitry Andric if (!Dom.second.isValid() || VNI->def < Dom.second) 7626122f3e6SDimitry Andric Dom.second = VNI->def; 7636122f3e6SDimitry Andric } else { 7646122f3e6SDimitry Andric // Different basic blocks. Check if one dominates. 7656122f3e6SDimitry Andric MachineBasicBlock *Near = 7666122f3e6SDimitry Andric MDT.findNearestCommonDominator(Dom.first, ValMBB); 7676122f3e6SDimitry Andric if (Near == ValMBB) 7686122f3e6SDimitry Andric // Def ValMBB dominates. 7696122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 7706122f3e6SDimitry Andric else if (Near != Dom.first) 7716122f3e6SDimitry Andric // None dominate. Hoist to common dominator, need new def. 7726122f3e6SDimitry Andric Dom = DomPair(Near, SlotIndex()); 7736122f3e6SDimitry Andric } 7746122f3e6SDimitry Andric 7756122f3e6SDimitry Andric DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def 7766122f3e6SDimitry Andric << " for parent " << ParentVNI->id << '@' << ParentVNI->def 7776122f3e6SDimitry Andric << " hoist to BB#" << Dom.first->getNumber() << ' ' 7786122f3e6SDimitry Andric << Dom.second << '\n'); 7796122f3e6SDimitry Andric } 7806122f3e6SDimitry Andric 7816122f3e6SDimitry Andric // Insert the hoisted copies. 7826122f3e6SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 7836122f3e6SDimitry Andric DomPair &Dom = NearestDom[i]; 7846122f3e6SDimitry Andric if (!Dom.first || Dom.second.isValid()) 7856122f3e6SDimitry Andric continue; 7866122f3e6SDimitry Andric // This value needs a hoisted copy inserted at the end of Dom.first. 7876122f3e6SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i); 7886122f3e6SDimitry Andric MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def); 7896122f3e6SDimitry Andric // Get a less loopy dominator than Dom.first. 7906122f3e6SDimitry Andric Dom.first = findShallowDominator(Dom.first, DefMBB); 7916122f3e6SDimitry Andric SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot(); 7926122f3e6SDimitry Andric Dom.second = 7936122f3e6SDimitry Andric defFromParent(0, ParentVNI, Last, *Dom.first, 794dff0c46cSDimitry Andric SA.getLastSplitPointIter(Dom.first))->def; 7956122f3e6SDimitry Andric } 7966122f3e6SDimitry Andric 7976122f3e6SDimitry Andric // Remove redundant back-copies that are now known to be dominated by another 7986122f3e6SDimitry Andric // def with the same value. 7996122f3e6SDimitry Andric SmallVector<VNInfo*, 8> BackCopies; 80039d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) { 8017ae0e2c9SDimitry Andric if (VNI->isUnused()) 8027ae0e2c9SDimitry Andric continue; 8036122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 8046122f3e6SDimitry Andric const DomPair &Dom = NearestDom[ParentVNI->id]; 8056122f3e6SDimitry Andric if (!Dom.first || Dom.second == VNI->def) 8066122f3e6SDimitry Andric continue; 8076122f3e6SDimitry Andric BackCopies.push_back(VNI); 8086122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 8096122f3e6SDimitry Andric } 8106122f3e6SDimitry Andric removeBackCopies(BackCopies); 8116122f3e6SDimitry Andric } 8126122f3e6SDimitry Andric 8136122f3e6SDimitry Andric 8143b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges. 8156122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend(). 8163b0f4066SDimitry Andric bool SplitEditor::transferValues() { 8173b0f4066SDimitry Andric bool Skipped = false; 8183b0f4066SDimitry Andric RegAssignMap::const_iterator AssignI = RegAssign.begin(); 81939d628a0SDimitry Andric for (const LiveRange::Segment &S : Edit->getParent()) { 82039d628a0SDimitry Andric DEBUG(dbgs() << " blit " << S << ':'); 82139d628a0SDimitry Andric VNInfo *ParentVNI = S.valno; 8223b0f4066SDimitry Andric // RegAssign has holes where RegIdx 0 should be used. 82339d628a0SDimitry Andric SlotIndex Start = S.start; 8243b0f4066SDimitry Andric AssignI.advanceTo(Start); 8253b0f4066SDimitry Andric do { 8263b0f4066SDimitry Andric unsigned RegIdx; 82739d628a0SDimitry Andric SlotIndex End = S.end; 8283b0f4066SDimitry Andric if (!AssignI.valid()) { 8293b0f4066SDimitry Andric RegIdx = 0; 8303b0f4066SDimitry Andric } else if (AssignI.start() <= Start) { 8313b0f4066SDimitry Andric RegIdx = AssignI.value(); 8323b0f4066SDimitry Andric if (AssignI.stop() < End) { 8333b0f4066SDimitry Andric End = AssignI.stop(); 8343b0f4066SDimitry Andric ++AssignI; 8353b0f4066SDimitry Andric } 8363b0f4066SDimitry Andric } else { 8373b0f4066SDimitry Andric RegIdx = 0; 8383b0f4066SDimitry Andric End = std::min(End, AssignI.start()); 839e580952dSDimitry Andric } 840e580952dSDimitry Andric 8413b0f4066SDimitry Andric // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. 8423b0f4066SDimitry Andric DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx); 843f785676fSDimitry Andric LiveRange &LR = LIS.getInterval(Edit->get(RegIdx)); 8443b0f4066SDimitry Andric 8453b0f4066SDimitry Andric // Check for a simply defined value that can be blitted directly. 8466122f3e6SDimitry Andric ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id)); 8476122f3e6SDimitry Andric if (VNInfo *VNI = VFP.getPointer()) { 8483b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id); 849f785676fSDimitry Andric LR.addSegment(LiveInterval::Segment(Start, End, VNI)); 8503b0f4066SDimitry Andric Start = End; 8513b0f4066SDimitry Andric continue; 8523b0f4066SDimitry Andric } 8533b0f4066SDimitry Andric 8546122f3e6SDimitry Andric // Skip values with forced recomputation. 8556122f3e6SDimitry Andric if (VFP.getInt()) { 8566122f3e6SDimitry Andric DEBUG(dbgs() << "(recalc)"); 8573b0f4066SDimitry Andric Skipped = true; 8583b0f4066SDimitry Andric Start = End; 8593b0f4066SDimitry Andric continue; 8603b0f4066SDimitry Andric } 8613b0f4066SDimitry Andric 8626122f3e6SDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 8633b0f4066SDimitry Andric 8643b0f4066SDimitry Andric // This value has multiple defs in RegIdx, but it wasn't rematerialized, 8653b0f4066SDimitry Andric // so the live range is accurate. Add live-in blocks in [Start;End) to the 8663b0f4066SDimitry Andric // LiveInBlocks. 8673b0f4066SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 8683b0f4066SDimitry Andric SlotIndex BlockStart, BlockEnd; 86991bc56edSDimitry Andric std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB); 8703b0f4066SDimitry Andric 8713b0f4066SDimitry Andric // The first block may be live-in, or it may have its own def. 8723b0f4066SDimitry Andric if (Start != BlockStart) { 873f785676fSDimitry Andric VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End)); 8743b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped value"); 8753b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber()); 8763b0f4066SDimitry Andric // MBB has its own def. Is it also live-out? 8776122f3e6SDimitry Andric if (BlockEnd <= End) 8786122f3e6SDimitry Andric LRC.setLiveOutValue(MBB, VNI); 8796122f3e6SDimitry Andric 8803b0f4066SDimitry Andric // Skip to the next block for live-in. 8813b0f4066SDimitry Andric ++MBB; 8823b0f4066SDimitry Andric BlockStart = BlockEnd; 8833b0f4066SDimitry Andric } 8843b0f4066SDimitry Andric 8853b0f4066SDimitry Andric // Handle the live-in blocks covered by [Start;End). 8863b0f4066SDimitry Andric assert(Start <= BlockStart && "Expected live-in block"); 8873b0f4066SDimitry Andric while (BlockStart < End) { 8883b0f4066SDimitry Andric DEBUG(dbgs() << ">BB#" << MBB->getNumber()); 8893b0f4066SDimitry Andric BlockEnd = LIS.getMBBEndIdx(MBB); 8903b0f4066SDimitry Andric if (BlockStart == ParentVNI->def) { 8913b0f4066SDimitry Andric // This block has the def of a parent PHI, so it isn't live-in. 8923b0f4066SDimitry Andric assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); 893f785676fSDimitry Andric VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End)); 8943b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped parent PHI"); 8956122f3e6SDimitry Andric if (End >= BlockEnd) 8966122f3e6SDimitry Andric LRC.setLiveOutValue(MBB, VNI); // Live-out as well. 8973b0f4066SDimitry Andric } else { 8986122f3e6SDimitry Andric // This block needs a live-in value. The last block covered may not 8996122f3e6SDimitry Andric // be live-out. 9003b0f4066SDimitry Andric if (End < BlockEnd) 901f785676fSDimitry Andric LRC.addLiveInBlock(LR, MDT[MBB], End); 9023b0f4066SDimitry Andric else { 9036122f3e6SDimitry Andric // Live-through, and we don't know the value. 904f785676fSDimitry Andric LRC.addLiveInBlock(LR, MDT[MBB]); 90591bc56edSDimitry Andric LRC.setLiveOutValue(MBB, nullptr); 9063b0f4066SDimitry Andric } 9073b0f4066SDimitry Andric } 9083b0f4066SDimitry Andric BlockStart = BlockEnd; 9093b0f4066SDimitry Andric ++MBB; 9103b0f4066SDimitry Andric } 9113b0f4066SDimitry Andric Start = End; 91239d628a0SDimitry Andric } while (Start != S.end); 9133b0f4066SDimitry Andric DEBUG(dbgs() << '\n'); 9143b0f4066SDimitry Andric } 9153b0f4066SDimitry Andric 9167ae0e2c9SDimitry Andric LRCalc[0].calculateValues(); 9176122f3e6SDimitry Andric if (SpillMode) 9187ae0e2c9SDimitry Andric LRCalc[1].calculateValues(); 9193b0f4066SDimitry Andric 9203b0f4066SDimitry Andric return Skipped; 9213b0f4066SDimitry Andric } 9223b0f4066SDimitry Andric 9233b0f4066SDimitry Andric void SplitEditor::extendPHIKillRanges() { 9243b0f4066SDimitry Andric // Extend live ranges to be live-out for successor PHI values. 92539d628a0SDimitry Andric for (const VNInfo *PHIVNI : Edit->getParent().valnos) { 9263b0f4066SDimitry Andric if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) 9273b0f4066SDimitry Andric continue; 9283b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(PHIVNI->def); 929f785676fSDimitry Andric LiveRange &LR = LIS.getInterval(Edit->get(RegIdx)); 9306122f3e6SDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 9313b0f4066SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); 9323b0f4066SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 9333b0f4066SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 9346122f3e6SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(*PI); 9356122f3e6SDimitry Andric SlotIndex LastUse = End.getPrevSlot(); 9363b0f4066SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an 9373b0f4066SDimitry Andric // undef PHI operand. 9386122f3e6SDimitry Andric if (Edit->getParent().liveAt(LastUse)) { 9396122f3e6SDimitry Andric assert(RegAssign.lookup(LastUse) == RegIdx && 9403b0f4066SDimitry Andric "Different register assignment in phi predecessor"); 941f785676fSDimitry Andric LRC.extend(LR, End); 9423b0f4066SDimitry Andric } 9433b0f4066SDimitry Andric } 9443b0f4066SDimitry Andric } 9453b0f4066SDimitry Andric } 9463b0f4066SDimitry Andric 9473b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg(). 9483b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) { 9493b0f4066SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), 9502754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) { 95191bc56edSDimitry Andric MachineOperand &MO = *RI; 952e580952dSDimitry Andric MachineInstr *MI = MO.getParent(); 953e580952dSDimitry Andric ++RI; 9542754fe60SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions. 955e580952dSDimitry Andric if (MI->isDebugValue()) { 956e580952dSDimitry Andric DEBUG(dbgs() << "Zapping " << *MI); 957e580952dSDimitry Andric MO.setReg(0); 958e580952dSDimitry Andric continue; 959e580952dSDimitry Andric } 9602754fe60SDimitry Andric 9616122f3e6SDimitry Andric // <undef> operands don't really read the register, so it doesn't matter 9626122f3e6SDimitry Andric // which register we choose. When the use operand is tied to a def, we must 9636122f3e6SDimitry Andric // use the same register as the def, so just do that always. 9642754fe60SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI); 9656122f3e6SDimitry Andric if (MO.isDef() || MO.isUndef()) 966dff0c46cSDimitry Andric Idx = Idx.getRegSlot(MO.isEarlyClobber()); 9672754fe60SDimitry Andric 9682754fe60SDimitry Andric // Rewrite to the mapped register at Idx. 9692754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx); 970f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 9716122f3e6SDimitry Andric MO.setReg(LI->reg); 9722754fe60SDimitry Andric DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' 9732754fe60SDimitry Andric << Idx << ':' << RegIdx << '\t' << *MI); 9742754fe60SDimitry Andric 9753b0f4066SDimitry Andric // Extend liveness to Idx if the instruction reads reg. 9766122f3e6SDimitry Andric if (!ExtendRanges || MO.isUndef()) 9772754fe60SDimitry Andric continue; 9783b0f4066SDimitry Andric 9793b0f4066SDimitry Andric // Skip instructions that don't read Reg. 9803b0f4066SDimitry Andric if (MO.isDef()) { 9813b0f4066SDimitry Andric if (!MO.getSubReg() && !MO.isEarlyClobber()) 9823b0f4066SDimitry Andric continue; 9833b0f4066SDimitry Andric // We may wan't to extend a live range for a partial redef, or for a use 9843b0f4066SDimitry Andric // tied to an early clobber. 9853b0f4066SDimitry Andric Idx = Idx.getPrevSlot(); 9863b0f4066SDimitry Andric if (!Edit->getParent().liveAt(Idx)) 9873b0f4066SDimitry Andric continue; 9883b0f4066SDimitry Andric } else 989dff0c46cSDimitry Andric Idx = Idx.getRegSlot(true); 9903b0f4066SDimitry Andric 991f785676fSDimitry Andric getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot()); 992e580952dSDimitry Andric } 993e580952dSDimitry Andric } 994e580952dSDimitry Andric 9953b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() { 9963b0f4066SDimitry Andric SmallVector<MachineInstr*, 8> Dead; 9973b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ 998f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(*I); 99939d628a0SDimitry Andric for (const LiveRange::Segment &S : LI->segments) { 1000dff0c46cSDimitry Andric // Dead defs end at the dead slot. 100139d628a0SDimitry Andric if (S.end != S.valno->def.getDeadSlot()) 10023b0f4066SDimitry Andric continue; 100339d628a0SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def); 10043b0f4066SDimitry Andric assert(MI && "Missing instruction for dead def"); 10053b0f4066SDimitry Andric MI->addRegisterDead(LI->reg, &TRI); 10063b0f4066SDimitry Andric 10073b0f4066SDimitry Andric if (!MI->allDefsAreDead()) 10083b0f4066SDimitry Andric continue; 10093b0f4066SDimitry Andric 10103b0f4066SDimitry Andric DEBUG(dbgs() << "All defs dead: " << *MI); 10113b0f4066SDimitry Andric Dead.push_back(MI); 10123b0f4066SDimitry Andric } 10133b0f4066SDimitry Andric } 10143b0f4066SDimitry Andric 10153b0f4066SDimitry Andric if (Dead.empty()) 10163b0f4066SDimitry Andric return; 10173b0f4066SDimitry Andric 1018dff0c46cSDimitry Andric Edit->eliminateDeadDefs(Dead); 10193b0f4066SDimitry Andric } 10203b0f4066SDimitry Andric 10213b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { 10223b0f4066SDimitry Andric ++NumFinished; 10232754fe60SDimitry Andric 10242754fe60SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to 10252754fe60SDimitry Andric // the inserted copies. 10262754fe60SDimitry Andric 10272754fe60SDimitry Andric // Add the original defs from the parent interval. 102839d628a0SDimitry Andric for (const VNInfo *ParentVNI : Edit->getParent().valnos) { 10292754fe60SDimitry Andric if (ParentVNI->isUnused()) 10302754fe60SDimitry Andric continue; 10313b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(ParentVNI->def); 10327ae0e2c9SDimitry Andric defValue(RegIdx, ParentVNI, ParentVNI->def); 10333b0f4066SDimitry Andric 10346122f3e6SDimitry Andric // Force rematted values to be recomputed everywhere. 10353b0f4066SDimitry Andric // The new live ranges may be truncated. 10363b0f4066SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 10373b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 10386122f3e6SDimitry Andric forceRecompute(i, ParentVNI); 10396122f3e6SDimitry Andric } 10406122f3e6SDimitry Andric 10416122f3e6SDimitry Andric // Hoist back-copies to the complement interval when in spill mode. 10426122f3e6SDimitry Andric switch (SpillMode) { 10436122f3e6SDimitry Andric case SM_Partition: 10446122f3e6SDimitry Andric // Leave all back-copies as is. 10456122f3e6SDimitry Andric break; 10466122f3e6SDimitry Andric case SM_Size: 10476122f3e6SDimitry Andric hoistCopiesForSize(); 10486122f3e6SDimitry Andric break; 10496122f3e6SDimitry Andric case SM_Speed: 10506122f3e6SDimitry Andric llvm_unreachable("Spill mode 'speed' not implemented yet"); 10512754fe60SDimitry Andric } 10522754fe60SDimitry Andric 10533b0f4066SDimitry Andric // Transfer the simply mapped values, check if any are skipped. 10543b0f4066SDimitry Andric bool Skipped = transferValues(); 10553b0f4066SDimitry Andric if (Skipped) 10563b0f4066SDimitry Andric extendPHIKillRanges(); 10572754fe60SDimitry Andric else 10583b0f4066SDimitry Andric ++NumSimple; 10592754fe60SDimitry Andric 10603b0f4066SDimitry Andric // Rewrite virtual registers, possibly extending ranges. 10613b0f4066SDimitry Andric rewriteAssigned(Skipped); 10622754fe60SDimitry Andric 10633b0f4066SDimitry Andric // Delete defs that were rematted everywhere. 10643b0f4066SDimitry Andric if (Skipped) 10653b0f4066SDimitry Andric deleteRematVictims(); 10662754fe60SDimitry Andric 10672754fe60SDimitry Andric // Get rid of unused values and set phi-kill flags. 1068f785676fSDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) { 1069f785676fSDimitry Andric LiveInterval &LI = LIS.getInterval(*I); 1070f785676fSDimitry Andric LI.RenumberValues(); 1071f785676fSDimitry Andric } 10722754fe60SDimitry Andric 10733b0f4066SDimitry Andric // Provide a reverse mapping from original indices to Edit ranges. 10743b0f4066SDimitry Andric if (LRMap) { 10753b0f4066SDimitry Andric LRMap->clear(); 10763b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 10773b0f4066SDimitry Andric LRMap->push_back(i); 10783b0f4066SDimitry Andric } 10793b0f4066SDimitry Andric 10802754fe60SDimitry Andric // Now check if any registers were separated into multiple components. 10812754fe60SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS); 10823b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) { 10832754fe60SDimitry Andric // Don't use iterators, they are invalidated by create() below. 1084f785676fSDimitry Andric LiveInterval *li = &LIS.getInterval(Edit->get(i)); 10852754fe60SDimitry Andric unsigned NumComp = ConEQ.Classify(li); 10862754fe60SDimitry Andric if (NumComp <= 1) 10872754fe60SDimitry Andric continue; 10882754fe60SDimitry Andric DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n'); 10892754fe60SDimitry Andric SmallVector<LiveInterval*, 8> dups; 10902754fe60SDimitry Andric dups.push_back(li); 10913b0f4066SDimitry Andric for (unsigned j = 1; j != NumComp; ++j) 1092f785676fSDimitry Andric dups.push_back(&Edit->createEmptyInterval()); 10933b0f4066SDimitry Andric ConEQ.Distribute(&dups[0], MRI); 10943b0f4066SDimitry Andric // The new intervals all map back to i. 10953b0f4066SDimitry Andric if (LRMap) 10963b0f4066SDimitry Andric LRMap->resize(Edit->size(), i); 10972754fe60SDimitry Andric } 10982754fe60SDimitry Andric 1099e580952dSDimitry Andric // Calculate spill weight and allocation hints for new intervals. 1100f785676fSDimitry Andric Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI); 11013b0f4066SDimitry Andric 11023b0f4066SDimitry Andric assert(!LRMap || LRMap->size() == Edit->size()); 1103e580952dSDimitry Andric } 1104e580952dSDimitry Andric 1105e580952dSDimitry Andric 1106e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1107e580952dSDimitry Andric // Single Block Splitting 1108e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1109e580952dSDimitry Andric 11106122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI, 11116122f3e6SDimitry Andric bool SingleInstrs) const { 11126122f3e6SDimitry Andric // Always split for multiple instructions. 11136122f3e6SDimitry Andric if (!BI.isOneInstr()) 11146122f3e6SDimitry Andric return true; 11156122f3e6SDimitry Andric // Don't split for single instructions unless explicitly requested. 11166122f3e6SDimitry Andric if (!SingleInstrs) 11172754fe60SDimitry Andric return false; 11186122f3e6SDimitry Andric // Splitting a live-through range always makes progress. 11196122f3e6SDimitry Andric if (BI.LiveIn && BI.LiveOut) 11206122f3e6SDimitry Andric return true; 11216122f3e6SDimitry Andric // No point in isolating a copy. It has no register class constraints. 11226122f3e6SDimitry Andric if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike()) 11236122f3e6SDimitry Andric return false; 11246122f3e6SDimitry Andric // Finally, don't isolate an end point that was created by earlier splits. 11256122f3e6SDimitry Andric return isOriginalEndpoint(BI.FirstInstr); 1126e580952dSDimitry Andric } 1127e580952dSDimitry Andric 11283b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { 11293b0f4066SDimitry Andric openIntv(); 11303b0f4066SDimitry Andric SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); 11316122f3e6SDimitry Andric SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr, 11323b0f4066SDimitry Andric LastSplitPoint)); 11336122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) { 11346122f3e6SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastInstr)); 11353b0f4066SDimitry Andric } else { 11363b0f4066SDimitry Andric // The last use is after the last valid split point. 11373b0f4066SDimitry Andric SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); 11383b0f4066SDimitry Andric useIntv(SegStart, SegStop); 11396122f3e6SDimitry Andric overlapIntv(SegStop, BI.LastInstr); 11403b0f4066SDimitry Andric } 11413b0f4066SDimitry Andric } 11423b0f4066SDimitry Andric 114317a519f9SDimitry Andric 114417a519f9SDimitry Andric //===----------------------------------------------------------------------===// 114517a519f9SDimitry Andric // Global Live Range Splitting Support 114617a519f9SDimitry Andric //===----------------------------------------------------------------------===// 114717a519f9SDimitry Andric 114817a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a 114917a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split 115017a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference. 115117a519f9SDimitry Andric // 115217a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges 115317a519f9SDimitry Andric // are on the stack. 115417a519f9SDimitry Andric 115517a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum, 115617a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore, 115717a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter){ 115817a519f9SDimitry Andric SlotIndex Start, Stop; 115991bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum); 116017a519f9SDimitry Andric 116117a519f9SDimitry Andric DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop 116217a519f9SDimitry Andric << ") intf " << LeaveBefore << '-' << EnterAfter 116317a519f9SDimitry Andric << ", live-through " << IntvIn << " -> " << IntvOut); 116417a519f9SDimitry Andric 116517a519f9SDimitry Andric assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks"); 116617a519f9SDimitry Andric 11676122f3e6SDimitry Andric assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block"); 11686122f3e6SDimitry Andric assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf"); 11696122f3e6SDimitry Andric assert((!EnterAfter || EnterAfter >= Start) && "Interference before block"); 11706122f3e6SDimitry Andric 11716122f3e6SDimitry Andric MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum); 11726122f3e6SDimitry Andric 117317a519f9SDimitry Andric if (!IntvOut) { 117417a519f9SDimitry Andric DEBUG(dbgs() << ", spill on entry.\n"); 117517a519f9SDimitry Andric // 117617a519f9SDimitry Andric // <<<<<<<<< Possible LeaveBefore interference. 117717a519f9SDimitry Andric // |-----------| Live through. 117817a519f9SDimitry Andric // -____________ Spill on entry. 117917a519f9SDimitry Andric // 118017a519f9SDimitry Andric selectIntv(IntvIn); 118117a519f9SDimitry Andric SlotIndex Idx = leaveIntvAtTop(*MBB); 118217a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 118317a519f9SDimitry Andric (void)Idx; 118417a519f9SDimitry Andric return; 118517a519f9SDimitry Andric } 118617a519f9SDimitry Andric 118717a519f9SDimitry Andric if (!IntvIn) { 118817a519f9SDimitry Andric DEBUG(dbgs() << ", reload on exit.\n"); 118917a519f9SDimitry Andric // 119017a519f9SDimitry Andric // >>>>>>> Possible EnterAfter interference. 119117a519f9SDimitry Andric // |-----------| Live through. 119217a519f9SDimitry Andric // ___________-- Reload on exit. 119317a519f9SDimitry Andric // 119417a519f9SDimitry Andric selectIntv(IntvOut); 119517a519f9SDimitry Andric SlotIndex Idx = enterIntvAtEnd(*MBB); 119617a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 119717a519f9SDimitry Andric (void)Idx; 119817a519f9SDimitry Andric return; 119917a519f9SDimitry Andric } 120017a519f9SDimitry Andric 120117a519f9SDimitry Andric if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) { 120217a519f9SDimitry Andric DEBUG(dbgs() << ", straight through.\n"); 120317a519f9SDimitry Andric // 120417a519f9SDimitry Andric // |-----------| Live through. 120517a519f9SDimitry Andric // ------------- Straight through, same intv, no interference. 120617a519f9SDimitry Andric // 120717a519f9SDimitry Andric selectIntv(IntvOut); 120817a519f9SDimitry Andric useIntv(Start, Stop); 120917a519f9SDimitry Andric return; 121017a519f9SDimitry Andric } 121117a519f9SDimitry Andric 121217a519f9SDimitry Andric // We cannot legally insert splits after LSP. 121317a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(MBBNum); 12146122f3e6SDimitry Andric assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf"); 121517a519f9SDimitry Andric 121617a519f9SDimitry Andric if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter || 121717a519f9SDimitry Andric LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) { 121817a519f9SDimitry Andric DEBUG(dbgs() << ", switch avoiding interference.\n"); 121917a519f9SDimitry Andric // 122017a519f9SDimitry Andric // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference. 122117a519f9SDimitry Andric // |-----------| Live through. 122217a519f9SDimitry Andric // ------======= Switch intervals between interference. 122317a519f9SDimitry Andric // 122417a519f9SDimitry Andric selectIntv(IntvOut); 12256122f3e6SDimitry Andric SlotIndex Idx; 12266122f3e6SDimitry Andric if (LeaveBefore && LeaveBefore < LSP) { 12276122f3e6SDimitry Andric Idx = enterIntvBefore(LeaveBefore); 122817a519f9SDimitry Andric useIntv(Idx, Stop); 12296122f3e6SDimitry Andric } else { 12306122f3e6SDimitry Andric Idx = enterIntvAtEnd(*MBB); 12316122f3e6SDimitry Andric } 123217a519f9SDimitry Andric selectIntv(IntvIn); 123317a519f9SDimitry Andric useIntv(Start, Idx); 123417a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 123517a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 123617a519f9SDimitry Andric return; 123717a519f9SDimitry Andric } 123817a519f9SDimitry Andric 123917a519f9SDimitry Andric DEBUG(dbgs() << ", create local intv for interference.\n"); 124017a519f9SDimitry Andric // 124117a519f9SDimitry Andric // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference. 124217a519f9SDimitry Andric // |-----------| Live through. 124317a519f9SDimitry Andric // ==---------== Switch intervals before/after interference. 124417a519f9SDimitry Andric // 124517a519f9SDimitry Andric assert(LeaveBefore <= EnterAfter && "Missed case"); 124617a519f9SDimitry Andric 124717a519f9SDimitry Andric selectIntv(IntvOut); 124817a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter); 124917a519f9SDimitry Andric useIntv(Idx, Stop); 125017a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 125117a519f9SDimitry Andric 125217a519f9SDimitry Andric selectIntv(IntvIn); 125317a519f9SDimitry Andric Idx = leaveIntvBefore(LeaveBefore); 125417a519f9SDimitry Andric useIntv(Start, Idx); 125517a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 125617a519f9SDimitry Andric } 125717a519f9SDimitry Andric 125817a519f9SDimitry Andric 125917a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI, 126017a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore) { 126117a519f9SDimitry Andric SlotIndex Start, Stop; 126291bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 126317a519f9SDimitry Andric 126417a519f9SDimitry Andric DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop 12656122f3e6SDimitry Andric << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 126617a519f9SDimitry Andric << ", reg-in " << IntvIn << ", leave before " << LeaveBefore 126717a519f9SDimitry Andric << (BI.LiveOut ? ", stack-out" : ", killed in block")); 126817a519f9SDimitry Andric 126917a519f9SDimitry Andric assert(IntvIn && "Must have register in"); 127017a519f9SDimitry Andric assert(BI.LiveIn && "Must be live-in"); 127117a519f9SDimitry Andric assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference"); 127217a519f9SDimitry Andric 12736122f3e6SDimitry Andric if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) { 127417a519f9SDimitry Andric DEBUG(dbgs() << " before interference.\n"); 127517a519f9SDimitry Andric // 127617a519f9SDimitry Andric // <<< Interference after kill. 127717a519f9SDimitry Andric // |---o---x | Killed in block. 127817a519f9SDimitry Andric // ========= Use IntvIn everywhere. 127917a519f9SDimitry Andric // 128017a519f9SDimitry Andric selectIntv(IntvIn); 12816122f3e6SDimitry Andric useIntv(Start, BI.LastInstr); 128217a519f9SDimitry Andric return; 128317a519f9SDimitry Andric } 128417a519f9SDimitry Andric 128517a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 128617a519f9SDimitry Andric 12876122f3e6SDimitry Andric if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) { 128817a519f9SDimitry Andric // 128917a519f9SDimitry Andric // <<< Possible interference after last use. 129017a519f9SDimitry Andric // |---o---o---| Live-out on stack. 129117a519f9SDimitry Andric // =========____ Leave IntvIn after last use. 129217a519f9SDimitry Andric // 129317a519f9SDimitry Andric // < Interference after last use. 129417a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use. 129517a519f9SDimitry Andric // ============ Copy to stack after LSP, overlap IntvIn. 129617a519f9SDimitry Andric // \_____ Stack interval is live-out. 129717a519f9SDimitry Andric // 12986122f3e6SDimitry Andric if (BI.LastInstr < LSP) { 129917a519f9SDimitry Andric DEBUG(dbgs() << ", spill after last use before interference.\n"); 130017a519f9SDimitry Andric selectIntv(IntvIn); 13016122f3e6SDimitry Andric SlotIndex Idx = leaveIntvAfter(BI.LastInstr); 130217a519f9SDimitry Andric useIntv(Start, Idx); 130317a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 130417a519f9SDimitry Andric } else { 130517a519f9SDimitry Andric DEBUG(dbgs() << ", spill before last split point.\n"); 130617a519f9SDimitry Andric selectIntv(IntvIn); 130717a519f9SDimitry Andric SlotIndex Idx = leaveIntvBefore(LSP); 13086122f3e6SDimitry Andric overlapIntv(Idx, BI.LastInstr); 130917a519f9SDimitry Andric useIntv(Start, Idx); 131017a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 131117a519f9SDimitry Andric } 131217a519f9SDimitry Andric return; 131317a519f9SDimitry Andric } 131417a519f9SDimitry Andric 131517a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvIn. That 131617a519f9SDimitry Andric // means we need to create a local interval that can be allocated a 131717a519f9SDimitry Andric // different register. 131817a519f9SDimitry Andric unsigned LocalIntv = openIntv(); 131917a519f9SDimitry Andric (void)LocalIntv; 132017a519f9SDimitry Andric DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n"); 132117a519f9SDimitry Andric 13226122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LSP) { 132317a519f9SDimitry Andric // 132417a519f9SDimitry Andric // <<<<<<< Interference overlapping uses. 132517a519f9SDimitry Andric // |---o---o---| Live-out on stack. 132617a519f9SDimitry Andric // =====----____ Leave IntvIn before interference, then spill. 132717a519f9SDimitry Andric // 13286122f3e6SDimitry Andric SlotIndex To = leaveIntvAfter(BI.LastInstr); 132917a519f9SDimitry Andric SlotIndex From = enterIntvBefore(LeaveBefore); 133017a519f9SDimitry Andric useIntv(From, To); 133117a519f9SDimitry Andric selectIntv(IntvIn); 133217a519f9SDimitry Andric useIntv(Start, From); 133317a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 133417a519f9SDimitry Andric return; 133517a519f9SDimitry Andric } 133617a519f9SDimitry Andric 133717a519f9SDimitry Andric // <<<<<<< Interference overlapping uses. 133817a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use. 133917a519f9SDimitry Andric // =====------- Copy to stack before LSP, overlap LocalIntv. 134017a519f9SDimitry Andric // \_____ Stack interval is live-out. 134117a519f9SDimitry Andric // 134217a519f9SDimitry Andric SlotIndex To = leaveIntvBefore(LSP); 13436122f3e6SDimitry Andric overlapIntv(To, BI.LastInstr); 134417a519f9SDimitry Andric SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore)); 134517a519f9SDimitry Andric useIntv(From, To); 134617a519f9SDimitry Andric selectIntv(IntvIn); 134717a519f9SDimitry Andric useIntv(Start, From); 134817a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 134917a519f9SDimitry Andric } 135017a519f9SDimitry Andric 135117a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, 135217a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter) { 135317a519f9SDimitry Andric SlotIndex Start, Stop; 135491bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 135517a519f9SDimitry Andric 135617a519f9SDimitry Andric DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop 13576122f3e6SDimitry Andric << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 135817a519f9SDimitry Andric << ", reg-out " << IntvOut << ", enter after " << EnterAfter 135917a519f9SDimitry Andric << (BI.LiveIn ? ", stack-in" : ", defined in block")); 136017a519f9SDimitry Andric 136117a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 136217a519f9SDimitry Andric 136317a519f9SDimitry Andric assert(IntvOut && "Must have register out"); 136417a519f9SDimitry Andric assert(BI.LiveOut && "Must be live-out"); 136517a519f9SDimitry Andric assert((!EnterAfter || EnterAfter < LSP) && "Bad interference"); 136617a519f9SDimitry Andric 13676122f3e6SDimitry Andric if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) { 136817a519f9SDimitry Andric DEBUG(dbgs() << " after interference.\n"); 136917a519f9SDimitry Andric // 137017a519f9SDimitry Andric // >>>> Interference before def. 137117a519f9SDimitry Andric // | o---o---| Defined in block. 137217a519f9SDimitry Andric // ========= Use IntvOut everywhere. 137317a519f9SDimitry Andric // 137417a519f9SDimitry Andric selectIntv(IntvOut); 13756122f3e6SDimitry Andric useIntv(BI.FirstInstr, Stop); 137617a519f9SDimitry Andric return; 137717a519f9SDimitry Andric } 137817a519f9SDimitry Andric 13796122f3e6SDimitry Andric if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) { 138017a519f9SDimitry Andric DEBUG(dbgs() << ", reload after interference.\n"); 138117a519f9SDimitry Andric // 138217a519f9SDimitry Andric // >>>> Interference before def. 138317a519f9SDimitry Andric // |---o---o---| Live-through, stack-in. 138417a519f9SDimitry Andric // ____========= Enter IntvOut before first use. 138517a519f9SDimitry Andric // 138617a519f9SDimitry Andric selectIntv(IntvOut); 13876122f3e6SDimitry Andric SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr)); 138817a519f9SDimitry Andric useIntv(Idx, Stop); 138917a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 139017a519f9SDimitry Andric return; 139117a519f9SDimitry Andric } 139217a519f9SDimitry Andric 139317a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvOut. That 139417a519f9SDimitry Andric // means we need to create a local interval that can be allocated a 139517a519f9SDimitry Andric // different register. 139617a519f9SDimitry Andric DEBUG(dbgs() << ", interference overlaps uses.\n"); 139717a519f9SDimitry Andric // 139817a519f9SDimitry Andric // >>>>>>> Interference overlapping uses. 139917a519f9SDimitry Andric // |---o---o---| Live-through, stack-in. 140017a519f9SDimitry Andric // ____---====== Create local interval for interference range. 140117a519f9SDimitry Andric // 140217a519f9SDimitry Andric selectIntv(IntvOut); 140317a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter); 140417a519f9SDimitry Andric useIntv(Idx, Stop); 140517a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 140617a519f9SDimitry Andric 140717a519f9SDimitry Andric openIntv(); 14086122f3e6SDimitry Andric SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr)); 140917a519f9SDimitry Andric useIntv(From, Idx); 141017a519f9SDimitry Andric } 1411