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" 193ca95b02SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 202754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 21e580952dSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 226122f3e6SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 23e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 24139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 25e580952dSDimitry Andric #include "llvm/Support/Debug.h" 26e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h" 27e580952dSDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 28e580952dSDimitry Andric #include "llvm/Target/TargetMachine.h" 29e580952dSDimitry Andric 30e580952dSDimitry Andric using namespace llvm; 31e580952dSDimitry Andric 3291bc56edSDimitry Andric #define DEBUG_TYPE "regalloc" 3391bc56edSDimitry Andric 343b0f4066SDimitry Andric STATISTIC(NumFinished, "Number of splits finished"); 353b0f4066SDimitry Andric STATISTIC(NumSimple, "Number of splits that were simple"); 36bd5abe19SDimitry Andric STATISTIC(NumCopies, "Number of copies inserted for splitting"); 37bd5abe19SDimitry Andric STATISTIC(NumRemats, "Number of rematerialized defs for splitting"); 38bd5abe19SDimitry Andric STATISTIC(NumRepairs, "Number of invalid live ranges repaired"); 39e580952dSDimitry Andric 40e580952dSDimitry Andric //===----------------------------------------------------------------------===// 413ca95b02SDimitry Andric // Last Insert Point Analysis 423ca95b02SDimitry Andric //===----------------------------------------------------------------------===// 433ca95b02SDimitry Andric 443ca95b02SDimitry Andric InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis, 453ca95b02SDimitry Andric unsigned BBNum) 463ca95b02SDimitry Andric : LIS(lis), LastInsertPoint(BBNum) {} 473ca95b02SDimitry Andric 483ca95b02SDimitry Andric SlotIndex 493ca95b02SDimitry Andric InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI, 503ca95b02SDimitry Andric const MachineBasicBlock &MBB) { 513ca95b02SDimitry Andric unsigned Num = MBB.getNumber(); 523ca95b02SDimitry Andric std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num]; 533ca95b02SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB); 543ca95b02SDimitry Andric 553ca95b02SDimitry Andric SmallVector<const MachineBasicBlock *, 1> EHPadSucessors; 563ca95b02SDimitry Andric for (const MachineBasicBlock *SMBB : MBB.successors()) 573ca95b02SDimitry Andric if (SMBB->isEHPad()) 583ca95b02SDimitry Andric EHPadSucessors.push_back(SMBB); 593ca95b02SDimitry Andric 603ca95b02SDimitry Andric // Compute insert points on the first call. The pair is independent of the 613ca95b02SDimitry Andric // current live interval. 623ca95b02SDimitry Andric if (!LIP.first.isValid()) { 633ca95b02SDimitry Andric MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator(); 643ca95b02SDimitry Andric if (FirstTerm == MBB.end()) 653ca95b02SDimitry Andric LIP.first = MBBEnd; 663ca95b02SDimitry Andric else 673ca95b02SDimitry Andric LIP.first = LIS.getInstructionIndex(*FirstTerm); 683ca95b02SDimitry Andric 693ca95b02SDimitry Andric // If there is a landing pad successor, also find the call instruction. 703ca95b02SDimitry Andric if (EHPadSucessors.empty()) 713ca95b02SDimitry Andric return LIP.first; 723ca95b02SDimitry Andric // There may not be a call instruction (?) in which case we ignore LPad. 733ca95b02SDimitry Andric LIP.second = LIP.first; 743ca95b02SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin(); 753ca95b02SDimitry Andric I != E;) { 763ca95b02SDimitry Andric --I; 773ca95b02SDimitry Andric if (I->isCall()) { 783ca95b02SDimitry Andric LIP.second = LIS.getInstructionIndex(*I); 793ca95b02SDimitry Andric break; 803ca95b02SDimitry Andric } 813ca95b02SDimitry Andric } 823ca95b02SDimitry Andric } 833ca95b02SDimitry Andric 843ca95b02SDimitry Andric // If CurLI is live into a landing pad successor, move the last insert point 853ca95b02SDimitry Andric // back to the call that may throw. 863ca95b02SDimitry Andric if (!LIP.second) 873ca95b02SDimitry Andric return LIP.first; 883ca95b02SDimitry Andric 893ca95b02SDimitry Andric if (none_of(EHPadSucessors, [&](const MachineBasicBlock *EHPad) { 903ca95b02SDimitry Andric return LIS.isLiveInToMBB(CurLI, EHPad); 913ca95b02SDimitry Andric })) 923ca95b02SDimitry Andric return LIP.first; 933ca95b02SDimitry Andric 943ca95b02SDimitry Andric // Find the value leaving MBB. 953ca95b02SDimitry Andric const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd); 963ca95b02SDimitry Andric if (!VNI) 973ca95b02SDimitry Andric return LIP.first; 983ca95b02SDimitry Andric 993ca95b02SDimitry Andric // If the value leaving MBB was defined after the call in MBB, it can't 1003ca95b02SDimitry Andric // really be live-in to the landing pad. This can happen if the landing pad 1013ca95b02SDimitry Andric // has a PHI, and this register is undef on the exceptional edge. 1023ca95b02SDimitry Andric // <rdar://problem/10664933> 1033ca95b02SDimitry Andric if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd) 1043ca95b02SDimitry Andric return LIP.first; 1053ca95b02SDimitry Andric 1063ca95b02SDimitry Andric // Value is properly live-in to the landing pad. 1073ca95b02SDimitry Andric // Only allow inserts before the call. 1083ca95b02SDimitry Andric return LIP.second; 1093ca95b02SDimitry Andric } 1103ca95b02SDimitry Andric 1113ca95b02SDimitry Andric MachineBasicBlock::iterator 1123ca95b02SDimitry Andric InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI, 1133ca95b02SDimitry Andric MachineBasicBlock &MBB) { 1143ca95b02SDimitry Andric SlotIndex LIP = getLastInsertPoint(CurLI, MBB); 1153ca95b02SDimitry Andric if (LIP == LIS.getMBBEndIdx(&MBB)) 1163ca95b02SDimitry Andric return MBB.end(); 1173ca95b02SDimitry Andric return LIS.getInstructionFromIndex(LIP); 1183ca95b02SDimitry Andric } 1193ca95b02SDimitry Andric 1203ca95b02SDimitry Andric //===----------------------------------------------------------------------===// 121e580952dSDimitry Andric // Split Analysis 122e580952dSDimitry Andric //===----------------------------------------------------------------------===// 123e580952dSDimitry Andric 12439d628a0SDimitry Andric SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, 125e580952dSDimitry Andric const MachineLoopInfo &mli) 12639d628a0SDimitry Andric : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli), 12739d628a0SDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr), 1283ca95b02SDimitry Andric IPA(lis, MF.getNumBlockIDs()) {} 129e580952dSDimitry Andric 130e580952dSDimitry Andric void SplitAnalysis::clear() { 1312754fe60SDimitry Andric UseSlots.clear(); 1323b0f4066SDimitry Andric UseBlocks.clear(); 1333b0f4066SDimitry Andric ThroughBlocks.clear(); 13491bc56edSDimitry Andric CurLI = nullptr; 135bd5abe19SDimitry Andric DidRepairRange = false; 136e580952dSDimitry Andric } 137e580952dSDimitry Andric 1382754fe60SDimitry Andric /// analyzeUses - Count instructions, basic blocks, and loops using CurLI. 139e580952dSDimitry Andric void SplitAnalysis::analyzeUses() { 1403b0f4066SDimitry Andric assert(UseSlots.empty() && "Call clear first"); 1413b0f4066SDimitry Andric 1423b0f4066SDimitry Andric // First get all the defs from the interval values. This provides the correct 1433b0f4066SDimitry Andric // slots for early clobbers. 14439d628a0SDimitry Andric for (const VNInfo *VNI : CurLI->valnos) 14539d628a0SDimitry Andric if (!VNI->isPHIDef() && !VNI->isUnused()) 14639d628a0SDimitry Andric UseSlots.push_back(VNI->def); 1473b0f4066SDimitry Andric 1483b0f4066SDimitry Andric // Get use slots form the use-def chain. 1492754fe60SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 15091bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg)) 15191bc56edSDimitry Andric if (!MO.isUndef()) 1523ca95b02SDimitry Andric UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot()); 1533b0f4066SDimitry Andric 1542754fe60SDimitry Andric array_pod_sort(UseSlots.begin(), UseSlots.end()); 1553b0f4066SDimitry Andric 1563b0f4066SDimitry Andric // Remove duplicates, keeping the smaller slot for each instruction. 1573b0f4066SDimitry Andric // That is what we want for early clobbers. 1583b0f4066SDimitry Andric UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(), 1593b0f4066SDimitry Andric SlotIndex::isSameInstr), 1603b0f4066SDimitry Andric UseSlots.end()); 1613b0f4066SDimitry Andric 1623b0f4066SDimitry Andric // Compute per-live block info. 1633b0f4066SDimitry Andric if (!calcLiveBlockInfo()) { 1643b0f4066SDimitry Andric // FIXME: calcLiveBlockInfo found inconsistencies in the live range. 16517a519f9SDimitry Andric // I am looking at you, RegisterCoalescer! 166bd5abe19SDimitry Andric DidRepairRange = true; 167bd5abe19SDimitry Andric ++NumRepairs; 1683b0f4066SDimitry Andric DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n"); 1693b0f4066SDimitry Andric const_cast<LiveIntervals&>(LIS) 1703b0f4066SDimitry Andric .shrinkToUses(const_cast<LiveInterval*>(CurLI)); 1713b0f4066SDimitry Andric UseBlocks.clear(); 1723b0f4066SDimitry Andric ThroughBlocks.clear(); 1733b0f4066SDimitry Andric bool fixed = calcLiveBlockInfo(); 1743b0f4066SDimitry Andric (void)fixed; 1753b0f4066SDimitry Andric assert(fixed && "Couldn't fix broken live interval"); 1763b0f4066SDimitry Andric } 1773b0f4066SDimitry Andric 1783b0f4066SDimitry Andric DEBUG(dbgs() << "Analyze counted " 1793b0f4066SDimitry Andric << UseSlots.size() << " instrs in " 1803b0f4066SDimitry Andric << UseBlocks.size() << " blocks, through " 1813b0f4066SDimitry Andric << NumThroughBlocks << " blocks.\n"); 182e580952dSDimitry Andric } 183e580952dSDimitry Andric 1842754fe60SDimitry Andric /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks 1852754fe60SDimitry Andric /// where CurLI is live. 1863b0f4066SDimitry Andric bool SplitAnalysis::calcLiveBlockInfo() { 1873b0f4066SDimitry Andric ThroughBlocks.resize(MF.getNumBlockIDs()); 188bd5abe19SDimitry Andric NumThroughBlocks = NumGapBlocks = 0; 1892754fe60SDimitry Andric if (CurLI->empty()) 1903b0f4066SDimitry Andric return true; 191e580952dSDimitry Andric 1922754fe60SDimitry Andric LiveInterval::const_iterator LVI = CurLI->begin(); 1932754fe60SDimitry Andric LiveInterval::const_iterator LVE = CurLI->end(); 194e580952dSDimitry Andric 1952754fe60SDimitry Andric SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE; 1962754fe60SDimitry Andric UseI = UseSlots.begin(); 1972754fe60SDimitry Andric UseE = UseSlots.end(); 1982754fe60SDimitry Andric 1992754fe60SDimitry Andric // Loop over basic blocks where CurLI is live. 2007d523365SDimitry Andric MachineFunction::iterator MFI = 2017d523365SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator(); 2022754fe60SDimitry Andric for (;;) { 2032754fe60SDimitry Andric BlockInfo BI; 2047d523365SDimitry Andric BI.MBB = &*MFI; 2052754fe60SDimitry Andric SlotIndex Start, Stop; 20691bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 2072754fe60SDimitry Andric 208bd5abe19SDimitry Andric // If the block contains no uses, the range must be live through. At one 20917a519f9SDimitry Andric // point, RegisterCoalescer could create dangling ranges that ended 210bd5abe19SDimitry Andric // mid-block. 211bd5abe19SDimitry Andric if (UseI == UseE || *UseI >= Stop) { 212bd5abe19SDimitry Andric ++NumThroughBlocks; 213bd5abe19SDimitry Andric ThroughBlocks.set(BI.MBB->getNumber()); 214bd5abe19SDimitry Andric // The range shouldn't end mid-block if there are no uses. This shouldn't 215bd5abe19SDimitry Andric // happen. 216bd5abe19SDimitry Andric if (LVI->end < Stop) 217bd5abe19SDimitry Andric return false; 218bd5abe19SDimitry Andric } else { 219bd5abe19SDimitry Andric // This block has uses. Find the first and last uses in the block. 2206122f3e6SDimitry Andric BI.FirstInstr = *UseI; 2216122f3e6SDimitry Andric assert(BI.FirstInstr >= Start); 2222754fe60SDimitry Andric do ++UseI; 2232754fe60SDimitry Andric while (UseI != UseE && *UseI < Stop); 2246122f3e6SDimitry Andric BI.LastInstr = UseI[-1]; 2256122f3e6SDimitry Andric assert(BI.LastInstr < Stop); 226bd5abe19SDimitry Andric 227bd5abe19SDimitry Andric // LVI is the first live segment overlapping MBB. 228bd5abe19SDimitry Andric BI.LiveIn = LVI->start <= Start; 229e580952dSDimitry Andric 2306122f3e6SDimitry Andric // When not live in, the first use should be a def. 2316122f3e6SDimitry Andric if (!BI.LiveIn) { 232f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 2336122f3e6SDimitry Andric assert(LVI->start == BI.FirstInstr && "First instr should be a def"); 2346122f3e6SDimitry Andric BI.FirstDef = BI.FirstInstr; 2356122f3e6SDimitry Andric } 2366122f3e6SDimitry Andric 2372754fe60SDimitry Andric // Look for gaps in the live range. 2382754fe60SDimitry Andric BI.LiveOut = true; 2392754fe60SDimitry Andric while (LVI->end < Stop) { 2402754fe60SDimitry Andric SlotIndex LastStop = LVI->end; 2412754fe60SDimitry Andric if (++LVI == LVE || LVI->start >= Stop) { 2422754fe60SDimitry Andric BI.LiveOut = false; 2436122f3e6SDimitry Andric BI.LastInstr = LastStop; 244e580952dSDimitry Andric break; 245e580952dSDimitry Andric } 2466122f3e6SDimitry Andric 2472754fe60SDimitry Andric if (LastStop < LVI->start) { 248bd5abe19SDimitry Andric // There is a gap in the live range. Create duplicate entries for the 249bd5abe19SDimitry Andric // live-in snippet and the live-out snippet. 250bd5abe19SDimitry Andric ++NumGapBlocks; 251bd5abe19SDimitry Andric 252bd5abe19SDimitry Andric // Push the Live-in part. 253bd5abe19SDimitry Andric BI.LiveOut = false; 254bd5abe19SDimitry Andric UseBlocks.push_back(BI); 2556122f3e6SDimitry Andric UseBlocks.back().LastInstr = LastStop; 256bd5abe19SDimitry Andric 257bd5abe19SDimitry Andric // Set up BI for the live-out part. 258bd5abe19SDimitry Andric BI.LiveIn = false; 259bd5abe19SDimitry Andric BI.LiveOut = true; 2606122f3e6SDimitry Andric BI.FirstInstr = BI.FirstDef = LVI->start; 261e580952dSDimitry Andric } 262e580952dSDimitry Andric 263f785676fSDimitry Andric // A Segment that starts in the middle of the block must be a def. 264f785676fSDimitry Andric assert(LVI->start == LVI->valno->def && "Dangling Segment start"); 2656122f3e6SDimitry Andric if (!BI.FirstDef) 2666122f3e6SDimitry Andric BI.FirstDef = LVI->start; 2676122f3e6SDimitry Andric } 2686122f3e6SDimitry Andric 2693b0f4066SDimitry Andric UseBlocks.push_back(BI); 270e580952dSDimitry Andric 2712754fe60SDimitry Andric // LVI is now at LVE or LVI->end >= Stop. 2722754fe60SDimitry Andric if (LVI == LVE) 2732754fe60SDimitry Andric break; 274bd5abe19SDimitry Andric } 2752754fe60SDimitry Andric 2762754fe60SDimitry Andric // Live segment ends exactly at Stop. Move to the next segment. 2772754fe60SDimitry Andric if (LVI->end == Stop && ++LVI == LVE) 2782754fe60SDimitry Andric break; 2792754fe60SDimitry Andric 2802754fe60SDimitry Andric // Pick the next basic block. 2812754fe60SDimitry Andric if (LVI->start < Stop) 2822754fe60SDimitry Andric ++MFI; 2832754fe60SDimitry Andric else 2847d523365SDimitry Andric MFI = LIS.getMBBFromIndex(LVI->start)->getIterator(); 2852754fe60SDimitry Andric } 286bd5abe19SDimitry Andric 287bd5abe19SDimitry Andric assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count"); 2883b0f4066SDimitry Andric return true; 2893b0f4066SDimitry Andric } 2903b0f4066SDimitry Andric 2913b0f4066SDimitry Andric unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const { 2923b0f4066SDimitry Andric if (cli->empty()) 2933b0f4066SDimitry Andric return 0; 2943b0f4066SDimitry Andric LiveInterval *li = const_cast<LiveInterval*>(cli); 2953b0f4066SDimitry Andric LiveInterval::iterator LVI = li->begin(); 2963b0f4066SDimitry Andric LiveInterval::iterator LVE = li->end(); 2973b0f4066SDimitry Andric unsigned Count = 0; 2983b0f4066SDimitry Andric 2993b0f4066SDimitry Andric // Loop over basic blocks where li is live. 3007d523365SDimitry Andric MachineFunction::const_iterator MFI = 3017d523365SDimitry Andric LIS.getMBBFromIndex(LVI->start)->getIterator(); 3027d523365SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(&*MFI); 3033b0f4066SDimitry Andric for (;;) { 3043b0f4066SDimitry Andric ++Count; 3053b0f4066SDimitry Andric LVI = li->advanceTo(LVI, Stop); 3063b0f4066SDimitry Andric if (LVI == LVE) 3073b0f4066SDimitry Andric return Count; 3083b0f4066SDimitry Andric do { 3093b0f4066SDimitry Andric ++MFI; 3107d523365SDimitry Andric Stop = LIS.getMBBEndIdx(&*MFI); 3113b0f4066SDimitry Andric } while (Stop <= LVI->start); 3123b0f4066SDimitry Andric } 313e580952dSDimitry Andric } 314e580952dSDimitry Andric 315dd6029ffSDimitry Andric bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const { 316dd6029ffSDimitry Andric unsigned OrigReg = VRM.getOriginal(CurLI->reg); 317dd6029ffSDimitry Andric const LiveInterval &Orig = LIS.getInterval(OrigReg); 318dd6029ffSDimitry Andric assert(!Orig.empty() && "Splitting empty interval?"); 319dd6029ffSDimitry Andric LiveInterval::const_iterator I = Orig.find(Idx); 320dd6029ffSDimitry Andric 321dd6029ffSDimitry Andric // Range containing Idx should begin at Idx. 322dd6029ffSDimitry Andric if (I != Orig.end() && I->start <= Idx) 323dd6029ffSDimitry Andric return I->start == Idx; 324dd6029ffSDimitry Andric 325dd6029ffSDimitry Andric // Range does not contain Idx, previous must end at Idx. 326dd6029ffSDimitry Andric return I != Orig.begin() && (--I)->end == Idx; 327dd6029ffSDimitry Andric } 328dd6029ffSDimitry Andric 329e580952dSDimitry Andric void SplitAnalysis::analyze(const LiveInterval *li) { 330e580952dSDimitry Andric clear(); 3312754fe60SDimitry Andric CurLI = li; 332e580952dSDimitry Andric analyzeUses(); 333e580952dSDimitry Andric } 334e580952dSDimitry Andric 335e580952dSDimitry Andric 336e580952dSDimitry Andric //===----------------------------------------------------------------------===// 3373b0f4066SDimitry Andric // Split Editor 338e580952dSDimitry Andric //===----------------------------------------------------------------------===// 339e580952dSDimitry Andric 3403b0f4066SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 3413ca95b02SDimitry Andric SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa, 3423ca95b02SDimitry Andric LiveIntervals &lis, VirtRegMap &vrm, 343f785676fSDimitry Andric MachineDominatorTree &mdt, 344f785676fSDimitry Andric MachineBlockFrequencyInfo &mbfi) 3453ca95b02SDimitry Andric : SA(sa), AA(aa), LIS(lis), VRM(vrm), 3463ca95b02SDimitry Andric MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt), 3473ca95b02SDimitry Andric TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()), 34839d628a0SDimitry Andric TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()), 34939d628a0SDimitry Andric MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition), 35039d628a0SDimitry Andric RegAssign(Allocator) {} 351e580952dSDimitry Andric 3526122f3e6SDimitry Andric void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) { 3536122f3e6SDimitry Andric Edit = &LRE; 3546122f3e6SDimitry Andric SpillMode = SM; 3553b0f4066SDimitry Andric OpenIdx = 0; 3563b0f4066SDimitry Andric RegAssign.clear(); 3572754fe60SDimitry Andric Values.clear(); 3583b0f4066SDimitry Andric 3596122f3e6SDimitry Andric // Reset the LiveRangeCalc instances needed for this spill mode. 3607ae0e2c9SDimitry Andric LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 3617ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator()); 3626122f3e6SDimitry Andric if (SpillMode) 3637ae0e2c9SDimitry Andric LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT, 3647ae0e2c9SDimitry Andric &LIS.getVNInfoAllocator()); 3653b0f4066SDimitry Andric 3663b0f4066SDimitry Andric // We don't need an AliasAnalysis since we will only be performing 3673b0f4066SDimitry Andric // cheap-as-a-copy remats anyway. 36891bc56edSDimitry Andric Edit->anyRematerializable(nullptr); 3692754fe60SDimitry Andric } 3702754fe60SDimitry Andric 3713861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3723ca95b02SDimitry Andric LLVM_DUMP_METHOD void SplitEditor::dump() const { 3733b0f4066SDimitry Andric if (RegAssign.empty()) { 3743b0f4066SDimitry Andric dbgs() << " empty\n"; 3753b0f4066SDimitry Andric return; 3762754fe60SDimitry Andric } 3772754fe60SDimitry Andric 3783b0f4066SDimitry Andric for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I) 3793b0f4066SDimitry Andric dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value(); 3803b0f4066SDimitry Andric dbgs() << '\n'; 3813b0f4066SDimitry Andric } 3823861d79fSDimitry Andric #endif 3833b0f4066SDimitry Andric 3843b0f4066SDimitry Andric VNInfo *SplitEditor::defValue(unsigned RegIdx, 3853b0f4066SDimitry Andric const VNInfo *ParentVNI, 3863b0f4066SDimitry Andric SlotIndex Idx) { 387e580952dSDimitry Andric assert(ParentVNI && "Mapping NULL value"); 388e580952dSDimitry Andric assert(Idx.isValid() && "Invalid SlotIndex"); 3893b0f4066SDimitry Andric assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI"); 390f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 3912754fe60SDimitry Andric 3922754fe60SDimitry Andric // Create a new value. 393dff0c46cSDimitry Andric VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator()); 3942754fe60SDimitry Andric 395e580952dSDimitry Andric // Use insert for lookup, so we can add missing values with a second lookup. 396e580952dSDimitry Andric std::pair<ValueMap::iterator, bool> InsP = 3976122f3e6SDimitry Andric Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), 3986122f3e6SDimitry Andric ValueForcePair(VNI, false))); 3992754fe60SDimitry Andric 4003b0f4066SDimitry Andric // This was the first time (RegIdx, ParentVNI) was mapped. 4013b0f4066SDimitry Andric // Keep it as a simple def without any liveness. 4023b0f4066SDimitry Andric if (InsP.second) 4033b0f4066SDimitry Andric return VNI; 4043b0f4066SDimitry Andric 4053b0f4066SDimitry Andric // If the previous value was a simple mapping, add liveness for it now. 4066122f3e6SDimitry Andric if (VNInfo *OldVNI = InsP.first->second.getPointer()) { 4073b0f4066SDimitry Andric SlotIndex Def = OldVNI->def; 408f785676fSDimitry Andric LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI)); 4096122f3e6SDimitry Andric // No longer a simple mapping. Switch to a complex, non-forced mapping. 4106122f3e6SDimitry Andric InsP.first->second = ValueForcePair(); 4113b0f4066SDimitry Andric } 4123b0f4066SDimitry Andric 4133b0f4066SDimitry Andric // This is a complex mapping, add liveness for VNI 4143b0f4066SDimitry Andric SlotIndex Def = VNI->def; 415f785676fSDimitry Andric LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI)); 4162754fe60SDimitry Andric 4172754fe60SDimitry Andric return VNI; 4182754fe60SDimitry Andric } 4192754fe60SDimitry Andric 4206122f3e6SDimitry Andric void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) { 4212754fe60SDimitry Andric assert(ParentVNI && "Mapping NULL value"); 4226122f3e6SDimitry Andric ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)]; 4236122f3e6SDimitry Andric VNInfo *VNI = VFP.getPointer(); 4243b0f4066SDimitry Andric 4256122f3e6SDimitry Andric // ParentVNI was either unmapped or already complex mapped. Either way, just 4266122f3e6SDimitry Andric // set the force bit. 4276122f3e6SDimitry Andric if (!VNI) { 4286122f3e6SDimitry Andric VFP.setInt(true); 4293b0f4066SDimitry Andric return; 4306122f3e6SDimitry Andric } 4313b0f4066SDimitry Andric 4323b0f4066SDimitry Andric // This was previously a single mapping. Make sure the old def is represented 4333b0f4066SDimitry Andric // by a trivial live range. 4343b0f4066SDimitry Andric SlotIndex Def = VNI->def; 435f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 436f785676fSDimitry Andric LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI)); 4376122f3e6SDimitry Andric // Mark as complex mapped, forced. 43891bc56edSDimitry Andric VFP = ValueForcePair(nullptr, true); 439e580952dSDimitry Andric } 440e580952dSDimitry Andric 4412754fe60SDimitry Andric VNInfo *SplitEditor::defFromParent(unsigned RegIdx, 4422754fe60SDimitry Andric VNInfo *ParentVNI, 4432754fe60SDimitry Andric SlotIndex UseIdx, 444e580952dSDimitry Andric MachineBasicBlock &MBB, 445e580952dSDimitry Andric MachineBasicBlock::iterator I) { 44691bc56edSDimitry Andric MachineInstr *CopyMI = nullptr; 4472754fe60SDimitry Andric SlotIndex Def; 448f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 4493b0f4066SDimitry Andric 4503b0f4066SDimitry Andric // We may be trying to avoid interference that ends at a deleted instruction, 4513b0f4066SDimitry Andric // so always begin RegIdx 0 early and all others late. 4523b0f4066SDimitry Andric bool Late = RegIdx != 0; 4532754fe60SDimitry Andric 4542754fe60SDimitry Andric // Attempt cheap-as-a-copy rematerialization. 4553ca95b02SDimitry Andric unsigned Original = VRM.getOriginal(Edit->get(RegIdx)); 4563ca95b02SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original); 4573ca95b02SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx); 4582754fe60SDimitry Andric LiveRangeEdit::Remat RM(ParentVNI); 4593ca95b02SDimitry Andric RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def); 4603ca95b02SDimitry Andric 4613ca95b02SDimitry Andric if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) { 462dff0c46cSDimitry Andric Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late); 463bd5abe19SDimitry Andric ++NumRemats; 4642754fe60SDimitry Andric } else { 4652754fe60SDimitry Andric // Can't remat, just insert a copy from parent. 4662754fe60SDimitry Andric CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg) 4673b0f4066SDimitry Andric .addReg(Edit->getReg()); 4683ca95b02SDimitry Andric Def = LIS.getSlotIndexes() 4693ca95b02SDimitry Andric ->insertMachineInstrInMaps(*CopyMI, Late) 470dff0c46cSDimitry Andric .getRegSlot(); 471bd5abe19SDimitry Andric ++NumCopies; 4722754fe60SDimitry Andric } 4732754fe60SDimitry Andric 4742754fe60SDimitry Andric // Define the value in Reg. 475dff0c46cSDimitry Andric return defValue(RegIdx, ParentVNI, Def); 476e580952dSDimitry Andric } 477e580952dSDimitry Andric 478e580952dSDimitry Andric /// Create a new virtual register and live interval. 4793b0f4066SDimitry Andric unsigned SplitEditor::openIntv() { 4802754fe60SDimitry Andric // Create the complement as index 0. 4813b0f4066SDimitry Andric if (Edit->empty()) 482f785676fSDimitry Andric Edit->createEmptyInterval(); 483e580952dSDimitry Andric 4842754fe60SDimitry Andric // Create the open interval. 4853b0f4066SDimitry Andric OpenIdx = Edit->size(); 486f785676fSDimitry Andric Edit->createEmptyInterval(); 4873b0f4066SDimitry Andric return OpenIdx; 4883b0f4066SDimitry Andric } 4893b0f4066SDimitry Andric 4903b0f4066SDimitry Andric void SplitEditor::selectIntv(unsigned Idx) { 4913b0f4066SDimitry Andric assert(Idx != 0 && "Cannot select the complement interval"); 4923b0f4066SDimitry Andric assert(Idx < Edit->size() && "Can only select previously opened interval"); 49317a519f9SDimitry Andric DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n'); 4943b0f4066SDimitry Andric OpenIdx = Idx; 4952754fe60SDimitry Andric } 496e580952dSDimitry Andric 4972754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) { 4982754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvBefore"); 4992754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvBefore " << Idx); 5002754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 5013b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 5022754fe60SDimitry Andric if (!ParentVNI) { 5032754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5042754fe60SDimitry Andric return Idx; 5052754fe60SDimitry Andric } 5062754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 5072754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 508e580952dSDimitry Andric assert(MI && "enterIntvBefore called with invalid index"); 509e580952dSDimitry Andric 5102754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI); 5112754fe60SDimitry Andric return VNI->def; 512e580952dSDimitry Andric } 513e580952dSDimitry Andric 51417a519f9SDimitry Andric SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) { 51517a519f9SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAfter"); 51617a519f9SDimitry Andric DEBUG(dbgs() << " enterIntvAfter " << Idx); 51717a519f9SDimitry Andric Idx = Idx.getBoundaryIndex(); 51817a519f9SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 51917a519f9SDimitry Andric if (!ParentVNI) { 52017a519f9SDimitry Andric DEBUG(dbgs() << ": not live\n"); 52117a519f9SDimitry Andric return Idx; 52217a519f9SDimitry Andric } 52317a519f9SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 52417a519f9SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 52517a519f9SDimitry Andric assert(MI && "enterIntvAfter called with invalid index"); 52617a519f9SDimitry Andric 52717a519f9SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), 52891bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI))); 52917a519f9SDimitry Andric return VNI->def; 53017a519f9SDimitry Andric } 53117a519f9SDimitry Andric 5322754fe60SDimitry Andric SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) { 5332754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before enterIntvAtEnd"); 5342754fe60SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(&MBB); 5352754fe60SDimitry Andric SlotIndex Last = End.getPrevSlot(); 5362754fe60SDimitry Andric DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last); 5373b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last); 5382754fe60SDimitry Andric if (!ParentVNI) { 5392754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5402754fe60SDimitry Andric return End; 5412754fe60SDimitry Andric } 5422754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id); 5432754fe60SDimitry Andric VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB, 544dff0c46cSDimitry Andric SA.getLastSplitPointIter(&MBB)); 5452754fe60SDimitry Andric RegAssign.insert(VNI->def, End, OpenIdx); 5462754fe60SDimitry Andric DEBUG(dump()); 5472754fe60SDimitry Andric return VNI->def; 548e580952dSDimitry Andric } 549e580952dSDimitry Andric 5502754fe60SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI. 551e580952dSDimitry Andric void SplitEditor::useIntv(const MachineBasicBlock &MBB) { 5522754fe60SDimitry Andric useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB)); 553e580952dSDimitry Andric } 554e580952dSDimitry Andric 555e580952dSDimitry Andric void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) { 5562754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before useIntv"); 5572754fe60SDimitry Andric DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):"); 5582754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 5592754fe60SDimitry Andric DEBUG(dump()); 560e580952dSDimitry Andric } 561e580952dSDimitry Andric 5622754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) { 5632754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAfter"); 5642754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAfter " << Idx); 5652754fe60SDimitry Andric 5662754fe60SDimitry Andric // The interval must be live beyond the instruction at Idx. 5676122f3e6SDimitry Andric SlotIndex Boundary = Idx.getBoundaryIndex(); 5686122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary); 5692754fe60SDimitry Andric if (!ParentVNI) { 5702754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 5716122f3e6SDimitry Andric return Boundary.getNextSlot(); 5722754fe60SDimitry Andric } 5732754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 5746122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Boundary); 5752754fe60SDimitry Andric assert(MI && "No instruction at index"); 5766122f3e6SDimitry Andric 5776122f3e6SDimitry Andric // In spill mode, make live ranges as short as possible by inserting the copy 5786122f3e6SDimitry Andric // before MI. This is only possible if that instruction doesn't redefine the 5796122f3e6SDimitry Andric // value. The inserted COPY is not a kill, and we don't need to recompute 5806122f3e6SDimitry Andric // the source live range. The spiller also won't try to hoist this copy. 5816122f3e6SDimitry Andric if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) && 5826122f3e6SDimitry Andric MI->readsVirtualRegister(Edit->getReg())) { 5836122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 5846122f3e6SDimitry Andric defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 5856122f3e6SDimitry Andric return Idx; 5866122f3e6SDimitry Andric } 5876122f3e6SDimitry Andric 5886122f3e6SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(), 58991bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI))); 5902754fe60SDimitry Andric return VNI->def; 591e580952dSDimitry Andric } 592e580952dSDimitry Andric 5932754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) { 5942754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvBefore"); 5952754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvBefore " << Idx); 596e580952dSDimitry Andric 5972754fe60SDimitry Andric // The interval must be live into the instruction at Idx. 5986122f3e6SDimitry Andric Idx = Idx.getBaseIndex(); 5993b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx); 6002754fe60SDimitry Andric if (!ParentVNI) { 6012754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6022754fe60SDimitry Andric return Idx.getNextSlot(); 6032754fe60SDimitry Andric } 6042754fe60SDimitry Andric DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n'); 6052754fe60SDimitry Andric 6062754fe60SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Idx); 6072754fe60SDimitry Andric assert(MI && "No instruction at index"); 6082754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI); 6092754fe60SDimitry Andric return VNI->def; 610e580952dSDimitry Andric } 611e580952dSDimitry Andric 6122754fe60SDimitry Andric SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) { 6132754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before leaveIntvAtTop"); 6142754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(&MBB); 6152754fe60SDimitry Andric DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start); 6162754fe60SDimitry Andric 6173b0f4066SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 6182754fe60SDimitry Andric if (!ParentVNI) { 6192754fe60SDimitry Andric DEBUG(dbgs() << ": not live\n"); 6202754fe60SDimitry Andric return Start; 621e580952dSDimitry Andric } 622e580952dSDimitry Andric 6232754fe60SDimitry Andric VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB, 6242754fe60SDimitry Andric MBB.SkipPHIsAndLabels(MBB.begin())); 6252754fe60SDimitry Andric RegAssign.insert(Start, VNI->def, OpenIdx); 6262754fe60SDimitry Andric DEBUG(dump()); 6272754fe60SDimitry Andric return VNI->def; 628e580952dSDimitry Andric } 629e580952dSDimitry Andric 6302754fe60SDimitry Andric void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) { 6312754fe60SDimitry Andric assert(OpenIdx && "openIntv not called before overlapIntv"); 6323b0f4066SDimitry Andric const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start); 633dff0c46cSDimitry Andric assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) && 6342754fe60SDimitry Andric "Parent changes value in extended range"); 6352754fe60SDimitry Andric assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) && 6362754fe60SDimitry Andric "Range cannot span basic blocks"); 637e580952dSDimitry Andric 6386122f3e6SDimitry Andric // The complement interval will be extended as needed by LRCalc.extend(). 6393b0f4066SDimitry Andric if (ParentVNI) 6406122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 6412754fe60SDimitry Andric DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):"); 6422754fe60SDimitry Andric RegAssign.insert(Start, End, OpenIdx); 6432754fe60SDimitry Andric DEBUG(dump()); 644e580952dSDimitry Andric } 645e580952dSDimitry Andric 6466122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 6476122f3e6SDimitry Andric // Spill modes 6486122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 6496122f3e6SDimitry Andric 6506122f3e6SDimitry Andric void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) { 651f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 6526122f3e6SDimitry Andric DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n"); 6536122f3e6SDimitry Andric RegAssignMap::iterator AssignI; 6546122f3e6SDimitry Andric AssignI.setMap(RegAssign); 6556122f3e6SDimitry Andric 6566122f3e6SDimitry Andric for (unsigned i = 0, e = Copies.size(); i != e; ++i) { 657ff0cc061SDimitry Andric SlotIndex Def = Copies[i]->def; 6586122f3e6SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(Def); 6596122f3e6SDimitry Andric assert(MI && "No instruction for back-copy"); 6606122f3e6SDimitry Andric 6616122f3e6SDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 6626122f3e6SDimitry Andric MachineBasicBlock::iterator MBBI(MI); 6636122f3e6SDimitry Andric bool AtBegin; 6646122f3e6SDimitry Andric do AtBegin = MBBI == MBB->begin(); 6656122f3e6SDimitry Andric while (!AtBegin && (--MBBI)->isDebugValue()); 6666122f3e6SDimitry Andric 6676122f3e6SDimitry Andric DEBUG(dbgs() << "Removing " << Def << '\t' << *MI); 668ff0cc061SDimitry Andric LIS.removeVRegDefAt(*LI, Def); 6693ca95b02SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI); 6706122f3e6SDimitry Andric MI->eraseFromParent(); 6716122f3e6SDimitry Andric 672ff0cc061SDimitry Andric // Adjust RegAssign if a register assignment is killed at Def. We want to 673ff0cc061SDimitry Andric // avoid calculating the live range of the source register if possible. 6747ae0e2c9SDimitry Andric AssignI.find(Def.getPrevSlot()); 6756122f3e6SDimitry Andric if (!AssignI.valid() || AssignI.start() >= Def) 6766122f3e6SDimitry Andric continue; 6776122f3e6SDimitry Andric // If MI doesn't kill the assigned register, just leave it. 6786122f3e6SDimitry Andric if (AssignI.stop() != Def) 6796122f3e6SDimitry Andric continue; 6806122f3e6SDimitry Andric unsigned RegIdx = AssignI.value(); 6816122f3e6SDimitry Andric if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) { 6826122f3e6SDimitry Andric DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n'); 6836122f3e6SDimitry Andric forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def)); 6846122f3e6SDimitry Andric } else { 6853ca95b02SDimitry Andric SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot(); 6866122f3e6SDimitry Andric DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI); 6876122f3e6SDimitry Andric AssignI.setStop(Kill); 6886122f3e6SDimitry Andric } 6896122f3e6SDimitry Andric } 6906122f3e6SDimitry Andric } 6916122f3e6SDimitry Andric 6926122f3e6SDimitry Andric MachineBasicBlock* 6936122f3e6SDimitry Andric SplitEditor::findShallowDominator(MachineBasicBlock *MBB, 6946122f3e6SDimitry Andric MachineBasicBlock *DefMBB) { 6956122f3e6SDimitry Andric if (MBB == DefMBB) 6966122f3e6SDimitry Andric return MBB; 6976122f3e6SDimitry Andric assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def."); 6986122f3e6SDimitry Andric 6996122f3e6SDimitry Andric const MachineLoopInfo &Loops = SA.Loops; 7006122f3e6SDimitry Andric const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB); 7016122f3e6SDimitry Andric MachineDomTreeNode *DefDomNode = MDT[DefMBB]; 7026122f3e6SDimitry Andric 7036122f3e6SDimitry Andric // Best candidate so far. 7046122f3e6SDimitry Andric MachineBasicBlock *BestMBB = MBB; 7056122f3e6SDimitry Andric unsigned BestDepth = UINT_MAX; 7066122f3e6SDimitry Andric 7076122f3e6SDimitry Andric for (;;) { 7086122f3e6SDimitry Andric const MachineLoop *Loop = Loops.getLoopFor(MBB); 7096122f3e6SDimitry Andric 7106122f3e6SDimitry Andric // MBB isn't in a loop, it doesn't get any better. All dominators have a 7116122f3e6SDimitry Andric // higher frequency by definition. 7126122f3e6SDimitry Andric if (!Loop) { 7136122f3e6SDimitry Andric DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 7146122f3e6SDimitry Andric << MBB->getNumber() << " at depth 0\n"); 7156122f3e6SDimitry Andric return MBB; 7166122f3e6SDimitry Andric } 7176122f3e6SDimitry Andric 7186122f3e6SDimitry Andric // We'll never be able to exit the DefLoop. 7196122f3e6SDimitry Andric if (Loop == DefLoop) { 7206122f3e6SDimitry Andric DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 7216122f3e6SDimitry Andric << MBB->getNumber() << " in the same loop\n"); 7226122f3e6SDimitry Andric return MBB; 7236122f3e6SDimitry Andric } 7246122f3e6SDimitry Andric 7256122f3e6SDimitry Andric // Least busy dominator seen so far. 7266122f3e6SDimitry Andric unsigned Depth = Loop->getLoopDepth(); 7276122f3e6SDimitry Andric if (Depth < BestDepth) { 7286122f3e6SDimitry Andric BestMBB = MBB; 7296122f3e6SDimitry Andric BestDepth = Depth; 7306122f3e6SDimitry Andric DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#" 7316122f3e6SDimitry Andric << MBB->getNumber() << " at depth " << Depth << '\n'); 7326122f3e6SDimitry Andric } 7336122f3e6SDimitry Andric 7346122f3e6SDimitry Andric // Leave loop by going to the immediate dominator of the loop header. 7356122f3e6SDimitry Andric // This is a bigger stride than simply walking up the dominator tree. 7366122f3e6SDimitry Andric MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom(); 7376122f3e6SDimitry Andric 7386122f3e6SDimitry Andric // Too far up the dominator tree? 7396122f3e6SDimitry Andric if (!IDom || !MDT.dominates(DefDomNode, IDom)) 7406122f3e6SDimitry Andric return BestMBB; 7416122f3e6SDimitry Andric 7426122f3e6SDimitry Andric MBB = IDom->getBlock(); 7436122f3e6SDimitry Andric } 7446122f3e6SDimitry Andric } 7456122f3e6SDimitry Andric 7463ca95b02SDimitry Andric void SplitEditor::computeRedundantBackCopies( 7473ca95b02SDimitry Andric DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) { 7483ca95b02SDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 7493ca95b02SDimitry Andric LiveInterval *Parent = &Edit->getParent(); 7503ca95b02SDimitry Andric SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums()); 7513ca95b02SDimitry Andric SmallPtrSet<VNInfo *, 8> DominatedVNIs; 7523ca95b02SDimitry Andric 7533ca95b02SDimitry Andric // Aggregate VNIs having the same value as ParentVNI. 7543ca95b02SDimitry Andric for (VNInfo *VNI : LI->valnos) { 7553ca95b02SDimitry Andric if (VNI->isUnused()) 7563ca95b02SDimitry Andric continue; 7573ca95b02SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 7583ca95b02SDimitry Andric EqualVNs[ParentVNI->id].insert(VNI); 7593ca95b02SDimitry Andric } 7603ca95b02SDimitry Andric 7613ca95b02SDimitry Andric // For VNI aggregation of each ParentVNI, collect dominated, i.e., 7623ca95b02SDimitry Andric // redundant VNIs to BackCopies. 7633ca95b02SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 7643ca95b02SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i); 7653ca95b02SDimitry Andric if (!NotToHoistSet.count(ParentVNI->id)) 7663ca95b02SDimitry Andric continue; 7673ca95b02SDimitry Andric SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin(); 7683ca95b02SDimitry Andric SmallPtrSetIterator<VNInfo *> It2 = It1; 7693ca95b02SDimitry Andric for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) { 7703ca95b02SDimitry Andric It2 = It1; 7713ca95b02SDimitry Andric for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) { 7723ca95b02SDimitry Andric if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2)) 7733ca95b02SDimitry Andric continue; 7743ca95b02SDimitry Andric 7753ca95b02SDimitry Andric MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def); 7763ca95b02SDimitry Andric MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def); 7773ca95b02SDimitry Andric if (MBB1 == MBB2) { 7783ca95b02SDimitry Andric DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1)); 7793ca95b02SDimitry Andric } else if (MDT.dominates(MBB1, MBB2)) { 7803ca95b02SDimitry Andric DominatedVNIs.insert(*It2); 7813ca95b02SDimitry Andric } else if (MDT.dominates(MBB2, MBB1)) { 7823ca95b02SDimitry Andric DominatedVNIs.insert(*It1); 7833ca95b02SDimitry Andric } 7843ca95b02SDimitry Andric } 7853ca95b02SDimitry Andric } 7863ca95b02SDimitry Andric if (!DominatedVNIs.empty()) { 7873ca95b02SDimitry Andric forceRecompute(0, ParentVNI); 7883ca95b02SDimitry Andric for (auto VNI : DominatedVNIs) { 7893ca95b02SDimitry Andric BackCopies.push_back(VNI); 7903ca95b02SDimitry Andric } 7913ca95b02SDimitry Andric DominatedVNIs.clear(); 7923ca95b02SDimitry Andric } 7933ca95b02SDimitry Andric } 7943ca95b02SDimitry Andric } 7953ca95b02SDimitry Andric 7963ca95b02SDimitry Andric /// For SM_Size mode, find a common dominator for all the back-copies for 7973ca95b02SDimitry Andric /// the same ParentVNI and hoist the backcopies to the dominator BB. 7983ca95b02SDimitry Andric /// For SM_Speed mode, if the common dominator is hot and it is not beneficial 7993ca95b02SDimitry Andric /// to do the hoisting, simply remove the dominated backcopies for the same 8003ca95b02SDimitry Andric /// ParentVNI. 8013ca95b02SDimitry Andric void SplitEditor::hoistCopies() { 8026122f3e6SDimitry Andric // Get the complement interval, always RegIdx 0. 803f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(0)); 8046122f3e6SDimitry Andric LiveInterval *Parent = &Edit->getParent(); 8056122f3e6SDimitry Andric 8066122f3e6SDimitry Andric // Track the nearest common dominator for all back-copies for each ParentVNI, 8076122f3e6SDimitry Andric // indexed by ParentVNI->id. 8086122f3e6SDimitry Andric typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair; 8096122f3e6SDimitry Andric SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums()); 8103ca95b02SDimitry Andric // The total cost of all the back-copies for each ParentVNI. 8113ca95b02SDimitry Andric SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums()); 8123ca95b02SDimitry Andric // The ParentVNI->id set for which hoisting back-copies are not beneficial 8133ca95b02SDimitry Andric // for Speed. 8143ca95b02SDimitry Andric DenseSet<unsigned> NotToHoistSet; 8156122f3e6SDimitry Andric 8166122f3e6SDimitry Andric // Find the nearest common dominator for parent values with multiple 8176122f3e6SDimitry Andric // back-copies. If a single back-copy dominates, put it in DomPair.second. 81839d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) { 8197ae0e2c9SDimitry Andric if (VNI->isUnused()) 8207ae0e2c9SDimitry Andric continue; 8216122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 8226122f3e6SDimitry Andric assert(ParentVNI && "Parent not live at complement def"); 8236122f3e6SDimitry Andric 8246122f3e6SDimitry Andric // Don't hoist remats. The complement is probably going to disappear 8256122f3e6SDimitry Andric // completely anyway. 8266122f3e6SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 8276122f3e6SDimitry Andric continue; 8286122f3e6SDimitry Andric 8296122f3e6SDimitry Andric MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def); 8303ca95b02SDimitry Andric 8316122f3e6SDimitry Andric DomPair &Dom = NearestDom[ParentVNI->id]; 8326122f3e6SDimitry Andric 8336122f3e6SDimitry Andric // Keep directly defined parent values. This is either a PHI or an 8346122f3e6SDimitry Andric // instruction in the complement range. All other copies of ParentVNI 8356122f3e6SDimitry Andric // should be eliminated. 8366122f3e6SDimitry Andric if (VNI->def == ParentVNI->def) { 8376122f3e6SDimitry Andric DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n'); 8386122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 8396122f3e6SDimitry Andric continue; 8406122f3e6SDimitry Andric } 8416122f3e6SDimitry Andric // Skip the singly mapped values. There is nothing to gain from hoisting a 8426122f3e6SDimitry Andric // single back-copy. 8436122f3e6SDimitry Andric if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) { 8446122f3e6SDimitry Andric DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n'); 8456122f3e6SDimitry Andric continue; 8466122f3e6SDimitry Andric } 8476122f3e6SDimitry Andric 8486122f3e6SDimitry Andric if (!Dom.first) { 8496122f3e6SDimitry Andric // First time we see ParentVNI. VNI dominates itself. 8506122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 8516122f3e6SDimitry Andric } else if (Dom.first == ValMBB) { 8526122f3e6SDimitry Andric // Two defs in the same block. Pick the earlier def. 8536122f3e6SDimitry Andric if (!Dom.second.isValid() || VNI->def < Dom.second) 8546122f3e6SDimitry Andric Dom.second = VNI->def; 8556122f3e6SDimitry Andric } else { 8566122f3e6SDimitry Andric // Different basic blocks. Check if one dominates. 8576122f3e6SDimitry Andric MachineBasicBlock *Near = 8586122f3e6SDimitry Andric MDT.findNearestCommonDominator(Dom.first, ValMBB); 8596122f3e6SDimitry Andric if (Near == ValMBB) 8606122f3e6SDimitry Andric // Def ValMBB dominates. 8616122f3e6SDimitry Andric Dom = DomPair(ValMBB, VNI->def); 8626122f3e6SDimitry Andric else if (Near != Dom.first) 8636122f3e6SDimitry Andric // None dominate. Hoist to common dominator, need new def. 8646122f3e6SDimitry Andric Dom = DomPair(Near, SlotIndex()); 8653ca95b02SDimitry Andric Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB); 8666122f3e6SDimitry Andric } 8676122f3e6SDimitry Andric 8686122f3e6SDimitry Andric DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def 8696122f3e6SDimitry Andric << " for parent " << ParentVNI->id << '@' << ParentVNI->def 8706122f3e6SDimitry Andric << " hoist to BB#" << Dom.first->getNumber() << ' ' 8716122f3e6SDimitry Andric << Dom.second << '\n'); 8726122f3e6SDimitry Andric } 8736122f3e6SDimitry Andric 8746122f3e6SDimitry Andric // Insert the hoisted copies. 8756122f3e6SDimitry Andric for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) { 8766122f3e6SDimitry Andric DomPair &Dom = NearestDom[i]; 8776122f3e6SDimitry Andric if (!Dom.first || Dom.second.isValid()) 8786122f3e6SDimitry Andric continue; 8796122f3e6SDimitry Andric // This value needs a hoisted copy inserted at the end of Dom.first. 8806122f3e6SDimitry Andric VNInfo *ParentVNI = Parent->getValNumInfo(i); 8816122f3e6SDimitry Andric MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def); 8826122f3e6SDimitry Andric // Get a less loopy dominator than Dom.first. 8836122f3e6SDimitry Andric Dom.first = findShallowDominator(Dom.first, DefMBB); 8843ca95b02SDimitry Andric if (SpillMode == SM_Speed && 8853ca95b02SDimitry Andric MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) { 8863ca95b02SDimitry Andric NotToHoistSet.insert(ParentVNI->id); 8873ca95b02SDimitry Andric continue; 8883ca95b02SDimitry Andric } 8896122f3e6SDimitry Andric SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot(); 8906122f3e6SDimitry Andric Dom.second = 8916122f3e6SDimitry Andric defFromParent(0, ParentVNI, Last, *Dom.first, 892dff0c46cSDimitry Andric SA.getLastSplitPointIter(Dom.first))->def; 8936122f3e6SDimitry Andric } 8946122f3e6SDimitry Andric 8956122f3e6SDimitry Andric // Remove redundant back-copies that are now known to be dominated by another 8966122f3e6SDimitry Andric // def with the same value. 8976122f3e6SDimitry Andric SmallVector<VNInfo*, 8> BackCopies; 89839d628a0SDimitry Andric for (VNInfo *VNI : LI->valnos) { 8997ae0e2c9SDimitry Andric if (VNI->isUnused()) 9007ae0e2c9SDimitry Andric continue; 9016122f3e6SDimitry Andric VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def); 9026122f3e6SDimitry Andric const DomPair &Dom = NearestDom[ParentVNI->id]; 9033ca95b02SDimitry Andric if (!Dom.first || Dom.second == VNI->def || 9043ca95b02SDimitry Andric NotToHoistSet.count(ParentVNI->id)) 9056122f3e6SDimitry Andric continue; 9066122f3e6SDimitry Andric BackCopies.push_back(VNI); 9076122f3e6SDimitry Andric forceRecompute(0, ParentVNI); 9086122f3e6SDimitry Andric } 9093ca95b02SDimitry Andric 9103ca95b02SDimitry Andric // If it is not beneficial to hoist all the BackCopies, simply remove 9113ca95b02SDimitry Andric // redundant BackCopies in speed mode. 9123ca95b02SDimitry Andric if (SpillMode == SM_Speed && !NotToHoistSet.empty()) 9133ca95b02SDimitry Andric computeRedundantBackCopies(NotToHoistSet, BackCopies); 9143ca95b02SDimitry Andric 9156122f3e6SDimitry Andric removeBackCopies(BackCopies); 9166122f3e6SDimitry Andric } 9176122f3e6SDimitry Andric 9186122f3e6SDimitry Andric 9193b0f4066SDimitry Andric /// transferValues - Transfer all possible values to the new live ranges. 9206122f3e6SDimitry Andric /// Values that were rematerialized are left alone, they need LRCalc.extend(). 9213b0f4066SDimitry Andric bool SplitEditor::transferValues() { 9223b0f4066SDimitry Andric bool Skipped = false; 9233b0f4066SDimitry Andric RegAssignMap::const_iterator AssignI = RegAssign.begin(); 92439d628a0SDimitry Andric for (const LiveRange::Segment &S : Edit->getParent()) { 92539d628a0SDimitry Andric DEBUG(dbgs() << " blit " << S << ':'); 92639d628a0SDimitry Andric VNInfo *ParentVNI = S.valno; 9273b0f4066SDimitry Andric // RegAssign has holes where RegIdx 0 should be used. 92839d628a0SDimitry Andric SlotIndex Start = S.start; 9293b0f4066SDimitry Andric AssignI.advanceTo(Start); 9303b0f4066SDimitry Andric do { 9313b0f4066SDimitry Andric unsigned RegIdx; 93239d628a0SDimitry Andric SlotIndex End = S.end; 9333b0f4066SDimitry Andric if (!AssignI.valid()) { 9343b0f4066SDimitry Andric RegIdx = 0; 9353b0f4066SDimitry Andric } else if (AssignI.start() <= Start) { 9363b0f4066SDimitry Andric RegIdx = AssignI.value(); 9373b0f4066SDimitry Andric if (AssignI.stop() < End) { 9383b0f4066SDimitry Andric End = AssignI.stop(); 9393b0f4066SDimitry Andric ++AssignI; 9403b0f4066SDimitry Andric } 9413b0f4066SDimitry Andric } else { 9423b0f4066SDimitry Andric RegIdx = 0; 9433b0f4066SDimitry Andric End = std::min(End, AssignI.start()); 944e580952dSDimitry Andric } 945e580952dSDimitry Andric 9463b0f4066SDimitry Andric // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI. 9473b0f4066SDimitry Andric DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx); 948f785676fSDimitry Andric LiveRange &LR = LIS.getInterval(Edit->get(RegIdx)); 9493b0f4066SDimitry Andric 9503b0f4066SDimitry Andric // Check for a simply defined value that can be blitted directly. 9516122f3e6SDimitry Andric ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id)); 9526122f3e6SDimitry Andric if (VNInfo *VNI = VFP.getPointer()) { 9533b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id); 954f785676fSDimitry Andric LR.addSegment(LiveInterval::Segment(Start, End, VNI)); 9553b0f4066SDimitry Andric Start = End; 9563b0f4066SDimitry Andric continue; 9573b0f4066SDimitry Andric } 9583b0f4066SDimitry Andric 9596122f3e6SDimitry Andric // Skip values with forced recomputation. 9606122f3e6SDimitry Andric if (VFP.getInt()) { 9616122f3e6SDimitry Andric DEBUG(dbgs() << "(recalc)"); 9623b0f4066SDimitry Andric Skipped = true; 9633b0f4066SDimitry Andric Start = End; 9643b0f4066SDimitry Andric continue; 9653b0f4066SDimitry Andric } 9663b0f4066SDimitry Andric 9676122f3e6SDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 9683b0f4066SDimitry Andric 9693b0f4066SDimitry Andric // This value has multiple defs in RegIdx, but it wasn't rematerialized, 9703b0f4066SDimitry Andric // so the live range is accurate. Add live-in blocks in [Start;End) to the 9713b0f4066SDimitry Andric // LiveInBlocks. 9727d523365SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); 9733b0f4066SDimitry Andric SlotIndex BlockStart, BlockEnd; 9747d523365SDimitry Andric std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB); 9753b0f4066SDimitry Andric 9763b0f4066SDimitry Andric // The first block may be live-in, or it may have its own def. 9773b0f4066SDimitry Andric if (Start != BlockStart) { 978f785676fSDimitry Andric VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End)); 9793b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped value"); 9803b0f4066SDimitry Andric DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber()); 9813b0f4066SDimitry Andric // MBB has its own def. Is it also live-out? 9826122f3e6SDimitry Andric if (BlockEnd <= End) 9837d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, VNI); 9846122f3e6SDimitry Andric 9853b0f4066SDimitry Andric // Skip to the next block for live-in. 9863b0f4066SDimitry Andric ++MBB; 9873b0f4066SDimitry Andric BlockStart = BlockEnd; 9883b0f4066SDimitry Andric } 9893b0f4066SDimitry Andric 9903b0f4066SDimitry Andric // Handle the live-in blocks covered by [Start;End). 9913b0f4066SDimitry Andric assert(Start <= BlockStart && "Expected live-in block"); 9923b0f4066SDimitry Andric while (BlockStart < End) { 9933b0f4066SDimitry Andric DEBUG(dbgs() << ">BB#" << MBB->getNumber()); 9947d523365SDimitry Andric BlockEnd = LIS.getMBBEndIdx(&*MBB); 9953b0f4066SDimitry Andric if (BlockStart == ParentVNI->def) { 9963b0f4066SDimitry Andric // This block has the def of a parent PHI, so it isn't live-in. 9973b0f4066SDimitry Andric assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?"); 998f785676fSDimitry Andric VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End)); 9993b0f4066SDimitry Andric assert(VNI && "Missing def for complex mapped parent PHI"); 10006122f3e6SDimitry Andric if (End >= BlockEnd) 10017d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well. 10023b0f4066SDimitry Andric } else { 10036122f3e6SDimitry Andric // This block needs a live-in value. The last block covered may not 10046122f3e6SDimitry Andric // be live-out. 10053b0f4066SDimitry Andric if (End < BlockEnd) 10067d523365SDimitry Andric LRC.addLiveInBlock(LR, MDT[&*MBB], End); 10073b0f4066SDimitry Andric else { 10086122f3e6SDimitry Andric // Live-through, and we don't know the value. 10097d523365SDimitry Andric LRC.addLiveInBlock(LR, MDT[&*MBB]); 10107d523365SDimitry Andric LRC.setLiveOutValue(&*MBB, nullptr); 10113b0f4066SDimitry Andric } 10123b0f4066SDimitry Andric } 10133b0f4066SDimitry Andric BlockStart = BlockEnd; 10143b0f4066SDimitry Andric ++MBB; 10153b0f4066SDimitry Andric } 10163b0f4066SDimitry Andric Start = End; 101739d628a0SDimitry Andric } while (Start != S.end); 10183b0f4066SDimitry Andric DEBUG(dbgs() << '\n'); 10193b0f4066SDimitry Andric } 10203b0f4066SDimitry Andric 10217ae0e2c9SDimitry Andric LRCalc[0].calculateValues(); 10226122f3e6SDimitry Andric if (SpillMode) 10237ae0e2c9SDimitry Andric LRCalc[1].calculateValues(); 10243b0f4066SDimitry Andric 10253b0f4066SDimitry Andric return Skipped; 10263b0f4066SDimitry Andric } 10273b0f4066SDimitry Andric 10283b0f4066SDimitry Andric void SplitEditor::extendPHIKillRanges() { 10293b0f4066SDimitry Andric // Extend live ranges to be live-out for successor PHI values. 103039d628a0SDimitry Andric for (const VNInfo *PHIVNI : Edit->getParent().valnos) { 10313b0f4066SDimitry Andric if (PHIVNI->isUnused() || !PHIVNI->isPHIDef()) 10323b0f4066SDimitry Andric continue; 10333b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(PHIVNI->def); 1034f785676fSDimitry Andric LiveRange &LR = LIS.getInterval(Edit->get(RegIdx)); 10353ca95b02SDimitry Andric 10363ca95b02SDimitry Andric // Check whether PHI is dead. 10373ca95b02SDimitry Andric const LiveRange::Segment *Segment = LR.getSegmentContaining(PHIVNI->def); 10383ca95b02SDimitry Andric assert(Segment != nullptr && "Missing segment for VNI"); 10393ca95b02SDimitry Andric if (Segment->end == PHIVNI->def.getDeadSlot()) { 10403ca95b02SDimitry Andric // This is a dead PHI. Remove it. 10413ca95b02SDimitry Andric LR.removeSegment(*Segment, true); 10423ca95b02SDimitry Andric continue; 10433ca95b02SDimitry Andric } 10443ca95b02SDimitry Andric 10456122f3e6SDimitry Andric LiveRangeCalc &LRC = getLRCalc(RegIdx); 10463b0f4066SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def); 10473b0f4066SDimitry Andric for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 10483b0f4066SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI) { 10496122f3e6SDimitry Andric SlotIndex End = LIS.getMBBEndIdx(*PI); 10506122f3e6SDimitry Andric SlotIndex LastUse = End.getPrevSlot(); 10513b0f4066SDimitry Andric // The predecessor may not have a live-out value. That is OK, like an 10523b0f4066SDimitry Andric // undef PHI operand. 10536122f3e6SDimitry Andric if (Edit->getParent().liveAt(LastUse)) { 10546122f3e6SDimitry Andric assert(RegAssign.lookup(LastUse) == RegIdx && 10553b0f4066SDimitry Andric "Different register assignment in phi predecessor"); 1056f785676fSDimitry Andric LRC.extend(LR, End); 10573b0f4066SDimitry Andric } 10583b0f4066SDimitry Andric } 10593b0f4066SDimitry Andric } 10603b0f4066SDimitry Andric } 10613b0f4066SDimitry Andric 10623b0f4066SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit->getReg(). 10633b0f4066SDimitry Andric void SplitEditor::rewriteAssigned(bool ExtendRanges) { 10643b0f4066SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()), 10652754fe60SDimitry Andric RE = MRI.reg_end(); RI != RE;) { 106691bc56edSDimitry Andric MachineOperand &MO = *RI; 1067e580952dSDimitry Andric MachineInstr *MI = MO.getParent(); 1068e580952dSDimitry Andric ++RI; 10692754fe60SDimitry Andric // LiveDebugVariables should have handled all DBG_VALUE instructions. 1070e580952dSDimitry Andric if (MI->isDebugValue()) { 1071e580952dSDimitry Andric DEBUG(dbgs() << "Zapping " << *MI); 1072e580952dSDimitry Andric MO.setReg(0); 1073e580952dSDimitry Andric continue; 1074e580952dSDimitry Andric } 10752754fe60SDimitry Andric 10766122f3e6SDimitry Andric // <undef> operands don't really read the register, so it doesn't matter 10776122f3e6SDimitry Andric // which register we choose. When the use operand is tied to a def, we must 10786122f3e6SDimitry Andric // use the same register as the def, so just do that always. 10793ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI); 10806122f3e6SDimitry Andric if (MO.isDef() || MO.isUndef()) 1081dff0c46cSDimitry Andric Idx = Idx.getRegSlot(MO.isEarlyClobber()); 10822754fe60SDimitry Andric 10832754fe60SDimitry Andric // Rewrite to the mapped register at Idx. 10842754fe60SDimitry Andric unsigned RegIdx = RegAssign.lookup(Idx); 1085f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx)); 10866122f3e6SDimitry Andric MO.setReg(LI->reg); 10872754fe60SDimitry Andric DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t' 10882754fe60SDimitry Andric << Idx << ':' << RegIdx << '\t' << *MI); 10892754fe60SDimitry Andric 10903b0f4066SDimitry Andric // Extend liveness to Idx if the instruction reads reg. 10916122f3e6SDimitry Andric if (!ExtendRanges || MO.isUndef()) 10922754fe60SDimitry Andric continue; 10933b0f4066SDimitry Andric 10943b0f4066SDimitry Andric // Skip instructions that don't read Reg. 10953b0f4066SDimitry Andric if (MO.isDef()) { 10963b0f4066SDimitry Andric if (!MO.getSubReg() && !MO.isEarlyClobber()) 10973b0f4066SDimitry Andric continue; 10983b0f4066SDimitry Andric // We may wan't to extend a live range for a partial redef, or for a use 10993b0f4066SDimitry Andric // tied to an early clobber. 11003b0f4066SDimitry Andric Idx = Idx.getPrevSlot(); 11013b0f4066SDimitry Andric if (!Edit->getParent().liveAt(Idx)) 11023b0f4066SDimitry Andric continue; 11033b0f4066SDimitry Andric } else 1104dff0c46cSDimitry Andric Idx = Idx.getRegSlot(true); 11053b0f4066SDimitry Andric 1106f785676fSDimitry Andric getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot()); 1107e580952dSDimitry Andric } 1108e580952dSDimitry Andric } 1109e580952dSDimitry Andric 11103b0f4066SDimitry Andric void SplitEditor::deleteRematVictims() { 11113b0f4066SDimitry Andric SmallVector<MachineInstr*, 8> Dead; 11123b0f4066SDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ 1113f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(*I); 111439d628a0SDimitry Andric for (const LiveRange::Segment &S : LI->segments) { 1115dff0c46cSDimitry Andric // Dead defs end at the dead slot. 111639d628a0SDimitry Andric if (S.end != S.valno->def.getDeadSlot()) 11173b0f4066SDimitry Andric continue; 11183ca95b02SDimitry Andric if (S.valno->isPHIDef()) 11193ca95b02SDimitry Andric continue; 112039d628a0SDimitry Andric MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def); 11213b0f4066SDimitry Andric assert(MI && "Missing instruction for dead def"); 11223b0f4066SDimitry Andric MI->addRegisterDead(LI->reg, &TRI); 11233b0f4066SDimitry Andric 11243b0f4066SDimitry Andric if (!MI->allDefsAreDead()) 11253b0f4066SDimitry Andric continue; 11263b0f4066SDimitry Andric 11273b0f4066SDimitry Andric DEBUG(dbgs() << "All defs dead: " << *MI); 11283b0f4066SDimitry Andric Dead.push_back(MI); 11293b0f4066SDimitry Andric } 11303b0f4066SDimitry Andric } 11313b0f4066SDimitry Andric 11323b0f4066SDimitry Andric if (Dead.empty()) 11333b0f4066SDimitry Andric return; 11343b0f4066SDimitry Andric 11353ca95b02SDimitry Andric Edit->eliminateDeadDefs(Dead, None, &AA); 11363b0f4066SDimitry Andric } 11373b0f4066SDimitry Andric 11383b0f4066SDimitry Andric void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) { 11393b0f4066SDimitry Andric ++NumFinished; 11402754fe60SDimitry Andric 11412754fe60SDimitry Andric // At this point, the live intervals in Edit contain VNInfos corresponding to 11422754fe60SDimitry Andric // the inserted copies. 11432754fe60SDimitry Andric 11442754fe60SDimitry Andric // Add the original defs from the parent interval. 114539d628a0SDimitry Andric for (const VNInfo *ParentVNI : Edit->getParent().valnos) { 11462754fe60SDimitry Andric if (ParentVNI->isUnused()) 11472754fe60SDimitry Andric continue; 11483b0f4066SDimitry Andric unsigned RegIdx = RegAssign.lookup(ParentVNI->def); 11497ae0e2c9SDimitry Andric defValue(RegIdx, ParentVNI, ParentVNI->def); 11503b0f4066SDimitry Andric 11516122f3e6SDimitry Andric // Force rematted values to be recomputed everywhere. 11523b0f4066SDimitry Andric // The new live ranges may be truncated. 11533b0f4066SDimitry Andric if (Edit->didRematerialize(ParentVNI)) 11543b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 11556122f3e6SDimitry Andric forceRecompute(i, ParentVNI); 11566122f3e6SDimitry Andric } 11576122f3e6SDimitry Andric 11586122f3e6SDimitry Andric // Hoist back-copies to the complement interval when in spill mode. 11596122f3e6SDimitry Andric switch (SpillMode) { 11606122f3e6SDimitry Andric case SM_Partition: 11616122f3e6SDimitry Andric // Leave all back-copies as is. 11626122f3e6SDimitry Andric break; 11636122f3e6SDimitry Andric case SM_Size: 11646122f3e6SDimitry Andric case SM_Speed: 11653ca95b02SDimitry Andric // hoistCopies will behave differently between size and speed. 11663ca95b02SDimitry Andric hoistCopies(); 11672754fe60SDimitry Andric } 11682754fe60SDimitry Andric 11693b0f4066SDimitry Andric // Transfer the simply mapped values, check if any are skipped. 11703b0f4066SDimitry Andric bool Skipped = transferValues(); 11713ca95b02SDimitry Andric 11723ca95b02SDimitry Andric // Rewrite virtual registers, possibly extending ranges. 11733ca95b02SDimitry Andric rewriteAssigned(Skipped); 11743ca95b02SDimitry Andric 11753b0f4066SDimitry Andric if (Skipped) 11763b0f4066SDimitry Andric extendPHIKillRanges(); 11772754fe60SDimitry Andric else 11783b0f4066SDimitry Andric ++NumSimple; 11792754fe60SDimitry Andric 11803b0f4066SDimitry Andric // Delete defs that were rematted everywhere. 11813b0f4066SDimitry Andric if (Skipped) 11823b0f4066SDimitry Andric deleteRematVictims(); 11832754fe60SDimitry Andric 11842754fe60SDimitry Andric // Get rid of unused values and set phi-kill flags. 1185f785676fSDimitry Andric for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) { 1186f785676fSDimitry Andric LiveInterval &LI = LIS.getInterval(*I); 1187f785676fSDimitry Andric LI.RenumberValues(); 1188f785676fSDimitry Andric } 11892754fe60SDimitry Andric 11903b0f4066SDimitry Andric // Provide a reverse mapping from original indices to Edit ranges. 11913b0f4066SDimitry Andric if (LRMap) { 11923b0f4066SDimitry Andric LRMap->clear(); 11933b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) 11943b0f4066SDimitry Andric LRMap->push_back(i); 11953b0f4066SDimitry Andric } 11963b0f4066SDimitry Andric 11972754fe60SDimitry Andric // Now check if any registers were separated into multiple components. 11982754fe60SDimitry Andric ConnectedVNInfoEqClasses ConEQ(LIS); 11993b0f4066SDimitry Andric for (unsigned i = 0, e = Edit->size(); i != e; ++i) { 12002754fe60SDimitry Andric // Don't use iterators, they are invalidated by create() below. 12017d523365SDimitry Andric unsigned VReg = Edit->get(i); 12027d523365SDimitry Andric LiveInterval &LI = LIS.getInterval(VReg); 12037d523365SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs; 12047d523365SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs); 12057d523365SDimitry Andric unsigned Original = VRM.getOriginal(VReg); 12067d523365SDimitry Andric for (LiveInterval *SplitLI : SplitLIs) 12077d523365SDimitry Andric VRM.setIsSplitFromReg(SplitLI->reg, Original); 12087d523365SDimitry Andric 12093b0f4066SDimitry Andric // The new intervals all map back to i. 12103b0f4066SDimitry Andric if (LRMap) 12113b0f4066SDimitry Andric LRMap->resize(Edit->size(), i); 12122754fe60SDimitry Andric } 12132754fe60SDimitry Andric 1214e580952dSDimitry Andric // Calculate spill weight and allocation hints for new intervals. 1215f785676fSDimitry Andric Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI); 12163b0f4066SDimitry Andric 12173b0f4066SDimitry Andric assert(!LRMap || LRMap->size() == Edit->size()); 1218e580952dSDimitry Andric } 1219e580952dSDimitry Andric 1220e580952dSDimitry Andric 1221e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1222e580952dSDimitry Andric // Single Block Splitting 1223e580952dSDimitry Andric //===----------------------------------------------------------------------===// 1224e580952dSDimitry Andric 12256122f3e6SDimitry Andric bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI, 12266122f3e6SDimitry Andric bool SingleInstrs) const { 12276122f3e6SDimitry Andric // Always split for multiple instructions. 12286122f3e6SDimitry Andric if (!BI.isOneInstr()) 12296122f3e6SDimitry Andric return true; 12306122f3e6SDimitry Andric // Don't split for single instructions unless explicitly requested. 12316122f3e6SDimitry Andric if (!SingleInstrs) 12322754fe60SDimitry Andric return false; 12336122f3e6SDimitry Andric // Splitting a live-through range always makes progress. 12346122f3e6SDimitry Andric if (BI.LiveIn && BI.LiveOut) 12356122f3e6SDimitry Andric return true; 12366122f3e6SDimitry Andric // No point in isolating a copy. It has no register class constraints. 12376122f3e6SDimitry Andric if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike()) 12386122f3e6SDimitry Andric return false; 12396122f3e6SDimitry Andric // Finally, don't isolate an end point that was created by earlier splits. 12406122f3e6SDimitry Andric return isOriginalEndpoint(BI.FirstInstr); 1241e580952dSDimitry Andric } 1242e580952dSDimitry Andric 12433b0f4066SDimitry Andric void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) { 12443b0f4066SDimitry Andric openIntv(); 12453b0f4066SDimitry Andric SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber()); 12466122f3e6SDimitry Andric SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr, 12473b0f4066SDimitry Andric LastSplitPoint)); 12486122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) { 12496122f3e6SDimitry Andric useIntv(SegStart, leaveIntvAfter(BI.LastInstr)); 12503b0f4066SDimitry Andric } else { 12513b0f4066SDimitry Andric // The last use is after the last valid split point. 12523b0f4066SDimitry Andric SlotIndex SegStop = leaveIntvBefore(LastSplitPoint); 12533b0f4066SDimitry Andric useIntv(SegStart, SegStop); 12546122f3e6SDimitry Andric overlapIntv(SegStop, BI.LastInstr); 12553b0f4066SDimitry Andric } 12563b0f4066SDimitry Andric } 12573b0f4066SDimitry Andric 125817a519f9SDimitry Andric 125917a519f9SDimitry Andric //===----------------------------------------------------------------------===// 126017a519f9SDimitry Andric // Global Live Range Splitting Support 126117a519f9SDimitry Andric //===----------------------------------------------------------------------===// 126217a519f9SDimitry Andric 126317a519f9SDimitry Andric // These methods support a method of global live range splitting that uses a 126417a519f9SDimitry Andric // global algorithm to decide intervals for CFG edges. They will insert split 126517a519f9SDimitry Andric // points and color intervals in basic blocks while avoiding interference. 126617a519f9SDimitry Andric // 126717a519f9SDimitry Andric // Note that splitSingleBlock is also useful for blocks where both CFG edges 126817a519f9SDimitry Andric // are on the stack. 126917a519f9SDimitry Andric 127017a519f9SDimitry Andric void SplitEditor::splitLiveThroughBlock(unsigned MBBNum, 127117a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore, 127217a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter){ 127317a519f9SDimitry Andric SlotIndex Start, Stop; 127491bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum); 127517a519f9SDimitry Andric 127617a519f9SDimitry Andric DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop 127717a519f9SDimitry Andric << ") intf " << LeaveBefore << '-' << EnterAfter 127817a519f9SDimitry Andric << ", live-through " << IntvIn << " -> " << IntvOut); 127917a519f9SDimitry Andric 128017a519f9SDimitry Andric assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks"); 128117a519f9SDimitry Andric 12826122f3e6SDimitry Andric assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block"); 12836122f3e6SDimitry Andric assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf"); 12846122f3e6SDimitry Andric assert((!EnterAfter || EnterAfter >= Start) && "Interference before block"); 12856122f3e6SDimitry Andric 12866122f3e6SDimitry Andric MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum); 12876122f3e6SDimitry Andric 128817a519f9SDimitry Andric if (!IntvOut) { 128917a519f9SDimitry Andric DEBUG(dbgs() << ", spill on entry.\n"); 129017a519f9SDimitry Andric // 129117a519f9SDimitry Andric // <<<<<<<<< Possible LeaveBefore interference. 129217a519f9SDimitry Andric // |-----------| Live through. 129317a519f9SDimitry Andric // -____________ Spill on entry. 129417a519f9SDimitry Andric // 129517a519f9SDimitry Andric selectIntv(IntvIn); 129617a519f9SDimitry Andric SlotIndex Idx = leaveIntvAtTop(*MBB); 129717a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 129817a519f9SDimitry Andric (void)Idx; 129917a519f9SDimitry Andric return; 130017a519f9SDimitry Andric } 130117a519f9SDimitry Andric 130217a519f9SDimitry Andric if (!IntvIn) { 130317a519f9SDimitry Andric DEBUG(dbgs() << ", reload on exit.\n"); 130417a519f9SDimitry Andric // 130517a519f9SDimitry Andric // >>>>>>> Possible EnterAfter interference. 130617a519f9SDimitry Andric // |-----------| Live through. 130717a519f9SDimitry Andric // ___________-- Reload on exit. 130817a519f9SDimitry Andric // 130917a519f9SDimitry Andric selectIntv(IntvOut); 131017a519f9SDimitry Andric SlotIndex Idx = enterIntvAtEnd(*MBB); 131117a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 131217a519f9SDimitry Andric (void)Idx; 131317a519f9SDimitry Andric return; 131417a519f9SDimitry Andric } 131517a519f9SDimitry Andric 131617a519f9SDimitry Andric if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) { 131717a519f9SDimitry Andric DEBUG(dbgs() << ", straight through.\n"); 131817a519f9SDimitry Andric // 131917a519f9SDimitry Andric // |-----------| Live through. 132017a519f9SDimitry Andric // ------------- Straight through, same intv, no interference. 132117a519f9SDimitry Andric // 132217a519f9SDimitry Andric selectIntv(IntvOut); 132317a519f9SDimitry Andric useIntv(Start, Stop); 132417a519f9SDimitry Andric return; 132517a519f9SDimitry Andric } 132617a519f9SDimitry Andric 132717a519f9SDimitry Andric // We cannot legally insert splits after LSP. 132817a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(MBBNum); 13296122f3e6SDimitry Andric assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf"); 133017a519f9SDimitry Andric 133117a519f9SDimitry Andric if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter || 133217a519f9SDimitry Andric LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) { 133317a519f9SDimitry Andric DEBUG(dbgs() << ", switch avoiding interference.\n"); 133417a519f9SDimitry Andric // 133517a519f9SDimitry Andric // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference. 133617a519f9SDimitry Andric // |-----------| Live through. 133717a519f9SDimitry Andric // ------======= Switch intervals between interference. 133817a519f9SDimitry Andric // 133917a519f9SDimitry Andric selectIntv(IntvOut); 13406122f3e6SDimitry Andric SlotIndex Idx; 13416122f3e6SDimitry Andric if (LeaveBefore && LeaveBefore < LSP) { 13426122f3e6SDimitry Andric Idx = enterIntvBefore(LeaveBefore); 134317a519f9SDimitry Andric useIntv(Idx, Stop); 13446122f3e6SDimitry Andric } else { 13456122f3e6SDimitry Andric Idx = enterIntvAtEnd(*MBB); 13466122f3e6SDimitry Andric } 134717a519f9SDimitry Andric selectIntv(IntvIn); 134817a519f9SDimitry Andric useIntv(Start, Idx); 134917a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 135017a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 135117a519f9SDimitry Andric return; 135217a519f9SDimitry Andric } 135317a519f9SDimitry Andric 135417a519f9SDimitry Andric DEBUG(dbgs() << ", create local intv for interference.\n"); 135517a519f9SDimitry Andric // 135617a519f9SDimitry Andric // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference. 135717a519f9SDimitry Andric // |-----------| Live through. 135817a519f9SDimitry Andric // ==---------== Switch intervals before/after interference. 135917a519f9SDimitry Andric // 136017a519f9SDimitry Andric assert(LeaveBefore <= EnterAfter && "Missed case"); 136117a519f9SDimitry Andric 136217a519f9SDimitry Andric selectIntv(IntvOut); 136317a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter); 136417a519f9SDimitry Andric useIntv(Idx, Stop); 136517a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 136617a519f9SDimitry Andric 136717a519f9SDimitry Andric selectIntv(IntvIn); 136817a519f9SDimitry Andric Idx = leaveIntvBefore(LeaveBefore); 136917a519f9SDimitry Andric useIntv(Start, Idx); 137017a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 137117a519f9SDimitry Andric } 137217a519f9SDimitry Andric 137317a519f9SDimitry Andric 137417a519f9SDimitry Andric void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI, 137517a519f9SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore) { 137617a519f9SDimitry Andric SlotIndex Start, Stop; 137791bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 137817a519f9SDimitry Andric 137917a519f9SDimitry Andric DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop 13806122f3e6SDimitry Andric << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 138117a519f9SDimitry Andric << ", reg-in " << IntvIn << ", leave before " << LeaveBefore 138217a519f9SDimitry Andric << (BI.LiveOut ? ", stack-out" : ", killed in block")); 138317a519f9SDimitry Andric 138417a519f9SDimitry Andric assert(IntvIn && "Must have register in"); 138517a519f9SDimitry Andric assert(BI.LiveIn && "Must be live-in"); 138617a519f9SDimitry Andric assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference"); 138717a519f9SDimitry Andric 13886122f3e6SDimitry Andric if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) { 138917a519f9SDimitry Andric DEBUG(dbgs() << " before interference.\n"); 139017a519f9SDimitry Andric // 139117a519f9SDimitry Andric // <<< Interference after kill. 139217a519f9SDimitry Andric // |---o---x | Killed in block. 139317a519f9SDimitry Andric // ========= Use IntvIn everywhere. 139417a519f9SDimitry Andric // 139517a519f9SDimitry Andric selectIntv(IntvIn); 13966122f3e6SDimitry Andric useIntv(Start, BI.LastInstr); 139717a519f9SDimitry Andric return; 139817a519f9SDimitry Andric } 139917a519f9SDimitry Andric 140017a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 140117a519f9SDimitry Andric 14026122f3e6SDimitry Andric if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) { 140317a519f9SDimitry Andric // 140417a519f9SDimitry Andric // <<< Possible interference after last use. 140517a519f9SDimitry Andric // |---o---o---| Live-out on stack. 140617a519f9SDimitry Andric // =========____ Leave IntvIn after last use. 140717a519f9SDimitry Andric // 140817a519f9SDimitry Andric // < Interference after last use. 140917a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use. 141017a519f9SDimitry Andric // ============ Copy to stack after LSP, overlap IntvIn. 141117a519f9SDimitry Andric // \_____ Stack interval is live-out. 141217a519f9SDimitry Andric // 14136122f3e6SDimitry Andric if (BI.LastInstr < LSP) { 141417a519f9SDimitry Andric DEBUG(dbgs() << ", spill after last use before interference.\n"); 141517a519f9SDimitry Andric selectIntv(IntvIn); 14166122f3e6SDimitry Andric SlotIndex Idx = leaveIntvAfter(BI.LastInstr); 141717a519f9SDimitry Andric useIntv(Start, Idx); 141817a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 141917a519f9SDimitry Andric } else { 142017a519f9SDimitry Andric DEBUG(dbgs() << ", spill before last split point.\n"); 142117a519f9SDimitry Andric selectIntv(IntvIn); 142217a519f9SDimitry Andric SlotIndex Idx = leaveIntvBefore(LSP); 14236122f3e6SDimitry Andric overlapIntv(Idx, BI.LastInstr); 142417a519f9SDimitry Andric useIntv(Start, Idx); 142517a519f9SDimitry Andric assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference"); 142617a519f9SDimitry Andric } 142717a519f9SDimitry Andric return; 142817a519f9SDimitry Andric } 142917a519f9SDimitry Andric 143017a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvIn. That 143117a519f9SDimitry Andric // means we need to create a local interval that can be allocated a 143217a519f9SDimitry Andric // different register. 143317a519f9SDimitry Andric unsigned LocalIntv = openIntv(); 143417a519f9SDimitry Andric (void)LocalIntv; 143517a519f9SDimitry Andric DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n"); 143617a519f9SDimitry Andric 14376122f3e6SDimitry Andric if (!BI.LiveOut || BI.LastInstr < LSP) { 143817a519f9SDimitry Andric // 143917a519f9SDimitry Andric // <<<<<<< Interference overlapping uses. 144017a519f9SDimitry Andric // |---o---o---| Live-out on stack. 144117a519f9SDimitry Andric // =====----____ Leave IntvIn before interference, then spill. 144217a519f9SDimitry Andric // 14436122f3e6SDimitry Andric SlotIndex To = leaveIntvAfter(BI.LastInstr); 144417a519f9SDimitry Andric SlotIndex From = enterIntvBefore(LeaveBefore); 144517a519f9SDimitry Andric useIntv(From, To); 144617a519f9SDimitry Andric selectIntv(IntvIn); 144717a519f9SDimitry Andric useIntv(Start, From); 144817a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 144917a519f9SDimitry Andric return; 145017a519f9SDimitry Andric } 145117a519f9SDimitry Andric 145217a519f9SDimitry Andric // <<<<<<< Interference overlapping uses. 145317a519f9SDimitry Andric // |---o---o--o| Live-out on stack, late last use. 145417a519f9SDimitry Andric // =====------- Copy to stack before LSP, overlap LocalIntv. 145517a519f9SDimitry Andric // \_____ Stack interval is live-out. 145617a519f9SDimitry Andric // 145717a519f9SDimitry Andric SlotIndex To = leaveIntvBefore(LSP); 14586122f3e6SDimitry Andric overlapIntv(To, BI.LastInstr); 145917a519f9SDimitry Andric SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore)); 146017a519f9SDimitry Andric useIntv(From, To); 146117a519f9SDimitry Andric selectIntv(IntvIn); 146217a519f9SDimitry Andric useIntv(Start, From); 146317a519f9SDimitry Andric assert((!LeaveBefore || From <= LeaveBefore) && "Interference"); 146417a519f9SDimitry Andric } 146517a519f9SDimitry Andric 146617a519f9SDimitry Andric void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, 146717a519f9SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter) { 146817a519f9SDimitry Andric SlotIndex Start, Stop; 146991bc56edSDimitry Andric std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB); 147017a519f9SDimitry Andric 147117a519f9SDimitry Andric DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop 14726122f3e6SDimitry Andric << "), uses " << BI.FirstInstr << '-' << BI.LastInstr 147317a519f9SDimitry Andric << ", reg-out " << IntvOut << ", enter after " << EnterAfter 147417a519f9SDimitry Andric << (BI.LiveIn ? ", stack-in" : ", defined in block")); 147517a519f9SDimitry Andric 147617a519f9SDimitry Andric SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber()); 147717a519f9SDimitry Andric 147817a519f9SDimitry Andric assert(IntvOut && "Must have register out"); 147917a519f9SDimitry Andric assert(BI.LiveOut && "Must be live-out"); 148017a519f9SDimitry Andric assert((!EnterAfter || EnterAfter < LSP) && "Bad interference"); 148117a519f9SDimitry Andric 14826122f3e6SDimitry Andric if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) { 148317a519f9SDimitry Andric DEBUG(dbgs() << " after interference.\n"); 148417a519f9SDimitry Andric // 148517a519f9SDimitry Andric // >>>> Interference before def. 148617a519f9SDimitry Andric // | o---o---| Defined in block. 148717a519f9SDimitry Andric // ========= Use IntvOut everywhere. 148817a519f9SDimitry Andric // 148917a519f9SDimitry Andric selectIntv(IntvOut); 14906122f3e6SDimitry Andric useIntv(BI.FirstInstr, Stop); 149117a519f9SDimitry Andric return; 149217a519f9SDimitry Andric } 149317a519f9SDimitry Andric 14946122f3e6SDimitry Andric if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) { 149517a519f9SDimitry Andric DEBUG(dbgs() << ", reload after interference.\n"); 149617a519f9SDimitry Andric // 149717a519f9SDimitry Andric // >>>> Interference before def. 149817a519f9SDimitry Andric // |---o---o---| Live-through, stack-in. 149917a519f9SDimitry Andric // ____========= Enter IntvOut before first use. 150017a519f9SDimitry Andric // 150117a519f9SDimitry Andric selectIntv(IntvOut); 15026122f3e6SDimitry Andric SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr)); 150317a519f9SDimitry Andric useIntv(Idx, Stop); 150417a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 150517a519f9SDimitry Andric return; 150617a519f9SDimitry Andric } 150717a519f9SDimitry Andric 150817a519f9SDimitry Andric // The interference is overlapping somewhere we wanted to use IntvOut. That 150917a519f9SDimitry Andric // means we need to create a local interval that can be allocated a 151017a519f9SDimitry Andric // different register. 151117a519f9SDimitry Andric DEBUG(dbgs() << ", interference overlaps uses.\n"); 151217a519f9SDimitry Andric // 151317a519f9SDimitry Andric // >>>>>>> Interference overlapping uses. 151417a519f9SDimitry Andric // |---o---o---| Live-through, stack-in. 151517a519f9SDimitry Andric // ____---====== Create local interval for interference range. 151617a519f9SDimitry Andric // 151717a519f9SDimitry Andric selectIntv(IntvOut); 151817a519f9SDimitry Andric SlotIndex Idx = enterIntvAfter(EnterAfter); 151917a519f9SDimitry Andric useIntv(Idx, Stop); 152017a519f9SDimitry Andric assert((!EnterAfter || Idx >= EnterAfter) && "Interference"); 152117a519f9SDimitry Andric 152217a519f9SDimitry Andric openIntv(); 15236122f3e6SDimitry Andric SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr)); 152417a519f9SDimitry Andric useIntv(From, Idx); 152517a519f9SDimitry Andric } 1526